-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
121 lines (91 loc) · 2.07 KB
/
main.c
File metadata and controls
121 lines (91 loc) · 2.07 KB
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
119
120
121
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct no_lista {
int data;
struct no_lista* proximo;
}no;
no *cabeca=NULL;
no *criarNo(int data) {
no *temporario = (no *) malloc(sizeof(no));
temporario->data = data;
temporario->proximo = NULL;
return temporario;
}
no *inserirNo(no *x, no *novoNo) {
novoNo->proximo = x;
cabeca = novoNo;
return novoNo;
}
void imprimeLista(no *lista) {
no *tmp = lista;
while (tmp!= NULL) {
printf("%d -> ", tmp->data);
tmp = tmp->proximo;
}
}
void inserirAposNo(no *noAInserir, no* noNovo) {
noNovo->proximo = noAInserir->proximo;
noAInserir->proximo = noNovo;
}
no *buscarNo(no *x, int value) {
no *tmp = x;
while(tmp!=NULL) {
if(tmp->data == value) {
return tmp;
}
tmp = tmp->proximo;
}
return NULL;
}
no *buscarNo2(int n) {
no *temp1 = cabeca;
if(n==1) {
// cabeca = temp1->proximo;
// free(temp1);
// return cabeca;
}
else {
int i;
for(i=0;i<n-1;i++) {
temp1 = temp1->proximo;
}
// no *temp2 = temp1->proximo;
// temp1->proximo = temp2->proximo;
// free(temp2);
}
return temp1;
}
void Delete(int n) {
no *temp1 = cabeca;
if(n==1) {
cabeca = temp1->proximo;
free(temp1);
return;
}
int i;
for(i=0;i<n-2;i++) {
temp1 = temp1->proximo;
}
no *temp2 = temp1->proximo;
temp1->proximo = temp2->proximo;
free(temp2);
}
int main(){
int numeros[] = {3, 2, 8, 6, 4, 4, 12};
no *no;
for (int i=0; i<7; i++){
no = criarNo(numeros[i]);
inserirNo(cabeca, no);
}
imprimeLista(cabeca);
printf("\n");
Delete(5);
no = buscarNo(cabeca, 2);
printf("No encontrado %d\n", no->data);
inserirAposNo(no, criarNo(25));
imprimeLista(cabeca);
printf("\n");
system("PAUSE");
return 0;
}