DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Experiment No - 4
Student Name: Mohd Talib Siddiqi UID: 22BCS13509
Branch: BE-CSE Section/Group: 615-A
Semester: 5th Date of Performance: 16/08/24
Subject Name: DAA Subject Code: 22CSH-311
1. Aim:
Apply the concept of Linked list and write code to Insert and Delete an
element at the beginning and at end in Singly, Doubly and Circular Linked
List.
2. Objective
To understand singly, doubly and circular linked list.
3. Algorithm:
Singly Linked List Algorithm:
1. Node Creation: Define a node class with data and next pointers.
2. Insert at Head: Create a new node and update its next to point to the
current head. Update the head pointer to this new node.
3. Insert at Tail: Traverse to the end of the list or use the tail pointer
directly. Link the new node at the end and update the tail pointer.
4. Traversal: Start from the head and print each node’s data while moving to
the next node until reaching NULL.
5. Deletion: Handle edge cases for empty list, single node list, or general
case by updating pointers to bypass the node to be deleted.
Doubly Linked List Algorithm:
1. Node Creation: Define a node class with data, next, and prev pointers.
2. Insert at Head: Create a new node, set its next to the current head, and
update the head’s prev. Update the head pointer to this new node.
1|22 BCS135 0 9
9
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3. Insert at Tail: Create a new node, set its prev to the current tail, and
update the tail’s next. Update the tail pointer to this new node.
4. Traversal: Start from the head and print each node’s data while moving to
the next node until reaching NULL.
5. Deletion: Handle edge cases for empty list, single node list, or general
case by updating both next and prev pointers to bypass the node to be
deleted.
Circular Linked List Algorithm:
1. Node Creation: Define a Node class with data and next pointers.
2. Insert at Head: Create a new node, set its next to the current head, update
the tail’s next to the new head, and update the head pointer to this new
node.
3. Insert at Tail: Create a new node, link it to the current tail’s next, and
update the tail pointer. Set the new tail’s next to the head to maintain
circularity.
4. Traversal: Start from the head, print each node’s data while moving to
the next node until returning to the head.
5. Deletion: Handle edge cases for empty list, single node list, or general
case by updating pointers and ensuring the circular link between tail and
head is maintained.
4. Source Code:
(i) Singly Linked List
#include<iostream>
class node{
public:
int data;
node* next;
node(int value){
this -> data = value;
this -> next = NULL;
}
2|22 BCS135 0 9
9
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
};
// inserting at head
void insertAtHead(int value, node* &head, node* &tail){
node* newNode = new node(value);
// if linkedlist is empty
if(head == NULL && tail == NULL){
head = newNode;
tail = newNode;
}else{
node* temp = head;
newNode -> next = head;
head = newNode;
}
}
void insertAtTail(int value, node* &head, node* &tail){
node * newNode = new node(value);
if(head == NULL && tail == NULL){
head = newNode;
tail = newNode;
}else{
node * temp = tail;
temp -> next = newNode;
tail = newNode;
}
}void traverse(node* head){
node* temp = head;
while(temp != NULL){
cout<<temp -> data<<" -> ";
temp = temp -> next;
}
cout<<"NULL"<<endl;
}
void deleteFromLinkedList(int position, node* &head, node* &tail){
if(head == NULL && tail == NULL){
3|22 BCS135 0 9
9
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
cout<<"LinkedList is Empty"<<endl;
return;
}
if(head == tail){
node* temp = head;
head = NULL;
tail = NULL;
delete temp;
}else{
if(position == 1){
node* temp = head;
head = head -> next;
delete temp;
}else{
node* temp = head;
int i=1;
while(i < position-1){
temp = temp -> next;
i++;
} temp-> next = temp -> next -> next;
}
}
}
int main(){
node* head = NULL;
node* tail = NULL;
insertAtHead(10, head, tail);
insertAtHead(20, head, tail);
insertAtHead(30, head, tail);
insertAtHead(40, head, tail);
insertAtTail(50, head, tail);
insertAtTail(60, head, tail);
traverse(head);
deleteFromLinkedList(9, head, tail);
4|22 BCS135 0 9
9
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
traverse(head);
return 0;
}
(ii) Doubly LinkedList
#include<iostream>
using namespace std;
class node{
public:
int data;
node* next;
node* prev;
node(int value){
data = value;
prev = NULL;
next = NULL;
}
};
void insertAtHead(int value, node* &head, node* &tail){
node* newNode = new node(value);
if(head == NULL && tail == NULL){
head = newNode;
tail = newNode;
}else{
newNode -> next = head;
head -> prev = newNode;
head = newNode;
}
}void insertAtTail(int value, node* &head, node* &tail){
node* newNode = new node(value);
if(head == NULL && tail == NULL){
head = newNode;
tail = newNode;
}
else{
5|22 BCS135 0 9
9
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
tail -> next = newNode;
newNode -> prev = tail;
tail = newNode;
}
}void deleteFromLinkedlist(int position, node* &head, node* &tail){
if(position < 1 || position > getLength(head)){
cout<<"Position out of bound"<<endl;
return;
}
if(head == NULL && tail == NULL){
cout<<"Linkedlist is Empty"<<endl;
return;
} else if(head == tail){
node*temp = head;
head = NULL;
tail = NULL;
delete temp;
}else if(position == 1){
node* temp = head;
head = head -> next;
head -> prev = NULL;
temp -> next = NULL;
delete temp;
}else if(position == getLength(head)){
node* temp = tail;
tail = tail -> prev;
tail -> next = NULL;
temp -> prev = NULL;
delete temp;
}else{
node* left = head;
int i=1;
while(i < position-1){ // we are at the node to delete
left = left -> next;
6|22 BCS135 0 9
9
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
i++;
}
node* current = left -> next;
node* right = current -> next;
left -> next = right;
right -> prev = left;
current -> next = NULL;
current -> prev = NULL;
delete current;
}
}void traversee(node* &head){
node* temp = head;
while(temp != NULL){
cout<<temp -> data<<" -> ";
temp = temp -> next;
}cout<<"NULL"<<endl;
}
int main(){
node* head = NULL;
node* tail = NULL;
insertAtHead(10, head, tail);
insertAtHead(20, head, tail);
insertAtHead(30, head, tail);
insertAtTail(40, head, tail);
insertAtTail(50, head, tail);
deleteFromLinkedlist(1, head, tail);
traversee(head);
deleteFromLinkedlist(5, head, tail);
traversee(head);
deleteFromLinkedlist(10, head, tail);
traversee(head);
return 0;
}
(iii) Circular LinkedList
7|22 BCS135 0 9
9
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int value) {
this->data = value;
this->next = NULL;
}
};
void insertAtHead(int value, Node* &head, Node* &tail) {
Node* newNode = new Node(value);
if (head == NULL && tail == NULL) {
head = newNode;
tail = newNode;
tail->next = head; // Make it circular
} else {
newNode->next = head;
head = newNode;
tail->next = head; // Update the tail's next to the new head
}
}void insertAtTail(int value, Node* &head, Node* &tail) {
Node* newNode = new Node(value);
if (head == NULL && tail == NULL) {
head = newNode;
tail = newNode;
tail->next = head; // Make it circular
} else {
tail->next = newNode;
tail = newNode;
tail->next = head; // Make the tail point to head
}
}
void traverse(Node* head) {
8|22 BCS135 0 9
9
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
if (head == NULL) {
cout << "List is empty." << endl;
return;
}Node* temp = head;
do {
cout << temp->data << " -> ";
temp = temp->next;
} while (temp != head);
cout << "HEAD (Circular)" << endl;
}
void deleteFromLinkedList(int position, Node* &head, Node* &tail) {
if (head == NULL && tail == NULL) {
cout << "LinkedList is Empty" << endl;
return;
}
if (head == tail) { // Only one element
delete head;
head = NULL;
tail = NULL;
} else {
if (position == 1) { // Deleting the head node
Node* temp = head;
head = head->next;
tail->next = head; // Update tail's next
delete temp;
} else {
Node* temp = head;
int i = 1;
while (i < position - 1 && temp->next != head) {
temp = temp->next;
i++;
}
if (temp->next == head) {
cout << "Position out of bounds" << endl;
return;
9|22 BCS135 0 9
9
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
Node* delNode = temp->next;
temp->next = temp->next->next;
if (delNode == tail) { // Deleting the tail node
tail = temp;
}
delete delNode;
}
}
}
int main() {
Node* head = NULL;
Node* tail = NULL;
insertAtHead(10, head, tail);
insertAtHead(20, head, tail);
insertAtHead(30, head, tail);
insertAtHead(40, head, tail);
insertAtTail(50, head, tail);
insertAtTail(60, head, tail);
traverse(head);
deleteFromLinkedList(3, head, tail); // Deleting the 3rd node
traverse(head);
deleteFromLinkedList(6, head, tail); // Deleting the last node (tail)
traverse(head);
deleteFromLinkedList(1, head, tail); // Deleting the head node
traverse(head);
return 0;
}
10 | 2 2 B C S 1 3 5 0 9
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
5. Screenshot of Outputs:
(i) Singly Linked List:
(ii) doubly Linked List:
(iii) Circular Linked List:
11 | 2 2 B C S 1 3 5 0 9
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. Learning Outcomes:
1. Node Structure and Links
2. Insertion and Deletion Operations
3. Traversal Techniques
4. Memory Management
5. Use Cases and Applications
11 | 2 2 B C S 1 3 5 0 9