使用推入堆栈时遇到麻烦

时间:2016-10-06 17:40:48

标签: c stack

我正在学习数据结构,并且在将数据推入堆栈时遇到问题。即使我在程序的早期使用了相同的推送功能,它似乎也没有推动字符。程序应该将中缀表达式转换为后缀

当给出数字“1 + 2”时,它应返回“12+”,但它只返回“12” 谢谢你的建议

       bool revisar(char * inf, stack p){
            inicializa(&p);
            Nodo D;
            char pos[MAX];
            int i=0;
            int j=0;
            while(inf[i]!='\0')
            {//WHILE
                if(inf[i]>='0' && inf[i]<='9')
                {     
                    pos[j++]=inf[i++];     
                }
                if(inf[i]=='(')
                {     
                    D.caracter=inf[i++];
                    push(&p,D);    
                }
                if(inf[i]==')')
                {   

                    while(top(p).caracter!='(')
                    {
                        pos[j++]=pop(&p).caracter;                 
                    }
                    if(top(p).caracter=='(')
                        pop(&p);
                    i++;   
                }
                else
                {
                    if(inf[i]=='+'||inf[i]=='-'||inf[i]=='*'||inf[i]=='/')
                    {
                        if(empty(p)||top(p).caracter=='(')
                        {   
                            D.cara

cter=inf[i++];
                        push(&p,D);
                        if(empty(p));
                        printf("NNNNNNOOOOOOOOOOOOOOOOOOOOOO"); **Here it prints that the stack is still empty....
                    }
                    else
                    {
                        if(menorpre(top(p).caracter,inf[i]))
                        {
                            D.caracter=inf[i++];
                            push(&p,D); 
                        }
                        else
                        {
                            pos[j++]=pop(&p).caracter;
                            if(!empty(p))
                            {
                                if(top(p).caracter!='(')

                                pos[j++]=pop(&p).caracter;
                            }
                            else
                            {   
                                D.caracter=inf[i++];
                                push(&p,D);
                            }
                        }
                    }
                }
            }
        }
        while(!empty(p)){
        printf("ddd");
        pos[j++]=pop(&p).caracter;  
        }   
        pos[j]='\0';

        printf("\n \n");
        printf("i=%d,j=%d",i,j);
        printf("LA CADENA EN POSFIJO ES: ");
        puts(pos); 
    }

我的堆栈库

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include "pila.h"

void inicializa(stack *p ){
    *p = NULL;
}

bool empty(stack p){
    if (p==NULL){
    return true;
}
    else return false;      
}

int push(stack * p, Nodo n){
    stack nuevo = (stack)malloc(sizeof(Nodo));
    if(nuevo == NULL){
        return 300;
    }
    *nuevo = n;
    if(empty(*p)){
        nuevo->next = NULL;
        }
    else{
        nuevo->next = *p;
    *p = nuevo;
    }
    return 301;
}


Nodo pop(stack * p){
    Nodo copy = *(*p);
    free(*p);
    *p = copy.next;
    inicializa(&copy.next);
    return copy;
}

Nodo top(stack p){
    Nodo copy;
    copy = *p;
    copy.next = NULL;
    return copy;
}

头文件

struct nodo{
    char caracter;
    int numero;
    struct nodo * next;
};
typedef struct nodo Nodo;
typedef Nodo * stack;
void inicializa(stack *p);
bool empty(stack p);
Nodo top(stack p);
Nodo pop(stack *p);
int push(stack *p, Nodo n);

1 个答案:

答案 0 :(得分:1)

答案简短 - 在此代码中:

int push(stack * p, Nodo n){
    stack nuevo = (stack)malloc(sizeof(Nodo));
    if(nuevo == NULL){
        return 300;
    }
    *nuevo = n;
    if(empty(*p)){
        nuevo->next = NULL;
        }
    else{
        nuevo->next = *p;
    *p = nuevo;
    }
    return 301;
}

*p = nuevo;应该在else之后,而不是在struct nodo { char caracter; struct nodo *next; }; typedef struct nodo Nodo; typedef Nodo *stack; void inicializa(stack *p); bool empty(stack p); char top(stack p); char pop(stack *p); int push(stack *p, char c); void inicializa(stack *p) { *p = NULL; } bool empty(stack p) { return (p == NULL); } int push(stack *p, char caracter) { Nodo *nuevo = malloc(sizeof(*nuevo)); if (nuevo == NULL) { return 300; } nuevo->caracter = caracter; if (empty(*p)) { nuevo->next = NULL; } else { nuevo->next = *p; } *p = nuevo; return 301; } char pop(stack *p) { Nodo *copy = *p; *p = copy->next; char caracter = copy->caracter; free(copy); return caracter; } char top(stack p) { return p->caracter; } 之内。无论如何都适用。更长的答案:

您的堆栈代码存在一些问题,包括malloc&memory内存管理不善。我建议使用更简单的堆栈设计:

revisar()

这需要修改void revisar(char *inf, stack p) { char caracter; char pos[MAX]; int i = 0; int j = 0; inicializa(&p); while (inf[i] != '\0') { if (inf[i] >= '0' && inf[i] <= '9') { pos[j++] = inf[i++]; } else if(inf[i] == '(') { (void) push(&p, inf[i++]); } else if (inf[i] == ')') { while (top(p) != '(') { pos[j++] = pop(&p); } if (top(p) == '(') { (void) pop(&p); } i++; } else { if (inf[i] == '+' || inf[i] == '-' || inf[i] == '*' || inf[i] == '/') { if (empty(p) || top(p) == '(') { (void) push(&p, inf[i++]); if (empty(p)) { printf("NNNNNNOOOOOOOOOOOOOOOOOOOOOO"); // Here it prints that the stack is still empty.... } } else { if (menorpre(top(p), inf[i])) { (void) push(&p, inf[i++]); } else { pos[j++] = pop(&p); if (!empty(p)) { if (top(p) != '(') { pos[j++] = pop(&p); } } else { (void) push(&p, inf[i++]); } } } } } } while (!empty(p)) { pos[j++] = pop(&p); } pos[j]='\0'; printf("\n \n"); printf("i=%d, j=%d", i, j); printf("LA CADENA EN POSFIJO ES: "); puts(pos); } 行:

revisar("1+2", p);

在我的测试中,我能够得到% ./a.out ddd i=3, j=3LA CADENA EN POSFIJO ES: 12+ %

menorpre()

我猜测的就是你所追求的。如果您对此有更多疑问,请务必在上面的代码中添加private async Task<ResultRow> RetreiveLastRow() { ResultRow row = null; var connectionString = "server=<ip>;user id=<user>;password=<pass>;database=<db name>"; using (var connection = new NpgsqlConnection(connectionString)) using (var command = connection.CreateCommand()) { await connection.OpenAsync(); command.CommandText = "SELECT <fields> FROM <table> ORDER BY <field> DESC LIMIT 1"; using (var reader = await command.ExecuteReaderAsync()) { if (await reader.ReadAsync()) { row = <read row>; } } connection.Close(); } return row; } 的定义,因为它是无法重建的部分。