Thread: Help With Dynamically Allocated Stack

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    12

    Help With Dynamically Allocated Stack

    Write a program that evaluates postfix expression using array implementation of stack.

    The expression [the input] is evaluated from left to right using a stack.


    1. When the element read from the expression is an operand, push it into the stack.
    2. When the element read from the expression is an operator:
      1. Pop two operands from the stack.
      2. Evaluate the two operands
      3. Push the result of the evaluation into the stack.


    The final result lies on the top of the stack at the end of the calculation. Make sure to display the result before terminating the program.Write a program that evaluates postfix expression using array implementation of stack.

    I've tried to combine some programs I had in my notes and such but I still have no idea what I'm doing wrong and how to fix this. I hope you guys help me out and if it's not too much explain why I'm wrong and how to fix it

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdbool.h>
    #define M 20
    
    typedef struct{
           char *stk[M];
           int top;
           int maxsize;
    }STAK;
    
    void inputString(char *);
    void determOper(char *);
    void push(STAK *,char);
    char pop(STAK *);
    void createStack(STAK *);
    bool isFull(int);
    bool isEmpty(int);
    
    int main(void)
    {
       char str[M];
       inputString(str);
       determOper(str);
       return 0;
    }
    void inputString(char *str[])
    {
         printf("Enter your expression: ");
         gets(str);
         return;
    }
    void creatStack(STAK *ostk)
    {
    
       char *newstk[M];
       char newstk =  malloc(sizeof(char)*M);
       if(newstk==NULL)
           printf("Error in allocation");
       else
       ostk->stk =newstk;
       ostk->top = -1;
       ostk -> maxsize = M;
    }
    void evalOperand(char stk[])
    {
    
       int op1,op2,i,tnum,result;
       bool full,empty;
       STAK ostk;
       createStack(&ostk);
      
       
           fgets(stk, M, stdin);
            for (i = 0; i < strlen(stk); i++) 
            {
                    if (isdigit(stk[i])) 
                    {
                            data = (data == -1) ? 0 : data;
                            data = (data * 10) + (stk[i] - 48);
                            continue;
                    }
    
                    if (data != -1) 
                    {
                            push(data);
                    }
    
                    if (stk[i] == '+' || stk[i] == '-' || stk[i] == '*' || stk[i] == '/') 
                            {
                            
                            op2 = pop();
                            op1 = pop();
                            if (op1 == -1 || op2 == -1)
                                    break;
                            switch (stk[i]) 
                            {
                                    case '+':
                                            result = op1 + op2;
                                            push(result);
                                            break;
                                    case '-':
                                            result = op1 - op2;
                                            push(result);
                                            break;
                                    case '*':
                                            result = op1 * op2;
                                            push(result);
                                            break;
                                    case '/':
                                            result = op1 / op2;
                                            push(result);
                                            break;
                            }
                    }
                    data = -1;
            }
           full = isFull(ostk);
           if(full)
           {
               printf("Error: stack is full");
               getch();
               exit(1);
    
           }
           else
                   push(&ostk,result);
       i = -1;
       do{
           empty = isEmpty(ostk.top);
           if(!empty)
           {
               ret=pop(&ostk);
               ++i;
               bin[i]=ret;
           }
       }while(!empty);
       dispRes(result);
       return;
    }
    bool isFull(STAK ostk)
    {
       return(ostk.top==(ostk.maxsize-1));
    }
    void push(STAK *ostk,char stk)
    {
    
       ostk->stk[++ostk->top] = stk;
       return;
    }
    int pop(STAK *ostk)
    {
       int r;
       r = ostk->stk[ostk->top--];
       return r;
    }
      void dispBin(char stk[], int result)
    {
    
           printf("%d", result);
           getch();
           return;
    }
    
    

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    First things first, please post your code as plain text next time, so the forum can do it's syntax highlighting and line numbering.

    Second, always compile with your warnings set to maximum. When I do that with the code you provided:
    Code:
    $ make foo
    gcc -g -Wall -std=c99 -o foo foo.c -lpthread -lm -lpq
    foo.c:27:6: error: conflicting types for ‘inputString’
    foo.c:12:6: note: previous declaration of ‘inputString’ was here
    foo.c: In function ‘inputString’:
    foo.c:30:5: warning: passing argument 1 of ‘gets’ from incompatible pointer type [enabled by default]
    /usr/include/stdio.h:634:14: note: expected ‘char *’ but argument is of type ‘char **’
    foo.c: In function ‘creatStack’:
    foo.c:37:10: error: conflicting types for ‘newstk’
    foo.c:36:11: note: previous declaration of ‘newstk’ was here
    foo.c:37:5: warning: implicit declaration of function ‘malloc’ [-Wimplicit-function-declaration]
    foo.c:37:20: warning: incompatible implicit declaration of built-in function ‘malloc’ [enabled by default]
    foo.c:37:20: warning: initialization makes integer from pointer without a cast [enabled by default]
    foo.c:38:14: warning: comparison between pointer and integer [enabled by default]
    foo.c:41:19: error: incompatible types when assigning to type ‘char *[20]’ from type ‘char’
    foo.c: In function ‘evalOperand’:
    foo.c:57:9: warning: implicit declaration of function ‘isdigit’ [-Wimplicit-function-declaration]
    foo.c:59:13: error: ‘data’ undeclared (first use in this function)
    foo.c:59:13: note: each undeclared identifier is reported only once for each function it appears in
    foo.c:66:13: error: too few arguments to function ‘push’
    foo.c:14:6: note: declared here
    foo.c:72:13: error: too few arguments to function ‘pop’
    foo.c:15:6: note: declared here
    foo.c:73:13: error: too few arguments to function ‘pop’
    foo.c:15:6: note: declared here
    foo.c:80:21: warning: passing argument 1 of ‘push’ makes pointer from integer without a cast [enabled by default]
    foo.c:14:6: note: expected ‘struct STAK *’ but argument is of type ‘int’
    foo.c:80:21: error: too few arguments to function ‘push’
    foo.c:14:6: note: declared here
    foo.c:84:21: warning: passing argument 1 of ‘push’ makes pointer from integer without a cast [enabled by default]
    foo.c:14:6: note: expected ‘struct STAK *’ but argument is of type ‘int’
    foo.c:84:21: error: too few arguments to function ‘push’
    foo.c:14:6: note: declared here
    foo.c:88:21: warning: passing argument 1 of ‘push’ makes pointer from integer without a cast [enabled by default]
    foo.c:14:6: note: expected ‘struct STAK *’ but argument is of type ‘int’
    foo.c:88:21: error: too few arguments to function ‘push’
    foo.c:14:6: note: declared here
    foo.c:92:21: warning: passing argument 1 of ‘push’ makes pointer from integer without a cast [enabled by default]
    foo.c:14:6: note: expected ‘struct STAK *’ but argument is of type ‘int’
    foo.c:92:21: error: too few arguments to function ‘push’
    foo.c:14:6: note: declared here
    foo.c:98:5: error: incompatible type for argument 1 of ‘isFull’
    foo.c:17:6: note: expected ‘int’ but argument is of type ‘STAK’
    foo.c:102:9: warning: implicit declaration of function ‘getch’ [-Wimplicit-function-declaration]
    foo.c:103:9: warning: implicit declaration of function ‘exit’ [-Wimplicit-function-declaration]
    foo.c:103:9: warning: incompatible implicit declaration of built-in function ‘exit’ [enabled by default]
    foo.c:113:13: error: ‘ret’ undeclared (first use in this function)
    foo.c:115:13: error: ‘bin’ undeclared (first use in this function)
    foo.c:118:5: warning: implicit declaration of function ‘dispRes’ [-Wimplicit-function-declaration]
    foo.c:48:19: warning: unused variable ‘tnum’ [-Wunused-variable]
    foo.c: At top level:
    foo.c:121:6: error: conflicting types for ‘isFull’
    foo.c:17:6: note: previous declaration of ‘isFull’ was here
    foo.c: In function ‘push’:
    foo.c:128:28: warning: assignment makes pointer from integer without a cast [enabled by default]
    foo.c: At top level:
    foo.c:131:5: error: conflicting types for ‘pop’
    foo.c:15:6: note: previous declaration of ‘pop’ was here
    foo.c: In function ‘pop’:
    foo.c:134:7: warning: assignment makes integer from pointer without a cast [enabled by default]
    make: *** [foo] Error 1
    That's a whole lot of errors. Basically, this is the result of mashing together random pieces of code and hoping it works.

    Your best bet is to toss this code, put away the keyboard, and get out the paper and pencil. Work through several small examples by hand until you understand how to solve the problem. Pay attention to every little step, as that will become the basis for your algorithm. When you have it all figured out, write your algorithm in pseudo code. Convert that pseudo code to real code, in small chunks. By small, I mean 5-10 lines at a time. Compile (at max warning level) every time you finish a chunk. Fix all errors/warnings, then test your code. Fix any bugs and do not move on to the next chunk until all previous code is 100% working and error free.

    To address the bulk of the errors above, your function prototype, definition, and anywhere you call it must match in return type, and in the number, order and type of parameters.

    EDIT
    * You need to #include the right headers for the functions you're using. stdlib.h for malloc, free and exit; and ctype.h for isdigit.
    * getch is non-standard, I suggest using getchar instead.
    * You need to declare ret and bin and any other functions/variables you plan on using.
    Last edited by anduril462; 04-27-2014 at 06:43 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamically Allocated Array
    By chris4 in forum C Programming
    Replies: 9
    Last Post: 05-06-2011, 10:01 AM
  2. Dynamically Allocated arrays and MPI
    By DerekC in forum C Programming
    Replies: 3
    Last Post: 03-09-2010, 01:06 PM
  3. Dynamically Allocated Array
    By vb.bajpai in forum C Programming
    Replies: 3
    Last Post: 06-17-2007, 08:40 AM
  4. Dynamically allocated memory
    By ^xor in forum Linux Programming
    Replies: 9
    Last Post: 06-28-2005, 11:42 AM
  5. Dynamically allocated matrices
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 10-27-2001, 10:10 PM