-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularLinkedList.java
More file actions
155 lines (137 loc) · 4.02 KB
/
CircularLinkedList.java
File metadata and controls
155 lines (137 loc) · 4.02 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
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
144
145
146
147
148
149
150
151
152
153
154
155
package com.company;
import java.util.Scanner;
//Node class
class Node{
int data;
Node next;
}
//Main Class
class CircularLinkedList{
static Node head;
//Function to add First element when there is no element in Linked List
static void firstElement(int data){
System.out.println("It is the first element of the linked list and added successfully");
head = new Node();
head.data = data;
head.next = head;
}
// Function to add element at last
static void addLast(int data){
if(head==null){
firstElement(data);
return;
}
Node newNode = new Node();
Node temp = head;
newNode.data = data;
newNode.next = head;
while(temp.next!=head){
temp = temp.next;
}
temp.next = newNode;
System.out.println("Data added to last successfully");
}
//Function to add element at starting
static void addFirst(int data){
Node newNode = new Node();
Node temp = head;
newNode.data = data;
if(temp==null){
firstElement(data);
return;
}
while(temp.next!=head){
temp = temp.next;
}
temp.next = newNode;
newNode.next = head;
head = newNode;
System.out.println("Data added successfully at first");
}
//Function to delete the element
static void delete(int data){
Node temp = head;
if(head==null){
System.out.print("No element in Linked list");
return;
}
do{
if(temp.next.data==data) {
if (temp.next == head && temp == head)
head = null;
else if(temp.next==head) {
head = head.next;
temp.next = head;
}else
temp.next = temp.next.next;
System.out.println(data + " deleted successfully");
return;
}
temp = temp.next;
}while(temp!=head);
System.out.println("Data "+data+" not found");
}
//Function to Search the element
static void search(int data){
Node temp = head;
if(temp==null){
System.out.println("No data in the list");
return;
}
do{
if(temp.data==data){
System.out.println("Data Found");
return;
}
temp = temp.next;
}while(temp!=head);
System.out.println("Data not found");
}
//Function to print the Data in the Linked List
static void print(){
Node temp = head;
if(temp==null){
System.out.println("No data in the linked list");
return;
}
System.out.println("Data stored in linked list are:");
do{
System.out.println(temp.data);
temp = temp.next;
}while(temp!=head);
}
//Main Function
public static void main(String args[]){
Scanner scan = new Scanner(System.in);
while(true){
System.out.println("Enter the command:\n1 - print\n2 - addFirst\n3 - addLast\n4 - delete\n5 - Search\n6 - Exit");
int n = scan.nextInt();
switch (n){
case 1:
print();
break;
case 2:
int d = scan.nextInt();
addFirst(d);
break;
case 3:
d = scan.nextInt();
addLast(d);
break;
case 4:
d = scan.nextInt();
delete(d);
break;
case 5:
d = scan.nextInt();
search(d);
break;
case 6:
scan.close();
return;
default:
System.out.println("Type a valid command");
}
}
}
}