Commit 66a3257b authored by Moritz Sokoll's avatar Moritz Sokoll 🦀
Browse files

Inital Commit

parents
" let g:vbuild_build_release_cmd = 'make -B'
" let g:vbuild_build_dev_cmd = 'make dev'
" let g:vbuild_debug_cmd = 'gdb ./linked-list'
set makeprg=make\ -B
CC = gcc
CFLAGS = -Wall -std=c18 -Iinclude -o linked-list
SRC = $(wildcard src/*.c)
all: linked-list
linked-list:
$(CC) $(CFLAGS) $(SRC)
dev:
$(CC) $(CFLAGS) -ggdb $(SRC)
clean:
rm -rf linked-list
# A linked list implementation in C
## This is simply a implementation to test my knowledge about C and linked lists. It's not meant to be anything close to a library
## for helping you with collection types
This implementation is automatically allocating and freeing memory (kind of). If you wanted to use this implementation make sure to always
call `linked_list_clean(list)` if you're done with your program/list
## Compilation
**Dependencies**
1. A C compiler (`gcc` is used)
2. `make`
If you have all of the dependencies installed you can just run `make`.
[
{
"arguments": [
"/usr/bin/gcc",
"-c",
"-Wall",
"-std=c18",
"-Iinclude",
"-o",
"linked-list",
"src/main.c"
],
"directory": "/home/moritz/Code/C/collections/linked_list",
"file": "/home/moritz/Code/C/collections/linked_list/src/main.c",
"output": "/home/moritz/Code/C/collections/linked_list/linked-list"
}
]
#ifndef __LINKED_LIST_H__
#define __LINKED_LIST_H__
#include <stdlib.h>
struct LinkedListNode {
int value;
struct LinkedListNode *next;
};
typedef struct LinkedListNode linked_list_node_t;
struct LinkedList {
linked_list_node_t *head;
size_t size;
};
typedef struct LinkedList linked_list_t;
linked_list_t *linked_list_empty_new();
void linked_list_push(linked_list_t *list, int value);
void linked_list_clean(linked_list_t *list);
int linked_list_peek(linked_list_t *list);
void linked_list_pre(linked_list_t *list, int value);
#endif
File added
#include "linkedlist.h"
#include <stdlib.h>
#include <assert.h>
linked_list_t *linked_list_empty_new() {
linked_list_t *list = malloc(sizeof(linked_list_t));
list->head = NULL;
list->size = 0;
return list;
}
void linked_list_push(linked_list_t *list, int value) {
linked_list_node_t *cur_head = list->head;
linked_list_node_t *new_head = malloc(sizeof(linked_list_node_t));
new_head->value = value;
new_head->next = cur_head;
list->head = new_head;
list->size++;
}
void linked_list_clean(linked_list_t *list) {
linked_list_node_t *cur = list->head;
while (cur != NULL) {
linked_list_node_t *next = cur->next;
free(cur);
cur = next;
}
free(list);
}
int linked_list_peek(linked_list_t *list) {
assert(list->head != NULL);
return list->head->value;
}
void linked_list_pre(linked_list_t *list, int value) {
// get the last node
linked_list_node_t *cur_tail = list->head;
while (cur_tail->next != NULL) {
cur_tail = cur_tail->next;
}
linked_list_node_t *new_tail = malloc(sizeof(linked_list_node_t));
new_tail->value = value;
new_tail->next = NULL;
cur_tail->next = new_tail;
list->size++;
}
#include <stdio.h>
#include "linkedlist.h"
int main() {
linked_list_t *list = linked_list_empty_new();
linked_list_push(list, 1);
printf("value at head of linked list is: %d, size is: %ld\n", linked_list_peek(list), list->size);
linked_list_pre(list, 2);
printf("value at head of linked list is: %d, size is: %ld\n", linked_list_peek(list), list->size);
// deallocate all the nodes and the list itself
linked_list_clean(list);
return 0;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment