Hi,
Can someone tell me If my code is right? It compiles ok, but I am not sure if the implementation is right.
I am trying to write a singly circular linked list class with insert, getfirst, getNext, delete and empty methods. Can someone help me with this? Thank you in advance on your help?
1. public class SingleLinkedCircular {
2.
3. Link head;
4.
5. public void insert(int item) {
6. if (head == null) {
7. head = new Link(item, null);
8. head.next = head;
9. } else {
10. head.next = new Link(item, head.next);
11. }
12. }
13.
14. public void remove(int key) {
15. Link current = head;
16. do {
17. if (current.next.item == key) {
18. Link temp = current.next;
19. current = temp.next;
20. {
21. head = head.next;
22. }
23. temp = null;
24. break;
25. }
26. current = current.next;
27. } while (current != head);
28. }
29.
30. public Link getFirst() {
31. return head;
32.
33. }
34.
35. public void getNext() {
36. Link current = head.next;
37. }
38.
39. public static void main(String args[]) {
The Link Class:
1. public class Link {
2. int item = 0;
3. Link next;
4.
5. public Link() {
6. // this.item = 0;
7. Link next = null;
8. // head = null;
9. }
10.
11. public Link(int i, Link n) {
12. item = i;
13. next = n;
14. }
15. }