473,379 Members | 1,243 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Circular Linked List

I am trying to create a circular list that includes insert() , find() and remove() methodes. also a step() method that moves current along to the next link.
I still need to display the list by breaking the circle at some arbitrary point to print it on the screen. Does anyone have an idea how to do that?

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.JOptionPane;
  2.  public class CLL
  3. {
  4. public class Link
  5. {
  6. int item=0;
  7. Link next;
  8.  
  9. public Link()
  10. {
  11. //this.item = 0;
  12. Link next = null;
  13. }
  14. public Link(int i, Link n)
  15. {
  16. item = i;
  17. next = n;
  18. }
  19. }
  20. Link head;
  21.  
  22. public void insert(int item)
  23. {
  24. if(head == null)
  25. {
  26. head = new Link(item,null);
  27. head.next = head;
  28. }
  29. else
  30. {
  31. head.next = new Link(item,head.next);
  32. }
  33. }
  34.  
  35.  
  36.  
  37. public void remove(int key)
  38. {
  39. Link current = head;
  40. do
  41. {
  42. if(current.next.item == key )
  43. Link temp = current.next;
  44. current = temp.next;
  45. if(temp == head)
  46. {
  47. head = head.next;
  48. }
  49. temp = null;
  50. break;
  51. }
  52. current = current.next;
  53. } while(current != head);
  54. }
  55. public int find(int key)
  56. {
  57. Link current = head;
  58. while(current!=null && !(current.item == key))
  59. {
  60. current = current.next;
  61. if(current!= null)
  62. {
  63. return current.item;
  64. }
  65. }
  66. return 0;
  67. }
  68.  
  69. public static void step(Link current)
  70. {
  71. current= current.next;
  72. }
  73. public static void main(String args[])
  74. {String output ="";
  75. output+="it's working";
  76. JOptionPane.showMessageDialog(null,output);
  77. }
  78. }
May 18 '08 #1
2 6414
JosAH
11,448 Expert 8TB
I am trying to create a circular list that includes insert() , find() and remove() methodes. also a step() method that moves current along to the next link.
I still need to display the list by breaking the circle at some arbitrary point to print it on the screen. Does anyone have an idea how to do that?
You have to find a definition of your last node; I'd say that if the next node of a
node is the first node then the node itself happens to be the last node.

kind regards,

Jos
May 18 '08 #2
You have to find a definition of your last node; I'd say that if the next node of a
node is the first node then the node itself happens to be the last node.

kind regards,

Jos
I got the idea.
Thank you very much
May 18 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

16
by: surender | last post by:
Hi, Let us think that we have 100 nodes in the single linked list. I have only 50th node address and i don't have the first node address but i want to print the 49th node data. So how can we print...
19
by: RAJASEKHAR KONDABALA | last post by:
Hi, Does anybody know what the fastest way is to "search for a value in a singly-linked list from its tail" as oposed to its head? I am talking about a non-circular singly-linked list, i.e.,...
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
12
by: shivapadma | last post by:
please anyone tell me whether the following code works for checking the linked list is circular or not? address 0 address1 address2 | A|address1| |B|address2| |C|address0|...
2
by: sathishc58 | last post by:
Hi Can anyone explain me the difference between circular linked list and looped linked list? 1) In Circular linked list Next part of last node will be pointing to the first nodes starting...
12
by: kalyan | last post by:
Hi, I am using Linux + SysV Shared memory (sorry, but my question is all about offset + pointers and not about linux/IPC) and hence use offset's instead on pointers to store the linked list in...
2
by: manpreet25 | last post by:
I doing a program with circular linked list but its not working.In the program there are 6 suitors and there are elimination process proceeds as follows: 123456 ...
0
by: Joseph Guildino | last post by:
Good afternoon all! I have this program in Python that is really starting to make my head go crazy. It utilizes linked lists (circularly linked list). Now, correct me if I am wrong, but a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.