by Lionel B <me@[EMAIL PROTECTED]
>
May 7, 2008 at 10:58 AM
On Wed, 07 May 2008 02:57:31 -0700, curiousEngine wrote:
> 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.
--
Lionel B