View Javadoc
1 package jrre.unittests; 2 3 class Node { 4 5 private int value; 6 private Node next; 7 private Node previous; 8 9 public Node(int value, Node previous, Node next){ 10 11 this.previous = previous; 12 this.value = value; 13 this.next = next; 14 } 15 16 public Node(int value){ 17 this.next = null; 18 this.value = value; 19 } 20 21 /*** 22 * Gets the Data. 23 */ 24 public int getValue(){ 25 return this.value; 26 } 27 28 /*** 29 * Sets the Data. 30 * @param Data The value to set it to. 31 */ 32 public void setValue(int value){ 33 this.value = value; 34 } 35 36 /*** 37 * Gets the Previous. 38 */ 39 public Node getPrevious(){ 40 return this.previous; 41 } 42 43 /*** 44 * Sets the Previous. 45 * @param Previous The value to set it to. 46 */ 47 public void setPrevious(Node previous){ 48 this.previous = previous; 49 } 50 51 /*** 52 * Gets the Next. 53 */ 54 public Node getNext(){ 55 return this.next; 56 } 57 58 /*** 59 * Sets the Next. 60 * @param Next The value to set it to. 61 */ 62 public void setNext(Node next){ 63 this.next = next; 64 } 65 66 } 67 68 public class LinkedList { 69 70 private int count; 71 private Node head; 72 private Node tail; 73 private Node cursor; 74 75 public void insert(int value){ 76 77 if(head == null){ 78 head = new Node(value, null, null); 79 tail = head; 80 } 81 else { 82 head = new Node(value, null, head); 83 } 84 } 85 86 public void append(int value){ 87 tail.setNext(new Node(value, tail, null)); 88 tail = tail.getNext(); 89 } 90 91 public int removeHead(){ 92 Node headNode = head; 93 head = head.getNext(); 94 return headNode.getValue(); 95 } 96 97 public int removeTail(){ 98 int toReturn = tail.getValue(); 99 tail = tail.getPrevious(); 100 tail.setNext(null); 101 return toReturn; 102 } 103 104 public boolean find(int value){ 105 cursor = head; 106 while(cursor != null){ 107 108 if(cursor.getValue() == value) 109 return true; 110 111 cursor = cursor.getNext(); 112 } 113 return false; 114 } 115 116 public void printList(){ 117 118 cursor = head; 119 while(cursor != null){ 120 println(cursor.getValue()); 121 cursor = cursor.getNext(); 122 } 123 } 124 125 public void println(int toPrint){ 126 jrre.api.java.lang.System.out.println(toPrint); 127 } 128 129 public void fillList(int [] args){ 130 131 132 /* 133 int k=0; 134 for(int i=0;i < 5;i++) 135 list.println(args[i]); 136 list.insert(10); 137 list.insert(20); 138 list.insert(30); 139 140 list.append(1); 141 list.append(2); 142 list.append(3); 143 144 list.removeHead(); 145 list.removeTail(); 146 147 list.printList(); 148 */ 149 } 150 151 public static void main(String [] args){ 152 153 LinkedList list = new LinkedList(); 154 155 int [] toSort = {2, 156 9, 157 3, 158 25, 159 11}; 160 161 for(int i=0;i < toSort.length;i++) 162 list.println(toSort[i]); 163 164 } 165 }

This page was automatically generated by Maven