I wrote a Double linked list class which has insertAtStart, insertAtEnd, removeFromStart, removeFromEnd
member functions. Check the code below
class Node {
public:
int val;
Node *next;
Node *prev;
Node(int v)
: val(v),
next(nullptr),
prev(nullptr) {}
};
#include<iostream>
using namespace std;
#include "../node.h"
class LinkedList {
private:
Node* start;
Node* end;
public:
LinkedList(): start(nullptr), end(nullptr) { }
void insertAtStart(int val) {
Node* n = new Node(val);
if(start == nullptr) {
start = n;
end = n;
} else {
n->next = start;
start = n;
}
}
void insertAtEnd(int val) {
Node* n = new Node(val);
if(end == nullptr) {
end = n;
start = n;
} else {
end->next = n;
n->prev = end;
end = n;
}
}
void removeFromStart() {
if(start == nullptr) return;
if(start == end) {
start = end = nullptr;
return;
}
start = start->next;
start->prev = nullptr;
}
void removeFromEnd() {
if(end == nullptr) return;
if(start == end) {
start = end = nullptr;
return;
}
end = end->prev;
end->next = nullptr;
}
void printList() {
Node *ptr = start;
while(ptr != nullptr) {
cout << ptr->val << " ";
ptr = ptr->next;
}
cout << endl;
}
};
I tested the above code using the following main function and it worked perfectly
#include "double-linked-list.h"
int main() {
LinkedList l = LinkedList();
l.insertAtStart(1);
l.insertAtStart(2);
l.insertAtStart(3);
l.insertAtStart(4);
l.insertAtStart(5);
l.insertAtEnd(6);
l.insertAtEnd(7);
l.insertAtEnd(8);
l.insertAtEnd(9);
l.insertAtEnd(10);
l.printList();
l.removeFromStart();
l.printList();
l.removeFromStart();
l.printList();
l.removeFromEnd();
l.printList();
l.removeFromEnd();
l.printList();
return 0;
}
output:
Inserting 1 at start
Linked list: 1
Inserting 2 at start
Linked list: 2 1
Inserting 3 at start
Linked list: 3 2 1
Inserting 4 at start
Linked list: 4 3 2 1
Inserting 5 at start
Linked list: 5 4 3 2 1
Inserting 6 at end
Linked list: 5 4 3 2 1 6
Inserting 7 at end
Linked list: 5 4 3 2 1 6 7
Inserting 8 at end
Linked list: 5 4 3 2 1 6 7 8
Inserting 9 at end
Linked list: 5 4 3 2 1 6 7 8 9
Inserting 10 at end
Linked list: 5 4 3 2 1 6 7 8 9 10
Removing from start
Linked list: 4 3 2 1 6 7 8 9 10
Removing from start
Linked list: 3 2 1 6 7 8 9 10
Removing from end
Linked list: 3 2 1 6 7 8 9
Removing from end
Linked list: 3 2 1 6 7 8
I now, wrote a Queue class that is derived from the above linked list class
#include "../double-linked-list/double-linked-list.h"
class Queue {
private:
LinkedList q;
public:
Queue() {}
void enqueue(int val) {
q.insertAtStart(val);
}
void dequeue() {
q.removeFromEnd();
}
void printQueue() {
q.printList();
}
};
and the following main function to test it
#include "queue.h"
int main() {
Queue q = Queue();
q.enqueue(1);
q.printQueue();
q.enqueue(2);
q.printQueue();
q.enqueue(3);
q.printQueue();
q.enqueue(4);
q.printQueue();
q.enqueue(5);
q.printQueue();
q.dequeue();
q.printQueue();
q.dequeue();
q.printQueue();
}
enqueue seems to work perfectly but dequeue doesn't. It's not even printing anything after dequeue.
Output:
Enqueuing 1
1
Enqueuing 2
2 1
Enqueuing 3
3 2 1
Enqueuing 4
4 3 2 1
Enqueuing 5
5 4 3 2 1
Denqueuing
In an attempt to debug, I put a cout
statement in removeFromEnd
function in LinkedList
.
void LinkedList::removeFromEnd(){
if(end == nullptr) return;
if(start == end) {
start = end = nullptr;
return;
}
end = end->prev;
cout << "print statement 1";
end->next = nullptr;
cout << "print statement 2";
}
Now, when I ran queue's main function again, print statement 1 is printing in console but the 2nd one isn't. I'm not able to understand why. Can someone pls help me figure out what I'm doing wrong?
EDIT:
After reading the comments I made changes to the following functions
void insertAtStart(int val) {
Node* n = new Node(val);
// If start and end are null
if(start == nullptr && end == nullptr) {
start = end = n;
}
// There is at least 1 node in the list
else {
n->next = start;
start->prev = n;
// There is only one node in the list
if(start->next == nullptr) {
end->prev = n;
}
start = n;
}
}
void insertAtEnd(int val) {
Node* n = new Node(val);
// If start and end are null
if(start == nullptr && end == nullptr) {
start = end = n;
}
// There is at least 1 node in the list
else {
n->prev = end;
end->next = n;
// There is only one node in the list
if(end->prev == nullptr) {
start->next = n;
}
end = n;
}
}
This worked. I posted the entire Linked List class as answer if interested.