#includeusing namespace std;/**栈 后进先出1, 初始化栈initstack2,入栈push() 3,出栈pop4,显示所有栈元素*/#define MaxSize 100typedef char ElemType;typedef struct { ElemType stack[MaxSize]; int top;} stacktype;void initstack(stacktype *S){ S->top=-1;}void push(stacktype *S , ElemType x){ if(S->top > MaxSize){ cout<<"栈溢出"< top++; S->stack[S->top]=x; }}void pop(stacktype *S){ if(S->top==-1){ cout<<"栈溢出"< top--; }}void display(stacktype *S){ int i; cout<<"栈中的元素:"< top;i>=0;i--){ printf("%C",S->stack[i]); }}void main(){ struct stacktype *st; initstack(st); cout<<"依次插入a,b,c,d"<
#include#include using namespace std;/**栈 链表1, 初始化栈initstack2,入栈push() 3,出栈pop4,显示所有栈元素*/#define MaxSize 100typedef char ElemType;typedef struct linknode{ ElemType data; struct linknode *next;} linkStack;void initstack(linkStack **S){ S=NULL;}void push(linkStack **S , ElemType x){ linkStack *q; q=(linkStack *)malloc(sizeof(linkStack)); q->data=x; q->next=*S; *S=q; }void pop(linkStack **S){ linkStack *t; if(*S==NULL){ cout<<"栈溢出"< next; free(t); }}void display(linkStack **S){ linkStack *q; q=*S; cout<<"栈中的元素:"< data); q=q->next; } }void main(){ linkStack *stack; initstack(&stack); cout<<"依次插入a,b,c,d"<