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'.
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the...
Reverse the contents of a stack using only stack operations [ push() and pop()  ]. Using the java and give the explanation
JAVA Stack - Implementation. You will be able to use the push, pop and peek of...
JAVA Stack - Implementation. You will be able to use the push, pop and peek of Stack concept. Post-Fix calculator - When an arithmetic expression is presented in the postfix form, you can use a stack to evaluate the expression to get the final value. For example: the expression 3 + 5 * 9 (which is in the usual infix form) can be written as 3 5 9 * + in the postfix. More interestingly, post form removes all parentheses...
(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...
Python - Please include running time of push(), pop(), and top() methods and tester code for...
Python - Please include running time of push(), pop(), and top() methods and tester code for all methods. Design a stack ADT using a single queue as an instance variable, and only constant additional local memory within the method bodies. What is the running time of the push(), pop(), and top() methods for your design? Implement your modified stack ADT in Python, including tester code to test all its methods.
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)
Consider an ADT with the same operators as a stack (push(), pop(), size(), empty()), except that...
Consider an ADT with the same operators as a stack (push(), pop(), size(), empty()), except that the pop() operator returns the largest element rather than the element most recently pushed. For this to make sense, the objects on the stack must be totally ordered (e.g. numbers). Describe array and linked list implementations of the ADT. Your implementations should be as time and space efficient as you can manage. Give the running times for the CRUD operators.
# ArrayStack # TODO: push and pop using array stack with doubling technique. import numpy as...
# ArrayStack # TODO: push and pop using array stack with doubling technique. import numpy as np from future import print_function # Definisi class ArrayStack class ArrayStack(object): def __init__(self): self.data = np.zeros(20, dtype=np.int) self.count = 0 def isEmpty(self): pass # ubah saya def size(self): pass # ubah saya def push(self, obj): pass # ubah saya def pop(self): pass # ubah saya #-------------------------- # End of class if __name__ == "__main__": stack = ArrayStack() randn = np.random.permutation(1000) for i in range(1000):...
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.
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H...
C++ Given Stack Code, Implements Queue. enqueue, dequeue. Modify to function like Queue. Stack #ifndef STACK_H #define STACK_H #include "Node.h" template class Stack { private: Node* top; public: // Constructor Stack() { top = nullptr; } void push(Object ab) { if (top != nullptr) { Node* newNode = new Node(ab, *top); top = newNode; } else { Node* newNode = new Node(ab); top = newNode; } } Object pop() { if (top != nullptr) { Node *returnVal = top; top...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT