Link List Data Structure
Link List Data Structure
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.
Singly linked lists contain two “buckets” in one node; one bucket
holds the data and the other bucket holds the address of the next
node of the list. Traversals can be done in one direction only as
there is only a single link between two nodes of the same list.
Basic Operations in the Linked Lists
The basic operations in the linked lists are insertion, deletion,
searching, display, and deleting an element at a given key. These
operations are performed on Singly Linked Lists as given below −
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.
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
#include <bits/stdc++.h>
#include <string>
struct node {
int data;
void printList(){
while(p != NULL) {
p = p->next;
//create a link
lk->data = data;
lk->next = head;
head = lk;
}
int main(){
insertatbegin(12);
insertatbegin(22);
insertatbegin(30);
insertatbegin(44);
insertatbegin(50);
// print list
printList();
Output
Linked List:
[ 50 44 30 22 12 ]
Insertion at Ending
#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:
[ 50 30 12 22 44 ]
#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;
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.
Deletion at Beginning
#include <bits/stdc++.h>
#include <string>
int data;
};
void printList(){
while(p != NULL) {
p = p->next;
//create a link
lk->data = data;
lk->next = head;
//point first to new first node
head = lk;
void deleteatbegin(){
head = head->next;
int main(){
insertatbegin(12);
insertatbegin(22);
insertatbegin(30);
insertatbegin(44);
insertatbegin(50);
// print list
printList();
deleteatbegin();
printList();
Output
Linked List:
[ 50 44 30 22 12 ]
Linked List after deletion:
[ 44 30 22 12 ]
Deletion at Ending
#include <string>
struct node {
int data;
};
void printList(){
while(p != NULL) {
p = p->next;
//create a link
lk->data = data;
lk->next = head;
//point first to new first node
head = lk;
void deleteatend(){
linkedlist = linkedlist->next;
linkedlist->next = NULL;
int main(){
insertatbegin(12);
insertatbegin(22);
insertatbegin(30);
insertatbegin(44);
insertatbegin(50);
// print list
printList();
deleteatend();
printList();
Output
Linked List: 50 44 30 22 12
Linked List after deletion: 50 44 30 22
#include <bits/stdc++.h>
#include <string>
struct node {
int data;
};
void printList(){
while(p != NULL) {
p = p->next;
//create a link
struct node *lk = (struct node*) malloc(sizeof(struct node));
lk->data = data;
lk->next = head;
head = lk;
head = temp->next;
return;
prev = temp;
temp = temp->next;
prev->next = temp->next;
int main(){
insertatbegin(12);
insertatbegin(22);
insertatbegin(30);
insertatbegin(44);
insertatbegin(50);
// print list
printList();
deletenode(30);
printList();
Output
Linked List:
[ 50 44 30 22 12 ]Linked List after deletion:
[ 50 44 22 12 ]
Doubly Linked Lists contain three “buckets” in one node; one bucket
holds the data and the other buckets hold the addresses of the
previous and next nodes in the list. The list is traversed twice as the
nodes in the list are connected to each other from both sides.
Doubly Linked List contains a link element called first and last.
Each link carries a data field(s) and a link field called next.
Each link is linked with its next link using its next link.
Each link is linked with its previous link using its previous link.
The last link carries a link as null to mark the end of the list
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
struct node {
int data;
int key;
struct node *next;
};
bool isEmpty(){
void printList(){
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
//create a link
link->key = key;
link->data = data;
if(isEmpty()) {
last = link;
} else {
head->prev = link;
}
//point it to old first link
link->next = head;
head = link;
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printList();
return 0;
}
This deletion operation deletes the existing first nodes in the doubly
linked list. The head is shifted to the next node and the link is
removed.
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
struct node {
int data;
int key;
};
bool isEmpty(){
}
//display the doubly linked list
void printList(){
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
//create a link
link->key = key;
link->data = data;
if(isEmpty()) {
last = link;
} else {
//update first prev link
head->prev = link;
link->next = head;
head = link;
if(head->next == NULL) {
last = NULL;
} else {
head->next->prev = NULL;
head = head->next;
return tempLink;
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertFirst(5,40);
insertFirst(6,56);
printList();
deleteFirst();
printList();
return 0;
}
Doubly Linked List: (6,56) (5,40) (4,1) (3,30) (2,20) (1,10)
List after deleting first record: (5,40) (4,1) (3,30) (2,20) (1,10)
In this insertion operation, the new input node is added at the end
of the doubly linked list; if the list is not empty. The head will be
pointed to the new node, if the list is empty.
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdbool>
struct node {
int data;
int key;
};
bool isEmpty(){
void printList(){
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
//create a link
link->data = data;
if(isEmpty()) {
last = link;
} else {
head->prev = link;
link->next = head;
head = link;
link->key = key;
link->data = data;
if(isEmpty()) {
last = link;
} else {
last->next = link;
link->prev = last;
last = link;
}
int main(){
insertFirst(1,10);
insertFirst(2,20);
insertFirst(3,30);
insertFirst(4,1);
insertLast(5,40);
insertLast(6,56);
printList();
return 0;
Output
Doubly Linked List: (4,1) (3,30) (2,20) (1,10) (5,40) (6,56)
Circular linked lists can exist in both singly linked list and doubly
linked 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.
Circular singly linked list is a type of data structure that is made up of nodes
that are created using self referential structures. Each of these nodes contain
two parts, namely the data and the reference to the next list node.
Only the reference to the first list node is required to access the whole linked
list. This is known as the head. The last node in the list points to head or first
node of the list. That is the reason this is known as a circular linked list.
Example
Live Demo
#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* head = NULL;
void insert(int newdata) {
struct Node *newnode = (struct Node
*)malloc(sizeof(struct Node));
struct Node *ptr = head;
newnode->data = newdata;
newnode->next = head;
if (head!= NULL) {
while (ptr->next != head)
ptr = ptr->next;
ptr->next = newnode;
} else
newnode->next = newnode;
head = newnode;
}
void display() {
struct Node* ptr;
ptr = head;
do {
cout<<ptr->data <<" ";
ptr = ptr->next;
} while(ptr != head);
}
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The circular linked list is: ";
display();
return 0;
}
Output
The circular linked list is: 9 2 7 1 3
In the above program, the structure Node forms the linked list node. It contains
the data and a pointer to the next linked list node. This is given as follows.
struct Node {
int data;
struct Node *next;
};
The function insert() inserts the data into the beginning of the linked list. It
creates a newnode and inserts the number in the data field of the newnode. If
the head is NULL, then newnode points to itself otherwise the last node in the
circular linked list points to newnode. Then the head points to the start of the
list i.e. to the newnode. This is given below.
void insert(int newdata) {
struct Node *newnode = (struct Node
*)malloc(sizeof(struct Node));
struct Node *ptr = head;
newnode->data = newdata;
newnode->next = head;
if (head!= NULL) {
while (ptr->next != head)
ptr = ptr->next;
ptr->next = newnode;
} else
newnode->next = newnode;
head = newnode;
}
The function display() displays the whole linked list. First ptr points to head.
Then it is continuously forwarded to the next node until all the data values of
the nodes are printed. This is given below.
void display() {
struct Node* ptr;
ptr = head;
do {
cout<< ptr->data <<" ";
ptr = ptr->next;
} while(ptr != head);
}
In the function main(), first various values are inserted into the circular linked
list by calling insert(). Then the linked list is displayed. This is given below.
int main() {
insert(3);
insert(1);
insert(7);
insert(2);
insert(9);
cout<<"The circular linked list is: ";
display();
return 0;
}
void push_back(int newElement) {
#include <iostream>
using namespace std;
//node structure
struct Node {
int data;
Node* next;
};
class LinkedList {
private:
Node* head;
public:
LinkedList(){
head = NULL;
}
return 0;
}