I am starting to jump into C++ right now for my class, and we covered enqueue and dequeue in Scheme. However, we're doing it in C++ now, and we need to use pointers, which I am struggling to understand. I understand what pointers are, when to use them, etc. However, I'm struggling to figure out how to implement them into this lab. I currently have this for code:
#include <stdlib.h>
#include <iostream>
#include <assert.h>
using namespace std;
struct node {
int item;
node* next;
};
typedef node* nodePtr;
typedef nodePtr* queue;
queue makeQueue() {
queue q = new nodePtr[2];
q[0] = NULL;
q[1] = NULL;
return q;
}
nodePtr start(queue q) {
return q[0];
}
nodePtr tail(queue q) {
return q[1];
}
void setStart(queue q, nodePtr newNode) {
q[0] = newNode;
}
void setTail(queue q, nodePtr newNode) {
q[1] = newNode;
}
bool emptyQueue(queue q) {
return q[0] == NULL;
}
int head(queue q) {
if (emptyQueue(q)) {
cout << "Attempt to take head of an empty queue.\n";
exit(1);
}
return start(q)->item;
}
void enqueue(queue q, int newItem) {
// Answer goes here.
}
void dequeue(queue q) {
// Answer goes here.
}
For this code, I understand what I need to do: Enqueue will push newItem onto the cdr (or tail) of the queue, and Dequeue will remove the node at the start of the queue, with appropriate locations updated accordingly. But it's just the pointers that are really confusing me, and I don't know what to do.
How would I dive into writing enqueue and dequeue with pointers? I'm going to blankly assume that enqueue and dequeue shouldn't be lines and lines of code.
Thank you so much!