Skip to content

Commit 313df26

Browse files
committed
commit
1 parent ec387d2 commit 313df26

File tree

11 files changed

+22594
-12
lines changed

11 files changed

+22594
-12
lines changed

Pt/ex11 Pt/ex1

21.4 KB
Binary file not shown.

Pt/ex11.c Pt/ex1.c

+9-6
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@ int main()
3939

4040
lista_imprime(l);
4141
printf("\n");
42-
42+
//fim 1d inicio 1e
4343
lista_ordena(l);
4444
printf("\nLista ordenada: \n");
45-
4645
lista_imprime(l);
46+
//fim 1e inicio 1f
47+
4748
int i=0;
4849
l_elemento *jogo_pesquisa = lista_pesquisa(l, "Duke Nukem (PS3)", LISTA_INICIO);
4950
if (!jogo_pesquisa)
@@ -55,18 +56,20 @@ int main()
5556
{
5657
if (lista_elemento(l, i)== jogo_pesquisa)
5758
{
58-
printf("\nO jogo Duke Nukem (PS3) encontra-se na posição %d.\n", i);
59+
printf("\nO jogo %s encontra-se na posição %d.\n",jogo_pesquisa->str, i);
5960
break;
6061
}
6162
i++;
6263

6364
}
64-
65-
66-
65+
//fim 1f inicio 1g
66+
printf("O jogo Minesweeper (PC) foi inserido na posição %d\n\n", lista_insere_ordenada(l, "Minesweeper (PC) "));
67+
//lista_imprime(l);
6768

6869
lista_apaga(l);
6970
lista_apaga(substring);
7071
lista_apaga(lst2);
72+
73+
7174
return 0;
7275
}

Pt/ex2

26.1 KB
Binary file not shown.

Pt/ex2.c

+178
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
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+
}

Pt/lista1.c

+34-6
Original file line numberDiff line numberDiff line change
@@ -334,19 +334,18 @@ lista *lst_pesq_sub_remo(lista *lst, char *string)
334334
lista *lista_concatena(lista *lst1, lista *lst2)
335335
{
336336

337-
l_elemento *elemento=lst2->inicio;
338-
339-
if (lst1 == NULL || lst2==NULL)
337+
l_elemento *elemento = lst2->inicio;
338+
339+
if (lst1 == NULL || lst2 == NULL)
340340
{
341341
return NULL;
342342
}
343343

344-
while (elemento!=NULL)
344+
while (elemento != NULL)
345345
{
346346
lista_insere(lst1, elemento->str, NULL);
347-
elemento=elemento->proximo;
347+
elemento = elemento->proximo;
348348
}
349-
350349

351350
return lst1;
352351
}
@@ -372,4 +371,33 @@ lista *lista_importa(lista *lst, char *nome)
372371
}
373372
fclose(f);
374373
return lst;
374+
}
375+
int lista_insere_ordenada(lista *lst, char *inserir)
376+
{
377+
if (lst == NULL || inserir == NULL)
378+
{
379+
return -1;
380+
}
381+
l_elemento *aponta;
382+
for (int i = 0; i < lst->tamanho; i++)
383+
{
384+
aponta=lista_elemento(lst, i);
385+
if (strcmp(aponta->str, inserir) == 0)// a string ja existe
386+
{
387+
return -1;
388+
}
389+
390+
else if (strcmp(aponta->str, inserir) > 0)
391+
{
392+
393+
lista_insere(lst, inserir, aponta);
394+
return i;
395+
}
396+
397+
}
398+
if (!lista_insere(lst, inserir, NULL))
399+
{
400+
return -1;}
401+
return lst->tamanho;
402+
375403
}

Pt/lista1.h

+3
Original file line numberDiff line numberDiff line change
@@ -133,5 +133,8 @@ lista *lista_concatena(lista *lst1, lista *lst2);
133133

134134

135135
lista *lista_importa(lista *lst, char *nome);
136+
137+
138+
int lista_insere_ordenada(lista *lst, char *inserir);
136139
#define LISTA_H
137140
#endif

Pt/lista1.o

488 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)