-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.h
68 lines (59 loc) · 1.96 KB
/
LinkedList.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
#ifndef COURSE_GRAPH_LINKEDLIST_H
#define COURSE_GRAPH_LINKEDLIST_H
#include "stdbool.h"
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include "Util.h"
/**
* Node for a linked list. Uses void* to point to data.
*/
typedef struct Node_struct {
void *data;
struct Node_struct *next;
} Node;
/**
* Singly-LinkedList implementation using void * ptrs
*/
typedef struct LinkedList_struct {
Node *head;
size_t size;
} LinkedList;
/**
* Initializes the linked list. Needs mem to be allocated
* @param list The pointer to allocated memory.
*/
void LinkedList_init(LinkedList *list);
/**
* Appends data to the end of the list
* @param list Initialized list
* @param data Data to append
*/
void LinkedList_push(LinkedList *list, void *data);
/**
* Searches for an element in the list based on a comparator. Returns the
* element if found. Useful when looking for Course object based on courseName.
* We can use a special comparator to fetch the object associated with that name.
* @param list an inialized list
* @param data the data to find
* @param compare the comparator function
* @return NULL if not found, void* pointer to the found data.
*/
void * LinkedList_find(const LinkedList *list, const void *data, int (*compare)(const void *, const void *));
/**
* Destructs an initialized list.
* @param list initialized list.
* @param data_free destructor of the elements of this list.
*/
void LinkedList_free(LinkedList *list, void (*data_free)(void *));
/**
* Removes data from a list.
* @param list The initialized list
* @param data The data to look for to remove from the list
* @param compare The comparator to use
* @param data_free The destructor to use for the removed data
* @return true if removed from the list, false if could not be found
*/
bool LinkedList_remove(LinkedList *list, const void *data, int (*compare)(const void *, const void *),
void (*data_free)(void *));
#endif //COURSE_GRAPH_LINKEDLIST_H