Thread: Insert in Linked List ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    11

    Insert in Linked List ...

    Hi;
    Here is the function which insert the node according to dictionalry in the Circurlar list, but I couldn't find where is wrong. It is not working.
    Anyone has any idea????

    void insertnode( struct node *new )
    {
    struct node *cur, *prev;

    while( cur != end )
    {
    if(strcmp( cur->name, new->name) < 0)
    {
    prev=cur;
    cur = cur->next;
    } else
    break;
    }
    if(head == NULL)
    { head = new;
    end = new;
    end->next = head; }
    else {

    if(head == end)
    {
    if(head == end)
    {
    if(strcmp(cur->name,new->name)<0)
    new->next=head;
    head->next=new;
    end = new;
    } else { new->next = head;
    head->next=new;
    head=new;}

    if ( cur == head) {
    new->next = head;
    head = new; }
    else if (cur == end) {
    end->next = new;
    new->next = head;
    }
    else {
    prev->next = new;
    new->next = cur;
    }
    }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void insertnode( struct node *new ) 
    { 
    struct node *cur, *prev; 
    
    while( cur != end )
    Here you have a problem. Two of them. You shouldn't use the word "new", because while it isn't a keyword in C, it is in C++, and if you ever go to convert this program, you'll get tons of errors. Not really a problem here, but just something to keep in minde.

    Second, you use "cur" and "end" without ever giving them values.

    Code:
    if(head == end) 
    { 
    if(head == end) 
    { 
    if(strcmp(cur->name,new->name)<0)
    You've just tested "head==end", so why do it twice?

    Third: this is way to complex.

    Given a circular list, do this:

    List *myList;

    Assuming "myList" is holding some point in a circular list, do this:
    Code:
    insert( List *toInsert )
    {
        List *cur, *end;
    
        cur = end = myList;
        do {
            if ( strcmp( cur->data, toInsert->data ) < 0  )
            {
                cur = cur->next;
            }
            else
                break;
        } while ( cur != end  );
    
        /*insert after the end*/
        if ( cur == end )
        {
            toInsert->next = myList;
            end->next = toInsert;
        }
        /*insert after cur*/
        else
        {
            toInsert->next = cur->next;
            cur->next = toInsert;
        }
    }
    Your code is WAY to complicated.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User goran's Avatar
    Join Date
    Sep 2001
    Posts
    68
    yescha, here is a modified version. i assumed that you wish to arrange your linked list in ascending order based on nodename.

    typedef struct tag{
    char name[50];
    ...
    ...
    struct tag *next;
    }node;

    node *newnode()
    {
    node *np;
    np = (node *)malloc(sizeof(node));
    if(np)
    {
    // take whatever you want from input and
    // populate node np;
    np->next = NULL;
    }
    else
    fprintf( stdout, "Error\n");
    return np;
    }

    node *insertnode(node **head)
    {
    node *np;
    node *curr = *head;
    node *prev = NULL;

    np=newnode();

    if(np)
    {
    for(; strcmp(curr->name, np->name) < 0; curr=curr->next)
    prev=curr;

    np->next=curr;
    if(prev)
    prev->next=np;
    else
    *head=np;
    }
    else
    fprintf( stdout, " allocation of new node failed.\n");
    return np;
    }


    main()
    {
    node *head=NULL;
    ...
    ...
    insertnode(&head);
    ...
    ...
    }
    Last edited by goran; 11-28-2001 at 02:31 AM.
    I don't wait for the future; it comes soon enough.

  4. #4
    Registered User goran's Avatar
    Join Date
    Sep 2001
    Posts
    68
    yup, the indentation really sucks. but i just can't make it proper. no matter how many spaces you put for indentation, they get deleted while submitting the reply.

    I don't wait for the future; it comes soon enough.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use "code" tags. It'll preserve indentation. If you're familiar with HTML tags, they work the same, but use [ ] instead of < >. Simply do:

    [ code ]
    ...Your code here...
    [ /code ]

    Without the extra spaces. ie: [likethis] [/likethis]

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pleas take a look & give a critique
    By sh3rpa in forum C++ Programming
    Replies: 14
    Last Post: 10-19-2007, 10:01 PM
  2. singly linked to doubly linked
    By jsbeckton in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 07:47 PM
  3. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  4. 1st Class LIST ADT
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 11-09-2001, 07:29 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM