-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue2.java
More file actions
58 lines (56 loc) · 922 Bytes
/
queue2.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
import java.util.LinkedList;
import java.util.Stack;
import java.util.Queue;
public class Reverse
{
static Queue<Integer> q;
static void reversefirstkele(int k)
{
if(q.isEmpty()==true || k>q.size())
return;
if(k<=0)
return;
Stack<Integer> s = new Stack<Integer>();
//pushing elements into the stack
for(int i=0;i<k;i++) {
s.push(q.peek());
q.remove();
}
while(!s.empty())
{
q.add(s.peek());
s.pop();
}
for(int i=0;i<q.size()-k;i++)
{
q.add(q.peek());
q.remove();
}
}
static void print()
{
while(!q.isEmpty())
{
System.out.print(q.peek()+" ");
q.remove();
}
}
public static void main(String args[])
{
q=new LinkedList<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);
int k=2;
reversefirstkele(k);
print();
}
}