-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.java
executable file
·123 lines (113 loc) · 3.4 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* queue data structure
* working: first in first out
* class implement following interface
* methods:
* createQueue: re-create queue wash previous values
* enqueue: store element on back of queue
* dequeue: remove front element of queue
* peek: return front element of queue
* isempty : return true if queue has no element
* isFull : return true if queue is full no capacity to store further
* display : print elements of queue on screen
* toString : return elements of queue in string format
* */
interface QueueI<E> {
public void createQueue();
public void enqueue ( E e );
public E dequeue ();
public E peek();
public boolean isEmpty();
public boolean isFull();
}
public class Queue<E> implements QueueI<E>{
private int numberOfQueueElements = 0;
private int front = 0;
private int rear = -1;
private E[] aQueue;
Queue() {
this.aQueue = (E[])new Object[10];
this.numberOfQueueElements = 0;
this.front = 0;
this.rear = -1;
}
Queue( int size ) {
if (size<=0) {
System.out.println("Size of array not allowed");
size = 10;
}
this.aQueue = (E[])new Object[size];
this.numberOfQueueElements = 0;
this.front = 0;
this.rear = -1;
}
@Override
public void createQueue() {
this.aQueue = (E[])new Object[aQueue.length];
this.numberOfQueueElements = 0;
this.front = 0;
this.rear = -1;
}
@Override
public void enqueue(E e) {
if ( isFull() ) {
System.out.println(String.format("Array is full"));
} else {
// this.rear ++;
this.rear = (this.rear + 1)% aQueue.length;
this.aQueue[this.rear] = e;
this.numberOfQueueElements ++;
}
}
@Override
public E dequeue() {
if ( isEmpty() ) {
throw new NullPointerException("Can't dequeue queue its Empty!!");
} else {
this.numberOfQueueElements --;
E e = this.aQueue[this.front];
// this.front ++;
this.front = (this.front + 1)% aQueue.length;
return e;
}
}
@Override
public E peek() {
if ( isEmpty() )
throw new NullPointerException("Can't peek at front item as the queue is Empty!!");
else
return this.aQueue[this.front];
}
@Override
public boolean isEmpty() {
return ( this.getQueueSize() == 0 );
}
@Override
public boolean isFull() {
return ( this.numberOfQueueElements == aQueue.length);
}
public void displayQueue() {
System.out.println( this.toString() );
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder( "\n" );
//int tmp = this.getStackSize() - 1;
if(isEmpty())
{
sb.append("The queue is Empty!!");
}
else
{
int qPos = this.front;
for(int element = 0; element < this.numberOfQueueElements; element++) {
sb.append( this.aQueue[ qPos ] + "\n");
qPos = (qPos + 1)% aQueue.length;
}
}
return sb.toString();
}
public int getQueueSize() {
return this.numberOfQueueElements;
}
}