forked from ritikkhatana79020/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion_3.java
More file actions
178 lines (143 loc) · 3.65 KB
/
Question_3.java
File metadata and controls
178 lines (143 loc) · 3.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package questions;
import java.util.Scanner;
public class Question_3 {
int size ;
int [] stack;
int top=-1;
void set_Size(int size)
{
this.size = size;
stack = new int[size];
}
void push(int data)
{
if(top==-1)
{
top =0;
stack[top] = data;
}
else if(top == size-1)
{
System.out.println("Overflow");
}
else
{
top++;
stack[top] = data;
}
}
int pop()
{
int element=0;
if(top==-1)
{
System.out.println("Underflow");
return 0;
}
else
{
element = stack[top];
top--;
return element;
}
}
void enque(int data)
{
push(data);
}
int deque()
{
Question_3 stack_1 = new Question_3();
stack_1.set_Size(top+1);
for(int i=top;i>-1;i--)
{
stack_1.push(pop());
}
int element = stack_1.pop();
for(int i=stack_1.top;i>-1;i--)
{
push(stack_1.pop());
}
return element;
}
void traversal()
{
if(top==-1)
{
System.out.println("No element is present");
}
else
{
for(int i=0;i<=top;i++)
{
System.out.print(stack[i]+" ");
}
}
}
void peek()
{
if(top==-1)
{
System.out.println("There is no element");
}
else
{
System.out.println("Peek element is : " +stack[top] );
}
}
void size()
{
System.out.println(top+1);
}
public static void main(String ... args)
{
Scanner scan = new Scanner(System.in);
System.out.println("==========================Queue Test==========================\n");
System.out.print("Please enter Size of Integer queue: ");
int capacity = scan.nextInt();
Question_3 queue = new Question_3();
queue.set_Size(capacity);
char ans;
do{
System.out.println("=====================Queue Operations=====================");
System.out.println(">> 1. Enque");
System.out.println(">> 2. Dequeue");
System.out.println(">> 3. Find the peek element in queue ");
System.out.println(">> 4. Find the number of elements in queue");
System.out.print(">> 5. Traversal \n $ ");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
//Enqueue an element
System.out.print("Enter integer element to enque: ");
queue.enque (scan.nextInt());
break;
case 2 :
//Dequeue an element
System.out.print("You have opted to delete an element from the queue is " );
System.out.println(queue.deque());
break;
case 3 :
//showing peek element
queue.peek();
break;
case 4 :
//Show size of queue
queue.size();
break;
case 5 :
//Traverse the queue
queue.traversal();
break;
default :
System.out.println("Wrong Entry \n ");
break;
}
/* display stack */
System.out.print("\nDo you want to continue (Type y or n) : ");
ans= scan.next().charAt(0);
} while (ans == 'Y'|| ans == 'y');
System.out.print("Thank you visit again!!!!! :) " );
scan.close();}
}