如何实现pop()函数

时间:2014-03-04 04:43:28

标签: c++ pointers

我正在编写一个程序,它将实现堆栈的所有功能,但我似乎无法做到这一点。我在最后一部分pop()而且我被卡住了。我无法弄清楚如何将m_top指针向下移动到堆栈中的下一个框,或者如果堆栈为空则返回nullptr并从顶部框返回值。

#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;
}

标头文件

#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

程序通过主文件进行测试。

#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;
    }

}

3 个答案:

答案 0 :(得分:0)

您无需分配

new Box;

弹出时。

我希望这更像是你想要的......

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;
}

答案 1 :(得分:0)

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;
}

答案 2 :(得分:0)

更多详细信息可以帮助您获得更准确的答案。但是从代码我猜你正试图实现这样的东西。

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;
}