Skip to content

Commit

Permalink
add list
Browse files Browse the repository at this point in the history
  • Loading branch information
mcksp committed Dec 11, 2016
1 parent 804598d commit d7bf7d8
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@

game
*.ttf
*.out
38 changes: 38 additions & 0 deletions list.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "list.h"
#include <string.h>
#include <stdlib.h>

void push(struct node **head, void *data, size_t data_size) {
struct node* new_node = malloc(sizeof(struct node));
new_node->data = malloc(data_size);
new_node->next = *head;
memcpy(new_node->data, data, data_size);
*head = new_node;
}

void erase(struct node **head, int n) {
int i;
struct node *tmp = *head;
if (n == 0) {
tmp = (*head)->next;
free((*head)->data);
free(*head);
*head = tmp;
} else {
for (i = 0; i < n - 1; i++) {
if (tmp->next != NULL) {
tmp = tmp->next;
} else {
break;
}
}
struct node *next = NULL;
if (tmp->next != NULL) {
next = tmp->next->next;
}
free(tmp->next->data);
free(tmp->next);
tmp->next = next;
}
}

14 changes: 14 additions & 0 deletions list.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef LIST_H
#define LIST_H
#include <stdio.h>


struct node {
void *data;
struct node *next;
};

void push(struct node **head, void *new_data, size_t data_size);
void erase(struct node **head, int n);

#endif

0 comments on commit d7bf7d8

Please sign in to comment.