Java - lista doblemente encadenada ordenada

 
Vista:

lista doblemente encadenada ordenada

Publicado por tathiana (2 intervenciones) el 02/10/2018 04:29:31
hola como estan
no se si me puedan ayudar
tengo este programa que hace un directorio
necesito que me imprima el iD de menor a mayor
y no he podido
ayudenme !!!!


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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
public class Lista {
 
	class Nodo{
		Nodo ant;
		int id;
		String apellido;
		String nombre;
		int celular;
		char genero;
		Nodo sig;
	}
 
	private Nodo ap;
	private static int contf=0;
	private static int contm=0;
 
	public Lista() {
		ap=null;
		contm = 0;
		contf = 0;
	}
	public void insertar(int idP, String apellidoP, String nombreP, int celularP, char generoP) {
		Nodo aux;
		aux = new Nodo();
		Nodo nuevo;
		nuevo = new Nodo();
		nuevo = ap;
		if (ap==null) {
			aux.ant = null;
			aux.sig = null;
			aux.id = idP;
			aux.nombre = nombreP;
			aux.apellido = apellidoP;
			aux.celular = celularP;
			aux.genero = generoP;
			ap = aux;
 
		}else {
			nuevo = ap;
			while(nuevo.sig!=null) {
				nuevo = nuevo.sig;
			}
			aux.id = idP;
			aux.nombre = nombreP;
			aux.apellido = apellidoP;
			aux.celular = celularP;
			aux.genero = generoP;
			nuevo.sig = aux;
			aux.ant = nuevo;
			aux.sig = null;
		}
 
		if(generoP == 'm' ) {
			contm++;
		}else {
			if(generoP =='f') {
				contf++;
			}
		}
		//Ordenar(aux, nuevo);
	}
 
	public void Ordenar(Nodo aux, Nodo nuevo) {
		if(aux.id>aux.ant.id) {
			aux.sig=nuevo;
			nuevo.ant=ap;
			nuevo.sig = aux;
		}else {
			nuevo.sig = aux.sig;
			aux.sig = nuevo;
			nuevo.ant =null;
		}
	}
 
	public void Buscar_Id(int idP) {
		Nodo recorrer=ap;
		boolean encontrado=false;
		while (recorrer!=null && encontrado==false) {
			if (recorrer.id==idP) {
				encontrado=true;
			}
 
		}
		if (encontrado==true) {
			System.out.println("\n ID: "+"["+recorrer.id+"]"+" - "+"["+"Apellido: "+recorrer.apellido+"]"+" - "+"["+"Nombre: "+recorrer.nombre+"]"+" - "+"["+"[Telefono: "+recorrer.celular+"]"+" - "+"["+"Genero: "+recorrer.genero +"]");
		}else {
			System.out.println("no hay nada ");
		}
	}
 
	public void Buscar_Ap(String apP) {
		Nodo recorrer=ap;
		boolean encontrado=false;
		while (recorrer!=null && encontrado==false) {
			if (recorrer.apellido==apP) {
				encontrado=true;
			}
 
		}
		if (encontrado==true) {
			System.out.println("\n ID: "+"["+recorrer.id+"]"+" - "+"["+"Apellido: "+recorrer.apellido+"]"+" - "+"["+"Nombre: "+recorrer.nombre+"]"+" - "+"["+"[Telefono: "+recorrer.celular+"]"+" - "+"["+"Genero: "+recorrer.genero +"]");
        }else {
            System.out.println("no hay nada ");
        }
    }
 
	public void Buscar_No(String noP) {
		Nodo recorrer=ap;
		boolean encontrado=false;
		while (recorrer!=null && encontrado==false) {
			if (recorrer.nombre==noP) {
				encontrado=true;
			}
 
		}
		if (encontrado==true) {
			System.out.println("\n ID: "+"["+recorrer.id+"]"+" - "+"["+"Apellido: "+recorrer.apellido+"]"+" - "+"["+"Nombre: "+recorrer.nombre+"]"+" - "+"["+"[Telefono: "+recorrer.celular+"]"+" - "+"["+"Genero: "+recorrer.genero +"]");		}else {
			System.out.println("no hay nada ");
		}
	}
 
	public int eliminar_pila() {
		if(ap!=null) {
			int identidad = ap.id;
			ap = ap.sig;
			return identidad;
		}else {
			return Integer.MAX_VALUE;
		}
	}
 
