1
+ #include <stdio.h>
2
+ #include <time.h>
3
+ #include <string.h>
4
+ #include <stdlib.h>
5
+ #include "vetor.h"
6
+ #include "lista1.h"
7
+
8
+ #define MAX_LEN 128
9
+ #define NPOS 10000
10
+
11
+ int main ()
12
+ {
13
+ int i = 0 ;
14
+ vetor * vec = vetor_novo ();
15
+ lista * lst = lista_nova ();
16
+ clock_t start_t , end_t ;
17
+ FILE * file = fopen ("nomes.txt" , "r" );
18
+ if (!file )
19
+ {
20
+ printf ("Erro a abrir nomes.txt\n" );
21
+ return -1 ;
22
+ }
23
+ char str [MAX_LEN ];
24
+
25
+ /* Inserir todos os nomes no início do vetor */
26
+ start_t = clock ();
27
+ while (1 )
28
+ {
29
+ if (!fgets (str , MAX_LEN , file ))
30
+ break ; // EOF
31
+ str [strlen (str ) - 1 ] = '\0' ;
32
+
33
+ if (vetor_insere (vec , str , 0 ) == -1 ) // insere na primeira posição
34
+ {
35
+ printf ("Erro a inserir no vetor\n" );
36
+ return -1 ;
37
+ }
38
+ }
39
+ end_t = clock ();
40
+ printf ("%d elementos inseridos no início do vetor\n" , vec -> tamanho );
41
+ printf ("tempo: %.3fs\n" , (double )(end_t - start_t ) / CLOCKS_PER_SEC );
42
+ printf ("\n" );
43
+
44
+ fseek (file , 0 , SEEK_SET );
45
+
46
+ /* Inserir todos os nomes no início da lista */
47
+ start_t = clock ();
48
+ while (1 )
49
+ {
50
+ if (!fgets (str , MAX_LEN , file ))
51
+ break ; // EOF
52
+ str [strlen (str ) - 1 ] = '\0' ;
53
+
54
+ if (!lista_insere (lst , str , lista_elemento (lst , 0 ))) // insere na primeira posição
55
+ {
56
+ printf ("Erro a inserir na lista\n" );
57
+ return -1 ;
58
+ }
59
+ }
60
+ end_t = clock ();
61
+ printf ("%d elementos inseridos no início da lista\n" , vec -> tamanho );
62
+ printf ("tempo: %.3fs\n" , (double )(end_t - start_t ) / CLOCKS_PER_SEC );
63
+ printf ("\n" );
64
+
65
+ fseek (file , 0 , SEEK_SET );
66
+
67
+ /* Aceder a NPOS posições aleatórias do vetor */
68
+ int pos [NPOS ];
69
+ for (int i = 0 ; i < NPOS ; i ++ )
70
+ pos [i ] = rand ()%(vec -> tamanho );
71
+
72
+ start_t = clock ();
73
+ for (int i = 0 ; i < NPOS ; i ++ )
74
+ {
75
+ if (!vetor_elemento (vec , pos [i ]))
76
+ {
77
+ printf ("Erro a ler vetor\n" );
78
+ return -1 ;
79
+ }
80
+ }
81
+ end_t = clock ();
82
+ printf ("%d elementos lidos do vetor\n" , NPOS );
83
+ printf ("tempo: %.3fs\n" , (double )(end_t - start_t ) / CLOCKS_PER_SEC );
84
+ printf ("\n" );
85
+
86
+ /* Aceder a NPOS posições aleatórias da lista */
87
+ start_t = clock ();
88
+ for (int i = 0 ; i < NPOS ; i ++ )
89
+ {
90
+ if (!lista_elemento (lst , pos [i ]))
91
+ {
92
+ printf ("Erro a ler lista\n" );
93
+ return -1 ;
94
+ }
95
+ }
96
+ end_t = clock ();
97
+ printf ("%d elementos lidos da lista\n" , NPOS );
98
+ printf ("tempo: %.3fs\n" , (double )(end_t - start_t ) / CLOCKS_PER_SEC );
99
+ printf ("\n" );
100
+
101
+ /* Remover todos os elementos do vetor a partir do início */
102
+ int vec_tamanho = vec -> tamanho ;
103
+ start_t = clock ();
104
+ for (int i = 0 ; i < vec_tamanho ; i ++ )
105
+ {
106
+ if (vetor_remove (vec , 0 ) == -1 )
107
+ {
108
+ printf ("Erro a remover elemento de vetor\n" );
109
+ return -1 ;
110
+ }
111
+ }
112
+ end_t = clock ();
113
+ printf ("%d elementos removidos do vetor\n" , i );
114
+ printf ("tempo: %.3fs\n" , (double )(end_t - start_t ) / CLOCKS_PER_SEC );
115
+ printf ("\n" );
116
+
117
+ /* Remover todos os elementos do vetor a partir do início */
118
+ int lst_tamanho = lst -> tamanho ;
119
+ start_t = clock ();
120
+ for (int i = 0 ; i < lst_tamanho ; i ++ )
121
+ {
122
+ if ((!lista_remove (lst , lista_elemento (lst , 0 ))) && (lst -> tamanho > 0 ))
123
+ {
124
+ printf ("Erro a remover elemento de lista %d\n" , i );
125
+ return -1 ;
126
+ }
127
+ }
128
+ end_t = clock ();
129
+ printf ("%d elementos removidos da lista\n" , i );
130
+ printf ("tempo: %.3fs\n" , (double )(end_t - start_t ) / CLOCKS_PER_SEC );
131
+ printf ("\n" );
132
+
133
+ /* Inserir todos os nomes no fim do vetor */
134
+ start_t = clock ();
135
+ while (1 )
136
+ {
137
+ if (!fgets (str , MAX_LEN , file ))
138
+ break ; // EOF
139
+ str [strlen (str ) - 1 ] = '\0' ;
140
+
141
+ if (vetor_insere (vec , str , -1 ) == -1 ) // insere na última posição
142
+ {
143
+ printf ("Erro a inserir no vetor\n" );
144
+ return -1 ;
145
+ }
146
+ }
147
+ end_t = clock ();
148
+ printf ("%d elementos inseridos no fim do vetor\n" , vec -> tamanho );
149
+ printf ("tempo: %.3fs\n" , (double )(end_t - start_t ) / CLOCKS_PER_SEC );
150
+ printf ("\n" );
151
+
152
+ fseek (file , 0 , SEEK_SET );
153
+
154
+ /* Inserir todos os nomes no fim da lista */
155
+ start_t = clock ();
156
+ while (1 )
157
+ {
158
+ if (!fgets (str , MAX_LEN , file ))
159
+ break ; // EOF
160
+ str [strlen (str ) - 1 ] = '\0' ;
161
+
162
+ if (!lista_insere (lst , str , NULL )) // insere na última posição
163
+ {
164
+ printf ("Erro a inserir na lista\n" );
165
+ return -1 ;
166
+ }
167
+ }
168
+ end_t = clock ();
169
+ printf ("%d elementos inseridos no fim da lista\n" , vec -> tamanho );
170
+ printf ("tempo: %.3fs\n" , (double )(end_t - start_t ) / CLOCKS_PER_SEC );
171
+ printf ("\n" );
172
+
173
+ fclose (file );
174
+ vetor_apaga (vec );
175
+ lista_apaga (lst );
176
+
177
+ return 0 ;
178
+ }
0 commit comments