N
Nikola
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(struct lista));
q->element=rand()%100;
pocetak=q;
q->next=NULL;
}
q=pocetak;
printf("Nasumicni 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(struct lista));
nova->element=pocetak+(5-i)*(sizeof(struct lista));
pocetak=nova;
nova->next=NULL;
}
nova=pocetak;
printf("\nNaopako:\n");
while(nova!=NULL)
{
printf("\n%d",nova->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(struct lista));
q->element=rand()%100;
pocetak=q;
q->next=NULL;
}
q=pocetak;
printf("Nasumicni 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(struct lista));
nova->element=pocetak+(5-i)*(sizeof(struct lista));
pocetak=nova;
nova->next=NULL;
}
nova=pocetak;
printf("\nNaopako:\n");
while(nova!=NULL)
{
printf("\n%d",nova->element);
nova=nova->next;
}
system("pause");
return 0;
}