List.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* File : List.java
  2. * Program : Handle Reduction Aimation - Applet
  3. * By Jean Fromentin <jfroment@info.unicaen.fr>
  4. * Copyright 2008 Jean Fromentin
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version 2
  9. * of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. class List {
  17. private Node first;
  18. private Node last;
  19. public Node current;
  20. public int length;
  21. public List(){
  22. length=0;
  23. }
  24. public Node getFirst(){
  25. return first;
  26. }
  27. public Node getLast(){
  28. return last;
  29. }
  30. public void initCurrent(){
  31. current=first;
  32. }
  33. public void setCurrent(Node _current){
  34. current=_current;
  35. }
  36. public Node getCurrent(){
  37. return current;
  38. }
  39. public boolean isEnd(){
  40. return current==last;
  41. }
  42. public void shift(){
  43. current=current.getNext();
  44. }
  45. public void addFirst(int value){
  46. Node node=new Node(value);
  47. if(length==0){
  48. first=node;
  49. last=node;
  50. }
  51. else{
  52. first.setPrevious(node);
  53. node.setNext(first);
  54. first=node;
  55. }
  56. length++;
  57. }
  58. public void addLast(int value){
  59. Node node=new Node(value);
  60. if(length==0){
  61. first=node;
  62. last=node;
  63. }
  64. else{
  65. last.setNext(node);
  66. node.setPrevious(last);
  67. last=node;
  68. }
  69. length++;
  70. }
  71. public Node addBefore(int value){
  72. Node node=new Node(value);
  73. if(current==first){
  74. first=node;
  75. node.setNext(current);
  76. current.setPrevious(node);
  77. }
  78. else {
  79. Node temp=current.getPrevious();
  80. temp.setNext(node);
  81. node.setPrevious(temp);
  82. current.setPrevious(node);
  83. node.setNext(current);
  84. }
  85. length++;
  86. return node;
  87. }
  88. public Node addAfter(int value){
  89. Node node=new Node(value);
  90. if(current==last){
  91. last=node;
  92. node.setPrevious(current);
  93. current.setNext(node);
  94. }
  95. else{
  96. Node temp=current.getNext();
  97. temp.setPrevious(node);
  98. node.setNext(temp);
  99. current.setNext(node);
  100. node.setPrevious(current);
  101. }
  102. length++;
  103. return node;
  104. }
  105. public void remove(Node node){
  106. if(length!=0){
  107. if(length==1){
  108. length=0;
  109. }
  110. else{
  111. if(node==first){
  112. first=node.getNext();
  113. node=first;
  114. }
  115. else if(node==last){
  116. last=node.getPrevious();
  117. current=last;
  118. }
  119. else{
  120. (node.getPrevious()).setNext(node.getNext());
  121. (node.getNext()).setPrevious(node.getPrevious());
  122. }
  123. length--;
  124. }
  125. }
  126. }
  127. public int value(){
  128. return current.getValue();
  129. }
  130. public int length(){
  131. return length;
  132. }
  133. }