Thread: linked list insert node between two nodes.

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    6

    linked list insert node between two nodes.

    Hey I have a problem with inserting a node between two nodes.
    Can someone have a look on it.




    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
     struct listelem
    {
    
       int nbr;
    
    
       struct listelem *next;
    
    };
    
    typedef struct listelem listElem;
    
    
    void instructions(void);
    
    listElem *laenka_in(listElem *lista , int tal );
    void printList(listElem *lista);
    
    
    void main(){
    
        listElem *first = NULL;
    
        unsigned int choice;
        int nbr;
    
        instructions();
        printf("%s","?");
        scanf("%d",&choice);
    
    
        while(choice!=3){
    
            switch (choice){
    
                case 1:
                    printf("Nbr? \n");
                    scanf("%d",&nbr);
                    first = laenka_in(first, nbr);
    
                    break;
                case 2:
                    printList(first);
                    fflush(stdin);
                    break;
                case 3:
    
                    break;
                default:
                    puts("Invalid choice");
                    instructions();
                    break;
            }
    
    
        instructions();
        printf("%s","?");
        fflush(stdin);
        scanf("%d",&choice);
    
        }
    
    }
    
    void instructions(){
    
        puts("Meny \n"
             "1: put in a nbr in list\n"
             "2: Search for a nbr\n"
             "3: Quite the program");
    
    }
    
    listElem *laenka_in(listElem *lista , int tal ){
    
        listElem *currentPtr;
        listElem *newPtr;
        listElem *previousPtr;
        int unique =1;
    
        newPtr = malloc(sizeof(listElem));
    
        if(newPtr != NULL){
            newPtr ->nbr = tal;
            newPtr ->next =NULL;
    
            previousPtr = NULL;
            currentPtr = lista;
    
            while(currentPtr != NULL && tal > currentPtr->nbr){
                previousPtr = currentPtr;
                currentPtr = currentPtr->next;
            }
    
            if(previousPtr == NULL ){
                puts("First in line \n");
                newPtr -> next = currentPtr;
                currentPtr = newPtr;
    
            }
            else { // THIS IS HERE I HAVE PROBLEMS 
                puts("Last in line \n");
                previousPtr ->next = newPtr;
                newPtr->next = currentPtr;
    
            }
        }
        else{
            puts("Not enough memory\n");
        }
    
        return currentPtr;
    
    
    
    }
    
    void printList(listElem *lista){
    
    
        if(lista == NULL ){
            puts("List is empty!");
        }
            else{
                puts("The list is:\n");
    
                while(lista != NULL){
                    printf("%d\n",lista->nbr);
                    lista = lista->next;
    
                }
    
            }
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I don't think you have your first in line case down pat, which means you're probably not inserting any elements to your list at all.

    When you insert at the head of the list, you need to set the list-head-pointer thing to point to the new node, so that your calling function knows that the list now starts at the new place. This means your function needs to modify "first", so it has to be passed by reference (ie passed by pointer).

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    6
    When I for ex insert the same nbr several times in a row it will go in to the first if:

    Code:
       if(previousPtr == NULL ){
                puts("First in line \n");
                newPtr -> next = currentPtr;
                currentPtr = newPtr;
            }
    And then print it by using meny nbr 2 (printList) it will print out the nbr, but if put in 1,1,1,2 so it will go in to else it doesnt work because it drops the pointers.

    Code:
           else {
                puts("Last in line \n");
    
                previousPtr ->next = newPtr;
                newPtr->next = currentPtr;
    
            }

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,649
    Quote Originally Posted by tabstop View Post
    I don't think you have your first in line case down pat, which means you're probably not inserting any elements to your list at all.

    When you insert at the head of the list, you need to set the list-head-pointer thing to point to the new node, so that your calling function knows that the list now starts at the new place. This means your function needs to modify "first", so it has to be passed by reference (ie passed by pointer).
    In this case you need to change the function to listElem *laenka_in(listElem **lista , int tal ), and then use *lista instead of lista in the insert function. For first in line, you'll need to set *lista = newPtr;

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    OH hey, you have a return statement! Okay, so that's how the first-in-line is working; apologies for not seeing that at the bottom.

    However, since you need to be returning the top of the list, you can't just return currentPtr. That works for inserting at the front, because currentPtr is the front in that case; but if you're inserting elsewhere currentPtr isn't the front of the list. In those situations you'll have to return the original lista. (NOTE: You are allowed (at least by C; perhaps not by your instructor) to have two different return statements in a function, so you can return different things from different places.)
    Last edited by tabstop; 10-25-2013 at 09:42 AM. Reason: NOTE

  6. #6
    Registered User
    Join Date
    Oct 2013
    Posts
    6
    Thanks that helped me a lot, I was fixing it by having to reurn statments maybe not so nice but I have no Idea how to fix it. Thanks!

  7. #7
    Registered User
    Join Date
    Oct 2013
    Posts
    6

    Another question list should only contain unique nbrs.

    As the title says,
    How can I fix it that nbr in the list is unique that the user cant type in the same nbr twice.

  8. #8
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    You can't prevent them from typing in the same number twice, but you can refuse to insert the same number into your list a second time. When the user enters a number, search for it in the list. If it's there, don't insert it (maybe also inform the user "number already exists, please enter a different number"). If you don't find it, insert the number.

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    1,649
    You don't need to use two returns, just fix the else that handles the case when it's in the middle or the end of the list to set currentPtr back to lista:

    Code:
            else {  // this is where ...
                puts("not first in line \n");
                previousPtr ->next = newPtr;
                newPtr->next = currentPtr;
                currentPtr = lista;
            }
    Although not a bug, you might consider putting the error handling code closer to where the error occurred, and not indenting the main line part of the code. Do a web search for "arrow anti pattern" to see examples of a coding style to be avoided.

    Code:
    listElem *laenka_in(listElem *lista , int tal ){
    /* ... */
        newPtr = malloc(sizeof(listElem));
        if(newPtr == NULL){
            puts("Not enough memory\n");
            return NULL;
        }
    
        newPtr ->nbr = tal;
    /* ... */
    Code:
    int main(){
    /* ... */
                    first = laenka_in(first, nbr);
                    if(first == NULL)
                        return 0;
    /* ... */
    Last edited by rcgldr; 10-25-2013 at 04:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 10-31-2012, 02:31 AM
  2. Insert a node into the end of a linked list
    By aw1742 in forum C Programming
    Replies: 4
    Last Post: 10-12-2011, 03:50 PM
  3. Replies: 0
    Last Post: 09-16-2008, 05:04 AM
  4. Insert node in a linked list.
    By antonis in forum C Programming
    Replies: 2
    Last Post: 10-22-2005, 02:30 PM
  5. Replies: 4
    Last Post: 09-10-2005, 01:07 PM