Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MyQueue {
- int[] queue;
- int head;
- int tail;
- public MyQueue() {
- queue = new int[10];
- head = 0;
- tail = 0;
- }
- public void push(int x) {
- if(tail < queue.length){
- queue[tail] = x;
- tail++;
- }
- else{
- System.out.println("The queue is full");
- }
- }
- public int pop() {
- if(head < tail){
- int elemento = queue[head];
- head++;
- return elemento;
- }
- else{
- System.out.println("The queue is empty");
- return -1;
- }
- }
- public int peek() {
- if(head < tail){
- return queue[head];
- }
- else {
- System.out.println("The queue is empty");
- return -1;
- }
- }
- public boolean empty() {
- if(head >= tail){
- return true;
- }
- else{
- return false;
- }
- }
- }
- /**
- * Your MyQueue object will be instantiated and called as such:
- * MyQueue obj = new MyQueue();
- * obj.push(x);
- * int param_2 = obj.pop();
- * int param_3 = obj.peek();
- * boolean param_4 = obj.empty();
- */
Add Comment
Please, Sign In to add comment