Assignment-7
Chain is an entity which can hold atmost 50 integers.
The chain enables the user to add and remove integers from both the ends
I.e. front and rear. Define a class chain with the following details.
Class name-Chain
Data member-
ele[]- the array to hold the integer elements.
cap- stores the maximum capacity of the array
front- to point the index of the front
rear- to point the index of the rear
Member methods-
Chain(int max)- constructor to initialize the data n=max,
front=-1,rear=-1 and to create the integer array.
void pushfront(int v)-to add integers from the front index if possible.
Else display the message Full from front.
int popfront()- to remove the return element from front. If array is empty then return -999.
void pushrear(int v)- to add integers from the rear index if possible. Else display the
message Full from rear.
int poprear()- to remove the return element from rear. If array is empty then return -999.
Write a main function to implement the Deque concept.
Source Code:
import java.io.*;
class chain
{
BufferedReader br1=new BufferedReader(new InputStreamReader(System.in));
int DQ []=new int [50];
int front,rear,item,n;
int f2=0;
int f1=0;
int k1=0;
int k2=0;
chain(int max)
{
n=max;
front=-1;
rear=-1;
}
void initialize()
{
for(int i=0;i<n;i++)
{
DQ[i]=-9999;
}
}
void pushrear(int x)
{
item=x;
if(rear==front-1)
{
System.out.println("\nQueue is full from front,so item can't be inserted\n");
}
else
{
if(rear==-1)
{
rear=0;
front=n-1;
DQ[rear]=item;
f1=1;
}
if(rear==0 && f1==0 && k1==0)
{
DQ[rear]=item;
k1=1;
}
else
{
rear=rear+1;
DQ[rear]=item;
}
}
}
int poprear()
{
item=-999;
if(rear==-1)
{
System.out.println("\nQueue is empty from rear,so item can't be deleted\n");
}
else
{
item=DQ[rear];
if(front==rear)
{
front=-1;
rear=-1;
f1=0;
f2=0;
k1=0;
k2=0;
}
else
{
rear=rear-1;
}
}
return item;
}
void pushfront(int x)
{
item=x;
if(front==rear+1)
{
System.out.println("\nQueue is full from rear,so item can't be inserted\n");
}
else
{
if(front==-1)
{
front=n-1;
rear=0;
DQ[front]=item;
f2=1;
}
else if(front==n-1 && f2==0)
DQ[front]=item;
else
{
front=front-1;
DQ[front]=item;
}
}
}
int popfront()
{
item=-999;
if(front==-1 || front==n)
{
System.out.println("\nQueue is empty from front,so item can't be deleted\n");
}
else
{
item=DQ[front];
if(front==rear)
{
front=-1;
rear=-1;
f1=0;
f2=0;
k1=0;
k2=0;
}
else
{
front=front+1;
}
}
return item;
}
void displaydata()
{
for(int i=0;i<n;i++)
{
if(i<front && i>rear)
{
System.out.print(" _");
}
else
{
if(DQ[i]!=-9999)
System.out.print(" "+DQ[i]);
else
System.out.print(" _");
}
}
System.out.println("\n");
}
}
************MAIN FUNCTION*************
class main
{
public static void main(String ars[])throws IOException
{
int x,j=0;
BufferedReader br2=new BufferedReader(new InputStreamReader(System.in));
System.out.println("\n..............DQueue Operations.............\n");
j=1;
System.out.println("Enter maximum capacity of the Queue:");
int max=Integer.parseInt(br2.readLine());
chain ob=new chain(max);
ob.initialize();
while(true)
{
if(j==1)
System.out.println("\n..............DQueue Operations.............");
System.out.println("\n1. Insert at Front");
System.out.println("2. Delete from Front");
System.out.println("3. Insert at Rear");
System.out.println("4. Delete from Rear");
System.out.println("5. Display the queue");
System.out.println("6. Exit");
System.out.println("\nEnter your choice:");
int ch=Integer.parseInt(br2.readLine());
switch(ch)
{
case 1:
System.out.println("\nEnter item:");
x=Integer.parseInt(br2.readLine());
ob.pushfront(x);
break;
case 2:
x=ob.popfront();
if(x!=-999)
System.out.println("\nDeleted element from front is:"+x);
break;
case 3:
System.out.println("\nEnter item:");
x=Integer.parseInt(br2.readLine());
ob.pushrear(x);
break;
case 4:
x=ob.poprear();
if(x!=-999)
System.out.println("\nDeleted element from front is:"+x);
break;
case 5:
System.out.println("\nThe Queue is:\n");
ob.displaydata();
break;
case 6: System.exit(0);
}
}
}
}
OUTPUT:
F:\>javac assignment7.java
F:\>java main
.............DQueue Operations.............
Enter maximum capacity of the Queue:
5
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
1
Enter item:
44
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
1
Enter item:
55
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
5
The Queue is:
_ _ _ 55 44
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
3
Enter item:
77
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
3
Enter item:
88
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
5
The Queue is:
77 88 _ 55 44
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
1
Enter item:
99
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
3
Enter item:
11
Queue is full from front,so item can't be inserted
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
5
The Queue is:
77 88 99 55 44
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
2
Deleted element from front is:99
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
5
The Queue is:
77 88 _ 55 44
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
2
Deleted element from front is:55
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
5
The Queue is:
77 88 _ _ 44
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
4
Deleted element from front is:88
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
5
The Queue is:
77 _ _ _ 44
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
4
Deleted element from front is:77
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
5
The Queue is:
_ _ _ _ 44
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
4
Queue is empty from rear,so item can't be deleted
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
5
The Queue is:
_ _ _ _ 44
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
2
Deleted element from front is:44
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
5
The Queue is:
_ _ _ _ _
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
2
Queue is empty from front,so item can't be deleted
..............DQueue Operations.............
1. Insert at Front
2. Delete from Front
3. Insert at Rear
4. Delete from Rear
5. Display the queue
6. Exit
Enter your choice:
6
0 comments