栈堆的定义与操作(last in,first out list)
1.顺序存储:
struct stack_order{
elementtype data[max];
int max;
int top;
}
typedef struct stack_order* stack;
stack initial(int max){
stack s=(stack)malloc(sizeof(struct stack_order));
s->data[0]=0;
s->max=max;
s->top=-1;
return s;
}
bool isfull(stack s){
return (s->top+1==max);
}
bool push(stack s,elementtype x){
if (isfull(stack s))
return false;
else{
s->data[++(s->top)]=x;
return true;
}
}
bool isempty(stack s){
return (s->top==-1);
}
bool pop(stack s){
if (isempty(s))
return false;
else
return (s->data[(s->top)--]);
}
2.链式存储:
typedef struct stack_chain* stack;
struct stack_chain{
elementtype data;
stack next;
}
stack initial(){
stack s=(stack)malloc(sizeof(struct stack_chain));
s->next=null;
s->data=-1;
return s;
}
bool isempty(stack s){
return (s->next==null);
}
bool push(stack s,elementtype x){
stack p=(stack)malloc(sizeof(struct stack_chain));
p->next=s->next;
p->data=x;
s->next=p;
return true;
}
elementtype pop(stack s){
if (isempty(s))
return false;
else{
stack p=s;
s=p->next;
elementtype temp=p->data;
free(p);
return temp;
}
}