forked from shikaruki/Hactoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cir_linkedlist.java
94 lines (83 loc) · 2.3 KB
/
cir_linkedlist.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
import java.util.*;
class cir_linkedlist
{
static class Node
{
int data;
Node link;
}
static class Queue
{
Node front, rear;
}
// Function to create Circular queue
static void enQueue(Queue q, int value)
{
Node temp = new Node();
temp.data = value;
if (q.front == null)
q.front = temp;
else
q.rear.link = temp;
q.rear = temp;
q.rear.link = q.front;
}
// Function to delete element from Circular Queue
static int deQueue(Queue q)
{
if (q.front == null)
{
System.out.printf("Queue is empty");
return Integer.MIN_VALUE;
}
// If this is the last node to be deleted
int value; // Value to be dequeued
if (q.front == q.rear)
{
value = q.front.data;
q.front = null;
q.rear = null;
}
else // There are more than one nodes
{
Node temp = q.front;
value = temp.data;
q.front = q.front.link;
q.rear.link = q.front;
}
return value;
}
// Function displaying the elements of Circular Queue
static void displayQueue(Queue q)
{
Node temp = q.front;
System.out.printf("\nElements in Circular Queue are: ");
while (temp.link != q.front)
{
System.out.printf("%d ", temp.data);
temp = temp.link;
}
System.out.printf("%d", temp.data);
}
/* Driver of the program */
public static void main(String args[])
{
// Create a queue and initialize front and rear
Queue q = new Queue();
q.front = q.rear = null;
// Inserting elements in Circular Queue
enQueue(q, 14);
enQueue(q, 22);
enQueue(q, 6);
// Display elements present in Circular Queue
displayQueue(q);
// Deleting elements from Circular Queue
System.out.printf("\nDeleted value = %d", deQueue(q));
System.out.printf("\nDeleted value = %d", deQueue(q));
// Remaining elements in Circular Queue
displayQueue(q);
enQueue(q, 9);
enQueue(q, 20);
displayQueue(q);
}
}