ICode9

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

数据结构(5) - 顺序栈

2022-06-25 15:36:05  阅读:147  来源: 互联网

标签:seqstack LPSEQSTACK int 顺序 lpstack 数据结构 data stack


顺序栈是栈的顺序实现。顺序栈是指利用顺序存储结构实现的栈。采用地址连续的存储空间(数组)依次存储栈中数据元素,由于入栈和出栈运算都是在栈顶进行,而栈底位置是固定不变的,可以将栈底位置设置在数组空间的起始处;栈顶位置是随入栈和出栈操作而变化的,故需用一个整型变量top来记录当前栈顶元素在数组中的位置。

 

 sequence-stack.c

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

sequence-stack.h

 1 /**
 2  * C data structure sequence list example.
 3  * 
 4  * License - MIT.
 5 */
 6 
 7 #ifndef __SEQ_STACK_H__
 8 #define __SEQ_STACK_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 _SEQSTACK {
18     int data[MAX_DATA_SIZE];
19     int top;
20 } SEQSTACK, *LPSEQSTACK;
21 
22 
23 bool seqstack_isempty (LPSEQSTACK lpstack);
24 bool seqstack_isfull  (LPSEQSTACK lpstack);
25 int seqstack_push     (LPSEQSTACK lpstack, int data);
26 int seqstack_pop      (LPSEQSTACK lpstack, int *data);
27 int seqstack_init     (LPSEQSTACK *lpHead);
28 int seqstack_clear    (LPSEQSTACK lpHead);
29 
30 
31 #endif /* __SEQ_STACK_H__ */

main.c

 1 /**
 2  * C data structure sequence stack example.
 3  * 
 4  * License - MIT.
 5 */
 6 
 7 #include <stdio.h>
 8 #include <string.h>
 9 
10 #include "sequence-stack.h"
11 
12 
13 /**
14  * stack_test - Use stack test example.
15 */
16 int stack_test(LPSEQSTACK lpHead, int buf[], int len)
17 {
18     int i = 0;
19     int data = 0;
20 
21     printf("Push data: ");
22 
23     for (i = 0; i < len; i++) {
24         seqstack_push(lpHead, buf[i]);
25         printf("%d ", buf[i]);
26     }
27 
28     printf("\nPop data: ");
29 
30     for (i = 0; i < len; i++) {
31         seqstack_pop(lpHead, &data);
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     LPSEQSTACK seqhead = NULL;
48 
49     seqstack_init(&seqhead);
50 
51     stack_test(seqhead, buf, 5);
52 
53     seqstack_clear(seqhead);
54 
55     return 0;
56 }

Makefile:

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

标签:seqstack,LPSEQSTACK,int,顺序,lpstack,数据结构,data,stack
来源: https://www.cnblogs.com/tinyshark/p/16411650.html

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

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

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

ICode9版权所有