Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2...

Post on 12-Mar-2020

3 views 0 download

transcript

Singly Linked Lists

2

Objectives

In this session, you will learn to:Identify the features of linked lists

Implement a singly-linked list

Insertion in a linked List

Deletion from a Linked List

Traversing a Linked list

3

Suppose you have to write an algorithm to generate andstore all prime numbers between 1 and 10,00,000 anddisplay them.

How will you solve this problem?

Linked List

4

Consider the following algorithm, which uses an array tosolve this problem:1. Set I = 0

2. Repeat step 3 varying N from 2 to 1000000

3. If N is a prime numbera. Set A[I] = N // If N is prime store it in an array

b. I = I + 1

4. Repeat step 5 varying J from 0 to I-1

5. Display A[J] // Display the prime numbers // stored in the array

Linked List (Contd.)

5

What is the problem in this algorithm?The number of prime numbers between 1 and 10,00,000 is notknown. Since you are using an array to store the primenumbers, you need to declare an array of arbitrarily large sizeto store the prime numbers.

Disadvantages of this approach, suppose you declare an arrayof size N:

If the number of prime numbers between 1 and 10,00,000is more than N then all the prime numbers cannot bestored.

If the number of prime numbers is much less than N, a lotof memory space is wasted.

Linked List (Contd.)

6

Thus, you cannot use an array to store a set of elements ifyou do not know the total number of elements in advance.

How do you solve this problem?By having some way in which you can allocate memory as andwhen it is required.

Linked List (Contd.)

7

When you declare an array, a contiguous block of memory is allocated.

Let us suppose you declare an array of size 10 to store first 10 primenumbers.

If you know the address of the first element in the array you cancalculate the address of any other elements as shown:

Address of the first element + (size of the element × index of the element)

0123456789

100010021004

1018

10081010101210141016

1006One contiguous block ofmemory allocated forthe array to store 10elements.

Memory representation

Dynamic Memory Allocation

8

When memory is allocated dynamically, a block of memoryis assigned arbitrarily from any location in the memory.

Therefore, unlike arrays, these blocks may not becontiguous and may be spread randomly in the memory.

0123456789

100010021004

1018

10081010101210141016

1006One contiguous block ofmemory allocated forthe array to store 10elements.

Memory representation

Dynamic Memory Allocation (Contd.)

9

Let us see how this happens by allocating memorydynamically for the prime numbers.

Allocate memory for 2 Memory allocated for 221002

Memory representation

Dynamic Memory Allocation (Contd.)

10

Let us see how this happens.

Allocate memory for 3 Memory allocated for 32

3

1002

1036

Memory representation

Dynamic Memory Allocation (Contd.)

11

Let us see how this happens.

Allocate memory for 5 Memory allocated for 52

3

51008

1002

1036

Memory representation

Dynamic Memory Allocation (Contd.)

12

Let us see how this happens.

Allocate memory for 7 Memory allocated for 72

3

5

71020

1008

1002

1036

Memory representation

Dynamic Memory Allocation (Contd.)

13

Let us see how this happens.

Allocate memory for 11 Memory allocated for 112

3

5

7

111030

1020

1008

1002

1036

Memory representation

Dynamic Memory Allocation (Contd.)

14

If you know the address of the first element, you cannotcalculate the address of the rest of the elements.

This is because, all the elements are spread at randomlocations in the memory.

2

3

5

7

111030

1020

1008

1002

1036

Memory representation

To access any element, you need to know its address.

Dynamic Memory Allocation (Contd.)

15

Therefore, it would be good if every allocated block ofmemory contains the address of the next block insequence.

This gives the list a linked structure where each block islinked to the next block in sequence.

2

3

5

7

11

1036

1008

1020

1030

1030

1020

1008

1036

1002

Memory representation

Dynamic Memory Allocation (Contd.)

16

You can declare a variable, START, that stores the addressof the first block.

You can now begin at START and move through the list byfollowing the links.

START

1030

1020

1008

1036

1002 2

3

5

7

11

1036

1008

1020

1030

Memory representation

An example of a data structure that implements thisconcept is a linked list.

Dynamic Memory Allocation (Contd.)

17

Linked list:Is a dynamic data structure.

Allows memory to be allocated as and when it is required.

Consists of a chain of elements, in which each element isreferred to as a node.

Defining Linked Lists

18

Defining Linked Lists (Contd.)

