【发布时间】: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->~DblStack();??!!为什么?const DblStack& DblStack::operator= (const DblStack& rhs) { DblStack temp(rhs); std::swap(temp.myTop, myTop); return *this; }这就是您的 operator= 需要做的所有事情。
标签: c++ linked-list stack