Understanding and implementing a Linked List in C and Java
Why Stack Overflow recommends it
Stack Overflow users cite it as the tutorial they followed when learning linked lists in C, and then ask about pointer problems in code adapted from it.
Recommended by 1 people across 1 different months, 2020 to 2022.
What it is
An 18-minute programming tutorial by Jacob Sorber, a Clemson University professor, introducing linked lists as a core data structure. He explains the concepts and walks through code examples in both C and Java, covering printing, creating nodes and inserting at the head.
What people said
Recommends hugocar2005 · 2021-05 · Stack Overflow · on “error: invalid conversion from 'void*' to 'node_t*'”
Whenever I try running the code below, I get the following error on line 34: error: invalid conversion from 'void*' to 'node_t*' I'm new to pointers and linked lists, and I was watching a tutorial on how to use them (this one: Understanding and implementing a Linked List in C and Java [this video] ), and I copied the most of it exactly like shown in the video, but it still doesn't work. How can I fix it? #include <stdio.h> #include <time.h> #include <stdlib.h> #include <windows.h> #include <conio.h> #include <math.h> #include <iostream> using namespace std; typedef struct node { int value; struct node *next; } node_t; void printlist(node_t *head){ node_t *temporary = head; while(temporary != NULL
Cites Sesion S · 2020-04 · Stack Overflow · on “Double pointer in linked list”
I almost figured out this code, but there are two details I can't figure out. I found this code on YouTube. source: [this video] #include <stdio.h> #include <stdlib.h> typedef struct node{ int vaule; struct node *next; }node_t; void printlist(node_t *head) { node_t *temp = head; while(temp != NULL) { printf("%d - ", temp->vaule); temp = temp->next; } printf("\n"); } node_t *create_node(int var) { node_t *res = malloc(sizeof(node_t)); res->vaule = var; res->next = NULL; return res; } node_t *insert_at_head(node_t **head, node_t *node_to_insert) { node_to_insert->next = *head; *head = node_to_insert; return node_to_insert; } void find_node() int main() { node_t *tmp; node_t *head = NULL; for(int i
Cites Billy Smith · 2021-03 · Stack Overflow · on “Why do I need to use a pointer to a pointer when adding a new node at head of linked list in a function?”
I have a linked list implemented in C as so: struct node { int value; struct node* next; }; typedef struct node node_t; node_t* new_node(int value){ node_t* n = malloc(sizeof(node_t)); n->value = value; n->next = NULL; return n; } Why does this work: node_t* insert_at_head(node_t **head, node_t *node_to_insert){ node_to_insert->next = *head; *head = node_to_insert; return node_to_insert; } but this doesn't?: node_t* insert_at_head(node_t *head, node_t *node_to_insert){ node_to_insert->next = head; head = node_to_insert; return node_to_insert; } This is how it is implemented in main: node_t* head = NULL; node_t* tmp; for(int i = 0; i < 25; i++){ tmp = new_node(i); insert_at_head(&head, tmp);
Cites stochasticlover1 · 2022-12 · Stack Overflow · on “How to pass the head of a Linked List ot a function?”
I'm trying to code a Linked List in C. I know this is very simple, but I'm doing this as an exercise to learn pointers in C. I'm following this video, [this video] specifically at 8:05 where he creates this printlist() function and passes through the head of the list. I'm trying to do the same thing: My code is below: #include <stdio.h> typedef struct node_t{ int value; struct node_t *next; }node_t; void print_ll(node_t *head) { node_t *tmp = head; printf("first node val: %d and next pointer %p\n", tmp->value, tmp->next); //prints 5 and some non-NULL address. /* while (tmp != NULL) { printf("%d - ", tmp->value); tmp = tmp->next; } */ printf("\n"); } node_t *init_ll(int value) { node_t new_node; n