隊列-鏈式結構存儲
#include<iostream>
using namespace std;
typedef int elemtype;
typedef struct LinkNode {
elemtype data;
struct LinkNode *next;
}LinkNode;
typedef struct {
LinkNode *front, *rear;
}LinkQueue;
void InitQueue(LinkQueue &q)
{
q.front = q.rear = new LinkNode;
q.front->next = NULL;
}
bool IsEmpty(LinkQueue &q)
{
if (q.front == q.rear)
{
cout << "It is empty !!" << endl;
return true;
}
else {
cout << "Not Empty !" << endl;
return false;
}
}
bool EnQueue(LinkQueue &q, elemtype x)
{
LinkNode *s = new LinkNode;
s->data = x;
s->next = NULL;
q.rear->next = s;
q.rear = s;
return true;
}
elemtype DeQueue(LinkQueue &q)
{
if (q.front == q.rear)
return false;
LinkNode * temp = new LinkNode;
temp = q.front->next;
elemtype x = temp->data;
q.front->next = temp->next;
if (temp == q.rear)
q.rear = q.front;
return x;
}
int main()
{
int aa[5] = { 45,2,3,4,5 };
LinkQueue q;
InitQueue(q);
IsEmpty(q);
for (int j = 0; j < (sizeof(aa) / sizeof(aa[0])); j++)
{
EnQueue(q, aa[j]);
}
IsEmpty(q);
for (int k = 0; k < 5; k++)
{
cout << DeQueue(q) << endl;
}
IsEmpty(q);
system("pause");
}