首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

重温数据结构之5 Stack

2014-06-01 
重温数据结构之五 Stack#include#include#define MAX 5#define OK 1#define NO 0typed

重温数据结构之五 Stack

#include<stdio.h>#include<stdlib.h>#define MAX 5#define OK 1#define NO 0typedef int Type,*TypePoint;typedef struct Stack{Type value[MAX];int first;int last;struct Stack *next;}Stack,*StackPoint,**S;StackPoint stackPoint = 0;void initPoint(S s){(*s) = (StackPoint)malloc(sizeof(Stack));(*s)->first = (*s)->last = 0;(*s)->next = 0;}void pushCheck(S s){if(*s){if(((*s)->last-(*s)->first+MAX)%MAX == 1){initPoint(&stackPoint);stackPoint->next = (*s);(*s) = stackPoint;}return ;}initPoint(s);}void removePoint(S s){stackPoint = (*s);(*s) = stackPoint->next;free(stackPoint);stackPoint = 0;}void popCheck(S s){if(*s){if((*s)->last == (*s)->first){removePoint(s);}}}int push(S s,Type e){pushCheck(s);(*s)->value[(++((*s)->first))%MAX] = e;return OK;}int pop(S s,TypePoint ep){popCheck(s);if(*s){*ep = (*s)->value[(*s)->first--];(*s)->first = ((*s)->first + MAX)%MAX;return OK;}return NO;}void detroyStack(S s){while(*s){removePoint(s);}}int main(){int i = 0,j=0;StackPoint sp = 0;for(;i<16;i++)push(&sp,i);while(pop(&sp,&j)){printf("pop %d\n",j);}detroyStack(&sp);getchar();return 0;}

热点排行