Você está na página 1de 7

UNIVERSIDADE VEIGA DE ALMEIDA – UVA

ESTRUTURA DE DADOS
DIEGO LOPES DO NASCIMENTO
MATRÍCULA: 1220201496
POLO MADUREIRA
RIO DE JANEIRO – RJ
NOVEMBRO, 2023

CÓDIGO:

#include <stdio.h>
#include <stdlib.h>

// Definição da estrutura do nó da lista


struct Node {
int data;
struct Node* next;
};

// Função para criar um novo nó


struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
if (newNode == NULL) {
printf("Erro ao alocar memória para o novo nó.\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

// Função para inserir um novo nó no início da lista


void insertNode(struct Node** head, int data) {
struct Node* newNode = createNode(data);
newNode->next = *head;
*head = newNode;
printf("Nó com valor %d inserido com sucesso.\n", data);
}

// Função para exibir os elementos da lista


void displayList(struct Node* head) {
struct Node* current = head;
printf("Lista: ");
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
// Função para verificar se a lista está vazia
int isEmpty(struct Node* head) {
return (head == NULL);
}

// Função para procurar um elemento na lista


struct Node* searchNode(struct Node* head, int key) {
struct Node* current = head;
while (current != NULL) {
if (current->data == key) {
printf("Elemento %d encontrado na lista.\n", key);
return current;
}
current = current->next;
}
printf("Elemento %d não encontrado na lista.\n", key);
return NULL;
}

// Função para alterar o valor de um nó na lista


void modifyNode(struct Node* head, int oldKey, int newKey) {
struct Node* nodeToModify = searchNode(head, oldKey);
if (nodeToModify != NULL) {
nodeToModify->data = newKey;
printf("Valor do elemento %d alterado para %d.\n", oldKey, newKey);
}
}

// Função para remover um nó da lista


void deleteNode(struct Node** head, int key) {
struct Node *current = *head, *prev = NULL;

// Caso o nó a ser removido seja o primeiro


if (current != NULL && current->data == key) {
*head = current->next;
free(current);
printf("Elemento %d removido da lista.\n", key);
return;
}

// Buscar o nó a ser removido


while (current != NULL && current->data != key) {
prev = current;
current = current->next;
}

// Se o nó não foi encontrado


if (current == NULL) {
printf("Elemento %d não encontrado na lista.\n", key);
return;
}

// Remover o nó da lista
prev->next = current->next;
free(current);
printf("Elemento %d removido da lista.\n", key);
}

// Função principal
int main() {
struct Node* head = NULL; // Inicializar a lista vazia

// Menu de operações
int choice, value, oldValue, newValue;
do {
printf("\nEscolha a operacao:\n");
printf("1. Incluir\n");
printf("2. Consultar\n");
printf("3. Alterar\n");
printf("4. Remover\n");
printf("5. Exibir Lista\n");
printf("0. Sair\n");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Digite o valor a ser incluido: ");
scanf("%d", &value);
insertNode(&head, value);
break;
case 2:
printf("Digite o valor a ser consultado: ");
scanf("%d", &value);
searchNode(head, value);
break;
case 3:
printf("Digite o valor a ser alterado: ");
scanf("%d", &oldValue);
printf("Digite o novo valor: ");
scanf("%d", &newValue);
modifyNode(head, oldValue, newValue);
break;
case 4:
printf("Digite o valor a ser removido: ");
scanf("%d", &value);
deleteNode(&head, value);
break;
case 5:
displayList(head);
break;
case 0:
printf("Saindo do programa.\n");
break;
default:
printf("Escolha invalida. Tente novamente.\n");
}
} while (choice != 0);

// Liberar a memória alocada para os nós antes de sair do programa


struct Node* current = head;
struct Node* next;
while (current != NULL) {
next = current->next;
free(current);
current = next;
}

return 0;
}

Você também pode gostar