queue.h
#ifndef QUEUE_H_
#define QUEUE_H_
#include<iostream>
template<class T>
struct Node
{
T data;
Node<T>* next;
};
template<class T>
class Queue
{
private:
Node<T>* front;
Node<T>* rear;
public:
Queue();
~Queue();
void EnQueue(T x);
void Delete();
bool Empty();
T GetFront();
void Print();
};
template<class T>
Queue<T>::Queue()
{
front = rear = new Node<T>;
front->next = nullptr;
}
template<class T>
Queue<T>::~Queue()
{
while (front)
{
rear=front->next;
delete front;
front = rear;
}
delete rear;
}
template<class T>
void Queue<T>::EnQueue(T x)
{
Node<T>* p = new Node<T>;
p->data = x;
rear->next=p;
rear = p;
rear->next = nullptr;
}
template<class T>
void Queue<T>::Delete()
{
Node<T>* p = front->next;
front->next = p->next;
delete p;
}
template<class T>
bool Queue<T>::Empty()
{
return front == rear ? true : false;
}
template<class T>
T Queue<T>::GetFront()
{
return front->next->data;
}
template<class T>
void Queue<T>::Print()
{
Node<T>* p = front->next;
while (p)
{
std::cout << p->data<<" ";
p = p->next;
}
std::cout << std::endl;
}
#endif // !QUEUE_H_
UseQueue.cpp
#include<iostream>
#include"queue.h"
int main()
{
using std::cout;
using std::endl;
Queue<int> p;
for (int i = 1; i <= 10; i++)
p.EnQueue(i);
if (p.Empty())
{
cout << "Empty\n";
}
else
{
p.Print();
cout<<p.GetFront()<<endl;
p.Delete();
p.Print();
}
return 0;
}