简体   繁体   中英

How to implement the pop() function

I am writing a program that will implement all the functionality of a stack but I can't seem to be able to do just that. I'm in the last part pop() and well I'm stuck. I can't figure out how to move the m_top pointer down to the next box in the stack OR the nullptr if the stack is empty and return the value from the top box.

#include "StackOfBoxes.h"
#include "Box.h"
#include <iostream>

StackOfBoxes::StackOfBoxes()
{
    m_top=nullptr;
    m_size=0;
}

bool StackOfBoxes::isEmpty() const
{
    if (m_size==0)
    {
        return true;
    }
    return false;
}

int StackOfBoxes::size() const
{
    return m_size;
}

void StackOfBoxes::push(int value)
{
    Box* Box1 = new Box;
    Box1 -> m_previous = m_top;
    m_top=Box1;
    Box1 -> m_value = value;
    ++m_size;
}

int StackOfBoxes::pop()
{
    Box* temp = new Box;
    temp -> m_previous = m_top;
    int returnval = temp -> m_value;
    m_top = temp->m_previous;
    if (!isEmpty())
    {
        //move m_top down to next box.
    }
    else
    {
        //move nullptr down.
    }

    return returnval;
}

Header file

#ifndef STACKOFBOXES_H_INCLUDED
#define STACKOFBOXES_H_INCLUDED
#include "Box.h"

class StackOfBoxes
{
private:
    Box* m_top;
    int m_size;

public:
    StackOfBoxes();
    bool isEmpty() const;
    int size() const;
    void push(int value);
    int pop();
};





#endif // STACKOFBOXES_H_INCLUDED
#ifndef STACKOFBOXES_H_INCLUDED
#define STACKOFBOXES_H_INCLUDED
#include "Box.h"

class STACKOFBOXES_H_INCLUDED
{
private:
    Box* m_top;
    int m_size;

public:
    StackOfBoxes();
    bool isEmpty() const;
    int size() const;
    void push(int value);
    int pop();
};





#endif // STACKOFBOXES_H_INCLUDED

The program is tested through the main file.

#include <iostream> //std::cout std::cin
#include "StackOfBoxes.h" //StackOfBoxes

int main()
{
    StackOfBoxes myStack;   //Create an empty stack
    int sizeOfStack;    //int we'll use later to store the size of the stack

    //push some numbers onto the stack
    for(int i = 1; i <= 10; i++)
    {
        myStack.push( i * 5 );
    }


    //Store the size of the stack before popping anything
    sizeOfStack = myStack.size();

    std::cout << "There are " << sizeOfStack << " items on the stack" << std::endl;

    //Think about why we don't use i<myStack.size()
    for(int i = 0; i < sizeOfStack; i++)
    {
        std::cout << "Popping the top: " << myStack.pop() << std::endl;
    }

}

You won't need to allocate a

new Box;

While popping.

I expect this is more the sense of what you want....

if (!isEmpty())
{
    //mark existing top
    Box* temp = m_top;
    //move m_top down to next box.
    m_top = m_top->m_previous;
    delete temp;
    --msize;
}
int StackOfBoxes::pop()
{
    if(m_top==NULL) throw std::runtime_error("No such element");

    Box *tmp = m_top;
    int returnval = tmp->m_value;
    m_top = tmp->m_previous;

    delete tmp;
    m_size--;

    return returnval;
}

More details would be good for a more accurate answer. But from the code I'm guessing you're trying to implement something like this.

int StackOfBoxes::pop()
{
    // initialize
    int returnval = 0;

    if (!isEmpty())
    {       
        // fetch the value from the top one 
        returnval = m_top -> m_value;

        Box* temp = m_top;

        //move m_top down to previous box.
        m_top = m_top -> m_previous;

        //delete the popped one
        delete temp;        

        m_size--;       
    }
    else 
    {
        // you may want to throw an exception here for popping a empty stack
    }

    return returnval;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM