/*
* 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 Basic Array queue Implementation*/
package basic_queue_implementation;
import java.util.Scanner;
/**
*
* @author shabhatn
*/
class array_queue
{
int size;
int queue[];
int front;
int rear;
int length;
public array_queue(int size)
{
this.size = size;
queue = new int[size];
front = -1;
rear = -1;
length = -1;
}
public boolean isEmpty()
{
return front==-1;
}
public boolean isFull()
{
return front==0 && rear==size-1;
}
public int getSize()
{
return length;
}
public void enqueue(int data)
{
if(isFull())
{
System.out.println("queue is full: no space to enter");
}
else
{
if(front==-1)
{
front =0;
rear =0;
queue[rear] = data;
}
else
{
queue[++rear] = data;
}
length++;
}
}
public void dequeue()
{
int data;
if(isEmpty())
{
System.out.println("No more Items to remove");
}
else
{
data = queue[front];
length--;
if(front==rear)
{
front =-1;
rear=-1;
}
else
{
front++;
}
System.out.print("Element which has been removed is :->" + data);
}
}
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:
for(int i=front;i<=rear && front!=-1;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 Basic_queue_implementation {
/**
* @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();
array_queue aq = new array_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 Basic Array queue Implementation*/
package basic_queue_implementation;
import java.util.Scanner;
/**
*
* @author shabhatn
*/
class array_queue
{
int size;
int queue[];
int front;
int rear;
int length;
public array_queue(int size)
{
this.size = size;
queue = new int[size];
front = -1;
rear = -1;
length = -1;
}
public boolean isEmpty()
{
return front==-1;
}
public boolean isFull()
{
return front==0 && rear==size-1;
}
public int getSize()
{
return length;
}
public void enqueue(int data)
{
if(isFull())
{
System.out.println("queue is full: no space to enter");
}
else
{
if(front==-1)
{
front =0;
rear =0;
queue[rear] = data;
}
else
{
queue[++rear] = data;
}
length++;
}
}
public void dequeue()
{
int data;
if(isEmpty())
{
System.out.println("No more Items to remove");
}
else
{
data = queue[front];
length--;
if(front==rear)
{
front =-1;
rear=-1;
}
else
{
front++;
}
System.out.print("Element which has been removed is :->" + data);
}
}
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:
for(int i=front;i<=rear && front!=-1;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 Basic_queue_implementation {
/**
* @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();
array_queue aq = new array_queue(size);
aq.display();
}
}
No comments:
Post a Comment