zoukankan      html  css  js  c++  java
  • 数据结构实验六——链队列

    #include <stdio.h>
    #include <stdlib.h>
    int flag=0;
    typedef struct Qnode
    {
        char data;
        struct Qnode *next;
    }Qnode,*QnodePtr;
    typedef struct LinkQueue
    {
        QnodePtr front;
        QnodePtr rear;
    }LinkQueue;
    void InitQueue(LinkQueue *Q)
    {
        Q->front=(QnodePtr)malloc(sizeof(Qnode));
        Q->rear=Q->front;
        Q->front->next=NULL;
    }
    void CreateQueue(LinkQueue *Q)
    {
        int i,len;
        char ch;
        QnodePtr p;
        printf("请输入队列的长度:
    ");
        scanf("%d",&len);
        for(i=0;i<len;i++)
        {
            printf("请输入队列的元素:
    ");
            scanf("%c",&ch);
            p=(QnodePtr)malloc(sizeof(Qnode));
            if((ch=getchar())!='
    ')
            {
                p->data=ch;
                p->next=Q->rear->next;
                Q->rear->next=p;
                Q->rear=p;
            }
            else
                i--;
        }
        flag=1;
    }
    void PushQueue(LinkQueue *Q,char e)
    {
        QnodePtr p=(QnodePtr)malloc(sizeof(Qnode));
        p->data=e;
        p->next=Q->rear->next;
        Q->rear->next=p;
        Q->rear=p;
    }
    char PopQueue(LinkQueue *Q)
    {
        char ch;
        QnodePtr p;
        ch=Q->front->next->data;
        p=Q->front->next;
        Q->front->next=p->next;
        free(p);
        return ch;
    }
    int LengthQueue(LinkQueue Q)
    {
        int len=0;
        QnodePtr p=Q.front->next;
        while(p)
        {
            len++;
            p=p->next;
        }
        return len;
    }
    int EmptyQueue(LinkQueue Q)
    {
        if(Q.front->next==NULL)
            return 1;
        return 0;
    }
    void DisplayQueue(LinkQueue Q)
    {
        QnodePtr p=Q.front->next;
        printf("此链队列的输出为:
    ");
        while(p)
        {
            printf("%c ",p->data);
            printf("
    ");
            p=p->next;
        }
    }
    void DestroyQueue(LinkQueue *Q)
    {
        QnodePtr p,q;
        p=Q->front;
        q=p->next;
        while(q)
        {
            p=q;
            q=p->next;
            free(p);
        }
        Q->front->next=NULL;
        Q->rear=Q->front;
        flag=0;
    }
    void menu()
    {
        printf("	链队列基本实验操作
    ");
        printf("*********************************
    ");
        printf("1 建立链队列!			*
    ");
        printf("2 链队列入队操作!		*
    ");
        printf("3 链队列出队操作!		*
    ");
        printf("4 求链队列长度!		*
    ");
        printf("5 判断队列是否为空!		*
    ");
        printf("6 显示队列!			*
    ");
        printf("7 销毁队列!			*
    ");
        printf("0 退出程序!			*
    ");
        printf("*********************************
    ");
    }
    
    
    int main()
    {
        LinkQueue Q;
        int select;
        char ch;
        InitQueue(&Q);
        while(1)
        {
            menu();
            printf("请输入选择命令:
    ");
            scanf("%d",&select);
            switch(select)
            {
            case 1:
                if(flag==1)
                    printf("链队列已经创建!
    ");
                else
                    CreateQueue(&Q);
                break;
            case 2:
                if(flag==0)
                    printf("链队列还未创建,无法入队操作!
    ");
                else
                {
                    printf("请输入要入队的元素:
    ");
                    scanf("%c",&ch);
                    if((ch=getchar())!='
    ')
                        PushQueue(&Q,ch);
                }
                break;
            case 3:
                if(flag==0)
                    printf("链队列还未创建,无法出队操作!
    ");
                else if(EmptyQueue(Q))
                    printf("链队列为空,无法出队操作!
    ");
                else
                {
                    ch=PopQueue(&Q);
                    printf("链队列出队元素为:%c
    ",ch);
                }
                break;
            case 4:
                if(flag==0)
                    printf("链队列未创建!
    ");
                else
                    printf("链队列的长度为:%d
    ",LengthQueue(Q));
                break;
            case 5:
                if(flag==0)
                    printf("链队列未创建!
    ");
                else if(EmptyQueue(Q))
                    printf("链队列为空!
    ");
                else
                    printf("链队列不为空!
    ");
                break;
            case 6:
                if(flag==0)
                    printf("链队列未创建!
    ");
                else
                    DisplayQueue(Q);
                break;
            case 7:
                if(flag==0)
                    printf("链队列未创建!
    ");
                else
                    {
                        DestroyQueue(&Q);
                        printf("链队列已经被删除!
    ");
                    }
                break;
            case 0:
                exit(1);
                break;
            default :
                printf("输入命令有误,请重新输入!
    ");
                break;
            }
        }
        return 0;
    }
    

  • 相关阅读:
    The Mac Application Environment 不及格的程序员
    Xcode Plugin: Change Code In Running App Without Restart 不及格的程序员
    The property delegate of CALayer cause Crash. 不及格的程序员
    nil localizedTitle in SKProduct 不及格的程序员
    InApp Purchase 不及格的程序员
    Safari Web Content Guide 不及格的程序员
    在Mac OS X Lion 安装 XCode 3.2 不及格的程序员
    illustrate ARC with graphs 不及格的程序员
    Viewing iPhoneOptimized PNGs 不及格的程序员
    What is the dSYM? 不及格的程序员
  • 原文地址:https://www.cnblogs.com/abc-24990/p/4257507.html
Copyright © 2011-2022 走看看