Advertisement
fefetl08

ListaEncadeadaCDupla

Apr 7th, 2020
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.93 KB | None | 0 0
  1. public class ListaEncadeadaDupla {
  2.     private Node lista;
  3.  
  4.     public ListaEncadeadaDupla() {
  5.         this.lista = null;
  6.     }
  7.  
  8.     public boolean vazia() {
  9.         return lista == null;
  10.     }
  11.  
  12.     public void inserePrimeiro(int info) {
  13.         Node newnode = new Node(info);
  14.         if (!vazia()) {
  15.             lista.getAnterior().setProximo(newnode);
  16.             newnode.setProximo(lista);
  17.         }
  18.         lista = newnode;
  19.     }
  20.  
  21.     public void insereDepois(Node node, int info) {
  22.         if(vazia()) {
  23.             inserePrimeiro(info);
  24.         }
  25.         else {
  26.             Node newnode = new Node(info);
  27.             newnode.setProximo(node.getProximo());
  28.             node.setProximo(newnode);
  29.         }
  30.     }
  31.  
  32.     public void insereUltimo(int info) {
  33.         Node aux = lista;
  34.         if(vazia()) {
  35.             inserePrimeiro(info);
  36.         }
  37.         else {
  38.             Node newnode = new Node(info);
  39.             while (aux.getProximo() != lista) {
  40.                 aux = aux.getProximo();
  41.             }
  42.             aux.setProximo(newnode);
  43.             newnode.setProximo(lista);
  44.         }
  45.     }
  46.  
  47.     public void insereOrdenado(int info) {
  48.         Node aux = lista;
  49.         if (vazia() || (aux.getProximo() == lista && aux.getInformacao() > info)) {
  50.             inserePrimeiro(info);
  51.         }
  52.         else {
  53.             while (aux.getProximo() != lista) {
  54.                 if (aux.getInformacao() < info) {
  55.                     aux = aux.getProximo();
  56.                 }
  57.                 else if (aux == lista) {
  58.                     inserePrimeiro(info);
  59.                     return;
  60.                 }
  61.                 else {
  62.                     insereDepois(aux.getAnterior(), info);
  63.                     return;
  64.                 }
  65.             }
  66.             if (aux.getInformacao() < info) {
  67.                 insereUltimo(info);
  68.             }
  69.             else {
  70.                 insereDepois(aux.getAnterior(), info);
  71.             }
  72.         }
  73.     }
  74.  
  75.     public Node removePrimeiro() {
  76.         Node aux = lista;
  77.         if (lista.getProximo() == lista) {
  78.             lista = null;
  79.         }
  80.         else {
  81.             lista = aux.getProximo();
  82.             aux.getAnterior().setProximo(lista);
  83.         }
  84.         return aux;
  85.     }
  86.  
  87.     public Node removeUltimo() {
  88.         Node aux = lista;
  89.         if (aux.getProximo() == lista) {
  90.             lista = null;
  91.         }
  92.         else {
  93.             aux.getAnterior().getAnterior().setProximo(lista);
  94.         }
  95.         return aux;
  96.     }
  97.  
  98.     public Node remove(Node node) {
  99.         if (lista.getProximo() == lista) {
  100.             lista = null;
  101.         }
  102.         else if (node == lista) {
  103.             removePrimeiro();
  104.         }
  105.         else if (node.getProximo() == lista) {
  106.             removeUltimo();
  107.         }
  108.         else {
  109.             Node aux = lista;
  110.             while (aux.getProximo() != node) {
  111.                 aux = aux.getProximo();
  112.             }
  113.             aux.setProximo(node.getProximo());
  114.         }
  115.         return node;
  116.     }
  117.  
  118.     public Node getNode(int info) {
  119.         Node aux = lista;
  120.         if(aux.getProximo() == lista && aux.getInformacao() == info) {
  121.             return aux;
  122.         }
  123.         while (aux.getProximo() != lista) {
  124.             if (aux.getInformacao() == info) {
  125.                 return aux;
  126.             }
  127.             aux = aux.getProximo();
  128.         }
  129.         if (aux.getInformacao() == info) {
  130.             return aux;
  131.         }
  132.         else {
  133.             return null;
  134.         }
  135.     }
  136.  
  137.     public void imprime() {
  138.         if (vazia()) {
  139.             System.out.println("Lista vazia");
  140.         }
  141.         else {
  142.             Node aux = lista;
  143.             while(aux.getProximo() != lista) {
  144.                 System.out.println("Elemento " + aux.getInformacao());
  145.                 aux = aux.getProximo();
  146.             }
  147.             System.out.println("Elemento " + aux.getInformacao());
  148.         }
  149.     }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement