program - help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nikola

    #1

    program - help

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




  • Thomas Matthews

    #2
    Re: program - help

    Nikola wrote:[color=blue]
    > 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;[/color]
    For shorthand notation:
    typedef struct lista Node;

    [color=blue]
    >
    > main()[/color]
    The main function returns int. Always.
    int main(void)

    [color=blue]
    > {
    > struct lista *q, *nova;[/color]
    struct lista *q;
    struct lista *nova;

    [color=blue]
    > int i;
    > pocetak=NULL;[/color]
    I'll assume that pocetak is the head of the list.
    [color=blue]
    > 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;[/color]
    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.

    [color=blue]
    > }[/color]

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

    /* Always check for an error from malloc. */
    if (q == NULL)
    {
    Process_Memory_ Allocation_Erro r();
    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:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book

    Comment

    • Barry Schwarz

      #3
      Re: program - help

      On Mon, 14 Jun 2004 18:37:54 +0200, "Nikola" <azmiric@inet.h r> wrote:
      [color=blue]
      >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?[/color]

      Is this the same program you posted 11 minutes ago?


      <<Remove the del for email>>

      Comment

      • Nikola

        #4
        Re: program - help

        it is
        "Barry Schwarz" <schwarzb@deloz .net> je napisao u poruci interesnoj
        grupi:calmuo$vc r$1@216.39.135. 80...[color=blue]
        > On Mon, 14 Jun 2004 18:37:54 +0200, "Nikola" <azmiric@inet.h r> wrote:
        >[color=green]
        > >I need to write a program that generates 5 random numbers and puts them[/color][/color]
        into[color=blue][color=green]
        > >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[/color][/color]
        new[color=blue][color=green]
        > >one it should be
        > >1 3 8 5 (print the new list)
        > >
        > >where's the error in my code and why?[/color]
        >
        > Is this the same program you posted 11 minutes ago?
        >
        >
        > <<Remove the del for email>>[/color]


        Comment

        • Peter Shaggy Haywood

          #5
          Re: program - help

          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!]
          [color=blue]
          >"Barry Schwarz" <schwarzb@deloz .net> je napisao u poruci interesnoj
          >grupi:calmuo$v cr$1@216.39.135 .80...[color=green]
          >>
          >> Is this the same program you posted 11 minutes ago?[/color]
          >
          >it is[/color]

          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!


          "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 "technicall y correct" English; but since when was rock & roll "technicall y correct"?

          Comment

          • Nikola

            #6
            Re: program - help

            > Then why did you post it yet again? You've already posted it several[color=blue]
            > 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.[/color]

            Sorry, my bad .



            Comment

            Working...