Hi!
I'am trying to make my own function which creates linked list. I've already managed to do this function which creates the list from back to start, but now I would like to do it the other way, from start to the end. I have this piece of code:
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
struct node {
int info;
node* next;
};
node* start = NULL;
node* end = NULL;
void list_create(int N, node*& P, node*& Q)
{ // P will point on the first node and Q on the last one
node* R;
P = new node;
P->info = 1;
P->next = NULL;
Q=P;
for (int i=2; i!=N; ++i) {
R = new node;
R->info = i;
Q->next = R;
Q=R;
}
Q->next = NULL;
}
int main()
{
cout << "Set the size: " << endl;
int size;
cin >> size;
list_create(size, start, end);
while (start != end) {
cout << start->info << endl;
start=start->next;
}
return 0;
}
The problem is, that the output looks like this:
Set the size:
5
1
2
3
By the way it's supposed to write it's values from start to end. And for simplicity I am making it just a list of integers...
Do you have a clue what the problem might be?
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
struct node {
int info;
node* next;
};
node* start = NULL;
node* end = NULL;
void list_create(int N, node*& P, node*& Q)
{ // P bude ukazovat na zaciatok zoznamu a Q na koniec
node* R;
P = new node;
P->info = 1;
P->next = NULL;
Q=P;
for (int i=2; i<=N; ++i) {
R = new node;
R->info = i;
Q->next = R;
Q=R;
}
Q->next = NULL;
}
int main()
{
cout << "Set the size: " << endl;
int size;
cin >> size;
list_create(size, start, end);
for (int i=0; i != size; ++i) {
cout << start->info << endl;
start=start->next;
}
return 0;
}