Created
December 1, 2012 12:39
-
-
Save sdoro/4182058 to your computer and use it in GitHub Desktop.
eclipse formatter code snippet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Cell { | |
private Cell next; | |
private Object element; | |
public Cell(Object element) { | |
this.element = element; | |
} | |
public Cell(Object element, Cell next) { | |
this.element = element; | |
this.next = next; | |
} | |
public Object getElement() { | |
return element; | |
} | |
public void setElement(Object element) { | |
this.element = element; | |
} | |
public Cell getNext() { | |
return next; | |
} | |
public void setNext(Cell next) { | |
this.next = next; | |
} | |
public String toString() { | |
return (String) element; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.util.List nodes = new java.util.ArrayList(); | |
Cell current = this.tail; | |
while (current != null) { | |
nodes.add(current); | |
current = current.getNext(); | |
} | |
return nodes.toArray(new Cell[nodes.size()]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SingleLinkQueue { | |
protected Cell head; | |
protected Cell tail; | |
public void add(Object item) { | |
Cell c = new Cell (item, tail); | |
if (head == null) | |
head = c; | |
tail = c; | |
} | |
public void remove() { | |
Cell tmp = tail; | |
Cell prec = tail; | |
while ( tmp != head ) { | |
prec = tmp; | |
tmp = tmp.getNext(); | |
} | |
if (head == tail) | |
head = tail = null; | |
else { | |
prec.setNext(null); | |
head = prec; | |
} | |
return; | |
} | |
public String toString () { | |
String s = "["; | |
Cell tmp = tail; | |
while ( tmp != head ) { | |
s = s + tmp.getElement() + " -> "; | |
tmp = tmp.getNext(); | |
} | |
if (head == null) | |
s = "[]"; | |
else | |
s = s + tmp.getElement() + "]"; | |
return s; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Test { | |
public static void main(String[] args) { | |
SingleLinkQueue slq = new SingleLinkQueue(); | |
slq.add(new String("mondo")); | |
slq.add(new String("ciao")); | |
slq.add(new String("crudele")); | |
slq.add(new String("hihihi")); | |
System.out.println(slq); | |
slq.remove(); | |
slq.remove(); | |
slq.remove(); | |
slq.remove(); | |
System.out.println(slq); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment