mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-09 12:11:28 +08:00
@ -130,6 +130,20 @@ class Queue {
|
|||||||
public int getSize() {
|
public int getSize() {
|
||||||
return nItems;
|
return nItems;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("[");
|
||||||
|
for (int i = front; ; i = ++i % maxSize) {
|
||||||
|
sb.append(queueArray[i]).append(", ");
|
||||||
|
if (i == rear) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
sb.replace(sb.length() - 2, sb.length(), "]");
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -143,7 +157,7 @@ public class Queues {
|
|||||||
*
|
*
|
||||||
* @param args Command line arguments
|
* @param args Command line arguments
|
||||||
*/
|
*/
|
||||||
public static void main(String args[]) {
|
public static void main(String[] args) {
|
||||||
Queue myQueue = new Queue(4);
|
Queue myQueue = new Queue(4);
|
||||||
myQueue.insert(10);
|
myQueue.insert(10);
|
||||||
myQueue.insert(2);
|
myQueue.insert(2);
|
||||||
@ -161,5 +175,6 @@ public class Queues {
|
|||||||
|
|
||||||
System.out.println(myQueue.peekFront()); // Will print 2
|
System.out.println(myQueue.peekFront()); // Will print 2
|
||||||
System.out.println(myQueue.peekRear()); // Will print 7
|
System.out.println(myQueue.peekRear()); // Will print 7
|
||||||
|
System.out.println(myQueue.toString()); // Will print [2, 5, 3, 7]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user