Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this project, you will complete the provided partial C++ program that utilizes classes to implement a stack. The MiniStack class is an array-based implementation

For this project, you will complete the provided partial C++ program that utilizes classes to implement a

stack. 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. So with this project,

you practice implementing both an array-based stack (MiniStack) and a linked-node stack (BigStack).

* All I need is int BigStack::Size() const { }

==================== BigStack.h ====================

#include using namespace std;

#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

==================== BigStack.cpp ====================

#include "bigstack.h" #include using namespace std;

BigStack::BigStack() // Default constructor initializes empty bigstack object (no ListNodes) { headPtr = NULL; }

void BigStack::Push(char ch) // Adds element to top of bigstack assuming stack not full (creates ListNodes only as needed) { if(!IsFull()) { if(headPtr == NULL) { headPtr = new ListNode; headPtr->stackPtr = new MiniStack; headPtr->nextPtr = NULL; headPtr->previousPtr = NULL; } if(!headPtr->stackPtr->IsFull()) { headPtr->stackPtr->Push(ch); } else { ListNode* tempPtr; tempPtr = new ListNode; tempPtr->stackPtr = new MiniStack; tempPtr->stackPtr->Push(ch); headPtr->previousPtr = tempPtr; tempPtr->nextPtr = headPtr; headPtr = tempPtr; } } }

void BigStack::Pop() // Removes element from top of stack assuming bigstack not empty (deallocating only as needed) { if(!IsEmpty()) { headPtr->stackPtr->Pop(); } else { delete headPtr->stackPtr; headPtr = headPtr->nextPtr; delete headPtr->previousPtr; headPtr->previousPtr = NULL; headPtr->stackPtr->Pop(); } }

char BigStack::Top() // Returns copy of top value assuming bigstack not empty { if(!IsEmpty()) { return headPtr->stackPtr->Top(); } }

void BigStack::MakeEmpty() // Empties bigstack without leaking memory { if(!IsEmpty()) { ListNode* tempPtr; while(headPtr != NULL) { tempPtr = headPtr; headPtr = headPtr->nextPtr; delete tempPtr->stackPtr; delete tempPtr; } headPtr = NULL; } }

bool BigStack::IsFull() const // Returns true if bigstack full; false otherwise { try { ListNode* testPtr = new ListNode; delete testPtr; } catch(bad_alloc) { return true; } return false; }

bool BigStack::IsEmpty() const // Returns true if bigstack empty; false otherwise { if(headPtr != NULL) { return false; } else { return true; } }

int BigStack::Size() const // Returns number of data values stored in BigStack object { ?????

}

BigStack::~BigStack() // Destructor deallocates all dynamic memory associated with bigstack object including all list nodes { ListNode* tempPtr; while(headPtr != NULL) { tempPtr = headPtr; delete headPtr->stackPtr; headPtr = headPtr->nextPtr; delete tempPtr; } }

Everything else works, I just can't seem to get the Size function to work.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions

Question

What is SAP ERP and its modules?

Answered: 1 week ago