• 代码示例_数据结构_链式队列


    链式队列


    que.h

     1 #pragma once
     2 
     3 #include <stdio.h>
     4 #include <stdlib.h>
     5 
     6 #define MAXQUE 10
     7 
     8 
     9 typedef struct node{
    10     int data;
    11     struct node *next;
    12     struct node *pdrv;
    13 }node;    // 节点
    14 
    15 typedef struct que{
    16     node *front;
    17     node *rear;
    18     int nuque;       // 节点数 
    19 }que;    // 队列
    20 
    21 que* init_que(void);              // 初始化队列
    22 int     check_full ( que *q);        // 检查是否已满
    23 int  check_empty( que *q);        // 检查是否为空
    24 int  enque(que *q,int input);   // 入队
    25 int  deque(que *q);                // 出队
    26 int  que_count(que *q);            // 项数
    27 void del_que(que *q);            // 清空

    que.c

      1 #include "que.h"
      2 
      3 
      4 // 初始化队列
      5 que* init_que(void){
      6 
      7     que *q = (que*)malloc(sizeof(que));   // 申请一个队列
      8         if(q==NULL){
      9         perror("malloc failed!");
     10         exit(1);
     11     }
     12 
     13     q->front = q->rear = NULL;
     14     q->nuque = 0;
     15 
     16     return q ;
     17 }
     18 
     19 
     20 // 检查是否已满
     21 int check_full ( que *q){
     22 
     23     return q->nuque == MAXQUE;
     24 
     25 }
     26 
     27 
     28 // 检查是否为空
     29 int check_empty( que *q){
     30 
     31     return q->nuque == 0;
     32 
     33 }
     34 
     35 
     36 // 项数
     37 int que_count(que *q){
     38 
     39     return q->nuque;
     40 
     41 }
     42 
     43 
     44 // 入队
     45 int enque(que *q,int input){
     46 
     47     if(  check_full(q) ){        // 判断是否已满
     48         printf("que full
    ");
     49         return 0;
     50     }
     51 
     52     node *pnew = (node*)malloc(sizeof(node));
     53     if(pnew==NULL){
     54         perror("malloc failed!");
     55         exit(1);
     56     }
     57     pnew->data = input;
     58     pnew->next = NULL;
     59 
     60     if(  check_empty(q)  ){     // 判断是否为空
     61         q->front = pnew;        // 插头
     62         q->rear  = pnew; 
     63     }
     64         
     65     else{
     66         q->rear->next  = pnew;  // 插尾
     67         q->rear = pnew;
     68     }
     69 
     70     q->nuque++;
     71 
     72     printf("input =  %d  入队成功
    ",input);
     73 
     74     return 1;
     75 }
     76 
     77 
     78 // 出队
     79 int deque(que *q){
     80 
     81     if(  check_empty(q)  ){       // 判断是否为空
     82         printf("que empty
    ");
     83         return 0;
     84     }
     85     else
     86         printf("que not empty
    ");
     87 
     88     printf("output =  %d  出队成功
    ",q->front->data);
     89 
     90     node* pnew = q->front;       // 指向队首
     91     q->front = q->front->next;
     92     free(pnew);                  // 释放
     93     
     94     q->nuque--;
     95     printf("nuque:%d
    ",q->nuque);
     96     if(q->nuque==0){               // 项数为空,头尾为空
     97         printf("队列为空
    ");
     98         q->front = NULL;
     99         q->rear  = NULL;
    100     }
    101 
    102     return 1;
    103 }
    104 
    105 
    106 // 清空
    107 void del_que(que *q){
    108     while(  !check_empty(q)  )
    109         deque(q);
    110 }

    main.c

     1 #include "que.h"
     2 
     3 int main(void)
     4 {
     5     que *q = init_que();
     6     
     7     int i = 0;
     8     for(i=0;i<5;i++){    //     入队
     9 
    10         enque(q,i);
    11 
    12     }
    13 
    14     printf("
    ------------------
    
    ");
    15 
    16     for(i=0;i<5;i++){    //    出队
    17 
    18         deque(q);
    19 
    20     }
    21 
    22 
    23     return 0 ;
    24 }

    测试:


    success !

    Stay hungry, stay foolish 待续。。。
  • 相关阅读:
    python3.4 + pycharm 环境安装 + pycharm使用
    ddt源码修改:HtmlTestRunner报告依据接口名显示用例名字
    re模块
    LeetCode Weekly Contest 12
    求解强连通分量
    几道题-找规律-记录并查找
    欧几里德算法
    树上二分
    几道题-博弈
    随便写一些东西-缩边
  • 原文地址:https://www.cnblogs.com/panda-w/p/11081126.html
Copyright © 2020-2023  润新知