码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-07-02 00:04:39      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:函数   efi   ini   struct   include   分享   false   ++   oid   

工程目录结构:

技术分享图片

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("\n######## 链栈 测试 Start ############\n");
102 
103     LinkStack S;
104     if (OK == InitLinkStack(&S))
105     {
106         printf("\n初始化链栈成功,Count = %d\n", S.count);
107     }
108     else
109     {
110         printf("\n初始化链栈失败!\n");
111     }
112 
113     if (TRUE == LinkStackEmpty(S))
114     {
115         printf("\n链栈为空\n");
116     }
117     else
118     {
119         printf("\n链栈不为空,Count = %d\n", S.count);
120     }
121 
122     printf("\n压栈元素(1-5)\n");
123     for (int i = 1; i <= 5; ++i)
124         LinkStackPush(&S, i);
125 
126     if (TRUE == LinkStackEmpty(S))
127     {
128         printf("\n链栈为空\n");
129     }
130     else
131     {
132         printf("\n链栈不为空,Count = %d\n", S.count);
133     }
134 
135     printf("\n链栈中的元素一次是:\n");
136     LinkStackTraverse(S);
137 
138     ElemType e;
139     LinkStackPop(&S, &e);
140 
141     printf("\n\n弹出栈顶元素: %d", e);
142 
143     GetLinkStackTop(S, &e);
144     printf("\n\n现在的栈顶元素为: %d\n", e);
145 
146     ClearLinkStack(&S);
147     printf("\n清空链栈链栈,Count = %d\n", S.count);
148 
149     printf("\n\n######## 链栈 测试 End ############\n");
150 }

main.c:

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

 

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

标签:函数   efi   ini   struct   include   分享   false   ++   oid   

原文地址:https://www.cnblogs.com/luguoshuai/p/9251600.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!