summaryrefslogtreecommitdiff
path: root/src/sp_list.c
blob: c671f510869ba0b68a27fd8e640f6f1aa3559be6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include "sp_list.h"
#include <stdio.h>
#include <stdlib.h>
#include "php_snuffleupagus.h"

void sp_list_free(sp_node_t *node) {
  while(node) {
    sp_node_t *tmp = node->next;
    pefree(node, 1);
    node = tmp;
  }
}

sp_node_t *sp_list_new() {
  sp_node_t *new = pecalloc(sizeof(*new), 1, 1);
  new->next = new->data = new->head = NULL;
  return new;
}

void sp_list_insert(sp_node_t *list, void *data) {
  if (list->head == NULL) {
    list->data = data;
    list->next = NULL;
    list->head = list;
  } else {
    sp_node_t *new = pecalloc(sizeof(*new), 1, 1);

    new->data = data;
    new->next = NULL;
    new->head = list;

    while (list->next) {
      list = list->next;
    }
    list->next = new;
  }
}

void sp_list_prepend(sp_node_t *list, void *data) {
  if (list->head == NULL) {
    list->data = data;
    list->next = NULL;
    list->head = list;
  } else {
    sp_node_t *new = pecalloc(sizeof(*new), 1, 1);

    new->next = list->next;
    list->next = new;

    new->head = list;

    new->data = list->data;
    list->data = data;
  }
}