Question: The MiniStack class is an array-based implementation of the Stack Abstract Data Type (ADT) where the array size is limited to five elements of type
The MiniStack class is an array-based implementation of the Stack Abstract Data Type (ADT)
where the array size is limited to five elements of type char. MiniStack objects are used by BigStack to
create a doubly-linked-node, stack-of-MiniStacks implementation of the Stack ADT.
Needed Files : MiniStack.cpp and BigStack.cpp
=====================bigstack.h===============================
#include
#ifndef BIGSTACK_H #define BIGSTACK_H
#include "ministack.h"
struct ListNode // Description of a ListNode struct { MiniStack* stackPtr; // Pointer to a MiniStack object ListNode* nextPtr; // Pointer to next ListNode ListNode* previousPtr; // Pointer to previous ListNode };
// Client code responsible for all error handling class BigStack // Represents a double-linked node implementation of the Stack ADT { private: ListNode* headPtr; // Pointer to head of double-linked list of nodes representing bigstack
public: BigStack(); // Default constructor initializes empty bigstack object (no ListNodes) void Push(char ch); // Adds element to top of bigstack assuming stack not full (creates ListNodes only as needed) void Pop(); // Removes element from top of stack assuming bigstack not empty (deallocating only as needed) char Top(); // Returns copy of top value assuming bigstack not empty void MakeEmpty(); // Empties bigstack without leaking memory bool IsFull() const; // Returns true if bigstack full; false otherwise bool IsEmpty() const; // Returns true if bigstack empty; false otherwise int Size() const; // Returns number of data values stored in BigStack object ~BigStack(); // Destructor deallocates all dynamic memory associated with bigstack object including // all list nodes
void Print() const // Prints stack contents, top to bottom and bottom to top { ListNode* tempPtr = headPtr;
// Forward print cout << "Top [ "; while (tempPtr != NULL) { tempPtr->stackPtr->FwdPrint(); if (tempPtr->nextPtr == NULL) break; tempPtr = tempPtr->nextPtr; } cout << " ] Bottom [ "; // Reverse print while (tempPtr != NULL) { tempPtr->stackPtr->RevPrint(); tempPtr = tempPtr->previousPtr; } cout << "] Top" << endl;
} // End BigStack::Print() };
#endif
==================ministack.h===================================
#include
#ifndef MINISTACK_H
#define MINISTACK_H
class MiniStackFull { /* No Code - Empty Exeception Class */ }; class MiniStackEmpty { /* No Code - Empty Exeception Class */ };
const int MINI_STACK_SIZE = 5; // Fixed number of elements in stack
// Container code responsible for throwing exceptions to signal error conditions class MiniStack // Represents an array implementation of the Stack ADT { private: int top; // Index of topmost value stored in MiniStack char* stackPtr; // Pointer to dynamically allocated ministack array of MINI_STACK_SIZE chars
public: MiniStack(); // Default constructor creates an empty MiniStack object
void Push(char ch); // Adds element to top of ministack if stack is not full; // throws MiniStackFull if stack is full
void Pop(); // Removes element from top of ministack if stack is not empty // throws MiniStackEmpty if stack is empty
void MakeEmpty(); // Empties ministack
char Top() const; // Returns copy of value stored at top of ministack if stack is not empty; // throws MiniStackEmpty if stack is empty
bool IsFull() const; // Returns true if ministack is full; false otherwise
bool IsEmpty() const; // Returns true if ministack empty; false otherwise
int Size() const; // Returns number of data values currently stored in MiniStack object
~MiniStack(); // Destructor deallocates ministack array
void FwdPrint() const // Prints ministack contents, top to bottom { cout << "( "; for(int k = top; k >= 0; k--) cout << stackPtr[k] << ' '; cout << ")"; } // End MiniStack::Print() void RevPrint() const // Prints ministack contents, top to bottom { cout << "( "; for(int k = 0; k <= top; k++) cout << stackPtr[k] << ' '; cout << ")"; } // End MiniStack::Print() };
#endif
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
