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_forwardA group of objects that have been piled atop one another is known as a stack. Which kinds of applications make use of stacks are there?arrow_forwardWhat operations may be carried out on a stack?arrow_forward
- Q1: 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_forwardNesting 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_forward
- It 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_forwardWrite a sequence of statements that display a subroutine’s return address. Be sure that whatever modifications you make to the stack do not prevent the subroutine from returning to itscallerarrow_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