-
Notifications
You must be signed in to change notification settings - Fork 8
/
Queue.java
126 lines (125 loc) · 3.01 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
124
125
126
//*******************************************************
// Queue.java
// A class that represents an implementation of a Queue in java
// Author: liron mizrahi
//*******************************************************
public class Queue
{
int[] _queue;
int _size; // total number of elements in queue
int _front;
int _rear;
/**
* constractor of the class queue
* @param int
* @return None
*/
public Queue(int sizeOfArray)
{
_queue = new int[sizeOfArray];
_rear = -1;
_front = -1;
_size = 0;
}//end of method Queue
/**
* method return the size of the queue
* @param None
* @return int
*/
public int getSize()
{
return _size;
}// end of method getSize
/**
* return true if the queue is empty
* @param None
* @return boolean
*/
public boolean isEmpty()
{
boolean response = false;
// if the size in 0 there the queue is empty
if(_size == 0)
{
response = true;
}
return response;
}//end of method isEmpty
/**
* method is insert value to queue
* @param int
* @return boolean
*/
public boolean enQueue(int element)
{
boolean response = false;
// checking if the rear is not equeal the the final inded
if(_rear != _queue.length - 1)
{
_rear++;
_queue[_rear] = element;
_size = _size + 1;
response = true;
}
return response;
}//end of method enQueue
/**
* method is removing value from queue
* @param None
* @return int
*/
public int deQueue()
{
int response = 0;
// checking if the queue is not empty
if(_size != 0)
{
_front++;
response = _queue[_front];
_size--;
}
return response;
}// end of method deQueue
/**
* method is print the queue elements
* @param None
* @return None
*/
public void show()
{
System.out.println("Elements: ");
// loopint the queue elements and prints them
for(int i = 0; i < _size; i++)
{
System.out.print(_queue[i] + " ");
}
}// end of method show
/**
* method is show the first element in queue
* @param None
* @return int
*/
public int peek()
{
int response = 0;
if(!isEmpty())
{
response = _queue[_front + 1];
}
return response;
}// end of method peek
public static void main(String[] args)
{
// making queue object
Queue queue = new Queue(10);
// insert element to the queue
System.out.println(queue.enQueue(1000)); // return true
System.out.println(queue.enQueue(2000)); // retrun true
System.out.println(queue.enQueue(500)); // return true
System.out.println(queue.getSize()); // return 3
System.out.println(queue.peek()); // return 1000
queue.show(); // return 1000, 2000, 500
queue.isEmpty(); //return false
System.out.println(queue.deQueue() + " deleted from the list"); // return 1000 deleted from the list
}// end of method main
}// end of class Queue