わかりました、これはとてつもなく簡単な質問ですが、何らかの理由でリンクされたリストを機能させることができません。これまでに何百万回もやったので、本当に疲れているからかもしれません。私のプログラムを可能な限り単純な実装にまで煮詰めましたが、まだ機能していません。
非常に基本的な実装です。整数の LL を作成するだけです。これまでに 100 万回実行したことがありますが、なんらかの理由で頭を超えて進むことはありません。
main.cpp
#include <iostream>
#include "ll.h"
using namespace std;
int main()
{
int x;
list ll;
int i =0;
while(i == 0)
{
cout << "Enter a value to add to the LL ";
cin >> x;
ll.add(x);
ll.display();
}
return 0;
}
ll.h
struct node
{
int val;
node * next;
};
class list
{
public:
list();
void add(int);
void display();
node * head;
};
ll.cpp
#include <iostream>
#include "ll.h"
using namespace std;
list::list()
{
head = NULL;
}
void list::add(int x)
{
if(!head)
{
cout << "First " << endl;
head = new node;
head->val = x;
head->next = NULL;
}
else
{
node * current = head;
while (current)
current = current->next;
current = new node;
current->val = x;
current->next = NULL;
}
}
void list::display()
{
node * current = head;
while(current)
{
cout << current->val << endl;
current = current->next;
}
}