yes this is one of my homework projects - to implement a Circular Linked List based on Singly Linked List. It's a pretty simple and the code is easy to read. All my attributes are public to avoid getters and setters and private business. For the purposes of this project public will suffice.
I initialized my nItems counter (# of items in the list) and Link head right in the attribute field, but I will change it later by initializing it inside of the constructor.
My step() method doesn't seem to work at all. My compiler just freezes for a moment and then nothing shows up. This is how step() method should work if I call it 4 times:
List: 40 80 30 20 10 70 50
List: 80 30 20 10 70 50 40
List: 30 20 10 70 50 40 80
List: 20 10 70 50 40 80 30
Find() method works fine, that is as long as the value we are searching for is inside the Linked List. If it's not, it will keep on going forever. If this is my list, this what happens to find() method if you search for a value that isn't there (I debugged it step by step):
70 60 50 40 30 20 10 10 10 10 10 10 10 10...and 10 forever
Cause: If they key value we are searching for isn't there, it will never get out from the while loop, hence forever repeating current = current.next.
while(current.dData != key) {
current = current.next;
My delete method says it deleted value 60 just as I wanted to, but this what I get:
Deleted link with key 60
70 60 40 30 20 10 // lie! it deletes 50 instead of 60
Please also take a look at my display() and my insert() methods. They look fine to me, but I could be wrong and it could be the source of all problems I am having with find() and delete() methods.
Thank you so much in advance!!!
class Link {
public int dData;
public Link next;
public Link(int dd) {
dData = dd;
}
public void displayLink() {
System.out.print(dData + " ");
}
}
class CircList {
public Link head = null;
int nItems = 0;
public boolean isEmpty() {
return (nItems == 0);
}
// INSERT
public void insert(int key) {
Link current = new Link(key);
if(isEmpty())
head = current;
current.next = head;
head = current;
nItems++;
}
// STEP
public void step() {
Link current = head;
do {
current = current.next;
} while(current != head);
}
// FIND
public Link find (int key) {
Link current = head;
while(current.dData != key) {
current = current.next;
}
return current;
}
// DELETE
public Link delete(int key) {
Link current = head;
while(current.dData != key) {
current = current.next;
}
if(current == head)
head = head.next;
else {
current.next = current.next.next;
nItems--;
}
return current;
}
// DISPLAY
public void displayList() {
Link current = head; // start at beginning
int counter = nItems;
while(true) {
if(counter != 0) {
current.displayLink();
current = current.next; // move to next link
counter--;
}
else
break;
}
}
}
class CircListApp {
public static void main(String[] args) {
Link f, d;
CircList theList = new CircList();
theList.insert(10); // insert items
theList.insert(20);
theList.insert(30);
theList.insert(40);
theList.insert(50);
theList.insert(60);
theList.insert(70);
//System.out.println(theList.nItems + " ");
theList.displayList(); // display list
System.out.println("");
f = theList.find(30); // find item
if( f != null)
System.out.println("Found link with key " + f.dData);
else
System.out.println("Can't find link with key 30");
// theList.step();
//
// System.out.println("Inserting link with key 80");
// theList.insert(80);
// theList.displayList(); // display list
//
d = theList.delete(60); // delete item
if( d != null )
System.out.println("Deleted link with key " + d.dData);
else
System.out.println("Can't delete link with key 60");
theList.displayList(); // display list
//
// f = theList.find(99); // find item
// if( f != null)
// System.out.println("Found link with key " + f.dData);
// else
// System.out.println("Can't find link with key 99" );
// theList.displayList(); // display list
//
// d = theList.delete(13); // delete item
// if( d != null )
// System.out.println("Deleted link with key " + d.dData);
// else
// System.out.println("Can't delete link with key 13");
// theList.displayList(); // display list
//
// System.out.println("Stepping through list");
// for(int j=0; j<theList.getSize; j++)
// {
// theList.step();
// theList.displayList();
// }
//
// System.out.println("Will delete and step one by one");
// while(theList.isEmpty() == false)
// {
// theList.delete();
// theList.step();
// theList.displayList();
// }
} // end main()
}
To your "step" method: You create a local reference called current, which initially points to head. After
current = current.next
it references the successor of head. And in the next step, it references the successor of that, and so on. But it's a local reference, so you don't change anything in the list.
What you have to do, as I deduce from your desired output, is as simple as
if( head != null && head.next != null )
head = head.next
Next: For your find-method: Well, from your loop condition, it's quite obvious that it will run forever if the key is not in the list, because this is your only breaking condition. You should think of a mechanism that makes it stop after you've looked at every item in the list. Hint: Look at what you've done in your version of the step() method.
To your delete-method: The first part is (partially) okay (it has the same infinite-loop problem as your find-method). What you do is iterate through the list until the data of current is equal to the key. That's fine.
But now, current is a reference to the element you want to delete. But what you do is setting current.next to current.next.next, which means that you are deleting the successor of current, instead of current itself! In your loop where you search for the key, you must keep track of the predecessor, so that you can change predecessor.next to current.next, which is what you really want to do.
And then, of course, you must check if it's the head you're deleting, and if that's the case, you must set it to either the predecessor or the successor of it.
Finally, I see a problem with your insert() method! I don't see how this can produce a circular linked list, because you let the newly inserted element point to the head and make it the new head, but nothing points to it. So you only create a singly linked list? You insert a new item "before" the head ( current.next = head ) but then you should also have the former predecessor of head now point to the new item, current!