-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathll-helper.c
More file actions
183 lines (156 loc) · 3.07 KB
/
ll-helper.c
File metadata and controls
183 lines (156 loc) · 3.07 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
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
///////////////////////////////////////
// Author: Vishal Golcha
// Id: 2014B5A70717P
///////////////////////////////////////
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "lexer.h"
char * key_words[100] = {
"end","int","real","string","matrix","if","else","endif",
"read","print","function","_main",".and.",".or.",".not."
};
htable * create_hashtable(int array_size){
htable * x = (htable *)malloc(sizeof(htable));
x->llarray = (llst_str*)malloc(sizeof(llst_str)*array_size);
x->cnt = array_size;
return x;
}
unsigned long hash(char *str){
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash;
}
// unsigned long hash2(char *x){
// }
node * create_node(int y){
node* x = (node *)malloc(sizeof(node));
x->val = y;
x->next= NULL;
return x;
}
/* takes a character pointer as parameter */
str_node *str_create_node(){
str_node* x = (str_node *) malloc(sizeof(node));
x->str_val = (char *)malloc(sizeof(char)*50);
x->next=NULL;
return x;
}
llsti attach(llsti x,int val){
node* y =create_node(val);
if(x.cnt==0){
x.head=y;
x.tail=y;
}
else{
x.tail->next = y;
x.tail = x.tail->next;
}
x.cnt++;
return x ;
}
llsti rev_attach(llsti x,int val){
node* y =create_node(val);
if(x.cnt==0){
x.head=y;
x.tail=y;
}
else{
y->next = x.head;
x.head=y;
}
x.cnt++;
return x ;
}
// check for a need of a function to initialize the stack
llsti llst_pop(llsti x){
if(x.cnt==0){
x.head=NULL;
x.tail=NULL;
}
else{
node *temp = x.head;
x.head=x.head->next;
x.cnt--;
free(temp);
}
return x;
}
node* llst_top(llsti x){
if(x.cnt==0){
return NULL;
}
else{
return x.head;
}
}
llst_str attach_str(llst_str x,char *z,int n){
str_node* y =str_create_node();
strcpy(y->str_val,z);
// printf("%s\n",y->str_val);
y->arrid=n;
if(x.cnt==0){
x.head=y;
x.tail=y;
}
else{
x.tail->next = y;
x.tail = x.tail->next;
}
x.cnt++;
return x ;
}
void llst_traverse(llsti x){
node *y = x.head;
while(y!=NULL){
printf("%d\n",y->val);
y=y->next;
}
}
void llst_traverse_str(llst_str x){
str_node *y = x.head;
while(y!=NULL){
printf("%s\n",y->str_val);
y=y->next;
}
}
htable * insert(htable *z , char * y,int id){
unsigned long h = hash(y);
int x = h%123;
// printf("hash value %d\n",x );
z->llarray[x] = attach_str(z->llarray[x],y,id);
return z;
}
int hfind(htable* z,char *y){
unsigned long h = hash(y);
int x = h%123;
// printf("hash value %d\n",x );
str_node *t = (z->llarray[x]).head;
while(t!=NULL){
// printf("%d\n",y->val);
if(strcmp(t->str_val,y)==0){
return (t->arrid)+1; // retrieve this and index-1 on calls
}
t=t->next;
}
return 0;
// while()
}
/*
int main(){
htable *z = create_hashtable(123);
z = insert(z,"dopey",1);
int res = hfind(z,"dopey");
printf("%d\n",res );
// node *x = create_node(1);
// printf("%d\n",x->val);
// llsti list1 ;
// list1.cnt=0;
// // usage : as follows
// traverse(list1);
// list1 = attach(list1,10);
// list1 = attach(list1,10);
// list1 = attach(list1,20);
} */