0% found this document useful (0 votes)
9 views

C++ Programming Stacks Course

This lab report describes implementing functions for a doubly linked list data structure including adding and removing nodes from the head and tail, removing a specific element, adding a new element before an existing one, and displaying the list in forward and reverse order. The report includes code defining Node and DoublyLinkedList classes with the required functions as well as a main function testing the implementation.

Uploaded by

dammah2017
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

C++ Programming Stacks Course

This lab report describes implementing functions for a doubly linked list data structure including adding and removing nodes from the head and tail, removing a specific element, adding a new element before an existing one, and displaying the list in forward and reverse order. The report includes code defining Node and DoublyLinkedList classes with the required functions as well as a main function testing the implementation.

Uploaded by

dammah2017
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

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:

You might also like