-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathlab04.c
More file actions
116 lines (89 loc) · 2.57 KB
/
lab04.c
File metadata and controls
116 lines (89 loc) · 2.57 KB
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
// lab04.c - starter code for Lab 4
/*
* Convert your program from Lab 3 to use a linked list instead of an array.
* Use this code as a starting point.
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_COMMAND_SIZE 3
struct node {
int line;
char command[MAX_COMMAND_SIZE + 1];
unsigned int argument;
struct node *next;
};
struct node *create_node(int line, char *command, unsigned int argument)
{
assert(strlen(command) <= MAX_COMMAND_SIZE);
struct node *new = malloc(sizeof(struct node));
if (new == NULL) {
fprintf(stderr, "%s:%d: malloc() returned NULL\n", __FILE__, __LINE__);
exit(EXIT_FAILURE);
}
new->line = line;
strcpy(new->command, command);
new->argument = argument;
new->next = NULL;
return new;
}
struct node *append_node(struct node *start, struct node *new)
{
assert(start != NULL);
struct node *current = start;
while (current->next != NULL) {
current = current->next;
}
current->next = new;
return new;
}
void print_node(struct node *n)
{
printf("%2d %s %x\n", n->line, n->command, n->argument);
}
void print_list(struct node *start)
{
struct node *current = start;
while (current != NULL) {
print_node(current);
current = current->next;
}
}
struct node *create_list(void)
{
struct node *head, *next;
int line = 1;
head = create_node(line++, "set", 0x1);
next = append_node(head, create_node(line++, "shl", 0x1));
next = append_node(next, create_node(line++, "shl", 0x1));
next = append_node(next, create_node(line++, "or", 0x1));
next = append_node(next, create_node(line++, "or", 0x2));
next = append_node(next, create_node(line++, "shl", 0x2));
next = append_node(next, create_node(line++, "xor", 0x10));
next = append_node(next, create_node(line++, "xor", 0x10));
next = append_node(next, create_node(line++, "and", 0x4));
next = append_node(next, create_node(line++, "set", 0x1c));
next = append_node(next, create_node(line++, "and", 0x3));
next = append_node(next, create_node(line++, "set", 0x1c));
next = append_node(next, create_node(line++, "shr", 0x1));
return head;
}
void free_list(struct node *start)
{
struct node *prev, *current;
current = start;
while (current != NULL) {
prev = current;
current = current->next;
free(prev);
}
}
int main(void)
{
struct node *head = create_list();
print_list(head);
free_list(head);
return EXIT_SUCCESS;
}