-
Notifications
You must be signed in to change notification settings - Fork 1
/
cell.h
191 lines (148 loc) · 4.16 KB
/
cell.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
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
#include <stdio.h>
#include <stdlib.h>
struct Cell {
uint val;
struct Cell *next;
struct Cell *prev;
};
void print_tape(struct Cell *head) {
/*
Prints the tape (doubly linked list containing all cells)
args:
head - head of the tape (doubly linked list containing all cells)
*/
struct Cell *dummy = head;
while(dummy->prev != NULL)
dummy = dummy->prev;
while(dummy != NULL) {
printf("%d ", dummy->val);
dummy = dummy->next;
}
printf("\n\n");
}
struct Cell *move(struct Cell *head, char dir) {
/*
Moves the head of the tape (doubly linked list containing all cells)
args:
head - head of the tape (doubly linked list containing all cells)
dir - direction to move
L - move left
R - move right
S - stay
if excluded options are given the program will exit
(the validate_cards function will take care of this)
ret:
returns the head of the pointer moved to the specified direction
If when the shifted cell is non-exixting it creates a new cell and initialises
its valus to '0' and adds to the tape (doubly linked list containing all cells)
*/
if(dir == 'R') {
if(head->next == NULL) {
struct Cell *temp = (struct Cell*)malloc(sizeof(struct Cell));
if(temp == NULL) {
printf("System out of memory");
exit(0);
}
temp->val = 0;
head->next = temp;
temp->prev = head;
temp->next = NULL;
}
head = head->next;
} else if(dir == 'L') {
if(head->prev == NULL) {
struct Cell *temp = (struct Cell *)malloc(sizeof(struct Cell));
if(temp == NULL) {
printf("System out of memory");
exit(0);
}
temp->val = 0;
head->prev = temp;
temp->next = head;
temp->prev = NULL;
}
head = head->prev;
} else if(dir == 'S') {
} else {
printf("Invalid Direction\n");
exit(0);
}
return head;
}
void get_initial_tape(struct Cell *head, char *file) {
/*
Creates the initial tape (doubly linked list) from the file with the
specified format
args:
head - the one cell created at the beginning
file - the file with the specified format to create tape
*/
FILE *fptr = fopen(file, "r");
if(fptr == NULL) {
printf("Error reading file\n");
exit(0);
}
struct Cell *dummy = head;
char temp;
while(fscanf(fptr, "%c", &temp) != EOF) {
dummy->val = temp - '0';
dummy = move(dummy, 'R');
fscanf(fptr, "%c", &temp);
}
dummy = move(dummy, 'L');
free(dummy->next);
dummy->next = NULL;
}
uint validate_tape(struct Cell *head) {
/*
Validates the tape (the value of each cell must be '0'/'1')
args:
head - head of the tape (doubly linked list containing all cells)
*/
struct Cell* dummy = head;
while(dummy != NULL) {
if(dummy->val != 0 && dummy->val != 1) {
printf("Invalid Tape!\n");
return 0;
}
dummy = dummy->next;
}
printf("Valid tape\n\n");
return 1;
}
uint tape_count_ones(struct Cell *head) {
/*
Counts the number of ones in the tape (doubly linked list containing all cells)
args:
head - head of the tape (doubly linked list containing all cells)
ret:
count - number of ones in the tape
*/
uint count = 0;
struct Cell *dummy = head;
while(dummy != NULL) {
if(dummy->val == 1)
count++;
dummy = dummy->prev;
}
dummy = head->next;
while(dummy != NULL) {
if(dummy->val == 1)
count++;
dummy = dummy->next;
}
return count;
}
void free_tape(struct Cell *head) {
/*
Frees the memory of cells
args:
head - pointer to the head of the cell
*/
struct Cell *temp1 = head, *temp2;
while(temp1 != NULL) {
temp2 = temp1->next;
free(temp1);
temp1 = temp2;
}
}