1、数据结构栈栈:#includetypedef int Elem; #define N 100/*N是栈的足够的空间*/ Elem stackN; int top;/*栈顶指针,当栈为空时top=1,即top指向栈顶元素的上一个元素*/ /*定义栈的一般方法*/ void Init_Stack()/*栈的初始化*/ top=1;/*清空栈*/ int IsEmpty()/*判断栈是否为空*/ if (top=N)return 1; else return 0; void Push_Stack(Elem x)/*入栈函数*/ if (IsFull()printf(Stack is full,can
2、not push element!);exit(0); stacktop=x; top+; Elem Pop_Stack()/*出栈函数*/ Elem ret; if(IsEmpty()printf(Stack is empty,cannot pop element!n);exit(0); top-; ret=stacktop; return ret; main()/*用作测试*/ int i=0; Init_Stack(); for (i=0;i10;i+) Push_Stack(i); for (i=0;i10;i+) printf(%4d,Pop_Stack(); 栈的逆转程序:(正确的
3、)( #include #define MAXN 26int stackMAXN;int top=0;int sMAXN; main()int j,k,i;printf(nInput the number:n);for (k=0;k10;k+)scanf(%d,&sk);printf(nThe number is the group are:n);for (k=0;k10;k+)printf(%d ,sk);for (k=0;k=0;j-)printf(%d ,stackj);int push(x)int x;if (top=MAXN)return (1);stacktop+=x;return
4、 (0);运行结果:Input the number:0 1 2 3 4 5 6 7 8 9The number is the group are:0 1 2 3 4 5 6 7 8 9The order poped from the stack is:9 8 7 6 5 4 3 2 1 0/*迷宫游戏*/ /*进入tc运行游戏,按上下左右键控制,任何时候按ESC键退出*/ #include #include #include #include #defineLEFT120 #defineTOP40 #defineRIGHT520 #defineBOTTOM440 #defineSMALL20
5、 intb2020;/*用来保存地图信息*/ b2020=1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,1,1,1,1, 1,0,1,0,0,1,0,0,1,0,1,0,1,1,0,1,0,0,0,1, 1,0,1,0,1,1,0,1,1,0,1,0,1,0,0,1,0,1,0,1, 1,0,0,0,0,1,0,0,1,0,0,0,1,0,1,1,0,1,0,1, 1,1,1,0,1,1,1,0,1,1,1,1,0,0,1,0,0,1,0,1, 1,1,0,0,1,1,0,0,1,0,0,1,0
6、,1,1,0,1,0,0,1, 1,0,0,1,1,0,0,1,0,0,1,0,0,1,0,0,1,0,1,1, 1,0,1,1,0,0,1,1,0,1,1,0,1,1,0,1,1,0,1,1, 1,0,1,0,0,1,1,0,0,1,0,0,1,1,0,0,1,0,1,1, 1,0,0,0,1,1,0,1,0,0,0,1,1,0,1,0,1,0,0,1, 1,0,1,1,1,0,0,1,1,1,0,1,1,0,1,0,1,1,0,1, 1,0,0,1,0,0,1,0,1,0,0,1,0,0,0,0,1,0,0,1, 1,1,0,1,1,0,1,0,0,1,0,1,1,1,0,1,1,0,1,
7、1, 1,0,0,0,1,0,1,1,0,1,0,1,0,0,0,1,0,0,0,1, 1,0,1,0,1,0,0,0,0,1,0,0,0,1,1,0,0,1,0,1, 1,0,1,0,1,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1, 1,0,1,1,0,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1, 1,0,0,0,0,1,0,1,1,0,0,0,0,1,1,0,1,0,0,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1; voiddrawcake(intx,inty)/*划方块*/ setfillstyle(SOLID_F
8、ILL,BLUE); bar(LEFT+1+x*SMALL,TOP+1+y*SMALL,RIGHT-1-19*SMALL+x*SMALL,BOTTOM-1-19*SMALL+y*SMALL); voiddraw(intx,inty)/*划小方块*/ setfillstyle(SOLID_FILL,RED); bar(LEFT+5+x*SMALL,TOP+5+y*SMALL,RIGHT-5-19*SMALL+x*SMALL,BOTTOM-5-19*SMALL+y*SMALL); voidundraw(intx,inty)/*擦去小方块*/ setfillstyle(SOLID_FILL,BLAC
9、K); bar(LEFT+5+x*SMALL,TOP+5+y*SMALL,RIGHT-5-19*SMALL+x*SMALL,BOTTOM-5-19*SMALL+y*SMALL); main() intgr=DETECT,gm,k=0,i,j,x=1,y=1,ch; floatf; initgraph(&gr,&gm,); cleardevice(); setbkcolor(0); for(i=0;i20;i+) for(j=0;j20;j+) if(bij=1)drawcake(j,i); draw(x,y); do ch=bioskey(0); switch(ch) case19200:/*
10、按向左键*/ if(byx-1=0) undraw(x,y); byx=0; x-; draw(x,y); byx=2; break; case19712:/*按向右键*/ if(byx+1=0) undraw(x,y); byx=0; x+; draw(x,y); byx=2; break; case18432:/*按向上键*/ if(by-1x=0) undraw(x,y); byx=0; y-; draw(x,y); byx=2; break; case20480:/*按向下键*/ if(by+1x=0) undraw(x,y); byx=0; y+; draw(x,y); byx=2;
11、 break; if(b1818=2)ch=283; while(ch!=283); if(b1818=2)printf(恭贺你成功地走出了迷宫!); getch(); 用栈实现的迷宫寻径演示:#include #include #include #include #include #defineCOL20 #defineROW20 #defineLEFT120 #defineTOP40 #defineRIGHT520 #defineBOTTOM440 #defineSMALL20 intMazeCOLROW= 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
12、1,0,0,1,0,0,1,0,0,0,1,0,0,0,0,0,1,1,1,1, 1,0,1,0,0,1,0,0,1,0,1,0,1,1,0,1,0,0,0,1, 1,0,1,0,1,1,0,1,1,0,1,0,1,0,0,1,0,1,0,1, 1,0,0,0,0,1,0,0,1,0,0,0,1,0,1,1,0,1,0,1, 1,1,1,0,1,1,1,0,1,1,1,1,0,0,1,0,0,1,0,1, 1,1,0,0,1,1,0,0,1,0,0,1,0,1,1,0,1,0,0,1, 1,0,0,1,1,0,0,1,0,0,1,0,0,1,0,0,1,0,1,1, 1,0,1,1,0,0,1
13、,1,0,1,1,0,1,1,0,1,1,0,1,1, 1,0,1,0,0,1,1,0,0,1,0,0,1,1,0,0,1,0,1,1, 1,0,0,0,1,1,0,1,0,0,0,1,1,0,1,0,1,0,0,1, 1,0,1,1,1,0,0,1,1,1,0,1,1,0,1,0,1,1,0,1, 1,0,0,1,0,0,1,0,1,0,0,1,0,0,0,0,1,0,0,1, 1,1,0,1,1,0,1,0,0,1,0,1,1,1,0,1,1,0,1,1, 1,0,0,0,1,0,1,1,0,1,0,1,0,0,0,1,0,0,0,1, 1,0,1,0,1,0,0,0,0,1,0,0,0,
14、1,1,0,0,1,0,1, 1,0,1,0,1,0,1,1,0,0,1,0,1,1,0,0,1,1,0,1, 1,0,1,1,0,0,0,0,1,0,1,1,0,0,1,0,0,0,1,1, 1,0,0,0,0,1,0,1,1,0,0,0,0,1,1,0,1,0,0,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1; intflag=1; typedefstructSeat intx,y; intdir; Seat; typedefstructLSNode Seatdata; structLSNode*next; LSNode,*SNode; typede
15、fstructLSk SNodetop; LSk,*LStack; voidInitLStack(LStackLS) if(LS=NULL) LS=(LStack)malloc(sizeof(LSk); LS-top=NULL; Seat*Peek(LStackLS) Seat*p; Seattemp; temp=LS-top-data; p=(Seat*)malloc(sizeof(Seat); p-dir=temp.x; p-x=temp.x; p-y=temp.y; returnp; voidPush(LStackLS,Seatvalue) SNodep; p=(SNode)malloc
16、(sizeof(LSNode); p-data.x=value.x; p-data.y=value.y; p-data.dir=value.dir; p-next=LS-top; LS-top=p; Seat*Pop(LStackLS) SNodep; Seat*res; Seattemp; res=(Seat*)malloc(sizeof(Seat); res-x=-1; res-y=-1; res-dir=0; p=LS-top; if(p=NULL) flag=0; returnres; temp=p-data; res-x=temp.x; res-y=temp.y; res-dir=t
17、emp.dir; LS-top=p-next; returnres; intIsEmpty(LStackLS) if(LS-top=NULL) return1; else return0; voidPrint(LStackLS) SNodep; if(LS-top=NULL) printf(Nopathn); return; p=LS-top; printf(Pathisasfollow:n); while(p!=NULL) printf(%d,%dt,p-data.x,p-data.y); p=p-next; printf(n); voiddrawcake(intx,inty) setfil
18、lstyle(SOLID_FILL,BLUE); bar(LEFT+1+x*SMALL,TOP+1+y*SMALL,RIGHT-1-19*SMALL+x*SMALL,BOTTOM-1-19*SMALL+y*SMALL); voiddraw(intx,inty) setfillstyle(SOLID_FILL,RED); bar(LEFT+5+x*SMALL,TOP+5+y*SMALL,RIGHT-5-19*SMALL+x*SMALL,BOTTOM-5-19*SMALL+y*SMALL); voidundraw(intx,inty) setfillstyle(SOLID_FILL,BLACK);
19、 bar(LEFT+5+x*SMALL,TOP+5+y*SMALL,RIGHT-5-19*SMALL+x*SMALL,BOTTOM-5-19*SMALL+y*SMALL); voidfootprint(intx,inty) setfillstyle(SOLID_FILL,GREEN); bar(LEFT+5+x*SMALL,TOP+5+y*SMALL,RIGHT-5-19*SMALL+x*SMALL,BOTTOM-5-19*SMALL+y*SMALL); voidTranverse(Seatstart,Seatend,LStackLS) Seatcur; Seat*top; start.dir
20、=1; Push(LS,start); top=Peek(LS); while(!IsEmpty(LS)&(top-x!=end.x|top-y!=end.y) top=Pop(LS); cur.x=top-x; cur.y=top-y; cur.dir=top-dir; /*printf(%d,%d,%dt,cur.x,cur.y,cur.dir);*/ if(cur.dir=5) Mazecur.xcur.y=0; footprint(cur.y,cur.x); continue; else switch(cur.dir) case1: if(cur.y+1COL) if(Mazecur.xcur.y+1=0) Mazecur.xcur.y+1=2; cur.dir=2; Push(LS,cur); cur.dir=1; cur.y+=1; Push(LS,cur); draw(cur.y,cur.x); break; cur.dir=2; Push(LS,cur); break; case2: if(cur.x+1=0) if(Mazecur.xcur.y-1=0) Mazecur.xcur.y-1=2; cur.dir=4; Push(LS,cur); cur.dir=1; cur.y-=1; Push(LS,cur);
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1