栈的应用

来源:互联网 发布:类似余罪的网络剧 编辑:程序博客网 时间:2024/05/18 20:49

1.进制转换

void Conversion(SqStack *S){    int N,e;    printf("输入要转换的十进制数\n");    scanf("%d",&N);    while(N)    {        S=Push(S,N%8);        N=N/8;    }    printf("二进制为:");    while(!StackEmpty(S))    {        Pop(S,&e);        printf("%d",e);    }}

#include <stdio.h>#include <stdlib.h>#define MAX 20#define Status inttypedef struct SqStack{    int elem[MAX];    int top;}SqStack;SqStack *InitStack(){    SqStack *S;    S=(SqStack *)malloc(sizeof(SqStack));    if(S)    {        S->top=-1;        printf("置空栈成功");    }    return S;}Status StackEmpty(SqStack *S){    if(S->top==-1)return 1;    else return 0;}SqStack *Push(SqStack *S,int e){    if(S->top==MAX-1)        printf("栈满无法入栈\n");    S->top++;    S->elem[S->top]=e;    return S;}SqStack *Pop(SqStack *S,int *e){    *e=S->elem[S->top];    S->top--;    return S;}void outline(SqStack *S){    int i;    for ( i = S->top; i >= 0; i--)        printf("%2d ", S->elem[i]);}void Conversion(SqStack *S){    int N,e;    printf("输入要转换的十进制数\n");    scanf("%d",&N);    while(N)    {        S=Push(S,N%8);        N=N/8;    }    printf("二进制为:");    while(!StackEmpty(S))    {        Pop(S,&e);        printf("%d",e);    }}int main(){    SqStack *S;    int cord,data,flag;    do{        printf("\n1栈的初始化     \n");        printf("2数据入栈   \n");        printf("3出栈 \n");        printf("5输出栈中元素\n");        printf("6.十进制转二进制,需先置空栈\n");        scanf("%d", &cord);        switch (cord)        {        case 1:{  S = InitStack();}break;        case 2:{                int e;                printf("输入元素\n");                scanf("%d",&e);                S=Push(S,e);                printf("成功插入\n");                }break;        case 3:            {                int e;                S=Pop(S,&e);                printf("栈顶元素为%d\n",e);            }break;        case 5:{  outline(S); }break;        case 6:Conversion(S);break;        }    } while (cord <= 7 && cord > 0);    return 0;}

2.括号匹配

    #include <stdio.h>    #include <stdlib.h>    #define MAX 20    #define Status int    typedef struct SqStack    {        char elem[MAX];        int top;    }SqStack;    SqStack *InitStack()    {        SqStack *S;        S=(SqStack *)malloc(sizeof(SqStack));        if(S)        {            S->top=-1;            printf("置空栈成功");        }        return S;    }    Status StackEmpty(SqStack *S)    {        if(S->top==-1)return 1;        else return 0;    }    SqStack *Push(SqStack *S,char e)    {        if(S->top==MAX-1)            printf("栈满无法入栈\n");        S->top++;        S->elem[S->top]=e;        return S;    }    SqStack *Pop(SqStack *S,char *e)    {        *e=S->elem[S->top];        S->top--;        return S;    }Status Match(SqStack *s,char *str){    int i=0,flag=0;    char e;    while(str[i]!='\0')    {        switch(str[i])        {        case '(':Push(s,str[i]);break;        case '[':Push(s,str[i]);break;        case ')':{Pop(s,&e);if(e!='(') flag=1;}break;        case ']':{Pop(s,&e);if(e!='[')flag=1;}break;        default:break;        }    if(flag)break;//flag=1表示不匹配    i++;}   if(!flag&&StackEmpty(s))   printf(" Parenthesis is match!\n");   else   printf(" Parenthesis isn't match!\n");   return 1;}void outline(SqStack *S){    int i;    for ( i = 0; i < S->top; i++)        printf(" %c ", S->elem[i]);}void main()    {        SqStack *S;       char str[100],ch;        S=InitStack();    printf("input string chars:");    scanf("%s",str);    printf("%s",str);        outline(S);        Match(S,str);    }

3.行编辑程序

#include<stdio.h>#include<stdlib.h>#define Status int#define MAX 100#define OK 1typedef struct SqStack{char elem[MAX];int top;}SqStack;Status InitStack(SqStack *S){S->top = -1;return OK;}Status Push(SqStack *S, char e){if (S->top >= MAX){ printf("栈满不能入栈\n"); return 0; } S->top++; S->elem[S->top] = e; return OK;}Status Pop(SqStack *S, char *e){if (S->top == -1){ printf("栈是空的"); return 0; }else*e = S->elem[S->top];S->top--;return OK;}Status ClearStack(SqStack *S){S->top = -1;return OK;}void Outline(SqStack S){for (int i = 0; i <= S.top; i++)printf("%c", S.elem[i]);}void LineEdit(){SqStack S;char ch;InitStack(&S);//构造空栈printf("please input the strings:\n");  scanf("\n%c", &ch);while (ch != EOF) //ctrl+z结束输入{while (ch != EOF&&ch != '\n')//行内循环{switch (ch){case '#':Pop(&S, &ch); break;case '@':ClearStack(&S); break;default:Push(&S, ch); break;}ch = getchar();//从终端接收下一个字符}printf("The output string is:");Outline(S);ClearStack(&S);//// 一行遍历完毕,栈中所有元素已经全写入fp指向的文件,重置s为空栈  if (ch != EOF){printf("\nplease input the next line strings:\n");ch = getchar();//继续接收字符,进入下轮(行)循环  }//DestroyStack(S);采用的是数组不用销毁}}int main(){int cord;SqStack S;do{printf("\n\n1.初始化\n");printf("2.入栈\n");printf("3.出栈\n");printf("4.输出\n");printf("5.行编辑程序,+z结束\n");scanf("%d", &cord);switch (cord){case 1:if (InitStack(&S))printf("初始化成功\n"); break;case 2:{  char e;  printf("输入要插入的元素\n");  scanf("\n%c", &e);//不接受回车  if (Push(&S, e))printf("插入成功\n");}break;case 3:{char e;if (Pop(&S, &e))printf("成功删除%c\n", e);}break;case 4:Outline(S); break;case 5:LineEdit(); break;}} while (cord < 9);return 0;}

