Write documentation, a prototype, and a definition for a member function bottom(
ID: 3621278 • Letter: W
Question
Write documentation, a prototype, and a definition for a member function bottom() for the Stack class that returns the bottom element of the stack. The program is below:
----------------------------------------------------------------------------------------------------------
Lstack.h
#include
using namespace std;
#ifndef LSTACK
#define LSTACK
typedef int StackElement;
class Stack
{
public:
Stack();
Stack(const Stack & original);
/***** Destructor *****/
~Stack();
/*------------------------------------------------------------------------
Class destructor
Precondition: None
Postcondition: The linked list in the stack has been deallocated.
------------------------------------------------------------------------*/
/***** Assignment *****/
const Stack & operator= (const Stack & rightHandSide);
bool empty() const;
void push(const StackElement & value);
void display(ostream & out) const;
StackElement top() const;
void pop();
private:
/*** Node class ***/
class Node
{
public:
StackElement data;
Node * next;
//--- Node constructor
Node(StackElement value, Node * link = 0)
: data(value), next(link)
{}
};
typedef Node * NodePointer;
/***** Data Members *****/
NodePointer myTop; // pointer to top of stack
}; // end of class declaration
#endif
------------------------------------------------------------------------------------------------------
Lstach.cpp
#include
using namespace std;
#include "LStack.h"
//--- Definition of Stack constructor
Stack::Stack()
: myTop(0)
{}
//--- Definition of Stack copy constructor
Stack::Stack(const Stack & original)
{
myTop = 0;
if (!original.empty())
{
// Copy first node
myTop = new Stack::Node(original.top());
// Set pointers to run through the stacksÕ linked lists
Stack::NodePointer lastPtr = myTop,
origPtr = original.myTop->next;
while (origPtr != 0)
{
lastPtr->next = new Stack::Node(origPtr->data);
lastPtr = lastPtr->next;
origPtr = origPtr->next;
}
}
}
//--- Definition of Stack destructor
Stack::~Stack()
{
// Set pointers to run through the stack
Stack::NodePointer currPtr = myTop, // node to be deallocated
nextPtr; // its successor
while (currPtr != 0)
{
nextPtr = currPtr->next;
delete currPtr;
currPtr = nextPtr;
}
}
//--- Definition of assignment operator
const Stack & Stack::operator=(const Stack & rightHandSide)
{
if (this != &rightHandSide) // check that not st = st
{
this->~Stack(); // destroy current linked list
if (rightHandSide.empty()) // empty stack
myTop = 0;
else
{ // copy rightHandSide's list
// Copy first node
myTop = new Stack::Node(rightHandSide.top());
// Set pointers to run through the stacks' linked lists
Stack::NodePointer lastPtr = myTop,
rhsPtr = rightHandSide.myTop->next;
while (rhsPtr != 0)
{
lastPtr->next = new Stack::Node(rhsPtr->data);
lastPtr = lastPtr->next;
rhsPtr = rhsPtr->next;
}
}
}
return *this;
}
//--- Definition of empty()
bool Stack::empty() const
{
return (myTop == 0);
}
//--- Definition of push()
void Stack::push(const StackElement & value)
{
myTop = new Stack::Node(value, myTop);
}
//--- Definition of display()
void Stack::display(ostream & out) const
{
Stack::NodePointer ptr;
for (ptr = myTop; ptr != 0; ptr = ptr->next)
out << ptr->data << endl;
}
//--- Definition of top()
StackElement Stack::top() const
{
if (!empty())
return (myTop->data);
else
{
cerr << "*** Stack is empty "
" -- returning garbage *** ";
StackElement * temp = new(StackElement);
StackElement garbage = *temp; // "Garbage" value
delete temp;
return garbage;
}
}
//--- Definition of pop()
void Stack::pop()
{
if (!empty())
{
Stack::NodePointer ptr = myTop;
myTop = myTop->next;
delete ptr;
}
else
cerr << "*** Stack is empty -- can't remove a value *** ";
}
-------------------------------------------------------------------------------------------------------
driver.cpp
#include
using namespace std;
#include "LStack.h"
void print(Stack st)
{ st.display(cout); }
int main()
{
Stack s;
cout << "Stack created. Empty? " << boolalpha << s.empty() << endl;
cout << "How many elements to add to the stack? ";
int numItems;
cin >> numItems;
for (int i = 1; i <= numItems; i++)
s.push(100*i);
cout << "Stack empty? " << s.empty() << endl;
cout << "Contents of stack s (via print): ";
print(s); cout << endl;
cout << "Check that the stack wasn't modified by print: ";
s.display(cout); cout << endl;
Stack t, u;
t = u = s;
cout << "Contents of stacks t and u after t = u = s (via print): ";
cout << "u: "; print(u); cout << endl;
cout << "t: "; print(t); cout << endl;
cout << "Top value in t: " << t.top() << endl;
while (!t.empty())
{
cout << "Popping t: " << t.top() << endl;
t.pop();
}
cout << "Stack t empty? " << t.empty() << endl;
cout << "Top value in t: " << t.top() << endl;
cout << "Trying to pop t: " << endl;
t.pop();
}
-----------------------------------------------------------------------------------------------------
Explanation / Answer
The stack is represented in systems by a block of memory cells with bottom at a fixed location and the stack pointer holding the address of the curretn top cell in that stack. StackElement Stack:: bottom() const; { if(myTop!=-1) { return aray[0]; } coutRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.