【问题标题】:C++ Building a Sorted Link List from a LoopC++ 从循环中构建排序链表
【发布时间】:2013-02-07 04:18:48
【问题描述】:

我已经把头撞在墙上大约 3 个小时了,现在试图想出解决这个问题的方法,但我想不通。我的测试程序就是这样写的……

int main()
{
  SimpList<int> intList;   // (empty) list of integers

  cout << "Let's build a sorted list of integers." << endl;
  cout << endl << "Uninitialized List: ";
  intList.print();
  cout << endl << "Length: " << intList.size() << endl;

  int intData[] = { 5, 3, -2, 7, 9, -8, 1, -4 };

  for (int i=0; i<8; i++)
    intList.insert( intData[i] );

  cout << endl << "After inserting 8 integers: ";
  intList.print();
  cout << endl << "Length: " << intList.size() << endl;

  system("PAUSE");
  return 0;
}

所以链接列表是从一个数组和一个 for 循环中初始化的。我的节点和列表的类代码在这里...

    template < typename T >   // Forward declaration of the SimpList class
class SimpList;

template < typename T >
class Node                 // Node class for the SimpList class
{
  private:
    // Constructors
    Node () { next = 0; }  // default constructor

    // Complete the definition inline
    Node ( const T &initItem, Node<T> *ptr ) { }

    // Data members
    T           item;   // Node data item
    Node        *next;  // Pointer to the next node

  friend class SimpList<T>;
};

template < typename T >
class SimpList
{
  public:

    // Constructor (add your code inline)
    SimpList ()
    {
      head = &PHONY;
      length = 0;
    }


    // List manipulation operations
    void insert ( const T &newitem );   // insert a data item
    bool remove ( T &item );            // remove data item
    bool find ( T &item ) const;        // find data item
    void clear ();                      // empty the list

    bool isEmpty () const { 
     if (length == 0)
         return true;
     else
         return false;
    }

    // length accessor method
    int size () const { 
        return length;
    }

    // print the list items
    void print () const;

  private: // data members
    Node<T> PHONY;      // empty node that anchors the list
    Node<T> *head;      // pointer to the beginning of the list
    int length;         // length of list
};

然后插入和打印函数如下...

template < typename T >
void SimpList<T>::print() const
{
  if (length == 0)
  {
    cout << "List is empty." << endl;
    return;
  }

  Node<T> *ptr = head->next;
  while (ptr != NULL)
  {
    cout << ptr->item << "  ";
    ptr = ptr->next;
  }
  cout << endl;
}

template < typename T >
void SimpList<T>::insert(const T& newitem) {

        Node<T> *currN = head->next;
        Node<T> *prevN = 0;
        Node<T> *tmp = new Node<T>();
        tmp->item = newitem;

        if(head->next == NULL ) {
          head->next = tmp;
        }
        else {
            prevN = head;
            while(currN != NULL && currN->item < newitem) {
                prevN = currN;
                currN = currN->next;
            }
            prevN->next = tmp;
        }
        length++;
        print();
}

我将最后一个“print()”插入到 insert 函数中,作为调试正在发生的事情的一种方式,输出非常令人困惑,因为它给了我

5 3 -2 -2 7 -2 7 9 -8 -8 1 -8 -4

但我希望输出从小到大排序 (-8 -4 -2 1 3 5 7 9)

编辑:已解决...忘记更新 tmp->currN 旁边。 DERP。

【问题讨论】:

  • “但每次由于某种原因失败”,什么失败了?不编译?不执行?,没有按您的预期工作?吹你的电脑? 究竟是什么?我们要猜吗?
  • 哈哈对不起,我忘了在最后添加。当我编译时,它根本不会将任何项目添加到列表中。如果我删除 else 和 while 情况,那么它将添加数组中的第一项,但是当我添加这两种情况时,不会添加任何项目
  • 我不明白用头撞墙有什么帮助...
  • 在经历了这些头痛之后,它让你的头感觉好多了

标签: c++ list insert sorted


【解决方案1】:

你需要解决的几件事:

  1. PHONY-&gt;next 不为 null ,因此您的初始化将失败:

    if(head->next==NULL) {
      head->next=tmp;
      return;
    }
    
  2. 节点构造函数应该在 0 旁边初始化而不是 this ,因此您永远不会在列表末尾遇到 NULL 条件。

  3. 在 else 语句中插入时,您应该从 head->next 开始搜索,目前您从 currn = head 开始搜索,即 PHONY,这是不正确的。

  4. 您还需要适当地设置tmp-&gt;next,当您在列表中间插入时请考虑这种情况。

【讨论】:

  • 我将构造函数更改为初始化为 0 而不是 this 并更新了 currN = head->next 但我不明白如何处理数字 1 和数字 4。
  • ok 为 1st ,因为您的 head 已初始化为 PHONYPHONY next 未初始化为垃圾值,因此您的初始化将失败。对于第 4 点,请考虑您的列表 1->2->7->10->null ,当您在其中插入 3 时,您的 prevN 将指向元素 2。所以您需要说:prevN-&gt;next = tmp; tmp-&gt;next = currN
  • 我该如何解决这个问题?很抱歉,我们的讲师在解释 PHONY 节点的整个命题方面做得非常糟糕,但我的理解是它应该是一个 NULL 节点来标记列表的结尾。
  • 实际上,一旦在构造函数中在 0 旁边进行初始化,就应该注意第一个条件。不会是垃圾,之前是this
  • 我已将构造函数设置为在 0 旁边初始化而不是这个,现在输出在打印时达到 -8 -4 然后停止。我正在将打印功能添加到原始问题中,以便您查看是否也搞砸了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-05
  • 1970-01-01
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多