zl程序教程

您现在的位置是:首页 >  后端

当前栏目

【数据结构】顺序队列与链表队列

2023-09-27 14:27:16 时间

顺序队列

头文件

 1 //@ author 成鹏致远
 2 //@ net http://infodown.tap.cn
 3 //@ qq 552158509
 4 //@ blog lcw.cnblogs.com
 5 
 6 #ifndef __SQUEUE_H
 7 #define __SQUEUE_H
 8 
 9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdbool.h>
12 
13 #define MAXSIZE 1024
14 
15 typedef int datatype;
16 
17 typedef struct node
18 {
19     datatype data[MAXSIZE];
20     int front,rear;
21 }squeue,*p_squeue;
22 
23 extern void squeue_init(p_squeue *pqueue);//初始化队列
24 extern bool is_empty(p_squeue pqueue);//为了区别队满和队空,队列浪费一个空间
25 extern bool is_full(p_squeue pqueue);
26 extern bool in_squeue(p_squeue pqueue, datatype data);//入队操作
27 extern bool out_squeue(p_squeue pqueue, datatype *result);//出队操作
28 extern void squeue_show(p_squeue pqueue);//显示队列
29 
30 #endif
View Code

 顺序队列

 1 //@ author 成鹏致远
 2 //@ net http://infodown.tap.cn
 3 //@ qq 552158509
 4 //@ blog lcw.cnblogs.com
 5 
 6 //输入数字入队
 7 //输入字符出队
 8 
 9 #include "squeue.h"
10 
11 void squeue_init(p_squeue *pqueue)//初始化队列
12 {
13     *pqueue = (p_squeue)malloc(sizeof(squeue));
14     if(NULL == *pqueue)
15     {
16         perror("malloc");
17         exit(1);
18     }
19     (*pqueue)->front = MAXSIZE - 1;
20     (*pqueue)->rear = MAXSIZE - 1;
21 }
22 
23 bool is_empty(p_squeue pqueue)//为了区别队满和队空,队列浪费一个空间
24 {
25     if(pqueue->front == pqueue->rear)//队空标志
26         return true;
27     else
28         return false;
29 }
30 
31 bool is_full(p_squeue pqueue)
32 {
33     if((pqueue->rear+1)%MAXSIZE == pqueue->front)//队满标志
34         return true;
35     else
36         return false;
37 }
38 
39 bool in_squeue(p_squeue pqueue, datatype data)//入队操作
40 {
41     if(is_full(pqueue))
42         return false;
43     else//入队操作,从队尾开始入
44     {
45         pqueue->rear = (pqueue->rear+1)%MAXSIZE;//入队前要先移动rear
46         pqueue->data[pqueue->rear] = data;
47         
48         return true;
49     }
50 }
51 
52 bool out_squeue(p_squeue pqueue, datatype *result)//出队操作
53 {
54     if(is_empty(pqueue))
55         return false;
56     else//出队操作,从队头开始出
57     {
58         pqueue->front = (pqueue->front+1)%MAXSIZE;//出队前要先移动front
59         *result = pqueue->data[pqueue->front];
60 
61         return true;
62     }
63 }
64 
65 void squeue_show(p_squeue pqueue)//显示队列
66 {
67     int i;
68 
69     for(i=(pqueue->front+1)%MAXSIZE; i != (pqueue->rear+1)%MAXSIZE; i++)//注意循环条件
70     {
71         printf("%d\t",pqueue->data[i]);
72     }
73     printf("\n");
74 }
View Code

主文件

 1 //@ author 成鹏致远
 2 //@ net http://infodown.tap.cn
 3 //@ qq 552158509
 4 //@ blog lcw.cnblogs.com
 5 
 6 //输入数据则入栈
 7 //输入字符则出栈
 8 
 9 #include "squeue.h"
10 
11 int main()
12 {
13     p_squeue pqueue;
14     datatype data;
15     datatype result;//用来存放出队的元素
16 
17     squeue_init(&pqueue);//初始化队列
18 
19     while(1)
20     {
21         printf("Pls input data: ");
22         if(1 == scanf("%d",&data))//输入数据,入队
23         {
24             in_squeue(pqueue,data);//入队操作
25             squeue_show(pqueue);//显示队内内容
26         }
27         else//输入字符,出队
28         {
29             if(out_squeue(pqueue,&result))//元素出队
30             {
31                 printf("The Front is %d\n",result);
32                 squeue_show(pqueue);
33             }
34             while('\n' != getchar());//缓冲区处理
35         }
36     }
37 
38     return 0;
39 }
View Code

 


链表队列 

 

