【问题标题】:Print out or Traverse the Linked List in C++在 C++ 中打印或遍历链表
【发布时间】:2013-04-13 23:52:57
【问题描述】:

我想打印出我刚刚制作的链表。 我已经知道如何打印出第一个和最后一个元素,但想不出打印整个列表的方法。

我需要从第一个元素移动到下一个元素 最后需要一个停止条件。

但我没有在此实现迭代器。 仅使用指针和节点可以打印整个列表吗?

int main ()
{ LinkedList name_list;
  name_list.TraPrinhead(name_list); }

void LinkedList::TraPrinHead(const LinkedList& p)
{ 
  cout << "The First Element of this List is : ";
  cout << header->next->elem; // print out the first element
  cout << endl;

  cout << "The Last Element of this List is : ";
  cout << tail->prev->elem; // print out the first element
  cout << endl;

  cout << "Now the whole list.......";
  cout << ??????????????????????
}






  class LinkedList {

  public: class Nodes { // Doubly Linked List Node
  public:
    Nodes(const string& e);
    void ToNodeValue(const string& e);
    string getElemValue() const;
    void printNodeValue();
  private:
     string elem;  // node element value
     Nodes* prev; // previous node in list
     Nodes* next; // next node in list
     // pointer that points to current node is this pointer

  public:
    void ConnectSingly(Nodes* a, Nodes* b); 
    void ConnectDoubly(Nodes* a, Nodes* b);

  friend class LinkedList; 
  };


  public:
    LinkedList(); 
    virtual ~LinkedList(); 
    bool empty() const; 
    const string& getFirst() const; 
    const string& getLast() const; 
    void addtoFront(const string& e); 
    void addtoBack(const string& e); 
    void TraPrinHead(const LinkedList& p); 
  private: 
     Nodes* header; 
     Nodes* tail;

  protected: 
    void InsertDoublyBefore(Nodes* d, const string& e); 
    void InsertDoublyAfter(Nodes* d, const string& e);

  friend class Nodes;
  };


       void LinkedList::InsertDoublyBefore(Nodes* d, const string& e) {

       if (header->next == tail)
         { // header->next->elem = e;
           Nodes* n = new Nodes;
        n->elem = e; 
            n->next = tail;
        n->prev = tail->prev;
            tail->prev->next = tail->prev = n; 
        header->next = n; 
       }
       else
       {
        Nodes* n = new Nodes; 
        n->elem = e;
        n->next = d;
        n->prev = d->prev;
        d->prev->next = d->prev = n; 
       }

       } 

       void LinkedList::InsertDoublyAfter(Nodes* d, const string& e) 
       {
          InsertDoublyBefore(d->next, e);
       }

       void LinkedList::addtoFront(const string& e)  { InsertDoublyBefore(header->next, e); }

       void LinkedList::addtoBack(const string& e) { InsertDoublyBefore(tail, e); }


       void LinkedList::Nodes::ConnectSingly(Nodes* a, Nodes* b)
       {
        a->next = b; // a's next pointer points to b
       }

       void LinkedList::Nodes::ConnectDoubly(Nodes* a, Nodes* b)
       {
        a->next = b; // a's next pointer points to b
        b->prev = a; // b's prev pointer points to a
       }

【问题讨论】:

    标签: c++ pointers linked-list graph-theory traversal


    【解决方案1】:
    Node* p = myList.head;
    while(p) {
      std::cout << p->elem << " ";;
      p = p->next;
    }
    

    如你所料,继续到下一个元素直到它为 NULL(到达结束) NULL被编译器改成0,0==false

    你应该试着看看你的讲义,因为我已经为你回答了一个关于这门课的问题。

    另外,我相信 tail 通常指向最后一个元素,而不是过去的一个元素。 所以打印最后一个元素应该是

    cout << tail->elem; // print out the first element
    

    除非你的导师另有定义。

    很明显,您需要为这些变量中的大多数创建“getter”函数,因为它们被声明为私有。

    【讨论】:

    • 非常感谢!!!!!!非常感谢!!!!!!非常感谢!!!!!!非常感谢!!!!!!你们救了我的命
    • 我得到了这个无限循环。到底有没有办法停下来?
    • 当 p==tail 时,p->next==NULL。届时它将停止。当 p 指向 NULL 时它将停止。因为,就像我说的,NULL==0==FALSE
    • 我会仔细检查您的插入功能。并确保将 tail->next 保持为 NULL,每当您将元素添加到末尾(在 tail 之后)
    • 谢谢,我发布了我的插入函数……stackoverflow.com/questions/15994609/…
    【解决方案2】:

    链表的全部意义在于您可以使用一个元素访问下一个元素。所以你只需要使用指针遍历列表。

    Nodes* currentNodes = header->next; //pointer used to iterate through list; initially points at first element of list
    
    while(currentNodes != tail) { //condition to stop
      cout << currentNodes->elem << endl; 
      currentNodes = currentNodes->next;
    }
    

    【讨论】:

    • 非常感谢!!!!!!非常感谢!!!!!!非常感谢!!!!!!非常感谢!!!!!!你们救了我的命
    • 我可以无限循环。到底有没有办法停下来?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 2013-07-28
    • 2018-11-23
    • 1970-01-01
    相关资源
    最近更新 更多