• 链队


    #include<stdio.h>
    #include<stdlib.h>
    #include<malloc.h>
    typedef int type;
    typedef struct Node
    {
        type info;
        struct Node*next;
    }node;
    //队列声明,保存头指针,尾指针
    typedef struct
    {
        node *front;
        node *rear;
    }queue;
    //初始化一个空队列并返回其指针
    queue* init()
    {
        queue *head=(queue*)malloc(sizeof(queue));
        head->front=head->rear=NULL;
    }
    //打印一个队列
    void display(queue*head)
    {
        node *p=head->front;
        while(p){
            printf("%5d",p->info);
            p=p->next;
        }
        printf("
    ");
    }
    //入队
    void insert(queue*head)
    {
        node*pre;
        pre=(node*)malloc(sizeof(node));
        pre->next=NULL;
        scanf("%d",&pre->info);
        if(!head->front)//如果是空队列,则front和rear指针指向该节点
            head->front=head->rear=pre;
        else{//否则,在尾部插入
            head->rear->next=pre;
            head->rear=pre;
        }
    }
    //销毁一个队列
    void destory(queue*head)
    {
        node*pre=head->front,*p;
        while(pre)
        {
            p=pre->next;
            free(pre);
            pre=p;
        }
        free(head);
    }
    //出队
    void dele(queue *head)
    {
        node*p;
        if(!head->front)
            printf("NULL
    ");
        else{//非空队列
           p=head->front;
           head->front=p->next;
           free(p);
           if(!head->front)//如果是删除了最后一个节点,队列置空
            head->rear=NULL;
        }
    }
    //建立一个长度为n的队列
    void create(queue *head,int n)
    {
        int i;
        for(i=1;i<=n;i++)
            insert(head);
    }

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    如何选择Linux操作系统版本?
    js+html实现玫瑰花绽放
    Linux系统目录结构
    laravel5.6操作数据curd写法(查询构建器)
    laravel5.6 常规框架部署和配置文件说明
    PHP读取XML文件数据获取节点值
    Fiddler正则匹配调试接口示例
    php常用端口号
    php heredoc的用法详解
    oracle表空间操作
  • 原文地址:https://www.cnblogs.com/Thereisnospon/p/4768522.html
Copyright © 2020-2023  润新知