-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue.java
58 lines (53 loc) · 1.23 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
public class Queue
{
private Item front;
private Item rear;
/**
* creates empty queue
*/
public Queue()
{
front = null;
rear = null;
}
/**
* adds new item to rear
*/
public void insert(int i)
{
Item item = new Item(i);
//queue is empty (have to updae front & rear)
if (isEmpty())
front = item;
//queue is not empty (stick the new item to the end of the queue & updae rear)
else
rear.next = item;
rear = item;
}
/**
* removes item from front
*/
public int remove()
{
int temp = -999; //initialize to unusual value
if (isEmpty()) //report an error and quit
{
System.out.println("Error in remove() - queue is empty");
System.err.println("Error in remove() - queue is empty");
System.exit(1);
}
else //remove the item from the front of the queue and update front
{
temp = front.info;
front = front.next;
}
return temp;
}
/**
* returns true if the queue is empty
*/
public Boolean isEmpty()
{
return front == null;
}
}