1、栈代码实现/定义顺序表:#include #define MAXLEN 10typedef int ElementType;typedef struct ElementType dataMAXLEN;int top;SqStack;/建空栈SqStack InitStack() SqStack s;s.top=-1;return(s);/取栈顶元素,并用*x 返回栈顶元素int GetTopStack(SqStack *s,ElementType *x)if(s-top=-1)return(0);else *x=s-datas-top; return(1);/输出栈void PrintStac
2、k(SqStack s)int i;if(s.top!=-1)printf(“输出栈内的元素为:“);for(i=0;itop=MAXLEN-1)return(0);s-top+;s-datas-top=x;return(1);/出栈,删除 s 的栈顶元素,并用*x 返回栈顶元素int PopStack(SqStack *s,ElementType *x) if(s-top=-1)return(0);*x=s-datas-top;s-top-;return(1);/判空栈int IsEmptyStack(SqStack *s) return(s-top=-1);/主程序int main() S
3、qStack s;int i ;ElementType y;ElementType z;s=InitStack();printf(“请输入个数:n“);for(i=1;i#include “malloc.h“ /使用 malloc 标识符必须加此代码typedef int ElementType;typedef struct nodeElementType data;struct node *next;NODE;/建一个空栈NODE *CreateSqStackLink()NODE *s;s=(NODE *)malloc(sizeof(NODE);s-next=0;return(s);/进栈v
4、oid PushSqStackLink(NODE *s,ElementType x)NODE *p;p=(NODE *)malloc(sizeof(NODE);p-data=x;p-next=s-next;s-next=p;/输出栈void PrintSqStackLink(NODE *s)NODE *p;p=s-next;if(p!=NULL)printf(“输出栈的元素为:“);while(p!=NULL)printf(“%3d“,p-data);p=p-next;elseprintf(“此栈为空!“);printf(“n“); /出栈int PopSqStackLink(NODE *s)
5、ElementType t;NODE *p;p=s-next;if(p=0)return(0);elses-next=p-next;t=p-data;free(p);return(t);/主程序void main()int i,j;ElementType a;NODE *s;s=CreateSqStackLink();printf(“请输入进栈元素个数:“);scanf(“%d“,for(j=1;j=i;j+)printf(“请输入第%d 个数:“,j);scanf(“%d“,PushSqStackLink(s,a);PrintSqStackLink(s);a=PopSqStackLink(s);printf(“出栈元素为:%d“,a);