| | |
Help my Circular Linked List
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
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.
Many thanks in advance!
This is ListNode class
This is CLL class
This is Main Class
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.
Many thanks in advance!
This is ListNode class
Java Syntax (Toggle Plain Text)
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
Java Syntax (Toggle Plain Text)
public class CLL { private ListNode head; public CLL() { head = null; } //Add node of data to Linked List public void addToList(int data){ //Using addToTail method ListNode current = head; //Set head as current ListNode newNode = new ListNode(data); //Create a new node with data in it if (head == null) { //If head is empty then head = newNode; //newNode (that has data) set to head } else { //If head is already filled then 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; current = head; }// End if else{ previous = current; current = current.getNext(); }// End else }// End for if (current.getNext() == null) current = head; else current = current.getNext(); previous.setNext(current); } LuckyGuy = current.getData(); return LuckyGuy; }// End LuckySuitor public void printList() { ListNode current = head; while (current != null) { System.out.print(current.getData() + "<-"); current = current.getNext(); } System.out.println("null"); } }
This is Main Class
Java Syntax (Toggle Plain Text)
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.addToList(s); Suitors.printList(); System.out.println("The lucky guy is: " + Suitors.LuckySuitor(n)); } }
•
•
Join Date: Sep 2008
Posts: 1,606
Reputation:
Solved Threads: 205
"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.
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.
Out.
•
•
Join Date: Aug 2009
Posts: 3
Reputation:
Solved Threads: 0
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*
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*
•
•
Join Date: Dec 2008
Posts: 8
Reputation:
Solved Threads: 1
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.
when you add -->
if the list is empty then initialize head, and make head.next = head;
if not, move to the last node in the list which should be followed by the head, _ as you've done in the loop but instead of while(current.next != null) it will be while(current.next != head)_
and then make current.next = newNode; newNode.next = head;
and so on, take care of that at removing a node and the special cases
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.
when you add -->
if the list is empty then initialize head, and make head.next = head;
if not, move to the last node in the list which should be followed by the head, _ as you've done in the loop but instead of while(current.next != null) it will be while(current.next != head)_
and then make current.next = newNode; newNode.next = head;
and so on, take care of that at removing a node and the special cases
![]() |
Similar Threads
- circular linked list (C)
- Make a program in C of the Josephus problem using Circular Linked list (C)
- c++ circular linked list destructor (C++)
- Circular linked list (C)
Other Threads in the Java Forum
- Previous Thread: JAva Swing/SQL Server 2000 sending messages in a LAN
- Next Thread: adding to a dynamic array size...
| Thread Tools | Search this Thread |
android api applet application apps array arrays automation awt bidirectional binary birt bluetooth busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia link linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






