-
Notifications
You must be signed in to change notification settings - Fork 0
/
CircularSinglyLinkedList.java
143 lines (135 loc) · 4.1 KB
/
CircularSinglyLinkedList.java
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package com.TrX;
public class CircularSinglyLinkedList {
public Node Head;
public Node Tail;
public int size;
//Creation of Circular Singly LinkedList
public Node CreateCircularSinglyLL (int nodeValue){
Node node = new Node();
node.value = nodeValue;
node.next = node;
Head = node;
Tail = node;
size = 1;
return Head;
}
//Insertion in Circular Singly Linked List
public void Insertion(int nodeValue ,int location){
Node node = new Node();
node.value = nodeValue;
if(Head == null){
CreateCircularSinglyLL(nodeValue);
return;
}
else if (location == 0) { //Insertion in the beginning
node.next=Head;
Head=node;
Tail.next = Head;
}
else if (location >=size) { //Insertion in the end
Tail.next = node;
Tail = node;
Tail.next = Head;
} else { //Insertion after a specific node
Node tempNode = Head;
int i=0;
while (i < location) {
tempNode = tempNode.next;
i++;
}
node.next = tempNode.next;
tempNode.next = node;
}
size++;
}
//Traverse the circular Singly Linked List
public void Traverse(){
if(Head != null){
Node tempNode = Head;
for(int i=0 ; i<size ;i++) {
System.out.print(tempNode.value);
if (i != size-1) {
System.out.print("->");
}
tempNode = tempNode.next;
}
System.out.println("\n");
}
else {
System.out.println("Circular Singly Linked List Does not exits!");
}
}
//Searching in Singly Linked List
public boolean Search(int nodeValue){
Node tempNode = Head;
int i = 0;
while(i<size){
if(tempNode.value == nodeValue){
System.out.println("Element found at index :"+i);
return true;
}
tempNode = tempNode.next;
i++;
}
System.out.println("Element Not Found!");
return false;
}
//Deletion from circular Singly Linked List
public void Delete(int location){
if(Head == null){
System.out.println("Circular Singly Linked List is Empty!!");
return;
} else if(location == 0) { //Delete from beginning
Head = Head.next;
Tail.next = Head;
size--;
if(size == 0){
Head = null;
Tail = null;
}
}
else if(location >= size){ //Delete from end
Node tempNode = Head;
for(int i=0;i<size-1;i++){
tempNode=tempNode.next;
if(tempNode == Head){
Tail = Head = null;
size--;
return;
}
tempNode.next = Head;
Tail = tempNode;
size--;
}
}
else{ //Delete a specific node
Node tempNode = new Node();
for(int i=0;i<location-1;i++){
tempNode = tempNode.next;
}
tempNode.next = tempNode.next.next;
size--;
}
}
//Delete Entire Circular Singly Linked List
public void DeleteCSinglyLinkedList(){
if(Head == null){
System.out.println("Linked List not found!");
}
Head = Tail = null;
}
public static class CircularSinglyLLTest {
public static void main(String[] args) {
CircularSinglyLinkedList cSLL1 = new CircularSinglyLinkedList();
cSLL1.CreateCircularSinglyLL(10);
cSLL1.Insertion(20,1);
cSLL1.Insertion(30,2);
cSLL1.Insertion(40,3);
cSLL1.Insertion(50,4);
cSLL1.Traverse();
cSLL1.Search(30);
cSLL1.Delete(0);
cSLL1.Traverse();
}
}
}