Help my Circular Linked List

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 3
Reputation: Half_Pirate is an unknown quantity at this point 
Solved Threads: 0
Half_Pirate Half_Pirate is offline Offline
Newbie Poster

Help my Circular Linked List

 
0
  #1
Aug 13th, 2009
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
  1. public class ListNode {
  2.  
  3. private int data;
  4. private ListNode next;
  5.  
  6. public ListNode(int data) {
  7. this.data = data;
  8. this.next = null;
  9. }
  10.  
  11. public int getData() {
  12. return data;
  13. }
  14.  
  15. public void setData(int data) {
  16. this.data = data;
  17. }
  18.  
  19. public ListNode getNext() {
  20. return next;
  21. }
  22.  
  23. public void setNext(ListNode next) {
  24. this.next = next;
  25. }
  26. }

This is CLL class
  1. public class CLL {
  2.  
  3. private ListNode head;
  4.  
  5. public CLL() {
  6. head = null;
  7. }
  8.  
  9. //Add node of data to Linked List
  10. public void addToList(int data){ //Using addToTail method
  11.  
  12. ListNode current = head; //Set head as current
  13. ListNode newNode = new ListNode(data); //Create a new node with data in it
  14.  
  15. if (head == null) { //If head is empty then
  16. head = newNode; //newNode (that has data) set to head
  17. }
  18. else { //If head is already filled then
  19. while (current.getNext() != null) { //If the next node is filled
  20. current = current.getNext(); //Move on the next node
  21. }
  22. current.setNext(newNode); //Until next node is null, create new node at the end (tail).
  23. }
  24. }
  25.  
  26. public int LuckySuitor(int count){
  27.  
  28. ListNode current = head, previous = null;
  29. int toDel,s, LuckyGuy;
  30.  
  31. count--;
  32.  
  33. for(s=1; s<=count; s++){
  34.  
  35. for (toDel=1; toDel<=2; toDel++){
  36.  
  37. if (current.getNext() == null){
  38. previous = current;
  39. current = head;
  40. }// End if
  41.  
  42. else{
  43. previous = current;
  44. current = current.getNext();
  45. }// End else
  46.  
  47. }// End for
  48.  
  49. if (current.getNext() == null)
  50. current = head;
  51. else
  52. current = current.getNext();
  53. previous.setNext(current);
  54. }
  55.  
  56. LuckyGuy = current.getData();
  57. return LuckyGuy;
  58.  
  59. }// End LuckySuitor
  60.  
  61.  
  62. public void printList() {
  63.  
  64. ListNode current = head;
  65.  
  66. while (current != null) {
  67. System.out.print(current.getData() + "<-");
  68. current = current.getNext();
  69. }
  70. System.out.println("null");
  71. }
  72.  
  73. }

This is Main Class
  1. import java.util.Scanner;
  2.  
  3. public class CLL_Main {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. CLL Suitors = new CLL();
  8. int s;
  9.  
  10. Scanner input = new Scanner (System.in);
  11.  
  12. int n;
  13.  
  14. System.out.println("Enter the number of suitors: ");
  15. n = input.nextInt();
  16.  
  17. for (s=1; s <= n; s++)
  18. Suitors.addToList(s);
  19.  
  20. Suitors.printList();
  21. System.out.println("The lucky guy is: " + Suitors.LuckySuitor(n));
  22. }
  23. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,606
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 205
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: Help my Circular Linked List

 
0
  #2
Aug 13th, 2009
"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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: Half_Pirate is an unknown quantity at this point 
Solved Threads: 0
Half_Pirate Half_Pirate is offline Offline
Newbie Poster

Re: Help my Circular Linked List

 
0
  #3
Aug 13th, 2009
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*
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 8
Reputation: eng.M4AH is an unknown quantity at this point 
Solved Threads: 1
eng.M4AH eng.M4AH is offline Offline
Newbie Poster

Re: Help my Circular Linked List

 
0
  #4
Aug 15th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC