Question regarding Enqueue, Dequeue

Hi Can anyone help me with my enqueue and dequeue, I am supposed to read from a file and insert into a listlist to do an enqueue and dequeue
The text file sample goes like this,

I cant use template as i wasnt taught yet...

M Mevin
F Mary
etc...

It is a FIFO priority queue so if encounter a lady it will be the first of the link list and if a male at the last of link list

I am stuck at the enqueue and dequeue part and i dont know what to do. Below are my code.

And can anyone teach me how to implement this function,
friend ostream& operator<< (ostream&, Queue&);

And also is my assignment overloading correct??

Thanks all for the help..am really dying...and also pardon for my english..

#include <iostream>
#include <fstream>

using namespace std;

const int MAX = 20;

class Person
{
friend ostream& operator<< (ostream&, Person&);

public:
Person(); // do nothing
Person(char [], char);
Person(const Person&);

char getSex() const;
Person& operator= (const Person&);

private:
char name[MAX];
char sex;


};


class Queue
{
friend ostream& operator<< (ostream&, Queue&);

public:
Queue();

void enqueue (const Person&);
Person dequeue();

int getSize ();
bool isEmpty ();

private:
struct Node;
typedef Node* NodePtr;

struct Node
{
Person p;
NodePtr next, prev;

};

NodePtr head, tail;
static int size;

};
char Person:: getSex() const
{
return this -> sex;


}


Person:: Person(char name[], char gender)
{
int i = 0;

while(name[i] != '\0')
{
this->name[i] = name[i];
++i;
}

this -> name[i] = '\0';

this -> sex = gender;

}

Person:: Person(const Person& person)
{
this -> name[MAX] = person.name[MAX];
this -> sex = person.sex;


}

Person:: Person()
{

}

Person &Person:: operator= (const Person &person)
{
if(this == &person)
return *this;

name[MAX] = person.name[MAX];
sex = person.sex;

return *this;

}


ostream& operator<< (ostream &os, Person &person)
{
int i = 0;

while(person.name[i] != '\0')
{
os << person.name[i];
++i;
}

return os;


}

ostream& operator<< (ostream &os, Queue &queue)
{
// os << queue.head;


return os;

}

int Queue :: size = 0;

int Queue:: getSize ()
{
return size;

}


Person Queue:: dequeue()
{
// for(int i = 0; i < size; i++)
// {
//delete ;

// }


}

Queue::Queue()
{
head = tail = NULL;

}



void Queue:: enqueue (const Person& person)
{
NodePtr temp = new Node;
temp -> p = person;
temp -> next = NULL;

if (tail == NULL)
head = temp;
else
tail -> next = temp;

tail = temp;


}


bool Queue:: isEmpty()
{
return (head == NULL && tail == NULL);

}



int main()
{
fstream afile;
char filename[MAX];
bool check;

Queue queue;
Person person;

cout << "Testing of empty queue..." << endl;

if(queue.isEmpty() == true)
cout << "Queue is empty" << endl << endl;
else
cout << "Queue is not empty" << endl << endl;

cout << "Enter filename: ";
cin >> filename;
cin.clear();

cout << "------------------------------------------" << endl;

afile.open(filename, ios::in);

char gender;
char name[MAX];

if(!afile)
{
cout << "Open failed" << endl;
exit(-1);

}

else
{
while(afile >> gender >> name)
{
Person person(name, gender);
queue.enqueue(person);

if(person.getSex() == 'M')
cout << "Gentleman " << person << " inserted into queue" << endl;
else
cout << "Lady " << person << " inserted into queue" << endl;

}

cout << "------------------------------------------" << endl;

}

cout << "Printing of queue as an object" << endl;
cout << "------------------------------------------" << endl;
cout << "Testing of dequeue operation" << endl;
cout << "------------------------------------------" << endl;



}

Last edited on
Topic archived. No new replies allowed.