ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2022-06-25 16:00:18  阅读:133  来源: 互联网

标签:lpcursor LPQUEUECURSOR 队列 linkqueue queue int 链式 数据结构 data


链式队列,使用链表实现的队列存储结构,链式队列的实现思想同顺序队列类似,只需创建两个指针(命名为 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有