数据结构栈.docx

上传人:b****5 文档编号:8026261 上传时间:2023-01-28 格式:DOCX 页数:12 大小:17.74KB
下载 相关 举报
数据结构栈.docx_第1页
第1页 / 共12页
数据结构栈.docx_第2页
第2页 / 共12页
数据结构栈.docx_第3页
第3页 / 共12页
数据结构栈.docx_第4页
第4页 / 共12页
数据结构栈.docx_第5页
第5页 / 共12页
点击查看更多>>
下载资源
资源描述

数据结构栈.docx

《数据结构栈.docx》由会员分享,可在线阅读,更多相关《数据结构栈.docx(12页珍藏版)》请在冰豆网上搜索。

数据结构栈.docx

数据结构栈

栈:

#include

typedefintElem;

#defineN100/*N是栈的足够的空间*/

Elemstack[N];

inttop;/*栈顶指针,当栈为空时top=1,即top指向栈顶元素的上一个元素*/

/*定义栈的一般方法*/

voidInit_Stack()/*栈的初始化*/

{

top=1;/*清空栈*/

}

intIsEmpty()/*判断栈是否为空*/

{

if(top<=1)return1;

elsereturn0;

}

intIsFull()/*判断栈是否为满状态*/

{

if(top>=N)return1;

elsereturn0;

}

voidPush_Stack(Elemx)/*入栈函数*/

{

if(IsFull()){printf("Stackisfull,cannotpushelement!

");exit(0);}

stack[top]=x;

top++;

}

ElemPop_Stack()/*出栈函数*/

{

Elemret;

if(IsEmpty()){printf("Stackisempty,cannotpopelement!

\n");exit(0);}

top--;

ret=stack[top];

returnret;

}

main()/*用作测试*/

{

inti=0;

Init_Stack();

for(i=0;i<10;i++)

Push_Stack(i);

for(i=0;i<10;i++)

printf("%4d",Pop_Stack());

}

栈的逆转程序:

(正确的)(

#include

#defineMAXN26

intstack[MAXN];

inttop=0;

ints[MAXN];

main()

{

intj,k,i;

printf("\nInputthenumber:

\n");

for(k=0;k<10;k++)

scanf("%d",&s[k]);

printf("\nThenumberisthegroupare:

\n");

for(k=0;k<10;k++)

printf("%d",s[k]);

for(k=0;k<10;k++)

i=push(s[k]);

if(i==1)

printf("\nstackfullinsertfailed!

\n");

else

{

printf("\nTheorderpopedfromthestackis:

\n");

for(j=top-1;j>=0;j--)

printf("%d",stack[j]);

}

}

intpush(x)

intx;

{

if(top>=MAXN)

return

(1);

stack[top++]=x;

return(0);

}

运行结果:

Inputthenumber:

0123456789

Thenumberisthegroupare:

0123456789

Theorderpopedfromthestackis:

9876543210

/*迷宫游戏*/

/*进入tc运行游戏,按上下左右键控制,任何时候按ESC键退出*/

#include 

#include 

#include 

#include 

#define LEFT 120

#define TOP 40

#define RIGHT 520

#define BOTTOM 440

#define 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);

}

void draw(int x,int y)  /*划小方块*/

{

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);

}

void undraw(int x,int y)  /*擦去小方块*/

{

setfillstyle(SOLID_FILL,BLACK);

bar(LEFT+5+x*SMALL,TOP+5+y*SMALL,RIGHT-5-19*SMALL+x*SMALL,BOTTOM-5-19*SMALL+y*SMALL);

}

main()

{

int gr=DETECT,gm,k=0,i,j,x=1,y=1,ch;

float f;

initgraph(&gr,&gm,"");

cleardevice();

setbkcolor(0);

for(i=0;i<20;i++)

 for(j=0;j<20;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--;

  draw(x,y);

  b[y][x]=2;

  break;

  }

 case 19712:

   /*按向右键*/

  if(b[y][x+1]==0)

  {

  undraw(x,y);

  b[y][x]=0;

  x++;

  draw(x,y);

  b[y][x]=2;

  break;

  }

 case 18432:

   /*按向上键*/

  if(b[y-1][x]==0)

  {

  undraw(x,y);

  b[y][x]=0;

  y--;

  draw(x,y);

  b[y][x]=2;

  break;

  }

 case 20480:

   /*按向下键*/

  if(b[y+1][x]==0)

  {

  undraw(x,y);

  b[y][x]=0;

  y++;

  draw(x,y);

  b[y][x]=2;

  break;

  }

 }

if(b[18][18]==2)ch=283;

}while(ch!

=283);

if(b[18][18]==2)printf("                          恭贺你成功地走出了迷宫!

");

getch();

}

用栈实现的迷宫寻径演示:

#include 

#include 

#include 

#include 

#include 

#define COL 20

#define ROW 20

#define LEFT 120

#define TOP 40

#define RIGHT 520

#define BOTTOM 440

#define SMALL 20

int Maze[COL][ROW]={

   {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}};

int flag=1;

typedef struct Seat

{

    int x,y;

    int dir;

}Seat;

typedef struct LSNode

{

    Seat data;

    struct LSNode* next;

}LSNode,*SNode;

typedef struct LSk 

{

    SNode top;

}LSk,*LStack;

void InitLStack(LStack LS)

{

    if(LS==NULL)

    {

        LS=(LStack)malloc(sizeof(LSk));

    }

    LS->top=NULL;

}

Seat* Peek(LStack LS)

{

    Seat* p;

    Seat temp;

    temp=LS->top->data;

    p=(Seat*)malloc(sizeof(Seat));

    p->dir=temp.x;

    p->x=temp.x;

    p->y=temp.y;

    return p;

}

void Push(LStack LS,Seat value)

{

    SNode p;

    p=(SNode)malloc(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(LStack LS)

{

    SNode p;

    Seat *res;

    Seat temp;

    res=(Seat*)malloc(sizeof(Seat));

    res->x=-1;

    res->y=-1;

    res->dir=0;

    p=LS->top;

    if(p==NULL)

    {

        flag=0;

        return res;

    }

    temp=p->data;

    res->x=temp.x;

    res->y=temp.y;

    res->dir=temp.dir;

 LS->top=p->next;

    return res;

}

int IsEmpty(LStack LS)

{

    if(LS->top==NULL)

        return 1;

    else 

        return 0;

}

void Print(LStack LS)

{

    SNode p;

    if(LS->top==NULL)

    {

        printf("No path\n");

        return;

    }

    p=LS->top;

    printf("Path is as follow:

\n");

    while(p!

=NULL)

    {

        printf("%d,%d\t",p->data.x,p->data.y);

  p=p->next;   

    }

    printf("\n");

}

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);

}

void draw(int x,int y)

{

    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);

}

void undraw(int x,int y)

{

    setfillstyle(SOLID_FILL,BLACK);

 bar(LEFT+5+x*SMALL,TOP+5+y*SMALL,RIGHT-5-19*SMALL+x*SMALL,BOTTOM-5-19*SMALL+y*SMALL);

}

void footprint(int x,int y)

{

    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);

}

void Tranverse(Seat start,Seat end,LStack LS)

{

    

    Seat cur;

    Seat *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->dir;

 /*     printf("%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)

            {

            case 1:

                if(cur.y+1

                {

                    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;

                        Push(LS,cur);

                        draw(cur.y,cur.x);

                        break;

                    }

                }

                cur.dir=2;

                Push(LS,cur);

                break;

            case 2:

                if(cur.x+1

                {

                    if(Maze[cur.x+1][cur.y]==0)

                    {

                        Maze[cur.x+1][cur.y]=2;

                        cur.dir=3;

                        Push(LS,cur);

                        cur.dir=1;

                        cur.x+=1;

                        Push(LS,cur);

                        draw(cur.y,cur.x);

                        break;

                    }

                }

                cur.dir=3;

                Push(LS,cur);

                break;

            case 3:

                if(cur.y-1>=0)

                {

     

                    if(Maze[cur.x][cur.y-1]==0)

                    {

                        Maze[cur.x][cur.y-1]=2;

                        cur.dir=4;

                        Push(LS,cur);

                        cur.dir=1;

                        cur.y-=1;

                        Push(LS,cur);

                      

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 总结汇报 > 学习总结

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1