- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue2.java
More file actions
Latest commit
58 lines (56 loc) · 922 Bytes
/
Copy pathqueue2.java
File metadata and controls
58 lines (56 loc) · 922 Bytes
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
//finding reverse of queue for first given elements
importjava.util.LinkedList;
importjava.util.Stack;
importjava.util.Queue;
publicclassReverse
{
staticQueue<Integer> q;
staticvoidreversefirstkele(intk)
{
if(q.isEmpty()==true || k>q.size())
return;
if(k<=0)
return;
Stack<Integer> s = newStack<Integer>();
//pushing elements into the stack
for(inti=0;i<k;i++) {
s.push(q.peek());
q.remove();
}
while(!s.empty())
{
q.add(s.peek());
s.pop();
}
for(inti=0;i<q.size()-k;i++)
{
q.add(q.peek());
q.remove();
}
}
staticvoidprint()
{
while(!q.isEmpty())
{
System.out.print(q.peek()+" ");
q.remove();
}
}
publicstaticvoidmain(Stringargs[])
{
q=newLinkedList<Integer>();
q.add(1);
q.add(2);
q.add(4);
q.add(6);
q.add(8);
q.add(10);
q.add(12);
q.add(14);
q.add(16);
q.add(18);
intk=2;
reversefirstkele(k);
print();
}
}