DATA STRUCTURES AND ALGORITHMS
Lab Report 05
Submitted By
Hammad Ahmed Taj
NUML-F22-55108
Submitted To
Mam Sardar Un Nisa
CLASS: BS-CS (3rd Semester)
Department of Computer Sciences
NATIONAL UNIVERSITY OF MODERN LANGUAGES - RAWALPINDI
NATIONAL UNIVERSITY OF MODERN LANGUAGES - RAWALPINDI
Department of Computer Sciences
Lab Task 05
Task 01: Implement the following functions of Doubly Linkedlist
removeFromHead()
removeFromTail()
removeGivenElement(element)
addBeforeGivenElement(existingElement, newElement)
Add after specific node
Display list in forward and reverse order
Remove all nodes from the list
Code:
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node* prev;
Node* next;
Node(int value) {
data = value;
prev = NULL;
next = NULL;
}
};
class DoublyLinkedList {
private:
Node* head;
Node* tail;
public:
DoublyLinkedList() {
head = NULL;
tail = NULL;
}
void addToHead(int value) {
Node* newNode = new Node(value);
if (head == NULL) {
NATIONAL UNIVERSITY OF MODERN LANGUAGES - RAWALPINDI
Department of Computer Sciences
head = newNode;
tail = newNode;
} else {
newNode->next = head;
head->prev = newNode;
head = newNode;
}
}
void addToTail(int value) {
Node* newNode = new Node(value);
if (tail == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
void removeFromHead() {
if (head == NULL) {
cout << "List is empty, cannot remove from head." << endl;
return;
}
Node* temp = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
} else {
tail = NULL;
}
delete temp;
}
void removeFromTail() {
if (tail == NULL) {
cout << "List is empty, cannot remove from tail." << endl;
return;
}
Node* temp = tail;
tail = tail->prev;
if (tail != NULL) {
tail->next = NULL;
} else {
head = NULL;
}
delete temp;
NATIONAL UNIVERSITY OF MODERN LANGUAGES - RAWALPINDI
Department of Computer Sciences
}
void removeGivenElement(int element) {
Node* current = head;
while (current != NULL) {
if (current->data == element) {
if (current == head) {
removeFromHead();
} else if (current == tail) {
removeFromTail();
} else {
current->prev->next = current->next;
current->next->prev = current->prev;
delete current;
}
return;
}
current = current->next;
}
cout << "Element not found in the list." << endl;
}
void addBeforeGivenElement(int existingElement, int newElement) {
Node* current = head;
while (current != NULL) {
if (current->data == existingElement) {
Node* newNode = new Node(newElement);
newNode->prev = current->prev;
newNode->next = current;
if (current->prev != NULL) {
current->prev->next = newNode;
} else {
head = newNode;
}
current->prev = newNode;
return;
}
current = current->next;
}
cout << "Existing element not found in the list." << endl;
}
void displayForward() {
Node* current = head;
cout << "List in forward order: ";
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
NATIONAL UNIVERSITY OF MODERN LANGUAGES - RAWALPINDI
Department of Computer Sciences
cout << endl;
}
void displayReverse() {
Node* current = tail;
cout << "List in reverse order: ";
while (current != NULL) {
cout << current->data << " ";
current = current->prev;
}
cout << endl;
}
void removeAll() {
while (head != NULL) {
removeFromHead();
}
cout << "All nodes removed from the list." << endl;
}
};
int main() {
DoublyLinkedList list;
list.addToTail(1);
list.addToTail(2);
list.addToTail(3);
list.addToTail(4);
list.displayForward();
list.displayReverse();
list.removeFromHead();
list.removeFromTail();
list.displayForward();
list.displayReverse();
list.removeGivenElement(2);
list.addBeforeGivenElement(3, 5);
list.displayForward();
list.displayReverse();
list.removeAll();
return 0;
}
NATIONAL UNIVERSITY OF MODERN LANGUAGES - RAWALPINDI
Department of Computer Sciences
Output: