0

I am writing a program to become familiar with linked lists and when attempting to insert a new link at the end of the list the value is seemingly not stored. I assume it has to do with a local variable not saving to the variable passed into the function.

The function in question:

int insertAtTail(int intBeingAdded, List *userList){
    while(userList->next != NULL){
        userList = userList->next;
    }
    List *newLink = (List *)malloc(sizeof(List));
    userList->next = newLink;
    newLink->next = NULL;
    newLink->value = intBeingAdded;
    return 1;
}

The entirety of the file:

#include "stdio.h"
#include "stdlib.h"

typedef struct list{
    int value;
    struct list * next;
} List;

List * initIntegerList(void);
int insertAtHead(int intBeingAdded, List *userList);
int insertAtTail(int intBeingAdded, List *userList);
void printList(List *userList);

int main(void){
    List *myList;
    myList = initIntegerList();
    insertAtHead(2, myList);
    insertAtHead(1, myList);
    insertAtTail(6, myList);
    printList(myList);
    freeList(myList);
}

List * initIntegerList(void){
    List * listPointer = (List *)malloc(sizeof(List));
    if(listPointer != NULL){
        listPointer->next = NULL;
        return listPointer;
    }else{
        printf("Memory not available for allocation\n");
        return listPointer;
    }
}

int insertAtHead(int intBeingAdded, List *userList){
    List *previousHead = (List *)malloc(sizeof(List));
    if(previousHead != NULL){
        previousHead->value = intBeingAdded;
        previousHead->next = userList->next;
        userList->next = previousHead;
        return 1;
    }
    return 0;
}

int insertAtTail(int intBeingAdded, List *userList){
    while(userList->next != NULL){
        userList = userList->next;
    }
    List *newLink = (List *)malloc(sizeof(List));
    userList->next = newLink;
    newLink->next = NULL;
    newLink->value = intBeingAdded;
    return 1;
}

void printList(List *userList){
    printf("Values in list: ");
    List *currentLink = userList;
    while(currentLink->next != NULL){
        printf(" %d", currentLink->value);
        currentLink = currentLink->next;
    }
    printf("\n");
}

The output I am seeing is only 0,1,2 are stored and the 6 is not making it.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • [Should we cast the result of malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc)? I think you are seeing output `Values in list: 0 1 2` right? You seem never assign a value to the first element in the list allocated `initIntegerList`. What is the value of `listPointer->value`? `#include`s should be in `<` `>`. – KamilCuk Mar 10 '20 at 15:39
  • Using a dummy entry at the start of the list is more trouble than it's worth, especially since some of your code does not take the dummy entry into account. It is better to represent an empty list (or at least an empty singly linked list) with just a null pointer. Then your `initIntegerList()` would be quite redundant as all it would need to do is return a null pointer value. C only has "pass by value" but you can emulate the effect of "pass by reference" by passing a pointer to the object and deref it. So to emulate passing a pointer value by reference, you can pass a pointer to the pointer. – Ian Abbott Mar 10 '20 at 16:06

2 Answers2

1

When you print the list:

while(currentLink->next != NULL){

You stop when you reach the last node and don't print its contents. You instead want:

while(currentLink != NULL){

Also, your list has a dummy entry at the start of the list. When you print the list, you want to skip that one.

List *currentLink = userList->next;
dbush
  • 205,898
  • 23
  • 218
  • 273
0

Your list is built such a way that the head node does not contain a value. It is a dummy node. On the other hand, the last node always contains the data member next equal to NULL.

So this condition in the function printList

while(currentLink->next != NULL){

does not output the value of the last node.

The function can be written like

void printList( const List *userList )
{
    printf("Values in list: ");

    if ( userList != NULL )
    {
        for ( const List *currentLink = userList->next; 
              currentLink != NULL;
              currentLink = currentLink->next )
        {
            printf( " %d", currentLink->value );
        }
    }

    printf("\n");
}

Pay attention to that it is a bad idea to have a dummy node as a head node. The head node should be just set to NULL.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335