Está en la página 1de 4

Max Bruno Saavedra Monterrey; Código: 21200259

Listas Doblemente enlazadas

#include <iostream>
using namespace std;

// Estructura para el nodo de la lista doblemente enlazada


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

// Prototipos de funciones
Node* getNewNode(int);
Node* createList();
void destroyList(Node*);
void displayList(Node*);
Node* insertNode(Node*, int, int);
Node* deleteNode(Node*, int);
void showMenu();

int main() {
Node* head = NULL;
int choice, data, pos;
do {
showMenu();
cout << "Ingrese su eleccion: ";
cin >> choice;
switch (choice) {
case 1:
head = createList();
break;
case 2:
destroyList(head);
head = NULL;
break;
case 3:
cout << "Ingrese el valor del nodo: ";
cin >> data;
cout << "Ingrese la posicion: ";
cin >> pos;
head = insertNode(head, data, pos);
break;
case 4:
cout << "Ingrese la posicion: ";
cin >> pos;
head = deleteNode(head, pos);
break;
case 5:
displayList(head);
break;
case 6:
cout << "Saliendo del programa..." << endl;
break;
default:
cout << "Opcion invalida. Por favor intente de nuevo." << endl;
}
} while (choice != 6);
return 0;
}

// Funcion para mostrar el menu


void showMenu() {
cout << "========== MENU ==========" << endl;
cout << "1. Crear lista doble enlazada" << endl;
cout << "2. Destruir lista doble enlazada" << endl;
cout << "3. Insertar un nodo" << endl;
cout << "4. Eliminar un nodo" << endl;
cout << "5. Mostrar lista" << endl;
cout << "6. Salir" << endl;
}

// Funcion para crear un nuevo nodo de la lista doblemente enlazada


Node* getNewNode(int data) {
Node* newNode = new Node;
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}

// Funcion para crear la lista doblemente enlazada


Node* createList() {
int n, data;
Node* head = NULL;
Node* tail = NULL;
cout << "Ingrese el numero de nodos: ";
cin >> n;
for (int i = 0; i < n; i++) {
cout << "Ingrese el valor del nodo " << i + 1 << ": ";
cin >> data;
Node* newNode = getNewNode(data);
if (head == NULL) {
head = newNode;
tail = newNode;
} else {
tail->next = newNode;
newNode->prev = tail;
tail = newNode;
}
}
return head;
}

// Funcion para destruir la lista doblemente enlazada


void destroyList(Node* head) {
Node* temp;
while (head != NULL) {
temp = head;
head = head->next;
delete temp;
}
}

// Funcion para mostrar la lista doblemente enlazada


void displayList(Node* head) {
if (head == NULL) {
cout << "La lista esta vacia." << endl;
return;
}
Node* temp = head;
cout << "La lista es: ";
while (temp != NULL) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}

// Función para insertar un nodo en la lista doblemente enlazada


Node* insertNode(Node* head, int data, int pos) {
Node* newNode = getNewNode(data);
if (head == NULL) { // Lista vacía
head = newNode;
} else if (pos == 1) { // Insertar en la cabeza
newNode->next = head;
head->prev = newNode;
head = newNode;
} else { // Insertar en cualquier posición diferente de la cabeza
int i = 1;
Node* temp = head;
while (i < pos - 1 && temp->next != NULL) {
temp = temp->next;
i++;
}
if (i == pos - 1) { // Posición válida
newNode->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = newNode;
}
temp->next = newNode;
newNode->prev = temp;
} else { // Posición inválida
cout << "La posicion " << pos << " no existe en la lista." << endl;
}
}
return head;
}
// Función para eliminar un nodo de la lista doblemente enlazada
Node* deleteNode(Node* head, int pos) {
if (head == NULL) { // Lista vacía
cout << "La lista esta vacia." << endl;
} else if (pos == 1) { // Eliminar la cabeza
Node* temp = head;
head = head->next;
if (head != NULL) {
head->prev = NULL;
}
free(temp);
} else { // Eliminar cualquier nodo diferente de la cabeza
int i = 1;
Node* temp = head;
while (i < pos && temp != NULL) {
temp = temp->next;
i++;
}
if (temp != NULL) { // Posición válida
temp->prev->next = temp->next;
if (temp->next != NULL) {
temp->next->prev = temp->prev;
}
free(temp);
} else { // Posición inválida
cout << "La posicion " << pos << " no existe en la lista." << endl;
}
}
return head;
}

También podría gustarte