You are on page 1of 51

Linked List

Doubly Linkedlist
 Pointers exist between adjacent nodes in both directions.
 The list can be traversed from either forward or backward direction.
 Usually two pointers are maintained to keep track of the list, head and tail.

28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

Doubly Linked List


 A doubly linked list provides a natural implementation of the
List ADT

prev

next

 Nodes implement Position and store:


 element
 link to the previous node
 link to the next node

elem

node

 Special trailer and header nodes

header

28-Jul-14

nodes/positions trailer

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

elements

Basic Operations on a Linked List


1. Creating a list.
2. Inserting an element in the list (Add a node).
3. Deleting an element from the list (Delete a node).
4. Search/Find for a node.
5. Traversing (walk) the list.
aggregate operations.

Useful for counting operations or

6. Modifying elements from the List.


7. Concatenating two list into one.

28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

DOUBLY LINKED LIST


Each node has a Three field.
HEAD

FEA2
F68F

F003

FEA2

FEA2 B F68F
NULL A F003
Previous Address Field or Backward link field:

F003

C NULL

It contains the address of its previous node.


Data Field:
Stores the Actual Data value.
Next Address Field or Forward link field:
It contains the address of its next node.
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

Creation of a list
1.
2.
3.
4.
5.
6.
7.

Set SIZE=get the size of the NODE


7. If want to add another node
Set NEWNODE=Allocate space in memory
Repeat 1 to 4 then follow
Read, NEWNODE >DATA
8. Assign LAST >FLINK=NEWNODE
Set NEWNODE >BLINK=NULL
9.NEWNODE->BLINK=LAST
Set NEWNODE >FLINK=NULL
10. Assign LAST =LAST >LINK
Set HEAD=NEWNODE
11. If want to add another in the list
Set LAST=NEWNODE
Repeat step 7

HEAD
NULL

28-Jul-14

Memory
NEWNODE

BL D FL

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

Creation of a list
1.
2.
3.
4.
5.
6.
7.

Set SIZE=get the size of the NODE


7. If want to add another node
Set NEWNODE=Allocate space in memory
Repeat 1 to 4 then follow
Read, NEWNODE >DATA
8. Assign LAST >FLINK=NEWNODE
Set NEWNODE >BLINK=NULL
9.NEWNODE->BLINK=LAST
Set NEWNODE >FLINK=NULL
10. Assign LAST =LAST >LINK
Set HEAD=NEWNODE
11. If want to add another in the list
Set LAST=NEWNODE
Repeat step 7

HEAD
FEA2
NULL

NULL

NULL

NEWNODE

28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

Creation of a list
1.
2.
3.
4.
5.
6.
7.

Set SIZE=get the size of the NODE


7. If want to add another node
Set NEWNODE=Allocate space in memory
Repeat 1 to 4 then follow
Read, NEWNODE >DATA
8. Assign LAST >FLINK=NEWNODE
Set NEWNODE >BLINK=NULL
9.NEWNODE->BLINK=LAST
Set NEWNODE >FLINK=NULL
10. Assign LAST =LAST >LINK
Set HEAD=NEWNODE
11. If want to add another in the list
Set LAST=NEWNODE
Repeat step 7

HEAD
FEA2
FEA2
NULL

NULL

NEWNODE,LAST
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

Creation of a list
1.
2.
3.
4.
5.
6.
7.

Set SIZE=get the size of the NODE


7. If want to add another node
Set NEWNODE=Allocate space in memory
Repeat 1 to 4 then follow
Read, NEWNODE >DATA
8. Assign LAST >FLINK=NEWNODE
Set NEWNODE >BLINK=NULL
9.NEWNODE->BLINK=LAST
Set NEWNODE >FLINK=NULL
10. Assign LAST =LAST >LINK
Set HEAD=NEWNODE
11. If want to add another in the list
Set LAST=NEWNODE
Repeat step 7

HEAD
Memory

FEA2

NEWNODE

FEA2
NULL

BL D FL
NULL

NEWNODE,LAST
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

Creation of a list
1.
2.
3.
4.
5.
6.
7.

Set SIZE=get the size of the NODE


7. If want to add another node
Set NEWNODE=Allocate space in memory
Repeat 1 to 4 then follow
Read, NEWNODE >DATA
8. Assign LAST >FLINK=NEWNODE
Set NEWNODE >BLINK=NULL
9.NEWNODE->BLINK=LAST
Set NEWNODE >FLINK=NULL
10. Assign LAST =LAST >LINK
Set HEAD=NEWNODE
11. If want to add another in the list
Set LAST=NEWNODE
Repeat step 7

HEAD
FEA2
F003
FEA2
NULL

NULL
NULL

NULL

NEWNODE

LAST
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

10

Creation of a list
1.
2.
3.
4.
5.
6.
7.

Set SIZE=get the size of the NODE


7. If want to add another node
Set NEWNODE=Allocate space in memory
Repeat 1 to 4 then follow
Read, NEWNODE >DATA
8. Assign LAST >FLINK=NEWNODE
Set NEWNODE >BLINK=NULL
9.NEWNODE->BLINK=LAST
Set NEWNODE >FLINK=NULL
10. Assign LAST =LAST ->LINK
Set HEAD=NEWNODE
11. If want to add another in the list
Set LAST=NEWNODE
Repeat step 7

HEAD
FEA2
F003

FEA2
NULL

LAST
28-Jul-14

F003

FEA2

NULL

NEWNODE
PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

11

Creation of a list
1.
2.
3.
4.
5.
6.
7.

Set SIZE=get the size of the NODE


7. If want to add another node
Set NEWNODE=Allocate space in memory
Repeat 1 to 4 then follow
Read, NEWNODE >DATA
8. Assign LAST >FLINK=NEWNODE
Set NEWNODE >BLINK=NULL
9.NEWNODE->BLINK=LAST
Set NEWNODE >FLINK=NULL
10. Assign LAST =LAST >LINK
Set HEAD=NEWNODE
11. If want to add another in the list
Set LAST=NEWNODE
Repeat step 7

HEAD
FEA2
F003

FEA2
NULL

F003

FEA2

NULL

LAST
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

12

Creation of a list
1.
2.
3.
4.
5.
6.
7.

Set SIZE=get the size of the NODE


7. If want to add another node
Set NEWNODE=Allocate space in memory
Repeat 1 to 4 then follow
Read, NEWNODE >DATA
8. Assign LAST >FLINK=NEWNODE
Set NEWNODE >BLINK=NULL
9.NEWNODE->BLINK=LAST
Set NEWNODE >FLINK=NULL
10. Assign LAST =LAST >LINK
Set HEAD=NEWNODE
11. If want to add another in the list
Set LAST=NEWNODE
Repeat step 7

HEAD
Memory

FEA2
F003

FEA2
NULL A

28-Jul-14

F003

FEA2

NEWNODE

BL D FL

NULL

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

13

Creation of a list
1.
2.
3.
4.
5.
6.
7.

Set SIZE=get the size of the NODE


7. If want to add another node
Set NEWNODE=Allocate space in memory
Repeat 1 to 4 then follow
Read, NEWNODE >DATA
8. Assign LAST >FLINK=NEWNODE
Set NEWNODE >BLINK=NULL
9.NEWNODE->BLINK=LAST
Set NEWNODE >FLINK=NULL
10. Assign LAST =LAST >LINK
Set HEAD=NEWNODE
11. If want to add another in the list
Set LAST=NEWNODE
Repeat step 7

HEAD
F68F
FEA2

F003
FEA2

NULL A F003

28-Jul-14

F003

C NULL
NEWNODE

FEA2 B F68F

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

14

Creation of a list
1.
2.
3.
4.
5.
6.
7.

Set SIZE=get the size of the NODE


