1 #include <iostream>
2 #define QUEUEELEMENTTYPE int
3 using namespace std;
4
5 /*结点*/
6 typedef struct Node
7 {
8 QUEUEELEMENTTYPE data; /*数据域*/
9 Node * next; /*指针域*/
10 }LinkQueueNode;
11
12 /*队列*/
13 struct LinkQueue
14 {
15 LinkQueueNode * front; /*头指针*/
16 LinkQueueNode * rear; /*尾指针*/
17 };
18
19 /*将Q初始化为一个空的链队列*/
20 int InitQueue(LinkQueue * Q)
21 {
22 Q->front=(LinkQueueNode * )malloc(sizeof(LinkQueueNode));
23 if(Q->front!=0)
24 {
25 Q->rear = Q->front;
26 Q->front->next=0;
27 return true;
28 }
29 else
30 return false;
31 }
32
33 /*判断队列Q是否为空*/
34 int IsEmpty(LinkQueue * Q)
35 {
36 if(Q->front==Q->rear)
37 return true;
38 return false;
39 }
40
41 /*将数据元素x插入到队列中去*/
42 int EnterQueue(LinkQueue * Q , QUEUEELEMENTTYPE x)
43 {
44 Node * temp = (Node * )malloc(sizeof(Node));
45 temp->next = 0;
46 temp->data = x;
47 Q->rear->next = temp;
48 Q->rear = temp;
49 return true;
50 }
51
52 /*将队列Q的队头元素出队,并存到x所指的存储空间中*/
53 int DeleteQueue(LinkQueue * Q , QUEUEELEMENTTYPE &x)
54 {
55 if(Q->rear!=Q->front)
56 {
57 x = Q->front->next->data;
58 Q->front=Q->front->next;
59 return true;
60 }
61 return false;
62 }
63
64 /*获取队头的元素,并存到x所指的存储空间中*/
65 int GetHead(LinkQueue * Q , QUEUEELEMENTTYPE &x)
66 {
67 if(Q->rear==Q->front)
68 return false;
69 else
70 x = Q->front->next->data;
71 return true;
72 }
73
74 /*将队列Q置为空*/
75 void ClearQueue(LinkQueue * Q)
76 {
77 Q->front=Q->rear;
78 Q->front->next=0;
79 }
80
81 int main()
82 {
83 LinkQueue * queue=new LinkQueue;
84 InitQueue(queue);
85 cout << IsEmpty(queue) << endl;
86 for(int i=0;i<1;i++)
87 EnterQueue(queue,i);
88 cout << IsEmpty(queue) << endl;
89 int x = -1;
90 DeleteQueue(queue,x);
91 cout << "x = " << x << endl;
92 DeleteQueue(queue,x);
93 int y = -1;
94 GetHead(queue,x);
95 cout << "y = " << y << endl;
96 cout << IsEmpty(queue) << endl;
97 ClearQueue(queue);
98 cout << IsEmpty(queue) << endl;
99 return 0;
100 }