#include <iostream>
using namespace std;
template <class ItemType>
struct Node {
ItemType data;
Node<ItemType> *next;
};
template <class List_ItemType>
class Queue {
public:
Queue();
~Queue();
void Enqueue(List_ItemType dataIn);
int Dequeue(List_ItemType &dataOut);
int GetQueueFront(List_ItemType &dataOut);
int GetQueueRear(List_ItemType &dataOut);
void Clear();
int IsEmpty();
int GetSize();
void Print2Console();
private:
Node<List_ItemType> *front, *rear;
int count;
};
template <class List_ItemType>
Queue<List_ItemType>::Queue(){
this->count = 0;
this->front = NULL;
this->rear = NULL;
}
template <class List_ItemType>
Queue<List_ItemType>::~Queue(){
this->Clear();
}
template <class List_ItemType>
void Queue<List_ItemType>::Enqueue
(List_ItemType value){
Node<List_ItemType>* newPtr = new
Node<List_ItemType>();
newPtr->data = value;
newPtr->next = NULL;
if (this->count == 0)
this->front = newPtr;
else
this->rear->next = newPtr;
this->rear = newPtr;
this->count++;
}
template <class List_ItemType>
int Queue<List_ItemType>::Dequeue(
List_ItemType &dataOut){
if (count == 0)
return 0;
dataOut = front->data;
Node<List_ItemType>* dltPtr= this->front;
if (count == 1)
this->rear = NULL;
this->front = this->front->next;
this->count--;
delete dltPtr;
return 1;
}
template <class List_ItemType>
int Queue<List_ItemType>::GetQueueFront
(List_ItemType &dataOut){
if (count == 0)
return 0;
dataOut = this->front->data;
return 1;
}
template <class List_ItemType>
int Queue<List_ItemType>::GetQueueRear
(List_ItemType &dataOut){
if (count == 0)
return 0;
dataOut = this->rear->data;
return 1;
}
template <class List_ItemType>
void Queue<List_ItemType>::Clear() {
Node<List_ItemType>* temp;
while (this->front != NULL){
temp = this->front;
this->front= this->front->next;
delete temp;
}
this->front = NULL;
this->rear = NULL;
this->count = 0;
}
template <class List_ItemType>
int Queue<List_ItemType>::IsEmpty() {
return (this->count == 0);
}
template <class List_ItemType>
int Queue<List_ItemType>::GetSize() {
return this->count;
}
template <class List_ItemType>
void Queue<List_ItemType>::Print2Console(){
Node<List_ItemType>* p;
p = this->front;
cout << "Front: ";
while (p != NULL){
cout << p->data << " ";
p = p->next;
}
cout << endl;
}
int main()
{
cout << "Queue:" << endl;
Queue<int> *myQueue = new Queue<int>();
int val;
myQueue->Enqueue(7);
myQueue->Enqueue(9);
myQueue->Enqueue(10);
myQueue->Enqueue(8);
myQueue->Print2Console();
myQueue->Dequeue(val);
myQueue->Print2Console();
delete myQueue;
return 0;
}
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.