I need to write a program that generates 5 random numbers and puts them into
a linked list. (Print the list) From that list it forms another list in a
way if the arrangement of elements in the first one was 5 8 3 1 in the new
one it should be
1 3 8 5 (print the new list)
where's the error in my code and why?
#include<stdio. h>
#include<stdlib .h>
#include<time.h >
struct lista{
int element;
struct lista *next;
}*pocetak;
main()
{
struct lista *q, *nova;
int i;
pocetak=NULL;
srand(time(NULL ));
for(i=0;i<5;i++ )
{
q=(struct lista*) malloc(sizeof(s truct lista));
q->element=rand() %100;
pocetak=q;
q->next=NULL;
}
q=pocetak;
printf("Nasumic ni brojevi:\n");
while(q!=NULL)
{
printf("\n%d",q->element);
q=q->next;
}
q=pocetak;
for(i=0;i<5;i++ )
{
nova=(struct lista*) malloc(sizeof(s truct lista));
nova->element=poceta k+(5-i)*(sizeof(stru ct lista));
pocetak=nova;
nova->next=NULL;
}
nova=pocetak;
printf("\nNaopa ko:\n");
while(nova!=NUL L)
{
printf("\n%d",n ova->element);
nova=nova->next;
}
system("pause") ;
return 0;
}
a linked list. (Print the list) From that list it forms another list in a
way if the arrangement of elements in the first one was 5 8 3 1 in the new
one it should be
1 3 8 5 (print the new list)
where's the error in my code and why?
#include<stdio. h>
#include<stdlib .h>
#include<time.h >
struct lista{
int element;
struct lista *next;
}*pocetak;
main()
{
struct lista *q, *nova;
int i;
pocetak=NULL;
srand(time(NULL ));
for(i=0;i<5;i++ )
{
q=(struct lista*) malloc(sizeof(s truct lista));
q->element=rand() %100;
pocetak=q;
q->next=NULL;
}
q=pocetak;
printf("Nasumic ni brojevi:\n");
while(q!=NULL)
{
printf("\n%d",q->element);
q=q->next;
}
q=pocetak;
for(i=0;i<5;i++ )
{
nova=(struct lista*) malloc(sizeof(s truct lista));
nova->element=poceta k+(5-i)*(sizeof(stru ct lista));
pocetak=nova;
nova->next=NULL;
}
nova=pocetak;
printf("\nNaopa ko:\n");
while(nova!=NUL L)
{
printf("\n%d",n ova->element);
nova=nova->next;
}
system("pause") ;
return 0;
}
Comment