I have been given a sequence of integers & I need to create a linked list from them. I have tried the following code but it is erroring out with 'Runtime error'. I couldn't figure out what's the issue with this code snippet.
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class list1 {
public:
int val;
list1 *next;
list1(int x) : val(x), next(NULL) {}
};
int main() {
int n;
cin >> n;
list1 *x = NULL;
list1 *t = NULL;
int temp;
cin >> temp;
list1 A = list1(temp);
t = &A;
for (int i = 1; i < n; i++) {
int temp;
cin >> temp;
list1 k = list1(temp);
t->next = &k;
t = t->next;
}
x = &A;
while (x != NULL) {
cout << x->val << " ";
x = x->next;
}
cout << endl;
return 0;
}