1. how do you determine if the list if full if it never ends?
2. right now my code using the data set {1,2,3,4} will display 1,2,3,4,1,2.... so I know that it is kind of working but not there yet?
3. I put a method in the display list function that connects the last element to the first, but I dont think this makes sense. The only time the list is circular is when I print it to the screen. Could someone look at my code and please make a few reccomendations. The names of the files are odd because it is only the first step to a project of implementing a circular DLL.
thankyou for any suggestions
public class Solider{
private Node first;
private Node last;
public Solider(){
first = null;
last = null;
}
public boolean isEmpty(){
return first == null;
}
public void insert(int n){
Node newNode = new Node(n);
if (isEmpty())
last = newNode;
else
first.previous = newNode;
newNode.next = first;
first = newNode;
}
public void showList(){
System.out.print("Soliders: ");
Node current = first;
int count=0;
//while (current != null){
while (count < 9){
count++;
if (current == last){
current.next = first;
}
current.showNode();
current = current.next;
}
System.out.println("");
}
public Node deleteKey(int key){
Node current = first;
while (current.info != key){
current = current.next;
if (current == null) return null;
}
if (current == first) first = current.next;
else current.previous.next = current.next;
if (current == last) last = current.previous;
else current.next.previous = current.previous;
return current;
}
public static void main(String[] args){
Solider josephus = new Solider();
josephus.insert(1);
josephus.insert(2);
josephus.insert(3);
josephus.insert(4);
josephus.showList();
josephus.deleteKey(1);
josephus.deleteKey(2);
josephus.deleteKey(3);
josephus.deleteKey(4);
josephus.showList();
}
}
class Node {
public int info;
public Node next;
public Node previous;
public Node(int n) {
info = n;
}
public void showNode(){
System.out.print(info + " ");
}
}
Admin Edit: [/b]Please[/b] use code tags when posting your code. Code tags are used like so =>
Thanks,
PsychoCoder

New Topic/Question
Reply
MultiQuote









|