The following code tries to add a node to the beginning of a linked list.
Can anyone explain to me why my insert
function doesn't work? The program passed in a pointer nodePtr
and modified it inside insert
function. Since the program declared nodePtr
in the main, the pointer should change but it didn't.
/* Copyright 2017 linkedList.c */
#include <stdlib.h>
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
// Linked list structure
typedef struct llnode {
int val;
struct llnode* node;
} llnode;
// Fill a linked list and create a pointer that points to it
void create(llnode* head);
// Print all values in a linked list
void printVal(llnode* head);
// Delete an entire linkedList
void delete(llnode* head);
// Insert a node (val) at the beginning of a linked List
void insert(llnode* head, int n);
int main(void) {
// Create a pointer that points to a linked list
llnode* nodePtr = malloc(sizeof(llnode));
if (nodePtr == NULL)
printf("Not enough space can be allocated.\n");
create(nodePtr);
printVal(nodePtr);
insert(nodePtr, 3);
printf("New List: \n");
printVal(nodePtr);
delete(nodePtr);
return 0;
}
/**
* Add integers to an existed linkedList and create a new linkedList
*/
void create(llnode* head) {
// temp node
llnode* temp = head;
// Fill in a linkedList
while (true) {
// Input a number
printf("Input a number: ");
char* line = GetString();
int n;
// if not a digit, terminate the head and break
if (sscanf(line, " %d ", &n) == 0) {
free(line);
temp -> node = NULL;
break;
}
// if a digit, allocate space and check if NULL
// Assign value and node
free(line);
temp -> val = n;
temp -> node = malloc(sizeof(llnode));
if (temp -> node == NULL)
printf("Not enough space can be allocated.\n");
// Update temp to point to next node
temp = temp -> node;
}
}
/**
* Print all interger value in a linkedList in order
*/
void printVal(llnode* head) {
llnode* temp = head;
int i = 0; // The ith element of a linked head
// Check if next node is NULL
while (temp -> node != NULL) {
printf("The %d integer is %d.\n", i, temp->val);
// Update i and node
temp = temp -> node;
i++;
}
}
/**
* Delete an entire linkedList
*/
void delete(llnode* head) {
// temp is used for traversing
llnode* temp = head;
/* Nullify every node */
while (temp != NULL) {
temp = temp -> node; // Traverse to next node
free(head); // Free memory of previous node
head = temp; // Update list to next node
}
}
/**
* Insert a node with integer n at the beginning of a linked list
*/
void insert(llnode* head, int n) {
/* Declare another node for insertion */
llnode* temp = malloc(sizeof(llnode));
if (temp == NULL)
printf("Not enough space can be allocated.\n");
/* Assign values */
temp -> val = n;
// temp > node needs to store the address of head's first node
temp -> node = head;
/* Reassign head */
head = temp;
}