-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack.java
More file actions
51 lines (49 loc) · 1.23 KB
/
Stack.java
File metadata and controls
51 lines (49 loc) · 1.23 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
import java.util.Arrays;
public class Stack {
private int top;
private int default_size=6;
public int ar[];
Stack(){
top=-1;
this.ar=new int[default_size];
}
public boolean isFull(){
return top==ar.length-1;
}
public boolean push(int element) throws Exception
{
if(isFull())
throw new Exception("bhar gayi hai be");
ar[++top]=element;
return true;
}
public boolean isEmpty(){
return top==-1;
}
public int pop() throws Exception
{
if(isEmpty())
throw new Exception("khali hai be");
return ar[top--];
}
// public int[] show(){
// return ar;
// }
public void reverse_stack(int i) throws Exception{
if (top==-1)
return;
int a= ar[i++];
pop();
reverse_stack(i);
push(a);
}
public static void main(String[] args) throws Exception {
Stack s=new Stack();
for (int i = 0; i <s.ar.length ; i++) {
s.push(i+1);
}
System.out.println("stack before reversing-"+Arrays.toString(s.ar));
s.reverse_stack(0);
System.out.println("stack after reversing-"+Arrays.toString(s.ar));
}
}