-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.c
More file actions
86 lines (71 loc) · 1.8 KB
/
Main.c
File metadata and controls
86 lines (71 loc) · 1.8 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
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* createNode(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
newNode->next = NULL;
return newNode;
}
void addNode(struct Node** head, int value) {
struct Node* newNode = createNode(value);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != NULL) {
temp = temp->next;
}
temp->next = newNode;
}
}
void printList(struct Node* head) {
struct Node* temp = head;
if (temp == NULL) {
printf("A lista está vazia.\n");
return;
}
printf("Lista: ");
while (temp != NULL) {
printf("%d -> ", temp->data);
temp = temp->next;
}
printf("NULL\n");
}
int main() {
struct Node* head = NULL;
int choice, value;
do {
printf("Escolha uma opção:\n");
printf("0 - Sair\n");
printf("1 - Adicionar um novo elemento\n");
printf("2 - Imprimir a lista\n");
printf("Digite sua escolha: ");
scanf("%d", &choice);
switch (choice) {
case 0:
printf("Saindo do programa.\n");
break;
case 1:
printf("Digite o valor do novo elemento: ");
scanf("%d", &value);
addNode(&head, value);
break;
case 2:
printList(head);
break;
default:
printf("Opção inválida. Tente novamente.\n");
}
} while (choice != 0);
struct Node* temp;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
return 0;
}