• 链队列的实现(上课用)


    源程序:

    #include <stdio.h>
    #include <stdlib.h>
    typedef int DataType;
    typedef struct LinkQueueNode
    {
    DataType data;
    struct LinkQueueNode *next;
    }LkQueNode;

    typedef struct LkQueue
    {
    LkQueNode *front,*rear;
    }LkQue;

    //LkQue LQ;

    //初始化队列
    void InitQueue(LkQue *LQ)
    {
    LkQueNode *temp;
    temp=(LkQueNode *)malloc(sizeof(LkQueNode));
    LQ->front=temp;
    LQ->rear=temp;
    (LQ->front)->next=NULL;
    }

    //判断队列为空
    int EmptyQueue(LkQue *LQ)
    {
    if(LQ->rear==LQ->front)
    return 1;
    else
    return 0;
    }

    //入队列
    void EnQueue(LkQue *LQ,DataType x)
    {
    LkQueNode *temp;
    temp=(LkQueNode *)malloc(sizeof(LkQueNode));
    temp->data=x;
    temp->next=NULL;
    (LQ->rear)->next=temp;
    LQ->rear=temp;
    }

    //出队列
    int OutQueue(LkQue *LQ)
    {
    LkQueNode *temp;
    if(EmptyQueue(LQ))
    {
    printf("队列为空");
    return 0;
    }
    else
    {
    temp=(LQ->front)->next;
    (LQ->front)->next=temp->next;
    if(temp->next==NULL)
    {
    LQ->rear=LQ->front;
    free(temp);
    return 1;
    }
    }
    return 0;
    }

    //取队列首元素
    DataType GetHead(LkQue *LQ)
    {
    LkQueNode *temp;
    if(EmptyQueue(LQ))
    return 0;
    else
    {
    temp=LQ->front->next;
    return temp->data;
    }
    }

    //主函数

    int main()
    {
    LkQue lq;
    int i, n;
    InitQueue(&lq);
    printf("请输入5个整数,这5个整数依次入队列:\n");
    for (i = 0; i < 5; i++)
    {
    scanf("%d", &n);
    EnQueue(&lq, n);
    }
    printf("\n出队顺序:\n");
    for (i = 0; i < 5; i++)
    {
    if (!EmptyQueue(&lq))
    {
    n = GetHead(&lq);
    OutQueue(&lq);
    printf("第%d个出队的数为:%d\n", i+1,n);
    }
    else
    printf("\n队列为空!\n");
    }
    return 1;
    }

    运行结果 :

  • 相关阅读:
    LVS Nginx和HAproxy的区别,怎么选择最好
    PXE+kickstart自动化安装
    DHCP服务搭建
    自动化安装
    Zabbix trigger(触发器)设置
    Zabbix Agent 安装指南和 Zabbix Server 设置自动发现
    Zabbix Server安装指南
    MariaDB安装
    事件绑定
    事件驱动式
  • 原文地址:https://www.cnblogs.com/duanqibo/p/16391685.html
Copyright © 2020-2023  润新知