circular linked lists - nathaniel g....

69
Circular Linked Lists

Upload: others

Post on 30-Apr-2020

13 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

Circular Linked Lists

Page 2: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

2

Objectives

In this chapter, you will learn to:Implement a Circular-linked list

Traversing

Insertion

Deletion

Page 3: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

3

You can implement a circular linked list bylinking the last node back to the first nodein the list.

10 15 9 11 23 5 12

Implementing a Circular Linked List (Contd.)

LAST

Page 4: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

4

In a circular linked list, the last node holds theaddress of the first node.

In a circular linked list, you need to maintain avariable/pointer, LAST, which always point to the lastnode.

10

15

9 11

23

5 12

LAST

Implementing a Circular Linked List(Contd.)

Page 5: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

5

Traversing a Circular Linked ListWrite an algorithm to traverse a circular linked list.

1. Make currentNode point to the successor of the nodemarked as LAST, such that currentNode points to thefirst node in the list.

2. Repeat step 3 and 4 until currentNode = LAST.

3. Display the information contained in the node markedas currentNode.

4. Make currentNode point to the next node in itssequence.

5. Display the information contained in the node markedas LAST.

Algorithm to traverse a circular linked list.

Page 6: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

6

TRAVERSING IN LIST

Algorithm traversing()

{

1.new1 = Last -> next

2.while(new1 != Last)

2.1 Print new1 -> info

2.2 new1 = new1 -> next

3. Print Last -> info

}

Page 7: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

7

Inserting a Node in a Circular Linked List

In a circular linked list, you can insert anode at any of the following positions:

Beginning of the list

At specific position

End of the list

Page 8: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

8

Write an algorithm to insert a node in thebeginning of a circular linked list.

Inserting a Node at the Beginning of the List

Page 9: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

9

1010 15 17 20

new1

LAST

1. Allocate memory for the new node.

2. Assign value to the data field of the newnode.

3. Make the next field of the new node pointto the successor of LAST.

4. Make the next field of LAST point to thenew node.

Page 10: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

10

1010 15 17 20

LAST

1. Allocate memoryfor the new node.

2. Assign value tothe data field ofthe new node.

3. Make the nextfield of the newnode point to thesuccessor ofLAST.

4. Make the nextfield of LASTpoint to the newnode.

Algorithm to insert a node in thebeginning of a circular linked list.

Inserting a Node at the Beginning of the List (Contd.)

Page 11: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

11

1010 15 17 20

7

new1

LAST

Inserting a Node at the Beginning of the List (Contd.)

1. Allocate memoryfor the new node.

2. Assign value tothe data field ofthe new node.

3. Make the nextfield of the newnode point to thesuccessor ofLAST.

4. Make the nextfield of LASTpoint to the newnode.

Inserting a Node at the Beginning of the List (Contd.)

Page 12: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

12

new1 -> next = LAST ->next

Inserting a Node at the Beginning of the List (Contd.)

1. Allocate memoryfor the new node.

2. Assign value tothe data field ofthe new node.

3. Make the nextfield of the newnode point to thesuccessor ofLAST.

4. Make the nextfield of LASTpoint to the newnode.

1010 15 17 20

7

new1

LAST

Inserting a Node at the Beginning of the List (Contd.)

Page 13: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

13

LAST -> next = new1

Insertion complete

Inserting a Node at the Beginning of the List (Contd.)

1. Allocate memoryfor the new node.

2. Assign value tothe data field ofthe new node.

3. Make the nextfield of the newnode point to thesuccessor ofLAST.

4. Make the nextfield of LASTpoint to the newnode.

1010 15 17 20

7

new1

LAST

Inserting a Node at the Beginning of the List (Contd.)

Page 14: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

14

ALGORITHM TO INSERT NODE AT BEGINING

Last=NULL

Algorithm InsertAtBEG()

