Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C++ Implement the queue class as shown below. (don\'t use STL verison please) Us

ID: 3721991 • Letter: C

Question

C++

Implement the queue class as shown below.

(don't use STL verison please)

Use it to store 5 students waiting outside Student Services to meet with advisors. Add data to each node which includes student ID and major.

Add a member function displayQueue which displays the contents of the queue. Write the main function to test the queue. Be sure to show the contents of queue after adding or removing each student.

-------------------------------------------------------------- my queue class
#ifdef queue_h
#define queue_h
namespace queues
{
struct queueNode{
char data;
queueNode *link;
};
typedef queueNode* queueNodePtr;
class queue {
public:
queue();
queue(const queue& aQueue);
~queue();
void add(char item);
char remove();
bool empty() const;
private:
queueNodePtr front;
queueNodePtr back;
};
}
#endif

Explanation / Answer

// A C program to demonstrate linked list based implementation of queue
#include <stdlib.h>
#include <stdio.h>

// A linked list (LL) node to store a queue entry
struct QNode
{
int id;
int major;
struct QNode *next;
};

// The queue, front stores the front node of LL and rear stores ths
// last node of LL
struct Queue
{
struct QNode *front, *rear;
};

// A utility function to create a new linked list node.
struct QNode* newNode(int i,int m)
{
struct QNode *temp = (struct QNode*)malloc(sizeof(struct QNode));
temp->id = i;
temp->major=m;
temp->next = NULL;
return temp;
}

// A utility function to create an empty queue
struct Queue *createQueue()
{
struct Queue *q = (struct Queue*)malloc(sizeof(struct Queue));
q->front = q->rear = NULL;
return q;
}

// The function to add a key k to q
void enQueue(struct Queue *q, int i,int m)
{
// Create a new LL node
struct QNode *temp = newNode(i,m);

// If queue is empty, then new node is front and rear both
if (q->rear == NULL)
{
q->front = q->rear = temp;
return;
}

// Add the new node at the end of queue and change rear
q->rear->next = temp;
q->rear = temp;
}

// Function to remove a key from given queue q
struct QNode *deQueue(struct Queue *q)
{
// If queue is empty, return NULL.
if (q->front == NULL)
return NULL;

// Store previous front and move front one node ahead
struct QNode *temp = q->front;
q->front = q->front->next;

// If front becomes NULL, then change rear also as NULL
if (q->front == NULL)
q->rear = NULL;
return temp;
}

// Driver Program to test anove functions
int main()
{
struct Queue *q = createQueue();
enQueue(q, 10,1);
enQueue(q, 20,2);
deQueue(q);
deQueue(q);
enQueue(q, 30,3);
enQueue(q, 40,4);
enQueue(q, 50,5);
struct QNode *n = deQueue(q);
if (n != NULL)
printf("Dequeued item is %d", n->id);
return 0;
}