uuable to insert item to linked list

This is a discussion on uuable to insert item to linked list within the C Programming forums, part of the General Programming Boards category; hi I was doing a program for inserting a item in to the linked list..i have to insert the item ...

  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
    32,845
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 11:21 PM

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