-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMyQueue.java
More file actions
57 lines (46 loc) · 979 Bytes
/
MyQueue.java
File metadata and controls
57 lines (46 loc) · 979 Bytes
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
package edu.java.chap3;
import java.util.Stack;
//Implement a MyQueue class which implements a queue using two stacks
public class MyQueue {
Stack<Object> stack1;
Stack<Object> stack2;
public MyQueue(){
stack1 = new Stack<Object>();
stack2 = new Stack<Object>();
}
public void add(Object item){
stack1.push(item);
}
public Object remove(int num){
return null;
}
public Object get(int num){
stack2.clear();
int size = stack1.size();
for(int i = 0; i < size; i++){
//System.out.println(stack1.pop());
stack2.push(stack1.pop());
}
if(num<size){
Object temp = null;
for(int i = 0; i<size; i++){
if(i<=num){
temp = stack2.pop();
stack1.push(temp);
}
else{
stack1.push(stack2.pop());
}
}
return temp;
}
return null;
}
public static void main(String[] args) {
MyQueue mq = new MyQueue();
mq.add(1);
mq.add(2);
System.out.println(mq.get(0));
System.out.println(mq.get(1));
}
}