Program : CS(computer Science)
Semester : 3rd
Course : Data Structures & Algorithms
Presented By : Daniyal,Moazzam,Ayyan
Presented To : Prof. Asad Bilal
Topic : Linked List
Institute :
Introduction :
A linked list is a linear data structure, in which the
elements are not stored at contiguous memory locations.
The elements in a linked list are linked using pointers It is
implemented on the heap memory rather than the stack
memory as shown in the below image:
Types of Linked List
There are Three key types of linked lists:
Singly linked lists.
Doubly linked lists.
Circular linked lists.
Singly Linked List
A Singly Linked List is a specialized case of a linked list. In a
singly linked list, each node links to only the next node in the
sequence i.e.;
Creation Of Node
Node is a combination of two things One is Data & other is
Pointer that stores the address of next node.
Null
Head
5 Next
Code of Creating A Node in Singly Linked List
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
Node (int d){
this-> data= d;
this-> next= Null;
}
};
int main(){
Node* node1 = new Node(5);
Node* head = node1;
}
Insertion in Singly Linked List
Insertion At Head:
• Create a Node
Temp Head
Null Null
Temp Head
Temp-> next= Head
Null
Head
Head = Temp
Null
1 5
1 5
1 5
next next
next next
next next
Code of Inserting Node at Head in Singly LL
void insertAtHead (Node* &head, int d) {
// new node create
Node* temp = new Node(d);
temp -> next = head;
head = temp;
}
int main(){
Node* node1 = new Node(5);
Node* head = node1;
insertAtHead (head,1);
}
Insertion at Tail:
• Create a Node
Temp Head/Tail
Null Null
Head/Tail Temp
Tail-> next = Temp
Null
Head / Temp/Tail
Tail = Temp
Null
2 next 1 next
1 next
Tail
2 next
1 next 2 next
Code of Inserting Node at Tail in Singly LL
void insertAtTail (Node* &head, Node* &tail, int d) {
// new node create
Node* temp = new Node(d);
tail -> next = temp;
tail = temp;
}
int main(){
Node* node1 = new Node(1);
Node* head = node1;
Node* tail = node1;
insertAtTail (head,tail,2);
}
Doubly Linked List
A Doubly Linked List (DLL) contains an extra pointer, typically
called the previous pointer, together with the next pointer and
data which are there in the singly linked list. i.e.;
Creation Of Node
In Doubly linked list Node is a combination of three things One is
Data & other two are Pointer one stores the address of next
node and other stores the address of previous node.
Null Null
Head
5 Next
Previous
Code of Creating Node in Doubly Linked List
#include<iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
//constructor
Node(int d ) {
this-> data = d;
this->prev = NULL;
this->next = NULL;
}
};
Code for Insertion in Linked List
Insertion at Head:
• Create a node
Temp Head
Null Null Null Null
Temp Temp->next = Head
Null Null Null
Temp
Head->Previous = Temp
Null Null
Head Temp
Head = Temp
Null Null
Previ
ous
1
Nex
t
Previ
ous
2
Nex
t
Previ
ous
1
Nex
t
Nex
t
1
1
Previ
ous
Previ
ous
Nex
t
Previ
ous
Previ
ous
Previ
ous
2
2
2
Nex
t
Nex
t
Nex
t
Head
Code for insertion at Head
void insertAtHead(Node* &tail, Node* &head, int d)
{
//empty list
if(head == NULL) {
Node* temp = new Node(d);
head = temp;
tail = temp;
}
else{
Node* temp = new Node(d);
temp -> next = head;
head -> prev = temp;
head = temp;
}
}
int main(){
insertAtHead(tail,head, 1);
}
Code for Insertion in Linked List
Insertion at Tail:
• Create a node:
Head / Tail Temp
Null
Null Null Null
Head / Tail Tail->Next = Temp Temp
Null Head
Null
Head
Head->Previous = Temp
Null Null
Tail = Temp
Previ
ous
1
Nex
t
Previ
ous
2
Nex
t
Previ
ous
1
Nex
t
Nex
t
1
Previ
ous
Previ
ous
Previ
ous
2
2
Nex
t
Nex
t
temp/tail
Code for insertion at Tail
void insertAtTail(Node* &tail,Node* &head, int d)
{
if(tail == NULL) {
Node* temp = new Node(d);
tail = temp;
head = temp;
}
else{
Node* temp = new Node(d);
tail -> next = temp;
temp -> prev = tail;
tail = temp;
}
}
int main(){
insertAtTail(tail,head, 25);
}
Code of insertion at Middle
void insertAtPosition(Node* & tail, Node* &head, int position, int d)
Node* temp = head;
int cnt = 1;
while(cnt < position-1) {
temp = temp->next;
cnt++;
}
//creating a node for d
Node* nodeToInsert = new Node(d);
nodeToInsert ->next = temp -> next;
temp -> next -> prev = nodeToInsert;
temp -> next = nodeToInsert;
nodeToInsert -> prev = temp;
}
int main(){
insertAtPosition(tail, head, 7, 102);
}
Code for Traversing a Linked List
void print(Node* head)
{
Node* temp = head ;
while(temp != NULL) {
cout << temp -> data << " ";
temp = temp -> next;
}
cout << endl;
}
Circular Linked List
The circular linked list is a linked list where all nodes are
connected to form a circle. In a circular linked list, the first
node and the last node are connected to each other
which forms a circle. There is no NULL at the end.
Create a node
In Circular List Node is a combination of two things One is Data &
other is Pointer that stores the address of next node and the last
node points the first node.
1 Next
Code for creation a node in Circular Linked List
#include<iostream>
#include<map>
using namespace std;
class Node {
public:
int data;
Node* next;
//constructor
Node(int d) {
this->data = d;
this->next = NULL;
}
int main(){
Node* tail = NULL;
}
Code for Insertion a node in circular Linked List
void insertNode(Node* &tail, int element, int d) {
//empty list
if(tail == NULL) {
Node* newNode = new Node(d);
tail = newNode;
newNode -> next = newNode;
}
else{
//non-empty list
//assuming that the element is present in the list
Node* curr = tail;
while(curr->data != element) {
curr = curr -> next;
}
//element found -> curr is representing element wala node
Node* temp = new Node(d);
temp -> next = curr -> next;
curr -> next = temp;
}
}
Traversing a Node in Circular Linked List
void print(Node* tail) {
Node* temp = tail;
//empty list
if(tail == NULL) {
cout << "List is Empty "<< endl;
return ;
}
do
{
cout << tail -> data << " ";
tail = tail -> next;
}
while(tail != temp);
cout << endl;
}