1、Now ,those records are:n); p=head; if(head!=NULL) do printf(%4d,p-data); p=p-next; while(p!=NULL); int push(lstack_type *lp, int x) node_type *p; p=(node_type *)malloc(LEN); if(p! p-data = x; p-next = lp-top;lp- top = p; lp-length+; return(1); else return(0); int pop(lstack_type *lp)node_type *p; in
2、t x;if(lp-top=NULL)printf(stack is underflowreturn(0); elsex = lp-top-data;p = lp-top = lp-length-;free(p);return(true); void main() lstack_type *p1; int a=0,b=0,m=0; p1=create_list();n input the numbers: scanf(%d,&m); while(m!=0) push(p1,m); scanf( print(p1-top); pop(p1); pop(p1); print(p1-三:典型测试数据
3、(输入):2 4 6 9 43 应输出(上机前自己分析的结果):43 9 6 4 2出栈后6 4 2四:上机时遇到的问题: 不能输出 解决办法:更改函数返回类型 编译出错 解决办法:输入数据时“&”不要忘五:实际运行结果 六 、小结&体会 链栈的使用很方便,比顺序栈更好操作。题目二:一、程序流程说明(1)创建一个循环队列(2)编写出队列函数,入队列函数,和负数删除函数(3)编写主函数#includefront=0;lp-rear=0;n please input datas of the listn lp-data0=NULL ; for(i=1; idatai=elem;rear+;void
4、 printlist(queue_type *lp) int i,j=0;nThese records are: if(lp-rearfront+1;=lp-rear; printf( %4d , lp-datai);void enqueue(queue_type *q, int x) q-rear=(q-rear+1)%N;dataq-rear=x; int dequeue(queue_type *q)int i;i=q-front; q-front=(q-front+1)%N;/* printlist(q);*/ return (q-datai+1);void aa(queue_type
5、*lp) int i,l=lp-rear,j;printf(long=%4d,lp-rear);for(i=lp-datalp-front+1;i0)enqueue(lp,j);main() queue_type p; create_list(&p); printlist(& aa(& printf(after deletete list is: printlist(&三、测试数据* 输入:2 3 -4 6 -5 8 -9 7 -10 20 (-1)应输出:2 3 6 8 7 20负数不能完全删除 解决办法:更改循环,使每一个数都能与0比较,是负数则删除(调用出队列函数)五、实际运行结果: 循
6、环队列的使用能是空间更加合理的被利用。题目三:(1)编写两个pop函数,两个pus函数 (2)编写主函数#define M 10#define true 1#define false 0typedef struct int dataM; int top1,top2;stack_type;int push1(stack_type *s)int x;请输入数据(以0结束):scanf(x);while(x!=0&s-top1top2) s-datas-top1 = x; s-top1 = s-top1+1; scanf( if(s-top1=s- printf(栈已满! return;int pu
7、sh2(stack_type *s)top2 = x;top2= s-top2-1;int pop1(stack_type *s)int out;if(s-top1 top1-1; s-top1-1; return(out);int pop2(stack_type *s)top2 =M) return(false);top2+1;top2+1;int i,j,k; stack_type as,*lp;lp=&as;lp-top1=0;top2=M-1;while(1)请选择你要操作的栈:输入“1”或“2”n返回“0”n scanf(i); if(i=1) printf(请选择你要进行的操作:入栈“1”;出栈“2”n scanf(k); switch(k) case 1:push1(lp); break; case 2:j=pop1(lp); printf(出栈元素为:%dn,j); default:printf(输入错误!
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1