Data Link

Node

A node is the basic building block of a linked list.

A node consists of two parts:Data: Refers to the information held by the node

Link: Holds the address of the next node in the list

19

10Data 3352 10Data 5689 10Data 1012 10Data

2403 3352 5689 1012

All the nodes in a linked list are present at arbitrary memorylocations.

Therefore, every node in a linked list has link field thatstores the address of the next node in sequence.

The last node in a linked list does not point to any othernode. Therefore, it points to NULL.

NULL

Defining Linked Lists (Contd.)

20

To keep track of the first node, declare a variable/pointer,START, which always points to the first node.

When the list is empty, START contains null.

START

10Data 3352 10Data 5689 10Data 1012 10Data

2403 3352 5689 1012

NULL

Defining Linked Lists (Contd.)

21

HOW TO CREATE A NODE

struct node

{

int info;

struct node *next;

};

struct node *new1;new1=(struct node*) malloc(sizeof(struct node))

new1->info=3;

new1->next=NULL;

22

A new node can be inserted at any of the following positionsin the list:

Beginning of the list

End of the List

At specific position

In a Sorted Linked List

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

23

Refer to the given algorithm toinsert a node at the end of thelinked list.

1. Allocate memory for the newnode.

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

3. If START is NULL, then (If thelist is empty):a. Make START point to the

new node.b. Make LAST point to the

new node.c. Go to step 6.

4. Make the next field of LASTpoint to the new node.

5. Mark the new node as LAST.

6. Make the next field of the newnode point to NULL.

Inserting a Node a Singly-Linked LIST

24

Consider that the list is initiallyempty.

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then (Ifthe list is empty):a. Make START point to

the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field of thenew node point to NULL.

START = NULL

LAST = NULL

Insert a prime number 2.

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

25

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

START = NULL

Insert a prime number 2.

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

26

102

START = NULL

LAST = NULL

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

27

102

START = NULL

LAST = NULL

START

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

28

102

START LAST

LAST = NULL

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

29

102

START LAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

30

Insertion complete

102

START LAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

31

Insert a prime number 3.

102

START LAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

32

102

START LAST

Insert a prime number 3.

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

33

102

START

103

LAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

34

102

START

103

LAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

35

102

START

103

LAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

36

102

START

103

LASTLAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

37

Insertion complete

102

START

103

LAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

38

Insert a prime number 5.

102

START

103

LAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

39

102

START

103

LAST

Insert a prime number 5.

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

40

102

START

103 105

LAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

41

102

START

103 105

LAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

42

102

START

103 105

LAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

43

102

START

103 105

LASTLAST

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. If START is NULL, then(If the list is empty):a. Make START point

to the new node.b. Make LAST point to

the new node.c. Go to step 6.

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

5. Mark the new node asLAST.

6. Make the next field ofthe new node point toNULL.

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

44

Insertion complete

102

START

103 105

LAST

1. Allocate memory for the newnode.

2. Assign value to the data fieldof the new node.

3. If START is NULL, then (If thelist is empty):a. Make START point to the

new node.b. Make LAST point to the

new node.c. Go to step 6.

4. Make the next field of LASTpoint to the new node.

5. Mark the new node as LAST.

6. Make the next field of thenew node point to NULL.

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

45

ALGORITHM TO INSERT NODE AT END

Start=NULL

Algorithm InsertAtEnd()

{

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

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

3.if(Start == NULL)

3.1 Last=new1

3.2 Start=new1

else

3.1 Last -> next = new1

3.2 Last = new1

4. Last ->next = NULL

}

1. Allocate memory for the new node.

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

3. If START is NULL, then (If the list isempty):a. Make START point to the new

node.b. Make LAST point to the new node.c. Go to step 6.

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

5. Mark the new node as LAST.

6. Make the next field of the new nodepoint to NULL.

46

Write an algorithm to traverse a singly-linked list.

Traversing a Singly-Linked List

Algorithm for traversing a linked list.

2

START

3 5 7

1. Make NEW1 pointto the first node inthe list.

2. Repeat step 3 and 4until NEW1becomes NULL.

3. Display theinformationcontained in thenode marked asNEW1

4. Make NEW1 pointto the next node insequence.

47

Refer to the algorithm to display theelements in the linked list.

102

START

103 105 107

NEW1

Traversing a Singly-Linked List (Contd.)

1. Make NEW1 point tothe first node in thelist.

