-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbucket.c
91 lines (80 loc) · 1.92 KB
/
bucket.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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "bucket.h"
#include <stdbool.h>
Node * createNode(int offset, char name[], char type[]){
Node * node = (Node * ) malloc(sizeof(Node));
node ->offset = offset;
strcpy(node->name, name);
strcpy(node->type,type);
node->next = NULL;
return node;
}
Bucket * createBucket() {
Bucket * bucket = (Bucket *) malloc(sizeof(Bucket));
bucket->count = 0;
bucket->head = NULL;
bucket->tail = NULL;
return bucket;
}
int append(Bucket * bucket, int offset, char name[], char type[]){
Node * node = createNode(offset, name, type);
if(bucket->count == 0){
bucket->head = node;
bucket->tail = node;
}else{
(bucket->tail)->next = node;
bucket->tail = node;
}
bucket->count = bucket->count +1;
return bucket->count;
}
int search(Bucket * bucket, char * name){
Node * curr = bucket->head;
int i = 0;
while(curr != NULL){
if(strcmp(curr->name, name) == 0){
return i;
}
i++;
curr = curr->next;
}
return -1;
}
Node * get(Bucket * bucket, char * name){
Node * curr = bucket->head;
while(curr != NULL){
if(strcmp(curr->name, name) == 0){
break;
}
curr = curr->next;
}
return curr;
}
bool hasElement(Bucket * bucket, char * name){
return search(bucket, name) > -1;
}
int size(Bucket * bucket){
return bucket->count;
}
void print(Bucket * bucket){
if(bucket->count == 0){
printf("Empty\n");
return;
}
Node * curr = bucket->head;
while (curr != NULL){
printf("(%s, %d, %s)\n", curr->name, curr->offset, curr->type);
curr = curr->next;
}
}
void clearBucket(Bucket * bucket){
Node * curr = bucket->head;
while(curr != NULL){
Node * temp = curr;
curr = curr->next;
free(temp);
}
free(bucket);
}