队列的链式存储的实现及操作

本文介绍了一种使用链表实现的队列——链队列,并提供了队列的基本操作,如初始化、入队、出队等。通过具体代码展示了链队列的工作原理及其在实际应用中的使用方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

队列的链式存储的实现及操作

用链表表示的队列简称为链队列, 空的链队列的队头和队尾指针均指向头结点

#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
    int data;
    struct Node *next;
}LinkQNode;
typedef struct
{
    LinkQNode *front;
    LinkQNode *rear;
}LinkQueue;
void InitLinkQueue(LinkQueue *Q);
int IsLQEmpty(LinkQueue *Q);
int EnLinkQueue(LinkQueue *Q, int x);
int DeLinkQueue(LinkQueue *Q, int *x);
int GetLQHead(LinkQueue *Q, int *x);
void print_hyphen(int n);
int main(void)
{
    LinkQueue LQ;
    InitLinkQueue(&LQ);
    if(LQ.front)
        printf("队列初始化成功!\n");
    else{
        printf("队列初始化失败!\n");
        exit(1);
    }
    int end = 0;
    int ope;
    int n;
    while(!end){
        print_hyphen(15); printf("\n");
        printf("请输入指令来执行操作\n");
        print_hyphen(15); printf("\n");
        printf("1、入队\n2、出队\n3、取队头元素\n4、判断队列是否为空\n5、退出\n");
        print_hyphen(15); printf("\n");
        printf("输入要使用的功能的序号: ");
        scanf("%d", &ope);
        getchar();
        switch(ope){
            case 1:
                printf("请输入要入队的数据: ");
                scanf("%d", &n);
                EnLinkQueue(&LQ, n);
                break;

            case 2:
                if(!DeLinkQueue(&LQ, &n))
                    printf("队列为空!\n");
                else
                    printf("出队的元素为: %d\n", n);
                break;

            case 3:
                if(!GetLQHead(&LQ, &n))
                    printf("队列为空!\n");
                else
                    printf("出队的元素为: %d\n", n);
                break;

            case 4:
                if(IsLQEmpty(&LQ))
                    printf("队列为空!\n");
                else
                    printf("队列不为空! \n");
                break;

            case 5:
                printf("再见!\n");
                end = 1;
                break;

            default:
                printf("无此序号,请重新输入!\n");
        }
    }
    return 0;
}
void InitLinkQueue(LinkQueue *Q)
{
    Q->front = (LinkQNode*)malloc(sizeof(LinkQNode));
    Q->rear = Q->front;
    Q->front->next = NULL;
}
int IsLQEmpty(LinkQueue *Q)
{
    if(Q->front == Q->rear)
        return 1;
    else
        return 0;
}
int EnLinkQueue(LinkQueue *Q, int x)
{
    LinkQNode *NewNode;
    NewNode = (LinkQNode*)malloc(sizeof(LinkQNode));
    if(NewNode != NULL){
        NewNode->data = x;
        NewNode->next = NULL;
        Q->rear->next = NewNode;
        Q->rear = NewNode;
        return 1;
    }
    else
        return 0;
}
int DeLinkQueue(LinkQueue *Q, int *x)
{
    LinkQNode *p;
    if(Q->front == Q->rear)
        return 0;
    p = Q->front->next;
    Q->front->next = p->next;
    if(Q->rear == p)
        Q->rear = Q->front;
    *x = p->data;
    free(p);
    return 1;
}
int GetLQHead(LinkQueue *Q, int *x)
{
    LinkQNode *p;
    if(Q->front == Q->rear)
        return 0;
    *x = Q->front->next->data;
    return 1;
}
void print_hyphen(int n)
{
    while(n--)
        printf("-");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值