Question 3. Enqueue and dequeue using a linked list [6]
Given the definitions below for a linked list class and a queue class, provide appropriate implementations for the enqueue and dequeue methods.
class List {
private: // internal list and node composition
struct Lnode {
string data;
Lnode *next, *prev;
};
Lnode *front, *back;
public:
List(); // empty list constructor
~List(); // deallocate remaning nodes
// routines to insert a new node at the front or back
// of the list, returning true iff successful
bool insertAtFront(string s);
bool insertAtBack(string s);
// routines to copy front or back string out of the list
// and delete the node, returing true iff successful
bool removeFromFront(string &s);
bool removeFromBack(string &s);
};
|
class Queue {
private:
// internally the queue is stored as a list
List Q;
public:
Queue(); // empty queue constructor
~Queue(); // deallocate remaining queued items
// add new item to back of queue,
// returning true iff successful
bool enqueue(string s);
// remove front item from queue (copy into s)
// returning true iff successful
bool dequeue(string &s);
};
|