Created
February 4, 2025 11:46
-
-
Save G6i7l3a8d/e278ab7dca425a7a2edca82da96b3d27 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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