-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint_list.h
144 lines (127 loc) · 4.1 KB
/
int_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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#ifndef __INT_LIST_H
#define __INT_LIST_H
#include <stdbool.h>
#include "util.h"
/*
* Declaration of a undirected list for storing integers for the
* "Datastructures and algorithms" courses at the Department of
* Computing Science, Umea University. The implementation uses a
* dynamic, linked structure. After use, the function list_kill must
* be called to de-allocate the dynamic memory used by the list
* itself. The implementation is a written a code copy specialization
* of the generic list to provide a simpler starting data structure
* than the generic list.
*
* Authors: Niclas Borlin ([email protected])
*
* Version information:
* 2018-01-28: v1.0, first public version.
*/
// ==========PUBLIC DATA TYPES============
// List type.
typedef struct list list;
// List position type.
typedef struct cell *list_position;
// ==========DATA STRUCTURE INTERFACE==========
/**
* list_empty() - Create an empty list.
*
* Returns: A pointer to the new list.
*/
list *list_empty(void);
/**
* list_is_empty() - Check if a list is empty.
* @l: List to check.
*
* Returns: True if the list is empty, otherwise false.
*/
bool list_is_empty(const list *l);
/**
* list_first() - Return the first position of a list, i.e. the
* position of the first element in the list.
* @l: List to inspect.
*
* Returns: The first position in the given list.
*/
list_position list_first(const list *l);
/**
* list_end() - Return the last position of a list, i.e. the position
* after the last element in the list.
* @l: List to inspect.
*
* Returns: The last position in the given list.
*/
list_position list_end(const list *l);
/**
* list_next() - Return the next position in a list.
* @l: List to inspect.
* @pos: Any valid position except the last in the list.
*
* Returns: The position in the list after the given position.
* NOTE: The return value is undefined for the last position.
*/
list_position list_next(const list *l, const list_position pos);
/**
* list_previous() - Return the previous position in a list.
* @l: List to inspect.
* @pos: Any valid position except the first in the list.
*
* Returns: The position in the list before the given position.
* NOTE: The return value is undefined for the first position.
*/
list_position list_previous(const list *l, const list_position pos);
/**
* list_inspect() - Return the value of the element at a given
* position in a list.
* @l: List to inspect.
* @pos: Any valid position in the list, except the last.
*
* Returns: The integer value stored in the element at postiion pos.
* NOTE: The return value is undefined for the last position.
*/
int list_inspect(const list *l, const list_position pos);
/**
* list_insert() - Insert a new element with a given value into a list.
* @l: List to manipulate.
* @data: Integer value to be inserted into the list.
* @pos: Position in the list before which the value should be inserted.
*
* Creates a new element and inserts it into the list before pos.
* Stores data in the new element.
*
* Returns: The position of the newly created element.
*/
list_position list_insert(list *l, int data, const list_position pos);
/**
* list_remove() - Remove an element from a list.
* @l: List to manipulate.
* @pos: Position in the list of the element to remove.
*
* Removes the element at position pos from the list. If a free_func
* was registered at list creation, calls it to deallocate the memory
* held by the element value.
*
* Returns: The position after the removed position.
*/
list_position list_remove(list *l, const list_position pos);
/*
* list_kill() - Destroy a given list.
* @l: List to destroy.
*
* Returns all dynamic memory used by the list and its elements. If a
* free_func was registered at list creation, also calls it for each
* element to free any user-allocated memory occupied by the element values.
*
* Returns: Nothing.
*/
void list_kill(list *l);
/*
* list_print() - Iterate over the list element and print their values.
* @l: List to inspect.
*
* Iterates over the list and print each stored integer.
*
* Returns: Nothing.
*/
void list_print(const list *l);
#endif