队列c语言实现

利用c语言实现链队列及相关操作

#include <stdio.h>
#include <stdlib.h>
#define ElemType int

typedef struct node
{
    ElemType data;
    struct node * next;
}quenode,*quenlist;

struct quefr
{
    quenode *front, *rear;
};

void creat(struct quefr *q)
{
    quenode *h;
    h = (quenlist)malloc(sizeof(quenode));
    h->next = NULL;
    q->front = h;
    q->rear = h;
};

void enqueue(struct quefr *q, ElemType x)  /* 自定义函数,元素x入队 */
{
    quenode *s;
    s = (quenode*)malloc(sizeof(quenode));
    s->data = x;
    s->next = NULL;
    q->rear->next = s;
    q->rear = s; /*队尾指针*/
}

ElemType dequenue(struct quefr *q) /* 自定义函数实现元素出队 */
{
    quenode *p;
    ElemType x;
    p = (quenode*)malloc(sizeof(quenode));

    if(q->front == q->rear)
    {
        printf("Quenue is NULL.\n");
        exit(1);
    }

    else
    {
        p=q->front->next;
        q->front->next = p->next;
        if(p->next == NULL)
            q->front = q->rear;
        x = p->data;
        free(p);
    }
    return x;
}

void display(struct quefr* dp)
{
    quenode* p;
    if(dp->front == dp->rear)
    {
        printf("queue is NULL!\n");
        exit(1);
    }
    p = dp->front->next; /* front is a null head node */
    while(p != NULL)
    {
        printf("data = %d\n", p->data);
        p = p->next;
    }printf("end!");
}

int main()
{
    struct quefr *que;
    int n,i,x,sel;
    void display();
    void creat();
    void enqueue();
    ElemType dequenue();

    do
    {
        printf("\n\n");
        printf("1 create queue       \n");
        printf("2 enqueue            \n");
        printf("3 dequeue            \n");
        printf("4 display            \n");
        printf("5 exit               \n");
        printf("-----------------------\n");
        printf("please chose (1 2 3 4 5):\n");
        scanf("%d",&sel);
        switch(sel)
        {
        case 1:
            {
                que = (struct quefr*)malloc(sizeof(struct quefr)); /*storage allocation*/
                creat(que);
                printf("please input the number of element which do you want to creat:\n");
                scanf("%d",&n);
                for(int i=0; i<n; i++)
                {
                    scanf("%d", &x); /*输入元素*/
                    enqueue(que,x);
                }
                break;
            }
        case 2:
            {
                printf("please input the element:\n");
                scanf("%d", &x);
                enqueue(que,x);
                break;
            }
        case 3:
            {
                printf("x = %d\n", dequenue(que));
                break;
            }
        case 4:
            {
                display(que);
                break;
            }
        case 5:
            exit(0);
        }
    }while(sel <= 4);
    return 0;
}

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值