-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.java
65 lines (53 loc) · 1.43 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
public class Queue {
int Queue[]=new int[10];
int front=0;
int rear=-1;
public void enqueue(int data){
if(rear==Queue.length-1){
System.out.println("queue is full");
}else{
rear++;
Queue[rear]=data;
}
}
public void dequeue(){
if(front==-1){
System.out.println("queue is empty");
}else{
front++;
Queue[front]=-1;
}
}
public void search(int data){
int stack_index=front;
while(stack_index>=0){
if(Queue[stack_index]==data){
System.out.println("element found at index "+stack_index);
return;
}
stack_index++;
}
System.out.println("element not found");
}
public int middleoftheQueue(){
return Queue[front+rear/2];
}
public void show() {
for (int i = 0; i < Queue.length; i++) {
if(Queue[i]!=0){
System.out.print(Queue[i]+" ");
}
}
}
public static void main(String[] args) {
int arr[]={1,9,3,5,4,6,2,8};
Queue q=new Queue();
for(int i=0;i<arr.length;i++){
q.enqueue(arr[i]);
}
q.show();
System.out.println();
q.search(5);
System.out.println("middle of the queue "+ q.middleoftheQueue());
}
}