This repository has been archived by the owner on Dec 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist.h
104 lines (87 loc) · 2.89 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* CS 2110 - Spring 2011 - Homework #11
* Edited by: Brandon Whitehead
*
* list.h: Fix the datatype definitions!
*/
#ifndef _LIST_H_DEF_
#define _LIST_H_DEF_
/*************************
** Datatype definitions **
**************************/
/* The node struct. Has a prev pointer, next pointer, and data. */
/* NOTE PLEASE DEFINE THE FIELDS IN THE ORDER GIVEN! */
/* DO NOT DEFINE ANYTHING OTHER THAN WHAT I SAY HERE */
typedef struct lnode
{
struct lnode *prev;
struct lnode *next;
void *data;
} node;
/* The linked list struct. Has a head pointer. */
typedef struct llist
{
node *head;
unsigned int size;
} list;
typedef void (*list_op)(void *);
typedef int (*list_pred)(void *);
typedef int (*compare_op)(const void *, const void *);
/**************************************************
** Prototypes for linked list library functions. **
** **
** For more details on their functionality, **
** check list.c. **
***************************************************/
/* Creating */
list* create_list(void);
/* Adding */
void push_front(list* llist, void* data);
void push_back(list* llist, void* data);
/* Removing */
int pop_front(list* llist, list_op free_func);
int pop_back(list* llist, list_op free_func);
int remove_data(list* llist, void* data, compare_op compare_func, list_op free_func);
int remove_if(list* llist, list_pred pred_func, list_op free_func);
/* Querying List */
void* front(list* llist);
void* back(list* llist);
int is_empty(list* llist);
int size(list* llist);
/* Searching */
int find_occurrence(list* llist, void* search, compare_op compare_func);
/* Freeing */
void empty_list(list* llist, list_op free_func);
/* Traversal */
void traverse(list* llist, list_op do_func);
/* Default functions provided to the user. */
void print_data(void* data);
void free_data(void* data);
int compare_data(const void* a, const void* b);
int test_data(const void* a);
/* Debugging Support */
#ifdef DEBUG
/*
Does the following if compiled in debug mode
When compiled in release mode does absolutely nothing.
*/
#define IF_DEBUG(call) (call)
/* Prints text (in red) if in debug mode */
#define DEBUG_PRINT(string) fprintf(stderr, "\033[31m%s:%d %s\n\033[0m", __FILE__, __LINE__, (string))
/* Asserts if the expression given is true (!0) */
/* If this fails it prints a message and terminates */
#define DEBUG_ASSERT(expr) \
do \
{ \
if (!(expr)) \
{ \
fprintf(stderr, "ASSERTION FAILED %s != TRUE (%d) IN %s ON line %d\n", #expr, (expr), __FILE__, __LINE__); \
exit(0); \
} \
} while(0)
#else
#define IF_DEBUG(call)
#define DEBUG_PRINT(string)
#define DEBUG_ASSERT(expr)
#endif
#endif