program - help

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;
}
 
T

Thomas Matthews

Nikola said:
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;
For shorthand notation:
typedef struct lista Node;

The main function returns int. Always.
int main(void)

{
struct lista *q, *nova;
struct lista *q;
struct lista *nova;

int i;
pocetak=NULL;
I'll assume that pocetak is the head of the list.
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;
After first iteration:
+---+
q-> |n1 | -> NULL
+---+

+---+
pocetak -> |n1 | -> NULL
+---+

After 2nd iteration:
+---+
q-> |n2 | -> NULL
+---+

+---+
pocetak -> |n2 | -> NULL
+---+

Notice how the first node is no longer in the list.
This known as a memory leak.


Try this:
for(i=0;i<5;i++)
{
/* Allocate a new node. Casts are not required. */
q = malloc(sizeof(struct lista));

/* Always check for an error from malloc. */
if (q == NULL)
{
Process_Memory_Allocation_Error();
return EXIT_FAILURE;
}

/* Create data value for node. */
q->element=rand()%100;

/* Insert new node into list. */
q->next = pocetak; /* New node points to first node */
pocetak = q; /* Beginning of list points to new node */
}

After first iteration:
pocetak -> NULL

+---+
q-> |n1 |
+---+

+---+
q -> |n1 | -> NULL (pocetak)
+---+

+---+
pocetak -> |n1 | -> NULL
+---+

After 2nd iteration:
+---+
q-> |n2 |
+---+

+---+ +---+
q-> |n2 | -> {pocetak ->} |n1 | -> NULL
+---+ +---+

+---+ +---+
pocetak -> |n2 | -> |n1 | -> NULL
+---+ +---+

Apply this to your creating your other list.

If you want to add new nodes to the end of the list, you
will either have to traverse to the end of the list and
append to the last node or maintain a pointer to the last
node that was inserted.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
B

Barry Schwarz

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?

Is this the same program you posted 11 minutes ago?


<<Remove the del for email>>
 
P

Peter Shaggy Haywood

Groovy hepcat Nikola was jivin' on Tue, 15 Jun 2004 07:15:19 +0200 in
comp.lang.c.
Re: program - help's a cool scene! Dig it!

[Evil top posting corrected. DO NOT TOP POST PLEASE!]

Then why did you post it yet again? You've already posted it several
times, with very minor variations. Don't do that. And don't begin a
new thread to discuss the same thing you discussed in an existing
thread.

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
N

Nikola

Then why did you post it yet again? You've already posted it several
times, with very minor variations. Don't do that. And don't begin a
new thread to discuss the same thing you discussed in an existing
thread.

Sorry, my bad .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,798
Messages
2,569,649
Members
45,382
Latest member
tallzebra

Latest Threads

Top