	public int eliminar_cola() {
		Nodo aux = ap;
 
		if(ap == null) {
			System.out.println("NO HAY PERSONAS REGISTRADAS");
			return Integer.MAX_VALUE;
		}else {
			if(ap.sig==null) {
				System.out.println("persona eliminada " + aux.nombre);
				ap = null;
			}else {
				aux = ap;
				while(aux.sig.sig != null) {
					aux = aux.sig;
				}
				System.out.println("persona eliminada " + aux.sig.nombre);
				aux.sig = null;
				return aux.sig.id;
			}
		}
		return aux.sig.id;
	}
 
	public void imprimir() {
		Nodo aux = ap;
		System.out.println("--------------------");
	    System.out.println("\n Listado de personas registrados.");
	    System.out.println("--------------------");
	    while(aux!=null) {
	    	 System.out.println("\n ID: "+"["+aux.id+"]"+" - "+"["+"Apellido: "+aux.apellido+"]"+" - "+"["+"Nombre: "+aux.nombre+"]"+" - "+"["+"[Telefono: "+aux.celular+"]"+" - "+"["+"Genero: "+aux.genero +"]");	    	 aux=aux.sig;
	    }
	    System.out.println();
	}
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Lista lista1 = new Lista();
		int opc=0;
 
		do {
			Scanner ingreso= new Scanner(System.in);
			int id = 0;
			String nombre = "";
			String apellido = "";
			int celular = 0;
			char genero;
 
			System.out.println("\t         __________________");
            System.out.println("\t        |                  |");
            System.out.println("\t        |      MENU        |");
            System.out.println("\t        | 1.Insertar       |");
            System.out.println("\t        | 2.Eliminar       |");
            System.out.println("\t        | 3.buscar         |");
            System.out.println("\t        | 4.contar         |");
            System.out.println("\t        | 5.salir          |");
            System.out.println("\t        |__________________|");
            System.out.println("\t     ");
            System.out.println("\t      digite opcion:");
 
			opc = ingreso.nextInt();
 
			switch(opc) {
			case 1:
				System.out.println("--------------------");
				System.out.println("Ingrese numero de identificación, max 10 digitos: ");
				id = ingreso.nextInt();
				System.out.println("Ingrese nombre: ");
				nombre = ingreso.nextLine();
				nombre = ingreso.nextLine();
				System.out.println("Ingrese  apellido : ");
				apellido = ingreso.nextLine();
				System.out.println("Ingrese numero de contacto : ");
				celular = ingreso.nextInt();
				System.out.println("Ingrese el genero  'M' o 'F': ");
				genero = ingreso.next().charAt(0);
 
				lista1.insertar(id, apellido, nombre, celular, genero);
				lista1.imprimir();
			break;
 
			case 2:
				System.out.println("--------------------");
				System.out.println("¿De que manera desea eliminar, pila, cola o busqueda?");
				System.out.println("1. Pila \n2. Cola \n3. Busqueda");
				int opci = ingreso.nextInt();
				switch(opci) {
				case 1:
					System.out.println("--------------------");
					System.out.println("Eliminado de manera pila: "+ lista1.eliminar_pila());
					lista1.imprimir();
				break;
 
				case 2:
					System.out.println("--------------------");
					System.out.println("Eliminado de manera cola: "+ lista1.eliminar_cola());
					lista1.imprimir();
				break;
 
				case 3:
 
				break;
				}
 
			break;
 
			case 3:
				System.out.println("--------------------");
				System.out.println("¿De que manera desea buscar, por id, por apellido o por nombre?");
				System.out.println("1. ID \n2. Apellido \n3.Nombre ");
				int op = ingreso.nextInt();
				switch (op) {
				case 1:
					System.out.println("Digite ID a buscar: ");
					int idB = ingreso.nextInt();
					lista1.Buscar_Id(idB);
				break;
 
				case 2:
					System.out.println("Digite Apellido a buscar: ");
					String apB = ingreso.nextLine();
					lista1.Buscar_Ap(apB);
				break;
 
				case 3:
					System.out.println("Digite Apellido a buscar: ");
					String noB = ingreso.nextLine();
					lista1.Buscar_No(noB);
				break;
				}
			break;
 
			case 4:
				System.out.println("--------------------");
				System.out.println("¿Que genero  desea contar?");
				System.out.println("1. Hombres 'M' \n2. Mujeres 'F'");
				int elc = ingreso.nextInt();
				switch (elc) {
				case 1:
					System.out.println("La cantidad de personas de genero masculino, es: "+ contm);
				break;
 
				case 2:
					System.out.println("La cantidad de personas de genero femenino, es: "+ contf);
				break;
				}
			break;
			}
		}while(opc!=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