C++ DSA Linked List
C++ DSA Linked List
Home My courses CONT_23CSH-103 :: ELEMENTARY DATA STRUCTURES USING C++ Chapter 3.1
CO4 - Implement and evaluate the programs using the syntax and semantics of object-oriented programming.
A linked list is a collection of "nodes" connected together via links. These nodes consist of the data to be stored and a
pointer to the address of the next node within the linked list. In the case of arrays, the size is limited to the definition, but
in linked lists, there is no defined size. Any amount of data can be stored in it and can be deleted from it.
Singly Linked List − The nodes only point to the address of the next node in the list.
Doubly Linked List − The nodes point to the addresses of both previous and next nodes.
Circular Linked List − The last node in the list will point to the first node in the list. It can either be singly linked or
doubly linked.
As per the above illustration, following are the important points to be considered.
Each link is linked with its next link using its next link.
Last link carries a link as null to mark the end of the list.
Since the last node and the first node of the circular linked list are connected, the traversal in this linked list will go on
forever until it is broken.
Insertion Operation
Adding a new node in linked list is a more than one step activity. We shall learn this with diagrams here. First, create a
node using the same structure and find the location where it has to be inserted.
Imagine that we are inserting a node B (NewNode), between A (LeftNode) and C (RightNode). Then point B.next to C −
Now, the next node at the left should point to the new node.
This will put the new node in the middle of the two. The new list should look like this −
Insertion in linked list can be done in three different ways. They are explained as follows −
Insertion at Beginning
In this operation, we are adding an element at the beginning of the list.
Example
Following are the implementations of this operation in various programming languages −
CC++JavaPython
#include <bits/stdc++.h>
#include <string>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
}
Output
Linked List:
[ 50 44 30 22 12 ]
Insertion at Ending
In this operation, we are adding an element at the ending of the list.
Algorithm
1. START
2. Create a new node and assign the data
3. Find the last node
4. Point the last node to new node
5. END
Example
Following are the implementations of this operation in various programming languages −
#include <bits/stdc++.h>
#include <string>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
struct node *linkedlist = head;
// print list
printList();
}
Output
Linked List:
[ 12 22 30 44 50 ]
Insertion at a Given Position
In this operation, we are adding an element at any position within the list.
Algorithm
1. START
2. Create a new node and assign data to it
3. Iterate until the node at position is found
4. Point first to new first node
5. END
Example
Following are the implementations of this operation in various programming languages −
#include <bits/stdc++.h>
#include <string>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
}
Output
Linked List:
[ 30 22 44 50 12 ]
Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial representation. First, locate the target node
to be removed, by using searching algorithms.
The left (previous) node of the target node now should point to the next node of the target node −
This will remove the link that was pointing to the target node. Now, using the following code, we will remove what the
target node is pointing at.
We need to use the deleted node. We can keep that in memory otherwise we can simply deallocate memory and wipe
off the target node completely.
Similar steps should be taken if the node is being inserted at the beginning of the list. While inserting it at the end, the
second last node of the list should point to the new node and the new node will point to NULL.
Deletion in linked lists is also performed in three different ways. They are as follows −
Deletion at Beginning
In this deletion operation of the linked, we are deleting an element from the beginning of the list. For this, we point the
head to the second node.
Algorithm
1. START
2. Assign the head pointer to the next node in the list
3. END
Example
Following are the implementations of this operation in various programming languages −
#include <bits/stdc++.h>
#include <string>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
deleteatbegin();
cout << "\nLinked List after deletion: ";
printList();
}
Output
Linked List:
[ 50 44 30 22 12 ]
Linked List after deletion:
[ 44 30 22 12 ]
Deletion at Ending
In this deletion operation of the linked, we are deleting an element from the ending of the list.
Algorithm
1. START
2. Iterate until you find the second last element in the list.
3. Assign NULL to the second last element in the list.
4. END
Example
Following are the implementations of this operation in various programming languages −
#include <bits/stdc++.h>
#include <string>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
deleteatend();
cout << "\nLinked List after deletion: ";
printList();
}
Output
Linked List: 50 44 30 22 12
Linked List after deletion: 50 44 30 22
Deletion at a Given Position
In this deletion operation of the linked, we are deleting an element at any position of the list.
Algorithm
1. START
2. Iterate until find the current node at position in the list.
3. Assign the adjacent node of current node in the list
to its previous node.
4. END
Example
Following are the implementations of this operation in various programming languages −
#include <bits/stdc++.h>
#include <string>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
deletenode(30);
cout << "\nLinked List after deletion: ";
printList();
}
Output
Linked List:
[ 50 44 30 22 12 ]
Linked List after deletion:
[ 50 44 22 12 ]
Reversal Operation
This operation is a thorough one. We need to make the last node to be pointed by the head node and reverse the
whole linked list.
First, we traverse to the end of the list. It should be pointing to NULL. Now, we shall make it point to its previous node −
We have to make sure that the last node is not the last node. So we'll have some temp node, which looks like the head
node pointing to the last node. Now, we shall make all left side nodes point to their previous nodes one by one.
Except the node (first node) pointed by the head node, all nodes should point to their predecessor, making them their
new successor. The first node will point to NULL.
We'll make the head node point to the new first node by using the temp node.
Algorithm
Step by step process to reverse a linked list is as follows −
1. START
2. We use three pointers to perform the reversing:
prev, next, head.
3. Point the current node to head and assign its next value to
the prev node.
4. Iteratively repeat the step 3 for all the nodes in the list.
5. Assign head to the prev node.
Example
Following are the implementations of this operation in various programming languages −
#include <bits/stdc++.h>
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
reverseList(&head);
printf("\nReversed Linked List: ");
printList();
return 0;
}
Output
Linked List:
[ 55 40 30 22 12 ]
Reversed Linked List:
[ 12 22 30 40 55 ]
Search Operation
Searching for an element in the list using a key element. This operation is done in the same way as array search;
comparing every element in the list with the key element given.
Algorithm
1 START
2 If the list is not empty, iteratively check if the list
contains the key
3 If the key element is not present in the list, unsuccessful
search
4 END
Example
Following are the implementations of this operation in various programming languages −
#include <bits/stdc++.h>
#include <string>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
int ele = 16;
cout<<"\nElement to be searched is: "<<ele;
k = searchlist(ele);
if (k == 1)
cout << "\nElement is found";
else
cout << "\nElement is not found in the list";
}
Output
Linked List:
[ 50 44 30 22 12 ]
Element to be searched is: 16
Element is not found in the list
Traversal Operation
The traversal operation walks through all the elements of the list in an order and displays the elements in that order.
Algorithm
1. START
2. While the list is not empty and did not reach the end of the list,
print the data in each node
3. END
Example
Following are the implementations of this operation in various programming languages −
#include <bits/stdc++.h>
#include <string>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
// print list
printList();
}
Output
Linked List: 50 44 30 22 12
Complete implementation
Following are the complete implementations of Linked List in various programming languages −
#include <bits/stdc++.h>
#include <string>
using namespace std;
struct node {
int data;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
struct node *linkedlist = head;
// print list
printList();
deleteatbegin();
deleteatend();
deletenode(12);
cout << "\nLinked List after deletion: ";
// print list
printList();
insertatbegin(4);
insertatbegin(16);
cout << "\nUpdated Linked List: ";
printList();
k = searchlist(16);
if (k == 1)
cout << "\nElement is found";
else
cout << "\nElement is not present in the list";
return 0;
}
Output
Linked List:
[ 50 22 12 33 30 44 ]
Linked List after deletion:
[ 22 33 30 ]
Updated Linked List:
[ 16 4 22 33 30 ]
Element is found
Last modified: Tuesday, 26 December 2023, 10:30 AM
Previous activity
◄ PPT Lecture 2.3.1
Jump to...
Next activity
PPT Lecture 3.1.1 ►