I want to add random numbers into a linkedlist , and I dont know how to get the position for random numbers ? The problem indicates that the user should insert the number of elements and the program having the ability to create the nodes with a random value . here is what i have tried , but I got nothing:
class Node
{
public:
int info;
Node* next;
};
class List:public Node
{
Node *first,*last;
public:
List()
{
first=NULL;
last=NULL;
}
void create();
void insert();
void delet();
void display();
void search();
};
void List::create()
{
Node *temp;
temp=new Node;
int n;
int z;
cout<<"\nEnter the Element:";
cin>>n
if(first==NULL)
{
first=temp;
last=first;
}
else
{
last->next=temp;
last=temp;
}
}
void List::insert()
{
* Node *prev,*cur;
int z;
prev=NULL;
cur=first;
int count=1,pos,ch,n;
Node *temp=new Node;
cout<<"\nEnter the number of Elements:";
cin>>z;
for(int i,i<z,i++)
{
temp->info=rand();
temp->next=NULL;
}
cout<<"\nINSERT AS\n1:FIRSTNODE\n2:LASTNODE\n3:IN BETWEEN FIRST&LAST NODES";
cout<<"\nEnter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
temp->next=first;
first=temp;
break;
case 2:
last->next=temp;
last=temp;
break;
case 3:
while(count!=pos)
{
prev=cur;
cur=cur->next;
count++;
}
if(count==pos)
{
prev->next=temp;
temp->next=cur;
}
else
cout<<"\nNot Able to Insert";
break;*/
}
Aucun commentaire:
Enregistrer un commentaire