Skip to content

Instantly share code, notes, and snippets.

@G6i7l3a8d
Created February 4, 2025 11:46
Show Gist options
  • Save G6i7l3a8d/e278ab7dca425a7a2edca82da96b3d27 to your computer and use it in GitHub Desktop.
Save G6i7l3a8d/e278ab7dca425a7a2edca82da96b3d27 to your computer and use it in GitHub Desktop.
public class Main {
public static void main(String[] args) {
Queue<Integer> qu = new Queue<>();
qu.Insert(1);
qu.Insert(2);
qu.Insert(3);
print(qu);
print(clone(qu));
System.out.println(isIn(qu , 3));
}
public static <T> Queue<T> clone(Queue<T> qu) {
Queue<T> que = new Queue<>();
Queue<T> que1 = new Queue<>();
while(!qu.IsEmpty()) {
que.Insert(qu.Head());
que1.Insert(qu.Remove());
}
while (!que1.IsEmpty()) {
qu.Insert(que1.Remove());
}
return que;
}
public static <T> void print(Queue<T> qu) {
Queue<T> qu1 = new Queue<>();
System.out.print(qu.Head());
qu1.Insert(qu.Remove());
while (!qu.IsEmpty()) {
System.out.print("," + qu.Head());
qu1.Insert(qu.Remove());
}
while (!qu1.IsEmpty()) {
qu.Insert(qu1.Remove());
}
System.out.println();
}
public static boolean isIn(Queue<Integer> qu , int num) {
Queue<Integer> qu1 = new Queue<>();
boolean b = false;
while (!qu.IsEmpty()) {
if (num == qu.Head())
b = true;
qu1.Insert(qu.Remove());
}
while (!qu1.IsEmpty()) {
qu.Insert(qu1.Remove());
}
return b;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment