class Link { public int iData; public Link next; // Constructor public Link ( int item ) { iData = item; next = null; } // Print contents public String toString () { String str = "( " + iData + " ) "; return str; } } class CircularList { private Link head; // Constructor public CircularList () { head = null; } // Insert an element in the list public void insert ( int item ) { //new temp object made every time called Link temp = head; Link newLink = new Link(item); if( head != null) { while(temp.next != head) { temp = temp.next; newLink.next = head; temp.next = newLink; head = newLink; } } if ( head == null) { head = newLink; newLink.next = head; } } // Find the link with the given key NOT FINISHED public Link find ( int key ) { Link current = head; while ( current.iData != key ) { if ( current.next == null ) return null; else current = current.next; } return current; } // Delete a link with a given key public Link delete ( int key ) { Link current = head; Link previous = head; Link tail = current; while ( current.iData != key) { if ( current.next == null) return null; else { previous = current; current = current.next; } } if ( current == head ) head = head.next; else previous.next = current.next; if ( current != null) { if (current.next == current) { current = null; return current; } if (current.next != current) { while(tail.next != current) { tail = tail.next; } current = current.next; tail.next = current; } } return current; } // Delete the nth link starting from the Link start // Return the next link from the deleted Link public Link deleteAfter ( Link start, int n ) { } // Return a string representation of a Circular List public String toString () { StringBuffer buf = new StringBuffer(); int count = 0; Link current = head; while ( current != null ) { if ( count == 4 ) { buf.append ( current.toString() + "\n" ); count = 0; } else { buf.append ( current.toString() + " " ); count++; } current = current.next; } return buf.toString(); } } public class Josephus { // program will read in the number of soldiers, the elimination number n, and the soldier from whom the counting starts. //havent done this yet }
[b]I am not sure if my logic is correct for a circular link list in my delete, insert, find and toString method. I am also slightly confused as to what should be done in delete after?! should i just call the delete method? delete the key and print the new link?