Last active
August 29, 2015 14:04
-
-
Save pavelnganpi/822e8dbba019e942725e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
public class Queue { | |
int size; | |
int[] queueA; | |
int top; | |
int rear; | |
public Queue(int size){ | |
this.size = size; | |
queueA = new int[size]; | |
top = -1; | |
rear = 0; | |
} | |
public void enqueue(int data){ | |
if(size()<this.size){ | |
top++; | |
queueA[top] = data; | |
} | |
else{ | |
System.out.println("full queue"); | |
} | |
} | |
public void dequeue(){ | |
if(top != -1){ | |
System.out.println(queueA[rear]); | |
rear++; | |
top--; | |
} | |
else{ | |
System.out.println("empty queue"); | |
} | |
} | |
public int size(){ | |
System.out.println(top+1); | |
return top+1; | |
} | |
public static void main(String[]args){ | |
Queue q = new Queue(10); | |
q.enqueue(1); | |
q.enqueue(2); | |
q.size(); | |
q.dequeue(); | |
q.dequeue(); | |
q.size(); | |
q.dequeue(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i am not happy with this code its so bad logic......
What this issue here
suppose max queue size is 5
Push 5 item then queue is full
now pop 2-3 items so there is a space now become but your logic still say its not empty
use queue in circular fashion to solve this problem