-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.c
99 lines (81 loc) · 1.43 KB
/
list.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
This File is part of Boo's Chess Engine
Copyright 1999 by Christopher Bowron
*/
#include <stdlib.h>
#include <stdio.h>
#include "bce.h"
list *alloclist()
{
return malloc(sizeof(list));
}
void freelistitem(list *p)
{
if (p->free)
free(p->data.ptr);
free(p);
}
void push (list **l, long m)
{
list *new = alloclist();
new->data.l = m;
new->free = 0;
new->next = *l;
*l=new;
}
long pop (list **l)
{
long m;
list *temp=*l;
m = temp->data.l;
*l = temp->next;
freelistitem(temp);
return m;
}
void push_ptr(list **l, void *ptr)
{
list *new = alloclist();
new->data.ptr = ptr;
new->free = 1;
new->next = *l;
*l=new;
}
void *pop_ptr(list **l)
{
void *p;
list *temp=*l;
p = temp->data.ptr;
*l = temp->next;
temp->free = 0;
freelistitem(temp);
return p;
}
list *copylist(list *l)
{
list *n;
if (!l) return NULL;
n = malloc(sizeof(list));
n->data = l->data;
n->free = l->free;
n->next = copylist(l->next);
return n;
}
int length(list *l)
{
if (l == NULL) return 0;
return length(l->next)+1;
}
long nth(list *l, int n)
{
if (n==0)
return l->data.l;
return nth(l->next,n-1);
}
void killlist(list *l)
{
list *t = l;
if (!l)
return;
killlist(l->next);
freelistitem(t);
}