-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab1.c
103 lines (88 loc) · 2.16 KB
/
lab1.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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
struct Line{
char word[50];
int key;
struct Line *next;
};
//Function for displaying a list on the screen
void printLines(struct Line* head){
struct Line* current = head;
while (current != NULL){
printf("%s %d\n", current->word, current->key);
current = current->next;
}
}
// Function to insert an element into a sorted list
int insertLine(struct Line** head, char word[50], int key){
struct Line* newLine = (struct Line*)malloc(sizeof(struct Line));
strcpy(newLine->word, word);
newLine->key = key;
newLine->next = NULL;
if (*head == NULL || key < (*head)->key){
newLine->next = *head;
*head = newLine;
}else{
struct Line* current = *head;
while (current->next != NULL && key>= current->next->key){
if (key == current->next->key) {
return 1;
}
current = current->next;
}
newLine->next = current->next;
current->next = newLine;
}
return 0;
}
//Function to check the presence of a word by key number
int searchByKey(struct Line* head, int key){
struct Line* current = head;
while (current != NULL){
if (current->key == key){
printf("Word corresponding to key number %d: %s\n",key,current->word);
return -1;
}
current = current->next;
}
printf("Word with key number %d not found.\n",key);
return 0;
}
void Free(struct Line* head){
struct Line* temp;
while(head != NULL){
temp = head;
head = head->next;
free(temp);
}
}
int main(){
struct Line* head = NULL;
char word[50];
int key;
int error;
FILE* file = fopen("input.txt","r");
if (file == NULL){
printf("Failed to open file\n");
return 1;
}
while (fscanf(file, "%s %d",word,&key)!=EOF){
error = insertLine(&head,word,key);
if(error==1){
printf("Error: duplicate keywords\n");
fclose(file);
return 0;
}
}
fclose(file);
printf("Print list of lines:\n");
printLines(head);
int searchKey;
printf("Enter the key number to search: ");
scanf("%d", &searchKey);
searchByKey(head, searchKey);
Free(head);
return 0;
}