p37页 队列操作 p55中序转后序

#include <stdio.h>
#include <stdlib.h>
#define MAX 100
void push();
void pop();
void list();
char item[MAX][20];
int top = -1;

//堆栈的相关定义,插入删除输出操作的实现啊
int main()
{
    char option;
    while(1)
    {
            printf("\n****.................................\n");
            printf("     <1> insert(push)\n");
            printf("     <2> delete(pop)\n");
            printf("     <3> list\n");
            printf("     <4> quit\n");
            printf("\n****.................................\n");
            printf("输入选择");
            option = getchar();
            switch (option)
            {
            case '1':
                push();
                break;
            case '2':
                pop();
                break;
            case '3':
                list();
                break;
            case '4':
                exit(0);
            }
    }

    return 0;
}
void push()//插入栈顶元素
{
        if( top >= MAX-1 )
        {
                printf("堆栈满了\n");
        }
        else
        {
                top++;
                printf("输入要插入堆栈中的因素\n");
                scanf( "%s", item[top] );
        }
}
void pop()//删除栈顶元素
{
        if( top < 0 )
        {
                printf("堆栈为空\n");
        }
        else
        {
                printf( "从堆栈中删除元素%s", item[top]);
                top--;
        }
}
void list()
{
        int count = 0;
        int i;
        if(top < 0)
                printf("\n\n 这个堆栈是空的撒\n");
        else
        {
                printf("\n\n ITEM\n");
                printf("--------------\n");
                for(i = 0; i < top; i++)
                {
                        printf("%-20s\n",item[i]);
                        count++;
                        if( count%20 == 0)
                                getchar();
                }
               printf("---------------------\n");
               printf("全部元素%d\n", count );
               getchar();
        }
}






//p55页
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX 20
void intopostfix( char infix_q[], int rear);
int compare( char stack_o, char infix_o );
char infix_priority[9] = {'#', ')', '+', '-', '*', '/', '^', ' ', '(' };
char stack_priority[8] = { '#', '(', '+', '-', '*', '/', '^', ' '};

int main()
{
    int rear = -1;
    char infix_q[MAX];//储存使用者输入中序者的队列
    printf("****这里是分割线哟------------------------\n");
    printf("使用的运算符\n");
    printf(" ^: 指数\n");
    printf(" *: 乘法     /: 除法\n");
    printf(" +: 家法     -: 减法\n");
    printf(" (: 左括号   ):右括号  ");
    printf(" -----------这里依旧是分割线");
    printf(" 请输入中缀表达式:\n");
    /*while( infix_q[rear] != '\n' )
        infix_q[++rear] = getchar();*/
    gets(infix_q);
    rear = strlen( infix_q );
    infix_q[rear] = '#';//队列结束时插入#作为结束符号
    // printf("后缀表达式:\n");
    intopostfix( infix_q, rear);
    printf("\n");
    return 0;
}
void intopostfix(char infix_q[], int rear)
{
    //printf("rear");
    int top = 0,ctr, tag = 1;
    char stack_t[MAX];
    stack_t[top] = '#';
    for(ctr = 0; ctr <= rear; ctr++)
    {
        switch( infix_q[ctr])
        {
        case ')'://输入是左括号时,输出堆栈内运算符,直至堆栈内为左括号
            while( stack_t[top] != '(' )
            {
                printf("%c", stack_t[top--]);
            }
            top--;
            break;
        case '#'://输入#,堆栈内还未输出的运算符输出
            while( stack_t[top] != '#' )
            {
                printf("%c", stack_t[top--]);
            }
            break;
        case '(':
        case '^':
        case '*':
        case '/':
        case '+':
        case '-':
            while( compare( stack_t[top], infix_q[ctr] ) )//如果输入的运算符在表达式中的优先权,小于TOP在堆栈内所指向的运算符在堆栈中的优先权,则将堆栈所指运算符输出,若大于,则将输入运算符放入堆栈
                printf(" %c",stack_t[top--]);
            stack_t[++top] = infix_q[ctr];
           // tag = 1;
            break;
        /*case '+'://这一段代码是从书上抄的,但是加上去就会出错,改完就对了,好开心,也就是说tag没有什么用处呀
        case '-':
            if( tag == 1 )
            {
                stack_t[++top] = infix_q[ctr];
                tag = 2;
            }
            else
            {
                while( compare( stack_t[++top], infix_q[ctr]))
                    printf("%c",stack_t[top--]);
                stack_t[++top] = infix_q[ctr];
                tag = 1;
            }
            break;*/
        default:
            printf(" %c", infix_q[ctr]);
            /*if( tag == 2 )
                printf(" %c",stack_t[top--]);
            tag = 0;*/
            break;

        }

    }
}
int compare( char stack_o, char infix_o )
{
    int index_s = 0, index_i = 0;
    while( stack_priority[index_s] != stack_o)
        index_s++;
    while( infix_priority[index_i] != infix_o)
        index_i++;
    return (index_s/2) >= (index_i/2) ? 1 : 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值