Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. package estrutura_dados;
  2.  
  3. public class Lista<T> implements Colecao<T> {
  4.    
  5.     private No<T> head = null;
  6.    
  7.     @Override
  8.     public void add(T t) {
  9.         if(this.head == null) {
  10.             this.head = new No<T>(t);
  11.             return;
  12.         }
  13.         No<T> no = head;
  14.        
  15.         while(no.prox != null) {
  16.             no = no.prox;
  17.         }
  18.        
  19.         no.prox = new No<T>(t);
  20.     }
  21.    
  22.     public void addpilha(T t) {
  23.         if(this.head == null) {
  24.             this.head = new No<T>(t);
  25.         }else {
  26.            
  27.         }
  28.         No<T> no = new No<T>(t);
  29.         no.prox = head;
  30.         head = no;
  31.     }
  32.    
  33.     @Override
  34.     public T first() {
  35.         if(this.head != null)
  36.             return head.key;
  37.         return null;
  38.     }
  39.  
  40.  
  41.     @Override
  42.     public void print() {
  43.         No<T> no = head;
  44.         while(no != null) {
  45.             System.out.print(no.key + " ");
  46.             no = no.prox;
  47.         }
  48.         System.out.println();
  49.     }
  50.  
  51. }
  52. ==========================================================================================================
  53.  
  54. package estrutura_dados;
  55.  
  56. public class No<T> {
  57.    
  58.     public T key;
  59.     public No<T> prox = null;
  60.  
  61.     public No(T _key) {
  62.         this.key = _key;
  63.     }
  64. }
  65.  
  66. =============================================================================
  67.  
  68. package estrutura_dados;
  69.  
  70. public interface Colecao<T> {
  71.     public T first();
  72.     public void add(T t);
  73.     public void print();
  74. }
  75.  
  76. ==================================================================================
  77.  
  78. package estrutura_dados;
  79.  
  80. public class AppMain {
  81.  
  82.     public static void main(String[] args) {
  83.        
  84.         Lista<String> lista = new Lista<>();
  85.        
  86.         lista.add("Renan");
  87.         lista.add("João");
  88.         lista.print();
  89.        
  90.         lista.addpilha("Maria");
  91.         lista.print();
  92.  
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement