Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* HW5
- * Due: 22 October 2017
- * Problem Header Hash Code: bfd416aa02529df87ee21caadec5ab81
- */
- package hw5;
- public class Queue {
- Node[] arr; // circular Queue
- int capacity;
- int front;
- int back;
- int size;
- public Queue(int cap){
- arr = new Node[cap];
- capacity = cap;
- }
- public void enqueue(Node node){
- if (!isFull()){
- if(back < capacity-1){
- arr[back] = node;
- back++;
- }else{
- back = 0;
- arr[back] = node;
- }size++;
- }else{
- System.out.println("Queue Overflow!!!");
- }
- }
- public Node dequeue(){
- if (!isEmpty()){
- Node keep = arr[front];
- while(arr[front]==null){
- front++;
- }
- arr[front] = null;
- front++;
- //if(front==capacity) front=0;
- size--;
- return keep;
- }else{
- System.out.println("Queue Underflow!!!");
- return null;
- }
- }
- public boolean isEmpty(){
- boolean check = false;
- if(size == 0){
- check = true;
- }
- return check;
- }
- public boolean isFull(){
- boolean check2 = false;
- if(size == capacity){
- check2 = true;
- }
- return check2;
- }
- public void printCircularIndices(){
- System.out.println("Front index = " + front + " Back index = " + back);
- }
- public void printQueue(){
- if (!isEmpty()){
- System.out.print("[Front] ");
- if(back>front){
- for(int i=front;i<back;i++){
- System.out.print(arr[i].data);
- }
- }else{
- for(int i=0;i<front;i++){
- System.out.print(arr[i].data);
- }for(int i=back;i<capacity;i++){
- System.out.print(arr[i].data);
- }
- }
- System.out.println("[Back]");
- }else{
- System.out.println("Empty Queue!!!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement