LinkedList.java 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. package Fall0811;
  2. import java.util.Iterator;
  3. import Summer08.Node;
  4. public class LinkedList implements Iterable {
  5. private Node head;
  6. private Node tail;
  7. private int size;
  8. public Iterator iterator(){
  9. return new LLIterator();
  10. }
  11. private class LLIterator implements Iterator{
  12. private Node nextNode;
  13. private boolean removeOK;
  14. private int posToRemove;
  15. private LLIterator(){
  16. nextNode = head;
  17. removeOK = false;
  18. posToRemove = -1;
  19. }
  20. public boolean hasNext(){
  21. return nextNode != null;
  22. }
  23. public Object next(){
  24. assert hasNext();
  25. Object result = nextNode.getData();
  26. nextNode = nextNode.getNext();
  27. removeOK = true;
  28. posToRemove++;
  29. return result;
  30. }
  31. public void remove(){
  32. assert removeOK;
  33. removeOK = false;
  34. LinkedList.this.remove(posToRemove);
  35. posToRemove--;
  36. }
  37. }
  38. public void makeEmpty(){
  39. // let GC do its job!!!!!!!
  40. head = tail = null;
  41. size = 0;
  42. }
  43. public Object remove(int pos){
  44. assert pos >= 0 && pos < size;
  45. Object result;
  46. if( pos == 0 ){
  47. result = head.getData();
  48. head = head.getNext();
  49. if( size == 1 )
  50. tail = null;
  51. }
  52. else{
  53. Node temp = head;
  54. for(int i = 1; i < pos; i++)
  55. temp = temp.getNext();
  56. result = temp.getNext().getData();
  57. temp.setNext( temp.getNext().getNext() );
  58. if( pos == size - 1)
  59. tail = temp;
  60. }
  61. size--;
  62. return result;
  63. }
  64. public Object get(int pos){
  65. assert pos >= 0 && pos < size;
  66. // array based list
  67. // return myCon[pos]
  68. Object result;
  69. if( pos == size - 1 )
  70. result = tail.getData(); //O(1)
  71. else{
  72. Node temp = head;
  73. for(int i = 0; i < pos; i++)
  74. temp = temp.getNext();
  75. result = temp.getData();
  76. // average case O(N) :((((
  77. }
  78. return result;
  79. }
  80. public void insert(int pos, Object obj){
  81. assert pos >= 0 && pos <= size;
  82. // addFirst?
  83. if(pos == 0)
  84. addFirst(obj); // O(1)
  85. // add last?
  86. else if( pos == size )
  87. add(obj); //at end O(1)
  88. else{
  89. // general case
  90. Node temp = head;
  91. for(int i = 1; i < pos; i++)
  92. temp = temp.getNext();
  93. // I know temp is pointing at the
  94. // node at position pos - 1
  95. Node newNode = new Node(obj, temp.getNext());
  96. temp.setNext( newNode );
  97. size++;
  98. }
  99. }
  100. public void add(Object obj){
  101. Node newNode = new Node(obj, null);
  102. if( size == 0 )
  103. head = newNode;
  104. else
  105. tail.setNext(newNode);
  106. tail = newNode;
  107. size++;
  108. }
  109. public void addFirst(Object obj){
  110. if(size == 0)
  111. add(obj);
  112. else{
  113. Node newNode = new Node(obj, head);
  114. head = newNode;
  115. size++;
  116. }
  117. }
  118. public String toString(){
  119. String result = "";
  120. Node temp = head;
  121. for(int i = 0; i < size; i++){
  122. result += temp.getData() + " ";
  123. temp = temp.getNext();
  124. }
  125. return result;
  126. }
  127. }