头文件

 1 //@ author 成鹏致远
 2 //@ net http://infodown.tap.cn
 3 //@ qq 552158509
 4 //@ blog lcw.cnblogs.com
 5 
 6 #ifndef __LINKQUEUE_H
 7 #define __LINKQUEUE_H
 8 
 9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <stdbool.h>
12 
13 typedef int datatype;
14 
15 typedef struct node
16 {
17     datatype data;
18     struct node *next;
19 }link_node,*link_pnode;
20 
21 typedef struct queue
22 {
23     link_pnode front,rear;
24 }link_queue,*link_pqueue;
25 
26 
27 extern void linkqueue_init(link_pqueue *pqueue);//初始化链表队列
28 extern bool is_empty(link_pqueue pqueue);
29 extern bool in_linkqueue(link_pqueue pqueue, datatype data);//链表队列入队
30 extern bool out_linkqueue(link_pqueue pqueue, datatype *result);//链表队列出队
31 extern void linkqueue_show(link_pqueue pqueue);//显示链表队列
32 
33 #endif
View Code

链表队列

 1 //@ author 成鹏致远
 2 //@ net http://infodown.tap.cn
 3 //@ qq 552158509
 4 //@ blog lcw.cnblogs.com
 5 
 6 //输入数字入栈
 7 //输入字符出栈
 8 
 9 #include "linkqueue.h"
10 
11 void linkqueue_init(link_pqueue *pqueue)//初始化链表队列
12 {
13     link_pnode pnode;
14 
15     *pqueue = (link_pqueue)malloc(sizeof(link_queue));//分配链表
16     if(NULL == *pqueue)
17     {
18         perror("malloc");
19         exit(1);
20     }
21 
22     pnode = (link_pnode)malloc(sizeof(link_node));//分配节点
23     if(NULL == pnode)
24     {
25         perror("malloc");
26         exit(1);
27     }
28 
29     pnode->next = NULL;//初始化pnode
30     //初始化pqueue
31     (*pqueue)->front = pnode;
32     (*pqueue)->rear = pnode;
33 }
34 
35 bool is_empty(link_pqueue pqueue)
36 {
37     if(pqueue->front == pqueue->rear)
38         return true;
39     else
40         return false;
41 }
42 
43 bool in_linkqueue(link_pqueue pqueue, datatype data)//链表队列入队
44 {
45     link_pnode new;
46     new = (link_pnode)malloc(sizeof(link_node));//分配结点
47     if(NULL == new)
48         return false;
49     else
50     {
51         new->data = data;
52 
53         //将new结点插入到队列的末尾
54         new->next = pqueue->rear->next;//将尾结点的next赋值给new->next
55         pqueue->rear->next = new;//尾结点指向new,即new成为新的尾结点
56         pqueue->rear = new;//尾结点指向new
57 
58         return true;
59     }
60 }
61 
62 bool out_linkqueue(link_pqueue pqueue, datatype *result)//链表队列出队
63 {
64     link_pnode pnode;//临时指针,指向要出队的结点
65 
66     if(is_empty(pqueue))
67         return false;
68     else
69     {
70         pnode = pqueue->front;//头结点,将出队
71         pqueue->front = pnode->next;//头指针后移
72         //注意头指针指向的是头结点,第一个结点是pqueue->front,即出队结点
73         *result = pqueue->front->data;
74         free(pnode);//释放结点空间
75 
76         return true;
77     }
78 }
79 
80 
81 void linkqueue_show(link_pqueue pqueue)//显示链表队列
82 {
83     link_pnode pnode;//负责遍历过程中的节点移动
84 
85     pnode = pqueue->front->next;//指向第一个结点
86     while(NULL != pnode)
87     {
88         printf("%d\t",pnode->data);
89         pnode = pnode->next;
90     }
91     printf("\n");
92 }
View Code

主文件

 1 //@ author 成鹏致远
 2 //@ net http://infodown.tap.cn
 3 //@ qq 552158509
 4 //@ blog lcw.cnblogs.com
 5 
 6 //输入数字则入栈
 7 //输入字符则出栈
 8 
 9 #include "linkqueue.h"
10 
11 int main()
12 {
13     link_pqueue plink;
14     datatype data;//用于保存输入的数
15     datatype result;//用于保存出队的结果
16 
17     linkqueue_init(&plink);//初始化链表队列
18 
19     while(1)
20     {
21         printf("Pls input data: ");
22         if(1 == scanf("%d",&data))//输入数字,入队
23         {
24             in_linkqueue(plink,data);//入队
25             linkqueue_show(plink);
26         }
27         else//输入字符,出队
28         {
29             if(out_linkqueue(plink,&result))//出队成功
30             {
31                 printf("The front is %d\n",result);
32             }
33             linkqueue_show(plink);
34 
35             while('\n' != getchar());//缓冲区处理
36         }
37     }
38 
39     return 0;
40 }
View Code