-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathKWCircularLLExample.java
More file actions
71 lines (64 loc) · 2.01 KB
/
KWCircularLLExample.java
File metadata and controls
71 lines (64 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.codinginterview;
public class KWCircularLLExample {
NodeL head, tail;
public static void main(String[] args) {
KWCircularLLExample circularLLExample = new KWCircularLLExample();
System.out.println("Circular Linked List Example!");
circularLLExample.displayElements();
circularLLExample.insertElements(10);
circularLLExample.insertElements(20);
System.out.println("******* Display again *******");
circularLLExample.displayElements();
circularLLExample.deleteElements();
System.out.println("******* Display again *******");
circularLLExample.displayElements();
circularLLExample.insertElements(30);
circularLLExample.insertElements(40);
circularLLExample.insertElements(50);
circularLLExample.deleteElements();
System.out.println("******* Display again *******");
circularLLExample.displayElements();
}
void insertElements(int data) {
NodeL nodeL = new NodeL(data);
if (head == null && tail == null) {
head = nodeL;
tail = nodeL;
tail.next = nodeL;
}else{
tail.next = nodeL;
tail = nodeL;
tail.next = head;
}
}
// Delete head Node
void deleteElements(){
if(head == null){
System.out.println("There is nothing to delete!");
}else{
NodeL temp = head;
head = head.next;
tail.next = head;
System.out.println(temp.data + " Delete from the List!");
temp = null;
}
}
void displayElements() {
NodeL temp = head;
if (head == null) {
System.out.println("There is nothing to display!!");
} else {
do {
System.out.println(temp.data + " ");
temp = temp.next;
} while (temp != head);
}
}
}
class NodeL {
int data;
NodeL next;
NodeL(int data) {
this.data = data;
}
}