CSC 221 DSA Linked List Single Double Circular 10032023 053401pm
CSC 221 DSA Linked List Single Double Circular 10032023 053401pm
5
An Array A Linked List
6
ARRAYS VS LISTS
7
ARRAYS VS LISTS
Linked lists are more complex to code and manage than arrays, but they
have some distinct advantages.
Dynamic: a linked list can easily grow and shrink in size.
We don’t need to know how many nodes will be in the list. They are created in memory as needed.
In contrast, the size of a C++ array is fixed at compilation time.
A B C
Head
A linked list is a series of connected nodes
Each node contains at least
node
A piece of data (any type)
A
Pointer to the next node in the list
data pointer
Head: pointer to the first node
The last node points to NULL
10
REPRESENTATION OF LINKED LIST
A linked list is represented by a pointer to the first node of the linked list.
The first node is called the head.
If the linked list is empty, then the value of the head is NULL.
Each node in a list consists of at least two parts:
1) Data
2) Pointer (Or Reference) to the next node
LINKED LIST ACTUAL PICTURE IN MEMORY
1051 6
1052 1063
current 1053 1063
1054 2
head 1055 1051
1056
2 6 8 7 1 1057 7
1058 1060
current 1059
1060 1
1061 0
head 1062 1054
1063 8
1064 1057
1065
OPERATIONS IN LINKED LIST
Single Linked list is the default linked list and a linear data structure in which each
data node is connected to the next data node via a pointer, hence forming a chain.
The element in such a linked list can be inserted in 3 ways:
Insertion at beginning of the list.
Insertion at the end of the list.
Insertion at the particular position in the list.
WHAT IS A NODE?
A Node in a linked list holds the data value and the pointer which points
to the location of the next node in the linked list.
Why we need to store the location of the next node?
Because the memory locations allocated to these nodes are not contiguous
hence each node should know where the next node is stored.
As the node is a combination of multiple information, hence we will be
defining a class for Node which will have a variable to store data and
another variable to store the pointer.
CREATING A NODE
To avoid get/set methods
Class Node {
Public:
int data; // data in node
Node *next; // Pointer to next node
};
//Driver.cpp:
Node *p; p 10
p = new Node;
p -> data = 10;
p -> next = NULL; 18
DELETING A NODE
p 10
//Driver.cpp:
delete p;
p = NULL;
p
19
AN INTEGER (SINGLY) LINKED LIST
head
10 13 5 2
class Node {
public:
int data;
Node *next;
};
20
JOINING TWO NODES
Node *p, *q;
p 10
p = new Node;
p - > data = 10;
p - > next = NULL;
q = new Node; q 6
q - > data = 6;
q - > next = NULL;
p 10 6
p - > next = q; 21
q
ACCESSING LIST DATA
p 10 6
Expression Value
p Pointer to first node (head)
p - > data 10
p - > next Pointer to next node
p - > next - > data 6
p - > next - > next NULL pointer
22
Basic Concepts
head
23
Basic Concepts
head 1
ptr
24
ADDING MORE NODES
ptr
ptr2 3
The next pointer of the node which was
previously last should now point to newly
created node “ptr->next=ptr2”
27
head 1 2
ptr
ptr2 3
head 1 2 3
ptr
ptr2
29
TRAVERSING THE LIST
void printList()
{
Node *pointer = head;
while (pointer != NULL)
{
cout<< pointer->data;
pointer = pointer->next;
}
} 30
ANALYSIS OF LINKED LIST
add
we simply insert the new node after the current node.
remove
remove is also a one-step operation
search
You may have to search the entire list
back
moving the current pointer back one node requires traversing the list from the start
until the node whose next pointer points to current node.
NODE INSERTION
32
NODE INSERTION AT THE BEGINNING
Steps:
Create a node
Set the node data values
Connect the pointers
Initial list head 48 17 142 //
Step 1 Step 2
head 93
NODE INSERTION AT THE BEGINNING
Initial list head 48 17 142 //
ptr
Node *ptr; ptr
ptr = new Node; 93
head
ptr - > data = 93;
ptr - > next = head;
head = ptr;
head
Rearrange:
head 93
NODE INSERTION AT THE END
Steps:
Create a Node
Initial list head 48 17 142 //
Set the node data values Step 1 Step 2
Connect the pointers
Node * ptr;
ptr = head;
while (ptr->next != NULL)
{
ptr = ptr->next;
}
NODE INSERTION AT THE END
ptr
Node * p;
p = new Node;
p -> data = 150;
p -> next = NULL;
150 //
p
NODE INSERTION AT THE END
Node * p;
p = new Node;
p -> data = 150; ptr
150 //
p
NODE INSERTION AT ARBITRARY POSITION
Steps:
Create a Node
Set the node data values Step 1 Step 2
Step 4
NODE INSERTION AT ARBITRARY POSITION
Steps:
Create a Node
Set the node data values
Break pointer connection
Re-connect the pointers
Need a pointer on the node after which a new node is to be
Inserted. For instance, if new node is to be inserted after the node
having value ‘17’, we need a pointer at this node
ptr
How to get pointer on desired node?
NODE INSERTION AT ARBITRARY POSITION
Suppose we want to insert a node after the node having value ‘x’:
Node * ptr;
ptr = head;
while (ptr->data != x)
{
ptr = ptr->next;
}
ptr
NODE INSERTION AT ARBITRARY POSITION
Node * ptr; Node * p;
p = new Node;
ptr = head;
p -> data = 100;
while (ptr->data != x) p -> next = NULL;
{
ptr = ptr->next;
}
ptr 150 //
p
NODE INSERTION AT ARBITRARY POSITION
Node * ptr; Node * p;
ptr
150
p
NODE DELETION
head
6 4 17 42
head 4 17 42
DELETING FROM THE BEGINNING
head
ptr 6 4 17 42
head
head 6 4 17 42
ptr
head 4 17 42
Node * ptr;
ptr = head;
head = head ->next;
delete ptr;
DELETING FROM THE END
Steps:
Break the pointer connection
Set previous node pointer to NULL
Delete the node
head 6 4 17 42
head 6 4 17 42
head 6 4 17
DELETING FROM THE END
head 6 4 17 42
predPtr ptr
}
DELETING FROM THE END
head 6 4 17 42
predPtr ptr
head 6 4 17 42
predPtr ptr
head 6 4 17 42
Steps:
Set previous Node pointer to next node
Break Node pointer connection
head 4 17 42
Delete the node
head 4 17 42
head 4 42
DELETING FROM AN ARBITRARY
POSITION
head 4 17 42
predPtr ptr
Node * ptr; We need a pointer on the node to be
Node * predPtr; deleted (as well a pointer to one node
before the node to be deleted)
ptr = head;
predPtr = NULL;
while (ptr->data != x)
Given the value of the node to be
deleted, assume this to be variable ‘x’
{ Keep moving a pointer until the
predPtr = ptr; required node is reached
ptr = ptr->next;
}
DELETING FROM AN ARBITRARY POSITION
head 4 17 42
predPtr ptr
head 4 17 42
predPtr ptr
Circular Linked List
CIRCULARLY-LINKED LISTS
The next field in the last node in a singly-linked list is set to NULL.
Moving along a singly-linked list has to be done in a watchful manner.
Doubly-linked lists have two NULL pointers: prev in the first node and
next in the last node.
A way around this potential hazard is to link the last node with the first
node in the list to create a circularly-linked list.
CIRCULARLY-LINKED LISTS
The next field in the last node in a singly-linked list is set to NULL.
Moving along a singly-linked list has to be done in a watchful manner.
Doubly-linked lists have two NULL pointers: prev in the first node and
next in the last node.
A way around this potential hazard is to link the last node with the first
node in the list to create a circularly-linked list.
CIRCULARLY-LINKED LISTS
The next field in the last node in a singly-linked list is set to NULL.
Moving along a singly-linked list has to be done in a watchful manner.
Doubly-linked lists have two NULL pointers: prev in the first node and
next in the last node.
A way around this potential hazard is to link the last node with the first
node in the list to create a circularly-linked list.
CIRCULARLY-LINKED LISTS
The next field in the last node in a singly-linked list is set to NULL.
Moving along a singly-linked list has to be done in a watchful
manner.
Doubly-linked lists have two NULL pointers: prev in the first node
and next in the last node.
To create a circularly-linked list, link the last node with the first
node in the list.
CIRCULARLY LINKED LIST
Two views of a circularly linked list:
current
head 2 6 8 7 1 size=5
current
6
8
size=5
head 2
1
APPLICATIONS OF LINKED LIST
1. Images are linked with each other. So, an image viewer software uses a
linked list to view the previous and the next images using the previous
and next buttons.
2. Web pages can be accessed using the previous and the next URL links
which are linked using linked list.
3. The music players also use the same technique to switch between music.
4. To keep the track of turns in a multi player game, a circular linked list is
used.
CIRCULAR LINKED LISTS
60
CIRCULAR LINKED LIST
A linked list in which the last node points to the first node is called a
circular linked list
head 9 17 22 26 34
61
CIRCULAR LINKED LIST
head
data
One node in list
next
head
Multiple nodes in list
void CircularList::traverse()
{
Node *temp = head;
if (head!= NULL)
{
do
{
cout<<temp->data<<" ";
temp=temp->next;
}while (temp != head);
}
} 64
ADD NODE TO BEGINNING OF LIST
void CircularList::add_begin(int data)
else
{
{
Node *ptr = new Node;
Node *temp = head;
ptr->data = data;
while(temp->next!=head)
ptr->next = NULL;
{
if (head == NULL)
temp=temp->next;
{
}
head = ptr;
temp->next = ptr;
ptr->next = head;
ptr->next = head;
}
head = ptr;
}
}
65
ADD NODE TO END OF LIST
66
DELETE NODE FROM BEGINNING OF LIST
void CircularList::del_beg() }
{ else{
Node *temp; Node *t=head;
temp=head; while(temp->next!=head)
if(head==NULL){ {
cout<<"\nList has no nodes"; temp=temp->next;
return; }
} head=head->next;
//Only one node in list temp->next=head;
if(head->next==head){ delete t;
head=NULL; }
delete temp; }
return; 67
DELETE NODE FROM END OF LIST
void CircularList::del_end()
else
{
{
Node *temp;
Node *prevtemp;
temp=head;
while(temp->next!=head)
if(head==NULL)
{
{
prevtemp=temp;
cout<<"\nList has no nodes";
temp=temp->next;
}
}
//Only one node in list
prevtemp->next=temp->next;
if(head->next==head)
delete temp;
{
}
head=NULL;
}
delete temp;
} 68
DOUBLY LINKED LISTS
69
INTRODUCTION
The singly linked list contains only one pointer field i.e. every node holds an address
of the next node.
The singly linked list is uni-directional i.e. we can only move from one node to its
successor.
Doubly linked list: Each node has two pointers, next and previous
Each node has one pointer to its successor (NULL if there is none) and one pointer
to its predecessor (NULL if there is none).
70
INTRODUCTION
prev
data
predecessor successor
next
first 9 17 22 26 34
last
71
Node of a doubly linked list
*Previous Data *Next
Head 72
INTRODUCTION
Linked list
class Node
{
int data;
Node* next;
};
Doubly linked list
class Node {
Node *previous;
int data;
73
Node *next;
};
DOUBLY LINKED LIST : INSERTION
predptr class Node
{
public:
int data;
Node * next;
15 20 Node * prev;
};
15 17 20
delete ptr;
ADD NODE AT THE END OF LIST
node *temp;
temp = new node;
temp->prev = NULL;
temp->next = NULL;
temp->data = value;
// If list has no elements
if (head == NULL)
{
head = temp;
}
// List has element(s)
temp->next = head;
head->prev = temp;
77
head = temp;
}
DELETION OF NODE
//Last node to be deleted
if(p->next==NULL){
p->prev->next=NULL;
delete p;
return;
}