LIFO栈 ADT接口 数组实现
程序员文章站
2022-06-26 20:42:22
LIFO 栈结构 LIFO 栈基本操作 ......
lifo 栈结构
1 typedef int elementype; 2 struct seqstack{ 3 elemetype data[maxsize]; 4 int top; 5 }; 6 typedef struct seqstack* stack;
lifo 栈基本操作
1 //lifo 顺序栈 初始化 2 void initstack(stack s){ 3 s -> top = -1; 4 } 5 6 //lifo 顺序栈判断栈空 7 boolean stackempty(stack s){ 8 if(s -> top == -1) return true; 9 else return false; 10 } 11 12 //lifo 顺序栈判断栈满 13 boolean stackfull(stack s){ 14 if(s -> top == maxsize-1) return true; 15 else return false; 16 } 17 18 //lifo 顺序栈进栈 19 void push(stack s, elemtype x){ 20 if(s->top == maxsize-1){ 21 printf("栈满溢出错误!\n"); 22 exit(1); 23 } 24 s -> top++; 25 s -> data[s>top] = x; 26 } 27 28 //lifo 顺序栈出栈 29 elemtype pop(stack s){ 30 if(stackempty(s){ 31 printf("栈下溢错误!\n"); 32 exit(1); 33 } 34 x = s->data[s->top]; 35 s -> top--; 36 return x; 37 } 38 39 //lifo 顺序栈读取栈顶元素 40 elemtype gettop(stack s){ 41 if(stackempty(s){ 42 printf("下溢错误!\n"); 43 exit(1); 44 } 45 return s -> data[s -> top]; 46 }
下一篇: 是怕你自卑