3.判别读入的一个以‘@’为结束符的字符序列是否是“回文”。

int Palindrome_Test()//判别输入的字符串是否回文序列,是则返回1,否则返回0{InitStack(S); InitQueue(Q);while ((c = getchar()) != '@'){Push(S, c); EnQueue(Q, c); //同时使用栈和队列两种结构}while (!StackEmpty(S)){Pop(S, a); DeQueue(Q, b));if (a != b) return ERROR;}return OK;}//Palindrome_Test 


4.迷宫

    #include <stdio.h>        #include <stdlib.h>        #include"Stack.h"      #define Status int        #define OK 1       #define MAX 100            typedef struct seat//位置      {          int x;          int y;      }seat;            Status InitMaze(int maze[MAX][MAX])      {          int x, y;          printf("输入迷宫的行数和列数,空格隔开\n");          scanf("%d %d", &x, &y);          printf("0代表路,1代表墙\n");          for (int i = 0; i <x; i++)          for (int j = 0; j < y; j++)              scanf("%d", &maze[i][j]);          printf("你刚刚创建的迷宫为:\n");          for (int i = 0; i <x; i++) //输出迷宫           {              for (int j = 0; j <y; j++)                  printf("%d ", maze[i][j]);              printf("\n");          }          return OK;      }      Status MazePath(seat start,seat end,int maze[MAX][MAX],int diradd[4][2])      {          int x, y, d,a,b;          struct  Element elem,e;          LinkStack *top,*T;          top = (LinkStack *)malloc(sizeof(LinkStack));          T = (LinkStack *)malloc(sizeof(LinkStack));          InitStack(top); InitStack(T);                maze[start.x][start.y] = 2;//标记入口          elem.x = start.x;           elem.y = start.y;          elem.d = -1;//开始为-1          Push(top, elem);//入口入栈          while (top->next)          {              Pop(top, &elem);//4个方向没路,出栈              x = elem.x; y = elem.y;              d = elem.d + 1;//下一个方向,从东开始,顺时针              while (d < 4)              {                  a = x + diradd[d][0];//x方向增量                  b = y + diradd[d][1];//y方向增量                  if (a == end.x && b == end.y && maze[a][b] == 0) //下一个是出口                   {                      elem.x = x; elem.y = y;elem.d = d;                      Push(top, elem);                      elem.x = a; elem.y = b; elem.d = 5;                      Push(top, elem);                      while (top->next) //逆置序列 并输出迷宫路径序列                       {                          Pop(top, &e);                          Push(T, e);                      }                      while (T->next)                      {                          Pop(T, &e);                          printf("-->(%d,%d)", e.x, e.y);                      }                                            return OK;                  }                  if (maze[a][b] == 0) //找到可以前进的非出口的点                   {                      maze[a][b] = 2; //标记走过此点                       elem.x = x;                      elem.y = y;                      elem.d = d;                      Push(top, elem); //当前位置入栈                       x = a; //下一点转化为当前点                       y = b;                      d = -1;                  }                  d++;              }          }          printf("没有找到可以走出此迷宫的路径\n");      }      int main()      {          int maze[MAX][MAX];          //四个方向,x,y增量,分别为东南西北          int diradd[4][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };                seat start, end;          printf("ready\n");          InitMaze(maze);//创建迷宫          printf("输入入口的横纵坐标空格隔开\n");          scanf("%d %d", &start.x, &start.y);          printf("输入出口的横纵坐标空格隔开\n");          scanf("%d %d", &end.x, &end.y);          MazePath(start, end, maze, diradd);          return 0;      }            /***************stack.h************/      #include <stdio.h>        #include <stdlib.h>        #define Status int        #define OK 1       typedef struct Element//定义元素类型      {          int x;          int y;          int d;      }Element;      typedef struct LinkStack      {          struct LinkStack *next;          Element elem;      }LinkStack;            Status InitStack(LinkStack *top)      {          top->next = NULL;          return OK;      }      Status Push(LinkStack *top, Element e)      {          LinkStack *p;          p = (LinkStack *)malloc(sizeof(LinkStack));          p->elem = e;          p->next = top->next;          top->next = p;          return OK;      }      Status Pop(LinkStack *top, Element *e)      {          if (top->next == NULL){ printf("栈空\n"); return 0; }          LinkStack *temp;          temp = top->next;          *e = top->next->elem;          top->next = temp->next;          free(temp);          return OK;      }  


1 0
原创粉丝点击