Last active
December 25, 2015 08:49
-
-
Save tinylamb/6949752 to your computer and use it in GitHub Desktop.
ADT stack
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #define ElemType int | |
| #define MAXSIZE 100 | |
| /*...ADT stack...*/ | |
| /*数据集定义*/ | |
| typedef struct Stack{ | |
| int stacksize; | |
| int top;//top pointer | |
| ElemType *stack; | |
| }Stack; | |
| /*Attribution*/ | |
| void InitStack(Stack *s){ | |
| s->stacksize=MAXSIZE; | |
| s->stack=(ElemType*)malloc(s->stacksize*sizeof(ElemType)); | |
| s->top=0; | |
| } | |
| int IsEmpty(Stack *s){ | |
| return (s->top==0)?1:0; | |
| } | |
| void Push(Stack *s,ElemType e){ | |
| if(s->top==MAXSIZE-1) | |
| printf("error:stack full\n"); | |
| else | |
| s->stack[s->top++]=e; | |
| } | |
| ElemType Pop(Stack *s){ | |
| if(!IsEmpty(s)) | |
| return s->stack[--s->top]; | |
| else | |
| printf("error:empty stack\n"); | |
| } | |
| void Clear(Stack *s){ | |
| s->top=0; | |
| } | |
| void PrintStack(Stack *s){ | |
| if(!IsEmpty(s)){ | |
| int i=0; | |
| while(i<s->top) | |
| printf("%d ",s->stack[i++]); | |
| printf("\n"); | |
| } | |
| } | |
| /*test*/ | |
| int main(){ | |
| Stack s; | |
| //test init | |
| InitStack(&s); | |
| if(IsEmpty(&s)) | |
| printf("stack is empty\n"); | |
| //test push | |
| int i=0; | |
| while(i<5){ | |
| Push(&s,i); | |
| i++; | |
| } | |
| PrintStack(&s); | |
| //test pop | |
| i=Pop(&s); | |
| PrintStack(&s); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment