0

タイトルは自由に編集してください。

単純な連結リストを作成する必要があります (変更することはできません。こうあるべきです)。いいえ、STL または std::list を使用できません。そのほとんどは紙の上で行われますが、非常に基本的なカーソルの実装に問題があるようです。

これはリスト内の私のノードです (その一部):

struct Node {
    int ap_nr;
    Node *next;
};

ノード追加機能でリストを調べたい:

void add_node (Node **begin, int ap_nr)
{
     stuff happens
}

これは私が関数を呼び出す方法です:

add_node(&(*begin), ap_nr);

begin (リストの先頭) から始まりcursor->next、最後に到達するまですべてのノードを通過するカーソルを作成したい(while (cursor->next!=0))

しかし、私は簡単に言うことはできません:

Node *cursor;
cursor = new Node;
cursor = begin;

これは単にカーソルを begin で上書きするため、私の試みは無効になります。開始するためのポインターを作成し、STRUCT 関数を呼び出すことができるようにする必要があります "-> next"

これどうやってするの ?

*また *以前のノードを思い出すにはどうすればよいですか? 私はこれを行うことができます:

Node *previous;
previous = new Node;
previous = &(*begin); // ?
4

1 に答える 1

1

関数内のリストをトラバースしたいようですadd_node。その場合は、次のことを試してください

void add_node (Node **ppBegin, int ap_nr)
{
  if (!ppBegin) {
    // Need to handle the case of bad user data here
  }

  // Traverse until we get the to the empty next value
  while ((*ppBegin)->next) {
    ppBegin = &((*ppBegin)->next);
  }

  // ppBegin now points to the address of where the new node should go
  Node* created = new Node();
  created->ap_nr = ap_nr;
  *ppBegin = created;
}

注: この関数を最初に呼び出すには、add_node(&theListPointer).

于 2012-04-02T17:33:16.217 に答える