初めてリンクされたリストを作成して、何らかの理由で予想される出力が得られません。私は他の投稿を検索し、それを引き出しましたが、まだ問題を見つけることはできません。任意の洞察が評価されます。私は頭に2を、尾に7を挿入し、メインで印刷を呼び出すと、2つしか印刷されません。リンクリストの印刷が機能していません..?
void List::print()
{
if (length == 0)
cout << "The list is empty!" << endl;
else
{
Nodeptr temp = head; //temporary iterator
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->nextNode;
}
cout << endl;
}
}
void List::insert_head(int data)
{
if (length == 0)
{
head = new Node(data);
tail = head;
}
else
{
Nodeptr N = new Node(data); //create node by calling constructor
N->nextNode = head; // set new nodes next to point to current head
head = N; //make the head the new node
}
length++;
}
void List::insert_tail(int data)
{
if (length == 0)
{
head = new Node(data);
tail = head;
}
else
{
Nodeptr N = new Node(data);
N->nextNode = head;
tail = N;
}
length++; //increase the list length
}
リストとメインの定義を投稿する必要があります。 –