第4章栈和队列4.3栈和队列算法实现的C语言源程序4.3.1栈的顺序存储结构及实现#include"stdio.h"#include"stdlib.h"#defineMAXSIZE100typedefintElemType;typedefstruct{ElemTypeelem[MAXSIZE];inttop;}SqStack;/*顺序栈的类型标识符*/第4章栈和队列voidOutStack(SqStackp);/*此处参数必须与下边函数说明一致*/voidInitStack(SqStack*p);voidPush(SqStack*p,ElemTypex);ElemTypePop(SqStack*p);ElemTypeGetTop(SqStackp);第4章栈和队列main(){SqStackq;inti,y,cord;ElemTypea;do{printf("\n");printf("\n主菜单\n");printf("\n1初始化顺序栈\n");printf("\n2插入一个元素\n");printf("\n3删除栈顶元素\n");printf("\n4取栈顶元素\n");printf("\n5结束程序运行\n");printf("\n---------------------------\n");printf("请输入您的选择(1,2,3,4,5)");scanf("%d",&cord);switch(cord)第4章栈和队列{case1:{InitStack(&q);OutStack(q);}break;case2:{printf("\n请输入插入的数据a=");scanf("%d",&a);Push(&q,a);OutStack(q);}break;case3:{a=Pop(&q);printf("\na=%d",a);OutStack(q);}break;case4:{y=GetTop(q);printf("\ny=%d",y);OutStack(q);}break;第4章栈和队列case5:exit(0);}}while(cord<=5);}/*mainend*/voidInitStack(SqStack*p){p->top=0;}voidPush(SqStack*p,ElemTypex){if(p->toptop=p->top+1;p->elem[p->top]=x;}elseprintf("\nOverflow!");}第4章栈和队列ElemTypePop(SqStack*p){ElemTypex;if(p->top!=0){x=p->elem[p->top];p->top=p->top-1;return(x);}else{printf("\nUnderflow!");return(-1);}}第4章栈和队列ElemTypeGetTop(SqStackp){ElemTypex;if(p.top!=0){x=p.elem[p.top];return(x);}else{printf("\nUnderflow!");return(-1);}}第4章栈和队列voidOutStack(SqStackp){inti,j;if(p.top==0)printf("\nTheStackisNULL.");for(i=p.top;i>0;i--)printf("\n%2d%6d",i,p.elem[i]);}第4章栈和队列在上述程序中既包括了每个算法对应的函数代码,也包括在主函数之中对函数的调用。应该注意的是输出函数和取栈顶数据(但是不出栈)函数中形参SqStackp不使用指针,再看主函数的调用语句的实参q不用求地址。而进栈、出栈和初始化栈的函数形参SqStack*p使用指针,在主函数中调用语句的实参&q需将地址代入。第4章栈和队列4.3.2队列的链表存储结构及实现#include"stdio.h"#include"stdlib.h"#defineElemTypeinttypedefstructNodeType/*数据元素结点的结构*/{ElemTypedata;structNodeType*next;}NodeType;typedefstruct/*队列头、尾指针被封装在一起*/{NodeType*front,*rear;}LinkQueue;/*队列头尾指针结构体*/第4章栈和队列NodeType*p,*s,*h;voidoutlin(LinkQueueqq);/*参数要与下面的函数说明一致*/voidcreat(LinkQueue*qe);voidinsert(LinkQueue*qe,ElemTypex);ElemTypedelete(LinkQueue*qe);main(){LinkQueueque;ElemTypey,x;inti,x,y,cord;do{printf("\n主菜单\n");printf("1建立链表队列\n");printf("2入队一个元素X\n");printf("3出队一个元素\n");printf("4结束程序运行\n");第4章栈和队列printf("-----------------------------\n");printf("请输入您的选择(1,2,3,4)");scanf("%d",&cord);switch(cord){case1:{creat(&que);/*重要,这是头尾指针结点*/outlin(que);}break;case2:{printf("\nx=?");scanf("%d",&x);insert(&que,x);outlin(que);}break;case3:{y=delete(&que);printf("\nx=%d",y);outlin(que);}break;case4:exit(0);}}while(cord<=4);}/*mainend*/第4章栈和队列voidoutlin(LinkQueueqq){p=qq.front->next;/*指向第一个数据元素结点*/while(p!=NULL){printf("data=%4d\n",p->data);p=p->next;}printf("\noutend\n\n");}voidinsert(LinkQueue*qe,intx){/*值为x的结点入队*/s=(NodeType*)malloc(sizeof(NodeType));s->data=x;s->next=NULL;qe->rear->next=s;qe->rear=s;}第4章栈和队列ElemTypedelete(LinkQueue*qe){ElemTypex;if(qe->front==qe->rear){printf("队列为空。\n");x=0;}else{p=qe->front->next;qe->front->next=p->next;if(p->next==NULL...