队列的入队出队,0出队,@打印并结束,其他字符入队
#include<stdio.h>
#include<iostream>
using namespace std;
typedef struct LinkNode//节点的结构体
{
char data;
LinkNode *next;
}LinkNode;
typedef struct LinkQueue//队列的结构体
{
LinkNode *front, *rear;
}LinkQueue;
bool Emptyqueue(LinkQueue q)
{
if (q.front == q.rear)//空返回true
{
return true;
}
else
{
return false;//不空
}
}
void Clearqueue(LinkQueue &q)
{
LinkNode *now;
if (!Emptyqueue(q))
{
while (q.front != q.rear)
{
now = q.front->next;
q.front->next = now->next;
delete(now);
}
}
}
void Enqueue(LinkQueue &q,char c)
{
LinkNode *n = new(LinkNode);
n->data = c;
n -> next = NULL;
if (Emptyqueue(q))//如果队列为空,进行初始化
{
q.front = q.rear = new(LinkNode);
q.front->next = NULL;
}
q.rear->next = n;
q.rear = n;
}
char Dequeue(LinkQueue &q)
{
if (q.front == q.rear)
{
cout << "队列为空" << endl;
return '\0';
}
else
{
LinkNode *now = q.front->next;
char c = now->data;
q.front->next = now->next;
if (q.rear == now)//表示原本只有一个点,并且该点已经剔除
q.front = q.rear = NULL;
delete(now);
return c;
}
}
void printQueue(LinkQueue &q)
{
LinkNode *now;
while (q.front != q.rear)
{
now = q.front->next;
cout << now->data << " ";
q.front->next = now->next;
delete(now);
}
cout << endl;
}
int main()
{
char c;
LinkQueue q;
while (true)
{
cin >> c;
if (c == '0')//出队
{
char now = Dequeue(q);
cout << now << endl;
}
else if (c == '@')
{
printQueue(q);
break;
}
else
{
Enqueue(q,c);
}
}
return 0;
}