顺序堆栈的操作实现 (1)初始化 StackInitiate(S) void StackInitiate(seqStack"S) S->top=0; (2)非空否 StackNotEmpty(S) int StackNotEmpty (seqStacks if(S top<=Return 0 else return 1
顺序堆栈的操作实现: (1)初始化StackInitiate(S) void StackInitiate(SeqStack *S) { S->top = 0; } (2)非空否StackNotEmpty(S) int StackNotEmpty(SeqStack S) { if(S.top <= 0)return 0; else return 1; }
(3)入栈 StackPush(S,x) int StackPush(SeqStack *S, Data Type x) if(s->top > MaxStackSize) printf("堆栈已满无法插入!Ⅷm"); return else t S->stack[S->top]=x S->top++ return 1;
(3)入栈StackPush(S, x) int StackPush(SeqStack *S, DataType x) { if(S->top >= MaxStackSize) { printf("堆栈已满无法插入!\n"); return 0; } else { S->stack[S->top] = x; S->top ++; return 1; } }
(4)出栈 StackPop(S,d) int StackPop(seqstack"S, DataType *d) t if(s->top<=0 { print"堆栈已空无数据元素出栈!m"); return 0 else S->top--*d=s->stack S->top]; return I
(4)出栈StackPop(S, d) int StackPop(SeqStack *S, DataType *d) { if(S->top <= 0) { printf("堆栈已空无数据元素出栈!\n"); return 0; } else { S->top --;*d = S->stack[S->top]; return 1; } }
(5)取栈顶数据元素 StackTop( Seqstack s, DataType*d) int StackTop(seqstacks, Data Type *d) if(stop<=0) printf("堆栈已空!Ⅶn"); return o e t*d=sstack[S top-1; return 1
(5)取栈顶数据元素StackTop(SeqStack S, DataType *d) int StackTop(SeqStack S, DataType *d) { if(S.top <= 0) { printf("堆栈已空!\n"); return 0; } else { *d = S.stack[S.top - 1]; return 1; } }
测试主程序 任务:建立一个顺序堆栈,首先依次输入数据元素1,2, ······ 10,然后依次出栈堆栈中的数据元素并显示。 假设该顺序堆栈的数据元素个数在最坏情况下不会超过 100个。 #include <stdio. h> #include <stdlib.h> #define maxStackSize 100 typedef int Data Type; #include segstack h
测试主程序: 任务:建立一个顺序堆栈,首先依次输入数据元素1, 2, 3,......,10,然后依次出栈堆栈中的数据元素并显示。 假设该顺序堆栈的数据元素个数在最坏情况下不会超过 100个。 #include <stdio.h> #include <stdlib.h> #defineMaxStackSize 100 typedef int DataType; #include "SeqStack.h