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

链式队列

 


 

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\n");
 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  入队成功\n",input);
 73 
 74     return 1;
 75 }
 76 
 77 
 78 // 出队
 79 int deque(que *q){
 80 
 81     if(  check_empty(q)  ){       // 判断是否为空
 82         printf("que empty\n");
 83         return 0;
 84     }
 85     else
 86         printf("que not empty\n");
 87 
 88     printf("output =  %d  出队成功\n",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\n",q->nuque);
 96     if(q->nuque==0){               // 项数为空,头尾为空
 97         printf("队列为空\n");
 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("\n------------------\n\n");
15 
16     for(i=0;i<5;i++){    //    出队
17 
18         deque(q);
19 
20     }
21 
22 
23     return 0 ;
24 }

 

 

测试:


 

 

 

 

success !

 

转载于:https://www.cnblogs.com/panda-w/p/11081126.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值