05 DS and Algorithm Session 07
05 DS and Algorithm Session 07
Objectives
Ver. 1.0
Session 7
Suppose you have to write an algorithm to generate and store all prime numbers between 1 and 10,00,000 and display them. How will you solve this problem?
Ver. 1.0
Session 7
Consider the following algorithm, which uses an array to solve this problem:
1. Set I = 0 2. Repeat step 3 varying N from 2 to 1000000 3. If N is a prime number
a. Set A[I] = N b. I = I + 1 // If N is prime store it in an array
4. Repeat step 5 varying J from 0 to I-1 5. Display A[J] // Display the prime numbers
// stored in the array
Ver. 1.0
Session 7
Ver. 1.0
Session 7
Thus, you cannot use an array to store a set of elements if you 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 and when it is required.
Ver. 1.0
Session 7
If you knowdeclare an array, a contiguous block of array you When you the address of the first element in the memory can calculate the address of any other elements as shown: is allocated. Let Address of theyou declare an(size of of size 10 to store first us suppose first element + array the element index of the element) 10 prime numbers.
0 1 2 3 4 5 6 7 8 9
1000 1002 1004 1006 1008 1010 1012 1014 1016 1018
One contiguous block of memory allocated for the array to store 10 elements.
Memory representation
Ver. 1.0
Session 7
When memory is allocated dynamically, a block of memory is assigned arbitrarily from any location in the memory. Therefore, unlike arrays, these blocks may not be contiguous and may be spread randomly in the memory.
0 1 2 3 4 5 6 7 8 9
1000 1002 1004 1006 1008 1010 1012 1014 1016 1018
One contiguous block of memory allocated for the array to store 10 elements.
Memory representation
Ver. 1.0
Session 7
Let us see how this happens by allocating memory dynamically for the prime numbers.
1002
Memory representation
Ver. 1.0
Session 7
1002
1036
Memory representation
Ver. 1.0
Session 7
1002
2 5
1008
1036
Memory representation
Ver. 1.0
Session 7
1002
2 5
1008
1020
1036
Memory representation
Ver. 1.0
Session 7
1002
2 5
1008
1020
7 11 3
1030
1036
Memory representation
Ver. 1.0
Session 7
To access know the address of the know its address. Now, if youany element, you need to first element, you cannot calculate the address of the rest of the elements. This is because, all the elements are spread at random locations in the memory.
1002
2 5
1008
1020
7 11 3
1030
1036
Memory representation
Ver. 1.0
Session 7
Therefore, it would be good if every allocated block of memory contains the address of the next block in sequence. This gives the list a linked structure where each block is linked to the next block in sequence.
1002
2 5
1036 1020
1008
1020
7 11 3
1030
1030
1036
1008
Memory representation
Ver. 1.0
Session 7
An can declare data structure that that stores this concept Youexample of a a variable, START, implementsthe address is the first list. of a linked block. You can now begin at START and move through the list by following the links.
START
1002
2 5
1036 1020
1008
1020
7 11 3
1030
1030
1036
1008
Memory representation
Ver. 1.0
Session 7
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 is referred to as a node.
Ver. 1.0
Session 7
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
Data
Link
Node
Ver. 1.0
Session 7
The last node in a linked list does not point to any other All the nodes in a linked list are present at arbitrary memory node. Therefore, it points to NULL. locations. Therefore, every node in a linked list has link field that stores the address of the next node in sequence.
NULL
10 Data 1012
Ver. 1.0
Session 7
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 10 Data 3352 2403 10 Data 5689 3352 10 Data 1012 5689 10 Data 1012 NULL
Ver. 1.0
Session 7
Let us solve the given problem of storing prime numbers using a linked list.
1. Repeat step 2 varying N from 2 to 1000000. 2. If N is a prime number, insert it in the linked list:
a. Allocate memory for a new node. b. Store the prime number in the new node. c. Attach the new node in the linked list.
Ver. 1.0
Session 7
When a new prime number is generated, it should be inserted at the end of the list. Initially, when the list is empty, START contains NULL.
Ver. 1.0
Session 7
2.
3.
4.
5.
6.
Ver. 1.0
Consider NULL START = that the list is initially empty. Insert a prime number 2.
2.
3.
4.
5.
6.
Ver. 1.0
START = NULL
2.
3.
4.
5.
6.
Ver. 1.0
START = NULL
2.
3.
4.
2 10
5.
6.
Ver. 1.0
START = NULL
2.
3.
4.
2 10
5.
6.
Ver. 1.0
START = NULL
2.
3.
START
4.
2 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
Insertion complete
5.
6.
Ver. 1.0
2.
3.
START
4.
2 10
5.
6.
Ver. 1.0
2.
3.
START
4.
2 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
3 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
3 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
3 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
currentNode
3 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
currentNode
3 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
currentNode
3 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
currentNode
3 10
Insertion complete
5.
6.
Ver. 1.0
2.
3.
START
4.
2 10
currentNode
3 10
5.
6.
Ver. 1.0
2.
3.
START
4.
2 10
3 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
3 10
5 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
3 10
5 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
3 10
5 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
currentNode
3 10
5 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
currentNode
3 10
5 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
3 10
5 10
currentNode currentNode
5.
6.
Ver. 1.0
3.
START
4.
2 10
3 10
currentNode
5 10
5.
6.
Ver. 1.0
3.
START
4.
2 10
3 10
currentNode
5 10
Insertion complete
5.
6.
Ver. 1.0
What is thea better approach is to Therefore, problem with this algorithm? declare a variable, LAST, which will Consider aaddress of the last store the list of 10000 numbers. node in the a number at the end of To insert list. the list, you first need to locate the Hence, you need to maintain two last node in the and variables, START list. LAST, to To reach the last node, you have keep track of the first and last to start from the first node and nodes in the list respectively. visit all the preceding nodes If the list is empty, START node. before you reach the last and LAST point to NULL.
This approach is very time consuming for lengthy lists.
2.
3.
4.
5.
6.
Ver. 1.0
Refer to the given algorithm to insert a node at the end of the linked list.
2.
3.
4.
5. 6.
Ver. 1.0
Session 7
START = NULL Consider that the list is initially empty.= NULL LAST Insert a prime number 2.
2.
3.
4.
5. 6.
Ver. 1.0
Session 7
2.
3.
4.
5. 6.
Ver. 1.0
Session 7
2.
3.
2 10
4.
5. 6.
Ver. 1.0
Session 7
2.
3.
2 10
4.
5. 6.
Ver. 1.0
Session 7
2.
3.
START
2 10
4.
5. 6.
Ver. 1.0
Session 7
LAST = NULL
3.
START LAST
2 10
4.
5. 6.
Ver. 1.0
Session 7
3.
START LAST
2 10
4.
5. 6.
Ver. 1.0
Session 7
3.
START LAST
2 10
4.
5.
Insertion complete
6.
Ver. 1.0
Session 7
2.
3.
START LAST
2 10
4.
5. 6.
Ver. 1.0
Session 7
2.
3.
START LAST
2 10
4.
5. 6.
Ver. 1.0
Session 7
3.
START LAST
2 10
3 10
4.
5. 6.
Ver. 1.0
Session 7
3.
START LAST
2 10
3 10
4.
5. 6.
Ver. 1.0
Session 7
3.
START LAST
2 10
3 10
4.
5. 6.
Ver. 1.0
Session 7
3.
START LAST
LAST
2 10
3 10
4.
5. 6.
Ver. 1.0
Session 7
3.
START
LAST
2 10
3 10
4.
5.
Insertion complete
6.
Ver. 1.0
Session 7
2.
3.
START
LAST
2 10
3 10
4.
5. 6.
Ver. 1.0
Session 7
2.
3.
START
LAST
2 10
3 10
4.
5. 6.
Ver. 1.0
Session 7
3.
START
LAST
2 10
3 10
5 10
4.
5. 6.
Ver. 1.0
Session 7
3.
START
LAST
2 10
3 10
5 10
4.
5. 6.
Ver. 1.0
Session 7
3.
START
LAST
2 10
3 10
5 10
4.
5. 6.
Ver. 1.0
Session 7
3.
START
LAST
LAST
2 10
3 10
5 10
4.
5. 6.
Ver. 1.0
Session 7
3.
START
LAST
2 10
3 10
5 10
4.
5.
Insertion complete
6.
Ver. 1.0
Session 7
Ver. 1.0
Session 7
Consider the following algorithm, which uses an array to solve this problem.
1. Set N = 0 // Number of marks entered 2. Repeat until N = 20 a. Accept marks. b. Locate position I where the marks must be inserted. c. For J = N 1 down to I Move A[J] to A[J+1] // Move elements to make // place for the new element d. Set A[I] = marks e. N = N + 1
Ver. 1.0
Session 7
Let us perform some insert operations in the array by placing the elements at their appropriate positions to get a sorted list.
19
Ver. 1.0
Session 7
Let us perform some insert operations in the array by placing the elements at their appropriate positions to get a sorted list.
10 0 1 2 3 4 19
N=0
Ver. 1.0
Session 7
10 0 1 2 3 4 19
N=0
Ver. 1.0
Session 7
marks = 10
10 0 1 2 3 4 19
N=0
Ver. 1.0
Session 7
marks = 10
I=0
10 0 1 2 3 4 19
N=0
Ver. 1.0
Session 7
marks = 10
I=0
10 0 1 2 3 4 19
N=0
Ver. 1.0
Session 7
marks = 10
I=0
10 10 0 1 2 3 4 19
N=0
Ver. 1.0
Session 7
I=0
10 10 0 1 2 3 4 19
N=0
Ver. 1.0
Session 7
I=0
10 10 0 1 2 3 4 19
N=1
Ver. 1.0
Session 7
marks = 20
I=0
10 10 0 1 2 3 4 19
N=1
Ver. 1.0
Session 7
marks = 20
I=0 1
10 10 0 1 2 3 4 19
N=1
Ver. 1.0
Session 7
marks = 20
I=1
10 10 0 1 2 3 4 19
N=1
Ver. 1.0
Session 7
marks = 20
I=1
10 20 10 0 1 2 3 4 19
N=1
Ver. 1.0
Session 7
I=1
10 20 10 0 1 2 3 4 19
N=1
Ver. 1.0
Session 7
I=1
10 20 10 0 1 2 3 4 19
N=2
Ver. 1.0
Session 7
marks = 17
I=1
10 20 10 0 1 2 3 4 19
N=2
Ver. 1.0
Session 7
marks = 17
I=1
10 20 10 0 1 2 3 4 19
N=2
Ver. 1.0
Session 7
marks = 17
I=1
J=1 I
10 20 10 0 1 2 3 4 19
N=2
Ver. 1.0
Session 7
marks = 17
I=1
J=1 I
10 20 10 0 1 2 3 4 19
N=2
Ver. 1.0
Session 7
marks = 17
I=1
J=1 I
10 20 10 0 1
20
19
N=2
Ver. 1.0
Session 7
marks = 17
I=1
J=1 I
10 17 10 0 1
20
19
N=2
Ver. 1.0
Session 7
I=1
10 17 10 0 1
20
19
N=3
Ver. 1.0
Session 7
marks = 15
10 17 10 0 1
20
19
N=3
Ver. 1.0
Session 7
marks = 15
I=1
10 17 10 0 1
20
19
N=3
Ver. 1.0
Session 7
marks = 15
I=1
J=2 I
10 17 10 0 1
20
19
N=3
Ver. 1.0
Session 7
marks = 15
I=1
J=2 I
10 17 10 0 1
20
19
N=3
Ver. 1.0
Session 7
marks = 15
I=1
J=2 I
10 17 10 0 1
20
20
19
N=3
Ver. 1.0
Session 7
marks = 15
I=1
J=1 I
10 17 10 0 1 2
20
19
N=3
Ver. 1.0
Session 7
marks = 15
I=1
J=1 I
10 17 10 0 1 2
20
19
N=3
Ver. 1.0
Session 7
marks = 15
I=1
J=1 I
10 17 10 0 1
17
20
19
N=3
Ver. 1.0
Session 7
marks = 15
I=1
J=1 I
10 15 10 0 1
17
20
19
N=3
Ver. 1.0
Session 7
I=1
J=1 I
10 15 10 0 1
17
20
19
N=4
Ver. 1.0
Session 7
What is the problem in this algorithm? From the preceding example we conclude that insertion and deletion at anyelement atotherposition other thanan arrayof the To insert an position any than the end of the end is list, there is an additional complex and inefficient. overhead of shifting all succeeding elements one position this limitation? How can you overcome forward.
Similarly, to delete an element at any position shifting of data By using a data structure that does not requireother than the end of the list, every insert / delete succeeding elements after you need to shift the operation. elements one position backwards. A linked list offers this flexibility. When the list is very large, this would be very time consuming.
Ver. 1.0
Session 7
Consider a linked list that stores the marks of students in an ascending order. Insert marks (17). 17 should be inserted after 15.
20 10
1002 1011
START
10 2496 15 1002 20 1020 25 NULL
25 15
1020 2496
Memory representation
Ver. 1.0
Session 7
20 10
1002 1011
17 25
1008
START
10 2496
17
1020 2496
10
15 1002
10
20 1020
10
25 NULL
15
10
Memory representation
Ver. 1.0
Session 7
In the given list, node 15 contains the address of node 20. To add node 17 after node 15, you need to update the address field of node 15 so that it now contains the address of node 17.
20 10
1002 1011
17
25
1008
START
10 2496
17
1020 2496
10
15 1002
10
20 1020
10
25 NULL
15
10
Memory representation
Ver. 1.0
Session 7
Update the address field of node 15 to store the address of node 17.
20 10
1002 1011
17
1008
START
10 2496
17 25 15 1002
1020 2496
10
10
17
20 1020
10
25 NULL
10
15
Memory representation
Ver. 1.0
Session 7
Update the address field of node 15 to store the address of node 17.
20 10
1002 1011
17
1008
START
10 2496
25 1008 15 1002
1020 2496
10
10
17
20 1020
10
25 NULL
10
15
Ver. 1.0
Session 7
Store the address of node 20 in the address field of node 17 to make a complete list.
20 10
1002 1011
Node Inserted
START
10 2496
17
25
1008
1020 2496
10
15 1008
17 1002
20 1020
10
25 NULL
10
15
Ver. 1.0
Session 7
Now, let us solve the given problem using a linked list. Suppose the linked list currently contains the following elements.
START 10 15
17
20
The linked list needs to be created in the ascending order of values. Therefore, the position of a new node in the list will be determined by the value contained in the new node.
Ver. 1.0
Session 7
Write an algorithm to locate the position of the new node to be inserted in a linked list, where the nodes need to be stored in the increasing order of the values contained in them.
Ver. 1.0
Session 7
Algorithm for locating the position of a new node in the linked list. After executing this algorithm the variables/pointers previous and current will be placed on the nodes between which the new node is to be inserted.
2. 3.
4. 5.
Ver. 1.0
Session 7
2. 3.
4. 5.
START 10 15 17 20
Ver. 1.0
Session 7
2. 3.
4. 5.
START 10 10 15 17 20
current
Ver. 1.0
Session 7
4. 5.
START 10 10 15 17 20
Ver. 1.0
Session 7
4. 5.
START 10 10 15 17 20
Ver. 1.0
Session 7
4. 5.
START 10 10 15 17 20
Ver. 1.0
Session 7
4. 5.
START 10 10 15 17 20
previous current
current
Ver. 1.0
Session 7
4. 5.
START 10 10 15 17 20
previous
current
Ver. 1.0
Session 7
4. 5.
START 10 10 15 17 20
Ver. 1.0
Session 7
4. 5.
START 10 10 15 17 20
Ver. 1.0
Session 7
4. 5.
START 10 10 15 17 20
previous
current
Node located
Ver. 1.0
Session 7
Once the position of the new node has been determined, the new node can be inserted in the linked list. A new node can be inserted at any of the following positions in the list:
Beginning of the list End of the list Between two nodes in the list
Ver. 1.0
Session 7
Ver. 1.0
Session 7
2.
3.
START 10 15 17
4.
20
Ver. 1.0
Session 7
Insert 7
newnode
2.
3.
START 10 10 15 17
4.
20
Ver. 1.0
Session 7
Insert 7
newnode
2.
3.
START 10 10 15 17
4.
20
Ver. 1.0
Session 7
2.
newnode
3.
START 10 10 15 17
4.
20
Ver. 1.0
Session 7
2.
newnode
3.
START 10 10 15 17
4.
20 Insertion complete
Ver. 1.0
Session 7
Ver. 1.0
Session 7
2. 3.
4.
5.
Ver. 1.0
Session 7
Insert 16
START 10 10 15 17 20
2. 3.
4.
5.
Ver. 1.0
Session 7
Insert 16
START 10 10 current
2. 3.
15
17
20
4.
5.
Ver. 1.0
Session 7
Insert 16
START 10 10 current previous = NULL
2. 3.
15
17
20
4.
5.
Ver. 1.0
Session 7
Insert 16
START 10 10 current previous = NULL
2. 3.
15
17
20
4.
5.
Ver. 1.0
Session 7
Insert 16
START 10 10 previous current previous = NULL
2. 3.
15
17
20
4.
5.
Ver. 1.0
Session 7
Insert 16
START 10 10 previous current 15 current
2. 3.
17
20
4.
5.
Ver. 1.0
Session 7
Insert 16
START 10 10 previous 15 current
2. 3.
17
20
4.
5.
Ver. 1.0
Session 7
Insert 16
START 10 10 15 17 20
4.
5.
Ver. 1.0
Session 7
Insert 16
START 10 10 15 17 20
previous currentcurrent
2. 3.
4.
5.
Ver. 1.0
Session 7
Insert 16
START 10 10 15 previous 17 current
2. 3.
20
Nodes located
4.
5.
Ver. 1.0
Session 7
newnode
20
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 greater than newnode.info or current becomes equal to NULL. d. Make previous point to current. e. Make current point to the next node in sequence. Allocate memory for the new node. Assign value to the data field of the new node. Make the next field of the new node point to current. Make the next field of previous point to the new node.
Nodes located
4.
5.
Ver. 1.0
Session 7
newnode
16
START 10 10 15 previous 17 current
2. 3.
20
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 greater than newnode.info or current becomes equal to NULL. d. Make previous point to current. e. Make current point to the next node in sequence. Allocate memory for the new node. Assign value to the data field of the new node. Make the next field of the new node point to current. Make the next field of previous point to the new node.
4.
5.
Ver. 1.0
Session 7
newnode
16
START 10 10 15 previous 17 current
2. 3.
20
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 greater than newnode.info or current becomes equal to NULL. d. Make previous point to current. e. Make current point to the next node in sequence. Allocate memory for the new node. Assign value to the data field of the new node. Make the next field of the new node point to current. Make the next field of previous point to the new node.
newnode.next = current
4.
5.
Ver. 1.0
Session 7
newnode
16
START 10 10 15 previous 17 current
2. 3.
20
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 greater than newnode.info or current becomes equal to NULL. d. Make previous point to current. e. Make current point to the next node in sequence. Allocate memory for the new node. Assign value to the data field of the new node. Make the next field of the new node point to current. Make the next field of previous point to the new node.
newnode.next = current
previous.next = newnode
4.
5.
Insertion complete
Ver. 1.0
Session 7
Write an algorithm to traverse a list. Algorithm for traversing a linked singly-linked list.
2. 3.
START
4.
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
currentNode
3 10
5 10
7 10
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
currentNode
3 10
5 10
7 10
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
currentNode
3 10
5 10
7 10
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
3 10
5 10
7 10
currentNode currentNode
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
3 10
currentNode
5 10
7 10
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
3 10
currentNode
5 10
7 10
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
3 10
5 10
7 10
currentNode currentNode
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
3 10
5 10
currentNode
7 10
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
3 10
5 10
currentNode
7 10
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
3 10
5 10
7 10
currentNode currentNode
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
3 10
5 10
7 10
currentNode
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
3 10
5 10
7 10
currentNode
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
3 10
5 10
7 10
currentNode currentNode = NULL
Ver. 1.0
Session 7
2.
3.
START
4.
2 10
3 10
5 10
7 10
currentNode = NULL
Traversal complete
Ver. 1.0
Session 7
Delete operation in a linked list refers to the process of removing a specified node from the list. You can delete a node from the following places in a linked list:
Beginning of the list Between two nodes in the list End of the list
Ver. 1.0
Session 7
Ver. 1.0
Session 7
2.
3.
Ver. 1.0
Session 7
2.
START 10 10 15 17 20
3.
current
current = START
Ver. 1.0
Session 7
2.
START 10 10
START 15 17 20
3.
current
Ver. 1.0
Session 7
2.
START 10 15 17 20
3.
Session 7
Ver. 1.0
Session 7
3.
Ver. 1.0
Session 7
Delete 17
START 10 10 15 17 20
2.
3.
Ver. 1.0
Session 7
Delete 17
START 10 10 previous 15 17 20
2.
3.
Ver. 1.0
Session 7
Delete 17
3.
Ver. 1.0
Session 7
Delete 17
3.
Ver. 1.0
Session 7
Delete 17
3.
Ver. 1.0
Session 7
Delete 17
3.
Ver. 1.0
Session 7
Delete 17
3.
Ver. 1.0
Session 7
Delete 17
3.
Ver. 1.0
Session 7
Delete 17
START 10 10 15 17 20
2.
3.
Ver. 1.0
Session 7
Delete 17
3.
Ver. 1.0
Session 7
Delete 17
3.
previous.next = current.next
Ver. 1.0
Session 7
3.
previous.next = current.next
Ver. 1.0
Session 7
Problem Statement
Discuss the advantages and disadvantages of linked lists.
Ver. 1.0
Session 7
Problem Statement
Discuss the differences between arrays and linked lists.
Ver. 1.0
Session 7
Answer:
sequential
Ver. 1.0
Session 7
Ver. 1.0
Session 7
// Code in C++
class Node { public: int data; Node *next;
};
Ver. 1.0
Session 7
List class: This class consists of a set of operations implemented on a linked list. These operations are insertion, deletion, search, and traversal. It also contains the declaration of a variable/pointer, START that always points to the first node in the list. // Code in C#:
class Node { public int data; public Node next;
Ver. 1.0
Session 7
Ver. 1.0
Session 7
Ver. 1.0
Session 7
Problem Statement: Write a program to implement insert, search, delete, and traverse operations on a singly-linked list that stores the records of the students in a class. Each record holds the following information:
Roll number of the student Name of the student
Ver. 1.0
Session 7
Singly-linked list can be traversed only in a single direction. Insertion and deletion in a linked list is fast as compared to arrays. However, accessing elements is faster in arrays as compared to linked lists.
Ver. 1.0
Session 7