Click here to Skip to main content
15,671,597 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I wrote the following code to convert a postfix expression to a fully paranthesised infix expression, using stacks.

C#
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct node
{
    char *data;
    struct node *point;
}NODE;
NODE *start = NULL;

void push(char *);
char *pop();

int main()
{
    char c;
    char op[2];
    char *op1;
    char *op2;
    while((c = getchar()) != '\n')
    {
        op[0] = c;
        op[1] = '\0';
        printf("%s\n", op);
        if(isalnum(c))
        {
            push(op);
        }
        else
        {
            op2 = pop();
            op1 = pop();
            op1 = strcat(op1, op_);
            op1 = strcat(op1, op2);
            op1 = strcat("(", op1);
            op1 = strcat(op1, ")");
            push(op1);
            printf("%s\n%s\n", op1, op2);
        }
    }
    printf("%s\n", pop());
    return 0;
}

void push(char *value)
{
    NODE *top = malloc(sizeof(NODE));
    if(top == NULL)
    {
        fputs("Error: no memory\n", stderr);
        abort();
    }
    else
    {
        (top->data) = strcpy(top->data, value);
        top->point = start;
        start = top;
    }
}

char *pop()
{
    if(start == NULL)
    {
        fputs("Error: stack underflow\n", stderr);
        abort();
    }
    else
    {
        NODE *top = start;
        char *value = top->data;
        start = top->point;
        free(top);
        return value;
    }
}


The highlighted statement in the pop() function is the one causing problems. I have used the same statement in other stack programs that I wrote, yet I am facing problems here. Kindly help me out.
Thanks in advance.
Posted
Updated 9-May-12 17:59pm
v2
Comments
Sergey Alexandrovich Kryukov 10-May-12 0:14am    
What is supposed to happen and what happens, exactly?
--SA
Sandeep Mewara 10-May-12 1:44am    
'Causing problems' as in?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900