Repository navigation
Expand file tree
/
Copy pathlinked_list.h
More file actions
140 lines (121 loc) · 4.52 KB
/
Copy pathlinked_list.h
File metadata and controls
140 lines (121 loc) · 4.52 KB
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
#ifndef LINKED_LIST_H_
#define LINKED_LIST_H_
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
// Some rules for Pointer Wars 2025:
// 0. Implement all functions in linked_list.c
// 1. Feel free to add members to the structures, but please do not remove
// any or rename any. Doing so will cause test infrastructure to fail
// to link against your shared library.
// 2. Same goes for the function declarations.
// 3. In your implementation, make use of malloc_fptr() and free_fptr()
// functions for memory allocation and memory freeing. This allows
// test infrastructure a bit more flexility. See linked_list.c for
// declarations of those function pointers.
// Declaration of the linked_list data structure.
// Feel free to change as desired.
//
struct node;
struct linked_list {
struct node * head;
};
// A node in the linked_list structure.
// Feel free to change as desired.
//
struct node {
struct node * next;
unsigned int data;
};
// Very simple, not thread safe, iterator.
//
struct iterator {
struct linked_list * ll;
struct node * current_node;
size_t current_index;
unsigned int data;
};
// Creates a new linked_list.
// PRECONDITION: Register malloc() and free() functions via the
// linked_list_register_malloc() and
// linked_list_register_free() functions.
// POSTCONDITION: An empty linked_list has its head point to NULL.
// Returns a new linked_list on success, NULL on failure.
//
struct linked_list * linked_list_create(void);
// Deletes a linked_list.
// \param ll : Pointer to linked_list to delete
// POSTCONDITION : An empty linked_list has its head point to NULL.
// Returns TRUE on success, FALSE otherwise.
//
bool linked_list_delete(struct linked_list * ll);
// Returns the size of a linked_list.
// \param ll : Pointer to linked_list.
// Returns size on success, SIZE_MAX on failure.
//
size_t linked_list_size(struct linked_list * ll);
// Inserts an element at the end of the linked_list.
// \param ll : Pointer to linked_list.
// \param data : Data to insert.
// Returns TRUE on success, FALSE otherwise.
//
bool linked_list_insert_end(struct linked_list * ll,
unsigned int data);
// Inserts an element at the front of the linked_list.
// \param ll : Pointer to linked_list.
// \param data : Data to insert.
// Returns TRUE on success, FALSE otherwise.
//
bool linked_list_insert_front(struct linked_list * ll,
unsigned int data);
// Inserts an element at a specified index in the linked_list.
// \param ll : Pointer to linked_list.
// \param index : Index to insert data at.
// \param data : Data to insert.
// Returns TRUE on success, FALSE otherwise.
//
bool linked_list_insert(struct linked_list * ll,
size_t index,
unsigned int data);
// Finds the first occurrence of data and returns its index.
// \param ll : Pointer to linked_list.
// \param data : Data to find.
// Returns index of the first index with that data, SIZE_MAX otherwise.
//
size_t linked_list_find(struct linked_list * ll,
unsigned int data);
// Removes a node from the linked_list at a specific index.
// \param ll : Pointer to linked_list.
// \param index : Index to remove node.
// Returns TRUE on success, FALSE otherwise.
//
bool linked_list_remove(struct linked_list * ll,
size_t index);
// Creates an iterator struct at a particular index.
// \param linked_list : Pointer to linked_list.
// \param index : Index of the linked list to start at.
// Returns pointer to an iterator on success, NULL otherwise.
//
struct iterator * linked_list_create_iterator(struct linked_list * ll,
size_t index);
// Deletes an iterator struct.
// \param iterator : Iterator to delete.
// Returns TRUE on success, FALSE otherwise.
//
bool linked_list_delete_iterator(struct iterator * iter);
// Iterates to the next node in the linked_list.
// \param iterator: Iterator to iterate on.
// Returns TRUE when next node is present, FALSE once end of list is reached.
//
bool linked_list_iterate(struct iterator * iter);
// Registers malloc() function.
// \param malloc : Function pointer to malloc()-like function.
// Returns TRUE on success, FALSE otherwise.
//
bool linked_list_register_malloc(void * (*malloc)(size_t));
// Registers free() function.
// \param free : Function pointer to free()-like function.
// Returns TRUE on success, FALSE otherwise.
//
bool linked_list_register_free(void (*free)(void*));
#endif