Assignment 3
DSA
DoublyLinkedList Class Implementation for List, Stack, and
Queue ADTs
CODE:
#include <iostream>
class DoublyLinkedList {
private:
struct Node {
int data;
Node* prev;
Node* next;
Node(int value) : data(value), prev(nullptr), next(nullptr) {}
};
Node* head;
Node* tail;
int size;
public:
//CONSTRUCTOR
DoublyLinkedList() : head(nullptr), tail(nullptr), size(0) {}
//DESTRUCTOR
~DoublyLinkedList() {
clear();
}
//FUNCTION TO INSERT AN ELEMENT IN END
void insert(int element) {
Node* newNode = new Node(element);
if (!head) {
head = tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
++size;
}
//FUNCTION TO DEL AN ELEMENT
void removeAt(int index) {
if (index < 0 || index >= size) {
std::cout << "Invalid index. Cannot remove element.\n";
} else {
Node* current = head;
for (int i = 0; i < index; ++i) {
current = current->next;
}
if (current->prev) {
current->prev->next = current->next;
} else {
head = current->next;
}
if (current->next) {
current->next->prev = current->prev;
} else {
tail = current->prev;
}
delete current;
--size;
}
}
//ELEMENT RETRIEVAL FUNCTION
int getAt(int index) const {
if (index < 0 || index >= size) {
std::cout << "Invalid index/n";
return -1;
} else {
Node* current = head;
for (int i = 0; i < index; ++i) {
current = current->next;
}
return current->data;
}
}
//FUNCION TO CHECK IF LIST IS EMPTY
bool isEmpty() const {
return size == 0;
}
//FUNCION TO GET SIZE OF LIST
int getSize() const {
return size;
}
//FUNCION TO CLEAR LIST
void clear() {
while (head) {
Node* temp = head;
head = head->next;
delete temp;
}
tail = nullptr;
size = 0;
}
};
//STACK USING DOUBLYLINKEDLIST
class Stack : public DoublyLinkedList {
public:
using DoublyLinkedList::DoublyLinkedList; // Inherit constructor
//FUNCTION TO PUSH ELEMENT
void push(int element) {
insert(element);
}
//FUNCTION TO POP ELEMENT
void pop() {
removeAt(getSize() - 1);
}
//FUNCTION TO GET ELEMENT
int peek() const {
return getAt(getSize() - 1);
}
};
//QUEUE USING DOUBLYLINKEDLIST
class Queue : public DoublyLinkedList {
public:
using DoublyLinkedList::DoublyLinkedList; // Inherit constructor
//FUNCTION TO ENQUEU AN ELEMNET
void enqueue(int element) {
insert(element);
}
//FUNCTION TO DEQUEU AN ELEMNET
void dequeue() {
removeAt(0);
}
//FUNCTON TO GET FRONT QUEUE ELEMENT
int front() const {
return getAt(0);
}
};
int main() {
// DoublyLinkedList DEMONSTRTION
DoublyLinkedList myList;
myList.insert(10);
myList.insert(20);
myList.insert(30);
std::cout << "List: ";
for (int i = 0; i < myList.getSize(); ++i) {
std::cout << myList.getAt(i) << " ";
}
std::cout << "\n\n";
// Stack DEMONSTRTION
Stack myStack;
myStack.push(10);
myStack.push(20);
myStack.push(30);
std::cout << "Stack (Top to Bottom): ";
while (!myStack.isEmpty()) {
std::cout << myStack.peek() << " ";
myStack.pop();
}
std::cout << "\n\n";
// Queue DEMONSTRATION
Queue myQueue;
myQueue.enqueue(10);
myQueue.enqueue(20);
myQueue.enqueue(30);
std::cout << "Queue (Front to Rear): ";
while (!myQueue.isEmpty()) {
std::cout << myQueue.front() << " ";
myQueue.dequeue();
}
std::cout << "\n\n";
return 0;
}
OUTPUT:
List: 10 20 30
Stack (Top to Bottom): 30 20 10
Queue (Front to Rear): 10 20 30