C++ 链表输出1太多?

C++ 链表输出1太多?,c++,data-structures,linked-list,C++,Data Structures,Linked List,我正在创建一个单链表,并在开始节点中添加一个。每当我运行我的测试仪时,它都会工作,但它(我假设)会在地址的开头添加一个额外的节点 测试人员: #include <iostream> #include "linkedlist.h" using namespace std; void test01() { LinkedList < int > A; cout << endl << endl; cout << " ***

我正在创建一个单链表,并在开始节点中添加一个。每当我运行我的测试仪时,它都会工作,但它(我假设)会在地址的开头添加一个额外的节点

测试人员:

#include <iostream>
#include "linkedlist.h"

using namespace std;

void test01() {

  LinkedList < int > A;

  cout << endl << endl; 
  cout << " ***************** " << endl;
  cout << " *  TEST SET #1  * " << endl;
  cout << " ***************** " << endl;


  cout << "Is the list empty? " << boolalpha << A.isEmpty() <<endl; 
  cout << A << endl;
  cout << "Size of A = " << A.size() << endl;

  //TEST : Inserting 10 numbers to a
  cout << endl << "TEST : Inserting 10 numbers to A" << endl;
  for (int k=0; k<10; k++)
  {
    A.insert_front(k+1);
  } 
  cout << A << endl;
  cout << "Size of a = " << A.size() << endl;

  //TEST : Clearing A
  cout << endl << "TEST : Clearing A" << endl;
  A.clear();
  cout << A << endl;
  cout << "Size of A = " << A.size() << endl << endl;


  cout << "Test 01 - Done!" << endl;
} // Destructor Called Here!!

int main () {

  cout << "Hello World!!, This is the LinkedList LARGE Tester" << endl; 

  test01();


  cout << "LARGE Done!" << endl;
  return 0;
}
#包括
#包括“linkedlist.h”
使用名称空间std;
void test01(){
链接列表A;

cout让我们看看将第一个节点添加到列表时会发生什么:

template <class T>
 void LinkedList<T>::insert_front(const T& x)
 {
     if(m_next == NULL) // m_next is NULL
     {
        // ok, let's add the first node

        m_next = new LinkedList<T>;
        m_next->m_data = x;
        m_next->m_next = NULL; // this line isn't neccesary, the default constructor
                               // called in the new expression above took care of that
        // you should utilize the other constructor and say
        // m_next = new LinkedList<T>(x, m_next);

        // ok, done, let's continue with the code below
     }

     // Wait a second! We already added a node, what are we doing here?

     LinkedList<T> *temp;
     temp = new LinkedList<T>;
     temp->m_data = x;
     temp->m_next = m_next;
     m_next = temp;
 }

不过,我可以看到这种设计中存在一些问题。类本身同时充当容器和节点。通常,链表实现对节点使用单独的类,而实际的容器类只持有指向第一个节点的指针(可能还有缓存大小和尾部的成员等)。另一个问题是默认构造函数和插入第一个节点的方式。当前,默认构造的列表将未定义的
m_数据
作为第一个节点。第一次插入节点时,可能只需将
m_数据
设置为所需值,并将
m_next
设置为空。

<将第一个节点添加到列表中:

template <class T>
 void LinkedList<T>::insert_front(const T& x)
 {
     if(m_next == NULL) // m_next is NULL
     {
        // ok, let's add the first node

        m_next = new LinkedList<T>;
        m_next->m_data = x;
        m_next->m_next = NULL; // this line isn't neccesary, the default constructor
                               // called in the new expression above took care of that
        // you should utilize the other constructor and say
        // m_next = new LinkedList<T>(x, m_next);

        // ok, done, let's continue with the code below
     }

     // Wait a second! We already added a node, what are we doing here?

     LinkedList<T> *temp;
     temp = new LinkedList<T>;
     temp->m_data = x;
     temp->m_next = m_next;
     m_next = temp;
 }

不过,我可以看到这种设计中存在一些问题。类本身同时充当容器和节点。通常,链表实现对节点使用单独的类,而实际的容器类只持有指向第一个节点的指针(可能还有缓存大小和尾部的成员等)。另一个问题是默认构造函数和插入第一个节点的方式。当前,默认构造的列表将未定义的
m_数据
作为第一个节点。第一次插入节点可能只需将
m_数据
设置为所需值,并将
m_next
设置为空。

您的类不完整,您应该应用三法则。据我所知,你至少有一个内存泄漏。这是1:1的真实代码吗?这甚至不应该编译(实际上,它应该编译,但不是链接)–你不能只将模板代码转储到一个单独的编译单元中。我建议你节省时间和挫败感,使用STL
列表
。它已经被编写和验证,因此你不必这样做。你的类不完整,你应该应用三个规则。据我所知,你至少有一个内存泄漏。这是1:1的原因吗l代码?这甚至不应该编译(实际上,它应该编译,但不应该链接)–你不能只将模板代码转储到单独的编译单元中。我建议你节省时间和挫折,使用STL
列表
。它已经编写并验证过了,所以你不必这样做。当我将第二部分包装到else中时,第一个节点会因为某种原因被删除。因此,当它不能取消5,4,3,2]而不是5,4,3,2时,1]@T.J.Williams checked,它对我有效,可能是你迭代/打印列表的方式。所以当这个打印时,你会得到10,9,8,7,6,5,4,3,2,1?当我可以打印一些随机地址时,10,9,8,7,6,5,4,3,2,现在,m_数据总是节点1。你知道解决这个问题的方法吗?或者可能有第二部分(在m_next==NULL之外)跳过第一次调用它?当我将第二部分包装在一个else中时,第一个节点会因为某种原因而被删除。因此,当它展开5,4,3,2]而不是5,4,3,2,1]@T.J.Williams checked时,它对我有效,可能是你迭代/打印列表的方式。所以当它打印时,你会得到10,9,8,7,6,5,4,3,2,1?当我无法打印一些随机数据时地址,10,9,8,7,6,5,4,3,2现在,m_数据将始终是节点1。您是否知道解决此问题的方法?或者可能让第二部分(m_外部的next==NULL)在第一次调用时跳过它?
template <class T>
 void LinkedList<T>::insert_front(const T& x)
 {
     if(m_next == NULL) // m_next is NULL
     {
        // ok, let's add the first node

        m_next = new LinkedList<T>;
        m_next->m_data = x;
        m_next->m_next = NULL; // this line isn't neccesary, the default constructor
                               // called in the new expression above took care of that
        // you should utilize the other constructor and say
        // m_next = new LinkedList<T>(x, m_next);

        // ok, done, let's continue with the code below
     }

     // Wait a second! We already added a node, what are we doing here?

     LinkedList<T> *temp;
     temp = new LinkedList<T>;
     temp->m_data = x;
     temp->m_next = m_next;
     m_next = temp;
 }
m_next = new LinkedList<T>(x, m_next);