-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueueLinkedList
More file actions
39 lines (32 loc) · 1.28 KB
/
QueueLinkedList
File metadata and controls
39 lines (32 loc) · 1.28 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
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class QueueLinkedList {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
Queue<String> q = new LinkedList <String>();
Queue<String> q1 = new LinkedList <String>();
String[] animals = {"Bear","Giraffe","Lion","Tiger","Snake"};
for(String x : animals) {
q.offer(x);
}
System.out.println("Queue: " + q);
System.out.print("Enter the animal name you want to check: ");
String name = console.next();
int index =0;
int found = 0;
for(int x = 0; x < animals.length; x++) {
String name1 = q.poll();
if(name.compareTo(name1) == 0) {
index++;
found++;
break;
}
else {index++;}
name1 = "";
}
System.out.println("Animal Found? " + (found == 1 ? "YES" : "NO"));
System.out.println("Number of polls/remove items from queue: " + index);
System.out.println("Current Queue: " + q);
System.out.println();
//2nd Run same code as the above but different queue