This repository has been archived by the owner on Sep 19, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
89 lines (67 loc) · 2 KB
/
list.h
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
/*
*
* lnkd_list.h
*
* Baltasar Dinis 89416
* IAED project
*
* header file for lnkd_list.c
*
* implements a (doubly) linked list
* implements an auxiliar datatype: l_node
*
* note on implementation: two dummy nodes (head and tail) are used.
*/
#ifndef LNKD_LIST_H
#define LNKD_LIST_H
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include "l_item.h"
/* define l_node */
typedef struct _l_node {
l_item val;
struct _l_node *prev, *next;
} l_node;
/* selectors */
#define val(a) ((a).val)
#define prev(a) ((a).prev)
#define next(a) ((a).next)
/* doubly linked list */
typedef struct {
l_node *head, *tail;
} lnkd_list;
/* selectors */
#define head(a) ((a).head)
#define tail(a) ((a).tail)
/* initialize l_node */
l_node *l_node_(l_item val, l_node *prev, l_node *next);
/* tests: test if a node is head or tail */
bool is_head(l_node *n);
bool is_tail(l_node *n);
/* goes to the next node: a verification if the node is a tail is needed */
l_node *go_next(l_node *n);
/* goes to the prev node: a verification if the node is a head is needed */
l_node *go_prev(l_node *n);
/* insert l_node after: returns false if src is tail */
bool insert_l_node_after(l_node *src, l_item val);
/* insert l_node before: returns false if src is head */
bool insert_l_node_before(l_node *src, l_item val);
/* remove l_node: returns false if src is head or tail */
bool remove_l_node(l_node *src);
/* free the memory allocated for a node */
void free_l_node(l_node *n);
/* carelessly free the memory allocated for a nodei
* ONLY USE WHEN FREEING EVERYTHING */
void careless_free_l_node(l_node *n);
/* initialize list */
lnkd_list *lnkd_list_();
/* check if a list is empty */
bool empty_list(lnkd_list *a);
/* add an element at the beginning of the linked list */
void add_at_beginning(lnkd_list *l, l_item val);
/* add an element at the end of the linked list */
void add_at_end(lnkd_list *l, l_item val);
/* free a list (and all its elements) */
void free_lnkd_list(lnkd_list *l);
#endif /* !LNKD_LIST_H */