Question

In: Computer Science

Please code in C /* Implements functions that operate on Stack 1. PUSH 2. POP 3....

Please code in C


/* Implements functions that operate on Stack
   1. PUSH
   2. POP 
   3. isEmpty
   4. PEEK
   5. Size
  */

#include <stdio.h>

#define CAPACITY 1000

//Two stacks .. for each stack we need
// 1. An Array that can hold capacity of elements
// 2. A top initialzied to -1 (signifying that the stak is empty at the start)
//NOTE : THESE STACKS ARE OF TYPE CHAR :(  ... so you need to FIX IT!!!! to int and every function as well :((

char stack1[CAPACITY];
char stack2[CAPACITY];

int top1 = -1;
int top2 = -1;

int push(char stack[], int top, char data){
        if(*top<CAPACITY-1){
                *top += 1;
                stack[*top] = data;
                return 1;
        }
        return 0;
}

char pop(char stack[], int* top){
        char c;
        if(*top > -1){
                c = stack[*top];
                *top -=1;
        }
        return c;
} 


char peek(char stack[], int* top){
        char c = 0;
        if(*top > -1){
                c = stack[*top];
        }
        return c;
}

int isEmpty(char stack[],int* top){
        if (*top == -1)
                return 1;
        return 0;
}

int size(char stack[],int* top){
        return *top+1;
}



int main(){
        //YOUR CODE HERE for REVESE POLISH NOTATION CALCULATION!
}

Solutions

Expert Solution

Here's a C Code for the question:

int stack1[CAPACITY];
int stack2[CAPACITY];

int top1 = -1;
int top2 = -1;

int push(int stack[], int top, int data){
        if(*top<CAPACITY-1){
                *top += 1;
                stack[*top] = data;
                return 1;
        }
        return 0;
}

int pop(int stack[], int* top){
        int c;
        if(*top > -1){
                c = stack[*top];
                *top -=1;
        }
        return c;
} 


int peek(int stack[], int* top){
        int c = 0;
        if(*top > -1){
                c = stack[*top];
        }
        return c;
}

int isEmpty(int stack[],int* top){
        if (*top == -1)
                return 1;
        return 0;
}

int size(int stack[],int* top){
        return *top+1;
}



int evaluateReversePolish(char *exp){

        for (i = 0; exp[i]; i++) 
        { 
        // If the scanned character is an operand (number here), 
        // push it to the stack. 
                if (exp[i]>='0'&&exp[i]<='9') 
                    push(stack1, exp[i] - '0'); 

                // If the scanned character is an operator, pop two 
                // elements from stack apply the operator 
                else
                { 
                    int val1 = pop(stack1); 
                    int val2 = pop(stack1); 
                    switch (exp[i]) 
                    { 
                            case '+': push(stack1, val2 + val1); break; 
                            case '-': push(stack1, val2 - val1); break; 
                            case '*': push(stack1, val2 * val1); break; 
                            case '/': push(stack1, val2/val1); break; 
                    } 
                } 
        } 
        return pop(stack1); 
}

Related Solutions

All code should be in Python 3. Implement the Stack Class, using the push, pop, str,...
All code should be in Python 3. Implement the Stack Class, using the push, pop, str, init methods, and the insurance variable 'list'.
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use...
(In Java) Design a stack that supports getMin(), pop() and push() in O(1) time. Must use the iterator and comparator, does not need to be a linked list, although it's what I've tried using. I'm using another tester class to test this class. import java.util.Comparator; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.Stack; import java.util.ListIterator; public class FMinStack<T> implements MinStack<T> { protected Comparator<? super T> comp; T min; protected List<T> ds; public FMinStack() { this(new DefaultComparator<T>()); } public FMinStack(Comparator<? super...
Write a function that returns the largest value in a stack (only use push and pop)
Write a function that returns the largest value in a stack (only use push and pop)
Write push and pop functions on C/C++. Have a program having 10 random integers(hardcoded)and put them...
Write push and pop functions on C/C++. Have a program having 10 random integers(hardcoded)and put them on your stack. Also, print them. Read a character from the keyboard. If the character is an “o” then pop from the stack but don’t print. If the character is a “p”, then pop from the stack and print that number. If the character is an “e” or the stack is empty, end the program.
5 marks] A MinStack supports three main operations: the standard Stack operations push(x) and pop() and...
5 marks] A MinStack supports three main operations: the standard Stack operations push(x) and pop() and the non-standard min() operation which returns the minimum value stored on the stack. The zip file gives an implementation SlowMinStack that implements these operations so that push(x) and pop() each run in O(1) time, but  min()runs in Θ(n) time. For this question, you should complete the implementation of FastMinStack that implements all three operations in O(1) time per operation. As part of your implementation, you...
C++ Please Fill in for the functions for the code below. The functions will be implemented...
C++ Please Fill in for the functions for the code below. The functions will be implemented using vectors ONLY. Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public: // Default Constructor Stack() {// ... } // Push integer n onto top of...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer list using dynamic array ONLY (an array that can grow and shrink as needed, uses a pointer an size of array). Additional public helper functions or private members/functions can be used. The List class will be instantiated via a pointer and called similar to the code below: class List { public: // Default Constructor List() {// ... } // Push integer n onto...
C++ Please Fill in for the functions for the code below. The functions will implement an...
C++ Please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ please Fill in for the functions for the code below. The functions will implement an...
C++ please Fill in for the functions for the code below. The functions will implement an integer stack using deques ONLY. It is possible to use only one deque but using two deques also works. Additional public helper functions or private members/functions can be used. The Stack class will be instantiated via a pointer and called as shown below: Stack *ptr = new Stack(); ptr->push(value); int pop1 = ptr->pop(); int pop2 = ptr->pop(); bool isEmpty = ptr->empty(); class Stack{     public:...
C++ Please complete based on the code below. Declare another stack object to the code in...
C++ Please complete based on the code below. Declare another stack object to the code in main(). Add a stack operator called CopyStack to the Stack class which, when executed, copies the contents of the first stack into the second stack. Modify your menu so that this option is available. The menu should also allow the second stack to be printed, pushed, popped, and so forth, just like with the first stack. #include using namespace std; #define MAXSize 10 class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT