Question

Purpose This assignment is an exercise in implementing the Stack ADT using a dynamically-allocated array, as...

Purpose

This assignment is an exercise in implementing the Stack ADT using a dynamically-allocated array, as well as techniques for managing dynamically-allocated storage in C++.

Assignment

In this assignment, you will write a class called Stack that will encapsulate a dynamically-allocated array of elements of a generic data type.

A driver program is provided for this assignment to test your implementation. You don't have to write the tests.

Program

You will need to write a single template class for this assignment, the Stack class. You will need to implement several methods and functions associated with this class.

Since this is a C++ template, all of your code should be placed in a single header (.h) file. This includes the implementations of all methods and any other associated functions. There will not be a Stack.cpp file for this assignment.

class Stack

Data members

This class contains a pointer to an item of the template parameter type that will point to the first element of a dynamically allocated array (the stack array). Because the array is allocated dynamically, a data member is also maintained inside the class to determine the maximum number of elements that may be stored in the array (the stack capacity). Another data member is used to keep track of the number of data items currently stored in the vector (the stack size). Both of these data members should be declared as data type size_t (which corresponds to an unsigned integer).

You may also choose to have a data member to keep track of the subscript of the top item in the stack (the stack top subscript). Since the subscript of the top item is always equal to the stack size - 1, doing so is optional, but if you make this a separate data member it should be an integer.

In addition to the data members described above, your class declaration will need prototypes for the methods described below.

Methods and associated functions

As usual, methods that do not alter the data members of the object that called the method should be declared to be const.

  • Constructor

    The class should have a default constructor that takes no arguments. The constructor should set the stack size and stack capacity to 0 and the stack array pointer to nullptr. Alternately, the data members can be initialized in the class declaration, in which case this method's body can be empty.

  • Destructor

    The class should have a destructor that deletes the dynamic memory for the stack array. The destructor should NOT call the clear() method.

  • Copy Constructor

    The class should also have a proper copy constructor. Your code should account for the possibility that you might be copying an empty Stack object.

  • Copy Assignment Operator

    The assignment operator should be properly overloaded to allow one Stack object to be assigned to another. Your code should account for the possibility that you might be copying an empty Stack object.

  • operator<<

    The output operator should be overloaded so that a Stack can be printed on the standard output. This will need to be a standalone friend function rather than a method.

    The items stored in the stack should be printed starting with the first array element (subscript 0) and ending with the last valid element (subscript stack size - 1). Print a space after each stack element.

    Declaring a template function to be a friend of a template class requires some special syntax - see the Implementation Hints below.

  • clear()

    This method should set the stack size to 0. It should not change the stack capacity or the stack array.

  • size()

    This method should return the stack size.

  • capacity()

    This method should return the stack capacity.

  • empty()

    This method should return true if the stack size is equal to 0; otherwise it should return false.

  • top()

    This method should return the top element of the stack array (the one at the subscript stack size - 1). You may assume this method will not be called if the stack is empty.

  • push()

    This method takes a reference to a constant item of the template parameter type as its argument, the value to insert into the stack. If the stack is full (the stack size is equal to the stack capacity), this method will need to call the reserve() method to increase the capacity of the stack array and make room for the value to insert. If the stack capacity is currently 0, pass a new capacity of 1 to the reserve() method. Otherwise, pass a new capacity of twice the current stack capacity to the reserve() method.

    Using the stack size as the subscript, copy the value to be inserted into the stack array. The stack size should then be incremented by 1.

  • pop()

    This method should decrement the stack size by 1, which effectively removes the top (last) value from the stack array. No changes need be made to the stack array contents. You may assume this method will not be called if the stack is empty.

  • reserve()

    This method increases the capacity of the stack array. It takes a single integer argument, the new capacity. The logic for this method should look something like this:

    1. If the new capacity is less than the stack size or equal to the current stack capacity, simply return.
    2. Set the stack capacity to the new capacity.
    3. Declare a temporary array pointer (a pointer to the template parameter type).
    4. If the stack capacity is 0, set the temporary array pointer to nullptr. Otherwise, use the temporary array pointer to allocate an array of items of the template parameter type. The number of elements in the new temporary array should be equal to the updated stack capacity.
    5. Copy the contents of the stack array into the temporary array.
    6. Delete the stack array.
    7. Set the stack array pointer equal to the temporary array pointer.

Driver Program

A driver program, assign6.cpp is provided for this assignment. The purpose of a driver program is to test other pieces that you code. You do not need to write the driver program yourself.


#include <iostream>
#include "Stack.h"

using std::cout;
using std::endl;

int main()
   {
   cout << "Testing default constructor\n\n";
    
   Stack<int> s1;
    
   cout << "s1: " << s1 << endl;
   cout << "s1 size: " << s1.size() << ", capacity: " << s1.capacity() << endl;
   cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
   cout << endl;
    
   cout << "Testing push()\n\n";
    
   for (int i = 10; i < 80; i+= 10)
      s1.push(i);
    
   cout << "s1: " << s1 << endl;
   cout << "s1 size: " << s1.size() << ", capacity: " << s1.capacity() << endl;
   cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
   cout << endl;
    
   for (int i = 15; i < 85; i+= 10)
      s1.push(i);
    
   cout << "s1: " << s1 << endl;
   cout << "s1 size: " << s1.size() << ", capacity: " << s1.capacity() << endl;
   cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
   cout << endl;
    
   cout << "Testing copy constructor()\n\n";
    
    Stack<int> s2 = s1;
    
   cout << "s1: " << s1 << endl;
   cout << "s1 size: " << s1.size() << ", capacity: " << s1.capacity() << endl;
   cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
   cout << endl;
    
   cout << "Testing top()\n\n";
    
   cout << "Top item of s1: " << s1.top() << endl << endl;
    
   cout << "Testing pop()\n\nTop item of s1: ";
    
   while (!s1.empty())
      {
      cout << s1.top() << ' ';
      s1.pop();
      }
    
   cout << endl << endl;
   cout << "s1: " << s1 << endl;
   cout << "s1 size: " << s1.size() << ", capacity: " << s1.capacity() << endl;
   cout << "s1 is " << ((s1.empty()) ? "empty\n" : "not empty\n");
   cout << endl;
    
   cout << "Testing assignment operator\n\n";
    
   Stack<int> s3;
    
   s3 = s2;
    
   cout << "s2 (size " << s2.size() << "): " << s2 << endl;
   cout << "s3 (size " << s3.size() << "): " << s3 << endl << endl;
    
   cout << "Testing clear()\n\n";
    
   s2.clear();
    
   cout << "s2: " << s2 << endl;
   cout << "s2 size: " << s2.size() << ", capacity: " << s2.capacity() << endl;
   cout << "s2 is " << ((s2.empty()) ? "empty\n" : "not empty\n");
   cout << endl;
    
   cout << "s3: " << s3 << endl;
   cout << "s3 size: " << s3.size() << ", capacity: " << s3.capacity() << endl;
   cout << "s3 is " << ((s3.empty()) ? "empty\n" : "not empty\n");
   cout << endl;
    
   cout << "Testing assignment to self and swap\n\n";
    
   s3 = s3;
   s2 = s3;
   s3.clear();
    
   cout << "s2 (size " << s2.size() << "): " << s2 << endl;
   cout << "s3 (size " << s3.size() << "): " << s3 << endl << endl;
    
   cout << "Testing chained assignment\n\n";
    
   Stack<int> s4;
    
   s4 = s3 = s2;
    
   cout << "s2 (size " << s2.size() << "): " << s2 << endl;
   cout << "s3 (size " << s3.size() << "): " << s3 << endl;
   cout << "s4 (size " << s4.size() << "): " << s4 << endl << endl;
    
   Stack<int> s5 = s4;
    
   cout << "s5 (size " << s5.size() << "): " << s5 << endl << endl;


   cout << "Testing other data type\n\n";

   Stack<char> s6;

   for (char c = 'a'; c < 'k'; c++)
      s6.push(c);

   cout << "s6 (size " << s6.size() << "): " << s6 << endl << endl;

   cout << "Testing const correctness\n\n";
    
   const Stack<char>& r6 = s6;
    
   cout << "s6: " << r6 << endl;
   cout << "s6 size: " << r6.size() << ", capacity: " << r6.capacity() << endl;
   cout << "s6 is " << ((r6.empty()) ? "empty\n" : "not empty\n");
   cout << "Top item of s6: " << r6.top() << endl;

   Stack<char> s7 = r6;
    
   cout << "s7: " << s7 << endl;
        
   s7.clear();
        
   cout << "s7: " << s7 << endl;
        
   s7 = r6;
        
   cout << "s7: " << s7 << endl;

   return 0;
   }

Output

A successful run of the entire program should produce the output shown below.

Testing default constructor
    
s1:
s1 size: 0, capacity: 0
s1 is empty
    
Testing push()
    
s1: 10 20 30 40 50 60 70
s1 size: 7, capacity: 8
s1 is not empty
    
s1: 10 20 30 40 50 60 70 15 25 35 45 55 65 75
s1 size: 14, capacity: 16
s1 is not empty
    
Testing copy constructor()
    
s1: 10 20 30 40 50 60 70 15 25 35 45 55 65 75
s1 size: 14, capacity: 16
s1 is not empty
    
Testing top()
    
Top item of s1: 75
    
Testing pop()
    
Top item of s1: 75 65 55 45 35 25 15 70 60 50 40 30 20 10
    
s1:
s1 size: 0, capacity: 16
s1 is empty
    
Testing assignment operator

s2 (size 14): 10 20 30 40 50 60 70 15 25 35 45 55 65 75
s3 (size 14): 10 20 30 40 50 60 70 15 25 35 45 55 65 75

Testing clear()

s2:
s2 size: 0, capacity: 16
s2 is empty

s3: 10 20 30 40 50 60 70 15 25 35 45 55 65 75
s3 size: 14, capacity: 16
s3 is not empty

Testing assignment to self and swap

s2 (size 14): 10 20 30 40 50 60 70 15 25 35 45 55 65 75
s3 (size 0):

Testing chained assignment

s2 (size 14): 10 20 30 40 50 60 70 15 25 35 45 55 65 75
s3 (size 14): 10 20 30 40 50 60 70 15 25 35 45 55 65 75
s4 (size 14): 10 20 30 40 50 60 70 15 25 35 45 55 65 75

s5 (size 14): 10 20 30 40 50 60 70 15 25 35 45 55 65 75

Testing other data type

s6 (size 10): a b c d e f g h i j

Testing const correctness
    
s6: a b c d e f g h i j
s6 size: 10, capacity: 16
s6 is not empty
Top item of s6: j
s7: a b c d e f g h i j
s7:
s7: a b c d e f g h i j
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE FOR CLASS STACK

#include <iostream>
#include <conio>
#include <stdio>

using namespace std;

template <typename T>
class Stack;

template <typename T>
std::ostream& operator<<(std::ostream &out, const Stack<T> &s);

class Stack{
  
private:
  
T* data;
size_t capacity;
size_t size;

public:
  
Stack(){
*data=NULL;
capacity=0;
size=0;
}
  
~Stack(){
delete [] data;
size = 0;
capacity = 0;
}
  
Stack(const Stack &obj) {
if(obj.data==NULL){
*data=NULL;
size=0;
capacity=0;
}
data = new int;
*data = *obj.data;
size=obj.size;
capacity=obj.capacity;
}
  
void operator = (const Stack &obj ) {
if(obj.data==NULL){
*data=NULL;
size=0;
capacity=0;
}
data = new int;
*data = *obj.data;
size=obj.size;
capacity=obj.capacity;
}
  
friend std::ostream& operator<< <T>(std::ostream &out, const Stack<T> &s);
  
void clear(){
size=0;
}
  
int size(){
return size;
}
  
int capacity(){
return capacity;
}
  
boolean empty(){
if(size==0){
return true;
}
else{
return false;
}
}
  
T top(){
return *data[size-1];
}
  
void push(T val){
  
if(size<capacity){
*data[size]=val;
size=size+1;
}
  
else{
if(capacity==0){
reserve(1);
}
else{
reserve(2*capacity);
}
*data[size]=val;
size=size+1;
}
  
}
  
void pop(){
size=size-1;
}
  
void reserve(int newCapacity){
  
if(newCapacity<size || newCapacity<capacity){
return;
}
  
T* temp;
temp=new T[newCapacity];
capacity=newCapacity;
if(newCapacity>1){
  
for(int i=0;i<size; i++){
*temp[i]=*data[i];
}
  
delete [] data;
data=temp;
  
}
  
}
  
};

//friend function for << operator
template <typename T>
std::ostream& operator<<(std::ostream &out, const Stack<T> &s) {

if(size==0){
cout<<"The stack is empty";
}
  
for(int i=0; i<size; i++){
cout<<*data[i]<<" ";
}
  
}

Add a comment
Know the answer?
Add Answer to:
Purpose This assignment is an exercise in implementing the Stack ADT using a dynamically-allocated array, as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is...

    My Output s1 (size 0): s1 is empty Testing push() s1 (size 1): 17 s1 is not empty s1 (size 4): 4 6 2 17 s1 is not empty Testing copy constructor s1 (size 4): 4 6 2 17 s2 (size 4): 4 6 2 17 Testing clear() s1 (size 0): s2 (size 4): 0 1477251200 1477251168 1477251136 s3 (size 4): 28 75 41 36 Testing assignment operator s3 (size 4): 28 75 41 36 s4 (size 4): 28 75...

  • Create an array-based implementation of a stack. Each element of the stack should store a string....

    Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a string),...

  • C++ Create an array-based implementation of a stack. Each element of the stack should store a...

    C++ Create an array-based implementation of a stack. Each element of the stack should store a string. The stack class should include 3 private member variables (maximum stack size, top of the stack index, and a pointer to the array that holds the stack elements). Public member methods should include a constructor (with an argument of stack maximum size that is used to create a dynamic array), a destructor (that deletes the dynamic array), a push method (argument is a...

  • - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h"...

    - implement the Stack ADT using array – based approach. Use C++ program language #include "StackArray.h" template <typename DataType> StackArray<DataType>::StackArray(int maxNumber) { } template <typename DataType> StackArray<DataType>::StackArray(const StackArray& other) { } template <typename DataType> StackArray<DataType>& StackArray<DataType>::operator=(const StackArray& other) { } template <typename DataType> StackArray<DataType>::~StackArray() { } template <typename DataType> void StackArray<DataType>::push(const DataType& newDataItem) throw (logic_error) { } template <typename DataType> DataType StackArray<DataType>::pop() throw (logic_error) { } template <typename DataType> void StackArray<DataType>::clear() { } template <typename DataType> bool StackArray<DataType>::isEmpty() const {...

  • Lab 3 – Array-Based Stack and Queue Overview In this assignment, you will be implementing your...

    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...

  • in c++ please include all of the following " template class, template function, singly linked list,...

    in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...

  • I need to implement a stack array but the top of the stack has to be...

    I need to implement a stack array but the top of the stack has to be Initialize as the index of the last location in the array.    //Array implementation of stacks.    import java.util.Arrays;       public class ArrayStack implements Stack {        //Declare a class constant called DEFAULT_STACK_SIZE with the value 10.           private static final int DEFAULT_STACK_SIZE = 10;           /* Declare two instance variables:            1. An integer called...

  • QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top...

    QUESTION: ADT stack: resizable array-based implementation    for Ch4 programming problem 4 "maintain the stacks's top entry at the end of the array" at array index N-1 where the array is currently allocated to hold up to N entries. MAKE SURE YOU IMPLEMENT the functions:  bool isEmpty() const; bool push(const ItemType& newEntry); bool pop(); in ArrayStackP4.cpp //FILE StackInterface.h #ifndef STACK_INTERFACE_ #define STACK_INTERFACE_ template<class ItemType> class StackInterface { public:    /** Sees whether this stack is empty.    @return True if the...

  • Write a C++ console program that defines a class named Course that utilizes a dynamically allocated...

    Write a C++ console program that defines a class named Course that utilizes a dynamically allocated array. Do not use the vector class for this assignment. The Course class should define private data members for the name of the course, the number of students in the course, an array of student names (string*), and the capacity of the course (the array may not be full of students). Use pointer notation when dealing with the array. A 2-arg constructor should initialize...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT