forked from ShivangiSingh17/Java-Jet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularLinkedList.java
More file actions
80 lines (65 loc) · 1.2 KB
/
CircularLinkedList.java
File metadata and controls
80 lines (65 loc) · 1.2 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
// Java program for sorted insert in circular linked list
class Node
{
int data;
Node next;
Node(int d)
{
data = d;
next = null;
}
}
class CircularLinkedList
{
Node head;
LinkedList() { head = null; }
void sortedInsert(Node new_node)
{
Node current = head;
if (current == null)
{
new_node.next = new_node;
head = new_node;
}
else if (current.data >= new_node.data)
{
while (current.next != head)
current = current.next;
current.next = new_node;
new_node.next = head;
head = new_node;
}
else
{
while (current.next != head &&
current.next.data < new_node.data)
current = current.next;
new_node.next = current.next;
current.next = new_node;
}
}
void printList()
{
if (head != null)
{
Node temp = head;
do
{
System.out.print(temp.data + " ");
temp = temp.next;
} while (temp != head);
}
}
public static void main(String[] args)
{
LinkedList list = new LinkedList();
int arr[] = new int[] {12, 56, 2, 11, 1, 90};
Node temp = null;
for (int i = 0; i < 6; i++)
{
temp = new Node(arr[i]);
list.sortedInsert(temp);
}
list.printList();
}
}