Presentation on doubly linked list:- Presented by:- RITU NAMDEO M.C.A. 2 ND  SEM ‘ B’ SEC. MITM INDORE
Doubly linked list Doubly-linked list(DLL) is a more sophisticated kind of linked list. Each has three parts ,two links parts and one data parts. One points to previous node and one points to next node. There are two pointer variable head and tail. Head points to the first element of list and tail points last element of list.
REPRESENTATION OF DLL PPPP D  D P  D N  D Head Tail
DLL OPERATIONS: INITIALIZING CREATING A NODE
Initializing   Tail Head
Code initialization  >>.. initialize(node **head,node **tail){   *head=NULL;   *tail=NULL;   }
Creating a node data   ptr tail
Function for creating a node node* create(int item){   node *ptr;   ptr=(node*)malloc(sizeof(node));   ptr->item=item;   ptr->prev=NULL;   ptr->next=NULL;   return ptr;
OPERATIONS ON DLL INSERTION  At beginning of list At end of list After a specified node Before a specified node Deletion At beginning of list At end of list After a specified node Before a specified node Traversing
Insertion at begining Check for list is empty or not. insrt_beg(node **head,node **tail,itemtype item) { node *ptr; ptr=create(item); if(*head==NULL&&*tail==NULL){ *head=ptr; *tail=ptr; } Head Tail
Insertion at beginning   Before insertion  20 10    30 Head Tail
new node  2  ptr tail
Insertion 2  ptr tail 20 10    30 Head Tail
Insertion of new node 10 2    20 Head Tail 30
Code:- function  insrt_beg(node **head,node **tail,itemtype item) { node *ptr; ptr=create(item); if(*head==NULL&&*tail==NULL){ *head=ptr; *tail=ptr; } else{   (*head)->prev=ptr;   ptr->next=*head;   *head=ptr;   ptr==NULL; }   }
Insert at last of list new node before insertion 40  ptr tail
40  ptr tail 20 10    30 Head Tail
After insertion new node 20 10    30 Head Tail 40
Code for this operation:- insrt(node ** head,node **tail,itemtype item){ node *ptr; ptr=create(item); if(*head==NULL&& *tail==NULL){ *head=ptr; *tail=ptr; } else{   ptr->prev=*tail;   (*tail)->next=ptr;   *tail=ptr;   ptr=NULL;   } }
Insertion before a specified node:- new node 40  ptr tail
Inserting new node Inserting before 30 20 10    40 Head Tail 30
 
function insrt_bfr_specify(node **head,node **tail,itemtype item,itemtype sitem) { node *ptr,*temp,*temp1 ptr=create(item); temp=*head; if(*head==NULL&&*tail==NULL){ *head=ptr; *tail=ptr; } else{ while(temp->item!=sitem&&temp->next!=NULL) { temp=temp->next;
Cont. }   if(temp==NULL)   printf("item was not found");   else {   temp1=temp->prev;   ptr->prev=temp1->next; temp1->next=ptr;   ptr->next=temp;   temp->prev=ptr;   ptr=NULL;   } }
After a specified node New node 40  ptr tail
Inserting after 20: 20 10    40 Head Tail 30
Code :- insrt_aftr_specify(node **head,node **tail,itemtype item,itemtype sitem){ node *ptr,*temp,*temp1; ptr=create(item); temp=*head; if(*head==NULL&&*tail==NULL){ *head=ptr; *tail=ptr; } else{ while(temp->item!=sitem&&temp->next!=NULL) { temp=temp->next;
Cont. }   if(temp==NULL)   printf("item was not found");   else {   temp1=temp->next;   ptr->prev=temp;   ptr->next=temp->next;   temp1->prev=ptr;   temp->next=ptr;   ptr=NULL;   } } }
Deletion in DLL Deletion at begining case :-if list is empty. Head Tail
function deletion_at_beg(node **head){ node *temp; temp=*head; head=temp->next; (*head)->prev=NULL; free(temp); }
Deleting first node 10 2    20 Head Tail 30
10 20 Head Tail 30
Traversing 10 Head 20 30   Tail
fuction show(node *q) { while(q!=NULL) { printf("%d-",q->item); q=q->next; } }
Code:  main function void main() { int item,sitem,ch; char c; node *head,*tail; clrscr(); initialize(&head,&tail); do { ch=list(); switch(ch){
case 1: printf("please enter the item"); scanf("%d",&item); insrt(&head,&tail,item); show(head); break;
case 2:   printf("please enter the item");   scanf("%d",&item);   insrt_beg(&head,&tail,item);   show(head);   break;
Cont. case 3: printf("please enter the item"); scanf("%d",&item); show(head); printf("please enter the specify item"); scanf("%d",&sitem); insrt_bfr_specify(&head,&tail,item,sitem); show(head); break;
Cont. case 4: printf("please enter the item"); scanf("%d",&item); show(head); printf("please enter the specify item"); scanf("%d",&sitem); insrt_aftr_specify(&head,&tail,item,sitem); show(head); break;
Cont. case 5: show(head); deletion_at_beg(&head); show(head); break; default: printf("no choice"); } printf("\n\ndo u want to continue y\n"); scanf("\n%c",&c); }while(c=='y'||c=='Y'); getch(); }
Display function list() { int k; printf("\nchoose 1* for insert value in the list"); printf("\nchoose 2* for insert at begiging  in the list"); printf("\nchoose 3* for insert before specified node inthe list"); printf("\nchoose 4* for insert after specified node inhe list"); printf("\nchoose 5* for delete begining from the list");   scanf("\n%d",&k);   return(k); }
 

Doublylinklist

  • 1.
    Presentation on doublylinked list:- Presented by:- RITU NAMDEO M.C.A. 2 ND SEM ‘ B’ SEC. MITM INDORE
  • 2.
    Doubly linked listDoubly-linked list(DLL) is a more sophisticated kind of linked list. Each has three parts ,two links parts and one data parts. One points to previous node and one points to next node. There are two pointer variable head and tail. Head points to the first element of list and tail points last element of list.
  • 3.
    REPRESENTATION OF DLLPPPP D D P D N D Head Tail
  • 4.
  • 5.
    Initializing Tail Head
  • 6.
    Code initialization >>.. initialize(node **head,node **tail){ *head=NULL; *tail=NULL; }
  • 7.
    Creating a nodedata ptr tail
  • 8.
    Function for creatinga node node* create(int item){ node *ptr; ptr=(node*)malloc(sizeof(node)); ptr->item=item; ptr->prev=NULL; ptr->next=NULL; return ptr;
  • 9.
    OPERATIONS ON DLLINSERTION At beginning of list At end of list After a specified node Before a specified node Deletion At beginning of list At end of list After a specified node Before a specified node Traversing
  • 10.
    Insertion at beginingCheck for list is empty or not. insrt_beg(node **head,node **tail,itemtype item) { node *ptr; ptr=create(item); if(*head==NULL&&*tail==NULL){ *head=ptr; *tail=ptr; } Head Tail
  • 11.
    Insertion at beginning Before insertion 20 10 30 Head Tail
  • 12.
    new node 2 ptr tail
  • 13.
    Insertion 2 ptr tail 20 10 30 Head Tail
  • 14.
    Insertion of newnode 10 2 20 Head Tail 30
  • 15.
    Code:- function insrt_beg(node **head,node **tail,itemtype item) { node *ptr; ptr=create(item); if(*head==NULL&&*tail==NULL){ *head=ptr; *tail=ptr; } else{ (*head)->prev=ptr; ptr->next=*head; *head=ptr; ptr==NULL; } }
  • 16.
    Insert at lastof list new node before insertion 40 ptr tail
  • 17.
    40 ptrtail 20 10 30 Head Tail
  • 18.
    After insertion newnode 20 10 30 Head Tail 40
  • 19.
    Code for thisoperation:- insrt(node ** head,node **tail,itemtype item){ node *ptr; ptr=create(item); if(*head==NULL&& *tail==NULL){ *head=ptr; *tail=ptr; } else{ ptr->prev=*tail; (*tail)->next=ptr; *tail=ptr; ptr=NULL; } }
  • 20.
    Insertion before aspecified node:- new node 40 ptr tail
  • 21.
    Inserting new nodeInserting before 30 20 10 40 Head Tail 30
  • 22.
  • 23.
    function insrt_bfr_specify(node **head,node**tail,itemtype item,itemtype sitem) { node *ptr,*temp,*temp1 ptr=create(item); temp=*head; if(*head==NULL&&*tail==NULL){ *head=ptr; *tail=ptr; } else{ while(temp->item!=sitem&&temp->next!=NULL) { temp=temp->next;
  • 24.
    Cont. } if(temp==NULL) printf("item was not found"); else { temp1=temp->prev; ptr->prev=temp1->next; temp1->next=ptr; ptr->next=temp; temp->prev=ptr; ptr=NULL; } }
  • 25.
    After a specifiednode New node 40 ptr tail
  • 26.
    Inserting after 20:20 10 40 Head Tail 30
  • 27.
    Code :- insrt_aftr_specify(node**head,node **tail,itemtype item,itemtype sitem){ node *ptr,*temp,*temp1; ptr=create(item); temp=*head; if(*head==NULL&&*tail==NULL){ *head=ptr; *tail=ptr; } else{ while(temp->item!=sitem&&temp->next!=NULL) { temp=temp->next;
  • 28.
    Cont. } if(temp==NULL) printf("item was not found"); else { temp1=temp->next; ptr->prev=temp; ptr->next=temp->next; temp1->prev=ptr; temp->next=ptr; ptr=NULL; } } }
  • 29.
    Deletion in DLLDeletion at begining case :-if list is empty. Head Tail
  • 30.
    function deletion_at_beg(node **head){node *temp; temp=*head; head=temp->next; (*head)->prev=NULL; free(temp); }
  • 31.
    Deleting first node10 2 20 Head Tail 30
  • 32.
    10 20 HeadTail 30
  • 33.
  • 34.
    fuction show(node *q){ while(q!=NULL) { printf("%d-",q->item); q=q->next; } }
  • 35.
    Code: mainfunction void main() { int item,sitem,ch; char c; node *head,*tail; clrscr(); initialize(&head,&tail); do { ch=list(); switch(ch){
  • 36.
    case 1: printf("pleaseenter the item"); scanf("%d",&item); insrt(&head,&tail,item); show(head); break;
  • 37.
    case 2: printf("please enter the item"); scanf("%d",&item); insrt_beg(&head,&tail,item); show(head); break;
  • 38.
    Cont. case 3:printf("please enter the item"); scanf("%d",&item); show(head); printf("please enter the specify item"); scanf("%d",&sitem); insrt_bfr_specify(&head,&tail,item,sitem); show(head); break;
  • 39.
    Cont. case 4:printf("please enter the item"); scanf("%d",&item); show(head); printf("please enter the specify item"); scanf("%d",&sitem); insrt_aftr_specify(&head,&tail,item,sitem); show(head); break;
  • 40.
    Cont. case 5:show(head); deletion_at_beg(&head); show(head); break; default: printf("no choice"); } printf("\n\ndo u want to continue y\n"); scanf("\n%c",&c); }while(c=='y'||c=='Y'); getch(); }
  • 41.
    Display function list(){ int k; printf("\nchoose 1* for insert value in the list"); printf("\nchoose 2* for insert at begiging in the list"); printf("\nchoose 3* for insert before specified node inthe list"); printf("\nchoose 4* for insert after specified node inhe list"); printf("\nchoose 5* for delete begining from the list"); scanf("\n%d",&k); return(k); }
  • 42.