2016-03-28 15 views
1

私はリンクリストで動作するC++ programmを作成しています。しかし、私は他の構造にある構造にどのようにアクセスできるのか分かりません。struct C++内の構造体へのアクセス

#include <cstddef> 
#include "list.hpp" 
using std::size_t; 

struct list { 
    struct node { 
     double val; 
     node* prev; 
     node* next; 
    }; 

    node* head = nullptr; 
    node* tail = nullptr; 
    size_t size = 0; 
}; 

どのように動作するのか説明できますか?メソッドがありますが、このメソッドでこの構造体をどのように使用できるかはわかりません。

void push_back(list& l, double elem) { 
    node *new_node = new node(elem); 
    if (l.head==null) { 
     l.head = new_node; 

    } 
    node *curent = l.head; 
    while (curent) { 
     if (!curent->next) { 
      curent->next = new_node; 
     } 
     cur = cur->next; 
    } 
} 

ありがとうございます。このコードで

+4

'node'ではなく' list :: node' – deviantfan

+0

あなたは 'node'コンストラクタを定義していないので、' new node(elem) 'を実行することはできません。 – Barmar

+0

@Barmarなので、このようにしなければならないということです。 'list :: node * new_node = new list :: node(elem);'? – Lemmy

答えて

1

、あなたが二重にリンクされたリスト

を持って、私は一back関数のコードを説明しようとするでしょう。

私たちはvoid push_back(リスト&l、double elem)を持っています.lはあなたの現在のLinkedListが新しい要素をキューに追加したい場合、elemは新しい要素の値です。

if (l.head==null) { 
    l.head = new_node; 
} 

あなたのLinkedListのが空の場合、LinkedListのが空でない場合、我々は

exemple1 : empty LinkedList

新しい要素を追加

push back

この単純なコード

node *curent = l.head; // the current node is pointed to the head of the LinkedList 
    while (curent->next != null) { // while current->next is not equal to null 
      curent=curent->next ; // step forward to the next node 
     } 
     curent->next =new_node ; // add the new node to the queue of the linkedList 
関連する問題