的语言数据结构循环队列

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LinkNode
{
    ElemType data;
    struct LinkNode* next;
}LNode;
typedef struct {
    struct LinkNode* front,* rear;
}LQueue;
void InitQueue(LQueue& Q)
{
    Q.front  = Q.rear = (LinkNode*)malloc(sizeof(LinkNode));
    Q.rear->next = NULL;
}
bool IsEmpty(LQueue Q)
{
    if (Q.front == Q.rear)
    {
        return true;
    }
    return false;
}
void InQueue(LQueue& Q,ElemType e)
{
    
    LinkNode* L = (LinkNode*)malloc(sizeof(LinkNode));
    L->data = e;
    L->next = NULL;
    Q.rear->next = L;
    Q.rear = L;
}
void CreatQueue(LQueue& Q, int n)
{
    int x;
    printf("请输入你要入队的元素:\n");
    for (int i = 0; i < n; i++)
    {
        scanf_s("%d", &x);
        InQueue(Q, x);
    }
}
bool OutQueue(LQueue& Q,ElemType&e)
{
    if (Q.rear == Q.front)
    {
        return false;
    }
    LNode* s;
    s = Q.front->next;
    e = s->data;
    Q.front->next = s->next;
    if (s == Q.rear)
    {
        Q.rear = Q.front;
    }
    free(s);
    return true;
}
void PrintfQueue(LQueue Q)
{
    LNode*P = Q.front->next;
    while (P!=NULL)
    {
        printf("%4d", P->data);
        P = P->next;
    }
    printf("\n");
}
int main()
{
    int e;
    int n;
    bool ret;
    LQueue Q;
    InitQueue(Q);
    printf("你要入队几个元素?\n");
    scanf_s("%d", &n);
    CreatQueue(Q, n);
    PrintfQueue(Q);
    OutQueue(Q, e);
    printf("出队的元素是%d\n", e);
    PrintfQueue(Q);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值