Given a stack of integers, calculate the sum of the integer values. Also, the stack should still be unchanged after you have calculated the sum Hint: take the items off the stack to sum them up and keep them on a second temporary stack so you can put them all back on after you have calculated the sum.
Since you have not mentioned the language of your preference, I am providing the code in Java.
CODE
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Stack<Integer> st = new Stack<>();
System.out.println("Enter the number of elements of the stack: ");
int n = sc.nextInt();
System.out.println("Enter the stack elements: ");
for (int i=0; i<n; i++) {
st.push(sc.nextInt());
}
Stack<Integer> temp = new Stack<>();
int sum = 0;
for (int el : st) {
int popped = st.pop();
sum += popped;
temp.push(popped);
}
for (int el : temp ) {
st.push(temp.pop());
}
System.out.println("Sum = " + sum);
System.out.println("Stack elements: \n" + st);
}
}
Given a stack of integers, calculate the sum of the integer values. Also, the stack should...
Write a program that uses a stack to reverse its inputs. Your
stack must be generic and you must demonstrate that it accepts both
String and Integer types. Your stack must implement the following
methods:
push,
pop,
isEmpty (returns true if the stack is empty and false
otherwise), and
size (returns an integer value for the number of items in the
stack).
You may use either an ArrayList or a LinkedList to implement
your stack. Also, your pop method must...
C++
Carefully review Program 19-2 in your textbook,
MathStack. This is a static stack, meaning that the size
of the stack is set at the beginning of the program (see line 11):
MathStack stack(5).
I would like you to modify this program as follows:
1. Implement it as a dynamic stack (do not use a static
stack as it is designed in the book). Use a linked list to
implement this. There's code in the book.
2. Add functionality for...
Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....
Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....
Design and implement a Θ(n) algorithm that will simultaneously find the largest and second-largest elements (integers) in an array. Note that the second largest element should be distinctly smaller than the largest element. You should also adhere to the following restrictions. (1) You should traverse through the array ONLY ONCE. (2) The array could have both positive and negative elements. So, you should NOT initialize any temporary variables to very small negative values as part of your algorithm. (3) You...
Create a loop that will populate a listbox with random values 14 pts The random numbers should range for ranging from 0 to 10 (10 must be a potential number that will be added). Keep adding values to the listbox until the sum of the values in the listbox equals or exceeds 100. After adding the values, tell me how many items are in the listbox, and the sum of the numbers, displaying these values in labels. Create a...
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...
**TStack.py below**
# CMPT 145: Linear ADTs
# Defines the Stack ADT
#
# A stack (also called a pushdown or LIFO stack) is a compound
# data structure in which the data values are ordered according
# to the LIFO (last-in first-out) protocol.
#
# Implementation:
# This implementation was designed to point out when ADT operations are
# used incorrectly.
def create():
"""
Purpose
creates an empty stack
Return
an empty stack
"""
return '__Stack__',list()
def is_empty(stack):
"""...
Subroutines in MIPS Determines the minimum of two integers Functions within the MIPS slides describe how one can use subroutines (also called procedures, functions, and methods) in MIPS. Because of the importance of subroutines in modern programming, most hardware designers include mechanisms to help programmers. In a high-level language like C or Java, most of the details of subroutine calling are hidden from the programmer. MIPS has special registers to send information to and from a subroutine. The registers $a0,...
Dynamic MathStack The MathStack class shown in this chapter only has two member functions: add and sub . Write the following additional member functions: Function Description mult - Pops the top two values off the stack, multiplies them, and pushes their product onto the stack. div - Pops the top two values off the stack, divides the second value by the first, and pushes the quotient onto the stack. addAll - Pops all values off the stack, adds them, and...