-
Notifications
You must be signed in to change notification settings - Fork 1
/
linkedlist.c
145 lines (131 loc) · 2.76 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
/* Zhishen Wen
* Dec. 10, 2012
* MCIT, Penn CIS
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
* DO NOT CHANGE THIS DEFINITION!
*/
typedef struct Node node;
struct Node {
char name[10];
int offset;
int is_global; // determines whether this node is 'global'
node *next;
};
// global pointer to first Node of Linked List
struct Node *head;
/*
Adds a new node to the linked list, with name set to parameter "s" and offset set to parameter "v".
Returns 1 if successful, 0 if an error occurred.
*/
int add_node(char *s, int v, int g)
{
node *new = malloc(sizeof(node));
if (new == NULL) return 0;
strcpy(new->name, s);
new->offset = v;
new->is_global = g;
new->next = head;
head = new;
return 1;
}
/*
Finds the offset of the node whose name is equal to the first argument.
If the node is found, its offset value is put into the "v" parameter, and the function returns 1.
If an error occurs (e.g. there is no node with that name), the function returns 0.
*/
int get_offset(char *s, int *v)
{
node *n = head;
while(n != NULL) {
if(strcmp(s, n->name) == 0) {
*v = n->offset;
return 1;
}
n = n->next;
}
return 0;
}
/* determines whether node whose name is equal to the argument is 'global';
* returns 1 if it is;
* returns 0 if not;
* returns -1 if no such node found
*/
int get_global_status(char *s)
{
node *n = head;
while(n != NULL) {
if(strcmp(s, n->name) == 0) {
return n->is_global;
}
n = n->next;
}
return -1;
}
// Checks whether the linked list already has the name 's';
// returns 0 if not; 1 otherwise.
int has_name(char *s)
{
node *n = head;
while (n != NULL) {
if(strcmp(s, n->name) == 0)
return 1;
n = n->next;
}
return 0;
}
/* gets the total number of nodes that
* contains non-global var in the linked list
*/
int get_list_length()
{
node *n = head;
int cnt = 0;
while (n != NULL) {
if (n->is_global == 0)
cnt++;
n = n->next;
}
return cnt;
}
/* gets the number of nodes that contains
* local var in the linked list
*/
int get_local_var_number()
{
node *n = head;
int cnt = 0;
while (n != NULL) {
if (n->is_global == 0) {
cnt++;
if (n->offset == 0) return cnt;
}
n = n->next;
}
return 0;
}
/* gets the number of nodes that contains
* param in the linked list
*/
int get_param_number()
{
return get_list_length() - get_local_var_number();
}
/*
Print out (to the screen) the name and offset value for each node
in the linked list.
*/
void iterate()
{
node *n = head;
while (n != NULL) {
printf("Name: %s\t\tOffset: %d\n", n->name, n->offset);
n = n->next;
}
printf("%d\n", get_list_length());
printf("%d\n", get_param_number());
printf("%d\n", get_local_var_number());
}