131
Singly Linked Lists

Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

Singly Linked Lists

Page 2: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of 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

Page 3: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 4: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 5: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 6: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 7: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 8: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 9: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 10: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

10

Let us see how this happens.

Allocate memory for 3 Memory allocated for 32

3

1002

1036

Memory representation

Dynamic Memory Allocation (Contd.)

Page 11: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 12: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 13: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 14: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 15: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 16: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 17: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 18: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of 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

Page 19: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 20: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 21: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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;

Page 22: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 23: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 24: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 25: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 26: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 27: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 28: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 29: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 30: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 31: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 32: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 33: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 34: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 35: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 36: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 37: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 38: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 39: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 40: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 41: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 42: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 43: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 44: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 45: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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.

Page 46: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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.

Page 47: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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.

Page 48: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 49: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 50: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 51: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 52: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 53: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 54: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 55: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 56: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 57: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

57

TRAVERSING IN LIST

Algorithm traversing()

{

1.new1 = Start

2.while(new1 != NULL)

2.1 Print new1 -> info

2.2 new1 = new1 -> next

}

Page 58: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

58

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

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

Page 59: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 60: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 61: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 62: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 63: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 64: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 65: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

}

Page 66: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

66

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

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

Page 67: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 68: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 69: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 70: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 71: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 72: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 73: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 74: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 75: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 76: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 77: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 78: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

}

Page 79: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

79

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

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

Page 80: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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.

Page 81: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 82: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 83: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 84: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 85: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 86: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 87: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 88: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 89: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 90: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 91: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 92: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 93: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 94: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 95: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 96: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 97: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

}

Page 98: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 99: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

99

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

Deleting a Node From the Beginning of the List

Page 100: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 101: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 102: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 103: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 104: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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) ]

}

Page 105: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

105

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

Deleting a Node From the Beginning of the List

Page 106: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 107: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 108: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 109: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 110: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 111: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 112: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 113: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

}

Page 114: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

114

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

Deleting a Node Between two Nodes in the List

Page 115: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 116: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 117: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 118: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 119: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 120: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 121: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 122: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 123: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 124: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 125: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 126: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 127: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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

Page 128: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

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) ]

}

Page 129: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

129

Problem StatementDiscuss the advantages and disadvantages of linked lists .

Group Discussion

Page 130: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

130

Problem StatementDiscuss the differences between arrays and linked lists .

Group Discussion (Contd.)

Page 131: Singly Linked Lists - Nathaniel G. Martinds.nathanielgmartin.com/wk05/W5L2-Singly_Linked_List.pdf2 Objectives In this session, you will learn to: Identify the features of linked lists

131

Linked lists allow _________ access to elements.

Just a minute

Answer:sequential