-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinkedList.c
163 lines (125 loc) · 4.3 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
157
158
159
160
161
162
#include "LinkedList.h"
#include "MassageCases.h"
//Mostra a lista de produtos
void print_list_products(struct node* head) {
struct node* p = head;
while (p != NULL) {
printf("ID:%d, %s : %.2f$ \n", p->data.code, p->data.description, p->data.price);
p = p->next;
}
}
//Mostra a lista das vendas
void print_list_sales(struct node* head) {
struct node* p = head;
while (p != NULL) {
printf("ID:%d, %s : %.2f$, Quantidade: %d Data: %s \n", p->data.code, p->data.description, p->data.price, p->data.quantity, p->data.date);
p = p->next;
}
}
//Procura o produto por código
int search_by_code(struct node* head, int code, short printData) {
struct node* temp = head;
while (temp != NULL) {
if (temp->data.code == code) {
if(printData)
printf("\n<-------------Resultado Encontrado---------------->\nid: %d, Descricao: %s, Preco: %.2f$\n<------------------------------------------------->\n", temp->data.code, temp->data.description, temp->data.price);
return 1;
}
temp = temp->next;
}
return 0;
}
//Procura o produto por descricao
int search_by_description(struct node* head, char* desc, short printData) {
struct node* temp = head;
//Por todos os chars em lowercase
const char* lowerDesc = toLower(desc);
while (temp != NULL) {
char* tempLowerCase = toLower(temp->data.description);
if (!strcmp(lowerDesc, tempLowerCase)){
if (printData)
printf("\n<-------------Resultado Encontrado---------------->\nid: %d, Descricao: %s, Preco: %.2f$\n<------------------------------------------------->\n", temp->data.code, temp->data.description, temp->data.price);
return 1;
}
temp = temp->next;
}
return 0;
}
//Adiciona um nó à linked list
int push(struct node** head, struct Product data) {
//criar a ligação
struct node* temp = (struct node*)malloc(sizeof(struct node));
//Verifica se a memória foi alocada
if (temp == NULL) {
printf("\n[ERRO] Memória não alocada.\n");
return 0;
}
temp->data = data;
// aponta para o primeiro elemento da lista
temp->next = *head;
//assignamos o atual ao primeiro da lista
*head = temp;
return 1;
}
//Obtem os dados do produto do código inserido e retorna o valor da estrutura data
struct Product get_node_data(struct node* head, int code) {
struct node* temp = head;
while (temp != NULL) {
if (temp->data.code == code) {
return temp->data;
}
temp = temp->next;
}
return;
}
//Edita os dados do nó
void edit_node_data(struct node* head, int code, struct Product editData) {
struct node* temp = head;
while (temp != NULL) {
if (temp->data.code == code) {
//Copiamos os dados da estrutura para o nó que queremos
temp->data.code = editData.code;
strcpy(temp->data.description, editData.description);
temp->data.price = editData.price;
strcpy(temp->data.date, editData.date);
temp->data.quantity = editData.quantity;
printf("\n<-------------Resultado Editado------------------->\nid: %d, Descricao: %s, Preco: %.2f$", temp->data.code, temp->data.description, temp->data.price);
if (temp->data.date != NULL) {
printf("Quantidade: %d Data: %s\n<------------------------------------------------->\n", temp->data.quantity, temp->data.date);
}
return 1;
}
temp = temp->next;
}
return 0;
}
//Apaga o nó da Linked List(LL)
int delete_node(struct node* head, int code) {
struct node* temp = head, *prev = NULL;
if (temp != NULL && temp->data.code == code) {
head = temp->next;
free(temp);
return 1;
}
while (temp != NULL && temp->data.code != code) {
prev = temp;
temp = temp->next;
}
if (temp == NULL) {
printf("[ERRO] Produto nao encontrado\n");
return 0;
}
prev->next = temp->next;
free(temp);
return 1;
}
void delete_list(struct node* head) {
struct node* current = head;
struct node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}
head = NULL;
}