Reputation: 5
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.
Upvotes: 0
Views: 187
Reputation: 311068
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.
Upvotes: 0
Reputation: 224387
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;
Upvotes: 1