其他分享
首页 > 其他分享> > 数据结构(7) - 顺序队列

数据结构(7) - 顺序队列

作者:互联网

顺序队列是队列的顺序存储结构,顺序队列实际上是运算受限的顺序表。和顺序表一样,顺序队列用一个向量空间来存放当前队列中的元素。由于队列的队头和队尾的位置是变化的,设置两个指针front和rear分别指示队头元素和队尾元素在向量空间中的位置,它们的初值在队列初始化时均应设置为0。

sequence-queue.c

 1 /**
 2  * C data structure sequence queue example.
 3  * 
 4  * License - MIT.
 5 */
 6 
 7 #include "sequence-queue.h"
 8 
 9 
10 /**
11  * sequeue_isempty - Determine if the queue is empty.
12 */
13 bool sequeue_isempty(LPSEQUEUE lpqueue)
14 {
15     return (lpqueue->front == lpqueue->rear);
16 }
17 
18 
19 /**
20  * sequeue_isfull - Determine if the queue is full.
21 */
22 bool sequeue_isfull(LPSEQUEUE lpqueue)
23 {
24     return (lpqueue->front == (lpqueue->rear + 1) % MAX_DATA_SIZE);
25 }
26 
27 
28 /**
29  * sequeue_put - Put data to queue.
30 */
31 int sequeue_put(LPSEQUEUE lpqueue, int data)
32 {
33     /* Determine if sequential stack is full. */
34     if (sequeue_isfull(lpqueue))
35         return -1;
36 
37     /* Put data to stack. */
38     lpqueue->rear = (lpqueue->rear + 1) % MAX_DATA_SIZE;
39     lpqueue->data[lpqueue->rear] = data;
40 
41     return 0;
42 }
43 
44 
45 /**
46  * sequeue_get - Get data from queue.
47 */
48 int sequeue_get(LPSEQUEUE lpqueue, int *data)
49 {
50     /* Determine if sequential stack is empty. */
51     if (sequeue_isempty(lpqueue))
52         return -1;
53 
54     /* Get data from stack. */
55     lpqueue->front = (lpqueue->front + 1) % MAX_DATA_SIZE;
56     *data = lpqueue->data[lpqueue->front];
57 
58     return 0;
59 }
60 
61 
62 /**
63  * sequeue_init - Initialize sequence queue.
64 */
65 int sequeue_init(LPSEQUEUE *lpqueue)
66 {
67     /* Allocate memory space. */
68     *lpqueue = (LPSEQUEUE) malloc(sizeof(SEQUEUE));
69 
70     if (NULL == *lpqueue) {
71         printf("Error in queue init.\n");
72         return -1;
73     }
74 
75     (*lpqueue)->front = MAX_DATA_SIZE - 1;
76     (*lpqueue)->rear  = MAX_DATA_SIZE - 1;
77 
78     return 0;
79 }
80 
81 
82 /**
83  * sequeue_clear - Clear sequence queue.
84 */
85 int sequeue_clear(LPSEQUEUE lpqueue)
86 {
87     int tmp = -1;
88 
89     while (!sequeue_isempty(lpqueue))
90     {
91         sequeue_get(lpqueue, &tmp);
92     }
93 
94     free(lpqueue);
95     lpqueue = NULL;
96 
97     return 0;
98 }

sequence-queue.h

 1 /**
 2  * C data structure sequence queue example.
 3  * 
 4  * License - MIT.
 5 */
 6 
 7 #ifndef __SEQUENCE_QUEUE_H__
 8 #define __SEQUENCE_QUEUE_H__
 9 
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdbool.h>
14 
15 #define MAX_DATA_SIZE               10
16 
17 typedef struct _SEQUEUE {
18     int data[MAX_DATA_SIZE];
19     int front, rear;
20 } SEQUEUE, *LPSEQUEUE;
21 
22 
23 bool sequeue_isempty (LPSEQUEUE lpqueue);
24 bool sequeue_isfull  (LPSEQUEUE lpqueue);
25 int sequeue_put      (LPSEQUEUE lpqueue, int data);
26 int sequeue_get      (LPSEQUEUE lpqueue, int *data);
27 int sequeue_init     (LPSEQUEUE *lpqueue);
28 int sequeue_clear    (LPSEQUEUE lpqueue);
29 
30 
31 #endif /* __SEQUENCE_QUEUE_H__ */

main.c

 1 /**
 2  * C data structure sequence queue example.
 3  * 
 4  * License - MIT.
 5 */
 6 
 7 #include <stdio.h>
 8 
 9 #include "sequence-queue.h"
10 
11 
12 /**
13  * queue_test - Use queue test example.
14 */
15 int queue_test(LPSEQUEUE lpqueue, int buf[], int len)
16 {
17     int i = 0;
18     int data = 0;
19 
20     printf("Put data: ");
21 
22     for (i = 0; i < len; i++) {
23         sequeue_put(lpqueue, buf[i]);
24         printf("%d ", buf[i]);
25     }
26 
27     printf("\nGet data: ");
28 
29     for (i = 0; i < len; i++) {
30         sequeue_get(lpqueue, &data);
31         printf("%d ", data);
32     }
33 
34     printf("\n");
35 
36     return 0;
37 }
38 
39 
40 /**
41  * Main function.
42 */
43 int main(void)
44 {
45     int buf[5] = {1, 2, 3, 4, 5};
46     LPSEQUEUE sequeue = NULL;
47 
48     sequeue_init(&sequeue);
49 
50     queue_test(sequeue, buf, 5);
51 
52     sequeue_clear(sequeue);
53 
54     return 0;
55 }

Makefile

 1 # Makefile
 2 CC = gcc
 3 CFLAGS = -Wall -g -O0
 4 
 5 SRC = main.c sequence-queue.c
 6 
 7 OBJ = sequeue-test
 8 
 9 $(OBJ) : $(SRC)
10     $(CC) $(CFLAGS) -o $@ $^
11 
12 clean:
13     $(RM) $(OBJ) *.o *.*.sw?

 

详细请参考Github: [Link] [https://github.com/Phoebus-Ma/C-Helper/tree/main/Class-1/Queue.C].

标签:顺序,LPSEQUEUE,lpqueue,队列,queue,int,数据结构,data,sequeue
来源: https://www.cnblogs.com/tinyshark/p/16411768.html