Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
Chapter 16, Problem 5PP
Program Plan Intro
- Include required library files.
- Define a class named “StackOverflowException”.
- Inside the access specifier “public”,
- Define a constructor to assign the message
- Declare a parameterized constructor to assign “msg” to message.
- Define a function “display” to return message.
- Inside the access specifier “private”.
- Declare a string variable “message”.
- Define a class named “StackEmptyException”.
- Inside the access specifier “public”,
- Define a constructor to assign the message
- Declare a parameterized constructor to assign “msg” to message.
- Define a function “display” to return message.
- Inside the access specifier “private”.
- Declare a string variable “message”.
- Define a class named “Stack”.
- Declare an integer array and variable.
- Inside the access specifier “public”,
- Define a constructor to assign “-1” to “top”.
- Define a “push()” function.
- “try” block to check the top is equal to “3”.
- The condition is true, throw exception.
- Otherwise increment the top and the value is assigned to stack.
- “catch” block to display the error message.
- Define a “pop()” function.
- “try” block to check the top is equal to “-1”.
- The condition is true, throw exception.
- Otherwise decrement the top and return the value.
- “catch” block to display the error message.
- Define a “main()” function.
- Create an object for class “Stack”.
- Then check the “push()” and “pop()” function.
- Inside the access specifier “public”,
- Define a constructor to assign the message
- Declare a parameterized constructor to assign “msg” to message.
- Define a function “display” to return message.
- Inside the access specifier “private”.
- Declare a string variable “message”.
- Inside the access specifier “public”,
- Define a constructor to assign the message
- Declare a parameterized constructor to assign “msg” to message.
- Define a function “display” to return message.
- Inside the access specifier “private”.
- Declare a string variable “message”.
- Declare an integer array and variable.
- Inside the access specifier “public”,
- Define a constructor to assign “-1” to “top”.
- Define a “push()” function.
- “try” block to check the top is equal to “3”.
- The condition is true, throw exception.
- Otherwise increment the top and the value is assigned to stack.
- “catch” block to display the error message.
- “try” block to check the top is equal to “3”.
- Define a “pop()” function.
- “try” block to check the top is equal to “-1”.
- The condition is true, throw exception.
- Otherwise decrement the top and return the value.
- “catch” block to display the error message.
- “try” block to check the top is equal to “-1”.
- Create an object for class “Stack”.
- Then check the “push()” and “pop()” function.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
stacktype.h
stacktype.cPP
#ifndef STACKTYPE_H_INCLUDED
tdefine STACKTYPE H_INCLUDED
#include "StackType.h"
template
StackType::StackType ()
const int MAX_ITEMS - 5;
top - -1;
class Fulistack
// Exception class thrown
// by Push when stack is full.
template
bool StackType: :IsEmpty ()
class EmptyStack
// Exception class thrown
// by Pop and Top when stack is emtpy.
return (top = -1);
template
bool StackType: :ISFull ()
template
class StackType
return (top --
MAX_ITEMS-1):
template
void StackType: :Push (ItemType newItem)
public:
StackType () :
bool IsFull ():
bool IsEmpty ():
void Push (ItemType);
void Pop () i
if( IsFull ()) throw Fullstack ();
top++:
items (top]- newItem;
template
void StackType:: Pop ()
ItemType Top ():
private:
int top:
ItemType items [MAX_ITEMS ];
if( IsEmpty ()) throw EmptyStack ();
top--
};
tendif // STACKTYPE_H_INCLUDED
template
ItemType StackType::Top ()
if (IsEmpty ()) throw Emptystack ():
return items [top];
Generate the driver file (main.cpp) where…
2. Write an application utilizing JAVA-ready Stack class and perform the following.
Create a new instance of the Stack of type double.
Use a loop to get 5 input from the user and insert into the stack created. The inputs are
3.4, 10.1, 10.3, 11.5, 3.5.
Write a statement to display the content of the stack. Perform pop() all elements and
display on the screen if the number is greater than 5.0. Use the output sample given to
guide your code.
Sample output:
[3.4, 10.1, 10.3, 11.5, 3.5]
11.5 10.3 10.1
Stack:
Stacks are a type of container with LIFO (Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only. Your Stack should not be of the fixed sized. It should be able to grow itself. So using the class made in task 1, make a class named as Stack, having following additional functionalities:
bool empty() : Returns whether the Stack is empty or not. Time Complexity should be: O(1)
bool full() : Returns whether the Stack is full or not. Time Complexity should be: O(1)int size() : Returns the current size of the Stack. Time Complexity should be: O(1)Type top () : Returns the last element of the Stack. Time Complexity should be: O(1)
void push(Type) : Adds the element of type Type at the top of the stack. Time Complexity should be: O(1)
Type pop() : Deletes the top most element of the stack and returns it. Time Complexity should be: O(1)
Write non-parameterized constructor for the above class.
Write Copy…
Chapter 16 Solutions
Problem Solving with C++ (10th Edition)
Ch. 16.1 - Prob. 1STECh. 16.1 - What would be the output produced by the code in...Ch. 16.1 - Prob. 3STECh. 16.1 - What happens when a throw statement is executed?...Ch. 16.1 - In the code given in Self-Test Exercise 1, what is...Ch. 16.1 - Prob. 6STECh. 16.1 - Prob. 7STECh. 16.1 - What is the output produced by the following...Ch. 16.1 - What is the output produced by the program in...Ch. 16.2 - Prob. 10STE
Knowledge Booster
Similar questions
- Create an application that reads a statement from the user and outputs it with the characters of each word reversed. To reverse the characters in each word, use a stack.Create an application that reads a statement from the user and outputs it with the characters of each word reversed. To reverse the characters in each word, use a stack.arrow_forwardBriefly describe the stack parameter.arrow_forwardEx1) Given the file arrayImpOfStack.java then write a main method to read a sequence of numbers and using the stack operation print them in reverse order. Ex2) Given the file pointerImOfStack.java then write a main method to read a decimal number and print its equivalent binary number. Ex3) Given the file arrayImpOfStack.java then write a main method:- declaring two stack objects S1, S2;- reads a sequence of numbers; store the even ones in the stack S1, and store theodd ones in the stack S2.- print the even numbers followed by odd ones. Ex4) Given the file pointerImOfQueue.java then write a main method: to- add some elements into the queue,- reverse the queue contents, (Hint use an array)- print the queue contents. *************Solution using java*****************arrow_forward
- C# language Write a program that creates a Queue or Stack (your choice) that represents a list of work orders. This program should use loop, allowing the user to push and pop items on the stack / queue. The program should also allow the user to print all the items in the stack / queue to the console.arrow_forwardQ1: Write a java application for the stack operations with Linked List. Q2: Write java program to take the order from the customer and display the ordered items and total payment. If customer is entering the wrong name or not following the order to enter the menu it has to display error messages. (Find in the sample code) Sample Output:arrow_forwardComputer science JAVA programming languarrow_forward
- Nesting procedure calls how many return addresses will be on the stack by the time Sub3 is called? main PROC call Subl exit main ENDP Subl PROC call Sub2 ret Subl ENDP Sub2 PROC call Sub3 ret Rectangu Sub2 ENDP Sub3 PROC ret Sub3 ENDP 2 O1 3arrow_forwardIt would be beneficial to have the purpose of the back stack as well as the back button defined.arrow_forwardCreate a menu driven application that will implement a stack and the user will be asked about the size of an array they wanted.arrow_forward
- C++ Question You need to write a class called LinkedList that implements the following List operations: public void add(int index, Object item); // adds an item to the list at the given index, that index may be at start, end or after or before the // specific element 2.public void remove(int index); // removes the item from the list that has the given index 3.public void remove(Object item); // finds the item from list and removes that item from the list 4.public List duplicate(); // creates a duplicate of the list // postcondition: returns a copy of the linked list 5.public List duplicateReversed(); // creates a duplicate of the list with the nodes in reverse order // postcondition: returns a copy of the linked list with the nodes in 6.public List ReverseDisplay(); //print list in reverse order 7.public Delete_Smallest(); // Delete smallest element from linked list 8.public List Delete_duplicate(); // Delete duplicate elements from a given linked list.Retain the…arrow_forwardC++ Question You need to write a class called LinkedList that implements the following List operations: public void add(int index, Object item); // adds an item to the list at the given index, that index may be at start, end or after or before the // specific element 2.public void remove(int index); // removes the item from the list that has the given index 3.public void remove(Object item); // finds the item from list and removes that item from the list 4.public List duplicate(); // creates a duplicate of the list // postcondition: returns a copy of the linked list 5.public List duplicateReversed(); // creates a duplicate of the list with the nodes in reverse order // postcondition: returns a copy of the linked list with the nodes in 6.public List ReverseDisplay(); //print list in reverse order 7.public Delete_Smallest(); // Delete smallest element from linked list 8.public List Delete_duplicate(); // Delete duplicate elements from a given linked list.Retain the…arrow_forwardStack: Stacks are a type of container with LIFO (Last In First Out) type of working, where a new element is added at one end and (top) an element is removed from that end only. Your Stack should not be of the fixed sized. It should be able to grow itself. bool empty() : Returns whether the Stack is empty or not. Time Complexity should be: O(1) bool full() : Returns whether the Stack is full or not. Time Complexity should be: O(1)int size() : Returns the current size of the Stack. Time Complexity should be: O(1)Type top () : Returns the last element of the Stack. Time Complexity should be: O(1) void push(Type) : Adds the element of type Type at the top of the stack. Time Complexity should be: O(1) Type pop() : Deletes the top most element of the stack and returns it. Time Complexity should be: O(1) Write non-parameterized constructor for the above class. Write Copy constructor for the above class. Write Destructor for the above class. Now write a global function show stack which…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- C++ Programming: From Problem Analysis to Program...Computer ScienceISBN:9781337102087Author:D. S. MalikPublisher:Cengage Learning
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning