-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQueue
63 lines (58 loc) · 1.38 KB
/
Queue
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
import java.util.Scanner;
public class Queue {
private int [] items;
private int first;
private int n;
public Queue (int capacity) {
items=new int [capacity];
first=-1;
n=0;
}
public boolean isEmpty() {
return n == 0;
}
public boolean isFull() {
return n == items.length;
}
public int dequeue() {
if(!isEmpty()){
int temp = items[first++];
n--;
if(first==items.length)
first=0;
if(n==0)
first=-1;
return temp;
}
else return 0;
}
public void enqueue(int sayi) {
if(!isFull()) {
if(n==0) {
first++;
items[first]=sayi;
}
else {
items[(first+n)%items.length] = sayi;
}
n++;
}
}
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("Kapasite");
int capacity=scanner.nextInt();
Queue queue=new Queue(capacity);
queue.enqueue(15);
queue.enqueue(25);
queue.enqueue(35);
queue.enqueue(45);
System.out.println("silinen : "+ queue.dequeue());
System.out.println("silinen : "+ queue.dequeue());
queue.enqueue(55);
queue.enqueue(65);
System.out.println("silinen : "+ queue.dequeue());
System.out.println("silinen : "+ queue.dequeue());
System.out.println("silinen : "+ queue.dequeue());
}
}