【问题标题】:Keeping track of stack size in linked list implementation of a stack C++ [closed]在堆栈 C++ 的链表实现中跟踪堆栈大小 [关闭]
【发布时间】:2014-12-18 07:54:56
【问题描述】:

我正在处理 ItemType 堆栈的链表实现(当前设置为双精度,但可以更改)值。尽管我的代码编译得很好,但我意识到我的 size 方法效率很低,因为它使用迭代来获取链表的大小——O(n) 时间复杂度——当它可以简单地将大小存储为节点的字段时,在我的其他方法中更新它,然后返回该值——改进方法使其具有 O(1) 时间复杂度。然而不幸的是,我在弄清楚如何集成这个新的大小字段时遇到了一些问题,特别是如何初始化它的值,并且我的编译器一直告诉我它无法被编辑。那么,我的问题是如何将 size 实现为节点类的一个字段(或者我应该在其他地方实现它?)以及我应该在我的代码中的什么位置初始化和更新它?

下面是我的链表栈的头文件:

#ifndef DBLSTACK_H
#define DBLSTACK_H


typedef double ItemType; // stack currently holds doubles


class DblStack
{
private:
    //Node class
    class Node
    {
    public:
        double data;
        Node *next;

        // Node constructor
        Node(double value, Node * link = 0)
        {
            data = value;
            next = link;
        }
    };

    typedef Node * NodePtr;

    // Data members
    NodePtr myTop;  //points to top of stack

  public:
    // Class Constructor
    DblStack();

    // Copy Constructor
    DblStack(const DblStack& rhs);

    // Class Deconstructor
    ~DblStack();

    // Assignment operator
    // Assigns a stack to another
    const DblStack& operator= (const DblStack& rhs);

    // isEmpty
    // Checks if the stack is empty
    bool isEmpty() const;

    // push
    // Pushes an item on top of the stack.
    void push(const ItemType& item);

    // pop
    // Pops the top item off the stack.
    void pop();

    // top
    // Returns the top item of the stack without popping it.
    ItemType top() const;

    // size
    // Returns the number of items on the stack.
    size_t size() const;

};

#endif

这是我的源文件:

#include <cstddef>  //for NULL
#include <stdexcept>
#include "DblStack.h"
using namespace std;

// Class Constructor
DblStack::DblStack()
: myTop(0)
{
}


// Copy Constructor
DblStack::DblStack(const DblStack& rhs)
{
    myTop = 0;
    if (!rhs.isEmpty())
    {
        // Copy first node
        myTop = new DblStack::Node(rhs.top());

        // Set pointers to run through stack
        DblStack::NodePtr lastPtr = myTop;
        DblStack::NodePtr origPtr = rhs.myTop->next;
        while (origPtr != 0)
        {
            lastPtr->next = new DblStack::Node(origPtr->data);
            lastPtr = lastPtr->next;
            origPtr = origPtr->next;
        }
    }
}



// Class Deconstructor
DblStack::~DblStack()
{
    // Set pointers to run through stack
    DblStack::NodePtr curr = myTop, next;
    while (curr != 0)
    {
        next = curr->next;
        delete curr;
        curr = next;
    }
}


// Assignment operator
// Assigns a stack to another
const DblStack& DblStack::operator= (const DblStack& rhs)
{
    if (this != &rhs)
    {
        this->~DblStack();
        if (rhs.isEmpty())
        {
            myTop = 0;
        }
        else
        {
            DblStack tmp(rhs);  // Call copy constructor
            std::swap(myTop, tmp.myTop);
        }
    }
    return *this;
}


// isEmpty
// Checks if the stack is empty
bool DblStack::isEmpty() const
{
    return (myTop == 0);
}


// push
// Pushes an item on top of the stack.
void DblStack::push(const ItemType& item)
{
    myTop = new DblStack::Node(item, myTop);
}


// pop
// Pops the top item off the stack.
void DblStack::pop()
{
    if (!isEmpty())
    {
        DblStack::NodePtr ptr = myTop;
        myTop = myTop->next;
        delete ptr;
    }
    else
    {
        throw std::underflow_error("Stack is empty");
    }
}


// top
// Returns the top item of the stack without popping it.
ItemType DblStack::top() const
{
    if (!isEmpty())
    {
        return myTop->data;
    }
    else
    {
        throw std::underflow_error("Stack is empty");
    }
}


// size
// Returns the number of items on the stack.
size_t DblStack::size() const
{
    size_t size = 0;
    DblStack::NodePtr ptr;
    for (ptr = myTop; ptr != 0; ptr = ptr->next)
    {
        size++;
    }
    return size;
}

虽然改进我的尺寸方法是这个问题的主要目标,但我也非常感谢您对优化我的代码提出的任何其他建议。谢谢!

【问题讨论】:

  • 这个问题似乎跑题了,因为它是关于代码审查的。
  • 这应该在 CodeReview.SE 上进行。
  • this-&gt;~DblStack(); ??!!为什么? const DblStack&amp; DblStack::operator= (const DblStack&amp; rhs) { DblStack temp(rhs); std::swap(temp.myTop, myTop); return *this; } 这就是您的 operator= 需要做的所有事情。

标签: c++ linked-list stack


【解决方案1】:

将 size 包含为 DblStack 的成员,并修改其其他方法以使其保持最新。

【讨论】:

  • "...修改DblStack的其他方法..."
  • 对不起,我误解了你答案的第二部分。
【解决方案2】:

您需要在 DblStack 类中添加一个 size 成员变量:

class DblStack
{
private:
    size_t size;
// ...
}

当你第一次构造一个 DblStack 时,它里面什么都没有,所以它的大小应该是 0:

DblStack::DblStack()
    : myTop(0)
    , size(0)
{ }

现在您需要考虑尺寸何时会改变!您应该会发现它唯一改变的情况是当项目被压入或从堆栈中弹出时。因此,您会希望您的 push 和 pop 方法反映这一点:

// Push increases stack size
size++;

// Pop decreases stack size
size--;

最后,您可以更改 size() 方法以仅返回大小:

size_t size() const { return size; }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-23
    • 1970-01-01
    • 2014-03-29
    • 1970-01-01
    • 1970-01-01
    • 2017-08-06
    • 1970-01-01
    相关资源
    最近更新 更多