Thread: uuable to insert item to linked list

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    17

    uuable to insert item to linked list

    hi
    I was doing a program for inserting a item in to the linked list..i have to insert the item before the fist one..something is wrong with my ins_first() function
    just help me out
    Code:
    void ins_first(node *p)
    {
    int x;
    node *p1;
    p1=(node*)malloc(sizeof(node));
    printf("\nEnter the number to be inserted in the begining: ");
    scanf("%d",&x);
    p1->number=x;
    p1->next=p;
    p=p1;
    return ;
    }

  2. #2
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    On it's own it seems mainly fine, what errors are you getting?
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  3. #3
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Is "p" the current head of the linked list? Could you use less meaningful variable names?

    Code:
    void ins_first(node **p)
    {
        int x;
        node *p1;  
        p1 = (node*) malloc( sizeof(node) );
    
        /* Should check your malloc worked !! */
    
        printf("\nEnter the number to be inserted in the begining: ");
     
        scanf("%d",&x);
    
        /* Don't use scanf */
    
        p1->number = x;
        p1->next = (*p);
    
        *p = p1; 
    
        return;
    }

  4. #4
    Awesomefaceradcore bivhitscar's Avatar
    Join Date
    Apr 2006
    Location
    Melbourne, Australia
    Posts
    210
    Oh of course, I always get confused on this one.

    SKeane is correct, but you need to probably change your call to the function.

    Given your program looks like this:
    Code:
    node * head;
    
    ins_node( &head );

    The original problem is that while you were passing a pointer - you were actually passing a variable that holds the address as it's value - you were not passing the address itself. So, you need to pass the address, of the address - if that makes sense. :P
    it's ironic considerate rarity patron of love higher knowledge engulfs me...

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > p1=(node*)malloc(sizeof(node));
    Don't cast malloc in C
    See the FAQ

    As everyone has said, you need to return the modified result.

    I find this approach convenient in that it saves too many *'s
    Code:
    node *ins_first(node *p)
    {
      //...
      return p;
    }
    Then you do
    Code:
    list = ins_first( list );
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help! Placement of nodes in a Linked List
    By lostmyshadow in forum C Programming
    Replies: 6
    Last Post: 12-17-2007, 01:21 PM
  2. Recursion Revisited again, and again!
    By clegs in forum C++ Programming
    Replies: 93
    Last Post: 12-08-2007, 08:02 PM
  3. Replies: 5
    Last Post: 11-04-2006, 06:39 PM
  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, 10:21 PM