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.
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:
For pop(x) operation:
Remove one element from the top of the stack and let us call it as z
Example:
| Element Inserted | Elements in Stack | minimumElement |
|---|---|---|
| 4 | 4 | 4 |
| 7 | 4, 7 | 4 |
| 1 | 4, 7, -2 | 1 |
| Number popped | Original Number | Elements in stack | minimumElement |
|---|---|---|---|
| 4, 7, -2 | 1 | ||
| -2 | 1 | 4, 7 | 4 |
| 7 | 7 | 4 | 4 |
Key Points:
If the answer helped please upvote. It means a lot. For any query comment below.
NO NEED TO WRITE CODE,EXPLAIN IN C++ PLEASE. Suppose we have a Stack that can grow...
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, 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 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 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 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 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 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 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 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 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...