Lecture 13 - Linked List
Lecture 13 - Linked List
Lecture Thirteen
Linked List
memory locations.
They may be anywhere, but the accessing of these data
items is easier as each data item contains the address
of the next data item.
1
C++ Programming Language 4/16/2023
2
C++ Programming Language 4/16/2023
3
C++ Programming Language 4/16/2023
4
C++ Programming Language 4/16/2023
10
5
C++ Programming Language 4/16/2023
6
C++ Programming Language 4/16/2023
1. Creation.
2. Insertion.
3. Deletion.
4. Traversing.
14
7
C++ Programming Language 4/16/2023
8
C++ Programming Language 4/16/2023
18
9
C++ Programming Language 4/16/2023
Insertion of a Node
One of the most primitive operations that can be
done in a singly linked list is the insertion of a node.
Memory is to be allocated for the new node (in a similar
way that is done while creating a list) before reading the
data.
The new node will contain empty data field and empty next
field.
C++ Programming Language
The data field of the new node is then stored with the
information read from the user.
The next field of the new node is assigned to NULL.
The new node can then be inserted at three different
places namely:
1. Inserting a node at the beginning.
2. Inserting a node at the end. 20
3. Inserting a node at intermediate position.
10
C++ Programming Language 4/16/2023
21
{
start = newnode;
}
else
{
newnode -> next = start;
start = newnode;
} 22
}
11
C++ Programming Language 4/16/2023
23
newnode = getnode();
if(start == NULL)
start = newnode;
else
{
temp = start;
while(temp -> next != NULL)
temp = temp -> next;
temp -> next = newnode;
} 24
}
12
C++ Programming Language 4/16/2023
newnode = getnode();
cout<<“\n Enter the position: ";
cin>>&pos;
nodectr = countnode(start);
if(pos > 1 && pos < nodectr) {
temp = prev = start;
while(ctr < pos)
{
prev = temp;
temp = temp -> next; 26
ctr++; }
13
C++ Programming Language 4/16/2023
27
Deletion of a Node
Another primitive operation that can be done in
a singly linked list is the deletion of a node.
Memory is to be released for the node to be
deleted.
A node can be deleted from the list from three
C++ Programming Language
14
C++ Programming Language 4/16/2023
29
15
C++ Programming Language 4/16/2023
31
else {
temp = start;
prev = start;
while(temp -> next != NULL) {
prev = temp;
temp = temp -> next; }
prev -> next = NULL;
free(temp);
cout<<"\n Node deleted "); } 32
}
16
C++ Programming Language 4/16/2023
void delete_at_mid() {
int ctr = 1, pos, nodectr;
node *temp, *prev;
if(start == NULL) {
C++ Programming Language
17
C++ Programming Language 4/16/2023
18
C++ Programming Language 4/16/2023
{
if(st = = NULL)
return 0;
els e
return(1 + countnode(st - > next));
}
38
19
C++ Programming Language 4/16/2023
20
C++ Programming Language 4/16/2023
21
C++ Programming Language 4/16/2023
newnode = getnode();
if(start == NULL) {
start = newnode; }
else {
emp = start;
while(temp -> next != NULL)
temp = temp -> next; 44
temp -> next = newnode; } } }
22
C++ Programming Language 4/16/2023
if(start == NULL) {
return; }
else {
rev_traverse(start -> next);
cout<<"%d -->", start -> data; }
}
46
23
C++ Programming Language 4/16/2023
nodectr = countnode(start);
if(pos > 1 && pos < nodectr) {
temp = prev = start;
while(ctr < pos) {
prev = temp;
emp = temp -> next;
ctr++; }
prev -> next = newnode;
newnode -> next = temp; }
48
else cout<<"position %d is not a middle position", pos; }
24
C++ Programming Language 4/16/2023
cin>> &pos;
nodectr = countnode(start);
if(pos > nodectr) {
cout<<"\nThis node doesnot exist"; }
if(pos > 1 && pos < nodectr) {
temp = prev = start;
while(ctr < pos) {
prev = temp;
temp = temp -> next;
ctr ++; }
prev -> next = temp -> next; free(temp);
cout<<"\n Node deleted.."; } 50
else { cout<<"\n Invalid position.."; } } }
25
C++ Programming Language 4/16/2023
Exercises
1. Write a C++ functions to split a given
list of integers represented by a single
Exercises
4. Suppose that an ordered list L = (l 0 , l 1 ,
…..,l n ) is represented by a single linked list.
Sunday, April 16, 2023
26
C++ Programming Language 4/16/2023
Exercises
5. Which among the following segment of code
counts the number of elements in the single
Exercises
6. Which among the following segment of
Sunday, April 16, 2023
27
C++ Programming Language 4/16/2023
Exercises
7. A single linked list is declared as follows:
struct sllist {
Exercises
8. Which among the following segment of code
Sunday, April 16, 2023
28
C++ Programming Language 4/16/2023
Exercises
9. Which among the following segment of code
Thanks
58
29