测试环境:Win - TC
- #include <stdio.h>
- char stack[512];
- int top=0;
- void push(char c)
- {
- stack[top]=c;
- top++;
- }
- char pop()
- {
- top--;
- return stack[top];
- }
- int is_empty()
- {
- return 0==top;
- }
- void main()
- {
- push('1');
- push('2');
- push('3');
- push('4');
- push('5');
- while(!is_empty())
- putchar(pop());
- putchar('/n');
- getch();
- }
运行结果:
====================================================
栈——数组实现2
测试环境:Win - TC
- #include <stdio.h>
- #include <malloc.h>
- /* typedef int DataType; */
- #define DataType int
- #define MAX 1024
- typedef struct
- {
- DataType data[MAX];
- int top;
- }stack, *pstack;
- pstack *init_stack()
- {
- pstack ps;
- ps=(pstack)malloc(sizeof(stack));
- if(!ps)
- {
- printf("Error. fail malloc.../n");
- return NULL;
- }
- ps->top=-1;
- return ps;
- }
- int empty_stack(pstack ps)
- {
- if(-1 == ps->top)
- return 1;
- else
- return 0;
- }
- int push(pstack ps, DataType data)
- {
- if(ps->top == MAX-1)
- {
- printf("Stack is full.../n");
- return 0;
- }
- ps->top++;
- ps->data[ps->top]=data;
- return 1;
- }
- int pop(pstack ps, DataType *data)
- {