7. If want to add another node
Set NEWNODE=Allocate space in memory
Repeat 1 to 4 then follow
Read, NEWNODE >DATA
8. Assign LAST >FLINK=NEWNODE
Set NEWNODE >BLINK=NULL
9.NEWNODE->BLINK=LAST
Set NEWNODE >FLINK=NULL
10. Assign LAST =LAST >LINK
Set HEAD=NEWNODE
11. If want to add another in the list
Set LAST=NEWNODE
Repeat step 7

HEAD
FEA2
FEA2
NULL A F003

F003
FEA2 B F68F

F68F
F003

LAST
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

C NULL

NEWNODE
15

Creation of a list
1.
2.
3.
4.
5.
6.
7.

Set SIZE=get the size of the NODE


7. If want to add another node
Set NEWNODE=Allocate space in memory
Repeat 1 to 4 then follow
Read, NEWNODE >DATA
8. Assign LAST >FLINK=NEWNODE
Set NEWNODE >BLINK=NULL
9.NEWNODE->BLINK=LAST
Set NEWNODE >FLINK=NULL
10. Assign LAST =LAST >LINK
Set HEAD=NEWNODE
11. If want to add another in the list
Set LAST=NEWNODE
Repeat step 7

HEAD
FEA2
FEA2
NULL A F003

F003
FEA2 B F68F

F68F
F003

C NULL

LAST
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

16

Insertion of a node

The new node can be inserted in the list at three different places
1. Inserting as a first node
2. Inserting as a last node
3. Inserting an intermediate node

28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

17

Inserting as a first node


1. If HEAD==NULL
Set HEAD=NEWNODE
2. Assign NEWNODE >FLINK=HEAD
3. Set HEAD->BLINK=NEWNODE
3. Set HEAD=NEWNODE
FEA2

HEAD
NULL

NULL

NULL

NEWNODE

28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

18

Inserting as a first node


1. If HEAD==NULL
Set HEAD=NEWNODE
2. Assign NEWNODE >FLINK=HEAD
3. Set HEAD->BLINK=NEWNODE
3. Set HEAD=NEWNODE
HEAD
FEA2
FEA2
NULL

NULL

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

19

Inserting as a first node

HEAD

1. If HEAD==NULL
Set HEAD=NEWNODE
2. Assign NEWNODE >FLINK=HEAD
3. Set HEAD->BLINK=NEWNODE
3. Set HEAD=NEWNODE

FEA2
FEA2
NULL A F003

F003
FEA2 B F68F

F003

FB12
NULL

F68F
C NULL

LAST

NULL

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

20

Inserting as a first node

HEAD

1. If HEAD==NULL
Set HEAD=NEWNODE
2. Assign NEWNODE >FLINK=HEAD
3. Set HEAD->BLINK=NEWNODE
3. Set HEAD=NEWNODE

FEA2
FEA2
FB12 A F003

F003
FEA2 B F68F

F003

LAST

FB12
NULL

F68F
C NULL

FEA2

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

21

Inserting as a first node

HEAD

1. If HEAD==NULL
Set HEAD=NEWNODE
2. Assign NEWNODE >FLINK=HEAD
3. Set HEAD->BLINK=NEWNODE
3. Set HEAD=NEWNODE

FEA2
FEA2
FB12 A F003

F003
FEA2 B F68F

F003

LAST

FB12
NULL

F68F
C NULL

FEA2

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

22

Inserting as a first node

HEAD

1. If HEAD==NULL
Set HEAD=NEWNODE
2. Assign NEWNODE >FLINK=HEAD
3. Set HEAD->BLINK=NEWNODE
3. Set HEAD=NEWNODE

FB12
FEA2
FB12 A F003

F003
FEA2 B F68F

F003

LAST

FB12
NULL

F68F
C NULL

FEA2

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

23

Inserting as a last node


1. If HEAD==NULL
3. While(LAST->FLINK!=NULL)
Set HEAD=NEWNODE
Assign LAST=LAST->FLINK
2. Set LAST=HEAD
4. Assign LAST->FLINK=NEWNODE
5. Assign NEWNODE->BLINK=LAST
HEAD
FEA2

