The some code in Data book (5th Edition) from the 81 page to 82 page
Update completed
#define MaxSize 50
typedef int ElemType;
typedef struct{
ElemType data[MaxSize];
int top; //Sequential stack top of the stack pointer
} SqStack;
//Initialization Sequential stack
void InitStack(SqStack *&s){
s=(SqStack *)malloc(sizeof(SqStack));
s->top=-1;
}
//Create Sequential stack
void CreateStack(SqStack *&s, int a[], int n) {
s = (SqStack *)malloc(sizeof(SqStack));
s->top = -1;
for (int i = 0; i < n; ++i) {
s->data[i] = a[i];
}
s->top = n-1;
}
//Destroyed Sequential stack
void DestroyStack(SqStack *&s){
free(s);
}
//Determine if the Sequential stack is empty
bool StackEmpty(SqStack *s){
return(s->top==-1);
}
//Into stack
bool Push(SqStack *&s, ElemType e){
if (s->top==MaxSize-1){//Processing overflow
return false;
}
s->top++;//Top of the stack add 1
s->data[s->top]=e;//Put element e on the top of the stack pointer
return true;
}
//Out stack
bool Pop(SqStack *&s, ElemType &e){
if (s->top==-1)
return false;
e=s->data[s->top];
s->top--;
return true;
}
//Get length of sequential stack
int StackLength(SqStack *s) {
return s->top + 1;
}
//Output sequential stack
void ShowStack(SqStack *s) {
if (s->top == -1) {
printf("\n");
return;
}
for (int i = 0; i <= s->top; ++i) {
printf("%d ", s->data[i]);
}
printf("\n");
}
如有侵权,请联系作者删除