-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted-list.h
121 lines (96 loc) · 3.41 KB
/
sorted-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
#ifndef SORTED_LIST_H
#define SORTED_LIST_H
/*
* sorted-list.h
*/
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "datastructs.h"
/*
* When your sorted list is used to store objects of some type, since the
* type is opaque to you, you will need a comparator function to order
* the objects in your sorted list.
*
* You can expect a comparator function to return -1 if the 1st object is
* smaller, 0 if the two objects are equal, and 1 if the 2nd object is
* smaller.
*
* Note that you are not expected to implement any comparator functions.
* You will be given a comparator function when a new sorted list is
* created.
*/
/*
* SLCreate creates a new, empty sorted list. The caller must provide
* a comparator function that can be used to order objects that will be
* kept in the list.
*
* If the function succeeds, it returns a (non-NULL) SortedListT object.
* Else, it returns NULL.
*
* You need to fill in this function as part of your implementation.
*/
SortedListPtr SLCreate(CompareFuncT cf);
/*
* SLDestroy destroys a list, freeing all dynamically allocated memory.
* Ojects should NOT be deallocated, however. That is the responsibility
* of the user of the list.
*
* You need to fill in this function as part of your implementation.
*/
void SLDestroy(SortedListPtr list);
/*
* SLInsert inserts a given object into a sorted list, maintaining sorted
* order of all objects in the list. If the new object is equal to a subset
* of existing objects in the list, then the subset can be kept in any
* order.
*
* If the function succeeds, it returns 1. Else, it returns 0.
*
* You need to fill in this function as part of your implementation.
*/
int SLInsert(SortedListPtr list, void *newObj);
/*
* SLRemove removes a given object from a sorted list. Sorted ordering
* should be maintained.
*
* If the function succeeds, it returns 1. Else, it returns 0.
*
* You need to fill in this function as part of your implementation.
*/
int SLRemove(SortedListPtr list, void *newObj);
/*
* SLCreateIterator creates an iterator object that will allow the caller
* to "walk" through the list from beginning to the end using SLNextItem.
*
* If the function succeeds, it returns a non-NULL SortedListIterT object.
* Else, it returns NULL.
*
* You need to fill in this function as part of your implementation.
*/
SortedListIteratorPtr SLCreateIterator(SortedListPtr list);
/*
* SLDestroyIterator destroys an iterator object that was created using
* SLCreateIterator(). Note that this function should destroy the
* iterator but should NOT affectt the original list used to create
* the iterator in any way.
*
* You need to fill in this function as part of your implementation.
*/
void SLDestroyIterator(SortedListIteratorPtr iter);
/*
* SLNextItem returns the next object in the list encapsulated by the
* given iterator. It should return a NULL when the end of the list
* has been reached.
*
* One complication you MUST consider/address is what happens if a
* sorted list encapsulated within an iterator is modified while that
* iterator is active. For example, what if an iterator is "pointing"
* to some object in the list as the next one to be returned but that
* object is removed from the list using SLRemove() before SLNextItem()
* is called.
*
* You need to fill in this function as part of your implementation.
*/
void *SLNextItem(SortedListIteratorPtr iter);
#endif