不BB直接贴代码:
#include#include #include #define maxsize 100 typedef int datatype; typedef struct stack{ datatype data[maxsize]; int top; }seqstack; void Initstack(seqstack *s){//初始化栈 s->top = 0; } int Empty(seqstack *s){//判断栈空 if(s->top <= 0 ) return 1; else return 0; } void Push(seqstack *s,datatype x){//入栈 if(s->top >= maxsize-1){ printf("stack is fulln"); return ; } else{ s->data[++s->top] = x; } } datatype Pop(seqstack *s){//出栈 if(Empty(s)){ printf("stack is emptyn"); return 0; } else{ return s->data[s->top--]; } } void Print(seqstack *s){//打印栈中元素 while(!Empty(s)){ printf("%d ",s->data[s->top--]); } printf("n"); } void Delete(seqstack *s,datatype m){//删除元素 seqstack *p; p = (seqstack*) malloc(sizeof(seqstack)); p->top = 0; for(int i=s->top;i!=0;i--){ if(s->data[i] != m){ Push(p,s->data[i]); } } Initstack(s); while(p->top != 0){ Push(s,p->data[p->top--]); } } int main(){ seqstack *s,*p; datatype m; datatype a[8] = {1,2,3,4,5,6,7,8}; s = (seqstack*) malloc(sizeof(seqstack)); Initstack(s); for(int i=0;i<8;i++){ Push(s,a[i]); } printf("stack has been created,print s:"); Print(s); for(int i=0;i<8;i++){ Push(s,a[i]); } printf("put m:"); scanf("%d",&m); printf("s after delete:"); Delete(s,m); Print(s); return 0; }