LAST

FEA2
NULL A F003

F003
FEA2 B F68F

F003

F68F
C NULL

FB12
NULL

NULL

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

24

Inserting as a last node


1. If HEAD==NULL
3. While(LAST->FLINK!=NULL)
Set HEAD=NEWNODE
Assign LAST=LAST->FLINK
2. Set LAST=HEAD
4. Assign LAST->FLINK=NEWNODE
5. Assign NEWNODE->BLINK=LAST
HEAD
FEA2

LAST

FEA2
NULL A F003

F003
FEA2 B F68F

F003

F68F
C NULL

FB12
NULL

NULL

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

25

Inserting as a last node


1. If HEAD==NULL
3. While(LAST->FLINK!=NULL)
Set HEAD=NEWNODE
Assign LAST=LAST->FLINK
2. Set LAST=HEAD
4. Assign LAST->FLINK=NEWNODE
5. Assign NEWNODE->BLINK=LAST
HEAD
FEA2

LAST

FEA2
NULL A F003

F003
FEA2 B F68F

F003

F68F
C NULL

FB12
NULL

NULL

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

26

Inserting as a last node


1. If HEAD==NULL
3. While(LAST->FLINK!=NULL)
Set HEAD=NEWNODE
Assign LAST=LAST->FLINK
4. Assign LAST->FLINK=NEWNODE
2. Set LAST=HEAD
5. Assign NEWNODE->BLINK=LAST
HEAD
FEA2
FEA2
NULL A F003

LAST
F003
FEA2 B F68F

F68F
C FB12

F003

FB12
NULL

NULL

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

27

Inserting as a last node


1. If HEAD==NULL
3. While(LAST->FLINK!=NULL)
Set HEAD=NEWNODE
Assign LAST=LAST->FLINK
2. Set LAST=HEAD
4. Assign LAST->FLINK=NEWNODE
5. Assign NEWNODE->BLINK=LAST
HEAD
FEA2
FEA2
NULL A F003

LAST
F003
FEA2 B F68F

F68F
C FB12

F003

FB12
F68F

NULL

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

28

Inserting an intermediate node


1. If HEAD==NULL
Set HEAD=NEWNODE
2. Print, enter the node after which insertion is to be made
3.Read, CONDITION
HEAD

CONDITION = B

FEA2
FEA2
NULL A F003

F003
FEA2 B F68F

F003

F68F
C NULL

FB12
NULL

NULL

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

29

Inserting an intermediate node


4. LAST=HEAD
5. While (LAST!=NULL)
6. if (LAST->DATA=
=CONDITION)

Assign NEXT=LAST->FLINK
Assign NEWNODE->FLINK=NEXT
Assign NEWNODE->BLINK=LAST
Assign LAST->FLINK=NEWNODE
else LAST=LAST->FLINK

HEAD
CONDITION = B

FEA2
LAST
FEA2
NULL A F003

F003
FEA2 B F68F

F003

F68F
C NULL

FB12
NULL

NULL

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

30

Inserting an intermediate node


4. LAST=HEAD
5. While (LAST!=NULL)
6. if (LAST->DATA=
=CONDITION)

Assign NEXT=LAST->FLINK
Assign NEWNODE->FLINK=NEXT
Assign NEWNODE->BLINK=LAST
Assign LAST->FLINK=NEWNODE
else LAST=LAST->FLINK

HEAD
CONDITION = B

FEA2
LAST
FEA2
NULL A F003

F003
FEA2 B F68F

F003

F68F
C NULL

FB12
NULL

NULL

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

31

Inserting an intermediate node


4. LAST=HEAD
5. While (LAST!=NULL)
6. if (LAST->DATA=
=CONDITION)

Assign NEXT=LAST->FLINK
Assign NEWNODE->FLINK=NEXT
Assign NEWNODE->BLINK=LAST
Assign LAST->FLINK=NEWNODE
else LAST=LAST->FLINK

