darkstar97

lista java - lista

Apr 16th, 2019
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. public class Lista {
  2.     protected Node head;
  3.     protected Node tail;
  4.     protected long size;
  5.    
  6.     public Lista(){
  7.         head = null;
  8.         tail = null;
  9.         size = 0;
  10.     }
  11.    
  12.     public void insereInicio(Node n){
  13.         n.setProximo(head);
  14.         head = n;
  15.         size += 1;
  16.         if(size==1){
  17.             tail = n;
  18.         }
  19.     }
  20.    
  21.     public void insereFim(Node n){
  22.         tail.setProximo(n);
  23.         tail = n;
  24.         size = size + 1;
  25.        
  26.         if(size==1){
  27.             head = n;
  28.         }
  29.     }
  30.    
  31.     public boolean removeInicio() {
  32.         if(size!=0) {
  33.             head = head.getProximo();
  34.             size -=1;
  35.             return true;
  36.         }
  37.         else {
  38.             System.out.println("Lista vazia.");
  39.             return false;
  40.         }
  41.     }
  42.    
  43.     public boolean removeFim() {
  44.         if(size!=0) {
  45.             Node n = head;
  46.             int i;
  47.             for(i=1;i<size-1;i++) {
  48.                 n = n.getProximo();
  49.             }
  50.             System.out.println(i+" iteraçáes.\n");
  51.             tail = n;
  52.             tail.setProximo(null);
  53.             size -=1;
  54.             return true;
  55.         }
  56.         else {
  57.             System.out.println("Lista vazia.");
  58.             return false;
  59.         }
  60.     }
  61.    
  62.     public boolean busca(int valor) {
  63.         if(size==0) return false;
  64.         Node n = head;
  65.         for(int i=1;i<=size;i++) {
  66.             if(valor==n.getValor()) {
  67.                 return true;
  68.             }
  69.             else {
  70.                 n = n.getProximo();
  71.             }
  72.         }
  73.         return false;
  74.     }
  75.    
  76.     public Node getHead() {
  77.         return head;
  78.     }
  79.  
  80.     public void setHead(Node head) {
  81.         this.head = head;
  82.     }
  83.  
  84.     public Node getTail() {
  85.         return tail;
  86.     }
  87.  
  88.     public void setTail(Node tail) {
  89.         this.tail = tail;
  90.     }
  91.  
  92.     public long getSize() {
  93.         return size;
  94.     }
  95.  
  96.     public void setSize(long size) {
  97.         this.size = size;
  98.     }
  99.    
  100.     public String toString(){
  101.         Node cursor;
  102.         cursor = head;
  103.         while(cursor != null){
  104.             System.out.println(cursor.getValor());
  105.             cursor = cursor.getProximo();
  106.         }
  107.         return null;
  108.     }
  109. }
Add Comment
Please, Sign In to add comment