-
Notifications
You must be signed in to change notification settings - Fork 0
/
possibleCellsListsFuncs.c
122 lines (112 loc) · 2.88 KB
/
possibleCellsListsFuncs.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
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
#include "possibleCellsListsFuncs.h"
#include "helperFuncs.h"
//This function deletes the possible cells node from the list
void deletePossibleCellsNodeFromList(PossibleCellsList *lst, PossibleCellsListNode *node)
{
if (lst->head == node && lst->tail == node) //only
{
lst->head = lst->tail = NULL;
}
else if (lst->head == node) //head
{
lst->head = node->next;
lst->head->prev = NULL;
}
else if (lst->tail == node) //tail
{
lst->tail = node->prev;
lst->tail->next = NULL;
}
else //mid
{
node->prev->next = node->next;
node->next->prev = node->prev;
}
lst->size--;
}
//This function creats an empty possible cells list
void makeEmptyPossibleCellsList(PossibleCellsList *lst)
{
lst->size = 0;
lst->head = lst->tail = NULL;
}
//This function creates a new possible cells list node.
PossibleCellsListNode *createNewPossibleCellsListNode(CellPlace cellPlace)
{
PossibleCellsListNode *res = (PossibleCellsListNode*)malloc(sizeof(PossibleCellsListNode));
checkAllocation(res, "Failed to allocate list node to memory");
res->place = cellPlace;
res->next = res->prev = NULL;
return res;
}
//This function checks if the list is empty
bool isPossibleCellsListEmpty(PossibleCellsList *lst)
{
return (!lst->head);
}
//This function inserts the possible cells node to the end of list.
void insertPossibleCellsDataToEndList(PossibleCellsList *lst, CellPlace cellPlace)
{
PossibleCellsListNode *newTail;
newTail = createNewPossibleCellsListNode(cellPlace);
insertPossibleCellsNodeToEndList(lst, newTail);
}
//This function inserts the possible cells node to the end of list.
void insertPossibleCellsNodeToEndList(PossibleCellsList *lst, PossibleCellsListNode *newTail)
{
if (isPossibleCellsListEmpty(lst))
{
lst->head = lst->tail = newTail;
}
else
{
lst->tail->next = newTail;
newTail->prev = lst->tail;
lst->tail = newTail;
}
lst->size++;
}
//This function returns the the possible cells node of the cell it gets
PossibleCellsListNode *getPossibleCellsListNode(PossibleCellsList *lst, int cellNum)
{
PossibleCellsListNode *curr = lst->head;
if ((isPossibleCellsListEmpty(lst)) || (cellNum > lst->size)) {
return NULL;
}
else
{
while (cellNum > 0)
{
cellNum--;
curr = curr->next;
}
}
return curr;
}
//This function frees the list
void freePossibleCellsList(PossibleCellsList *lst)
{
PossibleCellsListNode *curr = lst->head, *temp;
while (curr)
{
temp = curr->next;
free(curr);
curr = temp;
lst->size--;
}
}
//This function creats a new possible cells list of all the board
void createPossibleCellsList(PossibleCellsList *lst)
{
int i, j;
CellPlace cellPlace;
for (i = 0; i < COLSIZE; i++)
{
for (j = 0; j < ROWSIZE; j++)
{
cellPlace.R = i;
cellPlace.C = j;
insertPossibleCellsDataToEndList(lst, cellPlace);
}
}
}