HEAD
CONDITION = B

FEA2
LAST
FEA2
NULL A F003

NEXT

F003
FEA2 B F68F

F003

F68F
C NULL

FB12
NULL

NULL

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

32

Inserting an intermediate node


4. LAST=HEAD
5. While (LAST!=NULL)
6. if (LAST->DATA=
=CONDITION)

Assign NEXT=LAST->FLINK
Assign NEWNODE->FLINK=NEXT
Assign NEWNODE->BLINK=LAST
Assign LAST->FLINK=NEWNODE
else LAST=LAST->FLINK

HEAD
CONDITION = B

FEA2
LAST

NEXT

F003
FEA2 B F68F

FEA2
NULL A F003

F003

F68F
C NULL

FB12
F003

F68F

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

33

Inserting an intermediate node


4. LAST=HEAD
5. While (LAST!=NULL)
6. if (LAST->DATA=
=CONDITION)

Assign NEXT=LAST->FLINK
Assign NEWNODE->FLINK=NEXT
Assign NEWNODE->BLINK=LAST
Assign LAST->FLINK=NEWNODE
Assign NEXT->BLINK=NEWNODE
else LAST=LAST->FLINK

HEAD
CONDITION = B

FEA2
LAST

NEXT

F003
FEA2 B FB12

FEA2
NULL A F003

FB12

F68F
C NULL

FB12
F003

F68F

NEWNODE
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

34

Modification of a node
1. Print, enter the value
4. while(LAST!=NULL)
which is going to modify
if(LAST->DATA==MODIFY)
then Read, LAST->DATA
2. Read, MODIFY
3. LAST=HEAD
else LAST=LAST->FLINK
HEAD
FEA2
LAST
FEA2
NULL A F003

F003
FEA2 B F68F

F003

F68F
C NULL

MODIFY = B

28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

35

Modification of a node
1. Print, enter the value
4. while(LAST!=NULL)
which is going to modify
if(LAST->DATA==MODIFY)
2. Read, MODIFY
then Read, LAST->DATA
3. LAST=HEAD
else LAST=LAST->FLINK
HEAD
FEA2
FEA2
NULL A F003

LAST
F003
FEA2 B F68F

F003

F68F
C NULL

MODIFY = B
28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

36

Modification of a node
1. Print, enter the value
4. while(LAST!=NULL)
which is going to modify
if(LAST->DATA==MODIFY)
then Read, LAST->DATA
2. Read, MODIFY
3. LAST=HEAD
else LAST=LAST->FLINK
HEAD
FEA2
FEA2
NULL A F003

LAST
F003
FEA2 F F68F

F003

F68F
C NULL

MODIFY = B

28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

37

Deletion of a node
The node can be deleted in the list at three different places
1. Deleting the first node
2. Deleting the last node
3. Deleting an intermediate node

28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

38

Deleting the first node


1. If (HEAD==NULL) then Print," list is empty
2. Else Set DELNODE=HEAD
3. Assign HEAD=HEAD->FLINK
4. Assign HEAD->BLINK=NULL
4. Print, Deleted data is, DELNODE->DATA
HEAD
FEA2
HEAD
FEA2
NULL A F003

F003
FEA2 B F68F

F003

F68F
C NULL

DELNODE

28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

39

Deleting the first node


1. If (HEAD==NULL) then Print," list is empty
2. Else Set DELNODE=HEAD
3. Assign HEAD=HEAD->FLINK
4. Assign HEAD->BLINK=NULL
4. Print, Deleted data is, DELNODE->DATA
HEAD
F003
FEA2
NULL A F003

HEAD
F003
FEA2 B F68F

F003

F68F
C NULL

DELNODE

28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

40

Deleting the first node


1. If (HEAD==NULL) then Print," list is empty
2. Else Set DELNODE=HEAD
3. Assign HEAD=HEAD->FLINK
4. Assign HEAD->BLINK=NULL
5. Print, Deleted data is, DELNODE->DATA
HEAD
F003
FEA2
NULL A F003

