-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASSG2_B190513CS_HADIF_2.c
87 lines (80 loc) · 1.6 KB
/
ASSG2_B190513CS_HADIF_2.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
typedef struct node{
char *word;
struct node *next;
} *Node;
Node insert(Node, char*);
void print(Node*, int);
Node createNode(char*);
int mystrcmp(char*, char*);
int main()
{
int k, c = 0;
char s[500];
char *word = (char*)calloc(50, sizeof(char));
scanf("%d\n", &k);
fgets(s, 500, stdin);
Node *T = (Node*)calloc(k, sizeof(Node));
for(int i=0; s[i]; i++)
{
if(s[i]==' ' || s[i]=='\n')
{
word[c] = '\0';
int j = (c*c)%k;
T[j] = insert(T[j], word);
c = 0;
word = (char*)calloc(50, sizeof(char));
}
else
word[c++] = s[i];
}
print(T, k);
return 0;
}
Node createNode(char* word)
{
Node w = (Node)malloc(sizeof(Node));
w->word = word;
w->next = NULL;
return w;
}
Node insert(Node T, char *word)
{
if(!T)
return T = createNode(word);
Node t = T, prev = NULL;
while(t && mystrcmp(t->word, word))
{
prev = t;
t = t->next;
}
if(!t)
prev->next = createNode(word);
return T;
}
void print(Node *T, int k)
{
for(int i=0; i<k; i++)
{
printf("%d:", i);
Node t = T[i];
if(!t)
printf("null ");
while(t)
{
printf("%s-", t->word);
t = t->next;
}
printf("\b \n");
}
}
int mystrcmp(char* word1, char* word2)
{
for(int i=0; word1[i] && word2[i]; i++)
if(tolower(word1[i]) != tolower(word2[i]))
return 1;
return 0;
}