Question: need help with assignment. c++. need simple code within scope of class, please follow all instructions and use template Don't worry about leaker.h or copying.
need help with assignment. c++. need simple code within scope of class, please follow all instructions and use template

Don't worry about leaker.h or copying. need abq.h and abs.h





21.5 Lab 3 - Stacks and Queues Part 1 In this assignment you are going to create a Stack and a Queue--two data structures which are similar in many ways in terms of how you write them, though their overall goals are different. The details for this assignment are in the file downloaded from Canvas. In this first part, you will only be implementing the Stack data structure. Both stacks and queues can be implemented in a variety of ways, but in this case you will be implementing a stack using a dynamically allocated array. You may NOT use std::vector for this assignment. Any submissions using std::vector will receive a grade of 0. The data must be stored in a dynamically allocated array. a a Memory Leaks When dealing with memory, you will inevitably encounter memory leaks in your program. Simply stated, a memory leak is memory that was allocated with new, and never deleted. Detecting memory leaks can be quite a challenge, but for this assignment there are two files which can help with this: leaker.h leaker.cpp To use these files, initially you don't have to do anything at all. Simply being present as part of your files will let you use their functionality. These files track your memory allocations/deallocations-that is, they track every time your code calls new, and every time your code calls delete. At the end of the program, if the number of new/delete calls don't match up, errors will reported. They will look something like this: LEAKER: errors found! Leaks found: 1 allocations (4 bytes). unknown: unknown (): 0 memory leak: memory was not deallocated. Lab 3 - Array-Based Stack and Queue Overview In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue (ABQ). A stack is a linear data structure which follows the Last-In, First-Out (LIFO) property. LIFO means that the data most recently added is the first data to be removed. (Imagine a stack of books, or a stack of papers on a desk-the first one to be removed is the last one placed on top.) A queue is another linear data structure that follows the First-In, First-Out (FIFO) property. FIFO means that the data added first is the first to be removed (like a line in a grocery store-the first person in line is the first to checkout). Both of these are data structures-some sort of class to store information. What makes them different is how they store and access the information. Adding the same values (say, the numbers 1-10) to either data structure would result in different ordering/output of the information. Why you might choose one or the other depends on the specific needs of a program. New keywords/language concepts . Templates - write code to work with multiple data types The Big Three - Copy Constructor, Copy Assignment Operator, and Destructor - necessary when working with dynamic memory Exceptions - Used to indicate something went wrong in an application. Unhandled exceptions will cause a program to terminate Deep copy - Creating a copy of dynamically allocated memory requires new memory to be allocated before copying values Stack Behavior I Stacks have two basic operations (with many other functions to accomplish these tasks): Push - Add something to the top of the stack. Pop - Remove something from the top of the stack and return it. 6 5 Push Push Push 21 Push 3 Push 2 3 2 1 4 3 2 1 1 1 6 6 Pop 5 4 3 Pop 4 3 Pop 3 -NWA Pop 2 NW Pop Example of LIFO operations-the data most recently added is the first to be removed Queue Behavior Like a stack, a queue has two basic operations: Enqueue - Add something to end of the queue. If this were a line, a new person getting into the line would start at the back. Dequeue - Remove something from the front of the queue. If this were a line, the person at the start of the line is next-for whatever the people are lining up for. 1 Back Front Dequeue Enqueue Example of FIFO operations-the newest data is last to be removed Description Your ABS and ABQ will be template classes, and thus will be able to hold any data type. (Many data structures follow this convention-reuse code whenever you can!) Because these classes will be using dynamic memory, you must be sure to define The Big Three: Copy Constructor Copy Assignment Operator Destructor Data will be stored using a dynamically allocated array (hence the array-based stack and queue). You may use any other variables/function in your class to make implementation easier. The nature of containers like these is that they are always changing size. You have 3 elements in a stack, and push() another... now you need space for 4 elements. Use push() to add another, now you need space for 5, etc... If your container is full, you can increase the size by an amount other than one, if you want. Why increase (or decrease) the size by any amount other than one? Short answer: performance! If you are increasing or decreasing the size of a container, it's reasonable to assume that you will want to increase or decrease the size again at some point, requiring another round of allocate, copy, delete, etc. Increasing the capacity by more than you might need (right now) or waiting to reduce the total capacity allows you to avoid costly dynamic allocations, which can improve performance-especially in situations in which this resizing happens frequently. This tradeoff to this approach is that it will use more memory, but this speed-versus-memory conflict is something that programmers have been dealing with for a long time. By default, your ABS and ABQ will have a scale factor 2.0f-store this as a class variable. 1. Attempting to push() or enqueue() an item onto an ABS/ABQ that is full will resize the current capacity to current_capacity* scale_factor. 2. When calling pop() or dequeue(), if the "percent full (e.g. current size / max capacity) becomes strictly less than 1/scale_factor, resize the storage array to current_capacity/scale_factor. Push() resizing . Pop () resizing The stack is full when push is called The stack is doubled in size The new item is added to the top of the stack The stack is exactly half full pop o is called and the top item is removed The stack is unde half full, and the stack is resized. An example of the resizing scheme to be implement on a stack. Resizing arrays What's easy to say isn't usually easy to do in programming. You can't "just" change the size of an array. You have to: 1. Create a new array based on the desired size 2. Copy elements from the old array to the new array (up to the size of the old array, or the capacity of the new ar arra, WHICHEVER IS SMALLER). 3. If you were adding something to the array, copy that as well 4. Delete the old array 5. Redirect the pointer to the old array to the new array Exceptions Some of your functions will throw exceptions. There are many types of exceptions that can be thrown, but in this case you will simply throw errors of type runtime_error. This is a general purpose error to indicate that something went wrong. The basic syntax for throwing an error is simply: throw type_of_exception ("Message describing the error."); If you wanted to throw a runtime_error exception that said "An error has occurred." you would write: throw runtime_error("An error has occurred."); There is also the concept of using try/catch blocks, but for this assignment you will only have to throw the exceptions. Checking for such exceptions will be handled by various unit tests on zyBooks. Stack Functions Your ABS must support the following methods: Method ABS() Description Default constructor. Maximum capacity should be set to 1, and current size set to 0; ABS (int capacity) Constructor for an ABS with the specified starting maximum capacity. ABS (const ABS &d) Copy Constructor ABS & operator= (const ABS &d) Assignment operator. ABS () Destructor void push (T data) Add a new item to the top of the stack and resize if necessary T pop () Remove the item at the top of the stack, resizes if necessary, and return the value removed. Throws a runtime_error if the stack is empty. T peek) Return the value of the item at the top of the stack, without removing it. Throws a runtime_error if the stack is empty. unsigned int getSize() Returns the current number of items in the ABS. unsigned int getMaxCapacity () Returns the current max capacity of the ABS. T* getData() Returns the array representing the stack. Queue Functions Your ABQ must support the following functions Method ABQ () Description Default constructor. Maximum capacity should be set to 1, and current size set to 0; ABQ (int capacity) Constructor for an ABQ with the specified starting maximum capacity. ABQ (const ABS &d) Copy Constructor ABQ & operator= (const ABQ &d) Assignment operator. ABQ O Destructor void enqueue (T data) Add a new item to end of the queue and resizes if necessary T dequeue () Remove the item at front of the queue, resizes if necessary, and return the value removed. Throws a runtime_error if the queue is empty. T peek () Return the value of the item at the front of the queue, without removing it. Throws a runtime_error if the queue is empty. unsigned int getSize() Returns the current number of items in the ABQ, unsigned int get MaxCapacity ( ) Returns the current max capacity of the ABQ. T* getData() Returns the array representing the queue
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
