队列的输入输出操作

21 篇文章 0 订阅
20 篇文章 2 订阅

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);
    }
}

  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值