其他分享
首页 > 其他分享> > 数据结构(8) - 链式队列

数据结构(8) - 链式队列

作者:互联网

链式队列,使用链表实现的队列存储结构,链式队列的实现思想同顺序队列类似,只需创建两个指针(命名为 top 和 rear)分别指向链表中队列的队头元素和队尾元素。

linked-queue.c

  1 /**
  2  * C data structure linked queue example.
  3  * 
  4  * License - MIT.
  5 */
  6 
  7 #include "linked-queue.h"
  8 
  9 
 10 /**
 11  * linkqueue_isempty - Determine if the queue is empty.
 12 */
 13 bool linkqueue_isempty(LPQUEUECURSOR lpcursor)
 14 {
 15     return (lpcursor->front == lpcursor->rear);
 16 }
 17 
 18 
 19 /**
 20  * linkqueue_put - Put data to queue.
 21 */
 22 int linkqueue_put(LPQUEUECURSOR lpcursor, int data)
 23 {
 24     LPLINKQUEUE node = NULL;
 25 
 26     node = (LPLINKQUEUE) malloc(sizeof(LINKQUEUE));
 27 
 28     if (NULL == node) {
 29         printf("Error in queue put.\n");
 30         return -1;
 31     }
 32 
 33     /* Put data to queue. */
 34     node->data              = data;
 35     node->next              = lpcursor->rear->next;
 36     lpcursor->rear->next    = node;
 37     lpcursor->rear          = lpcursor->rear->next;
 38 
 39     return 0;
 40 }
 41 
 42 
 43 /**
 44  * linkqueue_get - Get data from queue.
 45 */
 46 int linkqueue_get(LPQUEUECURSOR lpcursor, int *data)
 47 {
 48     LPLINKQUEUE tmp;
 49 
 50     /* Determine if queue is empty. */
 51     if (linkqueue_isempty(lpcursor))
 52         return -1;
 53 
 54     /* Get data from queue. */
 55     tmp             = lpcursor->front;
 56     lpcursor->front = lpcursor->front->next;
 57     *data           = lpcursor->front->data;
 58 
 59     free(tmp);
 60 
 61     return 0;
 62 }
 63 
 64 
 65 /**
 66  * linkqueue_init - Initialize linked queue.
 67 */
 68 int linkqueue_init(LPQUEUECURSOR *lpcursor)
 69 {
 70     /* Allocate cursor memory space. */
 71     *lpcursor = (LPQUEUECURSOR) malloc(sizeof(QUEUECURSOR));
 72 
 73     if (NULL == *lpcursor) {
 74         printf("Error in queue init.\n");
 75         return -1;
 76     }
 77 
 78     /* Allocate head node memory space. */
 79     (*lpcursor)->front = (LPLINKQUEUE) malloc(sizeof(LINKQUEUE));
 80 
 81     if (NULL == (*lpcursor)->front) {
 82         printf("Error in queue init.\n");
 83         return -1;
 84     }
 85 
 86     (*lpcursor)->front->next    = NULL;
 87     (*lpcursor)->rear           = (*lpcursor)->front;
 88 
 89     return 0;
 90 }
 91 
 92 
 93 /**
 94  * linkqueue_clear - Clear linked queue.
 95 */
 96 int linkqueue_clear(LPQUEUECURSOR lpcursor)
 97 {
 98     int tmp = -1;
 99 
100     while (!linkqueue_isempty(lpcursor))
101     {
102         linkqueue_get(lpcursor, &tmp);
103     }
104 
105     free(lpcursor);
106     lpcursor = NULL;
107 
108     return 0;
109 }

linked-queue.h

 1 /**
 2  * C data structure linked queue example.
 3  * 
 4  * License - MIT.
 5 */
 6 
 7 #ifndef __LINKED_QUEUE_H__
 8 #define __LINKED_QUEUE_H__
 9 
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdbool.h>
14 
15 
16 typedef struct _LINKQUEUE {
17     int data;
18     struct _LINKQUEUE *next;
19 } LINKQUEUE, *LPLINKQUEUE;
20 
21 typedef struct _QUEUECURSOR {
22     LPLINKQUEUE front, rear;
23 } QUEUECURSOR, *LPQUEUECURSOR;
24 
25 
26 bool linkqueue_isempty (LPQUEUECURSOR lpcursor);
27 int linkqueue_put      (LPQUEUECURSOR lpcursor, int data);
28 int linkqueue_get      (LPQUEUECURSOR lpcursor, int *data);
29 int linkqueue_init     (LPQUEUECURSOR *lpcursor);
30 int linkqueue_clear    (LPQUEUECURSOR lpcursor);
31 
32 
33 #endif /* __LINKED_QUEUE_H__ */

mian.c

 1 /**
 2  * C data structure linked queue example.
 3  * 
 4  * License - MIT.
 5 */
 6 
 7 #include <stdio.h>
 8 
 9 #include "linked-queue.h"
10 
11 
12 /**
13  * queue_test - Test linked queue example.
14 */
15 int queue_test(LPQUEUECURSOR lpcursor, 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         linkqueue_put(lpcursor, buf[i]);
24         printf("%d ", buf[i]);
25     }
26 
27     printf("\nGet data: ");
28 
29     for (i = 0; i < len; i++) {
30         linkqueue_get(lpcursor, &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     LPQUEUECURSOR lpcursor = NULL;
47 
48     linkqueue_init(&lpcursor);
49 
50     queue_test(lpcursor, buf, 5);
51 
52     linkqueue_clear(lpcursor);
53 
54     return 0;
55 }

Makefile

 1 # Makefile
 2 CC = gcc
 3 CFLAGS = -Wall -g -O0
 4 
 5 SRC = main.c linked-queue.c
 6 
 7 OBJ = linkqueue-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].

标签:lpcursor,LPQUEUECURSOR,队列,linkqueue,queue,int,链式,数据结构,data
来源: https://www.cnblogs.com/tinyshark/p/16411788.html