Thread: Help me, Friends and Gurus ????

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

    Help me, Friends and Gurus ????

    Hi; Friends;

    I solved most of the work. But it's now having problem to insert in the middle.

    Any can suggest me where to correct, how to correct, and where I did mistake.

    Here is my that insertnode function:

    ....Code ..........
    struct node *head = (struct node *) NULL;
    struct node *end = struct node *) NULL;
    ......
    void insertnode(struct node *insert)
    {
    struct node *curr, *prev;
    if(head == NULL)
    {
    head = insert;
    insert->next=head;
    end=insert;
    }
    else if(head==end)
    {
    if(strcmp(head->name,insert->name) < 0)
    {
    insert->next=head;
    head->next=insert;
    end=insert;
    }
    else {
    head=insert;
    insert->next=end;
    end->next=head;
    }
    }
    /* inserting in the middle part is here....*/
    /*...........Problem here ............*/
    /* .........segmentation error....... */

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

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    ---A COMPUTER ANIMATED BLOCK DESIGN---

    FIRST_POINTER---------------------------------NEXT_POINTER

    Okay, so you want to insert a node into the linked list? You will have to make it so that FIRST_POINTER's next * would be the insertion pointer. And the insertion pointer's next * would have to be NEXT_POINTER. So, it would end up like:

    FIRST_POINTER------------INSERTION_POINTER-------NEXT_POINTER

    Now, put that into code and then post that if it is wrong.

    --Garfield
    1978 Silver Anniversary Corvette

  3. #3
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Okay, I need a little more "error info". Did you try to compile this code, or is this just "thinking code"? If you already tried to compile it, what were the errors? Were there any Runtime Errors? Because I think I may see your problem.

    Also, you also know that strcmp returns 0 if the strings are equal and nonzero if the strings aren't equal, right? The reason I ask this is because this line doesn't look great:

    while(strcmp(curr->name,insert->name) < 0)

    are you trying to continue the while loop if curr->name and insert->name are not equal? Because, if you wanted to see if they are equal, you would have to do this:

    while(strcmp(curr->name,insert->name) == 0)

    strcmp is just screwy like that. Try changing this stuff and tell me if this works. Worst comes to worst and I'll just write up some code, compare it to yours, and tell you the error. Nothing to worry about.

    --Garfield
    1978 Silver Anniversary Corvette

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    I think you have problems when you are inserting to the end of the list. The lines

    while(strcmp(curr->name,insert->name) < 0) {
    curr = curr->next;
    if(curr==head)
    break;
    }
    if(curr=head){ // /What is this? should be if (curr==head)
    insert->next=head;
    head=insert;
    }

    seems to be buggy. What if you go to the end of the list? This seems to be a sorted circular queue so at the end you go to the beginning. What if you add "Bob" and then "Charlie".
    Bob will be Ok but Charlie will go to the beginning of list.
    try this

    while(strcmp(curr->name,insert->name) < 0) {
    curr = curr->next;
    if(curr==head)
    break;
    }
    if(curr==head){
    insert->next=head;
    end=insert;
    }
    Last edited by ozgulker; 12-03-2001 at 04:45 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked list, just small problem remained
    By yescha in forum C Programming
    Replies: 0
    Last Post: 12-02-2001, 01:31 AM