C语言实现队列链式存储

#include<stdio.h>
#include<stdlib.h>
typedef struct QNode *P_QNode;
struct QNode
{
    int Data;
    P_QNode Next;
};
struct MyQueue
{
    P_QNode Front,Rear;

};
typedef struct MyQueue *P_MyQueue;
P_MyQueue CreateQueue()
{
    P_MyQueue q=(P_MyQueue)malloc(sizeof(struct MyQueue));
    q->Front=q->Rear=NULL;
    return q;

}
int IsEmpty(P_MyQueue q)
{
    return(q->Front==NULL);
}
void Add(P_MyQueue q,int elem)
{
    P_QNode n;
    n=(P_QNode)malloc(sizeof(struct QNode));
    if(IsEmpty(q))
    {
        n->Data=elem;
        n->Next=NULL;
        q->Front=n;
        q->Rear=n;
    }
    else
    {
        q->Rear->Next=n;
        n->Data=elem;
        n->Next=NULL;
        q->Rear=q->Rear->Next;
    }


}
int Dele(P_MyQueue q)
{
    if(IsEmpty(q))
    {
        printf("队列空");
        return NULL;
    }
    else
    {
        P_QNode p;
        p=q->Front;
        int elem;
        elem=q->Front->Data;
        q->Front=q->Front->Next;
        free(p);
        return elem;

    }
}
void PrintAll(P_MyQueue q)
{
    P_QNode p=q->Front;
    while(p)
    {
        printf("%d ",p->Data);
        p=p->Next;
    }

}
int main()
{
    P_MyQueue Q1=CreateQueue();
    while(1)
    {
         printf("输入需要的操作:\nA.显示队列状态\nB.入队\nC.出队\nD.退出\n");
         char c;
         scanf("%c",&c);
         if(c=='A')
         {
             printf("队列状态:");
             PrintAll(Q1);
             printf("\n");
         }
         else if(c=='B')
         {
             printf("输入:");
             int a;
             scanf("%d",&a);
             Add(Q1,a);

         }
         else if(c=='C')
         {
             int a=Dele(Q1);
             printf("输出:%d",a);
             printf("\n");
         }
         else if(c=='D')
         {
             break;
         }
         else
         {
             printf("无效输入\n");
         }
         char m=getchar();

    }


    return 0;

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值