Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Stack.cpp: #include utility.h #include Stack.h Error_code Stack::push(const Stack_entry &item) /* Pre: None. Post: If the Stack is not full, item is added to the top

image text in transcribed

Stack.cpp:

#include "utility.h" #include "Stack.h"

Error_code Stack::push(const Stack_entry &item) /* Pre: None. Post: If the Stack is not full, item is added to the top of the Stack. If the Stack is full, an Error_code of overflow is returned and the Stack is left unchanged. */

{ Error_code outcome = success; if (count >= maxstack) outcome = overflow; else entry[count++] = item; return outcome; }

Error_code Stack::pop() /* Pre: None. Post: If the Stack is not empty, the top of the Stack is removed. If the Stack is empty, an Error_code of underflow is returned. */

{ Error_code outcome = success; if (count == 0) outcome = underflow; else --count; return outcome; }

Error_code Stack::top(Stack_entry &item) const /* Pre: None. Post: If the Stack is not empty, the top of the Stack is returned in item. If the Stack is empty an Error_code of underflow is returned. */

{ Error_code outcome = success; if (count == 0) outcome = underflow; else item = entry[count - 1]; return outcome; }

bool Stack::empty() const /* Pre: None. Post: If the Stack is empty, true is returned. Otherwise false is returned. */

{ bool outcome = true; if (count > 0) outcome = false; return outcome; }

Stack::Stack() /* Pre: None. Post: The stack is initialized to be empty. */ { count = 0; }

Stack.h:

const int maxstack = 10; // small value for testing

typedef double Stack_entry;

class Stack { public: Stack(); bool empty() const; Error_code pop(); Error_code top(Stack_entry &item) const; Error_code push(const Stack_entry &item);

private: int count; Stack_entry entry[maxstack]; };

Utility.h:

#include

using namespace std;

enum Error_code { success, range_err, underflow, overflow, fail, not_present, duplicate_error, entry_inserted, entry_found, internal_error };

Main.cpp:

#include "utility.h" #include "Stack.h"

int main() /* Pre: The user supplies an integer n and n decimal numbers. Post: The numbers are printed in reverse order. Uses: The STL class stack and its methods */

{ int n; Stack_entry item; Stack numbers; // declares and initializes a stack of numbers

cout > n;

for (int i = 0; i > item; numbers.push(item); }

cout

CStack.cpp:

#include "utility.h" #include "Stack.h"

Error_code Stack::push(const Stack_entry &item) /* Pre: None. Post: If the Stack is not full, item is added to the top of the Stack. If the Stack is full, an Error_code of overflow is returned and the Stack is left unchanged. */

{ Error_code outcome = success; if (count >= maxstack) outcome = overflow; else entry[count++] = item; return outcome; }

Error_code Stack::pop() /* Pre: None. Post: If the Stack is not empty, the top of the Stack is removed. If the Stack is empty, an Error_code of underflow is returned. */

{ Error_code outcome = success; if (count == 0) outcome = underflow; else --count; return outcome; }

Error_code Stack::top(Stack_entry &item) const /* Pre: None. Post: If the Stack is not empty, the top of the Stack is returned in item. If the Stack is empty an Error_code of underflow is returned. */

{ Error_code outcome = success; if (count == 0) outcome = underflow; else item = entry[count - 1]; return outcome; }

bool Stack::empty() const /* Pre: None. Post: If the Stack is empty, true is returned. Otherwise false is returned. */

{ bool outcome = true; if (count > 0) outcome = false; return outcome; }

Stack::Stack() /* Pre: None. Post: The stack is initialized to be empty. */ { count = 0; }

Please do in C++

Purpose In this assignment, you will review template class, template function, makefile, and a singly linked list. Requirements (1) A templatized Stack Modify the given code of Stack with Array that is posted under Programs on D2L and make sure the following requirements are met in your code Stack class is made as a template class If you are not familiar with templates, refer to Templates file and Lecture Video Templates under Prog II Review on D2L The internal data structure of Stack uses a singly linked List. To review how a singly linked list, download all the programs that I posted to D2L and review code from List with Linked List The template Stack must provide destructor additionally where the address of each node is displayed on the console as it is deallocated. a. b. c. (2) struct type Book Add a struct type named Book that has the following attributes: ISBN - Title - Author - Publisher along with release date (3) Testing With a reference to main.cpp of Stack with Array and main.cpp of List with Linked List provided, enhance it so that you test the template Stack class for int type and Book type. The required details are as follows Each member function of the Stack class must be clearly tested for each data type, int and Book From the output, the instructor must be able to clearly see what member function is being tested and the state of the Stack object after each test of a member function. Do not interact with the user to receive inputs or to fill the Stack objects, so that program testing time can be reduced. a. b. c. d. When you fill the stack with many keys or elements, use a loop for efficiency e. Provide a makefile though you may have only single .cpp f. remove unused functions and files Purpose In this assignment, you will review template class, template function, makefile, and a singly linked list. Requirements (1) A templatized Stack Modify the given code of Stack with Array that is posted under Programs on D2L and make sure the following requirements are met in your code Stack class is made as a template class If you are not familiar with templates, refer to Templates file and Lecture Video Templates under Prog II Review on D2L The internal data structure of Stack uses a singly linked List. To review how a singly linked list, download all the programs that I posted to D2L and review code from List with Linked List The template Stack must provide destructor additionally where the address of each node is displayed on the console as it is deallocated. a. b. c. (2) struct type Book Add a struct type named Book that has the following attributes: ISBN - Title - Author - Publisher along with release date (3) Testing With a reference to main.cpp of Stack with Array and main.cpp of List with Linked List provided, enhance it so that you test the template Stack class for int type and Book type. The required details are as follows Each member function of the Stack class must be clearly tested for each data type, int and Book From the output, the instructor must be able to clearly see what member function is being tested and the state of the Stack object after each test of a member function. Do not interact with the user to receive inputs or to fill the Stack objects, so that program testing time can be reduced. a. b. c. d. When you fill the stack with many keys or elements, use a loop for efficiency e. Provide a makefile though you may have only single .cpp f. remove unused functions and files

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Handbook Of Relational Database Design

Authors: Candace C. Fleming, Barbara Von Halle

1st Edition

0201114348, 978-0201114348

More Books

Students also viewed these Databases questions