-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedList.c
156 lines (136 loc) · 3.46 KB
/
LinkedList.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include "LinkedList.h"
List *newList(){
List *lista;
lista = (List *)malloc(sizeof(List));
lista -> size = 0;
lista -> first = NULL;
lista -> end = NULL;
return lista;
}
Item *newItem(Monom *value){
Item *newp;
newp=(Item*)malloc(sizeof(Item));
newp -> value = value;
newp -> next = NULL;
return newp;
}
void addOn(List *lista, Monom *value, int index){
//caso ser o primeiro
Item *aux = lista -> first;
if(index == 0){
lista -> first = newItem(value);
lista -> first -> next = aux;
lista -> size = lista -> size + 1;
return;
}
Item *newp = aux -> next;
int i = 0;
while (newp != NULL) {
if(++i == index){
aux -> next = newItem(value);
aux -> next -> next = newp;
}
aux = newp;
newp = newp -> next;
}
//tratar se for no fim
if((lista -> size) == index){
aux -> next = newItem(value);
lista -> end = aux -> next;
}
//atualizar o size
lista -> size = lista -> size + 1;
}
void add(List *lista, Monom *value){
Item *newp = lista -> end;
//criacao do novo item
if(newp == NULL){
lista -> first = newItem(value);
lista -> end = lista -> first ;
}
else{
newp -> next = newItem(value);
lista -> end = newp -> next;
}
//incremento do tamanho da lista
lista -> size = lista -> size + 1;
}
void removeItem(List *lista, int index){
//verificar se o array encontra s edentros do limites
if(lista->size<=index){
exit(0);
}
int count = 0;
Item *newp = lista -> first;
//eliminar o primeiro no
if(index == 0){
lista -> first = newp -> next;
free(newp);
lista -> size = lista -> size--;
return;
}
//eliminar no meio
while (newp -> next -> next != NULL) {
if(++count==index){
Item *aux = newp -> next;
newp -> next = aux -> next;
free(aux);
break;
}
newp = newp -> next;
}
if(++count==index){
newp -> next = NULL;
}
lista -> size = lista -> size--;
}
//Debug of list
void printList(List *lista){
Item *newp = lista -> first;
if(newp == NULL)
return;
//CASO INICIAL
switch (newp->value->flag) {
case Expre:
if(newp->value->aux == -1)
printf("%.2f*%c^%d ",newp->value->val->exp->coeficiente,newp->value->val->exp->variavel,newp->value->val->exp->expoente );
else
printf("%.2f*%c*%c^%d ",newp->value->val->exp->coeficiente,newp->value->val->exp->variavel,newp->value->aux,newp->value->val->exp->expoente );
break;
case Consta:
printf("%d ",newp->value->val->constante );
break;
}
newp = newp -> next;
while (newp != NULL) {
switch (newp->value->flag) {
case Expre: //exp->
if(newp->value->aux == -1)
printf("+ %.2f*%c^%d ",newp->value->val->exp->coeficiente,newp->value->val->exp->variavel,newp->value->val->exp->expoente );
else
printf("+ %.2f*%c*%c^%d ",newp->value->val->exp->coeficiente,newp->value->val->exp->variavel,newp->value->aux,newp->value->val->exp->expoente );
//printf("Estou aqui");
break;
case Consta:
printf("+ %d ",newp->value->val->constante );
break;
}
newp = newp -> next;
}
putchar('\n');
}
/*
int main(int argc, char const *argv[]) {
int n;
List *lista = newList();
scanf("%d",&n );
while (n != -1) {
add(lista,n);
scanf("%d",&n );
}
printList(lista);
removeItem(lista,0);
printList(lista);
return 0;
}
*/