Hello. I am an international student trying to solve my assignment which requires to implement Circular Linked List. I asked my lecturer to check my work and she said that I solved it but actually still using the normal Linked List (Linear). She hinted me that I need to modify my program to get the Circular Linked List.

So I'm still confused. In theory, I do know the difference between Linked List & Circular Linked List. However, since I've only learnt Linked List so far so I thought that playing some tricks on Linked List would do create the Circular.

But seems that I was wrong. So I am confused! I mean, is there a new code or way or something to make it Circular Linked List?

Help me.

I give you three classes consist of ListNode, CLL and the Main class.

Expect for your reply A.S.A.P since I need to submit it in next week.

This is ListNode class

``````public class ListNode {

private int data;
private ListNode next;

public ListNode(int data) {
this.data = data;
this.next = null;
}

public int getData() {
return data;
}

public void setData(int data) {
this.data = data;
}

public ListNode getNext() {
return next;
}

public void setNext(ListNode next) {
this.next = next;
}
}``````

This is CLL class

``````public class CLL {

public CLL() {
}

ListNode newNode = new ListNode(data);	//Create a new node with data in it

}
while (current.getNext() != null) {	//If the next node is filled
current = current.getNext();	//Move on the next node
}
current.setNext(newNode);			//Until next node is null, create new node at the end (tail).
}
}

public int LuckySuitor(int count){

ListNode current = head, previous = null;
int toDel,s, LuckyGuy;

count--;

for(s=1; s<=count; s++){

for (toDel=1; toDel<=2; toDel++){

if (current.getNext() == null){
previous = current;
}// End if

else{
previous = current;
current = current.getNext();
}// End else

}// End for

if (current.getNext() == null)
else
current = current.getNext();
previous.setNext(current);
}

LuckyGuy = current.getData();
return LuckyGuy;

}// End LuckySuitor

public void printList() {

while (current != null) {
System.out.print(current.getData() + "<-");
current = current.getNext();
}
System.out.println("null");
}

}``````

This is Main Class

``````import java.util.Scanner;

public class CLL_Main {

public static void main(String[] args) {

CLL  Suitors = new CLL();
int s;

Scanner input = new Scanner (System.in);

int n;

System.out.println("Enter the number of suitors: ");
n = input.nextInt();

for (s=1; s <= n; s++)

Suitors.printList();
System.out.println("The lucky guy is: " + Suitors.LuckySuitor(n));
}
}``````
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by eng.M4AH

"However, since I've only learnt Linked List so far so I thought that playing some tricks on Linked List would do create the Circular."

You are pretty much correct. The differences that I can think of off the top of my head:

1) Obviously, the circularly linked list needs a pointer to the next node and the previous node, whereas the singly linked list only needs a pointer to the next node.
2) The add method for the circularly linked list needs to keep a "previous" node reference, and then when a node is added, that node's previous variable should be set accordingly.
3) The remove method for the circularly linked list needs to update the previous variable as well as next variable whenever something is removed.

etc for the other methods. You're using the same exact methods, just modifying them somewhat to account for having that extra previous node. It isn't that different.

I believe that I have implemented all the three you mentioned.

But I'm still not sure which part should I modify in order to have Circular Linked List. I still cannot see myself as 'I-Know-What-I'm-Supposed-To-Do-Next'. Because my program did solve the question given by my lecturer but was told still using normal linked list.

*Confused*

Hi,

The only trick in the circular linked list is that the next of the last node is always pointing to the first node, instead of null, so it's simple.