/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/* Write a Program for dynamic circular array queue */
package dynamic_array_queue;
import java.util.Scanner;
/**
*
* @author shabhatn
*/
class dynamic_circular_queue
{
int queue[];
int size ;
int front;
int length;
int rear;
public dynamic_circular_queue(int size)
{
this.size = size;
queue = new int[size];
front = rear = -1;
length =0;
}
public boolean isEmpty()
{
return front==-1;
}
public boolean isFull()
{
return (rear+1)%size==front;
}
public int getsize()
{
return length;
}
public void resize_array()
{
int old_array[];
int oldsize = size;
size = 2*size;
old_array = queue;
queue = new int[size];
for(int i=0;i<old_array.length;i++)
{
queue[i] = old_array[i];
}
if(rear<front)
{
for(int i =0;i<front;i++)
{
queue[i+oldsize] = this.queue[i];
}
rear = rear+oldsize;
}
}
public void enqueue(int data)
{
if(isFull())
{
resize_array();
rear = (rear+1)%size;
queue[rear] = data;
}
else
{
if(front==-1)
{
front=0;
rear=0;
queue[rear]=data;
}
else
{
rear = (rear+1)%size;
queue[rear] = data;
System.out.println(data);
}
length++;
}
}
public void dequeue()
{
if(isEmpty())
{
System.out.println("Queue is empty");
}
else
{
int data = queue[front];
System.out.println("Dequeued data--->" + data);
length--;
if(front==rear)
{
front =-1;
rear = -1;
}
else
{
front = (front+1)%size;
}
}
}
public void display()
{
char choice;
Scanner sc = new Scanner(System.in);
char ch;
do
{
System.out.println("press 1 to enqueue");
System.out.println("press 2 to dequeue");
System.out.println("press 3 to see all inserted data");
int c = sc.nextInt();
switch(c)
{
case 1:
do
{
System.out.print("Insert data");
int data = sc.nextInt();
enqueue(data);
System.out.print("Do you want to insert more data");
choice = sc.next().charAt(0);
}while(choice =='Y'||choice=='y');
break;
case 2:
do
{
dequeue();
System.out.print("Do you want to delete more data");
choice = sc.next().charAt(0);
}while(choice =='Y'||choice=='y');
break;
case 3:
if(front>=0)
{
for(int i=front;i<queue.length && front!=-1;i++)
{
System.out.println("-->"+queue[i]);
}
}
if(front>=0&&rear<=front)
{
for(int i=0;i<=rear;i++)
{
System.out.println("-->"+queue[i]);
}
}
break;
default:
System.out.print("invalid input exit");
break;
}
System.out.print("Do you want to go back to menu press y");
ch = sc.next().charAt(0);
}while(ch=='Y'||ch=='y');
}
}
public class Dynamic_array_queue {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.print("Enter the size of queue");
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
dynamic_circular_queue aq = new dynamic_circular_queue(size);
aq.display();
}
}
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/* Write a Program for dynamic circular array queue */
package dynamic_array_queue;
import java.util.Scanner;
/**
*
* @author shabhatn
*/
class dynamic_circular_queue
{
int queue[];
int size ;
int front;
int length;
int rear;
public dynamic_circular_queue(int size)
{
this.size = size;
queue = new int[size];
front = rear = -1;
length =0;
}
public boolean isEmpty()
{
return front==-1;
}
public boolean isFull()
{
return (rear+1)%size==front;
}
public int getsize()
{
return length;
}
public void resize_array()
{
int old_array[];
int oldsize = size;
size = 2*size;
old_array = queue;
queue = new int[size];
for(int i=0;i<old_array.length;i++)
{
queue[i] = old_array[i];
}
if(rear<front)
{
for(int i =0;i<front;i++)
{
queue[i+oldsize] = this.queue[i];
}
rear = rear+oldsize;
}
}
public void enqueue(int data)
{
if(isFull())
{
resize_array();
rear = (rear+1)%size;
queue[rear] = data;
}
else
{
if(front==-1)
{
front=0;
rear=0;
queue[rear]=data;
}
else
{
rear = (rear+1)%size;
queue[rear] = data;
System.out.println(data);
}
length++;
}
}
public void dequeue()
{
if(isEmpty())
{
System.out.println("Queue is empty");
}
else
{
int data = queue[front];
System.out.println("Dequeued data--->" + data);
length--;
if(front==rear)
{
front =-1;
rear = -1;
}
else
{
front = (front+1)%size;
}
}
}
public void display()
{
char choice;
Scanner sc = new Scanner(System.in);
char ch;
do
{
System.out.println("press 1 to enqueue");
System.out.println("press 2 to dequeue");
System.out.println("press 3 to see all inserted data");
int c = sc.nextInt();
switch(c)
{
case 1:
do
{
System.out.print("Insert data");
int data = sc.nextInt();
enqueue(data);
System.out.print("Do you want to insert more data");
choice = sc.next().charAt(0);
}while(choice =='Y'||choice=='y');
break;
case 2:
do
{
dequeue();
System.out.print("Do you want to delete more data");
choice = sc.next().charAt(0);
}while(choice =='Y'||choice=='y');
break;
case 3:
if(front>=0)
{
for(int i=front;i<queue.length && front!=-1;i++)
{
System.out.println("-->"+queue[i]);
}
}
if(front>=0&&rear<=front)
{
for(int i=0;i<=rear;i++)
{
System.out.println("-->"+queue[i]);
}
}
break;
default:
System.out.print("invalid input exit");
break;
}
System.out.print("Do you want to go back to menu press y");
ch = sc.next().charAt(0);
}while(ch=='Y'||ch=='y');
}
}
public class Dynamic_array_queue {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.print("Enter the size of queue");
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
dynamic_circular_queue aq = new dynamic_circular_queue(size);
aq.display();
}
}
No comments:
Post a Comment