i was trying to generate some random linked lists to check a function i wrote when i noticed the results for the second linked list(which is smaller) were in relation with the first one such as the digits in the second linked list are the last of the first one. for example:
L1:5 8 9 7 6 3
L2:7 6 3
here is my code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
typedef struct element* node;
typedef struct element
{
int data;
node next;
}celule;
void generator(node *head,int n)
{
node temp;
srand(time(NULL));
for(int i=0;i<n;i++)
{
if(*head==NULL)
{
*head=malloc(sizeof(celule));
(*head)->data=rand()%20+1;
(*head)->next=NULL;
}
else
{
temp=malloc(sizeof(celule));
temp->data=rand()%20+1;
temp->next=*head;
*head=temp;
}
}
}
void display(node head)
{
while(head!=NULL)
{
printf("%d ",head->data);
head=head->next;
}
}
int main()
{
node L1,L2;
L1=NULL;
L2=NULL;
generator(&L1,5);
generator(&L2,2);
display(L1);
printf("\n");
display(L2);
return 0;
}
Aucun commentaire:
Enregistrer un commentaire