用数组实现栈
来源:互联网 发布:代码数据图 编辑:程序博客网 时间:2024/05/22 20:07
在ubuntu上经过gcc验证:
一、头文件:hangma@ubuntu:~/test/test/protest/stack_test$ cat my_stack.h
#ifndef _MY_STACK_#define _MY_STACK_struct stackrecord;typedef struct stackrecord* stack;typedef int ElementType;int IsEmpty(stack s);int IsFull(stack s);stack CreateStack(int MaxElements);void DisposeStack(stack s);void MakeEmpty(stack s);void Push(ElementType x,stack s);ElementType Top(stack s);void Pop(stack s);ElementType TopAndPop(stack s);#define EMPTY 0#define MinStackSize (5)struct stackrecord { int capacity; int size; ElementType *array;};#endif
二、C文件:
#include <stdio.h>#include <stdlib.h>#include "my_stack.h"int IsEmpty(stack s){ return (s->size == EMPTY);}int IsFull(stack s){ return s->size == s->capacity;}stack CreateStack(int MaxElements){ if(MaxElements < MinStackSize) {printf("the stack size is too small\n");exit(-1); } stack s; s = (stack)malloc(sizeof(struct stackrecord)); if(s == NULL) {printf("can't allocate the stack \n");exit(-2); } s->array = (ElementType *)malloc(MaxElements * sizeof(ElementType)); if(s->array == NULL) {printf("allocate memory error\n");exit(-3); } s->capacity = MaxElements; MakeEmpty(s); return s;}void DisposeStack(stack s){ if(s != NULL) {if(s->array != NULL) free(s->array);free(s); }}void MakeEmpty(stack s){ s->size = EMPTY;}void Push(ElementType x,stack s){ if(s == NULL) {printf("the stack is not exsit\n");exit(-4); } if(IsFull(s)) {printf("the stack is full\n");exit(-5); } s->array[s->size] = x; s->size++;}ElementType Top(stack s){ if(s == NULL) {printf("the stack is not exsit\n");exit(-7); } if(IsEmpty(s)) {printf("the stack is empty\n");exit(-8); } return s->array[s->size-1];}void Pop(stack s){ if(s == NULL) {printf("s is not exsit\n");exit(-6); } if(IsEmpty(s)) {printf("s is empty\n");exit(-7); } s->size--;}ElementType TopAndPop(stack s){ if(s == NULL) {printf("s is not exsit\n");exit(-6); } if(IsEmpty(s)) {printf("s is empty\n");exit(-7); } s->size--; return s->array[s->size];}int main(int argc,char *argv[]){ stack s; s = CreateStack(10); int i = 0; while(++i <= 10) {Push(i,s);printf("push the data:%d into the stack,the stack size is %d\n",i,s->size); } int x; while(s->size) {x = TopAndPop(s);printf("the top of stack is %d\n",x); sleep(1); } DisposeStack(s);}
三、打印输出
hangma@ubuntu:~/test/test/protest/stack_test$ ./my_stack push the data:1 into the stack,the stack size is 1push the data:2 into the stack,the stack size is 2push the data:3 into the stack,the stack size is 3push the data:4 into the stack,the stack size is 4push the data:5 into the stack,the stack size is 5push the data:6 into the stack,the stack size is 6push the data:7 into the stack,the stack size is 7push the data:8 into the stack,the stack size is 8push the data:9 into the stack,the stack size is 9push the data:10 into the stack,the stack size is 10the top of stack is 10the top of stack is 9the top of stack is 8the top of stack is 7the top of stack is 6the top of stack is 5the top of stack is 4the top of stack is 3the top of stack is 2the top of stack is 1
- 用数组实现栈
- 用数组实现栈
- 用数组实现栈
- 用数组实现栈
- java 用数组实现栈
- Java用数组实现栈
- 数据结构-用数组实现栈
- java用数组实现栈
- 栈 stack 用数组实现
- 用java数组实现栈
- 自己实现的栈 用数组实现
- java实现_用数组实现栈
- 栈(2)----用数组实现栈
- 用数组实现栈的基本操作
- 用静态数组实现栈数据结构
- Java用数组实现顺序栈
- 用一个数组实现三个栈
- 用数组实现栈的方法
- oracle rownum orderby
- linux下apk批量打包
- Android 模拟系统事件(一)
- 【南京】根据销售订单创建交货单并自动拣配、过账发货程序
- int *p=NULL和*p= NULL有什么区别
- 用数组实现栈
- Unity3D的世界坐标转化成屏幕坐标
- 书--益友--从不孤单
- linux的mount(挂载)命令详解
- CAS单点登录登出原理
- 调用打电话,发短信,safari
- dedecms 会员登录积分每天只限增加一次的方法
- Linux命令简单汇总
- ios实现播放器的后台运行,发送消息