Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
#include<stdio.h>
#include<stdlib.h>

struct stack
{
    char data;
    struct stack * next;
};

struct stack * createstack()
{
 struct stack * s;
 s=(struct stack*)malloc(sizeof(struct stack));
 return s;
}

void push(struct stack * s,char data)
{
    struct stack * p;
    p=createstack();
    p->data=data;
    p->next=NULL;
    if(s==NULL)
    {
        s=p;
        return ;
    }
    p->next=s;
    s=p;
}

char pop( struct stack * s)
{
    struct stack * p;
    p=s;
    char d;
    d=p->data;
    s=p->next;
    free(p);
    return d;
}

int prec(char x)
{
    if(x=='(')
        return 0;
    else if(x=='+' || x=='-')
        return 1;
    else if(x=='*' || x=='/')
        return 2;
    else if(x=='^')
        return 3;
    else
        return -1;
}

int isaplpha(char x)
{
  if((x>='A' || x>='a')&&(x>='Z' || x>='z' ))
    return 1;
  return 0;
}

int main()
{
    char * c;
    char s[20],x[20],d;
    int j=0;
    struct stack * head;
    head=NULL;
    printf("Enter the expression to convert it into postfix \n");
    gets(s);
    c=&s[0];
    while(*c!='\0')
    {
        if(isaplpha(*c))
           x[j++]=*c;
        else if(*c=='(')
            push(head,*c);
        else if(*c==')')
        {
                d=pop(head);
            while(d!='(')
            {
                d=pop(head);
                x[j++]=d;
            }
            push(head,*c);
        }
        else
        {
            while(prec(head->data)>=prec(*c))
                x[j++]=pop(head);
            push(head,*c);
        }
        c++;
    }
    while(head!=NULL)
    {
        x[j++]=pop(head);
        head=head->next;
    }
    puts(x);
    return 0;
}


What I have tried:

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

struct stack
{
    char data;
    struct stack * next;
};

struct stack * createstack()
{
 struct stack * s;
 s=(struct stack*)malloc(sizeof(struct stack));
 return s;
}

void push(struct stack * s,char data)
{
    struct stack * p;
    p=createstack();
    p->data=data;
    p->next=NULL;
    if(s==NULL)
    {
        s=p;
        return ;
    }
    p->next=s;
    s=p;
}

char pop( struct stack * s)
{
    struct stack * p;
    p=s;
    char d;
    d=p->data;
    s=p->next;
    free(p);
    return d;
}

int prec(char x)
{
    if(x=='(')
        return 0;
    else if(x=='+' || x=='-')
        return 1;
    else if(x=='*' || x=='/')
        return 2;
    else if(x=='^')
        return 3;
    else
        return -1;
}

int isaplpha(char x)
{
  if((x>='A' || x>='a')&&(x>='Z' || x>='z' ))
    return 1;
  return 0;
}

int main()
{
    char * c;
    char s[20],x[20],d;
    int j=0;
    struct stack * head;
    head=NULL;
    printf("Enter the expression to convert it into postfix \n");
    gets(s);
    c=&s[0];
    while(*c!='\0')
    {
        if(isaplpha(*c))
           x[j++]=*c;
        else if(*c=='(')
            push(head,*c);
        else if(*c==')')
        {
                d=pop(head);
            while(d!='(')
            {
                d=pop(head);
                x[j++]=d;
            }
            push(head,*c);
        }
        else
        {
            while(prec(head->data)>=prec(*c))
                x[j++]=pop(head);
            push(head,*c);
        }
        c++;
    }
    while(head!=NULL)
    {
        x[j++]=pop(head);
        head=head->next;
    }
    puts(x);
    return 0;
}
Posted
Updated 17-Sep-19 22:17pm
v2
Comments
jeron1 17-Sep-19 10:39am    
Tell us what you are trying to do (expected inputs and outputs) and what your program is doing that is not what you want. Try and narrow it down to a section of code if you can. Not too many people are going to go line by line through your code to diagnose it. More detail is better, and ask specific questions.

This code is wrong :
C++
int isaplpha(char x)
{
  if((x>='A' || x>='a')&&(x>='Z' || x>='z' ))
    return 1;
  return 0;
}
Firstly, there is a RTL function available that does performs the isalpha test. There is no need to write one for yourself. You are already using the RTL with malloc so using it should not be an issue. Secondly, to do this correctly it should be written something like this :
C++
int isalpha(char x)
{
   return ( ( x >= 'A' && x <='Z') || ( x >='a' && x <= 'z' ) );
}
I am not sure what ispalpha refers to so I wrote it isalpha. However, that could collide with the RTL function name so you are better just using the RTL function.
 
Share this answer
 
Comments
Shrimad Mishra 17-Sep-19 11:47am    
i tried it but output is not coming
Stefan_Lang 18-Sep-19 5:10am    
Are you saying no output at all, i. e. the program does not stop? If so, maybe your input is too long.
Check your push and pop methods. The passed in pointer will not be modified in the calling method. You will need to use a pointer to a pointer, or pass the pointer by reference.
 
Share this answer
 
Comments
Stefan_Lang 18-Sep-19 5:18am    
Correct, that's what I've also noted. head will always remain at NULL within main(). My 5.
Quote:
What the erroe in the following code

The problem is that you didn't tell us what this code is supposed to do and what is not as expected.
-----
Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
 
Share this answer
 
Comments
Stefan_Lang 18-Sep-19 5:20am    
As general as this advice is, it probably applies to this problem more than most. At least one of the main issues (see solution 2) could have been detected easily with the debugger.

Have a 5.
Patrice T 18-Sep-19 7:58am    
Thank you
I give you an example of stack implementation (together with an example of its usage):
C
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

//-> stack
struct stack
{
  char data;
  struct stack * down;
};

// pushes an item, returning pointer to it ( new TOP of the stack)
struct stack  * push(struct stack * top, char data)
{
  struct stack * newtop = (struct stack *) malloc( sizeof (struct stack) );
  assert(newtop);
  newtop->data = data;
  newtop->down = top;
  return newtop;
}

// pops (removes) the TOP item, returning the pointer to the new TOP
struct stack * pop( struct stack * top)
{
  if ( ! top ) return NULL;
  struct stack * newtop = top->down;
  free( top);
  return newtop;
}
//<- 

int main()

  const char * s = "Hello World!";
  struct stack * top = NULL;
  while ( *s )
  {
    top = push( top, *s);
    ++s;
  }

  while (top)
  {
    putchar(top->data);
    top = pop(top);
  }
  putchar('\n');
  return 0;
}


Please note, the memory allocation strategy used is not efficient. For instace with deep stacks, the ready-made C++ stack class[^] (leveraging the memory allocation strategy of STL containers) is way faster.
 
Share this answer
 
Comments
Maciej Los 19-Sep-19 4:22am    
Interesting sample ;)

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