2. Repeat step 3 and 4until NEW1 becomesNULL.

3. Display the informationcontained in the nodemarked as NEW1

4. Make NEW1 point tothe next node insequence.

48

Refer to the algorithm to display theelements in the linked list.

102

START

103 105 107

NEW1

1. Make NEW1 point tothe first node in thelist.

2. Repeat step 3 and 4until NEW1 becomesNULL.

3. Display the informationcontained in the nodemarked as NEW1

4. Make NEW1 point tothe next node insequence.

Traversing a Singly-Linked List (Contd.)

49

102

START

103 105 107

NEW1

2

1. Make NEW1 point tothe first node in thelist.

2. Repeat step 3 and 4until NEW1 becomesNULL.

3. Display the informationcontained in the nodemarked as NEW1

4. Make NEW1 point tothe next node insequence.

Traversing a Singly-Linked List (Contd.)

50

102

START

103 105 107

2

NEW1

1. Make NEW1 point tothe first node in thelist.

2. Repeat step 3 and 4until NEW1 becomesNULL.

3. Display the informationcontained in the nodemarked as NEW1

4. Make NEW1 point tothe next node insequence.

Traversing a Singly-Linked List (Contd.)

51

102

START

103 105 107

2

NEW1

3

1. Make NEW1 point tothe first node in thelist.

2. Repeat step 3 and 4until NEW1 becomesNULL.

3. Display the informationcontained in the nodemarked as NEW1

4. Make NEW1 point tothe next node insequence.

Traversing a Singly-Linked List (Contd.)

52

102

START

103 105 107

2 3

NEW1

1. Make NEW1 point tothe first node in thelist.

2. Repeat step 3 and 4until NEW1 becomesNULL.

3. Display the informationcontained in the nodemarked as NEW1

4. Make NEW1 point tothe next node insequence.

Traversing a Singly-Linked List (Contd.)

53

102

START

103 105 107

2 3

NEW1

5

1. Make NEW1 point tothe first node in thelist.

2. Repeat step 3 and 4until NEW1 becomesNULL.

3. Display the informationcontained in the nodemarked as NEW1

4. Make NEW1 point tothe next node insequence.

Traversing a Singly-Linked List (Contd.)

54

102

START

103 105 107

2 3 5

NEW1

1. Make NEW1 point tothe first node in thelist.

2. Repeat step 3 and 4until NEW1 becomesNULL.

3. Display the informationcontained in the nodemarked as NEW1

4. Make NEW1 point tothe next node insequence.

Traversing a Singly-Linked List (Contd.)

55

102

START

103 105 107

2 3 5

NEW1

7

1. Make NEW1 point tothe first node in thelist.

2. Repeat step 3 and 4until NEW1 becomesNULL.

3. Display the informationcontained in the nodemarked as NEW1

4. Make NEW1 point tothe next node insequence.

Traversing a Singly-Linked List (Contd.)

56

102

START

103 105 107

2 3 5 7

NEW1 = NULL

Traversal complete

1. Make NEW1 point tothe first node in thelist.

2. Repeat step 3 and 4until NEW1 becomesNULL.

3. Display the informationcontained in the nodemarked as NEW1

4. Make NEW1 point tothe next node insequence.

Traversing a Singly-Linked List (Contd.)

57

TRAVERSING IN LIST

Algorithm traversing()

{

1.new1 = Start

2.while(new1 != NULL)

2.1 Print new1 -> info

2.2 new1 = new1 -> next

}

58

Write an algorithm to insert a node in the beginning of alinked list.

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

59

START

15 17 20

1. Allocate memory for thenew node.

2. Assign value to the datafield of the new node.

3. Make the next field ofthe new node point tothe first node in the list.

4. Make START, point tothe new node.

Algorithm to insert a node in thebeginning of a linked list

10

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

60

10

START

10 15 17 20

new1

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 first node in thelist.

4. Make START, point tothe new node.

Insert 7

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

61

10

START

10 15 20

new1

7

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 first node in thelist.

4. Make START, point tothe new node.

17

Insert 7

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

62

10

START

10 15 17 20

new1

7

new1 -> next = START

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 first node in thelist.

4. Make START, point tothe new node.

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

63

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 first node in thelist.

4. Make START, point tothe new node.

START = new1

New1 -> next = START

Insertion complete

10

START

10 15 17 20

new1

7

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

