Advertisement
Guest User

Resolução Exercício 2 - Listas

a guest
Oct 31st, 2014
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6.  
  7. package exercicios.fixacao.lista.sequencial;
  8.  
  9. import com.sun.java.accessibility.util.java.awt.ListTranslator;
  10.  
  11. /**
  12.  *
  13.  * @author fernandommota
  14.  */
  15. public class ExercicioLista2 {
  16.    
  17.     public void insereOrdenado(NoLista[] lista, int n, NoLista novo, int MAX){
  18.         int count=0;
  19.         if(n < MAX){
  20.            
  21.             while(lista[count].getChave() < novo.getChave() && count < (n-1))
  22.                 count++;
  23.            
  24.             if(count==(n-1)){
  25.                 lista[n]=novo;
  26.                 n++;
  27.             }else{
  28.                 int elementoMaior=count;
  29.                 int elementomenor=count-1;
  30.  
  31.                 for(int i=n; i > elementoMaior; i--){
  32.                     lista[i]=lista[i-1];
  33.                 }
  34.                 lista[elementoMaior]=novo;
  35.             }
  36.         }else
  37.             System.out.println("lista cheia (overflow)");
  38.     }
  39.     public static void main(String args[]){
  40.         int MAX=10;
  41.         NoLista[] lista = new NoLista[MAX];
  42.        
  43.         NoLista no1 = new NoLista();
  44.         no1.setChave(7);
  45.         no1.setNome("Jose");
  46.         lista[0] = no1;
  47.        
  48.         NoLista no2 = new NoLista();
  49.         no2.setChave(9);
  50.         no2.setNome("Fabricio");
  51.         lista[1] = no2;
  52.        
  53.         NoLista novoNo = new NoLista();
  54.         novoNo.setChave(1);
  55.         novoNo.setNome("novo nó");
  56.        
  57.         ExercicioLista2 obj= new ExercicioLista2();
  58.         obj.insereOrdenado(lista, 2, novoNo, MAX);
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement