LEC 4,5 Linked List
LEC 4,5 Linked List
head A B C NULL
Array versus Linked Lists
But what if you int main()
• In arrays don’t know how {
many items you’ll int array[100];
• Arrays are static data structure have ahead of …
}
time?
4 25
element a[i] in constant time ). need to insert 5 30
a new item in 6 35
▪ Searching the list for a particular value. the middle of an array? 7 40
8 45
We have to move every 9 50
item below the insertion
spot down by one!
• In Linked lists
• adjacency between any two elements are maintained by means of links or pointers
▪no easy access to i-th element wrt the head of the list
Types of Lists: Single Linked List Implementation
Depending on the way in which the links are used to maintain
adjacency, several different types of linked lists are possible.
Head
Or NULL
A B C
list
Defining a Node of a Linked List
Each structure of the list is called a node, and consists of two fields:
• Item (or) data
• Address of the next item in the list (or) pointer to the next node in the list
How to define a node of a linked list?
It creates a single node. For example, if the data entered is 100 then the list look like
head
100 NULL
Creating a single linked list
If we need n number of nodes in the linked list:
• Allocate n newNodes, one by one.
• Read in the data for the newNodes.
• Modify the links of the newNodes so that the chain is formed.
ptr n1=new node;
ptr n2= new node;
ptr n3= new node;
ptr head = n1;
n1-> data=100;
n1-> next=n2;
n2-> data=200;
n2-> next=n3;
n3-> data=50;
n3-> next=null;
It creates n number of nodes . For e.g. if the data entered is 100, 200, 50 then the list look like
head
100 200 50 NULL
Operations on single linked list
• Traversing a list
• Printing, etc.
The function show()is given. This function to be called from main() function as:
}
}
Single Linked List: Insertion
Insertion steps:
• Create a new node
• Manage links to
• Insert at front
• Insert at end
• Insert at any position
Insertion at Front
Steps to insert node at the beginning of singly linked list Step 2: Link the newly created node with
the head node, i.e. the newNode will now
Step 1: Create a new node. point to head node.
Step 3: Make the new node as the head node, i.e. now head node will point to newNode.
Insertion at front Function
Step 2: Traverse to the last node of the linked list and connect the last node of the list with
the new node, i.e. last node will now point to new node. (lastNode->next =
newNode).
Insertion at End Function
/* Create a new node and insert at the end of the linked list. */
Deletion steps
• Manage links to
• Delete at front
• Delete at end
• Delete at any position
Free Memory after Deletion
• Do not forget to delete() memory location dynamically allocated for a
node after deletion of that node.
}
Single Linked List: Deletion at any Position
Steps to delete a node at any position of Singly Linked List
Step 1: Traverse to the nth node of the singly
linked list and also keep reference of n-1th node Step 2: Reconnect n-1th node with the n+1th node i.e.
in some temp variable say prevNode. prevNode->next = toDelete->next
(Where prevNode is n-1th node and toDelete node
is the nth node and toDelete->next is the n+1th node).
Step 3: Free the memory occupied by the nth node i.e. toDelete node.
Deletion at any Position
void delete_anynode(ptr &n)
{
ptr p = n->next;
n->next = p->next;
delete(p);
}
Single Linked List: Reversing
Reversing a List: Iterative Method Function
Steps to reverse a Singly Linked List using Iterative method
curr = head
Prev =null
void reverse(ptr &head)
Next = null {
ptr prev= null;
ptr curr= head;
ptr next= null;
while(curr != null)
{
next = curr->next;
curr->next = prev;
prev = curr;
curr = next;
}
head= prev;
}
continue to
Types of Lists: Double Linked List
Double linked list
• Pointers exist between adjacent nodes in both directions.
• The list can be traversed either forward or backward.
• Usually two pointers are maintained to keep track of the list, head
and tail.
head tail
null A B C
null
Defining a Node of a Double Linked List
Each node of doubly linked list (DLL) consists of three fields:
• Item (or) Data
• Pointer of the next node in DLL
• Pointer of the previous node in DLL node
Data
prev next
struct Node
{
string value;
Node * next;
Node * prev;
};
Double Linked List
• Doubly linked list is a collection of nodes linked together in a sequential way.
• Doubly linked list is almost similar to singly linked list except it contains two
address or reference fields, where one of the address field contains reference of
the next node and other contains reference of the previous node.
• Doubly linked list is sometimes also referred as bi-directional linked list since it
allows traversal of nodes in both direction.
• Since doubly linked list allows the traversal of nodes in both direction, we can
keep track of both first and last nodes.
Doubly-linked Lists: What Changes?
Every time we insert a new node or delete an existing node, we
must update three sets of pointers:
Wouldn’t it be
One of the downsides Given a pointer
nice if we could
with our simple to a node, I can
move both
linked list is that only find nodes
directions in a
we can only travel in below it!
linked list?
one direction… down!
Types of Lists: Circular Linked List
Circular linked list
•The pointer from the last element in the list points back to the
first element
Doubly circular linked list: Basic structure of singly circular linked list:
Circular Linked List
• A circular linked list is basically a linear linked list that may be single- or
double-linked.
• The only difference is that there is no any NULL value terminating the list.
• In fact in the list every node points to the next node and last node points to
the first node, thus forming a circle. Since it forms a circle with no end to
stop it is called as circular linked list.
• In circular linked list there can be no starting or ending node, whole node
can be traversed from any node.
• In order to traverse the circular linked list, only once we need to traverse
entire list until the starting node is not traversed again.
• A circular linked list can be implemented using both singly linked list and
doubly linked list.
Circular linked list:
Advantages of a Circular linked list
• Entire list can be traversed from any node.
• Circular lists are the required data structure when we want a list to be accessed
in a circle or loop.
• Despite of being singly circular linked list we can easily traverse to its previous
node, which is not possible in singly linked list.
PUSH
OPERATION POP
OPERATION
top
top
Basic Idea
• In the array implementation, we would:
LINKED LIST
Queue: Linked List Structure
• Basic idea:
• Create a linked list to which items would be added to one end
and deleted from the other end.
• Two pointers will be maintained:
• One pointing to the beginning of the list (point from where
elements will be deleted).
• Another pointing to the end of the list (point where new
elements will be inserted). Rear
DEQUEUE
front rear
Queue Operations using Linked List
void enqueue()
{ void dequeue()
{
int val;
cout<<"Insert the element in queue : ptr temp = frront;
"<<endl;
cin>>val; if (frront == NULL) {
cout<<"Underflow"<<endl;
return;
ptr newnode= new node; }
newnode->data=val; else
if (temp->next != NULL) {
newnode->next=NULL; temp = temp->next;
cout<<frront->data <<endl;
if ((frront == NULL) && (rear == NULL)) delete(frront);
{ frront = temp;
} else {
frront=rear=newnode;} cout<<frront->data<<endl;
else{ delete(frront);
frront = NULL;
rear = NULL;
rear->next=newnode; }
rear=newnode; }
}}
Applications of linked List
• Image viewer – Previous and next images are linked, hence can be accessed by next and previous
button
• Previous and next page in web browser – We can access previous and next url searched in web
browser by pressing back and next button since, they are linked as linked list.
• Music Player – Songs in music player are linked to previous and next song. you can play songs
either from starting or ending of the list.
Any question?