Reverse polish notation in C

profileaaditya1996

here is the starter code : 



/* 

 * File:   main.c

 * Author: MSU

 *

 * Created on March 13, 2019, 1:45 PM

 */


#include <stdio.h>

#include <stdlib.h>


#define MAXSIZEI 8

#define MAXSIZEC 7

int top = -1;


int isempty(int stack[]);

int isfull(int stack[]);

int peek(int stack[]);

int pop(int stack[]);

int push(int data, int stack[]);

int printStack(int stack[]);

int size();


int main()

{

    //push item on to the stack

    int stack[8];

    int ssize;

    push(3, stack);

    push(5, stack);

    push(9, stack);

    ssize = size();

    printf("The size of the stack is: %d\n", ssize);

    push(1, stack);

    push(12, stack);

    push(15, stack);

    ssize = size();

    printf("The size of the stack is: %d\n", ssize);

    

    printf("Element at top of the stack: %d\n", peek(stack));

    printStack(stack);

    

    if(isfull(stack) ==1)

        printf("Stack full\n");

    else if(isempty(stack)== 1)

        printf("Stack empty\n");

    

return 0;    

    

}

int printStack(int stack[])

{

    printf("Element: \n");

    while(!isempty(stack)){

        int data = pop(stack);

        printf("%d\n", data);

    }

}


int isempty(int stack[])

{

    if(top == -1)

        return 1;

    else 

        return 0;

}


int isfull(int stack[])

{

    if(top == MAXSIZEI)

        return 1;

    else

        return 0;

}


int peek(int stack[])

{

    return stack[top];

}


int pop(int stack[])

{

    int data;

    

    if(!isempty(stack)){

        data = stack[top];

        top = top - 1;

        return data;

    } else{

        printf("Could not retrieve data, Stack is empty");

        

    }

}


int push(int data, int stack[]){

    

    if(!isfull(stack)){

        top = top + 1;

        stack[top] = data;

    } else{

        printf("Count not insert data. Stack is full.\n");

    }

}

int size()

{

    return top + 1;

}

  • 6 years ago
  • 10
Answer(1)

Purchase the answer to view it

blurred-text
  • attachment
    updated2.c