Question
In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue . A stack is a linear data structure which follows
In this assignment, you will be implementing your own Array-Based Stack (ABS) and Array-Based Queue. 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 deskthe 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).
Stack Behavior
Push Add something to the top of the stack.
Pop Remove something from the top of the stack and return it,
Example of LIFO operations-the data most recently added is the first to be removed
Queue Behavior
Enqueue Add something to end of the queue.
Dequeue Remove something from the front of the queue.
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 conventionreuse code whenever you can!) As with previous classes that use dynamic memory, you must be sure to define The Big Three: The Copy Constructor, the Assignment Operator, and the 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.
By default, your ABS and ABQ will have a scale factor 2.0f.
- Attempting to push() or enqueue() an item onto an ABS/ABQ that is full will resize the current capacity to current_capacity*scale_factor.
- When calling pop() or dequeue(), if the percent full (e.g. current size / max capacity) becomes strictly lessthan 1/scale_factor, resized the storage array to current_capacity/scale_factor.
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, its 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 performanceespecially 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.
An example of the resizing scheme to be implement on a stack.
Stack Functions
HERE IS MAIN , I need ABS.h and ABQ.h and analysis of the results if possible
#include "ABS.h" #include "ABQ.h" #include
void ABSTest(); void ABQTest();
int main(){ int choice; cin >> choice;
if(choice==1) ABSTest(); else if(choice==2) ABQTest();
return 0; }
void ABSTest(){ cout << "Making integer ABS... "; ABS
cout << " Popping items... "; for(int i = 1; i < 10; i++){ cout << " Popped " << intABS.pop() << endl; cout << "New Size: " << intABS.getSize() << endl; cout << "New Max Capacity: " << intABS.getMaxCapacity() << endl; }
cout << " Making float ABS... "; ABS
cout << " Popping items... "; for(float i = 1; i < 5; i+=0.5f){ cout << " Popped " << floatABS.pop() << endl; cout << "New Size: " << floatABS.getSize() << endl; cout << "New Max Capacity: " << floatABS.getMaxCapacity() << endl; } }
void ABQTest(){ cout << "Making integer ABQ... "; ABQ
cout << " Dequeueing items... "; for(int i = 1; i < 10; i++){ cout << " Dequeued " << intABQ.dequeue() << endl; cout << "New Size: " << intABQ.getSize() << endl; cout << "New Max Capacity: " << intABQ.getMaxCapacity() << endl; }
cout << " Making float ABQ... "; ABQ
cout << " Dequeueing items... "; for(float i = 1; i < 5; i+=0.5f){ cout << " Dequeued " << floatABQ.dequeue() << endl; cout << "New Size: " << floatABQ.getSize() << endl; cout << "New Max Capacity: " << floatABQ.getMaxCapacity() << endl; } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started