zoukankan      html  css  js  c++  java
  • 队列的链式存储及其基本运算

    #include <stdio.h>
    #include <stdlib.h>
    #define QueueSize 10
    
    typedef struct Qnode    //链队列的定义
    {
        char data;
        struct Qnode *next;
    }Qtype;                 //链队中节点类型
    typedef struct qptr
    {
        Qtype *front,*rear; //链队类型
    }LinkQueue;
    
    void InitQueue(LinkQueue *&lq)  //初始化队列:置节点*lq的rear和front均为空
    {
        lq=(LinkQueue *)malloc(sizeof(LinkQueue));
        lq->rear=lq->front=NULL;
    }
    
    void EnQueue(LinkQueue *&lq,char x) //进队:创建一个新节点,将其链接到链队末尾,并由rear指向它
    {
        Qtype *s;
        s=(Qtype*)malloc(sizeof(Qtype)); //创建新节点,插入到链队的末尾
        s->data=x;s->next=NULL;
        if(lq->front==NULL&&lq->rear==NULL)
            lq->rear=lq->front=s;
        else
        {
            lq->rear->next=s;
            lq->rear=s;
        }
    }
    
    int DeQueue(LinkQueue *&lq,char x)  //出队:将*front节点的data域值赋给x,并删除该节点
    {
        Qtype *p;
        if(lq->front==NULL&&lq->rear==NULL)
            return 0;
        p=lq->front;
        x=p->data;
        if(lq->rear==lq->front) //若原队列中只有一个节点,删除后列队变空
            lq->rear=lq->front=NULL;
        else
            lq->front=lq->front->next;
        free(p);
        return 1;
    }
    
    int GetHead(LinkQueue *lq,char &x)  //取队头元素:将front节点的data域值赋给x
    {
        if(lq->front==NULL&&lq->rear==NULL)
            return 0;
        x=lq->front->data;
        return 1;
    }
    
    int QueueEmpty(LinkQueue *lq)   //判断队空:若链队为空,则返回1,否则返回0
    {
        if(lq->front==NULL&&lq->rear==NULL)
            return 1;
        else
            return 0;
    }
    
    int main()
    {
        LinkQueue *lq;
        char e;
        InitQueue(lq);
        printf("队%s
    ",QueueEmpty(lq)==1?"空":"不空");
        printf("a进队
    ");EnQueue(lq,'a');
        printf("b进队
    ");EnQueue(lq,'b');
        printf("c进队
    ");EnQueue(lq,'c');
        printf("d进队
    ");EnQueue(lq,'d');
        printf("队%s
    ",QueueEmpty(lq)==1?"空":"不空");
        GetHead(lq,e);
        printf("队头元素:%c
    ",e);
        printf("出队次序:");
        while(!QueueEmpty(lq))
        {
            DeQueue(lq,e);
            printf("%c ",e);
        }
        printf("
    ");
    }


    有Bug,不知咋改啊

    请dalao不吝赐教。
  • 相关阅读:
    高级特性(4)- 数据库编程
    UVA Jin Ge Jin Qu hao 12563
    UVA 116 Unidirectional TSP
    HDU 2224 The shortest path
    poj 2677 Tour
    【算法学习】双调欧几里得旅行商问题(动态规划)
    南洋理工大学 ACM 在线评测系统 矩形嵌套
    UVA The Tower of Babylon
    uva A Spy in the Metro(洛谷 P2583 地铁间谍)
    洛谷 P1095 守望者的逃离
  • 原文地址:https://www.cnblogs.com/liesun/p/7350347.html
Copyright © 2011-2022 走看看