0

作成した連結リストを印刷したい。最初と最後の要素を出力する方法はわかりましたが、リスト全体を出力する方法が思いつきません。

最初の要素から次の要素に移動する必要があり、最後に停止の条件が必要です。

しかし、私はこれにイテレータを実装しません。ポインターとノードを使用するだけで、リスト全体を印刷できますか?

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

2 に答える 2

0
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

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.

于 2013-04-14T00:02:26.597 に答える
0

リンク リストの要点は、1 つの要素を使用して次の要素にアクセスできることです。したがって、ポインターを使用してリストを反復処理するだけです。

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;
}
于 2013-04-14T00:07:02.973 に答える