-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.java
100 lines (82 loc) · 1.96 KB
/
Queue.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import java.util.Arrays;
class Queue {
// int front, rear;
// int []arr;
//
// Queue() {
// front = 0;
// rear = 0;
// arr = new int[6];
// }
// // Enqueue (add) element 'e' at the end of the queue.
// public void enqueue(int e) {
// arr[rear]=e;
// rear++;
// }
// // Dequeue (retrieve) the element from the front of the queue.
// public int dequeue() {
// if(front>=rear) return -1;
// int prev=arr[front];
// if(prev==0) return -1;
//
// front++;
//
//// for(int i=1;i<rear;i++){
//// arr[i-1]=arr[i];
//// }
//
// return prev;
// }
class Node {
public int data;
public Node next;
Node()
{
this.data = 0;
this.next = null;
}
Node(int data)
{
this.data = data;
this.next = null;
}
Node(int data, Node next)
{
this.data = data;
this.next = next;
}
};
Node front;
Node rear;
void push(){};
Queue() {
front =new Node(-1);
rear = front;
}
public static class Solution extends Queue{
public void push(int x) {
Node newnode=new Node(x);
rear.next=newnode;
rear=newnode;
}
public int pop() {
if(front==null || front.next==null || rear==null)return -1;
int prev=front.next.data;
front=front.next;
return prev;
}
}
public static void main(String[] args) {
Solution s =new Solution();
s.push(45);
s.push(100);
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}