HEAD
F003
NULL B F68F

F003

F68F
C NULL

DELNODE

28-Jul-14

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

41

Deleting the last node


1. If (HEAD==NULL) then Print," list is empty
2. Set LAST=HEAD
5. Set DELNODE=LAST
3. While(LAST->LINK!=NULL) 6. LAST->BLINK->FLINK=NULL
4. Assign LAST=LAST->FLINK 7. Print, Deleted data is,
DELNODE->DATA
HEAD
FEA2

LAST

FEA2
NULL A F003

28-Jul-14

F003
FEA2 B F68F

F003

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

F68F
C NULL

42

Deleting the last node


1. If (HEAD==NULL) then Print," list is empty
2. Set LAST=HEAD
6. Set DELNODE=LAST
3. While(LAST->LINK!=NULL) 7. LAST->BLINK->FLINK=NULL
4. Assign LAST=LAST->FLINK 8. Print, Deleted data is,
DELNODE->DATA
HEAD
FEA2
FEA2
NULL A F003

28-Jul-14

LAST
F003
FEA2 B F68F

F003

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

F68F
C NULL

43

Deleting the last node


1. If (HEAD==NULL) then Print," list is empty
2. Set LAST=HEAD
6. Set DELNODE=LAST
3. While(LAST->LINK!=NULL) 7. LAST->BLINK->FLINK=NULL
4. Assign LAST=LAST->FLINK 8. Print, Deleted data is,
DELNODE->DATA

HEAD
FEA2
FEA2
NULL A F003

28-Jul-14

LAST,DELNODE
F003
FEA2 B F68F

F003

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

F68F
C NULL

44

Deleting the last node


1. If (HEAD==NULL) then Print," list is empty
2. Set LAST=HEAD
6. Set DELNODE=LAST
3. While(LAST->LINK!=NULL) 7. LAST->BLINK->FLINK=NULL
4. Assign LAST=LAST->FLINK 8. Print, Deleted data is,
DELNODE->DATA

HEAD
FEA2
FEA2
NULL A F003

28-Jul-14

F003
FEA2 B NULL

LAST,DELNODE
F68F
F003 C NULL

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

45

Deleting an intermediate node


1.
2.
3.
4.

If (HEAD==NULL) then Print," list is empty


Print, Enter the Data of the node for deletion 6. While(LAST!=NULL)
Read, DELDATA
if(LAST->DATA==DELDATA)
If(HEAD->DATA==DELDATA)
Set DELNODE=LAST
then Set DELNODE=HEAD
Set PREV=LAST->BLINK
Assign HEAD=HEAD->FLINK
Set NEXT=LAST->FLINK
If (HEAD!=NULL)
Assign PREV->FLINK=NEXT
Assign HEAD->BLINK=NULL
Assign NEXT->BLINK=PREV
5.else LAST=HEAD->FLINK
else LAST=LAST->FLINK

HEAD

DELDATA=B

FEA2
HEAD
FEA2
NULL A F003

28-Jul-14

F003
FEA2 B F68F

F003

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

F68F
C NULL

46

Deleting an intermediate node


1.
2.
3.
4.

If (HEAD==NULL) then Print," list is empty


Print, Enter the Data of the node for deletion 6. While(LAST!=NULL)
if(LAST->DATA==DELDATA)
Read, DELDATA
If(HEAD->DATA==DELDATA)
Set DELNODE=LAST
then Set DELNODE=HEAD
Set PREV=LAST->BLINK
Assign HEAD=HEAD->FLINK
Set NEXT=LAST->FLINK
If (HEAD!=NULL)
Assign PREV->FLINK=NEXT
Assign HEAD->BLINK=NULL
Assign NEXT->BLINK=PREV
5.else LAST=HEAD->FLINK
else LAST=LAST->FLINK

HEAD
FEA2
FEA2
NULL A F003

28-Jul-14

