#include<iostream>
typedef int ElemType;
using namespace std;
struct Node {
ElemType data;
Node* next;
};
class Queue {
public:
Queue();
bool QueueEmpty() const;
bool EnQueue(ElemType e);
bool DeQueue(ElemType& e);
bool GetHead(ElemType& e);
void QueueLength();
private:
int count;
Node* front;
Node* rear;
};
Queue::Queue()
{
front = new Node;
rear = front;
front->next = NULL;
count=0;
}
bool Queue::QueueEmpty() const
{
return front == rear;
}
bool Queue::EnQueue(ElemType e)
{
Node* s = new Node;
if (NULL == s)
return false;
s->data = e;
s->next = NULL;
rear->next = s;
rear = s;
count++;
return true;
}
bool Queue::DeQueue(ElemType& e)
{
if (QueueEmpty()) return false;
Node* p = front->next;
e = p->data;
front->next = p->next;
delete p;
count--;
p = NULL;
if (front->next == NULL)
rear = front;
return true;
}
bool Queue::GetHead(ElemType& e)
{
if (QueueEmpty())
return false;
e = front->next->data;
return true;
}
void Queue::QueueLength()
{
cout << " QueueLength= " << count << endl;
}
int main()
{
Queue Q;
for (int i = 1; i < 9; i++)
{
Q.EnQueue(i);
}
Q.QueueLength();
ElemType e;
Q.DeQueue(e);
cout << "e=" << e << endl;
Q.DeQueue(e);
cout << "e=" << e << endl;
Q.DeQueue(e);
cout << "e=" << e << endl;
Q.QueueLength();
for (int i = 1; i < 6; i++)
{
Q.EnQueue(7 + i);
}
Q.QueueLength();
cout << "----------------" << endl;
bool ret;
ret=Q.DeQueue(e);
if (ret)
cout << "e=" << e << endl;
else
cout << "fail" << endl;
ret = Q.DeQueue(e);
if (ret)
cout << "e=" << e << endl;
else
cout << "fail" << endl;
ret = Q.GetHead(e);
if (ret)
cout << "e=" << e << endl;
else
cout << "fail" << endl;
Q.DeQueue(e);
cout << "e=" << e << endl;
Q.DeQueue(e);
cout << "e=" << e << endl;
Q.GetHead(e);
cout << "e=" << e << endl;
Q.QueueLength();
return 0;
}
#include<iostream>
using namespace std;
typedef int ElemType;
struct Node
{
ElemType data;
Node* next;
};
class Queue {
public:
Queue();
bool QueueEmpty() const;
bool EnQueue(ElemType e);
bool DeQueue(ElemType& e);
bool GetHead(ElemType& e);
void QueueLength();
private:
Node* front, * rear;
int count;
};
Queue::Queue()
{
count = 0;
front = NULL;
rear = NULL;
}
bool Queue::QueueEmpty() const
{
return (front == NULL);
}
bool Queue::EnQueue(ElemType e)
{
Node* s = new Node;
s->data = e;
s->next = NULL;
if (front == NULL)
{
rear = s;
front = rear;
}
else
{
rear->next = s;
rear = s;
}
count++;
return true;
}
bool Queue::DeQueue(ElemType& e)
{
if (QueueEmpty()) return false;
Node* p = front;
e = p->data;
count--;
front = p->next;
if (p == rear)
{
rear = NULL;
front = NULL;
}
delete p;
p = NULL;
return true;
}
bool Queue::GetHead(ElemType& e)
{
if (QueueEmpty())
return false;
e = front->data;
return true;
}
void Queue::QueueLength()
{
cout << "Length=" << count << endl;
}
int main()
{
Queue Q;
for (int i = 1; i < 9; i++)
{
Q.EnQueue(i);
}
Q.QueueLength();
ElemType e;
Q.DeQueue(e);
cout << "e=" << e << endl;
Q.DeQueue(e);
cout << "e=" << e << endl;
Q.DeQueue(e);
cout << "e=" << e << endl;
Q.QueueLength();
for (int i = 1; i < 6; i++)
{
Q.DeQueue(e);
}
Q.QueueLength();
cout << "----------------" << endl;
bool ret;
ret = Q.DeQueue(e);
if (ret)
cout << "e=" << e << endl;
else
cout << "fail" << endl;
ret = Q.DeQueue(e);
if (ret)
cout << "e=" << e << endl;
else
cout << "fail" << endl;
ret = Q.GetHead(e);
if (ret)
cout << "e=" << e << endl;
else
cout << "fail" << endl;
Q.EnQueue(99);
Q.DeQueue(e);
cout << "e=" << e << endl;
ret = Q.GetHead(e);
if (ret)
cout << "e=" << e << endl;
else
cout << "fail" << endl;
Q.QueueLength();
return 0;
}