Dev - C++ - listas dobles enlazadas

 
Vista:

listas dobles enlazadas

Publicado por ANON (3 intervenciones) el 23/03/2021 21:05:34
Hola me podrían ayudar a como agregar los siguientes puntos a mi código?

- Insertar al inicio de la lista
- Insertar en alguna posición de la lista
- Borrar un nodo según su posición
- Borrar un nodo según su número
- Borrar el primer nodo de la lista
- Borrar el último nodo de la lista

tengo un avance de unos puntos:
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
#include <iostream>
 
using namespace std;
 
struct nodo {
	int dato;
	nodo * siguiente;
	nodo * anterior;
	nodo * inicio;
};
 
typedef struct nodo * LD;
 
void insertarNodo(LD &l)
{
	int n;
	cout << endl << "Ingresa numero: ";
	cin >> n;
 
	/*CREAR NODO*/
	LD aux = new nodo;
	aux->dato = n;
	aux->siguiente = NULL;
	aux->anterior = NULL;
 
	LD aux2 = NULL;
 
	if (l->inicio == NULL)
	{
		l->inicio = aux;
	}
	else
	{
		aux2 = l->inicio;
		while (aux2->siguiente != NULL)
		{
			aux2 = aux2->siguiente;
		}
		aux2->siguiente = aux;
		aux->anterior = aux2;
	}
 
}
 
void mostrarLista(LD l)
{
	LD aux = l->inicio;
	cout << endl << "Lista: NULL ";
	while (aux != NULL)
	{
		cout << aux->dato << "  ";
		aux = aux->siguiente;
	}
	cout << " NULL"<<endl<<endl;
}
 
void eliminarLista(LD l)
{
	LD aux = NULL;
	while (l->inicio != NULL)
	{
		aux = l->inicio;
		l->inicio = aux->siguiente;
		aux->anterior = NULL;
		aux->siguiente = NULL;
	}
}
 
int main()
{
	LD lista = new nodo;
 
	lista->inicio = NULL;
 
	int opc, numero;
	do {
		cout << "\n\n\t Listas Doblemente enlazadas";
		cout << endl << "1 Insertar numero" << endl
			<< "2 Mostrar lista" << endl
			<< "3 Eliminar lista"<< endl
			<< "0 Salir" << endl;
		cin >> opc;
 
		switch (opc)
		{
		case 1:
			insertarNodo(lista);
			break;
		case 2:
			mostrarLista(lista);
			break;
		case 3:
			eliminarLista(lista);
			break;
		case 0:
			cout << endl << endl << "Terminando programa..."<<endl;
		default:
			cout << endl << endl << "Opcion no disponible...";
			break;
		}
	} while (opc != 0);
 
	system("Pause");
	return 0;
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder