Hello I'm new to this forum and was wondering if I could get some help since I'm having trouble with an assignment for my C++ data structures class. I'm unsure where to start or how to create the Node class in the first question. I'm not an expert so I have trouble understanding the terminology.
I have posted the directions for the assignment just below and below that I have posted the code I'm suppose to edit for the assignment.
1. The program queueclass1.cpp is written in an older style of C++ which does not use the Container concept. Please rewrite this program so that it adheres to the container concept. To this end, the following things must be done.
(A) Create a Node class which contains the data members: item and next as shown in the existing program. Also, this class contains a default constructor and four appropriate accessor methods to get and put each data member.
(B) The class currently named Node will be renamed to Queue. This class will contain the four methods Admit, Serve, Peek and DumpQueue. Each of these four methods will access the data members of the Node class by using the accessor methods of the Node class. They will NOT use the direct access to those two data members as they do now.
(C) The Node* data members front and end in the main of the program will be moved to the Queue class, and the main of the program will no longer have access to those variables.
(D) Also, please make any other changes which you find to be necessary to cause this program to adhere to the Container concept.
(E) Please compile and run the revised program to make sure that it works properly in its new format.
// A simple queue class using a linked list of queue
// objects to illustrate FIFO (first-in-first-out)
// behavior.
#include <iostream>
#include <cstdlib>
using namespace std;
class Node { // Definition of a Node on the Queue
public:
Node* Admit (Node*, char); // add a Node to the end of the Queue
Node* Serve (Node*); // remove a Node from the front of the Queue
char Peek (Node*); // get user from front node in Queue
void DumpQueue (Node*); // diagnostic tool to view entire Queue
private:
char item; // user data - is just one character
// following are the two pointers
Node* prior; // the one ahead of you in the line
Node* next; // the one behind you in the line
};
Node* Node::Admit (Node* endPtr, char a)
{Node* temp = NULL;
if (temp = new Node)
{temp->item = a;
temp->next = NULL;
temp->prior = endPtr;
if ( endPtr != NULL)
endPtr->next = temp;
}
else
cout << "Dynamic Memory Exhausted" << endl;
return temp;
}
Node* Node::Serve (Node* y)
{Node* temp;
temp = y->next;
delete y;
if (temp != NULL)
temp->prior = NULL;
return temp;
}
char Node::Peek (Node* y) // like Stack Peek
{return y->item;
}
void Node::DumpQueue (Node* z) // like getListForward but recursive
{if ( z!= NULL )
{cout << " " << z->item << endl;
DumpQueue (z->next);}
}
int main () {
Node q;
Node* front = NULL; // the one being served - head
Node* end = NULL; // the one at the end of the line - tail
char resp, cval;
bool peekFlag = false;
cout << "Actions: Admit (1), Serve (2), Peek (3), Dump (4),"
<< " Help (H), Quit (^Z)" << endl;
while (!cin.eof()) {
cout << "Enter action selection: ";
cin >> resp;
if (!cin.eof())
if(cin.good()){
switch (resp) {
case '1': // Admit
cout << "Enter printable character to add to Node: ";
cin >> cval;
if (!cin.eof()) {
end = q.Admit (end, cval);
if (front == NULL)
front = end;
cin.clear();
}
break;
case '2': // Serve
if ( front != NULL && peekFlag == true) {
front = q.Serve (front);
cout << "Just served front Node of Queue" << endl;
}
else
if ( peekFlag == false)
cout << "Front Node of Queue has not been peeked" << endl;
else
cout << "Queue is empty" << endl;
peekFlag = false;
break;
case '3': // Peek
if ( front != NULL ) {
cval = q.Peek (front);
cout << "Just peeked at User value " << cval << endl;
peekFlag = true;
}
else
cout << "Queue is empty" << endl;
break;
case '4':
if ( front != NULL)
{cout << " Dumping Queue from oldest to newest..." << endl;
q.DumpQueue(front);}
else
cout << "Queue is empty" << endl;
break;
case 'h': case 'H':
cout << "Actions: Admit (1), Serve (2), Peek (3), Dump (4),"
<< " Help (H), Quit (^Z)" << endl;
break;
default:
cout << " Invalid Action. Try again." << endl;
}
}
else {
cout << "Invalid entry. Try again." << endl;
cin.clear();
cin.ignore (256,'\n');
}
}
system("PAUSE");
return EXIT_SUCCESS;
}
class Node // Definition of a Node on the Queue
{
public:
Node();
void SetItem(char value);
char GetItem() const;
void SetNext(Node *n);
Node *GetNext() const;
private:
char item;
Node *next;
};
class Queue{ // Definition of the Queue
public:
Node* Admit (Node *, char); // add a Node to the end of the Queue
Node* Serve (Node*); // remove a Node from the front of the Queue
char Peek (Node*); // get user from front node in Queue
void DumpQueue (Node*); // diagnostic tool to view entire Queue
private:
// following are the two pointers
Node* front; // the one to the first node
Node* end; // the one to the last node
};
That might give you an idea of what has to be done
coder777 thanks for your reply. I noticed you removed "char item; // user data - is just one character" from "private:" which I'm refering to what is now the Queue class. I'm not sure if you just missed it or it's suppose to be removed but I included it in my new code below. Also wouldn't everything that previously said Node say Queue below the class Queue? I did this in my new code. Also in class Node that you created is everything in public the four assessor methods?
// A simple queue class using a linked list of queue
// objects to illustrate FIFO (first-in-first-out)
// behavior.
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class Node {
public:
Node ();
void SetItem(char value);
char GetItem() const;
void SetNext(Node *n);
Node *GetNext() const;
private:
char item;
Node* next;
};
class Queue { // Definition of the Queue
public:
Queue* Admit (Queue*, char); // add a Node to the end of the Queue
Queue* Serve (Queue*); // remove a Node from the front of the Queue
char Peek (Queue*); // get user from front node in Queue
void DumpQueue (Queue*); // diagnostic tool to view entire Queue
private:
// following are the three pointers
Queue* prior; // the one ahead of you in the line
Queue* front; // the one to the first node
Queue* end; // the one to the last node
First: please put your code in between the code tags like this [code]Your code here[/code] it is by far more readable.
I noticed you removed "char item; // user data - is just one character" from "private:" which I'm refering to what is now the Queue class.
It is intentionally. The Queue class is now a Container that holds the Nodes. The Node holds the Data. Queue does not refer to itself because it is a container.
Also in class Node that you created is everything in public the four assessor methods?
It is necessary because Queue needs to access them.
The Queue class as the container looks like this (cleaner now)
1 2 3 4 5 6 7 8 9 10 11
class Queue{ // Definition of the Queue
public:
void Admit (char); // Queue creates the node and sets the Data via SetItem() / end->SetNext(newly created Node); / end = newly created Node
void Serve (); // remove a Node from the front / front = front->GetNext()
char Peek (); // get data via GetItem() from front / front->GetItem()
void DumpQueue (); // diagnostic tool to view entire Queue / traverse front to end
private:
// following are the two pointers
Node* front; // the one to the first node
Node* end; // the one to the last node
};
I roughly wrote down what has to be done (you has to add safty checks)
You can even omit the member variable 'end' and add it by traversing