Doubly Linked List C++
Doubly Linked List C++
2
DOUBLY LINKED LIST
A doubly linked list is one in which all nodes are linked together by multiple links which help in
accessing both the successor (next) and predecessor (previous) node for any arbitrary node within
the list. Every nodes in the doubly linked list has three fields: LeftPointer, RightPointer and
DATA. Fig. 5.22 shows a typical doubly linked list.
LPoint will point to the node in the left side (or previous node) that is LPoint will hold the
address of the previous node. RPoint will point to the node in the right side (or nex node) that is
RPoint will hold the address of the next node. DATA will store the information of the node.
1 of 4
Lec.2
Algorithm for creating a doubly linked list (inserting at the end)
1. Input the DATA to be inserted
2. TEMP to create a new node
3. TEMP->info=DATA
4. TEMP->next=NULL
5. if (START is equal to NULL)
5.a. TEMP->prev=NULL
5.b. START=TEMP
5.c. exit
6. else
6.a. HOLD =START
6.b. while(HOLD->next not equal to NULL)
6.b.1 HOLD=HOLD->next
6.c. HOLD->next=TEMP
6.d. TEMP->prev=HOLD
7. exit
Suppose START is the first position in linked list. Let DATA be the element to be
inserted in the new node. POS is the position where the NewNode is to be inserted. TEMP
is a temporary pointer to hold the node address.
1. Input the DATA and POS
2. Initialize TEMP = START; i = 0
2 of 4
Lec.2
3. Repeat the step 4 if (i less than POS) and (TEMP is not equal to NULL)
4. TEMP = TEMP ->next; i = i +1
5. If (TEMP not equal to NULL) and (i equal to POS)
(a) Create a New Node
(b) NewNode -> DATA = DATA
(c) NewNode -> next = TEMP -> next
(d) NewNode -> prev = TEMP
(e) (TEMP -> next) -> prev = NewNode
(f ) TEMP -> next = New Node
6. Else
(a) Display “Position NOT found”
7. Exit
Suppose START is the address of the first node in the linked list. Let DATA be the
Element to be deleted. TEMP, HOLD is the temporary pointer to hold the address of the node.
1. Input the DATA to be deleted
2. if ((START->DATA)is equal to DATA)
(a) TEMP = START
(b) START = STAR->next
(c) START->prev=NULL
(d) set free the node TEMP , which is deleted
(e) Exit
3. HOLD = START
4. while((HOLD->next->next) not equal to NULL)
(a)if (HOLD->next->DATA) equal to DATA)
(a1) TEMP = HOLD->next
(a2)HOLD->next=TEMP->next
(a3) TEMP->next->prev=HOLD
(a4) set free the node TEMP, which is deleted
(a5) Exit
(b) HOLD=HOLD->next
5. if ((HOLD->next->DATA)==DATA)
(a) TEMP=HOLD->next
(b) set free the node TEMP, which is deleted
(c) HOLD->next=NULL
(d) exit
6. Display “DATA not found”
3 of 4
Lec.2
7.Exit
4 of 4