Writing a queue program
May 5, 2012 at 10:31pm May 5, 2012 at 10:31pm UTC
My professor is asking us to write a queue program, by which I mean that we are required to write out all of the functions of a queue like push, pop, etc. The program he asked us to write takes place in two .cpp files and one header file.
The professor provided some of the driver and the header files, but I still need to write the function itself, and add the code to a few functions on the driver. The header file is finished and cannot be modified. I wrote all of the necessary functions and the program does compile but I can't get it to do what it should.
Here is the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
//The Header
const int MAX = 3; //To do: determine appropriate number
struct Passenger {
char name[80];
};
class CQueue {
private :
int front;
int rear;
Passenger passengers[MAX];
public :
CQueue();
bool IsEmpty(void );
bool IsFull(void );
void Enqueue(Passenger);
Passenger Front(void );
void Dequeue(void );
};
//The Functions:
#include "cqueue.h"
#include <cstring>
#include <iostream>
using namespace std;
CQueue::CQueue()
{
for (int x=0;x<3;x++)
{
passengers[x].name==" " ;
}
front=0;
rear=0;
}
bool CQueue::IsEmpty()
{
for (int x=0;x<3;x++)
{
if (passengers[x].name!=" " )
{
return false ;
}
}
return true ;
}
bool CQueue::IsFull()
{
for (int x=0;x<3;x++)
{
if (passengers[x].name==" " )
{
return false ;
}
}
return true ;
}
void CQueue::Enqueue(Passenger thePassenger)
{
passengers[front]=thePassenger;
if (front!=rear)
{
rear--;
}
front++;
}
Passenger CQueue::Front()
{
cout<<"Outputting the passenger list: Booked Passengers:" <<endl;
return passengers[1];
}
void CQueue::Dequeue()
{
for (int x=0;x<3;x++)
{
passengers[x].name==" " ;
}
}
//The Driver:
#include <iostream>
#include <string>
#include <cstring>
#include "cqueue.h"
using namespace std;
enum choice { BOOKED, WAITING };
const int LINES = 2;
int showMenu(void );
void addPassenger(CQueue*);
void deletePassenger(CQueue*);
void showPassengers(CQueue*);
int main (void )
{
CQueue qPassengers[LINES];
int x;
do {
x = showMenu();
switch (x)
{
case 1: addPassenger(qPassengers);
break ;
case 2: deletePassenger(qPassengers);
break ;
case 3: showPassengers(qPassengers);
break ;
}
} while (x != 4);
return 0;
}
int showMenu(void )
{
int select;
cout << "Menu\n" ;
cout << "========\n" ;
cout << "1. Add Passenger\n" ;
cout << "2. Delete Passenger\n" ;
cout << "3. Show Passengers\n" ;
cout << "4. Exit\n" ;
cout << "Enter choice: " ;
cin >> select;
return select;
}
//void addPassenger(CQueue*);
//void deletePassenger(CQueue*);
//void showPassengers(CQueue*);
void addPassenger(CQueue *theQueue)
{
Passenger theString;
if ((theQueue[1].IsFull()==false ) && (theQueue[2].IsFull()==false ))
{
cout<<"Both queues are full." <<endl;
}
else if (theQueue[1].IsEmpty()==false )
{
cout<<"Please type in the name of the new passenger." ;
cin>>theString.name;
theQueue[1].Enqueue(theString);
}
else if ((theQueue[1].IsEmpty())==true && (theQueue[1].IsFull())==true )
{
cout<<"Please type in the name of the new passenger." ;
cin>>theString.name;
theQueue[1].Enqueue(theString);
}
else if (theQueue[2].IsEmpty()==false )
{
cout<<"Please type in the name of the new passenger." ;
cin>>theString.name;
theQueue[2].Enqueue(theString);
}
else if ((theQueue[2].IsEmpty())==true && (theQueue[2].IsFull())==true )
{
cout<<"Please type in the name of the new passenger." ;
cin>>theString.name;
theQueue[2].Enqueue(theString);
}
}
void deletePassenger(CQueue *theQueue)
{
theQueue[1].Dequeue();
}
void showPassengers(CQueue *theQueue)
{
for (int x=1;x<=2;x++)
{
cout<<theQueue[1].Front().name;
}
}
The way the program is supposed to work is to create two CQueue objects with one of them as a list of passengers on the plane and one as a waiting list. If the plane is full, passengers go on the waiting list (which also has a limit).
When I try to make the program show passengers it doesn't work (outputs a huge amount of random characters) and because of this I can't really tell if add or delete passengers works.
May 5, 2012 at 10:46pm May 5, 2012 at 10:46pm UTC
with a queue, if i remember correctly it doesn't always start from index [1]..
you should have 2 indices which point to the front and the back of the queue.
so you would start from the front of the queue theQueue[front] and increment it till front <= back;
May 6, 2012 at 3:08pm May 6, 2012 at 3:08pm UTC
1 2 3 4 5 6 7
void showPassengers(CQueue *theQueue)
{
for (int x=1;x<=2;x++)
{
cout<<theQueue[1].Front().name;
}
}
WHAT THE???
Maybe would have been useful if you used x anythere, but this is pure lazyness.
cout<<theQueue[1].Front().name;
You print the same thing 2 times! What's the point of that??
Topic archived. No new replies allowed.