-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedList.c
194 lines (181 loc) · 4.19 KB
/
linkedList.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* linkedList.c
*
* Created on: 1 áàåâ 2019
* Author: shake
*/
#include <stdlib.h>
#include <stdio.h>
#include "Cell.h"
typedef struct Node{
struct Node* next;
struct Node* prev;
int row;
int column;
int bef;
int aft;
int change; /*0 - only one cell have changed, 1 - the first cell in a series of changed cells,
2 - not the first cell in a series of changed cells, */
}Node;
typedef struct{
int length;
Node* first;
Node* last;
Node* curr;
}linkedList;
Node* createNode(int row, int column, int bef, int aft, int change)
{
Node* node = (Node*)malloc(sizeof(Node));
if(node == NULL)
{
printf("Error: malloc has failed.\n");
exit(0);
}
node->next = NULL;
node->prev = NULL;
node->row = row;
node->column = column;
node->bef = bef;
node->aft = aft;
node->change = change;
return node;
}
linkedList* createList()
{
linkedList* list = (linkedList*)malloc(sizeof(linkedList));
if(list == NULL)
{
printf("Error: malloc has failed.\n");
exit(0);
}
list->first = createNode(-1,-1,-1,-1, -1);
list->last = createNode(-1,-1,-1,-1, -1);
list->first->next = list->last;
list->last->prev = list->first;
list->curr = list->first;
list->length = 0;
return list;
}
void addNode(linkedList* list, int row, int column, int bef, int aft, int change)
{
Node* newNode = createNode(row, column, bef, aft, change);
if(!list->length)
{
list->first->next = newNode;
newNode->prev = list->first;
}
else
{
newNode->prev = list->last->prev;
newNode->prev->next = newNode;
}
newNode->next = list->last;
list->last->prev = newNode;
list->length++;
list->curr = list->last->prev;
}
void deleteNode(linkedList* list, Node* node)
{
Node* temp = node->next;
list->length--;
node->prev->next = temp;
temp->prev = node->prev;
free(node);
}
void deleteAll(linkedList* list)
{
int i, len = list->length;
for(i = 0; i < len; i++)
{
deleteNode(list, list->last->prev);
}
}
void deleteFromHere(linkedList* list, Node* fromHere)
{
Node* node = fromHere;
while(fromHere != list->last)
{
fromHere = node->next;
deleteNode(list, node);
node = fromHere;
}
}
void undo(linkedList* reun, Cell** board, Cell** clone, int UOR) /*UOR = 0 for undo, 1 for reset*/
{
int i, j;
if(!(reun->curr->change))
{
i = reun->curr->row;
j = reun->curr->column;
board[i][j].value = reun->curr->bef;
clone[i][j].value = reun->curr->bef;;
reun->curr = reun->curr->prev;
if(!UOR)
{
printf("Cell <%d,%d> changed to %d.\n", j+1, i+1, board[i][j].value);
}
}
else
{
while(reun->curr->change != 1)
{
i = reun->curr->row;
j = reun->curr->column;
board[i][j].value = reun->curr->bef;
clone[i][j].value = reun->curr->bef;;
reun->curr = reun->curr->prev;
if(!UOR)
{
printf("Cell <%d,%d> changed to %d.\n", j+1, i+1, board[i][j].value);
}
}
i = reun->curr->row;
j = reun->curr->column;
board[i][j].value = reun->curr->bef;
clone[i][j].value = reun->curr->bef;;
reun->curr = reun->curr->prev;
if(!UOR)
{
printf("Cell <%d,%d> changed to %d.\n", j+1, i+1, board[i][j].value);
}
}
}
void redo(linkedList* reun, Cell** board, Cell** clone)
{
int i, j;
reun->curr = reun->curr->next;
if(!(reun->curr->change))
{
i = reun->curr->row;
j = reun->curr->column;
board[i][j].value = reun->curr->aft;
clone[i][j].value = reun->curr->aft;
printf("Cell <%d,%d> changed to %d.\n", j+1, i+1, board[i][j].value);
}
else
{
i = reun->curr->row;
j = reun->curr->column;
board[i][j].value = reun->curr->aft;
clone[i][j].value = reun->curr->aft;
reun->curr = reun->curr->next;
printf("Cell <%d,%d> changed to %d.\n", j+1, i+1, board[i][j].value);
while(reun->curr->change == 2)
{
i = reun->curr->row;
j = reun->curr->column;
board[i][j].value = reun->curr->aft;
clone[i][j].value = reun->curr->aft;
reun->curr = reun->curr->next;
printf("Cell <%d,%d> changed to %d.\n", j+1, i+1, board[i][j].value);
}
reun->curr = reun->curr->prev;
}
}
void freeList(linkedList* list)
{
deleteAll(list);
free(list->first);
free(list->last);
free(list);
}