64

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 first node in thelist.

4. Make START, point tothe new node.

START = new1

New1 -> next = START

Insertion complete

1010 15 17 20

START

7

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

65

ALGORITHM TO INSERT NODE AT BEGINING

Start=NULL

Algorithm InsertAtBEG()

{

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

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

3. If (Start == NULL)

3.1 new1 -> next = NULL

3.2 Last=new1

3.3 Start=new1

else

3.1 new1 -> next=Start

3.2 Start = new1

}

66

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

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

67

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 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.

Insert 16 At LOC = 3

START

5 17 210

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

68

10

START

10 5 17 2

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 node pointto the next of previous node

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

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

69

10

START

10 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 node pointto the next of previous node

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

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

70

10

START

10 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 node pointto the next of previous node

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

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

71

10

START

10 5 17 2

previous

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 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.

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

72

10

START

10 5 17 2

previous

16

new1

Count=1

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.

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

73

10

START

10 5 17 2

previous

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.

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

74

10

START

10 5 17 2

Nodes located

Insert 16

previous

16

new1

1.Allocate memory for the newnode.

2. Assign value to the data fieldof the new node.

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

point to the first nodeand set count=1

b. Repeat step c and stepd until count becomesequal to location-1

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

next node in sequence

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

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

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

75

10

START

10 5 17 2

16

new1

new1 -> next = previous->next

previous

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.

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

76

10

START

10 5 17 2

16

new1

previous -> next = new1

new1 -> next = previous->next

previous

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.

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

77

10

START

10 5 17 2

16

previous -> next = new1

Insertion complete

previous

new1 -> next = previous->next

1.Allocate memory for the new node.

2. Assign value to the data field ofthe new 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 setcount=1

b. Repeat step c and step duntil 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 previouspoint to the new node.

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

78

ALGORITHM TO INSERT NODE At PARTICULAR POSITION IN A LINKED LIST

Algorithm 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 = Start

4.2 Start = new1

Else

4.1 Previous = Start

4.2 Count = 1

4.3 While( Count <= Location - 1 && Previous->next !=NULL)

4.3.1 Previous = Previous -> next

4.3.2 Count++

4.4 new1 -> next = Previous -> next

4.5 Previous -> next = new1

}

79

Write an algorithm to insert a node in a Sorted linked list.

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

80

10

START

10 15 17 20

Insert 16

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

1. Allocate memory for the new node.2. Assign value to the data field of the new node.3. If data of newnode is less than the data of start

node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between which the

new node is to be inserted. Mark them asprevious and current. To locate previous andcurrent, execute the following steps:a. Make current point to the first node.b. Make previous point to NULL.c. Repeat step d and step e until current.info

becomes greater than newnode.info orcurrent becomes equal to NULL.

d. Make previous point to current.e. Make current point to the next node in

sequence.5. Make the next field of the new node point to

current.6. Make the next field of previous point to thenew node.

81

10

START

10 15 17 20

new1

1. Allocate memory for the new node.2. Assign value to the data field of the

new node.3. If data of newnode is less than the

data of start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to beinserted. Mark them as previousand current. To locate previous andcurrent, execute the following steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greaterthan newnode.info or currentbecomes equal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node

point to current.6. Make the next field of previous point

to the new node.

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

82

10

START

10 15 17 20

16

new1

1. Allocate memory for the new node.2. Assign value to the data field of the new

node.3. If data of newnode is less than the data of

start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to be inserted.Mark them as previous and current. Tolocate previous and current, execute thefollowing steps:a. Make current point to the first node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greater thannewnode.info or current becomesequal to NULL.

d. Make previous point to current.e. Make current point to the next node

in sequence.5. Make the next field of the new node point

to current.6. Make the next field of previous point to thenew node.

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

83

10

START

10 15 17 20

16

new1

1. Allocate memory for the new node.2. Assign value to the data field of the new

node.3. If data of newnode is less than the data

of start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to be inserted.Mark them as previous and current. Tolocate previous and current, executethe following steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greaterthan newnode.info or currentbecomes equal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node point

to current.6. Make the next field of previous point to

thenew node.

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

84

10

START

10 15 17 20

16

new1

1. Allocate memory for the new node.2. Assign value to the data field of the

new node.3. If data of newnode is less than the

data of start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes

between which the new node is tobe inserted. Mark them as previousand current. To locate previous andcurrent, execute the following steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greaterthan newnode.info or currentbecomes equal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node

