-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmylist.h
137 lines (117 loc) · 3.51 KB
/
mylist.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
#ifndef _MYLIST_H_
#define _MYLIST_H_
/*
* A node in a linked list.
*/
struct Node {
void *data;
struct Node *next;
};
/*
* A linked list.
* 'head' points to the first node in the list.
*/
struct List {
struct Node *head;
};
/*
* Initialize an empty list.
*/
static inline void initList(struct List *list)
{
list->head = 0;
}
/*
* In all functions below, the 'list' parameter is assumed to point to
* a valid List structure.
*/
/*
* Create a node that holds the given data pointer,
* and add the node to the front of the list.
*
* Note that this function does not manage the lifetime of the object
* pointed to by 'data'.
*
* It returns the newly created node on success and NULL on failure.
*/
struct Node *addFront(struct List *list, void *data);
/*
* Traverse the list, calling f() with each data item.
*/
void traverseList(struct List *list, void (*f)(void *));
/*
* Traverse the list, comparing each data item with 'dataSought' using
* 'compar' function. ('compar' returns 0 if the data pointed to by
* the two parameters are equal, non-zero value otherwise.)
*
* Returns the first node containing the matching data,
* NULL if not found.
*/
struct Node *findNode(struct List *list, const void *dataSought,
int (*compar)(const void *, const void *));
/*
* Flip the sign of the double value pointed to by 'data' by
* multiplying -1 to it and putting the result back into the memory
* location.
*/
void flipSignDouble(void *data);
/*
* Compare two double values pointed to by the two pointers.
* Return 0 if they are the same value, 1 otherwise.
*/
int compareDouble(const void *data1, const void *data2);
/*
* Returns 1 if the list is empty, 0 otherwise.
*/
static inline int isEmptyList(struct List *list)
{
return (list->head == 0);
}
/*
* Remove the first node from the list, deallocate the memory for the
* ndoe, and return the 'data' pointer that was stored in the node.
* Returns NULL is the list is empty.
*/
void *popFront(struct List *list);
/*
* Remove all nodes from the list, deallocating the memory for the
* nodes. You can implement this function using popFront().
*/
void removeAllNodes(struct List *list);
/*
* Create a node that holds the given data pointer,
* and add the node right after the node passed in as the 'prevNode'
* parameter. If 'prevNode' is NULL, this function is equivalent to
* addFront().
*
* Note that prevNode, if not NULL, is assumed to be one of the nodes
* in the given list. The behavior of this function is undefined if
* prevNode does not belong in the given list.
*
* Note that this function does not manage the lifetime of the object
* pointed to by 'data'.
*
* It returns the newly created node on success and NULL on failure.
*/
struct Node *addAfter(struct List *list,
struct Node *prevNode, void *data);
/*
* Reverse the list.
*
* Note that this function reverses the list purely by manipulating
* pointers. It does NOT call malloc directly or indirectly (which
* means that it does not call addFront() or addAfter()).
*
* Implementation hint: keep track of 3 consecutive nodes (previous,
* current, next) and move them along in a while loop. Your function
* should start like this:
struct Node *prv = NULL;
struct Node *cur = list->head;
struct Node *nxt;
while (cur) {
......
* And at the end, prv will end up pointing to the first element of
* the reversed list. Don't forget to assign it to list->head.
*/
void reverseList(struct List *list);
#endif /* #ifndef _MYLIST_H_ */