数据结构(6) - 链式栈

链式栈是一种数据存储结构,可以通过单链表的方式来实现,使用链式栈的优点在于它能够克服用数组实现的顺序栈空间利用率不高的特点,但是需要为每个栈元素分配额外的指针空间用来存放指针域。

 

 

 linked-stack.c

 1 /**
 2  * C data structure linked stack example.
 3  * 
 4  * License - MIT.
 5 */
 6 
 7 #include "linked-stack.h"
 8 
 9 
10 /**
11  * linkstack_isempty - Determine if the stack is empty.
12 */
13 bool linkstack_isempty(LPLINKSTACK lpcursor)
14 {
15     return (NULL == lpcursor);
16 }
17 
18 
19 /**
20  * linkstack_push - Push data to stack.
21 */
22 int linkstack_push(LPLINKSTACK *lpcursor, int data)
23 {
24     LPLINKSTACK member = NULL;
25 
26     member = (LPLINKSTACK) malloc(sizeof(LINKSTACK));
27 
28     if (NULL == member) {
29         printf("Error in push.\n");
30         return -1;
31     }
32 
33     member->data = data;
34     member->next = *lpcursor;
35 
36     *lpcursor = member;
37 
38     return 0;
39 }
40 
41 
42 /**
43  * linkstack_pop - Pop data from stack.
44 */
45 int linkstack_pop(LPLINKSTACK *lpcursor, int *data)
46 {
47     LPLINKSTACK tmp;
48 
49     if(linkstack_isempty(*lpcursor)){
50         printf("Stack is empty.\n");
51         return -1;
52     }
53 
54     tmp       = *lpcursor;
55     *data     = tmp->data;
56     *lpcursor = (*lpcursor)->next;
57 
58     free(tmp);
59 
60     return 0;
61 }
62 
63 
64 /**
65  * linkstack_init - Initialize linked stack.
66 */
67 int linkstack_init(LPLINKSTACK *lpcursor)
68 {
69     *lpcursor = NULL;
70 
71     return 0;
72 }
73 
74 
75 /**
76  * linkstack_clear - Clear linked stack.
77 */
78 int linkstack_clear(LPLINKSTACK *lpcursor)
79 {
80     int i = -1;
81 
82     while (!linkstack_isempty(*lpcursor))
83     {
84         linkstack_pop(lpcursor, &i);
85     }
86 
87     *lpcursor = NULL;
88 
89     return 0;
90 }

linked-stack.h

 1 /**
 2  * C data structure linked stack example.
 3  * 
 4  * License - MIT.
 5 */
 6 
 7 #ifndef __LINKED_STACK_H__
 8 #define __LINKED_STACK_H__
 9 
10 
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <stdbool.h>
14 
15 
16 typedef struct _LINKSTACK {
17     int data;
18     struct _LINKSTACK *next;
19 } LINKSTACK, *LPLINKSTACK;
20 
21 
22 bool linkstack_isempty  (LPLINKSTACK lpcursor);
23 int linkstack_push      (LPLINKSTACK *lpcursor, int data);
24 int linkstack_pop       (LPLINKSTACK *lpcursor, int *data);
25 int linkstack_init      (LPLINKSTACK *lpcursor);
26 int linkstack_clear     (LPLINKSTACK *lpcursor);
27 
28 
29 #endif /* __LINKED_STACK_H__ */

main.c

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

Makefile

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

posted @ 2022-06-25 15:24  this毛豆  阅读(29)  评论(0编辑  收藏  举报