数据结构 --- 链栈(栈的链式存储结构)

Posted luguoshuai

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据结构 --- 链栈(栈的链式存储结构)相关的知识,希望对你有一定的参考价值。

工程目录结构:

技术分享图片

common.h:

 1 //#ifndef __common_h__
 2 //#define __common_h__
 3 
 4 #define OK 1
 5 #define ERROR 0
 6 #define TRUE 1
 7 #define FALSE 0 
 8 
 9 #define MAXSIZE 20
10 
11 typedef int Status;        //函数的返回结果,OK、ERREO、TRUE、FALSE
12 typedef int ElemType;   //结点数据域的数据类型
13 
14 //#endif

common.c:

1 #include "common.h"
2 
3 Status visit(ElemType e)
4 {
5     printf("%d , ", e);
6     return OK;
7 }

LinkStack.h:

 1 //链栈的结点结构
 2 typedef struct StackNode
 3 {
 4     ElemType data;
 5     struct StackNode *next;
 6 }StackNode, *LinkStackPtr;
 7 
 8 //链栈结构
 9 typedef struct
10 {
11     LinkStackPtr top;
12     int count;
13 }LinkStack;

LinkStack.c:

  1 #include <stdio.h>
  2 #include <malloc.h>
  3 #include "common.h"
  4 #include "LinkStack.h"
  5 
  6 //初始化链栈
  7 Status InitLinkStack(LinkStack *S)
  8 {
  9     S->top = (LinkStackPtr)malloc(sizeof(StackNode));
 10     if (!S->top)
 11     {
 12         return ERROR;
 13     }
 14 
 15     S->top = NULL;
 16     S->count = 0;
 17 
 18     return OK;
 19 }
 20 
 21 Status ClearLinkStack(LinkStack *S)
 22 {
 23     LinkStackPtr p,q;
 24     p = S->top;
 25     while (p)
 26     {
 27         q = p;
 28         p = p->next;
 29         free(q);
 30         --S->count;
 31     }
 32         
 33     return OK;
 34 }
 35 
 36 //判断链栈是否为空
 37 Status LinkStackEmpty(LinkStack S)
 38 {
 39     if (0 == S.count)
 40         return TRUE;
 41     else
 42         return FALSE;
 43 }
 44 
 45 //压栈
 46 Status LinkStackPush(LinkStack *S, ElemType e)
 47 {
 48     LinkStackPtr prt = (LinkStackPtr)malloc(sizeof(StackNode));
 49     prt->data = e;
 50     prt->next = S->top;
 51     S->top = prt;
 52     ++S->count;
 53     return OK;
 54 }
 55 
 56 //弹栈
 57 Status LinkStackPop(LinkStack *S, ElemType *e)
 58 {
 59     if (LinkStackEmpty(*S))
 60         return ERROR;
 61     LinkStackPtr ptr = S->top;
 62     *e = ptr->data;
 63     S->top = S->top->next;
 64     free(ptr);
 65     --S->count;
 66     return OK;
 67 }
 68 
 69 //获取栈顶元素的值
 70 Status GetLinkStackTop(LinkStack S, ElemType *e)
 71 {
 72     if (NULL == S.top)
 73         return ERROR;
 74     else
 75     {
 76         *e = S.top->data;
 77     }
 78     return OK;
 79 }
 80 
 81 //返回链栈的长度
 82 int LinkStackLength(LinkStack S)
 83 {
 84     return S.count;
 85 }
 86 
 87 Status LinkStackTraverse(LinkStack S)
 88 {
 89     LinkStackPtr p;
 90     p = S.top;
 91     while (p)
 92     {
 93         visit(p->data);
 94         p = p->next;
 95     }
 96     return OK;
 97 }
 98 
 99 void LinkStackStart()
100 {
101     printf("
######## 链栈 测试 Start ############
");
102 
103     LinkStack S;
104     if (OK == InitLinkStack(&S))
105     {
106         printf("
初始化链栈成功,Count = %d
", S.count);
107     }
108     else
109     {
110         printf("
初始化链栈失败!
");
111     }
112 
113     if (TRUE == LinkStackEmpty(S))
114     {
115         printf("
链栈为空
");
116     }
117     else
118     {
119         printf("
链栈不为空,Count = %d
", S.count);
120     }
121 
122     printf("
压栈元素(1-5)
");
123     for (int i = 1; i <= 5; ++i)
124         LinkStackPush(&S, i);
125 
126     if (TRUE == LinkStackEmpty(S))
127     {
128         printf("
链栈为空
");
129     }
130     else
131     {
132         printf("
链栈不为空,Count = %d
", S.count);
133     }
134 
135     printf("
链栈中的元素一次是:
");
136     LinkStackTraverse(S);
137 
138     ElemType e;
139     LinkStackPop(&S, &e);
140 
141     printf("

弹出栈顶元素: %d", e);
142 
143     GetLinkStackTop(S, &e);
144     printf("

现在的栈顶元素为: %d
", e);
145 
146     ClearLinkStack(&S);
147     printf("
清空链栈链栈,Count = %d
", S.count);
148 
149     printf("

######## 链栈 测试 End ############
");
150 }

main.c:

1 int main()
2 {
3     /*LinkListStart();
4     StackStart();*/
5     LinkStackStart();
6     getchar();
7     return 0;
8 }

 

以上是关于数据结构 --- 链栈(栈的链式存储结构)的主要内容,如果未能解决你的问题,请参考以下文章

数据结构栈的链式存储结构

数据结构 --- 链栈(栈的链式存储结构)

栈的链式存储结构及应用(CJava代码)

数据结构0404

栈的链式存储结构

Java栈之链式栈存储结构实现