• 编程实现队列的入队/出队操作


    思路:队列其实也是一个链表,只是队列还有两个特殊的结点,一个指向队头,一个指向队尾。先设计数据结构,如下

    typedef struct student * PNode;
    typedef struct linkqueue * Pqueue;
    
    typedef struct student
    {
        int data;
        PNode next;
    }Node;
    
    
    typedef struct linkqueue
    {
        PNode first;
        PNode rear;
    }queue;

    1.入队操作其实是指向队尾的指针向后移,要判断队列是否为空或者只有一个结点的情况

    2.出队操作其实是指向队头的指针向后移

    整体代码如下:

    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct student * PNode;
    typedef struct linkqueue * Pqueue;
    
    typedef struct student
    {
        int data;
        PNode next;
    }Node;
    
    
    typedef struct linkqueue
    {
        PNode first;
        PNode rear;
    }queue;
    
    
    Pqueue insert(Pqueue link,int num)
    {
        PNode p;
        Pqueue q=link;
            p=(PNode)malloc(sizeof(Node));
            p->data=num;
        if(link==NULL)
        {
            printf("添加第一个结点
    ");
            q=(Pqueue)malloc(sizeof(queue));
            q->first=p;
            q->rear=p;
            q->rear->next=NULL;
            return q;
        }
        q->rear->next=p;
        q->rear=p;    
        q->rear->next=NULL;
        return q;
    }
    
    Pqueue del(Pqueue queue)
    {
        if(queue==NULL)
        {
            printf("队列为空");
            return NULL;
        }
        Pqueue q=queue;
        PNode temp;
        temp=q->first;
        if(q->first->next!=NULL)
            q->first=q->first->next;
        else
        {
            printf("队列只有一个结点,删除完毕
    ");
            return NULL;
        }
        free(temp);
        return q;
    }
    
    void print(Pqueue link)
    {
        PNode q=link->first;
        while(q!=NULL)
        {
            printf("%d ",q->data);
            q=q->next;
        }
        printf("
    ");
    }
    
    int main(void)
    {
        Pqueue linkqueue=NULL;
        int flag=0,num;
        while(1)
        {
            printf("选择入队或者出队:1为入队,2为出队,0为退出
    ");
            scanf("%d",&flag);
            if(flag==1)
            {
                printf("请选择要入队的值:
    ");
                scanf("%d",&num);
                linkqueue=insert(linkqueue,num);
                printf("打印入队后的队列:
    ");
                print(linkqueue);
            }
            else if(flag==2)
            {
                linkqueue=del(linkqueue);
                printf("打印出队后的队列:
    ");
                print(linkqueue);
            }
            else
                break;
        }
        printf("打印最后的队列:
    ");
        print(linkqueue);
        return 0;
    }

     程序猿必读

  • 相关阅读:
    main函数的一些特性
    确保函数的操作不超出数组实参的边界
    今天学习了一点sed
    libevent 与事件驱动
    mvc3 action验证失败后的自定义处理
    使用spring.net+nibernate时如何用aspnet_regiis加密数据库连接字符串
    C# 中 IList IEnumable 转换成 List类型
    Nhibernate 过长的字符串报错 dehydration property
    小论接口(interface)和抽象类(abstract class)的区别
    C# 语言在函数参数列表中出现this关键词的作用
  • 原文地址:https://www.cnblogs.com/longzhongren/p/4418263.html
Copyright © 2020-2023  润新知