Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package estrutura_dados;
- public class Lista<T> implements Colecao<T> {
- private No<T> head = null;
- @Override
- public void add(T t) {
- if(this.head == null) {
- this.head = new No<T>(t);
- return;
- }
- No<T> no = head;
- while(no.prox != null) {
- no = no.prox;
- }
- no.prox = new No<T>(t);
- }
- public void addpilha(T t) {
- if(this.head == null) {
- this.head = new No<T>(t);
- }else {
- }
- No<T> no = new No<T>(t);
- no.prox = head;
- head = no;
- }
- @Override
- public T first() {
- if(this.head != null)
- return head.key;
- return null;
- }
- @Override
- public void print() {
- No<T> no = head;
- while(no != null) {
- System.out.print(no.key + " ");
- no = no.prox;
- }
- System.out.println();
- }
- }
- ==========================================================================================================
- package estrutura_dados;
- public class No<T> {
- public T key;
- public No<T> prox = null;
- public No(T _key) {
- this.key = _key;
- }
- }
- =============================================================================
- package estrutura_dados;
- public interface Colecao<T> {
- public T first();
- public void add(T t);
- public void print();
- }
- ==================================================================================
- package estrutura_dados;
- public class AppMain {
- public static void main(String[] args) {
- Lista<String> lista = new Lista<>();
- lista.add("Renan");
- lista.add("João");
- lista.print();
- lista.addpilha("Maria");
- lista.print();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement