Description
建立队列的顺序存储结构,进行入队和出队的操作,并输出队列中的数据元素。
Input
多组输入,每组输入为两行,第一行输入一个整数,为输入数据的个数,第二行输入要输入的数据(整数).
Output
每行输出是队列里面的数据元素。
Sample Input
5
1 2 3 4 5
3
12 3 90
Sample Output
1 2 3 4 5
12 3 90
//队列的输入输出操作
#include <bits/stdc++.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
//#define OVERFLOW 0
typedef int Status;
typedef int Boolean;
typedef int ElemType;
using namespace std;
typedef struct QNode
{
ElemType data;
struct QNode *next;
}QNode,*QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
Status InitQueue(LinkQueue &Q)
{
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
Q.front->next=NULL;
return OK;
}
Status DestoryQueue(LinkQueue &Q)
{
while (Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
return OK;
}
Status ClearQueue(LinkQueue &Q)
{
while(Q.front)
{
Q.front=NULL;
Q.rear=Q.front->next;
Q.front=Q.rear;
}
return OK;
}
int QueueLength(LinkQueue &Q)
{
int len=0;
while(Q.front)
{
len++;
Q.rear=Q.front->next;
Q.front=Q.rear;
}
return len;
}
Status QueueEmpty(LinkQueue &Q)
{
if(Q.front==Q.rear)
return OK;
return FALSE;
}
int GetHead(LinkQueue &Q)
{
return Q.front->data;
}
Status EnQueue(LinkQueue &Q,ElemType e)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode));
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
return OK;
}
Status DeQueue(LinkQueue &Q,ElemType &e)
{
if(Q.front==Q.rear)
return ERROR;
QueuePtr p;
p=Q.front->next;
e=p->data;
Q.front->next=p->next;
if(Q.rear==p)
Q.rear=Q.front;
free(p);
return OK;
}
void showQueue(LinkQueue &Q)
{
QNode *p;
Q.front=Q.front->next;
p=Q.front;
cout<<p->data;
Q.front=Q.front->next;
while(Q.front)
{
p=Q.front;
cout<<" "<<p->data;
Q.front=Q.front->next;
}
cout<<endl;
}
int main()
{
int n,temp;
LinkQueue Q;
InitQueue(Q);
while(cin>>n)
{
for(int i=1;i<=n;++i)
{
cin>>temp;
EnQueue(Q,temp);
}
showQueue(Q);
DestoryQueue(Q);
InitQueue(Q);
//showQueue(Q);
}
}