Advertisement
Guest User

Untitled

a guest
Sep 20th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 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. package estruturasdedados;
  7.  
  8. /**
  9.  *
  10.  * @author Roverson
  11.  * @param <T>
  12.  */
  13. public class ListaDuplamenteEncadeada <T> {
  14.     public ListNode head, tail;
  15.     public int size;
  16.     private class ListNode{
  17.         private T element;
  18.         private ListNode next, prev;
  19.         private ListNode(T elem,ListNode p, ListNode n){
  20.             element = elem;
  21.             prev = p;
  22.             next = n;
  23.         }
  24.     }
  25.     public ListaDuplamenteEncadeada(){
  26.         head = tail = new ListNode(null,null,null);
  27.         size = 0;
  28.     }
  29.     public boolean isEmpty(){
  30.         return (size==0);
  31.     }
  32.     public int size(){
  33.         return size;
  34.     }
  35.     public void clear(){
  36.         head.next = tail.prev = null;
  37.     }
  38.     public void add(T elem){
  39.         if(head.next == null){
  40.             ListNode novo = new ListNode(elem,null,null);
  41.             head.next = tail.prev = novo;
  42.         }
  43.  
  44.         else {
  45.             ListNode novo = new ListNode(elem,tail.prev,null);
  46.             tail.prev.next = novo;
  47.             tail.prev = novo;
  48.         }
  49.         size++;
  50.     }
  51.    
  52.     public void add(T elem, int index){
  53.         if(index<0 || index>size-1)
  54.             throw new IndexOutOfBoundsException("Index fora dos limites da lista");
  55.        
  56.         if(index==0){
  57.             ListNode novo = new ListNode(elem,null,head.next);
  58.             head.next.prev = novo;
  59.             head.next = novo;
  60.         }
  61.         if(index==size){
  62.             ListNode novo = new ListNode(elem,tail.prev,null);
  63.             tail.prev.next = novo;
  64.             tail.prev = novo;
  65.         }
  66.         else{
  67.             ListNode itr = head.next;
  68.             ListNode novo = new ListNode(elem,tail.prev,null);
  69.             for(int i=0; i<size;i++){
  70.                 if(index==i){
  71.                     itr.prev.next = novo;
  72.                     itr.prev = novo;
  73.                     size++;
  74.                     return;
  75.                 }
  76.             itr=itr.next;
  77.             }
  78.         }
  79.        
  80.     }
  81.  
  82.     public void printList(){
  83.         ListNode itr = head.next;
  84.         if(isEmpty())
  85.             return;
  86.         for(int i=0;i<size;i++){
  87.            System.out.println(itr.element.toString());
  88.            itr=itr.next;
  89.         }
  90.         /*);
  91.         itr=itr.next;
  92.         while(itr.next!=null){*/
  93.        
  94.     }
  95.    
  96. }
  97.  
  98.  
  99.  
  100.  
  101.  
  102. package estruturasdedados;
  103.  
  104. public class EstruturasdeDados {
  105.  
  106.     public static void main(String[] args) {
  107.         ListaDuplamenteEncadeada minhalista = new ListaDuplamenteEncadeada();
  108.         minhalista.add(new Aluno("Zeus","123"));
  109.         minhalista.add(new Aluno("Abdon","321"));
  110.         minhalista.add(new Aluno("Ana","432"));
  111.         minhalista.add(new Aluno("Zeca","122"));
  112.         minhalista.add(new Aluno("Paulo","111"));
  113.         minhalista.add(new Aluno("Pãozinho","5444"),0);
  114.         minhalista.printList();
  115.     }
  116.    
  117. }
  118.  
  119.  
  120.  
  121.  
  122.  
  123. package estruturasdedados;
  124.  
  125. public class Aluno implements Comparable{
  126.     private String nome;
  127.     private String matricula;
  128.  
  129.     public Aluno(String nome, String matricula) {
  130.         this.nome = nome;
  131.         this.matricula = matricula;
  132.     }
  133.  
  134.     public String getNome() {
  135.         return nome;
  136.     }
  137.  
  138.     public void setNome(String nome) {
  139.         this.nome = nome;
  140.     }
  141.  
  142.     public String getMatricula() {
  143.         return matricula;
  144.     }
  145.  
  146.     public void setMatricula(String matricula) {
  147.         this.matricula = matricula;
  148.     }
  149.  
  150.     @Override
  151.     public String toString() {
  152.         return "Aluno{" + "nome=" + nome + ", matricula=" + matricula + '}';
  153.     }
  154.  
  155.     @Override
  156.     public int compareTo(Object o) {
  157.         Aluno a=(Aluno)o;
  158.         return this.nome.compareTo(a.nome);
  159.     }
  160.    
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement