Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can you help me write out the code for this? I can\'t figure out what to write..

ID: 3546571 • Letter: C

Question

Can you help me write out the code for this? I can't figure out what to write..

#include <cstdlib>
#include <iostream>
#include "inlab12b.h"
using namespace std;

//The constructor for the class sets the top pointer to NULL.
Stack::Stack()
{top = NULL;}   

//The destructor pop's elements from the stack until it is empty.
Stack::~Stack()
{
    DataType item;
    while (!empty())
        item = pop();
}
        
// This function pushes a data item onto the stack.
void Stack::push(DataType item)
{
    NodePtr newNode = new StackNode;

************************************************************************************
     // Code goes here. The data field of
    //the new node should be loaded with the incoming item and
    // the new node should be placed at the top of the stack

************************************************************************************
}

// This function pops a data item off of  the stack.  If the stack is empty
// the program generates an error condition and the program is exited
DataType Stack::pop()
{
    DataType item;        //data to be returned
    NodePtr topNode;      //Pointer to the top node to be deleted

    //If the stack is empty, exit the program.
    if( empty())
    {     
        cerr << "Attempt to pop an empty stack! " << endl;
        exit(1);
    }
    
    //the stack wasn't empty so delete the node
    topNode = top;
    top = top->next;    
    item = topNode->data;
    delete(topNode);
    
    return item;
}

// This function returns TRUE if the stack is empty, FALSE otherwise.
bool Stack::empty()
{
    return top == NULL;
}

Explanation / Answer

#include <cstdlib>

#include <iostream>

#include "inlab12b.h"

using namespace std;


//The constructor for the class sets the top pointer to NULL.

Stack::Stack()

{top = NULL;}


//The destructor pop's elements from the stack until it is empty.

Stack::~Stack()

{

DataType item;

while (!empty())

item = pop();

}


// This function pushes a data item onto the stack.

void Stack::push(DataType item)

{

NodePtr newNode = new StackNode;


newNode->data = item; // Storing data of the item to be pushed.

newNode->next = top;


top = newNode; // Intialising the newly pushed item to the the top of the stack.


}


// This function pops a data item off of the stack. If the stack is empty

// the program generates an error condition and the program is exited

DataType Stack::pop()

{

DataType item; //data to be returned

NodePtr topNode; //Pointer to the top node to be deleted


//If the stack is empty, exit the program.

if( empty())

{

cerr << "Attempt to pop an empty stack! " << endl;

exit(1);

}


//the stack wasn't empty so delete the node

topNode = top;

top = top->next;

item = topNode->data;

delete(topNode);


return item;

}


// This function returns TRUE if the stack is empty, FALSE otherwise.

bool Stack::empty()

{

return top == NULL;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote