Problem with structure and dynamic array

C

curiousEngine

how to make provision of a dynamic array say of size 5 with each slot
holding a structure of type passenger?

/*
* Labsheet Queues
Question 1
Implement a program that shows a queue of people lining at a bus-stop.
The first one in the queue is the first one to get on the bus.
Your program should display the following data :
Id
First Name
Last Name
DestinationAddress
The id is to be incremented automatically. By default, it can be set
to 0.
*
* IMPLEMENTATION USING AN ARRAY
*/


#include <iostream>
using namespace std;

struct passenger{
int id;
char first_name[15];
char last_name[30];
char dest_add[20];
};

typedef passenger QueueElementType;

class Queue{
private:
QueueElementType * elements;
int MaxQueue;
int Front;
int Rear;
int fullqueue();
int emptyqueue();
public:
Queue(int MS);
void Enq(QueueElementType apassenger);
QueueElementType Deq();
void printQueue();
~Queue();
};

Queue::Queue(int MS){
elements = new QueueElementType;
MaxQueue = MS;
Front = MaxQueue -1;
Rear = MaxQueue -1;
}

Queue::~Queue(){
delete [] elements;
MaxQueue = 0;
Front = MaxQueue -1;
Rear = MaxQueue -1;
}

int Queue::fullqueue(){
return(Front = (Rear + 1) % MaxQueue);
}

int Queue::emptyqueue(){
return (Front == Rear);}

void Queue::Enq(QueueElementType apassenger){
if (fullqueue())
cout<<"Queue is Full";
else{
Rear = (Rear +1) % MaxQueue;
cout<<"Enter passenger first name:";
cin>> apassenger.first_name;
cout<<endl<<"Enter passenger last name:";
cin>>apassenger.last_name;
cout<<endl<<"Enter destination address:";
cin>>apassenger.dest_add;
apassenger.id = 1;
elements[Rear] = apassenger;
}
}

QueueElementType Queue::Deq(){
QueueElementType mypass;
if (emptyqueue())
cout<<"Queue is empty";
else{
Front = (Front + 1) % MaxQueue;
mypass = elements[Front];
}
return(mypass);
}

void Queue::printQueue(){
for(int i = Front ; i<= Rear; i++){
cout<<elements.id;
cout<<elements.first_name;
cout<<elements.last_name;
cout<<elements.dest_add;
}
}


int main(int argc, char** argv)
{
Queue myqueue(5);
QueueElementType mypass;
myqueue.Enq(mypass);
myqueue.printQueue();

myqueue.Enq(mypass);
myqueue.printQueue();
return 0;
}
 
L

Lionel B

how to make provision of a dynamic array say of size 5 with each slot
holding a structure of type passenger?

/*
* Labsheet Queues
Question 1
Implement a program that shows a queue of people lining at a bus-stop.
The first one in the queue is the first one to get on the bus. Your
program should display the following data : Id
First Name
Last Name
DestinationAddress
The id is to be incremented automatically. By default, it can be set to
0.

Does your homework assignment specifically state that you aren't allowed
to use std::queue? Otherwise, you're re-inventing a (*very* wonky) wheel.

[...]
Queue::Queue(int MS){
elements = new QueueElementType;

Here you allocate *one* element to 'elements'. I think you probably meant:

elements = new QueueElementType[MS];
Queue::~Queue(){
delete [] elements;

Here you delete an *array* of elements. This is not going to work.
void Queue::Enq(QueueElementType apassenger){
if (fullqueue())
cout<<"Queue is Full";
else{
Rear = (Rear +1) % MaxQueue;
cout<<"Enter passenger first name:";
cin>> apassenger.first_name;
cout<<endl<<"Enter passenger last name:"; cin>>apassenger.last_name;
cout<<endl<<"Enter destination address:"; cin>>apassenger.dest_add;
apassenger.id = 1;
elements[Rear] = apassenger; <------

Since you don't allocate an array of elements this is going to access an
unallocated element. Ditto many other places in your code.

This is a really bad attempt at implementing a queue. Either use
std::queue (*way* easier) or, if you have to implement it yourself, read
a good book on data structures (and C++) first - it's tricky.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,565
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top