【问题标题】:Checking if a singly linked list is empty c++检查单链表是否为空c ++
【发布时间】:2011-04-27 16:16:40
【问题描述】:

我正在尝试找出确定单链表是否为空的最佳和最简单的方法。

我需要创建一个布尔方法吗?

谢谢

读取方法

void List::Read(istream& r) {

char c[13];
r >> c;
r >> numberOfInts;

Node *node = new Node();

for(int i = 0; i < numberOfInts; i++)
{
    r >> node->data;
    cout << node->data << endl;
    node->next = new Node;
    //node = node->next;

    head = node;
}
}
else
{
    if(node->data > head->data)
    {
        head->next;
    }
    else if(node->data < head->data)
    {
        Node* tempNode;
        tempNode = head;
        head->data = node->data;
        node->data = tempNode->data;
    }
}
system("pause");

}

头文件

class Node
{
public:
    Node() {}
    Node(int d, Node* q = 0) : data(d), next(q) {} //constructor with parameters data and next
    int data; //holds data in node
    Node* next;//pointer to next node
};

class List
{
public:
    void Read(istream&);
    void Write(ostream&);

    void setReadSort(bool);
    void sortOwn();
    void insertionSort(Node*);
    bool isEmpty();

    bool _sortRead;
    int numberOfInts;

    List(void);
    ~List(void);
protected:
    Node *head;
    Node current;
};

【问题讨论】:

  • 抱歉,如果您需要更多信息
  • 'List' 不应有 'current' 数据成员,因为“current”节点仅在对列表执行某些操作的上下文中才有意义 - 根据定义,它是临时的,而不是数据模型的一部分。

标签: c++ if-statement singly-linked-list


【解决方案1】:

这完全取决于实现。但是,这通常可以通过检查第一个节点是否存在/是否有内容/等来非常快速地完成。

【讨论】:

  • 第一个节点始终存在,如果它没有读入任何内容,那么读入的是内存地址-8etc...
  • @Tazzy:如果第一个节点始终存在,它可能会将数据“存储”在指针或类似物中。只需检查指针是否为 NULL。
  • @Tazzy:如果您希望我们提供帮助,您需要向我们提供更多信息。除非我们知道事物的存储方式和格式,否则我们无法真正告诉您如何检查这一点
【解决方案2】:

是的。原因是:有时我们使用迭代器插入元素,处理迭代器上的元素数量会很不舒服(或不可能)。这就是为什么许多 STL 实现对size_t size(void) 函数具有线性时间的原因。 bool empty(void) 是检查列表是否为空且时间恒定的有效方法。

请注意:当您使用 STL 时,更喜欢使用 bool empty(void) 而不是 size_t size(void)。更多信息在:Meyers: Effective STL。

功能很简单:

bool empty( void ) const
{
  return ( this->begin() == this->end() );
}

在你的情况下:

bool empty( void ) const
{
  return ( this->head == 0 );
}

【讨论】:

  • 我不需要把 == 0 因为没有那个说它是空的
  • 我总是写出== 0,因为它对我来说更容易阅读。当然:你也可以不用== 0 来使用它。
  • @Tazzy:保留== 0 或使用bool(this-&gt;head)。 ;)
  • 也不是,是我用错了吗?这不会让我感到惊讶,我对此很陌生
【解决方案3】:

我没有对此进行测试,但它应该可以工作。

[编辑] 更改程序以说明始终存在的头部。

[edit] 更改程序以说明类,而不是结构

bool isEmpty(){return (this->head == NULL)};

这样会得到更合适的结果

还有一件事,我看到您正在使用 system("pause");。我知道这是家庭作业,但这是非常糟糕的做法。更好的方法是首先清除 stdin 中的缓冲区(如果需要),然后忽略一个字符。一个例子是:

cin >> somenumber; // Extract a number (assuming its safe)
cin.ignore(1, 10); // Ignore 1 character, or a newline, whichever comes first
                   // ^ this clears the buffer, it does NOT wait for input since
                   // the formatted extraction left a character in the buffer ('\n')
cin.ignore();      // Essentially the same as above, but since there is nothing in
                   // the buffer, it reads a single character from stdin

【讨论】:

  • isEmpty 方法明显中断
  • 不是我,我还没有投票,甚至不知道如何投反对票
  • @Tazzy 只要您将其设为类方法,它就可以工作。 this->head == NULL 只有在没有 head 时才会返回 true。如果你想总是有一个头,那么你将它更改为 this->head->next == NULL,以检查头是否有它指向的节点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-01
  • 2013-05-14
  • 1970-01-01
相关资源
最近更新 更多