output as:
//queue.h
class Queue;
class Node{
int data;
Node* next;
Node(int data , Node* next = (Node*)0);
friend class Queue;
};
class Queue{
Node* head;
Node* tail;
public:
Queue(); // empty list
void add(int data); // adds to tail of the list
int remove(); // removes from head
bool IsEmpty();// true if empty
~Queue();
};
//queue.cpp
#include "queue.h"
Node::Node(int data , Node* next){
this->data = data;
this->next = next;
}
Queue::Queue(){
head = (Node*)0;
tail = (Node*)0;
}
void Queue::add(int data){
if (head==tail && !head){
Node* temp = new Node(data, tail);
head = temp;
tail = temp;
}
else{
Node* temp = new Node(data, tail->next);
tail->next = temp;
tail = temp;
}
}
int Queue::remove(){
int data = head->data;
Node* ToDel = head;
head = head->next;
delete ToDel;
return data;
}
bool Queue::IsEmpty(){
return !head;
}
Queue::~Queue(){
while(!IsEmpty()){
remove();
}
}
//queuemain.cpp
#include
using namespace std;
#include "queue.h"
int main(){
Queue Q;
int i;
for(i=10;i<=50;i+=10){
cout<<"Add into a queue "< Q.add(i);
}
while(!Q.IsEmpty()){
cout<}
return 0;
}
Saturday, March 13, 2010
My solution for Queue
Here is my solution for Queue which we are asked to be finished by Fardad in this week class.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment