CSE225: Data Structure And
Algorithms
Linked List
What are Linked Lists
A linked list is a linear data structure.
Nodes make up linked lists.
Nodes are structures made up of data and a pointer to
another node.
Usually the pointer is called next.
Arrays and Linked Lists
• An array is a list store in contiguous memory.
– Any element of an array can be accessed quickly.
– Insertion and deletion in the middle of an array requires the
movement of many elements.
– The size of an array is fixed.
• A linked list is a list scattered throughout memory and connected with
pointers/Links of next element.
– The elements of a linked list must be accessed in order. Linear
Access Mechanism
– Insertion and deletion only requires re-assignment of a few
pointers.
– The length of the list can change at any time, making it a dynamic
data structure.
• Advantages of using an Array:
• only good for non-sparse polynomials.
• ease of storage and retrieval.
• Disadvantages of using an Array:
• have to allocate array size ahead of time.
• huge array size required for sparse polynomials.
• Waste of space and runtime.
Arrays vs. Linked Lists
Arrays Linked list
Fixed size: Resizing is expensive Dynamic size
Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No
Elements are usually shifted shifting
Random access i.e., efficient indexing No random access
Not suitable for operations requiring
accessing elements by index such as sorting
No memory waste if the array is full or almost Since memory is allocated dynamically
full; otherwise may result in much memory (according to our need) there is no waste of
waste. memory.
Sequential access is faster [Reason: Elements in Sequential access is slow [Reason: Elements not
contiguous memory locations] in contiguous memory locations]
There are two basic types of linked list
• Singly Linked list
• Doubly linked list
Singly Linked List
Each node has only one link part
Each link part contains the address of the next node in
the list
Link part of the last node contains NULL value which
signifies the end of the node
Schematic representation
Here is a singly-linked list (SLL):
myList
a b c d
• Each node contains a value(data) and a pointer to the next node
in the list
• myList is the header pointer which points at the first node in the
list
Basic Operations on a list
• Creating a List
• Inserting an element in a list
• Deleting an element from a list
• Searching a list
• Reversing a list
Creating a node
struct node { // A simple node of a linked list
int data;
node*next; //start points at the first node
}; initialised to NULL at beginning
class Node {
public:
int data;
Node *next;
};
Insertion Description
• Insertion at the beginning of the list
• Insertion at the end of the list
• Insertion in the middle of the list
Insertion at the beginning
Steps:
• Create a Node
• Set the node data Values
• Connect the pointers
Insertion Description
head 48 17 142 //
• Follow the previous steps and we get
Step 1 Step 2
Step 3
head 93
Insertion at the end
Steps:
• Create a Node
• Set the node data Values
• Connect the pointers
Insertion Description
head 48 17 142 //
• Follow the previous steps and we get
Step 1 Step 2
Step 3
Insertion in the middle
Steps:
• Create a Node
• Set the node data Values
• Break pointer connection
• Re-connect the pointers
Insertion Description
Step 1 Step 2
Step 3
Step 4
switch (option) { case 3:
case 1: cout << "\nEnter the Position to Insert:";
cin >> pos;
temp->next = first; while (cur && count!=pos) {
first = temp; prev=cur;
break; cur=cur->next;
count++;
case 2: }
last->next = temp; if (count==pos) {
prev->next=temp;
last = temp;
temp->next=cur;
break; } else {
cout << "\nNot Able to Insert";
}
break;
}
Deletion Description
• Deleting from the beginning of the list
• Deleting from the end of the list
• Deleting from the middle of the list
Deleting from the beginning
Steps
• Break the pointer connection and assign to temp node
• Re-connect the nodes change the starting node to next
• Delete the node
void delete_first()
{
node *temp=new node;
temp=head;
head=head->next;
delete temp;
}
Deletion Description
head
6 4 17 42
head
6 4 17 42
head
4 17 42
Deleting from the end
Steps
• Break the pointer connection
• Set previous node pointer to NULL
• Delete the node
Deletion Description
head
6 4 17 42
head
6 4 17 42
head
6 4 17
Deleting from the Middle
Steps
• Set previous Node pointer to next node
• Break Node pointer connection
• Delete the node
Deletion Description
head
4 17 42
head
4 17 42
head
4 42
Display the list
void display()
{
node *temp = head;
while(temp!=NULL) {
cout << temp->data <<"\t";
temp = temp->next;
}
}
Delete First Node
void delete_first()
{
node *temp = head;
head=head->next;
delete temp;
}
Delete Last Node
void delete_last()
{
node *current=new node;
node *previous=new node; current=head;
while(current->next!=NULL)
{
previous=current;
current=current->next;
} tail=previous;
} Previous->next=NULL;
delete current;
}
Deleting a particular node
Here we make the next pointer of the node previous to
the node being deleted ,point to the successor node of
the node to be deleted and then delete the node using
delete keyword
node1 node2 node3
To be deleted
void delete_position(int pos)
{
node *current=new node;
node *previous=new node; current=head;
for(int i=1;i<pos;i++)
{
previous=current;
current=current->next;
}
previous->next=current->next;
}
Searching a SLL
Searching involves finding the required element in the
list
We can use various techniques of searching like linear
search or binary search where binary search is more
efficient in case of Arrays
But in case of linked list since random access is not
available it would become complex to do binary search
in it
We can perform simple linear search traversal
In linear search each node is traversed till the data in
void search(int x)
{
node* temp = start;
while(temp!=NULL) {
if(temp->data==x) {
cout<<“Found the item”<<x;
break;
}
temp=temp->next;
}
}
COMPLEXITY OF VARIOUS OPERATIONS
IN ARRAYS AND SLL
Operation ID-Array Complexity Singly-linked list Complexity
Insert at beginning O(n) O(1)
Insert at end O(1) O(1) if the list has tail reference
O(n) if the list has no tail reference
Insert at middle O(n) O(n)
Delete at beginning O(n) O(1)
Delete at end O(1) O(n)
Delete at middle O(n): O(n):
O(1) access followed by O(n) O(n) search, followed by O(1) delete
shift
Search O(n) linear search O(n)
O(log n) Binary search
Indexing: What is O(1) O(n)
the element at a
given position k?
Doubly Linked Lists
• A Doubly Linked List is List in which every Node has
a Next Pointer and a Back Pointer
• Every Node (Except the Last Node) Contains the
Address of the Next Node, and Every Node (Except
the First Node) Contains the Address of the Previous
Node
• A Doubly Linked List can be Traversed in either
Direction
Definition and Graphical Representations
The Node is Divided into 3 parts
1) Information Field
2) Forward Link which Points to the Next Node
3) Backward Link which Points to the Previous Node
The starting Address or the Address of First Node is Stored in
START / FIRST Pointer
Another Pointer can be used to traverse Doubly LL from End.
This Pointer is Called END or LAST
4 X 2 10
Start Last
INFO Field NEXT Pointer
BACK Pointer
NODE
previous data next
A B C
NULL 11 786 200 656 400 786 777 NULL
200 786 400
A doubly linked list contain three fields: an integer value, the
link to the next node, and the link to the previous node.
DLL’s compared to SLL’s
• Advantages: Disadvantages:
• Can be traversed in either Requires more space
direction (may be essential
List manipulations are
for some programs)
slower (because more
• Some operations, such as
links must be changed)
deletion and inserting
before a node, become Greater chance of having
easier bugs (because more links
must be manipulated)
Structure of DLL
struct node
{
int data;
node*next;
node*previous; //holds the address of previous node
};
previous. .Data .next
inf
Inserting at beginning
void insert_beg(node *p)
{
if(start==NULL)
{
start=p;
cout<<"\nNode inserted successfully at the beginning\m";
}
else
{
node* temp=start;
start=p;
temp->previous=p; //making 1st nodes previous point to the
new node
p->next=temp; //making next of the new node point to the
1st node
cout<<"\nNode inserted successfully at the beginning\n";
}
Inserting at the end
void insert_end(node* p)
{
if(start==NULL) {
start=p;
cout<<"\nNode inserted successfully at the end";
} else {
node* temp=start;
while(temp->next!=NULL) {
temp=temp->next;
}
temp->next=p;
p->previous=temp;
cout<<"\nNode inserted successfully at the end\n";
}
}
Inserting after a node
Making next and previous pointer of the node to be
inserted point accordingly
Adjusting the next and previous pointers of the nodes b/w which
the new node accordingly
void insert_after(int c,node* p)
{
temp=start;
for(int i=1;i<c-1;i++)
{
temp=temp->next;
}
p->next=temp->next;
temp->next->previous=p;
temp->next=p;
p->previous=temp;
cout<<"\nInserted successfully";
}
Deleting a node
• Node deletion from a DLL involves changing two links
• In this example,we will delete node b
myDLL
a b c
• We don’t have to do anything about the links in node b
• Garbage collection will take care of deleted nodes
• Deletion of the first node or the last node is a special
case
void del_at(int c)
{
node*s=start;
for (int i=1;i<c-1;i++) {
s=s->next;
}
node* p=s->next;
s->next=p->next;
p->next->previous=s;
delete p;
cout<<"\nNode number "<<c<<" deleted successfully";
}
APPLICATIONS OF LINKED LIST
1. Applications that have an Most Recently Used (MRU) list
(a linked list of file names)
2. The cache in your browser that allows you to hit the BACK
button (a linked list of URLs)
3. Undo functionality in Photoshop or Word (a linked list of
state)
4. A stack, hash table, and binary tree can be implemented
using a doubly linked list.
Circular Linked List
1. A circular linked list is a linked list in which the head
element's previous pointer points to the tail element and
the tail element's next pointer points to the head
element.
2. A circularly linked list node looks exactly the same as a
linear singly linked list.
Header Linked Lists
• A Header Linked List always Contains a Special Node
called Header Node
• It has Two Types:
a. Grounded Header List: Last Node Contains the
NULL Pointer
b. Circular Header List: Last Node Points Back to
the Header Node
Graphical Representations
4 8 10
Start Header Data Link
Node Grounded Header Link List
LINK [START] = NULL
4 8 10
Start Header Data Link
Node Circular Header Link List
LINK [START] = START
Header Linked Lists
• One way to simplify insertion and deletion is never to insert an
item before the first or after the last item and never to delete
the first node
• You can set a header node at the beginning of the list
containing a value smaller than the smallest value in the data
set
• You can set a trailer node at the end of the list containing a
value larger than the largest value in the data set.
• These two nodes, header and trailer, serve merely to simplify
the insertion and deletion algorithms and are not part of the
actual list.
• The actual list is between these two nodes.