链队列的出队,需要注意一下,这里我在运行时遇到一些问题。
运行结果
源码
#include <iostream>
using namespace std;
typedef struct QNode
{
int data;
QNode* next;
}Node, *Pointer;
typedef struct Queue
{
Pointer front;
Pointer tail;
}Que;
void Init(Que& Q)
{
Node* head = new Node;
Q.front = Q.tail = head;
Q.front->next = NULL;
}
bool EnQueue(Que& Q, int e)
{
Node* p = new Node;
p->data = e;
Q.tail->next = p;
Q.tail = p;
return true;
}
bool OutQueue(Que& Q, int& e)
{
if (Q.front != Q.tail)
{
Node* q = Q.front->next;
e = q->data;
Q.front->next = q->next;
if (Q.tail == q)
Q.front = Q.tail;
delete q;
return true;
}
return false;
}
int GetFront(Que& Q)
{
if(Q.front != Q.tail)
return Q.front->next->data;
}
int main()
{
Que Q;
Init(Q);
int code;
int e;
do
{
printf("请输入操作码 0-退出系统 1-入队 2-出队 3-取队头元素\n");
cin >> code;
switch (code)
{
case 1:
cout << "请输入入队元素 输入-1退出操作\n";
cin >> e;
while (e != -1)
{
EnQueue(Q, e);
cout << "元素" << e << "入队\n";
cin >> e;
}
break;
case 2:
if (!OutQueue(Q, e))
{
cout << "队为空\n";
break;
}
cout << "元素" << e << "出队\n";
break;
case 3:
cout << "队头元素为" << GetFront(Q) << "\n";
break;
}
} while (code != 0);
return 0;
}
|