Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Problem with st...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 1 of 2 Topic 45792 of 45898
Post > Topic >>

Problem with structure and dynamic array

by curiousEngine <curious.engine@[EMAIL PROTECTED] > May 7, 2008 at 02:57 AM

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[i].id;
		cout<<elements[i].first_name;
		cout<<elements[i].last_name;
		cout<<elements[i].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;
}




 2 Posts in Topic:
Problem with structure and dynamic array
curiousEngine <curious  2008-05-07 02:57:31 
Re: Problem with structure and dynamic array
Lionel B <me@[EMAIL PR  2008-05-07 10:58:33 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed May 14 6:37:25 CDT 2008.