Vous êtes sur la page 1sur 11

Double Linked List In a singly linked list each node contains the address of the next node.

If there is one more field which may contain address of the previous node. Then it is possible to traverse the list in both forward and backward directions. A list where both forward and backward direction is possible should have two link field and such list will be called as Double Linked List. The links are represented by Llink and Rlink used to hold address of previous node and next node respectively. The pictorial representation is as shown below.
5050 1010h 4050 5050h 3050 4050

START=1010h 5050h 4050h Comparison between SLL and DLL SLL DLL

3050h

1. In SLL traversing is possible in only one In DLL traversing is possible in direction. two direction 2. In SLL to delete a specific node address In DLL the address of previous node of previous nodes should be known. Can be obtain with Llink of current node. 3. The SLL needs only single storage field The DLL needs additional storage (two) fields. node of the list. Operations on Double Linked List: The basic fundamental operation that can performed on the Double linked list are 1. 2. 3. 4. Creating a Double Linked List Inserting a node into the Double Linked List Deleting a node from the Double Linked List Display the content of the Double Linked List Creating a Double linked list This part explains how a nodes can be linked together to form a Double linked list. Creation function first create a node and copy the information to the node. Creation function adds a node one by one at a time to create a Double linked list. A Double linked list can be created by two method 1. By adding a node at the front of the existing list or as a first node. 2. By adding a node at end of the existing list or as a last node . By adding a node at the front of the existing list or as a first node 1

Here Double linked list is represented first node by variable START. Consider a node which is created recently represented by the pointer variable NEW as follows. 10 NEW = 1010h Initially if there is no node in the existing in list the START is pointing to NULL then this NEW node is act as first node in the list pointed by variable START. 10 START =NEW = LAST =1010h Create a one more node represented by NEW 20 NEW=0505h Connect this NEW node to the existing node as follows. The following code will gives method of connecting the node in front of existing node or list. New->Rlink = START START->Llink = NEW; START = NEW 20
1010h 0505h 10

START=0505h By adding a node at end of the existing list or as a last node

LAST=1010h

Here Double linked list is represented first node by variable START . Consider a node which is created recently represented by the pointer variable NEW as follows. 10 NEW = 1010h Initially if there is no node in the existing in list the START is pointing to NULL then this NEW node is act as first node in the list pointed by variable START. 10 2

START =NEW = LAST =1010h Create a one more node represented by NEW 20 NEW=0505h Connect this NEW node to the existing node as follows. The following code will gives method of connecting the node in front of existing node or list. New->Rlink = TEMP TEMP->Llink = NEW; TEMP = NEW 10
0505h 1010h 20

START=1010h Write C function to create Double linked List

TEMP=0505h

CREATE() { NODE *NEW,*TEMP; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->llink=NULL; NEW->rlink=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { TEMP->rlink=NEW; NEW->llink=TEMP; TEMP=NEW; } scanf("%d",&ele); } } 3

Inserting a node to a Double linked list This part explains how a insertion can be done to an existing circular singly linked list. There are different type of insertion to an existing circular singly linked list. Inserting a node as first node in an existing Double linked list. Inserting a node as last node in an existing Double linked list. Inserting a node in between list with a given position in existing Double linked list. Inserting a node infront of given information in an existing Double linked list Inserting a node at the back end of an given information in an existing Double linked list. Write C function to insert a node in between the list with a given position in existing Double linked list.

INSERT() { NODE *NEW,*TEMP,*PREV; int ele,pos,i; printf("Enter the element to be insert and position \n"); scanf("%d %d",&ele,&pos); NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->llink=NULL; NEW->rlink=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { if(pos==1) { NEW->rlink=START; START->llink=NEW; START=NEW; } else { TEMP=START; i=1; 4

while((TEMP!=NULL)&&(i<pos)) { PREV=TEMP; TEMP=TEMP->rlink; i++; } if(TEMP==NULL) { PREV->rlink=NEW; NEW->llink=PREV; } else { PREV->rlink=NEW; NEW->llink=PREV; NEW->rlink=TEMP; TEMP->llink=NEW; } } } } Deleting a node from the Double linked list The objective of this function is to delete a node from the list. The deletion means moving pointer variable to next location address logically. In a list any node can be deleted as Deleting a first node in the list Deleting last node in the list Deleting node with given position in the list Deleting a node with a given information existing in the list

Write C function to delete a node in between the list with a given position in existing Double linked list. DELETE() { NODE *NEW,*TEMP,*PREV; int ele,pos,i; printf("Enter the position to be delete \n"); scanf("%d",&pos); if(START==NULL) { printf("DLL is Empty \n"); return(0); } else { if(pos==1) { ele=START->info; START=START->rlink; return(ele); } else { TEMP=START; i=1; while((TEMP!=NULL)&&(i!=pos)) { PREV=TEMP; TEMP=TEMP->rlink; i++; } if(TEMP==NULL) { printf("The given position is not present in the list \n"); return(0); } else { ele=TEMP->info; PREV->rlink=TEMP->rlink; TEMP->rlink->llink=PREV; return(ele); } } }

} Write a C program to Double linked list by using dynamic variable and pointer and perform following operation 1.Insert Insert a node with given position in the list 2.Delete a node with given position 3.Display a content of the list

/* pgm to create Double Linked List*/ /* and insert with given position */ #include <stdio.h> #include <malloc.h> #include <conio.h> struct node { int info; struct node *llink; struct node *rlink; }; typedef struct node NODE; NODE *START=NULL; main() { int choice=0; int l; clrscr(); while(choice!=5) { printf("1.CREATE \n"); printf("2.INSERT \n"); printf("3.DELETE \n"); printf("4.DISPLAY \n"); printf("5 EXIT \n"); printf("Enter the choice \n"); scanf("%d",&choice); switch(choice) { case 1: CREATE(); printf("DLL after creation \n"); DISPLAY(); break; case 2: INSERT(); printf("DLL after insertion is \n"); DISPLAY(); break; case 3: l=DELETE(); printf("Deleted element is %d\n",l); printf("Content of the DLL is\n");

DISPLAY(); break; case 4: printf("Content of the DLL is \n"); DISPLAY(); break; case 5: exit(0); } } CREATE() { NODE *NEW,*TEMP; int ele; printf("Enter the element terminated by -999\n"); scanf("%d",&ele); while(ele != -999) { NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->llink=NULL; NEW->rlink=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { TEMP->rlink=NEW; NEW->llink=TEMP; TEMP=NEW; } scanf("%d",&ele); } } INSERT() { NODE *NEW,*TEMP,*PREV; int ele,pos,i; printf("Enter the element to be insert and position \n"); scanf("%d %d",&ele,&pos); NEW=(NODE *)malloc(sizeof(NODE)); NEW->info=ele; NEW->llink=NULL; NEW->rlink=NULL; if(START==NULL) { START=NEW; TEMP=NEW; } else { if(pos==1) { }

} else { TEMP=START; i=1; while((TEMP!=NULL)&&(i<pos)) { PREV=TEMP; TEMP=TEMP->rlink; i++; } if(TEMP==NULL) { PREV->rlink=NEW; NEW->llink=PREV; } else { PREV->rlink=NEW; NEW->llink=PREV; NEW->rlink=TEMP; TEMP->llink=NEW; } } } } DELETE() { NODE *NEW,*TEMP,*PREV; int ele,pos,i; printf("Enter the position to be delete \n"); scanf("%d",&pos); if(START==NULL) { printf("DLL is Empty \n"); return(0); } else { if(pos==1) { ele=START->info; START=START->rlink; return(ele); } else { TEMP=START; i=1; while((TEMP!=NULL)&&(i!=pos)) { PREV=TEMP;

NEW->rlink=START; START->llink=NEW; START=NEW;

TEMP=TEMP->rlink; i++; } if(TEMP==NULL) { printf("The given position is not present in the list \n"); return(0); } else { ele=TEMP->info; PREV->rlink=TEMP->rlink; TEMP->rlink->llink=PREV; return(ele); }

} } }

DISPLAY() { NODE *TEMP; if(START==NULL) { printf("DLL is Empty \n"); return; } else { TEMP=START; printf("NULL <=> "); while(TEMP!=NULL) { printf("%d <=> ",TEMP->info); TEMP=TEMP->rlink; } printf("NULL \n"); } }

10

11

Vous aimerez peut-être aussi