DELDATA=B
LAST
F003
FEA2 B F68F

F003

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

F68F
C NULL

47

Deleting an intermediate node


1.
2.
3.
4.

If (HEAD==NULL) then Print," list is empty


Print, Enter the Data of the node for deletion 6. While(LAST!=NULL)
Read, DELDATA
if(LAST->DATA==DELDATA)
If(HEAD->DATA==DELDATA)
Set DELNODE=LAST
then Set DELNODE=HEAD
Set PREV=LAST->BLINK
Assign HEAD=HEAD->FLINK
Set NEXT=LAST->FLINK
If (HEAD!=NULL)
Assign PREV->FLINK=NEXT
Assign HEAD->BLINK=NULL
Assign NEXT->BLINK=PREV
5.else LAST=HEAD->FLINK
else LAST=LAST->FLINK

HEAD

DELDATA=B

FEA2
FEA2
NULL A F003

28-Jul-14

LAST,DELNODE
F003
FEA2 B F68F

F003

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

F68F
C NULL

48

Deleting an intermediate node


1.
2.
3.
4.

If (HEAD==NULL) then Print," list is empty


Print, Enter the Data of the node for deletion 6. While(LAST!=NULL)
Read, DELDATA
if(LAST->DATA==DELDATA)
If(HEAD->DATA==DELDATA)
Set DELNODE=LAST
then Set DELNODE=HEAD
Set PREV=LAST->BLINK
Assign HEAD=HEAD->FLINK
Set NEXT=LAST->FLINK
If (HEAD!=NULL)
Assign PREV->FLINK=NEXT
Assign HEAD->BLINK=NULL
Assign NEXT->BLINK=PREV
5.else LAST=HEAD->FLINK
else LAST=LAST->FLINK

HEAD

DELDATA=B

FEA2
PREV
FEA2
NULL A F003

28-Jul-14

LAST,DELNODE
F003
FEA2 B F68F

NEXT
F68F
F003 C NULL

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

49

Deleting an intermediate node


1.
2.
3.
4.

If (HEAD==NULL) then Print," list is empty


Print, Enter the Data of the node for deletion 6. While(LAST!=NULL)
Read, DELDATA
if(LAST->DATA==DELDATA)
If(HEAD->DATA==DELDATA)
Set DELNODE=LAST
then Set DELNODE=HEAD
Set PREV=LAST->BLINK
Assign HEAD=HEAD->FLINK
Set NEXT=LAST->FLINK
If (HEAD!=NULL)
Assign PREV->FLINK=NEXT
Assign HEAD->BLINK=NULL
Assign NEXT->BLINK=PREV
5.else LAST=HEAD->FLINK
else LAST=LAST->FLINK

HEAD

DELDATA=B

FEA2
PREV
FEA2
NULL A F68F

28-Jul-14

LAST,DELNODE
F003
FEA2 B F68F

NEXT
F68F
F003 C NULL

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

50

Deleting an intermediate node


1.
2.
3.
4.

If (HEAD==NULL) then Print," list is empty


Print, Enter the Data of the node for deletion 6. While(LAST!=NULL)
Read, DELDATA
if(LAST->DATA==DELDATA)
If(HEAD->DATA==DELDATA)
Set DELNODE=LAST
then Set DELNODE=HEAD
Set PREV=LAST->BLINK
Assign HEAD=HEAD->FLINK
Set NEXT=LAST->FLINK
If (HEAD!=NULL)
Assign PREV->FLINK=NEXT
Assign HEAD->BLINK=NULL
Assign NEXT->BLINK=PREV
5.else LAST=HEAD->FLINK
else LAST=LAST->FLINK

HEAD

DELDATA=B

FEA2
PREV
FEA2
NULL A F68F

28-Jul-14

LAST,DELNODE
F003
FEA2 B F68F

NEXT
F68F
FEA2 C NULL

PROF. RAJESH M / S.C.S.E @ VIT UNIVERSITY - CHENNAI

51

You might also like