The two classes from the Queueclass5.cpp program are displayed below. Please add another method which will search the Queue for a name which is supplied in its parameter and will return a Boolean value of true if the name is found, or false if not. The method will have the name findName and it will have one parameter which will be a string. Please write its signature line for the Queue class definition and write its full method definition separately

#include <iostream>
#include <cstdlib>
using namespace std;

class Node {
public:
char item;
Node* prior;
Node* next;
public:
void putItem (char x ) {item = x;}
char getItem () {return item;}
void putPriorPtr (Node* x) {prior = x;}
Node* getPriorPtr () {return prior;}
void putNextPtr (Node* x) {next = x;}
Node* getNextPtr () {return next;}
};
class Queue {
private:
Node* beginPtr;
Node* endPtr;
public:
Queue () {beginPtr = endPtr = NULL; }
void Admit (char);
void Serve ();
char Peek ();

void ListQueueForward ();

void ListQueueReverse ();

};

void Queue::Admit (char a)
{Node* temp = NULL;
if (temp = new Node()) {
temp->putItem (a);
temp->putNextPtr (NULL);
if (endPtr == NULL)
endPtr = beginPtr = temp;
else {
temp->putPriorPtr (NULL);
endPtr->putPriorPtr (temp);
temp->putNextPtr (endPtr);
endPtr = temp;
}
}
else
cout << "Dynamic Memory Exhausted" << endl;
}
void Queue::Serve ()
{if (beginPtr == NULL)
cout << "Queue is empty" << endl;
else {
cout << "Serving first member of queue: " << beginPtr->getItem() << endl;
if (endPtr == beginPtr)
{delete endPtr;
endPtr = beginPtr = NULL;
}
else
{Node* temp;
temp = beginPtr->getPriorPtr();
temp->putNextPtr (NULL);
delete beginPtr;
beginPtr = temp;
}
}
}

char Node::Peek (Node* y)
{return y->item;
}

void Queue::ListQueueForward (){
Node* temp;
for (temp = beginPtr; temp != NULL; temp = temp->getPriorPtr())
cout << " " << temp->getItem() << endl;
}
void Queue::ListQueueReverse (){
Node* temp;
for (temp = endPtr; temp != NULL; temp = temp->getNextPtr())
cout << " " << temp->getItem() << endl;
}
int main () {
Queue q;
char resp, cval;
bool peekFlag = false;
cout << "Actions: (A)dmit, (S)Serve, (P)eek, List(F)orward,"
<< " List(R)everse, (H)elp, (^Z)Quit " << endl;
while (!cin.eof()) {
cout << "Enter action selection: ";
cin >> resp;
if (!cin.eof())
switch (resp) {
case 'A': case 'a':
cout << "Enter printable character to add to Node: ";
cin >> cval;
if (!cin.eof()) {
q.Admit (cval);
}
break;
case 'S': case 's':
q.Serve ();
break;

case 'P': case 'p':
if ( beginPtr != NULL ) {
cval = q.Peek (beginPtr);
cout << "Just peeked at User value " << cval << endl;
peekFlag = true;
}
else
cout << "Queue is empty" << endl;

break;
case 'F': case 'f':
{cout << " Listing Queue from oldest to newest ..." << endl;
q.ListQueueForward();}
break;
case 'R': case 'r':
cout << " Listing Queue from newest to oldest..." << endl;
q.ListQueueReverse();
break;
case 'h': case 'H':
cout << "Actions: (A)dmit, (S)Serve, (P)eek, List(F)orward,"
<< " List(R)everse, (H)elp, (^Z)Quit " << endl;
break;
default:
cout << " Invalid Action. Try again." << endl;
}
}
system("PAUSE");
return EXIT_SUCCESS;
}

Any help I can get would be greatly appreciated to get me off on the right foot, thanks a lot! :)

Edited 6 Years Ago by cgalvani: n/a

I see you quite quickly realized how to use code tags, that's nice.

Unfortunately, as it stands, the code is simply unreadable due to almost no formatting at all. So, try formatting the code properly and re-post it - otherwise, I doubt no one is going to take a look at it. And seriously, go the extra mile and come up with specific questions/details - I hope you get the point :)

This article has been dead for over six months. Start a new discussion instead.