Advertisement
santiagol26

SOLICITUD EN LISTAS ENLAZADAS

Oct 22nd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.82 KB | None | 0 0
  1. import java.util.*;
  2. //import java.io.*;
  3.  
  4. public class Solicitud{
  5.  
  6. static class Lista<T> {
  7.  
  8. static class NodoLista<T> {
  9. private NodoLista<T> anterior;
  10. private NodoLista<T> siguiente;
  11. private String cedula;
  12. private String texto;
  13. private double promedio;
  14. private String codigo;
  15. private int creditos;
  16. public NodoLista(double promedio,String cedula,String texto,int creditos, String codigo) {
  17. this.promedio=promedio;
  18. this.cedula = cedula;
  19. this.texto = texto;
  20. this.codigo=codigo;
  21. this.creditos=creditos;
  22. siguiente = anterior = null;
  23. }
  24. }
  25.  
  26. NodoLista<T> frente;
  27. NodoLista<T> cola;
  28. int longitud=0;
  29.  
  30. public Lista() {
  31. frente = cola = null;
  32. longitud = 0;
  33. }
  34. public void insertarFrente(double promedio,String cedula,String texto,int creditos, String codigo) {
  35. NodoLista<T> nodo = new NodoLista<>(promedio,cedula,texto, creditos,codigo);
  36. if (longitud == 0) {
  37. cola = nodo;
  38. } else {
  39. frente.anterior = nodo;
  40. nodo.siguiente = frente;
  41. }
  42. frente = nodo;
  43. longitud++;
  44. }
  45. //biene
  46. public String removerFrente(){
  47. String codigo= frente.codigo;
  48. frente = frente.siguiente;
  49. longitud--;
  50. if (longitud > 0) {
  51. frente.anterior = null;
  52. }
  53. return codigo;
  54. }
  55.  
  56. public String removerCola() {
  57. String codigo=cola.codigo;
  58. cola = cola.anterior;
  59. longitud--;
  60. if (longitud > 0) {
  61. cola.siguiente = null;
  62. }
  63. return codigo;
  64. }
  65.  
  66. public String remover(int posicion) { //remover el codigo y todo el nodo
  67. if (posicion == 0) {
  68. return removerFrente();
  69. }
  70. if (posicion == longitud-1) {
  71. return removerCola();
  72. }
  73. NodoLista<T> ref = frente;
  74.  
  75. for (int i = 0; i < posicion; i++) {
  76. ref = ref.siguiente;
  77. }
  78. String codigo = ref.codigo;
  79. ref.anterior.siguiente = ref.siguiente;
  80. ref.siguiente.anterior = ref.anterior;
  81. longitud--;
  82. return codigo;
  83. }
  84.  
  85. public double valorEn(int posicion){ //valor en la posicion metodo
  86. NodoLista<T> ref = frente;
  87. for (int i = 0; i < posicion; i++, ref = ref.siguiente);
  88. return ref.promedio;
  89. }
  90. public int creditos(int posicion){ //valor en la posicion metodo
  91. NodoLista<T> ref = frente;
  92. for (int i = 0; i < posicion; i++, ref = ref.siguiente);
  93. return ref.creditos;
  94. }
  95. public String cedulaEn( Integer posicion){ //valor en la posicion metodo
  96. NodoLista<T> ref = frente;
  97. for (int i = 0; i < posicion; i++, ref = ref.siguiente);
  98. return ref.cedula;
  99. }
  100. public String motivo( Integer posicion){ //valor en la posicion metodo
  101. NodoLista<T> ref = frente;
  102. for (int i = 0; i < posicion; i++, ref = ref.siguiente);
  103. return ref.texto;
  104. }
  105. }
  106.  
  107. public static void main(String[] args) {
  108. //try{
  109. Lista<Object> lista = new Lista<>();
  110. Scanner teclado= new Scanner(System.in);
  111. //System.out.println("escriba solicitud o atender");
  112.  
  113. while(teclado.hasNext()){
  114. String queja=teclado.next();
  115. if (queja.equals("solicitud")) {
  116. //System.out.println("digite su cedula y codigo de asignatura ");
  117. String cedula=teclado.next();
  118. String codigo=teclado.next();
  119. //System.out.println("que reclamo tiene? ");
  120. teclado.nextLine();
  121. String texto = teclado.nextLine();
  122. //System.out.println("digite su PAPA ");
  123. int creditos=Integer.parseInt(teclado.nextLine());
  124. double promedio=Double.parseDouble(teclado.nextLine());
  125. double Pappi=((promedio/creditos)*1000000)/1000000;
  126. lista.insertarFrente(Pappi, cedula, texto,creditos,codigo); //lo inserto en ua pila
  127. }
  128. else {
  129. //System.out.println(lista.longitud);
  130. if (lista.longitud>0){
  131. double mayor=0;
  132. for (int n=0;n<lista.longitud;n++){ //for para buscar el PAPPI mayor
  133. if(lista.valorEn(n)>= mayor){
  134. mayor=lista.valorEn(n);
  135. }
  136. }
  137. int credit=0;
  138. int posR=0;
  139. for (int n=0;n<lista.longitud;n++){ //bucle para encontrar la posicion que llego primero y tiene el mayor PAPPi
  140. if( lista.valorEn(n)== mayor && lista.creditos(n)>=credit ){
  141. credit=lista.creditos(n);
  142. posR=n; //posicion que busco
  143. }
  144. }
  145. System.out.println(lista.cedulaEn(posR)+","+lista.motivo(posR)+","+lista.remover(posR) ); //muestro los datos y lo remuevo
  146. }
  147. else {
  148. System.out.println("Todos tienen cupo");
  149. continue;
  150. }
  151. }
  152. }
  153. teclado.close();
  154. // }catch(Exception e){
  155. // System.err.println (e.getMessage ());
  156. // return;
  157. // }
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement