Simple linked list structure
This commit is contained in:
parent
522deb6f82
commit
2726e1ec76
|
@ -0,0 +1,56 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/** Linked list data structure
|
||||
*/
|
||||
|
||||
struct ListNode {
|
||||
int val;
|
||||
struct ListNode *next;
|
||||
};
|
||||
|
||||
struct ListNode *list_create(int val) {
|
||||
struct ListNode *node = malloc(sizeof(struct ListNode));
|
||||
node->next = NULL;
|
||||
node->val = val;
|
||||
return node;
|
||||
}
|
||||
|
||||
struct ListNode *list_insert(struct ListNode *list, int val) {
|
||||
if (list == NULL) {
|
||||
list = list_create(val);
|
||||
return list;
|
||||
} else {
|
||||
struct ListNode *tail = list_create(val);
|
||||
struct ListNode *current = list;
|
||||
while (current->next != NULL) {
|
||||
current = current->next;
|
||||
}
|
||||
current->next = tail;
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
void list_clear(struct ListNode *list) {
|
||||
struct ListNode *current = list;
|
||||
if (current == NULL) {
|
||||
return;
|
||||
}
|
||||
while (current->next != NULL) {
|
||||
struct ListNode *next = current->next;
|
||||
free(current);
|
||||
current = next;
|
||||
}
|
||||
}
|
||||
|
||||
void list_print(struct ListNode *list) {
|
||||
struct ListNode *current = list;
|
||||
printf("list: ");
|
||||
while (current != NULL) {
|
||||
printf("%d->", current->val);
|
||||
current = current->next;
|
||||
}
|
||||
printf("\n");
|
||||
}
|
|
@ -0,0 +1,82 @@
|
|||
#include "../include/list.h"
|
||||
|
||||
|
||||
/** Remove n-th element from linked list */
|
||||
struct ListNode *removeNthElement(struct ListNode *head, int n) {
|
||||
int i=0;
|
||||
if (n < 0) {
|
||||
return head;
|
||||
}
|
||||
if (n == 0) {
|
||||
return head->next;
|
||||
}
|
||||
struct ListNode *current = head;
|
||||
while (i < n - 1 && current != NULL) {
|
||||
current = current->next;
|
||||
i++;
|
||||
}
|
||||
if (current != NULL && current->next != NULL && current->next->next != NULL) {
|
||||
struct ListNode *to_remove = current->next;
|
||||
current->next = current->next->next;
|
||||
free(to_remove);
|
||||
} else if (current != NULL && current->next != NULL) {
|
||||
free(current->next);
|
||||
current->next = NULL;
|
||||
}
|
||||
return head;
|
||||
}
|
||||
|
||||
/** Length of a linked list */
|
||||
unsigned int list_length(struct ListNode *head) {
|
||||
unsigned int length = 0;
|
||||
struct ListNode *current = head;
|
||||
while (current != NULL) {
|
||||
current = current->next;
|
||||
length++;
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
struct ListNode *removeNthFromEnd(struct ListNode *head, int n) {
|
||||
if (n < 0) {
|
||||
return head;
|
||||
}
|
||||
unsigned int length = list_length(head);
|
||||
return removeNthElement(head, length - n);
|
||||
}
|
||||
|
||||
|
||||
void test_1() {
|
||||
struct ListNode *l = NULL;
|
||||
l = list_insert(l, 1);
|
||||
l = list_insert(l, 2);
|
||||
l = list_insert(l, 3);
|
||||
l = list_insert(l, 4);
|
||||
l = list_insert(l, 5);
|
||||
struct ListNode *result_list = removeNthFromEnd(l, 2);
|
||||
list_print(result_list);
|
||||
list_clear(result_list);
|
||||
}
|
||||
|
||||
void test_2() {
|
||||
struct ListNode *l = NULL;
|
||||
l = list_insert(l, 1);
|
||||
struct ListNode *result_list = removeNthFromEnd(l, 1);
|
||||
list_print(result_list);
|
||||
list_clear(result_list);
|
||||
}
|
||||
|
||||
void test_3() {
|
||||
struct ListNode *l = NULL;
|
||||
l = list_insert(l, 1);
|
||||
l = list_insert(l, 2);
|
||||
struct ListNode *result_list = removeNthFromEnd(l, 1);
|
||||
list_print(result_list);
|
||||
list_clear(result_list);
|
||||
}
|
||||
|
||||
int main() {
|
||||
test_1();
|
||||
test_2();
|
||||
test_3();
|
||||
}
|
Loading…
Reference in New Issue