-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue_Java.java
More file actions
166 lines (156 loc) · 4.49 KB
/
Queue_Java.java
File metadata and controls
166 lines (156 loc) · 4.49 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
public class Queue_Java {
public static class stack{
static Deque<Integer> dq = new LinkedList<Integer>();
public static void push(int data){
dq.addLast(data);
}
public static int pop(){
return dq.removeLast();
}
public static int peek(){
return dq.getLast();
}
}
public static class queue{
static Deque<Integer> dq = new LinkedList<Integer>();
public static void add(int data){
dq.addLast(data);
}
public static int remove(){
return dq.removeFirst();
}
public static int peek(){
return dq.getFirst();
}
}
public static void FirstNon_Repeatingletter(String str){
int frequency[]= new int[26];
Queue<Character>q = new LinkedList<>();
for(int i = 0 ; i<str.length();i++){
char c = str.charAt(i);
q.add(c);
frequency[c-'a']++;
while(!q.isEmpty()&&frequency[q.peek()-'a']>1){
q.remove();
}
if(q.isEmpty()){
System.out.println(-1);
}
else{
System.out.println(q.peek());
}
}
}
public static void InterLink(Queue<Integer>q){
Queue<Integer>helping = new LinkedList<>();
int size = q.size();
for(int i = 0; i<size/2;i++){
helping.add(q.remove());
}
while(!helping.isEmpty()){
q.add(helping.remove());
q.add(q.remove());
}
}
public static void Rev(Queue<Integer>q){
//useing recursion to reverse the queue
if(q.isEmpty()){
return;
}
int x = q.remove();
Rev(q);
System.out.println(x);
}
public static void rev2(Queue<Integer>q){
// use stack to reverse the queue
Stack<Integer>s = new Stack<>();
while(!q.isEmpty()){
s.push(q.remove());
}
while(!s.isEmpty()){
q.add(s.pop());
}
}
public static void Minrop(int n,int arr[]){
PriorityQueue<Integer>q = new PriorityQueue<>();
for(int i =0;i<n;i++){
q.add(arr[i]);
}
int res = 0;
while(q.size()>1){
int first = q.poll();
int second = q.poll();
res+=first+second;
q.add(first+second);
}
System.out.println("the resulted cost -->"+res);
}
public static void binaraynum(int n){
Queue<String>q = new LinkedList<>();
q.add("1");
while(n-->0){
String s1 = q.remove();
System.out.println(s1);
String s2 = s1;
q.add(s1+"0");
q.add(s2+"1");
}
}
public static void sameorder(Queue<Integer>q,int n ){
if(q.isEmpty()){
return;
}
Queue<Integer>helping = new LinkedList<>();
for(int i = 0 ; i<n;i++){
helping.add(q.remove());
}
rev2(helping);
while(!helping.isEmpty()){
q.add(helping.remove());
}
int i=0;
while(i<n){
i++;
q.add(q.remove());
}
}
public static void Maxnum(int arr[],int k , int n,Queue<Integer>q){
Deque<Integer>dq = new LinkedList<>();
int i;
for(i=0;i<k;i++){
while(!dq.isEmpty()&&arr[i]>=arr[dq.peekLast()]){
dq.remove();
}
dq.addLast(i);
}
for(;i<n;++i){
System.out.println(arr[dq.peek()]+" ");
while(!dq.isEmpty()&&dq.peek()<=i-k){
dq.removeFirst();
}
while(!dq.isEmpty()&&arr[i]>=arr[dq.peekLast()]){
dq.remove();
}
dq.addLast(i);
}
System.out.println(arr[dq.peek()]+" ");
}
public static void main(String arg[]){
Queue<Integer>q = new LinkedList<>();
// q.add(10);q.add(20);q.add(30);q.add(40);q.add(50);q.add(60);q.add(70);q.add(80);q.add(90);q.add(100);
// int n = 5;
// sameorder(q, n);
int arr[]= {1,2,3,1,4,5,2,3,6};
Maxnum(arr, 3, 9, q);
while(!q.isEmpty()){
System.out.println(q.remove());
}
System.out.println();
}
}