forked from huffz/Huffman_Coding
-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchlist.c
118 lines (103 loc) · 2.18 KB
/
searchlist.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
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef struct node{
long long int item;
struct node* next;
}Node;
/*
Node* createLinkedList();
Node* insertNode(Node *node, int item);
void printLinkedList(Node *first);
int isEmpty(node * first);*/
Node* createLinkedList(){
return NULL;
}
Node* createNode(long long int item){ //insercao na head
Node* new = (Node*) malloc(sizeof(Node));
new->item = item;
new->next = NULL;
return new;
}
Node *insert(Node *head, long long int item){
Node *new = createNode(item);
if(head == NULL) return new;
if(head != NULL)
{
Node *current = head;
while(current->next != NULL)
{
current = current->next;
}
current->next = new;
return head;
}
}
long long int comparisons=0;
void printLinkedList(Node *first){
while(first != NULL){
printf("%lld ",first->item);
first = first->next;
}
printf("\n");
//return NULL;
}
Node* search(Node* first, long long int item){
while(first != NULL){
if(first->item == item){
//printf("It is here!\n");
comparisons++;
return first;
}
comparisons++;
first = first->next;
}
//printf("Not found\n");
return NULL;
}
Node* removenode(Node* first, long long int item){
Node *previous = NULL;
Node *current = first;
while(current != NULL && current->item != item){
previous = current;
current = current->next;
}
if(current == NULL){
return first;
}
if(previous == NULL){
first = current->next;
} else {
previous->next = current->next;
}
free(current);
return first; //remember it is a recursive method so we have to return first;
}
int isEmpty(Node * first){
return (first == NULL);
}
Node *free_list(Node *head)
{
Node *aux = head;
while(aux != NULL
) {
head = head -> next;
free(aux);
aux = head;
}
return aux
;}
int main(){
Node* list = createLinkedList();
long long int n,size=0;
printf("num_nodes n_comparisons\n");
while(scanf("%lld", &n) != EOF)//le a entrada
{
list = insert(list, n);
search(list, n);//busca sempre n
size++;//apos n ser adicionado o num de nodes da list aumenta
printf("%lld %lld\n",size, comparisons);//imprime no inputabb.txt o tamanho da entrada e o num de comparisons
comparisons=0;
}
list = free_list(list);
}