blob: 443ebca8ad0124a9bc3f4c5bc6e431abd3b5f72f (
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#include <stdio.h>
#include <stdlib.h>
#include "dlist.h"
struct dlist *dlist_init(void)
{
struct dlist *res = malloc(sizeof(struct dlist));
if (res == NULL)
return NULL;
res->size = 0;
res->head = NULL;
res->tail = NULL;
return res;
}
int dlist_push_front(struct dlist *list, int element)
{
if (element < 0)
return 0;
struct dlist_item *new = malloc(sizeof(struct dlist_item));
if (new == NULL)
return 0;
new->data = element;
new->next = list->head;
new->prev = NULL;
if (list->size == 0)
list->tail = new;
else
list->head->prev = new;
list->head = new;
list->size++;
return 1;
}
void dlist_print(const struct dlist *list)
{
if (list->size == 0)
return;
for (struct dlist_item *i = list->head; i != list->tail; i = i->next)
{
printf("%d\n", i->data);
}
printf("%d\n", list->tail->data);
}
int dlist_push_back(struct dlist *list, int element)
{
if (element < 0)
return 0;
struct dlist_item *new = malloc(sizeof(struct dlist_item));
if (new == NULL)
return 0;
new->data = element;
new->prev = list->tail;
new->next = NULL;
if (list->size == 0)
list->head = new;
else
list->tail->next = new;
list->tail = new;
list->size++;
return 1;
}
size_t dlist_size(const struct dlist *list)
{
return list->size;
}
|