简体   繁体   English

打印或遍历C ++中的链接列表

[英]Print out or Traverse the Linked List in C++

I want to print out the linked list that I've just made. 我想打印出我刚刚创建的链表。 I've figured out how to print out the first and last element but can't think of a way to print the whole list. 我已经弄清楚了如何打印出第一个和最后一个元素,但是想不出一种打印整个列表的方法。

I need to move from first element to next element and need a condition of stop at the end. 我需要从第一个元素移到下一个元素,并需要在最后停止的条件。

but I DO NOT IMPLEMENT iterator on this. 但我不对此实施迭代器。 Just using pointer and nodes can I print the whole list? 仅使用指针和节点就可以打印整个列表吗?

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
       }
Node* p = myList.head;
while(p) {
  std::cout << p->elem << " ";;
  p = p->next;
}

It does as you would expect, continues to go to the next element until it is NULL(reaches end) NULL is changed to 0 by the compiler, and 0==false 如您所料,它继续执行下一个元素,直到它为NULL(到达结尾)为止。编译器将NULL更改为0,并且0 == false

You should try looking at your lecture notes, since I already answered a question about this class for you. 您应该尝试查看您的讲义,因为我已经为您回答了有关该课程的问题。

Also, I believe that generally tail points to the last element, not one past it. 另外,我相信通常尾巴指向最后一个元素,而不是最后一个。 So printing the last element should be 所以打印最后一个元素应该是

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

Unless your instructor defined otherwise. 除非您的讲师另有定义。

And clearly you are going to need to make 'getter' functions for most of these variables as they are declared to be private. 显然,您将需要为大多数这些变量创建“ getter”函数,因为它们被声明为私有变量。

The whole point of the linked list is that you can use one element to access the next element. 链接列表的重点是可以使用一个元素来访问下一个元素。 So you just need to iterate through the list using a pointer. 因此,您只需要使用指针遍历列表即可。

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM