Question

What would happen to the time complexity (Big-oh) of the methods (push, pop) in an array...

What would happen to the time complexity (Big-oh) of the methods (push, pop) in an array implementation of the stack if the top of the stack were at position 0? Explain.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
//This code is in java
public boolean push(int data){
    if (num < cap-1){
        stack[++num] = data;
        return true;
    }
    return false;
}

public int pop(){
    if (num >= 0){
        return stack[num--];
    }
    return

}

------------------------------------------------------------------------------------------------------

1 Push:

Adds a value at the end of the array, and incrementing the stack pointer by 1

So, time complexity = O(1)

------------------------------------------------------------------------------------------------------

2 Pop:

Decrementing the stack pointer by 1

So, time complexity = O(1)

------------------------------------------------------------------------------------------------------

Add a comment
Know the answer?
Add Answer to:
What would happen to the time complexity (Big-oh) of the methods (push, pop) in an array...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • What would happen to the time complexity (Big-oh) of the methods in an array implementation of...

    What would happen to the time complexity (Big-oh) of the methods in an array implementation of the stack if the top of the stack were at position 0? Explain.

  • I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and...

    I need to implement raw array Stack for create empty stack, isEmpty, isFull, push, pop, and size using below pseudo code. I need two classes with stack pseudo code implementation and the main method to demonstrate the correct working of each operation. pseudo code StackADT (using raw array) class StackADT { int top int items[] int max StackADT(int n) Initialize array to n capacity top = 0 max = n boolean isEmpty() if array has no elements return true else...

  • When using an array to implement a list ADT, what is the time complexity (Big-Oh) for...

    When using an array to implement a list ADT, what is the time complexity (Big-Oh) for finding an element in the list with N elements? (C++)

  • 1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show...

    1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show the Linked List after the following commands are executed: Stack myStack = new Stack(); myStack.push(20); myStack.push(40); myStack.pop(); myStack.push(60); myStack.push(80); 2)If the same commands were used but the Stack was implemented with an Array with maximum size of 10, show what the array would look like after all these commands are executed. Assume O(1) implementation of push and pop here as well. 3)Given a Queue...

  • 2. Consider a circular array based Queue as we have discussed in the lectures (class definition...

    2. Consider a circular array based Queue as we have discussed in the lectures (class definition given below for reference) public class CircArrayQueue<E> implements Queue<E private EI Q private int front-0 indicates front of queue l indicates position after end of queue private int end-0: public CircArrayQueue( public int getSize (.. public boolean isEmpty ( public void enqueue (E e)... public E dequeue ) throws EmptyQueueException... Il constructor We are interested in implementing a Stack class based on the above...

  • JAVA PROGRAM Design a stack that supports push, pop, top, and retrieving the minimum element in...

    JAVA PROGRAM Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. • push(x) -- Push element x onto stack. • pop() -- Removes the element on top of the stack. • top() -- Get the top element. • getMin() -- Retrieve the minimum element in the stack. Example 1: Input ["MinStack", "push", "push","push", "getMin","pop","top", "getMin"] [1], [-2], [0], [-3],[1,1],[1, [1] Output [null, null, null,null,-3, null,0,-2] Explanation MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0);...

  • For merge sort the time complexity is Θ(nlogn), but what if we had two unsorted stacks...

    For merge sort the time complexity is Θ(nlogn), but what if we had two unsorted stacks and wanted to but merge them into one final sorted stack! what is the time complexity then? code / Java program to merge to unsorted stacks // into a third stack in sorted way. import java.io.*; import java.util.*;    public class GFG {            // This is the temporary stack     static Stack<Integer> res = new Stack<Integer>();     static Stack<Integer> tmpStack = new Stack<Integer>();            //...

  • Stack.h Stack_test.cpp What would happen if we add s2.pop(); or cout << s2.top(); to the end...

    Stack.h Stack_test.cpp What would happen if we add s2.pop(); or cout << s2.top(); to the end of the program (when s2 is empty?) Fix Stack.h so that these problems would not occur. #ifndef STACK H #define STACK H /1 your name // Stack.h // date // description #include «vector» using namespace std; template <typename T> class Stack vector<T> container; public: Stack (): container) void push (T x) container.push back(x); void pop) container.pop back); T top) t return container.back); bool empty()...

  • In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> {...

    In Java. What would the methods of this class look like? StackADT.java public interface StackADT<T> { /** Adds one element to the top of this stack. * @param element element to be pushed onto stack */ public void push (T element);    /** Removes and returns the top element from this stack. * @return T element removed from the top of the stack */ public T pop(); /** Returns without removing the top element of this stack. * @return T...

  • Java - data structures Suppose that in the array-based stack, the array doubles in size after...

    Java - data structures Suppose that in the array-based stack, the array doubles in size after multiple push operations. But later on, fewer than half of the array’s locations might actually be used by the stack due to pop operations. Revise the implementation so that its array also can shrink in size as objects are removed from the stack. Accomplishing this task will require two new private methods, as follows: The first new method checks whether we should reduce the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT