This is only an addition to melpomene's answer, responding to the comments:
How do I create n nodes? Use of vectors is prohibited, so I am creating a linked linked list of n nodes, but the problem is I am unable to create n nodes in a loop.
Actually, your code was less incorrect than you thought, solely you missed one important point:
nn->next = n;
With this instruction, you lost the last reference to what nn
pointed to originally and you won't ever be able to re-gain it again...
What you yet need is a second pointer to the element created initially, so you don't lose your list head and thus the rest of the list any more:
node* nn = new node(); // this is the C++ way...
node* head = nn; // now you have a second pointer to the head...
for(int i=0;i<10;i++)
/* [...] */
Now you can access all the nodes you created via the head
pointer (as long as you do not move it away from...).
Get rid of the loop and simply use struct *n = malloc (sizeof *n * 10); – David C. Rankin
Another variant: creating 10 elements directly... Assuming node looks like this:
struct node
{
Data data;
node* next;
};
Then you get 10 elements of Data
simply by
Data* allData = new Data[10]; // again using the C++ way...
The C++ aproach has another advantage: the elements in the array are already initialised as the default constructor gets called (for primitive data types, see here).