point to current.6. Make the next field of previous point

to the new node.

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

85

10

START

10 15 17 20

current

16

new1

1. Allocate memory for the new node.2. Assign value to the data field of the

new node.3. If data of newnode is less than the

data of start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to beinserted. Mark them as previous andcurrent. To locate previous andcurrent, execute the following steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greaterthan newnode.info or currentbecomes equal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node

point to current.6. Make the next field of previous point to

the new node.

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

86

10

START

10 15 17 20

current

previous = NULL

16

new1

1. Allocate memory for the new node.2. Assign value to the data field of the new

node.3. If data of newnode is less than the data

of start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to be inserted.Mark them as previous and current. Tolocate previous and current, executethe following steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greater thannewnode.info or current becomesequal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node point

to current.6. Make the next field of previous point to

the new node.

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

87

10

START

10 15 17 20

current

previous = NULL

16

new1

1. Allocate memory for the new node.2. Assign value to the data field of the new

node.3. If data of newnode is less than the data of

start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to be inserted.Mark them as previous and current. Tolocate previous and current, execute thefollowing steps:a. Make current point to the first node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greater thannewnode.info or current becomesequal to NULL.

d. Make previous point to current.e. Make current point to the next node

in sequence.5. Make the next field of the new node point

to current.6. Make the next field of previous point to thenew node.

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

88

10

START

10 15 17 20

currentprevious

16

new1

1. Allocate memory for the new node.2. Assign value to the data field of the

new node.3. If data of newnode is less than the

data of start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to beinserted. Mark them as previous andcurrent. To locate previous andcurrent, execute the following steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greaterthan newnode.info or currentbecomes equal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node

point to current.6. Make the next field of previous point to

the new node.

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

89

10

START

10 15 17 20

currentprevious

16

new1

1. Allocate memory for the new node.2. Assign value to the data field of the new

node.3. If data of newnode is less than the data

of start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to be inserted.Mark them as previous and current. Tolocate previous and current, executethe following steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greaterthan newnode.info or currentbecomes equal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node

point to current.6. Make the next field of previous point to

the new node.

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

90

10

START

10 15 17 20

currentprevious

16

new1

1. Allocate memory for the new node.2. Assign value to the data field of the

new node.3. If data of newnode is less than the data

of start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to be inserted.Mark them as previous and current.To locate previous and current,execute the following steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greaterthan newnode.info or currentbecomes equal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node

point to current.6. Make the next field of previous point to

the new node.

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

91

10

START

10 15 17 20

currentprevious

16

new1

1. Allocate memory for the new node.2. Assign value to the data field of the new

node.3. If data of newnode is less than the data

of start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to be inserted.Mark them as previous and current. Tolocate previous and current, executethe following steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greaterthan newnode.info or currentbecomes equal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node

point to current.6. Make the next field of previous point to

the new node.

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

92

10

START

10 15 17 20

currentprevious

16

new1

1. Allocate memory for the new node.2. Assign value to the data field of the new

node.3. If data of newnode is less than the data

of start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to be inserted.Mark them as previous and current. Tolocate previous and current, executethe following steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greater thannewnode.info or current becomesequal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node point

to current.6. Make the next field of previous point to

the new node.

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

93

10

START

10 15 17 20

Nodes located

currentprevious

16

new1

1. Allocate memory for the new node.2. Assign value to the data field of the new

node.3. If data of newnode is less than the data of

start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to be inserted.Mark them as previous and current. Tolocate previous and current, execute thefollowing steps:a. Make current point to the first node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greater thannewnode.info or current becomesequal to NULL.

d. Make previous point to current.e. Make current point to the next node

in sequence.5. Make the next field of the new node point

to current.6. Make the next field of previous point to thenew node.

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

94

10

START

10 15 17 20

16

new1

new1 -> next = current

currentprevious

1. Allocate memory for the new node.2. Assign value to the data field of the new

node.3. If data of newnode is less than the data

of start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to be inserted.Mark them as previous and current. Tolocate previous and current, executethe following steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greater thannewnode.info or current becomesequal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node

point to current.6. Make the next field of previous point to

the new node.

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

95

10

START

10 15 17 20

16

new1

previous -> next = new1

new1 -> next = current

Insertion complete

1. Allocate memory for the new node.2. Assign value to the data field of the new

node.3. If data of newnode is less than the data of

start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to be inserted.Mark them as previous and current. Tolocate previous and current, execute thefollowing steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greater thannewnode.info or current becomesequal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node point

to current.6. Make the next field of previous point to

the new node.

currentprevious

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

96

10

START

10 15 17 20

16

newnode

previous -> next = new1

new1 -> next = current

Insertion complete

1. Allocate memory for the new node.2. Assign value to the data field of the new

node.3. If data of newnode is less than the data

of start node, Thena. make newnode point to the

start nodeb. make start point to the

newnode4. Otherwise,Identify the nodes between

which the new node is to be inserted.Mark them as previous and current.To locate previous and current,execute the following steps:a. Make current point to the first

node.b. Make previous point to NULL.c. Repeat step d and step e until

current.info becomes greaterthan newnode.info or currentbecomes equal to NULL.

d. Make previous point to current.e. Make current point to the next

node in sequence.5. Make the next field of the new node

point to current.6. Make the next field of previous point to

the new node.

currentprevious

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

97

ALGORITHM TO INSERT NODE IN A SORTED LINKED LIST

Algorithm InsertAtSorted()

{

1. Enter Data

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

3. new1->info=Data

4. If (Data <= Start -> info)

4.1 new1 -> next = Start

4.2 Start = new1

Else

4.1 Current = Start

4.2 Previous = NULL

4.3While( Data >= Current -> info && Current != NULL)

4.3.1 Previous = Current

4.3.2 Current = Current -> next

4.4 new1 -> next = Current

4.5 Previous -> next = new1

}

98

Deleting a Node from a Singly-Linked List

Delete operation in a linked list refers to the process ofremoving a specified node from the list.

You can delete a node from the following places in a linkedlist:

Beginning of the list

Between two nodes in the list

End of the list

99

Write an algorithm to delete the first node in a linked list.

Deleting a Node From the Beginning of the List

100

1. Mark the first node in the listas current.

2. Make START point to thenext node in its sequence.

3. Release the memory for thenode marked as current.

START

15 17 20

Algorithm to delete a node from thebeginning of a linked list.

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

10

101

10

START

15 17 20

current = START

10

current

1. Mark the first node in the listas current.

2. Make START point to thenext node in its sequence.

3. Release the memory for thenode marked as current.

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

102

10

START

15 17 20

current = START

10

START = START -> next

current

START

1. Mark the first node in the listas current.

2. Make START point to thenext node in its sequence.

3. Release the memory for thenode marked as current.

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

103

START

15 17 20

Memory released

10

current

Delete operation complete

current = STARTSTART = START -> next

1. Mark the first node in the listas current.

2. Make START point to thenext node in its sequence.

3. Release the memory for thenode marked as current.

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

104

ALGORITHM TO DELETE A NODE FROM THE BEGINNING

Algorithm DeleteAtBeg()

{

1. If (Start == NULL)

1.1 Print "underflow“

else

1.1 Current = Start

1.2 Start = Start -> next

1.3 Current -> next = NULL

1.4 Release the memory [ free (Current) ]

}

105

Write an algorithm to delete the End node in a linked list.

Deleting a Node From the Beginning of the List

106

1. Mark the firstnode in the list ascurrent..

2. Repeat step 3untill currentbecomes Lastnode.

3. Make currentpoint to the nextnode in itssequence.

4. Release thememory for thenode marked asLast.

5. Make Currentpoint to the lastnode

START

15 17 20

Algorithm to delete a node from theEnd.

Deleting a Node From the Beginning

of the List (Contd.)

10

Last

107

10

START

15 17 20

current = START

10

current

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

1. Mark the first node in the listas current..

2. Repeat step 3 untill currentbecomes Last node.

3. Make current point to thenext node in its sequence.

4. Release the memory for thenode marked as Last.

5. Make Current point to thelast node

Last

108

10

START

15 17 2010

current

1. Mark the first node in the listas current..

2. Repeat step 3 untill currentbecomes Last node.

3. Make current point to thenext node in its sequence.

4. Release the memory for thenode marked as Last.

5. Make Current point to thelast node

Last

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

109

10

START

15 17 2010

1. Mark the first node in the listas current..

2. Repeat step 3 untill next ofcurrent becomes Last node.

3. Make current point to thenext node in its sequence.

4. Release the memory for thenode marked as Last.

5. Make Current point to thelast node

Last

current

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

110

10

START

15 17 2010

1. Mark the first node in the listas current..

2. Repeat step 3 untill next ofcurrent becomes Last node.

3. Make current point to thenext node in its sequence.

4. Release the memory for thenode marked as Last.

5. Make Current point to thelast node

Last

current

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

111

10

START

15 17 2010

1. Mark the first node in the listas current..

2. Repeat step 3 untill next ofcurrent becomes Last node.

3. Make current point to thenext node in its sequence.

4. Release the memory for thenode marked as Last.

5. Make Current point to thelast node

Last

current

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

112

10

START

15 17 2010

1. Mark the first node in the listas current..

2. Repeat step 3 untill next ofcurrent becomes Last node.

3. Make current point to thenext node in its sequence.

4. Release the memory for thenode marked as Last.

5. Make Current point to thelast node

Last

current

Delete operation complete

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

113

ALGORITHM TO DELETE A NODE FROM THE END

Algorithm DeleteAtEnd()

{

1. If (Start == NULL)

1.1 Print "underflow“

else If (Start -> next == NULL )

1.1 Release the memory [ free (Start) ]

1.2 Start == NULL

else

1.1 Current = Start

1.2 while ( Current -> next != Last )

1.2.1 Current = Current -> next

1.3 Current -> next = NULL

1.4 Release the memory [ free (Last) ]

1.5 Last = Current

}

114

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

Deleting a Node Between two Nodes in the List

115

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 = STARTc. Repeat step d and e

until either the nodeis found or currentbecomes NULL.

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

START

15 17 2010

116

1. Locate the node to be deleted. Mark thenode to be deleted as current and itspredecessor as previous. To locatecurrent and previous, execute thefollowing steps:

a. Set previous = NULLb. Set current = START c. Repeat step d and e until

either the node is found orcurrent becomes NULL.

d. Make previous point tocurrent.

e. Make current point to thenext node in sequence.

2. Make the next field of previous point tothe successor of current.

3. Release the memory for the nodemarked as current.

Delete 17

10

START

10 15 17 20

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

117

10

START

10 15 17 20

1. Locate the node to be deleted. Mark thenode to be deleted as current and itspredecessor as previous. To locatecurrent and previous, execute thefollowing steps:

a. Set previous = NULLb. Set current = START c. Repeat step d and e until

either the node is found orcurrent becomes NULL.

d. Make previous point tocurrent.

e. Make current point to thenext node in sequence.

2. Make the next field of previous point tothe successor of current.

3. Release the memory for the nodemarked as current.

Delete 17

Previous = NULL

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

118

10

START

10 15 17 20

Previous = NULL

current

1. Locate the node to be deleted. Mark thenode to be deleted as current and itspredecessor as previous. To locatecurrent and previous, execute thefollowing steps:

a. Set previous = NULLb. Set current = START c. Repeat step d and e until

either the node is found orcurrent becomes NULL.

d. Make previous point tocurrent.

e. Make current point to thenext node in sequence.

2. Make the next field of previous point tothe successor of current.

3. Release the memory for the nodemarked as current.

Delete 17

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

119

10

START

10 15 17 20

current

1. Locate the node to be deleted. Mark thenode to be deleted as current and itspredecessor as previous. To locatecurrent and previous, execute thefollowing steps:

a. Set previous = NULLb. Set current = START c. Repeat step d and e until

either the node is found orcurrent becomes NULL.

d. Make previous point tocurrent.

e. Make current point to thenext node in sequence.

2. Make the next field of previous point tothe successor of current.

3. Release the memory for the nodemarked as current.

Delete 17

Previous = NULL

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

120

10

START

10 15 17 20

current

1. Locate the node to be deleted. Mark thenode to be deleted as current and itspredecessor as previous. To locatecurrent and previous, execute thefollowing steps:

a. Set previous = NULLb. Set current = START c. Repeat step d and e until

either the node is found orcurrent becomes NULL.

d. Make previous point tocurrent.

e. Make current point to thenext node in sequence.

2. Make the next field of previous point tothe successor of current.

3. Release the memory for the nodemarked as current.

Delete 17

previous

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

121

10

START

10 15 17 20

currentcurrent

1. Locate the node to be deleted. Mark thenode to be deleted as current and itspredecessor as previous. To locatecurrent and previous, execute thefollowing steps:

