Linked List
Linked List
Algorithms
Linked List
What are Linked Lists
Insertions and Deletions are inefficient: Insertions and Deletions are efficient: No
Elements are usually shifted shifting
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
myList
a b c d
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 //
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 //
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
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
4 X 2 10
Start Last
BACK Pointer
NODE
previous data next
A B C
NULL 11 786 200 656 400 786 777 NULL
• 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
};
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
2. The cache in your browser that allows you to hit the BACK
button (a linked list of URLs)
4 8 10
Start Header Data Link
Node Circular Header Link List