Doubly LinkedList Deletion at Beginning

Algorithm

INPUT: HEADER IS POINTER TO THE HEADER NODE OF THE LINKEDLIST
OUTPUT: LINKEDLIST WITHOUT THE DELETED ELEMENT
DATA STRUCTURE: DOUBLY LINKEDLIST

STEP 1: START
STEP 2: PTR=HEADER->RLINK
STEP 3: PTR1=PTR->RLINK
STEP 4: HEADER->RLINK=PTR1
STEP 5: PTR->LLINK=HEADER
STEP 6: FREE(PTR)
STEP 7: END

Code

#include <stdio.h>
#include <stdlib.h>
int getnode(int);
void display();
void displayrev();
void deleteatbeg();
int x, pos, n, item;
struct node
{
    int data;
    struct node *llink;
    struct node *rlink;
};
struct node *prev, *head, *tail, *news, *p, *ptr, *ptr1, *ptr2;
struct node *q1;
struct node *header, *header1;
struct node *q, *r, *s;
int main()
{
    int a;
    char ch;
    printf("ENTER THE LIMIT ");
    scanf("%d", &n);
    getnode(n);
    deleteatbeg();
    display(header);
    return 0;
}

void deleteatbeg()
{
    ptr = header->rlink;
    ptr1 = ptr->rlink;
    header->rlink = ptr1;
    ptr1->llink = header;
    free(ptr);
}
void display(struct node *head1)
{
    struct node *ptr;
    ptr = head1->rlink;
    while (ptr != NULL)
    {
        printf("%d ", ptr->data);
        ptr = ptr->rlink;
    }printf("\n");
}
void displayrev()
{
    struct node *ptr;
    ptr = tail;
    printf("reverse traversal ");
    while (ptr != NULL)
    {
        printf("%d ", ptr->data);
        ptr = ptr->llink;
    }printf("\n");
}
int getnode(int n)
{
    header = malloc(sizeof(struct node));
    head = NULL;
    ptr = malloc(sizeof(struct node));
    for (int i = 0; i < n; i++)
    {
        ptr->llink = NULL;
        p = malloc(sizeof(struct node));
        scanf("%d", &p->data);
        ptr->rlink = p;
        p->llink = ptr;
        p->rlink = NULL;
        if (head == NULL)
        {
            head = p;
            p->llink = NULL;
            ptr->llink = head;
        }
        else
        {
            prev->rlink = p;
            p->llink = prev;
        }
        prev = p;
    }
    tail = prev;
    header->rlink = head;
    header->llink = NULL;
    display(header);
    displayrev();
    return 0;
}

Output

ENTER THE LIMIT 5
1
2
3
4
5
1 2 3 4 5 
reverse traversal 5 4 3 2 1 
2 3 4 5 

Leave a Comment

Your email address will not be published. Required fields are marked *