Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package estruturasdedados;
- /**
- *
- * @author Roverson
- * @param <T>
- */
- public class ListaDuplamenteEncadeada <T> {
- public ListNode head, tail;
- public int size;
- private class ListNode{
- private T element;
- private ListNode next, prev;
- private ListNode(T elem,ListNode p, ListNode n){
- element = elem;
- prev = p;
- next = n;
- }
- }
- public ListaDuplamenteEncadeada(){
- head = tail = new ListNode(null,null,null);
- size = 0;
- }
- public boolean isEmpty(){
- return (size==0);
- }
- public int size(){
- return size;
- }
- public void clear(){
- head.next = tail.prev = null;
- }
- public void add(T elem){
- if(head.next == null){
- ListNode novo = new ListNode(elem,null,null);
- head.next = tail.prev = novo;
- }
- else {
- ListNode novo = new ListNode(elem,tail.prev,null);
- tail.prev.next = novo;
- tail.prev = novo;
- }
- size++;
- }
- public void add(T elem, int index){
- if(index<0 || index>size-1)
- throw new IndexOutOfBoundsException("Index fora dos limites da lista");
- if(index==0){
- ListNode novo = new ListNode(elem,null,head.next);
- head.next.prev = novo;
- head.next = novo;
- }
- if(index==size){
- ListNode novo = new ListNode(elem,tail.prev,null);
- tail.prev.next = novo;
- tail.prev = novo;
- }
- else{
- ListNode itr = head.next;
- ListNode novo = new ListNode(elem,tail.prev,null);
- for(int i=0; i<size;i++){
- if(index==i){
- itr.prev.next = novo;
- itr.prev = novo;
- size++;
- return;
- }
- itr=itr.next;
- }
- }
- }
- public void printList(){
- ListNode itr = head.next;
- if(isEmpty())
- return;
- for(int i=0;i<size;i++){
- System.out.println(itr.element.toString());
- itr=itr.next;
- }
- /*);
- itr=itr.next;
- while(itr.next!=null){*/
- }
- }
- package estruturasdedados;
- public class EstruturasdeDados {
- public static void main(String[] args) {
- ListaDuplamenteEncadeada minhalista = new ListaDuplamenteEncadeada();
- minhalista.add(new Aluno("Zeus","123"));
- minhalista.add(new Aluno("Abdon","321"));
- minhalista.add(new Aluno("Ana","432"));
- minhalista.add(new Aluno("Zeca","122"));
- minhalista.add(new Aluno("Paulo","111"));
- minhalista.add(new Aluno("Pãozinho","5444"),0);
- minhalista.printList();
- }
- }
- package estruturasdedados;
- public class Aluno implements Comparable{
- private String nome;
- private String matricula;
- public Aluno(String nome, String matricula) {
- this.nome = nome;
- this.matricula = matricula;
- }
- public String getNome() {
- return nome;
- }
- public void setNome(String nome) {
- this.nome = nome;
- }
- public String getMatricula() {
- return matricula;
- }
- public void setMatricula(String matricula) {
- this.matricula = matricula;
- }
- @Override
- public String toString() {
- return "Aluno{" + "nome=" + nome + ", matricula=" + matricula + '}';
- }
- @Override
- public int compareTo(Object o) {
- Aluno a=(Aluno)o;
- return this.nome.compareTo(a.nome);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement