Professional Documents
Culture Documents
Doubly Linked List
Doubly Linked List
elem
node
Each node references both its predecessor and its successor The Head pointer points to the first node in the List The Tail pointer Points to the last node in the List
3
TAIL
Note: the operations on a doubly-linked list are exactly the same that are required for a singly-linked list.
6
start
Traversing
NULL data next prev data next prev data NULL
while(ptr!==NULL)
{
ptr=ptr->next; }// traversing start to last node
How will you traverse last node to start? Ans:- using tail pointer
Current node pointer (pCur) can be in one of two states: it can contain the address of a node (i.e. you are adding somewhere after the first node in the middle or at the end) it can be NULL (i.e. you are adding either to an empty list or at the beginning of the list)
8
Code:
/*create node*/
malloc(sizeof(struct dllnode)); pNew -> data = 39; pCur pNew -> right = pHead; pNew -> left = pHead;
pHead = pNew;
pNew pHead 39
pCur
After:
start
Inserting at beginning
prev data next prev data NULL 1 next
1. tmp->next=start;
2. start->prev=tmp; 3. start=temp;
4. tmp->prev=NULL;
10
Insertion
We visualize operation insertAfter(q, X), which returns position q
A q A q A
tmp X tmp
C
11
start
Insertion in between
q NULL data
next
prev
data
next
prev
data NULL
NULL data
next
tmp
3. tmp->prev=q;
4. q->next = tmp;
12
Insertion in between
start
q NULL data
next
prev 1
data
next
prev
data NULL
NULL data
tmp
Insertion in between
start
q NULL data
next
prev 1
data
next 2 next
prev
data NULL
NULL data
tmp
1. q->next->prev = tmp; 2. tmp->next = q->next; 3. tmp->prev=q; 4. q->next = tmp;
14
Insertion in between
start
q NULL data
next
prev 1 prev
data
next 2
prev
data NULL
data
tmp
Insertion in between
start
q NULL data
next
prev 1
data
next 2 next
prev
data NULL
NULL data
tmp
1. q->next->prev = tmp; 4 2. tmp->next = q->next; 3. tmp->prev=q; 4. q->next = tmp;
16
Insertion at end
1. tmp->prev=q; 2. q->next=tmp; 3. tmp->next=NULL;
start q NULL data next prev data next prev 2 data NULL
null 1
data tmp
null
17
//insert a node into a linked list struct node *pNew; pNew = (struct node *) malloc(sizeof(struct node)); pNew -> data = item; if (q == NULL){ //add before first logical node or to an empty list pNew -> left = null; pNew -> right = start; start = pNew; } else { if (q -> right == NULL) { //add at the end pNew -> left = q; pNew -> right = q -> right; q -> right = pNew; }
code
18
Code..
else { //add in the middle q -> right->left = pNew; pNew -> right = q -> right; pNew -> left = q; q -> right = pNew; } } }
19
21
start
22
start
next
prev
data
next
prev
data NULL
23
start
next
NULL data
next
prev
data NULL
24
Figure 5-28
Reference changes for deletion
25
start
Deletion in between
NULL data next prev tmp data next prev data NULL
start
Deletion in between
NULL data next prev tmp data next 2 prev data NULL
start
Deletion in between
3 tmp data next 2 NULL data next prev prev data NULL
start
Deletion in between
3 NULL data next 2 prev data NULL
Deletion
We visualize remove(p), where p == last() p D
A A
B B
C C
D
A B C
30
NULL data
next
prev
data
next
If node to be deleted is last node of DLL then we will just free the last node and next part of second last node will be NULL:
tmp = q->next;// q is second last node free(tmp); q->next = NULL;
31
NULL data
next
prev
data
next
32
33
34
Figure 5-27
a) A circular doubly linked list with a dummy head node; b) an empty list with a dummy head node
35
Circular DLL
Insertion and Deletion at middle operations are same as that of simple DLL What are the operation for Insertion/Deletion in front or last.
Insertion at beginning or last is same since list is circular Similarly deletion at beginning or last is same since list is circular
36
38
39