-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
54 lines (46 loc) · 849 Bytes
/
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
#include <stdlib.h>
#include <stdio.h>
#include "list.h"
static list_t* mklistnod(int d)
{
list_t *t = NULL;
t = calloc(1, sizeof(list_t));
if (!t) return NULL;
t->d = d;
t->next = NULL;
return t;
}
void ins_list(list_t **head, int d)
{
list_t *t = NULL;
if (*head == NULL) {
*head = mklistnod(d);
return;
}
/* insert at head */
t = mklistnod(d);
t->next = *head;
*head = t;
}
/* return head node and delete it */
int rm_list(list_t **head)
{
int d = (*head)->d;
list_t *t = *head;
*head = (*head)->next;
free (t);
return d;
}
void dump_list (list_t *head)
{
list_t *p = head;
if (!p) {
printf ("List empty\n");
return;
}
while (p) {
printf (" [%d]-> ", p->d);
p = p->next;
}
printf ("\n");
}