Class Dqueue - ISC COMPUTER PROJECT 2018 1/4
1 import java.io.*;
2 import java.util.Scanner;
3
4 class Dqueue
5 {
6 int Dqueue[]=new int[5];
7 int capacity, front, rear;
8
9 public Dqueue()
10 {
11 int i;
12 for( i=0; i<=4; i++)
13 Dqueue[i]=0;
14
15 front=0; rear= -1;
16 }
17
18 public Dqueue( int nx, int nf, int nr)
19 {
20 capacity=nx; front=0; rear=-1;
21 }
22
23 public void pushFront(int num)
24 {
25 if( front==0 && rear== -1) // condition for queue empty
26 {
27 Dqueue[++rear]=num;
28 }
29 else
30 if (front==0 )
31 {
32 System.out.println("Insertion at Front not possible");
33 return;
34 }
35 else
36 if (front==0 && rear==4)
37 {
38
39 System.out.println("Dqueue overflow");
40
41 return;
42 }
43 else
44 if( front != 0)
45 {
46 Dqueue[--front]=num;
47 }
48 }
49
50 public void pushRear(int num)
51 {
52 if( rear==4 || ( front==0 && rear==4) )
53 {
5 Aug, 2017 10:11:02 PM
Class Dqueue - ISC COMPUTER PROJECT 2018 (continued) 2/4
54
55 System.out.println("Dqueue overflow/insertion at rear
not possible");
56
57 return;
58 }
59 else
60 Dqueue[++rear]=num;
61 }
62
63 public void display()
64 {
65 int i;
66 if( front==0 && rear== -1 )
67 {
68
69 System.out.println("Dqueue underflow");
70
71 }
72
73 for( i= front; i<=rear; i++)
74 System.out.print( Dqueue[i] + " ");
75 }
76
77 public int removeFront()
78 {
79 int value;
80 if (front > rear)
81 {
82
83 System.out.println("Dqueue underflow");
84 return -999;
85 }
86 else
87 if ( front==rear )
88 {
89
90 System.out.print("Deleted element is" );
91 value= Dqueue[front];
92 front=0; rear=-1;
93 return value;
94 }
95 else
96 {
97
98 System.out.print("Deleted element is ");
99 return Dqueue[front++];
100 }
101 }
102
103 public int removeRear()
104 {
105 int value;
5 Aug, 2017 10:11:02 PM
Class Dqueue - ISC COMPUTER PROJECT 2018 (continued) 3/4
106 if (front > rear)
107 {
108
109 System.out.println("Dqueue underflow");
110 return -999;
111 }
112 else
113 if ( front==rear )
114 {
115
116 System.out.print("Deleted element is" );
117 value= Dqueue[front];
118 front=0; rear=-1;
119 return value;
120 }
121 else
122 {
123
124 System.out.print("Deleted element is ");
125 return Dqueue[rear--];
126 }
127 }
128
129 public static void main(String args[])
130 {
131 Scanner sc=new Scanner(System.in);
132 int value, choice;
133 Dqueue dq=new Dqueue();
134
135 while(true)
136 {
137 System.out.println("1 for Insertion at Front");
138 System.out.println("2 for Insertion at Rear");
139 System.out.println("3 for Removal from Front");
140 System.out.println("4 for Removal from Rear");
141 System.out.println("5 for displaying the elements");
142 System.out.println("6 Exit");
143 System.out.println("Enter your choice");
144 choice=sc.nextInt();
145
146 switch(choice)
147 {
148 case 1:
149 System.out.println("Enter the value to
be inserted");
150 value=sc.nextInt();
151 dq.pushFront(value); break;
152 case 2:
153 System.out.println("Enter the value t
o be inserted");
154 value=sc.nextInt();
155 dq.pushRear(value); break;
156 case 3:
5 Aug, 2017 10:11:02 PM
Class Dqueue - ISC COMPUTER PROJECT 2018 (continued) 4/4
157 value= dq.removeFront();
158 System.out.println( value);
159
160 break;
161 case 4:
162 value=dq.removeRear();
163 System.out.println(value);
164
165 break;
166 case 5:
167 dq.display();
168
169 case 6: System.exit(0);
170 }
171 }
172 }
173 }
5 Aug, 2017 10:11:02 PM