I just want to create the linked list of n nodes using a loop.here is the code it will works as a infinite loop.
can anybody tell why it is not working and why it goes to an infinite loop.[BTW in am begineer in data structures so explain me according to that]
#include<iostream>
#include<stdlib.h>
usingnamespace std;
struct Node{
int info;
struct Node *next;
};
int main()
{
struct Node list,*start;
list.info=10;
list.next=NULL;
start=&list;
cout<<"enter number of nodes you want to make a linked list: ";
int n;
cin>>n;
n=n-1; // because first node is declared previously
for(int i=1;i<=n;i++)
{
struct Node temp,*trev;
temp.info=list.info+1;
temp.next=NULL;
trev=start;
// loop used for treversing all nodes
while(true)
{
if(trev->next==NULL)
{
trev->next=&temp;
break;
}
else
{
trev=trev->next;
}
}
}
// outputing the info of each node
for(Node *trev=start;trev!=NULL;trev=trev->next)
{
cout<<trev->info<<"\t"<<trev;
}
return 0;
}