-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorted-list.c
68 lines (57 loc) · 1.7 KB
/
sorted-list.c
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
#include <stdlib.h>
#include "Header.h"
SortedListPtr SLCreate(CompareFuncT cf)
{
SortedListPtr newPtr = (struct SortedListPtr*) malloc(sizeof(struct SortedList));
newPtr->head = NULL;
newPtr->CompareFuncT = cf;
/* newPtr->SortedListIteratorPtr = SLCreateIterator() */
};
void SLDestroy(SortedListPtr list)
{
struct Node *cur = list->head;
while(cur != NULL)
{
list->head = list->head->next;
free(cur->data);
cur->next = NULL;
free(cur->next);
free(cur);
cur = list->head;
}
free(list);
return;
}
int SLInsert(SortedListPtr list, void *newObj){
CompareFuncT* current = list->CompareFuncT;
Node nu = *(struct Node*) malloc(sizeof(struct Node));
Node *cur = list->head;
int temp = current(newObj, list->head->data); //This takes care of the first node in the linked list
while(temp<=0 &&cur!=null){
cur = cur->next;
int temp = current(newObj, list->head->data);
}
Node *timp = (Node*) malloc(sizeof(Node));
timp->data = cur->data;
timp->next = cur->next;
nu->data = newObj;
nu->next = timp;
free(cur);
return 1;
}
int SLRemove(<#SortedListPtr list#>, <#void *newObj#>){
CompareFuncT* current = list->CompareFuncT;
Node nu = *(struct Node*) malloc(sizeof(struct Node));
Node *cur = list->head;
int temp = current(newObj, list->head->data); //This takes care of the first node in the linked list
while(temp!=0 && cur!=NULL){
cur = cur->next;
int temp = current(newObj, list->head->data);
}
if (cur==NULL) {
return 0;
}
cur* = (cur->next)*;
return 1;
}
}