资源描述:
《软件技术基础_栈和队列_上机报告》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、11姓名:阚姗蕾学号:2010012030037上机实验三ex3_1:一、程序流程说明链栈1)链栈结点类型定义为:typedefstructnode{intdata;structnode*next;}node_type;2)编写进栈函数push3)编写出栈函数pop4)编写main函数,首先建立一空链栈;调用进栈函数,将从键盘输入的数据元素逐个进栈,输入0结束;显示进栈后的数据元素;调用两次出栈函数,显示出栈后的数据元素。二、程序代码#include#include//定义链栈typedefstructnode_type{intd
2、ata;structnode_type*next;}node_type;typedefstructstack_type{node_type*top;intlength;}stack_type;//进栈函数voidpush(stack_type*s,intnewnode)20currencydeposit,weprescribeapassonaregularbasis,qilucardaccountonaregularbasis),certificatebondsandsavingsbonds(electronic);3.notdrawnonabanksavingsce
3、rtificate,certificatebondsapplyformortgageloans,acceptingonlythelender11{node_type*p;p=(node_type*)malloc(sizeof(node_type));p->data=newnode;p->next=s->top;s->top=p;s->length++;}//出栈函数intpop(stack_type*s){node_type*p;intx;if(s->top==NULL){printf("ThestackisNULL!");return(0);}else{x=s-
4、>top->data;p=s->top;s->top=s->top->next;free(p);s->length--;return(x);}}//遍历并输出voidshowstack(stack_type*s){inti=0;intle;stack_type*s1;node_type*p;if(s->length<=0){printf("Nodata!");return;}20currencydeposit,weprescribeapassonaregularbasis,qilucardaccountonaregularbasis),certificatebon
5、dsandsavingsbonds(electronic);3.notdrawnonabanksavingscertificate,certificatebondsapplyformortgageloans,acceptingonlythelender11s1->length=0;le=s->length;while(itop->data);i++;}while(i--)//返回原栈push(s,pop(s1));}intmain(){intnum;stack_types
6、tack;printf("insert:number:");stack.length=0;while(scanf("%d",&num)&&num!=0)push(&stack,num);printf("Thelengthofthestackis:%d",stack.length);printf("stackafterpush:");showstack(&stack);printf("Thelengthofthestackis:%d",stack.length);printf("Thefirstnumbertopop");printf
7、("%d",pop(&stack));printf("Thesecondnumbertopop");printf("%d",pop(&stack));printf("stackafterpop:");return0;}三、输入与输出insert:number:123450Thelengthofthestackis:5stackafterpush:54321Thelengthofthestackis:520currencydeposit,weprescribeapassonaregularbasis,qilucardaccounto