-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCircularLinkedQueue.java
More file actions
149 lines (120 loc) Β· 2.04 KB
/
CircularLinkedQueue.java
File metadata and controls
149 lines (120 loc) Β· 2.04 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
import java.util.NoSuchElementException;
public class CircularLinkedQueue<T> implements QueueInterface<T>
{
private Node head;
private Node tail;
public CircularLinkedQueue()
{
head=null;
tail=null;
}
public void enqueue(T entry)
{
Node data = new Node(entry);
data.setNextNode(null);
if(isEmpty())
{
head=data;
tail=data;
tail.setNextNode(head);
}
else
{
tail.setNextNode(data);
tail=data;
tail.setNextNode(head);
}
}
public T dequeue()
{
T toreturn = getFront();
if(head!=null)
{
head.setData(null);
head=head.getNextNode();
tail.setNextNode(head);
if (head==null) {
tail=null;
}
}
return toreturn;
}
public T getFront()
{
if(isEmpty())
{
throw new NoSuchElementException("Queue is Empty");
}
else
return head.getData();
}
public boolean isEmpty()
{
if(head==null&&tail==null)
{
return true;
}
else
return false;
}
public void clear()
{
head=null;
tail=null;
}
public void print()
{
Node current = head;
if(head == null)
{
System.out.println("List is empty");
}
else
{
System.out.println("Nodes of the circular linked list: ");
do
{
System.out.print(" "+ current.data);
current = current.getNextNode();
}
while(current != head);
System.out.println();
}
}
private class Node
{
private T data;
private Node next;
private Node()
{
data=null;
next=null;
}
private Node(T dataPortion)
{
data = dataPortion;
next = null;
}
private Node(T dataPortion, Node linkPortion)
{
data = dataPortion;
next = linkPortion;
}
private T getData()
{
return data;
}
private void setData(T newData)
{
data = newData;
}
private Node getNextNode()
{
return next;
}
private void setNextNode(Node nextNode)
{
next = nextNode;
}
}
}