CSE 203 Week 3 - Single Linked Lists
CSE 203 Week 3 - Single Linked Lists
202
201
2
Limitations of Dynamic Array
heap Memory (Horizontal View)
200
202
201
202
201
int *p=(int*)malloc(3*sizeof(int));
4
Limitations of Dynamic Array
heap Memory (Horizontal View)
212
200
202
201
p
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
5
Limitations of Dynamic Array
heap Memory (Horizontal View)
212
200
202
201
p
Consider a program segment:
6
Limitations of Dynamic Array
heap Memory (Horizontal View)
212
213
220
200
202
201
p q
Consider a program segment:
7
Limitations of Dynamic Array
heap Memory (Horizontal View)
212
213
220
200
202
201
p q
Consider a program segment:
8
Limitations of Dynamic Array
heap Memory (Horizontal View)
212
213
220
200
202
201
p q
Consider a program segment:
9
Limitations of Dynamic Array
heap Memory (Horizontal View)
236
212
213
220
221
200
202
201
p q r
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes
10
Limitations of Dynamic Array
heap Memory (Horizontal View)
236
212
213
220
221
200
202
201
p q r
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes
free(q)
11
Limitations of Dynamic Array
heap Memory (Horizontal View)
236
212
213
220
221
200
202
201
p r
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes
free(q)
12
Limitations of Dynamic Array
heap Memory (Horizontal View)
236
212
213
220
221
200
202
201
p r
Consider a program segment:
int *p=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
float *q=(float*)malloc(sizeof(float)); // float size is 8 bytes
int *r=(int*)malloc(4*sizeof(int)); // int size is 4 bytes
free(q)
int *s=(int*)malloc(3*sizeof(int)); // int size is 4 bytes
Although free memory is more than required 3 x 4 = 12 bytes, yet it cannot be
allocated as it is not contiguous. Returns a null pointer. 13
Solution: Linked List
heap Memory (Horizontal View)
236
212
213
220
221
200
202
201
14
Solution: Linked List
heap Memory (Horizontal View)
236
212
213
220
221
200
202
201
p
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
15
Solution: Linked List
heap Memory (Horizontal View)
7 10
236
212
213
222
200
202
201
p q
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
16
Solution: Linked Listheap Memory (Horizontal View)
7 10
5
236
212
213
222
200
202
201
p r q
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
17
Solution: Linked List
heap Memory (Horizontal View)
7 10
5
233
212
213
222
200
202
201
r
9 s
p
Suppose we need to store a list of 4 integers: 7, 10, 5, 9.
q
int *p=(int*)malloc(sizeof(int)); *p=7;
int *q=(int*)malloc(sizeof(int)); *q=10;
int *r=(int*)malloc(sizeof(int)); *r=5;
int *s=(int*)malloc(sizeof(int)); *s=9;
Non-contiguous allocation: Hence it is not possible to traverse the list directly as
done in array.
18
Solution: Linked List
heap Memory (Horizontal View)
7 10
5
233
212
213
222
200
202
201
r
9 s
p
To traverse the elements of the list, one should store
q the address of the next element in
the list with each element.
19
Solution: Linked List
heap Memory (Horizontal View)
10
7 222 5 233
213 213 -1
233
213
222
9
202
p
To traverse the elements of the list {7, 10, 5, 9}, one should store the address of the
next element (pointer to next element) in the list with each element.
It is sufficient to know the address of (pointer to) the first node (also called as head
node) to retrieve all the elements of the list.
21
head
22
insert 7
head
23
head
24
insert 9
head
25
head
26
Linked List
● A linked list is a linear collection of data elements (called nodes), where the linear order is
given by means of pointers.
● Each node is divided into 2 parts:
▪ 1st part contains the information of the element.
▪ 2nd part is called the link field or next pointer field which contains the address
of the next node in the list.
n=5
Head=NULL Node *newNode = new Node;
newNode->data = val;
newNode->next = nullptr;
8 NULL
if (head == nullptr)
{
head = newNode;
}
n=5
Head Node *newNode = new Node;
newNode->data = val;
newNode->next = nullptr;
8 NULL
if (head == nullptr)
{
head = newNode;
}
else
{ temp
Node *temp = head; (temp->data=8
while (temp->next != nullptr) temp->next=NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
Lec Anika Ashraf, CSE Dept, MIST 33
● Case 2: Linked list is not empty
Head
Node *newNode = new Node;
newNode->data = val;
newNode->next = nullptr; 8 NULL 5 NULL
else
{ temp
Node *temp = head; (temp->data=8
while (temp->next != nullptr) temp->next=NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
Lec Anika Ashraf, CSE Dept, MIST 34
● Case 2: Linked list is not empty
Head
Node *newNode = new Node;
newNode->data = val;
newNode->next = nullptr; 8 5 NULL 3 NULL
else
{ temp
Node *temp = head; (temp->data=8
while (temp->next != nullptr) temp->next=address of next node)
{
temp = temp->next;
}
temp->next = newNode;
}
Lec Anika Ashraf, CSE Dept, MIST 35
● Case 2: Linked list is not empty
Head
Node *newNode = new Node;
newNode->data = val;
newNode->next = nullptr; 8 5 NULL 3 NULL
else
{ temp
Node *temp = head; (temp->data=5
while (temp->next != nullptr) temp->next=NULL)
{
temp = temp->next;
}
temp->next = newNode;
}
Lec Anika Ashraf, CSE Dept, MIST 36
● Case 2: Linked list is not empty
Head
Node *newNode = new Node;
newNode->data = val;
newNode->next = nullptr; 8 5 3 NULL
else
{ temp
Node *temp = head; (temp->data=5
while (temp->next != nullptr) temp->next=address of the
{ next node)
temp = temp->next;
}
temp->next = newNode;
}
Lec Anika Ashraf, CSE Dept, MIST 37
Head
● Case 2: Linked list is not empty
8 5 3 NULL 7 NULL
temp
else
{
Node *temp = head;
while (temp->next != nullptr)
{
temp = temp->next;
}
temp->next = newNode;
}
8 5 3 NULL 7 NULL
temp
else
{
Node *temp = head;
while (temp->next != nullptr)
{
temp = temp->next;
}
temp->next = newNode;
}
8 5 3 NULL 7 NULL
temp
else
{
Node *temp = head;
while (temp->next != nullptr)
{
temp = temp->next;
}
temp->next = newNode;
}
8 5 3 7 1 NULL
temp else
{
void createList(int n) if (head == nullptr) Node *temp = head;
{ { while (temp->next != nullptr)
for (int i = 1; i <= n; i++) head = newNode; {
{ } temp = temp->next;
Node *newNode = new Node; }
int val; temp->next = newNode;
cout << "Node " << i << ": "; }
cin >> val; }
newNode->data = val; cout << endl;
newNode->next = nullptr; }
Lec Anika Ashraf, CSE Dept, MIST 41
Basic Operations
● Insert: Add a new node in the first, last or interior of the list.
● Delete: Delete a node from the first, last or interior of the list.
NULL
newItem head
NULL
newItem
NULL
head
Lec Anika Ashraf, CSE Dept, MIST 47
Insert First (Cont.)
newItem head
void insertFirst(int val)
{ NULL
Node *newItem = new Node; newItem
newItem->data = val; head
newItem->next = head; NULL
head = newItem;
cout << "Successfully inserted!\n\n"; newItem
}
NULL
head
head newItem
NULL
head newItem
temp
NULL
head newItem
temp
NULL
NULL
newnode
53
Insert at any(to a specific index position)
void insertAt(int pos, int val)
{
if (pos == 1)
insertFirst(val);
else
{
Node *newNode = new Node; Head Temp Temp
newNode->data = val;
newnode->next=NULL;
Node *temp = head; NULL
for (int i = 2; i < pos; i++)
{
if (temp == nullptr)
{
cout << "Insertion not possible\n"; newnode
return;
}
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
cout << "Successfully inserted!<<endl;}}
Step 1
Step 3
Step 2
}
} Step 3
Lec Anika Ashraf, CSE Dept, MIST 58
Delete Last
● To delete the last node in a linked list, we use a local variable, cur, to point to the second last
node.
● To delete the last node in a linked list, we use a local variable, cur, to point to the second last
node.
Step 3
Step1
Step2 Step 4
● To delete a node that contains a particular value x in a linked list or is in a particular position,
we use a local variable, cur, to point to this node, and another variable, prev, to hold the
previous node.
1. temp=cur->next // 20
1. prev->next=cur->next 2. cur->next=cur->next->next
2. delete cur 3. delete temp
Step 1
Step 2
● …….
● Step3. Link the node pointed by pointer prev to the node after the cur’s node.
● Step4. Remove the node pointed by cur.
Step 3
Step 4
● Application: Linked lists are used in a variety of cases, most prominently being the
building block for other complex data structures. They may be used to implement stacks,
queues, graphs and hash tables. They also have particular use cases in applications, such as:
● Next and Previous page in a web browser – We can access the previous and next “url”
searched in a web browser by pressing the “back” and “next” buttons as they are
connected as a linked list.
● Music Player – Songs in the music player are connected to the previous and next song.
You can listen to the songs either from the beginning or end of the list.