Advertisement
fefetl08

ListaEncadeada

Mar 28th, 2020
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.89 KB | None | 0 0
  1. public class ListaEncadeada {
  2.     private Node lista;
  3.  
  4.     public ListaEncadeada() {
  5.         lista = null;
  6.     }
  7.  
  8.     private boolean vazia() {
  9.         return lista == null;
  10.     }
  11.  
  12.     public void inserePrimeiro(int info) {
  13.         Node newnode = new Node(info);
  14.         if (!vazia()) {
  15.             newnode.setProximo(lista);
  16.         }
  17.         lista = newnode;
  18.     }
  19.  
  20.     public void insereDepois(Node node, int info) {
  21.         if(vazia()) {
  22.             inserePrimeiro(info);
  23.         }
  24.         else {
  25.             Node newnode = new Node(info);
  26.             newnode.setProximo(node.getProximo());
  27.             node.setProximo(newnode);
  28.         }
  29.     }
  30.  
  31.     public void insereUltimo(int info) {
  32.         Node aux = lista;
  33.         if(vazia()) {
  34.             inserePrimeiro(info);
  35.         }
  36.         else {
  37.             Node newnode = new Node(info);
  38.             while (aux.getProximo() != null) {
  39.                 aux = aux.getProximo();
  40.             }
  41.             aux.setProximo(newnode);
  42.         }
  43.     }
  44.  
  45.     public void insereOrdenado(int info) {
  46.         Node aux = lista;
  47.         Integer temp = null;
  48.         if(vazia()) inserePrimeiro(info);
  49.         else {
  50.             while (aux != null) {
  51.                 if (aux.getInformacao() < info) {
  52.                     temp = aux.getInformacao();
  53.                     aux = aux.getProximo();
  54.                 }
  55.                 else {
  56.                     if (info < aux.getInformacao()) {
  57.                         inserePrimeiro(info);
  58.                     }
  59.                     else {
  60.                         insereDepois(getNode(temp), info);
  61.                     }
  62.                     return;
  63.                 }
  64.             }
  65.             insereUltimo(info);
  66.         }
  67.     }
  68.  
  69.     public void imprime() {
  70.         if (lista == null) System.out.println("Lista vazia");
  71.         else {
  72.             Node aux = lista;
  73.             while(aux != null) {
  74.                 System.out.println("Elemento " + aux.getInformacao());
  75.                 aux = aux.getProximo();
  76.             }
  77.         }
  78.     }
  79.  
  80.     public Node removePrimeiro() {
  81.         Node aux = lista;
  82.         lista = aux.getProximo();
  83.         return aux;
  84.     }
  85.  
  86.     public Node removeUltimo() {
  87.         Node aux = lista;
  88.         Node ref = aux;
  89.         while (aux.getProximo() != null) {
  90.             ref = aux;
  91.             aux = aux.getProximo();
  92.         }
  93.         ref.setProximo(null);
  94.         return aux;
  95.     }
  96.  
  97.     public Node remove(Node node) {
  98.         Node aux = lista;
  99.         while (aux.getProximo() != node) {
  100.             aux = aux.getProximo();
  101.         }
  102.         aux.setProximo(node.getProximo());
  103.         return aux;
  104.     }
  105.  
  106.     public Node getNode(int info) {
  107.         Node aux = lista;
  108.         while (aux.getProximo() != null) {
  109.             if (aux.getInformacao() == info) return aux;
  110.             aux = aux.getProximo();
  111.         }
  112.         return null;
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement