Question: Unique Stack In this assignment, you will write a template class implementation of a unique stack data structure. You will also be required to handle
Unique Stack
In this assignment, you will write a template class implementation of a unique stack data structure. You will also be required to handle all errors in functions using exceptions and monitor all stack operations by using exception handling. For an overview and possible implementation of a stack, you may want to read Chapter 18.1 (the pages for chapter 18.1 of my book is shown below after all of the text).
A Unique Stack is a special kind of stack that only allows unique items to be pushed onto it. This means no duplicate items are allowed in the stack. A Unique Stack may contain any data type. For example: integers, floats, characters, strings, Person classes, etc. The Unique Stack is able to handle any data type because it uses templates instead of a data type.
Programming Tasks:
1.) Create a template Unique Stack class named TUStack with the following public member functions:
1.1 TUStack(int nSize);
This is the constructor that initializes the maximum size of the stack, where 'nSize' is the maximum number of items. The array of items should be dynamic
1.2 void push(T item);
This function 'pushes' a new, unique item onto the top of the stack.
1.3 T pop();
This function 'pops', or removes, an item from the top of the stack and returns it to the caller. Before returning, the function should make sure that the second item on the stack is now the first (and so on).
1.4 int size()
This function returns the number of items in the stack
1.5 int position()
This function returns the current position of the stack pointer (ie, the top of the stack)
1.6 operator[]
Overload the [] operator so that it returns a copy of the item located at a specified index. This operation should be read-only.
2.) In addition to the above required public member functions of your class, you must include exception handling to disallow any error conditions to occur in your code. Each exception class must allow the calling code (the code that uses the stack class) to obtain a detailed message about the nature of the error, the current stack size, the current position of the stack pointer, and the new input item if applicable. Here are a few exceptions you have to consider:
2.1 initialize the maximum size of the stack with a negative value
2.2 push duplicate items
2.3 push an item when the stack is full
2.4 return a copy of the item at an illegal index (e.g., negative index and index above the maximum size of the stack)
2.5 pop value when the stack is empty
3.) Write driver code in your main program that clearly demonstrates all the required elements of this assignment. Specifically, your driver program must test two kinds of stack: a stack with integers and a stack with strings. Make sure to add appropriate comments to each of your test cases. It is your responsibility to prove to the grader your code is correct, not for him to guess if it worked correctly
Helpful Hints:
1.) Design, implement, and test a stack with integers without considering exception handling.
2.)Change the stack with integers to a stack with strings without considering exception handling.
3.) Add the exception handling.
4.) Modify the tested example to be a template.
5.) Make sure that there are only two files, TUStack.h and MainProg.cpp. (No TUSTack.cpp!)
Here are all the pages of chapter 18.1 of my book:









18 Stacks and Queues TOPICS 18.1 Introduction to the Stack ADT 18.2 Dynamic Stacks 18.3 The STL stack Container 18.4 Introduction to the Queue ADT 18.5 Dynamic Queues 18.6 The STL deque and queue Containers 18.7 Focus on Problem Solving and Program Design: Eliminating Recursion 18.8 Tying It All Together. Converting Postfix Expressions to Infix 8.1Introduction to the Stack ADT CONCEPT: A stack is a data structure that stores and retrieves items in a last-in-first-out manner. Definition Like an array or a linked list, a stack is a data structure that holds a sequence of elements. Unlike arrays and lists, however, stacks are last-in-first-out (LIFO) structures. This means that when a program retrieves elements from a stack, the last element inserted into the stack is the first one retrieved (and likewise, the first element inserted is the last one retrieved). 1071
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
