problme with ordered linked list

This is a discussion on problme with ordered linked list within the C Programming forums, part of the General Programming Boards category; Hii all, I am new in advance programming. I have a problme of generating 1,000,000 random number using rand() and ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    problme with ordered linked list

    Hii all,
    I am new in advance programming. I have a problme of generating 1,000,000 random number using rand() and inserting only 1 to 49 numbers in ordered listed list.
    I m getting segmentation fault error.
    When I put comment at insertevent(current,event) function, code runs fine. So problme is with insertevent function defination only.

    Here is my code..
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    typedef struct olnode olnode;
    struct olnode{
          int eventnr;
          int eventfq;
          struct olnode * next;
    };
    olnode * getnode();   // these prototypes not necessary to define
    int nextevent();
    int main(void){
        olnode * head;
        olnode * current;
        int i,j, event;
        head=getnode();
        current=head;
                                                         // insert item into HEAD
        srand(time(NULL));
        do{
               if(event <=49 && event >=0)
                  j=1;
               else
                  j=0;
        }while(j==0);
        current->eventnr=event;         //inserting
           for(i=0;i<=1000000;i++)      // generate 1000000 numbers
       {
            event=nextevent();      // generate nextevent .... CALLING //  NEXTEVENT()
            if(event<=49 && event>=0)// make sure that next event is between 0..49
               {
                                    //insert into list
                     temp=insertevent(current,event);
     }
       }
         current->next=NULL; 
      return(0);
    }
       // get new node
    olnode * getnode()
    {
      olnode *NODE;
      NODE=malloc(sizeof(olnode));
      if(NODE==NULL){
        printf("\n malloc() failed \n");
        exit(0);
      }
     return NODE;
    }
              
                                    //generate new event
    int nextevent()
    {
       int temp;
       temp=rand();
                                    
       return temp;
    }
                                    //insertevent into list
      
    int insertevent(olnode *current, int event)
    {
        current = getnode();
        current=current->next;
     
        current->eventnr=event;
              
        return 1;
    }
    -----------------------------------------------------
    Apart from this I have to keep the order of number. Means myoutput should look like.
    evennr eventfq (declared in struct)
    32 76 | 23 34 | 9 56
    (Means 32 number repeated 76 times.
    23 number repeated 34 times and
    9 number repeated 56 times)
    I have only 2 days to finish the code. I hope I would get help from you guys.
    Thanks in advance.

  2. #2
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by palku
    Code:
        do{
               if(event <=49 && event >=0)
                  j=1;
               else
                  j=0;
        }while(j==0);
    This loop does the same thing as the single line j = 1;, does it not? Also, event has not yet been initialised. You must have some confusion here.

    Quote Originally Posted by palku
    Code:
        current->eventnr=event;         //inserting
    At this point in time, event has not yet been initialised. So you are filling eventnr with junk bits.

    Quote Originally Posted by palku
    Code:
                                    //insertevent into list
      
    int insertevent(olnode *current, int event)
    {
        current = getnode();
        current=current->next;
     
        current->eventnr=event;
              
        return 1;
    }
    Why do you have current as an argument to the function if you overwrite the function's copy of the pointer with the location of a freshly allocated node? Then current contains the memory address of an uninitialised, but allocated space in memory. Then you try changing the value of current to equal current->next. But current->next, a value in an uninitialised node, is an arbitrary group of junk bits. Then you try treating these junk bits as the memory address of a node by assigning to a value in this garbage memory location. This is where your segmentation fault happens.

    But that is not the only problem. Major portions of your program make no sense at all. Think about what you are doing and make sure you understand completely what is going on.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    8
    Thanks a lot. I know there is lot of junk values. I m working on it. I wanted to have idea and thanks a lot for your suggestion.

    I really appreciate it.
    I ll try my best and post again if there is any problem.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > for(i=0;i<=1000000;i++) // generate 1000000 numbers
    This is 1000001 loops

    Also, fix your indentation (or fix your editor) when you post - that was a horrid jumbled mess which no one wants to wade through.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Sep 2005
    Posts
    8

    thanks for replying for order Linked list problme

    // Hii all thanks a lot for helping me. spl. Rashakil Fol and Salem.
    I m able to insert event into list. Now problme is when I try to print values in link list it gives me error..
    ..segmentation fault..

    HERE IS MY REVISED CODE

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    typedef struct olnode olnode;
    
    struct olnode{
            int eventnr;
            int eventfq;
            struct olnode * next;
    };
    
    olnode *list;
    olnode *ptr;
    olnode onode;
       
    olnode * getnode();            
    int nextevent();                
    int insertevent(olnode *current, int);
    void displayevent(olnode *current);
       
    int main(void){
                                    
       olnode * NODE;
       olnode * head;                 // Header node
       olnode * current;              // Current node
       int i,event=0,count,temp=0,j=0;
    
       head=getnode();              // Memory is allocated for HEAD
       current=head;                  // Current pointer pointing to HEAD
       srand(time(NULL));           // Seed the random number
                                                // Create an event for head
       do{
            event=rand();
            if(event <=49 && event >=0)
              j=1;
            else
              j=0;
       }while(j==0);
       
       current->eventnr=event;      // assign event to the HEAD OF CURRENT
       
       for(i=0;i<10000;i++)         // generate 1000000 numbers
       {    
            event=nextevent();      // generate nextevent 
            if(event<=49 && event>=0)// next event is between 0..49
               {
                    printf("\nEvent is : %d.. and ",event);
                    temp=insertevent(current,event);
                    if(temp == 1)                                
                            printf("succcess");
               }  
       }
       current->next=NULL;          //end of list
            
       current=head;                //get the first node in order to DISPLAY
       printf("\nfirst number in list is %d   ..", current->eventnr);
    
        
    /************************* here my segmentation fault occurs *********/
       
       current=current->next;
       printf("\nsecond number inlist is %d",current->eventnr);
            
       printf("\n");
       return(0);
    }
       
                                    // allocate memory for newnode
    olnode * getnode()
    {   
      olnode *NODE;
      NODE=malloc(sizeof(olnode));   //memory allocation   
      if(NODE==NULL){
        printf("\n malloc() failed \n");
      exit(0);
      }
    
    int nextevent() 
    {
       int temp;
       temp=rand();                 
                                    
       return temp;   
    }   
                                    //insertevent into list
      
       
    int insertevent(olnode *current, int event)
    {
        current->next = getnode();
        current = current->next;
     
        current->eventnr=event;
        printf(" .. the value in linklist is : %d ", current->eventnr);
                                    
        return 1;   
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    int nextevent() 
    {
       int temp;
       temp=rand();                 
                                    
       return temp;   
    }
    ==>
    Code:
    int nextevent() {
        return rand();
    }
    free() what you malloc().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked list question
    By mikeman in forum C Programming
    Replies: 1
    Last Post: 11-30-2008, 01:56 PM
  2. Duplicating value of pointer to linked list
    By zephyrcat in forum C Programming
    Replies: 14
    Last Post: 01-22-2008, 03:19 PM
  3. ordered linked list
    By redmondtab in forum C Programming
    Replies: 48
    Last Post: 10-22-2006, 07:09 AM
  4. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 11:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21