Question

NO NEED TO WRITE CODE,EXPLAIN IN C++ PLEASE. Suppose we have a Stack that can grow...

NO NEED TO WRITE CODE,EXPLAIN IN C++ PLEASE.

Suppose we have a Stack that can grow indefinitely (for example, the push method has been fixed to double the size of the array when at capacity instead of throwing a StackFullException). We want to create a second Stack data structure, which I promise will always contain only comparable items (e.g., integers, strings). We also want to add a function findMin to the Stack interface that will return the smallest element currently in the stack. We could implement this by searching the array, but that takes time linear in the number of elements in the Stack. Explain how you would change the Stack data structure to allow for this function to run in O(1) time. If you are storing additional private member data, state what else you are storing. If you are changing existing functions push, pop, or top (or the constructor/size
functions), explain briefly how you are changing them. Their running times must still be O(1); for example, you cannot search the full stack for the newest min at every push and pop.

You may assume that there will never be a duplicate item pushed to the Stack.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

For finding the minimum element in stack in O(1) time and O(1) space, we need to declare a variable which will always store the minimum element value. Let the variable be minimumElement.

Let us first see for push(x) operation:

  • If the stack is empty push x and set minimumElement = x
  • If the stack is not empty and x is greater than the minimumElement then insert x directly into the stack as at this time we don't need to update the minimumElement
  • If the stack is not empty and x is lesser than the minimumElement then insert (2*x - minimumElement) and set minimumElement = x. Example if the previous minimumElement was 5 and now we need to insert 1 then insert (2*1-5) ,i.e., insert -3 in the stack and set minimumElement = 1.

For pop(x) operation:

Remove one element from the top of the stack and let us call it as z

  • If the removed element is greater than or equal to the minimumElement then no need to change anything as the minimum element is in the stack only.
  • If the removed element is smaller than the minimumElement then we set minimumElement = (2*minimumElement-z). This is the place where the previous minimum element was found using the current minimum element. Example if z=2 and minimumElement = 4 then we update minimumElement = (2*4-2) = 6. So new minimumElement = 6

Example:

Push(x) Operation
Element Inserted Elements in Stack minimumElement
4 4 4
7 4, 7 4
1 4, 7, -2 1
  • First stack is empty and we inserted 4 so minimumElement = 4. Minimum element in stack is 4.
  • Then we insert 7 and as 7>minimumElement then it will be inserted directly without changing anything. Minimum element in stack is still 4
  • Then we want to insert 1 and as 1<minimumElement then we will insert 2*1-minimumElement ,.i.e, -2 and minimumElement=1. Now minimum element in stack is 1.
Pop() Operation
Number popped Original Number Elements in stack minimumElement
4, 7, -2 1
-2 1 4, 7 4  
7 7 4 4
  • Currently minimumElement = 1 which is the minimum element.
  • When we pop -2 then we find that -2<1 so the element to be popped is the minimum element in the stack ,i.e., minimumElement which is 1. So 1 which was the minimum element will be removed and new minimumElement=(2*1-(-2)) = 4.
  • So after removing 1 we can see that stack has two elements 4 and 7 and 4 is the minimum.
  • Now when 7 will be popped we will see 7>minimumElement so no need to change anything.

Key Points:

  • The minimum Element is always stored in minimumElement and not in stack.
  • The value stored in stack are used to find the element which was previously minimum.

If the answer helped please upvote. It means a lot. For any query comment below.

Add a comment
Know the answer?
Add Answer to:
NO NEED TO WRITE CODE,EXPLAIN IN C++ PLEASE. Suppose we have a Stack that can grow...
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
  • c program Here we see a Stack ADT implemented using array. We would like the stack...

    c program Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include <stdio.h> #include <stdlib.h> typedef struct { int *data;   // stack data, we assume integer for simplicity int top;     // top of the stack int maxSize; // max size of the stack } Stack; void StackInit(Stack* stack, int size) {     // this...

  • Suppose I want to implement the public member functions of a Stack (push, pop, top, size,...

    Suppose I want to implement the public member functions of a Stack (push, pop, top, size, isEmpty). However, instead of the private member data we had in class, I have only a single Queue. You may assume that the Queue has unbounded capacity, but is otherwise as per the one shown in class. Explain at a high level (you do not need to provide C++ code, but someone should be able to understand how you would code it from what...

  • USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class...

    USING THE C++ CODE BELOW, CAN YOU PLEASE SOLVE THE QUESTION AND EXPLAIN THE STEPS. class StaticStack { private: int *stack; int capacity; int top; // index of the top element public: StaticStack(int size); // constructor ~StaticStack(); bool isFull(); bool isEmpty(); void push(int); int pop(); }; #include "StaticStack.h" #include <iostream> using namespace std; StaticStack::StaticStack(int size) { stack = new int[size]; // constructing a size sized array capacity = size; top = -1; // empty stack    } StaticStack::~StaticStack() { delete...

  • In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will co...

    In C++ Task 3: Use the stack and queue to simulate receiving and transforming data We are creating a system that will convert strings sent over a serial bus one character at a time. The conversion will be from big to little endian or from little to big endian. To simplify this, each character will be considered a word. Little endian will have the lowest address first. Big endian will have the biggest address first. For example (for this lab),...

  • Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your...

    Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk—the first one to be removed is the last one placed on top.) A queue...

  • Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles...

    Review the Stack implementation with Vector, and implement/answer the following methods. Stack One of the principles of good programming is to reuse existing code whenever practical. If you can reuse existing code, you don't need to spend the time to rewrite it. Code used previously has also been debugged, and will likely contain fewer errors. One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we...

  • Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that p...

    Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that passes in a stack and returns (using a return statement) the number of items in the stack (the stack size). a. assume the this function is to be toolkit function in the implementation of the ADT stack. b. assume that this function is not a toolkit function. 2. Given the declaration: s = stack i = item struct STACK { INFO_RC i; int top; }...

  • I need help with this code This is what I need to do: Implement the Stack...

    I need help with this code This is what I need to do: Implement the Stack Class with an ArrayList instead of an array, including the following functions: • empty • push • peek • pop • overrided toString( ) function which returns all of the stack’s contents Things to note: • You no longer need a size. • You no longer need to define a constant DEFAULT_CAPACITY. Since ArrayLists grow dynamically. • Whenever possible, use ArrayList functions instead of...

  • I need help with a c++ code, i am new learner on stack, please help me...

    I need help with a c++ code, i am new learner on stack, please help me write Stack.cpp according to Stack.h provided, thanks(Notes: That data structure is a singly linked list in which pushed items are placed at the tail of the linked list. Similarly, popped items will be removed from the tail of the list.) Actually,I can write a regular one, but i'm not sure how to use the tail in the question. class Stack { private: // Desc:...

  • We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are...

    We as humans write math expression in infix notation, e.g. 5 + 2 (the operators are written in-between the operands). In a computer’s language, however, it is preferred to have the operators on the right side of the operands, i.e. 5 2 +. For more complex expressions that include parenthesis and multiple operators, a compiler has to convert the expression into postfix first and then evaluate the resulting postfix. Write a program that takes an “infix” expression as input, uses...

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