Skip to main content
Code Review

Return to Question

Fixed indentation
Source Link

In a TED talk Linus Torvalds made a point about "good taste" at approximately 14:10 in the interview. I read through his examples of "bad taste" and "good taste" and wanted to implement the same principle for a function that deletes the last node in a singly linked list. I also followed his coding style.

#include <stdio.h>
#include <stdlib.h>
/* linked list structure */
struct node {
 int data;
 struct node *next;
};
/* create new node */
struct node *new_node(int data, struct node *next)
{
 struct node *new = malloc(sizeof *new);
 if (!new) {
 fprintf(stderr, "Error: memory allocation failed\n");
 exit(EXIT_FAILURE);
 }
 new->data = data;
 new->next = next;
 return new;
}
/* delete last node */
void delete_last(struct node *head)
{
 if (head == NULL) {
 fprintf(stderr, "Error: linked list underflow\n");
 exit(EXIT_FAILURE);
 }
 struct node **cursor = &head;
 while ((*cursor)->next != NULL)
 cursor = &(*cursor)->next;
 free(*cursor);
 *cursor = NULL;
}

In a TED talk Linus Torvalds made a point about "good taste" at approximately 14:10 in the interview. I read through his examples of "bad taste" and "good taste" and wanted to implement the same principle for a function that deletes the last node in a singly linked list. I also followed his coding style.

#include <stdio.h>
#include <stdlib.h>
/* linked list structure */
struct node {
 int data;
 struct node *next;
};
/* create new node */
struct node *new_node(int data, struct node *next)
{
 struct node *new = malloc(sizeof *new);
 if (!new) {
 fprintf(stderr, "Error: memory allocation failed\n");
 exit(EXIT_FAILURE);
 }
 new->data = data;
 new->next = next;
 return new;
}
/* delete last node */
void delete_last(struct node *head)
{
 if (head == NULL) {
 fprintf(stderr, "Error: linked list underflow\n");
 exit(EXIT_FAILURE);
 }
 struct node **cursor = &head;
 while ((*cursor)->next != NULL)
 cursor = &(*cursor)->next;
 free(*cursor);
 *cursor = NULL;
}

In a TED talk Linus Torvalds made a point about "good taste" at approximately 14:10 in the interview. I read through his examples of "bad taste" and "good taste" and wanted to implement the same principle for a function that deletes the last node in a singly linked list. I also followed his coding style.

#include <stdio.h>
#include <stdlib.h>
/* linked list structure */
struct node {
 int data;
 struct node *next;
};
/* create new node */
struct node *new_node(int data, struct node *next)
{
 struct node *new = malloc(sizeof *new);
 if (!new) {
 fprintf(stderr, "Error: memory allocation failed\n");
 exit(EXIT_FAILURE);
 }
 new->data = data;
 new->next = next;
 return new;
}
/* delete last node */
void delete_last(struct node *head)
{
 if (head == NULL) {
 fprintf(stderr, "Error: linked list underflow\n");
 exit(EXIT_FAILURE);
 }
 struct node **cursor = &head;
 while ((*cursor)->next != NULL)
 cursor = &(*cursor)->next;
 free(*cursor);
 *cursor = NULL;
}
Source Link

Delete last node in singly linked list

In a TED talk Linus Torvalds made a point about "good taste" at approximately 14:10 in the interview. I read through his examples of "bad taste" and "good taste" and wanted to implement the same principle for a function that deletes the last node in a singly linked list. I also followed his coding style.

#include <stdio.h>
#include <stdlib.h>
/* linked list structure */
struct node {
 int data;
 struct node *next;
};
/* create new node */
struct node *new_node(int data, struct node *next)
{
 struct node *new = malloc(sizeof *new);
 if (!new) {
 fprintf(stderr, "Error: memory allocation failed\n");
 exit(EXIT_FAILURE);
 }
 new->data = data;
 new->next = next;
 return new;
}
/* delete last node */
void delete_last(struct node *head)
{
 if (head == NULL) {
 fprintf(stderr, "Error: linked list underflow\n");
 exit(EXIT_FAILURE);
 }
 struct node **cursor = &head;
 while ((*cursor)->next != NULL)
 cursor = &(*cursor)->next;
 free(*cursor);
 *cursor = NULL;
}
lang-c

AltStyle によって変換されたページ (->オリジナル) /