a. Set previous = NULLb. Set current = START c. Repeat step d and e until

either the node is found orcurrent becomes NULL.

d. Make previous point tocurrent.

e. Make current point to thenext node in sequence.

2. Make the next field of previous point tothe successor of current.

3. Release the memory for the nodemarked as current.

Delete 17

previous

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

122

10

START

10 15 17 20

current

1. Locate the node to be deleted. Mark thenode to be deleted as current and itspredecessor as previous. To locatecurrent and previous, execute thefollowing steps:

a. Set previous = NULLb. Set current = START c. Repeat step d and e until

either the node is found orcurrent becomes NULL.

d. Make previous point tocurrent.

e. Make current point to thenext node in sequence.

2. Make the next field of previous point tothe successor of current.

3. Release the memory for the nodemarked as current.

Delete 17

previous

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

123

10

START

10 15 17 20

previous current

1. Locate the node to be deleted. Mark thenode to be deleted as current and itspredecessor as previous. To locatecurrent and previous, execute thefollowing steps:

a. Set previous = NULLb. Set current = START c. Repeat step d and e until

either the node is found orcurrent becomes NULL.

d. Make previous point tocurrent.

e. Make current point to thenext node in sequence.

2. Make the next field of previous point tothe successor of current.

3. Release the memory for the nodemarked as current.

Delete 17

previous

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

124

1. Locate the node to be deleted. Mark thenode to be deleted as current and itspredecessor as previous. To locatecurrent and previous, execute thefollowing steps:

a. Set previous = NULLb. Set current = START c. Repeat step d and e until

either the node is found orcurrent becomes NULL.

d. Make previous point tocurrent.

e. Make current point to thenext node in sequence.

2. Make the next field of previous point tothe successor of current.

3. Release the memory for the nodemarked as current.

Delete 17

10

START

10 15 17 20

current currentprevious

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

125

1. Locate the node to be deleted. Mark thenode to be deleted as current and itspredecessor as previous. To locatecurrent and previous, execute thefollowing steps:

a. Set previous = NULLb. Set current = START c. Repeat step d and e until

either the node is found orcurrent becomes NULL.

d. Make previous point tocurrent.

e. Make current point to thenext node in sequence.

2. Make the next field of previous point tothe successor of current.

3. Release the memory for the nodemarked as current.

Delete 17

10

START

10 15 17 20

currentprevious

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

126

Previous -> next = current -> next

1. Locate the node to be deleted. Mark thenode to be deleted as current and itspredecessor as previous. To locatecurrent and previous, execute thefollowing steps:

a. Set previous = NULLb. Set current = START c. Repeat step d and e until

either the node is found orcurrent becomes NULL.

d. Make previous point tocurrent.

e. Make current point to thenext node in sequence.

2. Make the next field of previous point tothe successor of current.

3. Release the memory for the nodemarked as current.

Delete 17

10

START

10 15 17 20

currentprevious

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

127

Delete operation complete

1. Locate the node to be deleted. Mark thenode to be deleted as current and itspredecessor as previous. To locatecurrent and previous, execute thefollowing steps:

a. Set previous = NULLb. Set current = START c. Repeat step d and e until

either the node is found orcurrent becomes NULL.

d. Make previous point tocurrent.

e. Make current point to thenext node in sequence.

2. Make the next field of previous point tothe successor of current.

3. Release the memory for the nodemarked as current.

Delete 17

10

START

10 15 17 20

current

Previous -> next = current -> next

previous

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

128

ALGORITHM TO DELETE A NODE FROM THE SPECIFIC POSITION

Algorithm DeleteAtSpec()

{

1. Enter the Location

2. Current = Start

3. Previous = NULL

4. If (Start == 0)

4.1 Print "underflow“

else If ( Location == 1)

4.1 Start = Start -> next

4.2 Current -> next = NULL

4.3 Release the memory [ free (Current) ]

else

4.1 for (i=1 to Location-1)

4.1.1 Previous = Current

4.1.2 Current = Current -> next

4.2 Previous -> next = Current -> next

4.3 Current -> next = NULL

4.4 Release the memory [ free (Current) ]

}

129

Problem StatementDiscuss the advantages and disadvantages of linked lists .

Group Discussion

130

Problem StatementDiscuss the differences between arrays and linked lists .

Group Discussion (Contd.)

131

Linked lists allow _________ access to elements.

Just a minute

Answer:sequential