{

1. Create node [(new1=(struct node*) malloc(sizeof(struct node))]

2. Enter data [new1 -> info = data]

3. If (Last == NULL)

3.1 new1 -> next = new1

3.2 Last=new1

else

3.1 new1 -> next = Last -> next

3.2 Last -> next = new1

}

Page 15: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

15

Inserting a Node Between Two Nodes in the List

The algorithm to insert a node between two nodes ina circular linked list is same as that of a singly-linkedlist.

However, the algorithm to search for the nodesbetween which the new node is to be inserted is abit different in a circular linked list.

Page 16: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

16

Write an algorithm to insert a node at the particular positionin a linked list.

Inserting a Node in a Singly-Linked List (Contd.)

Page 17: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

17

1.Allocate memory for the new node.

2. Assign value to the data field ofthe new node.

3. Identify the nodes after whichthe new node is to be inserted.Mark it as previous a. Make previous node point

to the first node and setcount=1

b. Repeat step c and step duntil count becomes equal to location-1

c. Count=count+1. d. Make previous point to

next node in sequence

4. Make the next field of the newnode point to the next of previousnode

5.Make the next field of previouspoint to the new node.

Insert 16 At LOC = 3

5 17 210

Inserting a Node in a Singly-LinkedList (Contd.)

Last

Page 18: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

18

1010 5 17 2

Inserting a Node in a Singly-Linked List (Contd.)

new1

1.Allocate memory for the new node.

2. Assign value to the data field of thenew node.

3. Identify the nodes after which the newnode is to be inserted. Mark it asprevious a. Make previous node point to the

first node and set count=1 b. Repeat step c and step d until

count becomes equal to location-1

c. Count=count+1. d. Make previous point to next

node in sequence

4. Make the next field of the new nodepoint to the next of previous node

5.Make the next field of previous point tothe new node.

Last

Inserting a Node in a Singly-LinkedList (Contd.)

Page 19: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

19

1010 5 17 2

Insert 16

LOC = 3

16

new1

1.Allocate memory for the new node.

2. Assign value to the data field of the newnode.

3. Identify the nodes after which the newnode is to be inserted. Mark it as previous a. Make previous node point to the

first node and set count=1 b. Repeat step c and step d until

count becomes equal to location-1 c. Count=count+1. d. Make previous point to next node

in sequence

4. Make the next field of the new nodepoint to the next of previous node

5.Make the next field of previous point tothe new node.

Last

Inserting a Node in a Singly-LinkedList (Contd.)

Page 20: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

20

1010 5 17 2

Insert 16

LOC = 3

Inserting a Node in a Singly-Linked List (Contd.)

16

new1

1.Allocate memory for the new node.

2. Assign value to the data field of thenew node.

3. Identify the nodes after which thenew node is to be inserted. Mark itas previous a. Make previous node point to

the first node and set count=1 b. Repeat step c and step d until

count becomes equal tolocation-1

c. Count=count+1. d. Make previous point to next

node in sequence

4. Make the next field of the newnode point to the next of previousnode

5.Make the next field of previous pointto the new node.

Last

Inserting a Node in a Singly-LinkedList (Contd.)

Page 21: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

21

1010 5 17 2

previous

Inserting a Node in a Singly-Linked List (Contd.)

16

new1

Count=1

1.Allocate memory for the new node.

2. Assign value to the data field of the newnode.

3. Identify the nodes after which the newnode is to be inserted. Mark it as previous a. Make previous node point to the

first node and set count=1 b. Repeat step c and step d until

count becomes equal to location-1 c. Count=count+1. d. Make previous point to next node

in sequence

4. Make the next field of the new nodepoint to the next of previous node

5.Make the next field of previous point to thenew node.

Last

Inserting a Node in a Singly-LinkedList (Contd.)

Page 22: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

22

1010 5 17 2

previous

Inserting a Node in a Singly-Linked List (Contd.)

16

new1

Count=1

1.Allocate memory for the new node.

2. Assign value to the data field of the newnode.

3. Identify the nodes after which the newnode is to be inserted. Mark it as previous a. Make previous node point to the

first node and set count=1 b. Repeat step c and step d until

count becomes equal to location-1 c. Count=count+1. d. Make previous point to next node

in sequence

4. Make the next field of the new nodepoint to the next of previous node

5.Make the next field of previous point to thenew node.

Last

Inserting a Node in a Singly-LinkedList (Contd.)

Page 23: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

23

1010 5 17 2

previous

Inserting a Node in a Singly-Linked List (Contd.)

16

new1

Count=2

1.Allocate memory for the new node.

2. Assign value to the data field of thenew node.

3. Identify the nodes after which the newnode is to be inserted. Mark it asprevious a. Make previous node point to the

first node and set count=1 b. Repeat step c and step d until

count becomes equal to location-1 c. Count=count+1. d. Make previous point to next

node in sequence

4. Make the next field of the new nodepoint to the next of previous node

5.Make the next field of previous point tothe new node.

Last

Inserting a Node in a Singly-LinkedList (Contd.)

Page 24: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

24

1010 5 17 2

Nodes located

Insert 16

previous

Inserting a Node in a Singly-Linked List (Contd.)

16

new1

1.Allocate memory for the new node.

2. Assign value to the data field of thenew node.

3. Identify the nodes after which thenew node is to be inserted. Mark it asprevious a. Make previous node point to

the first node and set count=1 b. Repeat step c and step d until

count becomes equal tolocation-1

c. Count=count+1. d. Make previous point to next

node in sequence

4. Make the next field of the new nodepoint to the next of previous node

5.Make the next field of previous point tothe new node.

Last

Inserting a Node in a Singly-LinkedList (Contd.)

Page 25: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

25

1010 5 17 2

16

new1

new1 -> next = previous->next

previous

Inserting a Node in a Singly-Linked List (Contd.)

1.Allocate memory for the new node.

2. Assign value to the data field of thenew node.

3. Identify the nodes after which the newnode is to be inserted. Mark it asprevious a. Make previous node point to the

first node and set count=1 b. Repeat step c and step d until

count becomes equal to location-1 c. Count=count+1. d. Make previous point to next

node in sequence

4. Make the next field of the new nodepoint to the next of previous node

5.Make the next field of previous point tothe new node.

Last

Inserting a Node in a Singly-LinkedList (Contd.)

Page 26: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

26

1010 5 17 2

16

new1

previous -> next = new1

new1 -> next = previous->next

previous

Inserting a Node in a Singly-Linked List (Contd.)

1.Allocate memory for the new node.

2. Assign value to the data field of thenew node.

3. Identify the nodes after which thenew node is to be inserted. Mark itas previous a. Make previous node point to

the first node and set count=1 b. Repeat step c and step d until

count becomes equal tolocation-1

c. Count=count+1. d. Make previous point to next

node in sequence

4. Make the next field of the newnode point to the next of previousnode

5.Make the next field of previous pointto the new node.

Last

Inserting a Node in a Singly-LinkedList (Contd.)

Page 27: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

27

1010

5 17 2

16

Insertioncomplete

previous

Inserting a Node in a Singly-Linked List (Contd.)

1.Allocate memory for the new node.

2. Assign value to the data field of thenew node.

3. Identify the nodes after which the newnode is to be inserted. Mark it asprevious a. Make previous node point to the

first node and set count=1 b. Repeat step c and step d until

count becomes equal to location-1

c. Count=count+1. d. Make previous point to next

node in sequence

4. Make the next field of the new nodepoint to the next of previous node

5.Make the next field of previous point tothe new node.

Last

Inserting a Node in a Singly-LinkedList (Contd.)

Page 28: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

28

ALGORITHM TO INSERT NODE At PARTICULAR POSITION IN A LINKED LISTAlgorithm InsertAtSpec()

{

1. Create node [(new1=(struct node*) malloc(sizeof(struct node))]

2. Enter Data and Location

3. new1->info=Data

4. If (Location == 1)

4.1 new1 -> next = Last -> next

4.2 Last -> next = new1

Else

4.1 Previous = Last -> next

4.2 Count = 1

4.3 While( Count <= Location - 1)

4.3.1 Previous = Previous -> next

4.3.2 Count++

4.4 if (Prev == Last)

4.4.1 new1 -> next = Last -> next

4.4.2 Last ->next=new1;

4.4.3 Last = new1;

else

4.4.1 new1 -> next = Previous -> next

4.4.2 Previous -> next = new1

}

Page 29: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

29

Write an algorithm to insert a node at the end of a circularlinked list.

Inserting a Node at the End of the List

Page 30: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

30

LAST

15 17 20

Let us insert a node after node 20

1. Allocate memory forthe new node.

2. Assign value to thedata field of the newnode.

3. Make the next field ofthe new node point tothe successor ofLAST.

4. Make the next field ofLAST point to the newnode.

5. Mark LAST point tothe new node.

Algorithm to insert a node at theend of the list

Inserting a Node at the

End of the List (Contd.)

10

Page 31: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

31

new1

1. Allocate memoryfor the new node.

2. Assign value to thedata field of thenew node.

3. Make the next fieldof the new nodepoint to thesuccessor of LAST.

4. Make the next fieldof LAST point tothe new node.

5. Mark LAST point tothe new node.

Let us insert a node after node 20

10

LAST

10 15 17 20

Inserting a Node at the End of the List (Contd.)Inserting a Node at the

End of the List (Contd.)

Page 32: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

32

24

10

LAST

10 15 17 20

new1

Inserting a Node at the End of the List (Contd.)

1. Allocate memoryfor the new node.

2. Assign value to thedata field of thenew node.

3. Make the next fieldof the new nodepoint to thesuccessor of LAST.

4. Make the next fieldof LAST point tothe new node.

5. Mark LAST point tothe new node.

Inserting a Node at the

End of the List (Contd.)

Page 33: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

33

new1 -> next = LAST -> next

24

10

LAST

10 15 17 20

new1

Inserting a Node at the End of the List (Contd.)

1. Allocate memoryfor the new node.

2. Assign value to thedata field of thenew node.

3. Make the next fieldof the new nodepoint to thesuccessor of LAST.

4. Make the next fieldof LAST point tothe new node.

5. Mark LAST point tothe new node.

Inserting a Node at the

End of the List (Contd.)

Page 34: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

34

LAST -> next = new1

24

10

LAST

10 15 17 20

new1

Inserting a Node at the End of the List (Contd.)

1. Allocate memoryfor the new node.

2. Assign value to thedata field of thenew node.

3. Make the next fieldof the new nodepoint to thesuccessor of LAST.

4. Make the next fieldof LAST point tothe new node.

5. Mark LAST point tothe new node.

Inserting a Node at the

End of the List (Contd.)

Page 35: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

35

LAST = newnode

Insertion complete

24

1010 15 17 20

Last

Inserting a Node at the End of the List (Contd.)

1. Allocate memoryfor the new node.

2. Assign value to thedata field of thenew node.

3. Make the next fieldof the new nodepoint to thesuccessor of LAST.

4. Make the next fieldof LAST point tothe new node.

5. Mark LAST point tothe new node.

Inserting a Node at the

End of the List (Contd.)

Page 36: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

36

ALGORITHM TO INSERT NODE AT END

Last=NULL

Algorithm InsertAtEnd()

{

1. Create node [(new1 = (struct node*) malloc(sizeof(struct node))]

2. Enter data [new1 -> info =d ata]

3.if(Last == NULL)

3.1 new1 -> next = new1

3.2 Last=new1

else

3.1 new1 -> next = Last -> next

3.2 Last -> next = new1

3.3 Last = new1

}

Page 37: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

37

You can delete a node from any of the following places in acircular linked list:

Beginning of the list

End of the list

Between two nodes in the list

Deleting a Node from a Circular Linked List

Page 38: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

38

Write an algorithm to delete a node from the beginning of acircular linked list.

Deleting a Node From the Beginning of the List

Page 39: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

39

LAST

15 17 20

Algorithm for deleting a node from thebeginning of a circular linked list.

1. If the node to be deletedis the only node in thelist: // If LASTpoints to itself

a. Mark LAST asNULL

b. Exit

2. Make current point to thesuccessor of LAST

3. Make the next field ofLAST point to thesuccessor of current

4. Release the memory forthe node marked ascurrent

Deleting a Node From the Beginning of the List (Contd.)

10

Page 40: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

40

10

LAST

10 15 17 20

Deleting a Node From the Beginning of the List (Contd.)

1. If the node to be deletedis the only node in thelist: // If LASTpoints to itself

a. Mark LAST asNULL

b. Exit

2. Make current point to thesuccessor of LAST

3. Make the next field ofLAST point to thesuccessor of current

4. Release the memory forthe node marked ascurrent

Deleting a Node From the Beginning of the List (Contd.)

Page 41: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

41

current

Deleting a Node From the Beginning of the List (Contd.)

10

LAST

10 15 17 20

1. If the node to be deletedis the only node in thelist: // If LASTpoints to itself

a. Mark LAST asNULL

b. Exit

2. Make current point to thesuccessor of LAST

3. Make the next field ofLAST point to thesuccessor of current

4. Release the memory forthe node marked ascurrent

Deleting a Node From the Beginning of the List (Contd.)

Page 42: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

42

LAST -> next = current -> next

Deleting a Node From the Beginning of the List (Contd.)

10

LAST

10 15 17 20

current

1. If the node to be deletedis the only node in thelist: // If LASTpoints to itself

a. Mark LAST asNULL

b. Exit

2. Make current point to thesuccessor of LAST

3. Make the next field ofLAST point to thesuccessor of current

4. Release the memory forthe node marked ascurrent

Deleting a Node From the Beginning of the List (Contd.)

Page 43: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

43

Deletion Complete

Deleting a Node From the Beginning of the List (Contd.)

LAST

15 17 2010

current

1. If the node to be deletedis the only node in thelist: // If LASTpoints to itself

a. Mark LAST asNULL

b. Exit

2. Make current point to thesuccessor of LAST

3. Make the next field ofLAST point to thesuccessor of current

4. Release the memory forthe node marked ascurrent

Deleting a Node From the Beginning of the List (Contd.)

Page 44: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

44

Deletion Complete

Deleting a Node From the Beginning of the List (Contd.)

LAST

15 17 20

1. If the node to be deletedis the only node in thelist: // If LASTpoints to itself

a. Mark LAST asNULL

b. Exit

2. Make current point to thesuccessor of LAST

3. Make the next field ofLAST point to thesuccessor of current

4. Release the memory forthe node marked ascurrent

Deleting a Node From the Beginning of the List (Contd.)

Page 45: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

45

ALGORITHM TO DELETE A NODE FROM THE BEGINING

Algorithm DeleteAtBeg()

{

1. If (Last == NULL)

1.1 Print "underflow“

else If (Last -> next == Last )

1.1 Release the memory [ free (Last) ]

1.2 Last == NULL

else

1.1 Current = Last -> next

1.2 Last -> next = Current -> next

1.3 Current -> next = NULL

1.3 Release the memory [ free (Current) ]

}

Page 46: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

46

Deleting a Node Between Two Nodes in the List

Delete operation in between two nodes in a circular linkedlist is same as that of a singly-linked list.

However, the algorithm to locate the node to be deleted is abit different in a circular linked list.

Page 47: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

47

Write an algorithm to delete a node from a specific positionin a linked list.

Deleting a Node Between two Nodes in the List

Page 48: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

48

Algorithm to delete a node from aspecific position.

1. Locate the node to be deleted.Mark the node to be deleted ascurrent and its predecessor asprevious. To locate current andprevious, execute the followingsteps:

a. Set previous = NULLb. Set current = first

node c. Repeat step d and e

until either the nodeis found or previous

d. becomes Last. e. Make previous point

to current .f. Make current point

to the next node insequence.

2. Make the next field of previouspoint to the successor ofcurrent.

3. Release the memory for thenode marked as current.

Delete 17

15 17 20

Deleting a Node

Between two Nodes in the List (Contd.)

10

LAST

Page 49: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

49

1. Locate the node to be deleted.Mark the node to be deleted ascurrent and its predecessor asprevious. To locate current andprevious, execute the followingsteps:

a. Set previous = NULLb. Set current = first

node c. Repeat step d and e

until either the nodeis found or currentbecomes .

d. Make previous pointto current.

e. Make current pointto the next node insequence.

2. Make the next field of previouspoint to the successor ofcurrent.

3. Release the memory for thenode marked as current.

Delete 17

1010 15 17 20

LAST

Deleting a Node

Between two Nodes in the List (Contd.)

Page 50: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

50

1010 15 17 20

Delete 17

Deleting a Node Between two Nodes in the List (Contd.)

Previous = NULL

LAST

Deleting a Node

Between two Nodes in the List (Contd.)

Page 51: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

51

1010 15 17 20

Previous = NULL

current

Delete 17

Deleting a Node Between two Nodes in the List (Contd.)

LAST

Deleting a Node

Between two Nodes in the List (Contd.)

Page 52: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

52

1010 15 17 20

current

Delete 17

Deleting a Node Between two Nodes in the List (Contd.)

Previous = NULL

LAST

Deleting a Node

Between two Nodes in the List (Contd.)

Page 53: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

53

1010 15 17 20

current

Delete 17

Deleting a Node Between two Nodes in the List (Contd.)

previous

LAST

Deleting a Node

Between two Nodes in the List (Contd.)

Page 54: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

54

1010 15 17 20

currentcurrent

Delete 17

Deleting a Node Between two Nodes in the List (Contd.)

previous

LAST

Deleting a Node

Between two Nodes in the List (Contd.)

Page 55: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

55

1010 15 17 20

current

Delete 17

Deleting a Node Between two Nodes in the List (Contd.)

previous

LAST

Deleting a Node

Between two Nodes in the List (Contd.)

Page 56: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

56

1010 15 17 20

previous current

Delete 17

Deleting a Node Between two Nodes in the List (Contd.)

previous

LAST

Deleting a Node

Between two Nodes in the List (Contd.)

Page 57: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

57

Delete 17

1010 15 17 20

current current

Deleting a Node Between two Nodes in the List (Contd.)

previous

LAST

Deleting a Node

Between two Nodes in the List (Contd.)

Page 58: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

58

Delete 17

1010 15 17 20

current

Deleting a Node Between two Nodes in the List (Contd.)

previous

LAST

Deleting a Node

Between two Nodes in the List (Contd.)

Page 59: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

59

Previous -> next = current -> next

Delete 17

1010 15 17 20

current

Deleting a Node Between two Nodes in the List (Contd.)

previous

LAST

Deleting a Node

Between two Nodes in the List (Contd.)

Page 60: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

60

Delete operation complete

Delete 17

1010 15 17 20

current

Previous -> next = current -> next

Deleting a Node Between two Nodes in the List (Contd.)

previous

LAST

Deleting a Node

Between two Nodes in the List (Contd.)

Page 61: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

61

ALGORITHM TO DELETE A NODE FROM THE SPECIFIC POSITIONAlgorithm DeleteAtSpec()

{

1. Enter the Location

2. Current = Last -> next

3. Previous = NULL

4. If (Last == NULL)

4.1 Print "underflow“

else If ( Location == 1)

4.1 Last -> next = Current -> next

4.2 Current -> next = NULL

4.3 Release the memory [ free (Current) ]

else 4.1 for (i=1; i<Location; i++)

4.1.1 Previous = Current

4.1.2 Current = Current -> next

4.2 if ( Current == Last)

4.2.1 prev -> next = Current -> next

4.2.2 Last = Prev

else

4.2.1 Previous -> next = Current -> next

4.3 Current -> next = NULL

4.4 Release the memory [ free (Current) ]

}

Page 62: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

62

Write an algorithm to delete a node from the end of acircular linked list.

Deleting a Node From the End of the List

Page 63: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

63

1. Make current point tofirst node.

2. Repeat step c until thesuccessor of currentbecomes LAST.

a. Make currentpoint to the nextnode in itssequence.

3. Make the next field ofcurrent point to thesuccessor of LAST.

4. Release the memoryfor the node marked asLAST.

5. Mark current as LAST.

Algorithm for deleting a node from theend of a circular linked list.

Deleting a Node From the End of the List (Contd.)

LAST

15 17 2010

Page 64: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

64

10

LAST

10 15 17 20

Deleting a Node From the End of the List (Contd.)

1. Make current point toLAST.

2. Mark the predecessor ofLAST as previous. Tolocate the predecessor ofLAST, execute thefollowing steps:

a. Make previous pointto the successor ofLAST.

b. Repeat step c untilthe successor ofprevious becomesLAST.

c. Make previous pointto the next node inits sequence.

3. Make the next field ofprevious point to thesuccessor of LAST.

4. Mark previous as LAST.

5. Release the memory forthe node marked ascurrent.

current

Deleting a Node From the End of the List (Contd.)

Page 65: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

65

10

LAST

10 15 17 20

Deleting a Node From the End of the List (Contd.)

1. Make current point toLAST.

2. Mark the predecessor ofLAST as previous. Tolocate the predecessor ofLAST, execute thefollowing steps:

a. Make previous pointto the successor ofLAST.

b. Repeat step c untilthe successor ofprevious becomesLAST.

c. Make previous pointto the next node inits sequence.

3. Make the next field ofprevious point to thesuccessor of LAST.

4. Mark previous as LAST.

5. Release the memory forthe node marked ascurrent.

current

Deleting a Node From the End of the List (Contd.)

Page 66: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

66

10

LAST

10 15 17 20

current

Deleting a Node From the End of the List (Contd.)

1. Make current point toLAST.

2. Mark the predecessor ofLAST as previous. Tolocate the predecessor ofLAST, execute thefollowing steps:

a. Make previous pointto the successor ofLAST.

b. Repeat step c untilthe successor ofprevious becomesLAST.

c. Make previous pointto the next node inits sequence.

3. Make the next field ofprevious point to thesuccessor of LAST.

4. Mark previous as LAST.

5. Release the memory forthe node marked ascurrent.

Deleting a Node From the End of the List (Contd.)

Page 67: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

67

10

LAST

10 15 17 20

Current -> next = LAST -> next

current

Deleting a Node From the End of the List (Contd.)

1. Make current point toLAST.

2. Mark the predecessor ofLAST as previous. Tolocate the predecessor ofLAST, execute thefollowing steps:

a. Make previous pointto the successor ofLAST.

b. Repeat step c untilthe successor ofprevious becomesLAST.

c. Make previous pointto the next node inits sequence.

3. Make the next field ofprevious point to thesuccessor of LAST.

4. Mark previous as LAST.

5. Release the memory forthe node marked ascurrent.

Deleting a Node From the End of the List (Contd.)

Page 68: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

68

LAST

LAST = Current

1010 15 17 20

Current

LAST

Deleting a Node From the End of the List (Contd.)

1. Make current point toLAST.

2. Mark the predecessor ofLAST as previous. Tolocate the predecessor ofLAST, execute thefollowing steps:

a. Make previous pointto the successor ofLAST.

b. Repeat step c untilthe successor ofprevious becomesLAST.

c. Make previous pointto the next node inits sequence.

3. Make the next field ofprevious point to thesuccessor of LAST.

4. Mark previous as LAST.

5. Release the memory forthe node marked ascurrent.

Deleting a Node From the End of the List (Contd.)

Page 69: Circular Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk06/W6L1-Circular_Linked_List.pdf · Traversing a Circular Linked List Write an algorithm to traverse a circular

69

ALGORITHM TO DELETE A NODE FROM THE END

Algorithm DeleteAtEnd()

{

1. If (Last == NULL)

1.1 Print "underflow“

else If (Last -> next == Last )

1.1 Release the memory [ free (Last) ]

1.2 Last == NULL

else

1.1 Current = Last -> next

1.2 while ( Current -> next != Last )

1.2.1 Current = Current -> next

1.3 Current -> next = Last -> next

1.4 Last -> next = NULL

1.5 Release the memory [ free (Last) ]

1.6 Last = Current

}