栈的链表实现
2015-09-23 11:27
232 查看
声明 stack.h:
实现 implementation.c:
测试 main.c:
#ifndef STACK_H_INCLUDED #define STACK_H_INCLUDED struct Node; typedef struct Node *PtrToNode; typedef PtrToNode Stack; int IsEmpty(Stack S); Stack CreateStack(void); void DisposeStack(Stack S); void MakeEmpty(Stack S); void Push(int X, Stack S); int Top(Stack S); void Pop(Stack S); #endif // STACK_H_INCLUDED
实现 implementation.c:
#include<stdio.h> #include "stack.h" struct Node{ int Num; PtrToNode Next; }; int IsEmpty(Stack S) { return S->Next == NULL; } Stack CreateStack(void) { Stack S; S = malloc(sizeof(struct Node)); if(S == NULL){ printf("Error!"); } S->Next = NULL; MakeEmpty(S); return S; } void MakeEmpty(Stack S) { if(S == NULL) printf("Must use CreateStack() frist"); while(!IsEmpty(S)){ Pop(S); } } void Push(int X, Stack S) { PtrToNode TmpCell; TmpCell = malloc(sizeof(struct Node)); if(TmpCell == NULL) printf("Out of space!"); else{ TmpCell->Next = S->Next; TmpCell->Num = X; S->Next = TmpCell; } } void Pop(Stack S) { PtrToNode FirstCell; if(IsEmpty(S)) printf("Empty Stack!"); else{ FirstCell = S->Next; S->Next = S->Next->Next; free(FirstCell); } } int Top(Stack S) { if(!IsEmpty(S)){ return S->Next->Num; } else { printf("Empty Stack"); } }
测试 main.c:
#include <stdio.h> #include <stdlib.h> #include "stack.h" int main() { Stack S; S = CreateStack(); Push(1, S); Push(2, S); Push(3, S); printf("%d ", Top(S)); Pop(S); printf("%d",Top(S)); return 0; }
相关文章推荐
- UIActivityIndicatorView的使用
- java 随机获取国内IP
- 多线程学习网站,比较详细
- windows安装docker
- 排序算法 面试 JAVA
- rsync命令
- opencv学习笔记(十)-图像腐蚀与腐蚀
- android二维码的生成与解析
- c语言学习笔记(4)struct和union:柔性数组存储斐波那契数列以及union判断系统大小端
- [进化算法] 随机Q-竞争选择法
- spring学习历程 之 事件(二)
- HTTP 错误 404.3 - Not Found(WCF+ASP.NET)
- sublime text 2 更新后emmet插件tab键无法使用的解决办法
- Unique path
- proftpd的示例配置文件
- Java相似图片搜索算法之"感知哈希算法"实例
- Centos6.5 tomcat8 基本安装(适合初学者)
- shell的学习笔记——变量的设置规则
- Thinking in java:泛型
- Java基础教程汇总