-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathicanguessthedatastructure.java
64 lines (53 loc) · 2 KB
/
icanguessthedatastructure.java
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
package programming_challenges;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
public class icanguessthedatastructure{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
//create the 3 data structures to test
Queue<Integer> queue = new LinkedList<Integer>();
Stack<Integer> stack = new Stack<Integer>();
PriorityQueue<Integer> pqueue = new PriorityQueue<Integer>();
while(input.hasNext()){
//get the number of input we are going to test
int numLine = input.nextInt();
//this will keep track of the state of the data structures
boolean isQueue = true, isStack = true, isPQueue = true;
//after each loop we clear the data structures
stack.clear();
queue.clear();
pqueue.clear();
for(int i=0; i<numLine; i++){
//getting the command and its value
int command = input.nextInt();
int value = input.nextInt();
//if the command == 1, we push the values
if(command == 1){
if(isQueue) queue.add(value);
if(isStack) stack.push(value);
if(isPQueue) pqueue.add(-value);
}else{//if 2 we remove and test against the value
if(isQueue && !queue.isEmpty()){
if(queue.poll() != value) isQueue = false;
}else isQueue = false;
if(isStack && !stack.isEmpty()){
if(stack.pop() != value) isStack = false;
}else isStack = false;
if(isPQueue && !pqueue.isEmpty()){
if((-1*pqueue.poll()) != value) isPQueue = false;
}else isPQueue = false;
}
}//check which data structure it is
if(!isQueue && !isStack && !isPQueue) System.out.println("impossible");
else if(isQueue && !isStack && !isPQueue) System.out.println("queue");
else if(!isQueue && isStack && !isPQueue) System.out.println("stack");
else if(!isQueue && !isStack && isPQueue) System.out.println("priority queue");
else System.out.println("not sure");
}
//close scanner
input.close();
}
}