Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit2b3e82f

Browse files
authored
Update Queues.java
1 parentc725d91 commit2b3e82f

File tree

1 file changed

+143
-135
lines changed

1 file changed

+143
-135
lines changed

‎DataStructures/Queues/Queues.java

Lines changed: 143 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,156 @@
11
/**
22
* This implements Queues by using the class Queue.
3-
*
3+
*<p>
44
* A queue data structure functions the same as a real world queue.
55
* The elements that are added first are the first to be removed.
66
* New elements are added to the back/rear of the queue.
7-
*
8-
* @author Unknown
97
*
8+
* @author Unknown
109
*/
11-
classQueue{
12-
/** Max size of the queue */
13-
privateintmaxSize;
14-
/** The array representing the queue */
15-
privateint[]queueArray;
16-
/** Front of the queue */
17-
privateintfront;
18-
/** Rear of the queue */
19-
privateintrear;
20-
/** How many items are in the queue */
21-
privateintnItems;
22-
23-
/**
24-
* Constructor
25-
*
26-
* @param size Size of the new queue
27-
*/
28-
publicQueue(intsize){
29-
maxSize =size;
30-
queueArray =newint[size];
31-
front =0;
32-
rear = -1;
33-
nItems =0;
34-
}
35-
36-
/**
37-
* Inserts an element at the rear of the queue
38-
*
39-
* @param x element to be added
40-
* @return True if the element was added successfully
41-
*/
42-
publicbooleaninsert(intx){
43-
if(isFull())
44-
returnfalse;
45-
if(rear ==maxSize-1)//If the back of the queue is the end of the array wrap around to the front
46-
rear = -1;
47-
rear++;
48-
queueArray[rear] =x;
49-
nItems++;
50-
returntrue;
51-
}
52-
53-
/**
54-
* Remove an element from the front of the queue
55-
*
56-
* @return the new front of the queue
57-
*/
58-
publicintremove(){//Remove an element from the front of the queue
59-
if(isEmpty()){
60-
System.out.println("Queue is empty");
61-
return -1;
62-
}
63-
inttemp =queueArray[front];
64-
front++;
65-
if(front ==maxSize)//Dealing with wrap-around again
66-
front =0;
67-
nItems--;
68-
returntemp;
69-
}
70-
71-
/**
72-
* Checks what's at the front of the queue
73-
*
74-
* @return element at the front of the queue
75-
*/
76-
publicintpeekFront(){
77-
returnqueueArray[front];
78-
}
79-
80-
/**
81-
* Checks what's at the rear of the queue
82-
*
83-
* @return element at the rear of the queue
84-
*/
85-
publicintpeekRear(){
86-
returnqueueArray[rear];
87-
}
88-
89-
/**
90-
* Returns true if the queue is empty
91-
*
92-
* @return true if the queue is empty
93-
*/
94-
publicbooleanisEmpty(){
95-
return(nItems ==0);
96-
}
97-
98-
/**
99-
* Returns true if the queue is full
100-
*
101-
* @return true if the queue is full
102-
*/
103-
publicbooleanisFull(){
104-
return(nItems ==maxSize);
105-
}
106-
107-
/**
108-
* Returns the number of elements in the queue
109-
*
110-
* @return number of elements in the queue
111-
*/
112-
publicintgetSize(){
113-
returnnItems;
114-
}
10+
classQueue {
11+
/**
12+
* Max size of the queue
13+
*/
14+
privateintmaxSize;
15+
/**
16+
* The array representing the queue
17+
*/
18+
privateint[]queueArray;
19+
/**
20+
* Front of the queue
21+
*/
22+
privateintfront;
23+
/**
24+
* Rear of the queue
25+
*/
26+
privateintrear;
27+
/**
28+
* How many items are in the queue
29+
*/
30+
privateintnItems;
31+
32+
/**
33+
* Constructor
34+
*
35+
* @param size Size of the new queue
36+
*/
37+
publicQueue(intsize) {
38+
maxSize =size;
39+
queueArray =newint[size];
40+
front =0;
41+
rear = -1;
42+
nItems =0;
43+
}
44+
45+
/**
46+
* Inserts an element at the rear of the queue
47+
*
48+
* @param x element to be added
49+
* @return True if the element was added successfully
50+
*/
51+
publicbooleaninsert(intx) {
52+
if (isFull())
53+
returnfalse;
54+
if (rear ==maxSize -1)// If the back of the queue is the end of the array wrap around to the front
55+
rear = -1;
56+
rear++;
57+
queueArray[rear] =x;
58+
nItems++;
59+
returntrue;
60+
}
61+
62+
/**
63+
* Remove an element from the front of the queue
64+
*
65+
* @return the new front of the queue
66+
*/
67+
publicintremove() {// Remove an element from the front of the queue
68+
if (isEmpty()) {
69+
System.out.println("Queue is empty");
70+
return -1;
71+
}
72+
inttemp =queueArray[front];
73+
front++;
74+
if (front ==maxSize)//Dealing with wrap-around again
75+
front =0;
76+
nItems--;
77+
returntemp;
78+
}
79+
80+
/**
81+
* Checks what's at the front of the queue
82+
*
83+
* @return element at the front of the queue
84+
*/
85+
publicintpeekFront() {
86+
returnqueueArray[front];
87+
}
88+
89+
/**
90+
* Checks what's at the rear of the queue
91+
*
92+
* @return element at the rear of the queue
93+
*/
94+
publicintpeekRear() {
95+
returnqueueArray[rear];
96+
}
97+
98+
/**
99+
* Returns true if the queue is empty
100+
*
101+
* @return true if the queue is empty
102+
*/
103+
publicbooleanisEmpty() {
104+
return (nItems ==0);
105+
}
106+
107+
/**
108+
* Returns true if the queue is full
109+
*
110+
* @return true if the queue is full
111+
*/
112+
publicbooleanisFull() {
113+
return (nItems ==maxSize);
114+
}
115+
116+
/**
117+
* Returns the number of elements in the queue
118+
*
119+
* @return number of elements in the queue
120+
*/
121+
publicintgetSize() {
122+
returnnItems;
123+
}
115124
}
116125

117126
/**
118127
* This class is the example for the Queue class
119-
*
120-
* @author Unknown
121128
*
129+
* @author Unknown
122130
*/
123-
publicclassQueues{
124-
/**
125-
* Main method
126-
*
127-
* @param args Command line arguments
128-
*/
129-
publicstaticvoidmain(Stringargs[]){
130-
QueuemyQueue =newQueue(4);
131-
myQueue.insert(10);
132-
myQueue.insert(2);
133-
myQueue.insert(5);
134-
myQueue.insert(3);
135-
//[10(front), 2, 5, 3(rear)]
136-
137-
System.out.println(myQueue.isFull());//Will print true
138-
139-
myQueue.remove();//Will make 2 the new front, making 10 no longer part of the queue
140-
//[10, 2(front), 5, 3(rear)]
141-
142-
myQueue.insert(7);//Insert 7 at the rear which will be index 0 because of wrap around
143-
// [7(rear), 2(front), 5, 3]
144-
145-
System.out.println(myQueue.peekFront());//Will print 2
146-
System.out.println(myQueue.peekRear());//Will print 7
147-
}
148-
}
131+
publicclassQueues{
132+
/**
133+
* Main method
134+
*
135+
* @param args Command line arguments
136+
*/
137+
publicstaticvoidmain(Stringargs[]){
138+
QueuemyQueue =newQueue(4);
139+
myQueue.insert(10);
140+
myQueue.insert(2);
141+
myQueue.insert(5);
142+
myQueue.insert(3);
143+
//[10(front), 2, 5, 3(rear)]
144+
145+
System.out.println(myQueue.isFull());//Will print true
146+
147+
myQueue.remove();//Will make 2 the new front, making 10 no longer part of the queue
148+
//[10, 2(front), 5, 3(rear)]
149+
150+
myQueue.insert(7);//Insert 7 at the rear which will be index 0 because of wrap around
151+
// [7(rear), 2(front), 5, 3]
152+
153+
System.out.println(myQueue.peekFront());//Will print 2
154+
System.out.println(myQueue.peekRear());//Will print 7
155+
}
156+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp