Created
June 17, 2022 08:26
-
-
Save fengyfei/8fde0bfe6358fb99de2f59027a9f22b6 to your computer and use it in GitHub Desktop.
[C] Linked List
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct node { | |
struct node *next; | |
int value; | |
} node_t, *node_ptr; | |
typedef struct list { | |
node_ptr head; | |
} list_t; | |
node_ptr create_node(int value) { | |
node_ptr p_node = (node_ptr)malloc(sizeof(node_t)); | |
if (p_node != NULL) { | |
p_node->next = NULL; | |
p_node->value = value; | |
} | |
return p_node; | |
} | |
// Append at tail | |
void list_append(list_t *list, node_ptr node) { | |
if (node == NULL) { | |
return; | |
} | |
if (list->head == NULL) { | |
list->head = node; | |
return; | |
} | |
node_ptr last = list->head; | |
while(last->next != NULL) { | |
last = last->next; | |
} | |
last->next = node; | |
} | |
void list_append_front(list_t *list, node_ptr node) { | |
if (node == NULL) { | |
return; | |
} | |
node->next = list->head; | |
list->head = node; | |
} | |
void list_display(list_t *list) { | |
node_ptr p_node = list->head; | |
while(p_node != NULL) { | |
printf("node[%u] {\n next: %u, \n value: %d\n}\n", p_node, p_node->next, p_node->value); | |
p_node = p_node->next; | |
} | |
} | |
void list_destroy(list_t *list) { | |
node_ptr p_node = list->head->next; | |
while(p_node != NULL) { | |
free(list->head); | |
list->head = p_node; | |
p_node = p_node->next; | |
} | |
free(list->head); | |
list->head = NULL; | |
} | |
void list_delete(list_t *list, node_ptr p_node) { | |
if (p_node == list->head) { | |
list->head = p_node->next; | |
p_node->next = NULL; | |
return; | |
} | |
node_ptr p_prev = list->head; | |
while(p_prev != NULL && p_prev->next != p_node) { | |
p_prev = p_prev->next; | |
} | |
if (p_prev == NULL) { | |
return; | |
} | |
p_prev->next = p_node->next; | |
p_node->next = NULL; | |
} | |
void list_reverse(list_t *list) { | |
node_ptr p_node = list->head, p_next = list->head->next; | |
list->head->next = NULL; | |
while(p_next != NULL) { | |
list->head = p_next; | |
p_next = p_next->next; | |
list->head->next = p_node; | |
p_node = list->head; | |
} | |
list->head = p_node; | |
} | |
void list_push(list_t *list, node_ptr node) { | |
if (node == NULL) { | |
return; | |
} | |
node->next = list->head; | |
list->head = node; | |
} | |
node_ptr list_pop(list_t *list) { | |
if (list->head == NULL) { | |
return NULL; | |
} | |
node_ptr p_node = list->head; | |
list->head = list->head->next; | |
p_node->next = NULL; | |
return p_node; | |
} | |
int main() { | |
list_t list = { | |
.head = NULL | |
}; | |
int const len = 10; | |
for (int i = 0; i < len; i++) { | |
node_ptr p_node = create_node(i); | |
list_append_front(&list, p_node); | |
} | |
list_display(&list); | |
printf("\n"); | |
list_reverse(&list); | |
list_display(&list); | |
printf("\n"); | |
node_ptr p_node = create_node(len); | |
list_push(&list, p_node); | |
list_delete(&list, p_node->next); | |
list_delete(&list, p_node); | |
list_display(&list); | |
printf("\n"); | |
list_destroy(&list); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment