Herowaree

ListaEncadeada.java

Nov 9th, 2014
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. public class ListaEncadeada {
  2.  
  3. private No cabeca;
  4. private int tamanho;
  5.  
  6. public ListaEncadeada(){
  7. this.tamanho = 0;
  8. this.cabeca = null;
  9. }
  10.  
  11. public int getTamanho(){
  12. return this.tamanho;
  13. }
  14.  
  15. public void mostrarLista(){
  16. if(this.tamanho == 0){
  17. System.out.println("Lista vazia.");
  18. }else{
  19. No ptr = this.cabeca;
  20. System.out.println("Tamanho da lista :"+this.tamanho);
  21. System.out.println("Elementos da lista:");
  22. for(int count = 0; count < this.tamanho; count++){
  23. System.out.println("\tValor : "+ptr.getValor());
  24. System.out.println("\tNo : "+(count+1));
  25. ptr = ptr.getProximo();
  26. System.out.println();
  27. }
  28.  
  29. System.out.println();
  30. }
  31. }
  32.  
  33. public void insereComecoLista(int valor){
  34. if(this.tamanho==0){
  35. No novoNo = new No();
  36. novoNo.setValor(valor);
  37. novoNo.setProximo(this.cabeca);
  38. this.cabeca = novoNo;
  39.  
  40. this.tamanho = this.tamanho + 1;
  41. }else{
  42. No novoNo2 = new No();
  43. novoNo2.setValor(valor);
  44. novoNo2.setProximo(this.cabeca);
  45. this.cabeca = novoNo2;
  46.  
  47. this.tamanho = this.tamanho + 1;
  48. }
  49. }
  50.  
  51. public void insereFinalLista(int valor){
  52. No novoNo = new No();
  53. No aux = this.cabeca;
  54. while(aux.getProximo() != null){
  55. aux = aux.getProximo();
  56. }
  57. novoNo.setProximo(null);
  58. novoNo.setValor(valor);
  59. aux.setProximo(novoNo);
  60. this.tamanho = this.tamanho + 1;
  61. }
  62.  
  63. public void insere(int valor, int posicao){
  64. No aux = this.cabeca;
  65.  
  66. if(posicao==1){
  67. this.insereComecoLista(valor);
  68. }else if(posicao==this.tamanho + 1){
  69. this.insereFinalLista(valor);
  70. }else if(posicao > this.tamanho || posicao <=0){
  71. System.out.println("Entrada invalida.");
  72. }else{
  73. for(int i = 1; i < posicao - 1; i++){
  74. aux = aux.getProximo();
  75. }
  76. No novoNo = new No();
  77. novoNo.setValor(valor);
  78. novoNo.setProximo(aux.getProximo());
  79. aux.setProximo(novoNo);
  80. this.tamanho = this.tamanho + 1;
  81. }
  82. }
  83.  
  84. public void removeInicioLista(){
  85. if(this.tamanho==0){
  86. System.out.println("A lista está vazia.");
  87. }else{
  88. No aux = this.cabeca;
  89. cabeca = cabeca.getProximo();
  90. aux = null;
  91. this.tamanho = this.tamanho - 1;
  92. }
  93. }
  94.  
  95. public void removeMeioLista(int posicao){
  96. if(this.tamanho==0){
  97. System.out.println("A lista está vazia.");
  98. }else{
  99. No aux = this.cabeca;
  100. No anterior = this.cabeca;
  101.  
  102. for(int count = 1; count <= posicao - 1; count++){
  103. anterior = aux;
  104. aux = aux.getProximo();
  105. }
  106. anterior.setProximo(aux.getProximo());
  107. this.tamanho = this.tamanho - 1;
  108. aux = null;
  109. }
  110. }
  111.  
  112. public void removeLista(int posicao){
  113. if(posicao == 1){
  114. this.removeInicioLista();
  115. }else{
  116. this.removeMeioLista(posicao);
  117. }
  118. }
  119. }
Advertisement
Add Comment
Please, Sign In to add comment