数据结构栈Word文档下载推荐.docx
- 文档编号:21162526
- 上传时间:2023-01-28
- 格式:DOCX
- 页数:12
- 大小:17.74KB
数据结构栈Word文档下载推荐.docx
《数据结构栈Word文档下载推荐.docx》由会员分享,可在线阅读,更多相关《数据结构栈Word文档下载推荐.docx(12页珍藏版)》请在冰豆网上搜索。
if(IsEmpty()){printf("
Stackisempty,cannotpopelement!
\n"
top--;
ret=stack[top];
returnret;
main()/*用作测试*/
inti=0;
Init_Stack();
for(i=0;
i<
10;
i++)
Push_Stack(i);
printf("
%4d"
Pop_Stack());
}
栈的逆转程序:
(正确的)(
#include<
#defineMAXN26
intstack[MAXN];
inttop=0;
ints[MAXN];
main()
{
intj,k,i;
\nInputthenumber:
for(k=0;
k<
k++)
scanf("
%d"
&
s[k]);
\nThenumberisthegroupare:
%d"
s[k]);
i=push(s[k]);
if(i==1)
\nstackfullinsertfailed!
else
\nTheorderpopedfromthestackis:
for(j=top-1;
j>
=0;
j--)
stack[j]);
intpush(x)
intx;
=MAXN)
return
(1);
stack[top++]=x;
return(0);
运行结果:
Inputthenumber:
0123456789
Thenumberisthegroupare:
Theorderpopedfromthestackis:
9876543210
/*迷宫游戏*/
/*进入tc运行游戏,按上下左右键控制,任何时候按ESC键退出*/
#include
<
conio.h>
dos.h>
graphics.h>
#define
LEFT
120
TOP
40
RIGHT
520
BOTTOM
440
SMALL
20
int
b[20][20];
/*用来保存地图信息*/
b[20][20]={{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,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,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}};
void
drawcake(int
x,int
y)
/*划方块*/
setfillstyle(SOLID_FILL,BLUE);
bar(LEFT+1+x*SMALL,TOP+1+y*SMALL,RIGHT-1-19*SMALL+x*SMALL,BOTTOM-1-19*SMALL+y*SMALL);
draw(int
/*划小方块*/
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);
undraw(int
/*擦去小方块*/
setfillstyle(SOLID_FILL,BLACK);
main()
gr=DETECT,gm,k=0,i,j,x=1,y=1,ch;
float
f;
initgraph(&
gr,&
gm,"
cleardevice();
setbkcolor(0);
for(i=0;
20;
for(j=0;
j<
j++)
if(b[i][j]==1)drawcake(j,i);
draw(x,y);
do{
ch=bioskey(0);
switch(ch)
case
19200:
/*按向左键*/
if(b[y][x-1]==0)
undraw(x,y);
b[y][x]=0;
x--;
b[y][x]=2;
break;
19712:
/*按向右键*/
if(b[y][x+1]==0)
x++;
18432:
/*按向上键*/
if(b[y-1][x]==0)
y--;
20480:
/*按向下键*/
if(b[y+1][x]==0)
y++;
if(b[18][18]==2)ch=283;
}while(ch!
=283);
if(b[18][18]==2)printf("
恭贺你成功地走出了迷宫!
getch();
用栈实现的迷宫寻径演示:
stdlib.h>
COL
ROW
Maze[COL][ROW]={
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1},
flag=1;
typedef
struct
Seat
x,y;
dir;
}Seat;
LSNode
Seat
data;
LSNode*
next;
}LSNode,*SNode;
LSk
SNode
top;
}LSk,*LStack;
InitLStack(LStack
LS)
if(LS==NULL)
LS=(LStack)malloc(sizeof(LSk));
LS->
top=NULL;
Seat*
Peek(LStack
p;
temp;
temp=LS->
top->
p=(Seat*)malloc(sizeof(Seat));
p->
dir=temp.x;
x=temp.x;
y=temp.y;
return
Push(LStack
LS,Seat
value)
p=(SNode)malloc(sizeof(LSNode));
data.x=value.x;
data.y=value.y;
data.dir=value.dir;
next=LS->
top=p;
Pop(LStack
*res;
res=(Seat*)malloc(sizeof(Seat));
res->
x=-1;
y=-1;
dir=0;
p=LS->
if(p==NULL)
flag=0;
res;
temp=p->
dir=temp.dir;
top=p->
IsEmpty(LStack
if(LS->
top==NULL)
1;
else
0;
Print(LStack
No
path\n"
return;
Path
is
as
follow:
while(p!
=NULL)
%d,%d\t"
p->
data.x,p->
data.y);
p=p->
y)
footprint(int
setfillstyle(SOLID_FILL,GREEN);
Tranverse(Seat
start,Seat
end,LStack
cur;
*top;
start.dir=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->
/*
%d,%d,%d\t"
cur.x,cur.y,cur.dir);
*/
if(cur.dir==5)
Maze[cur.x][cur.y]=0;
footprint(cur.y,cur.x);
continue;
else
switch(cur.dir)
1:
if(cur.y+1<
COL)
if(Maze[cur.x][cur.y+1]==0)
Maze[cur.x][cur.y+1]=2;
cur.dir=2;
Push(LS,cur);
cur.dir=1;
cur.y+=1;
draw(cur.y,cur.x);
2:
if(cur.x+1<
ROW)
if(Maze[cur.x+1][cur.y]==0)
Maze[cur.x+1][cur.y]=2;
cur.dir=3;
cur.x+=1;
3:
if(cur.y-1>
=0)
if(Maze[cur.x][cur.y-1]==0)
Maze[cur.x][cur.y-1]=2;
cur.dir=4;
cur.y-=1;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 数据结构