forked from devesh-shetty/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTestStack.java
57 lines (45 loc) · 1.47 KB
/
TestStack.java
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
import structures.Stack;
import java.io.*;
import java.util.Arrays;
import java.util.Random;
class Main{
public static void main(String []args){
PrintWriter out = new PrintWriter(System.out, true);
Stack s1 = new Stack();
for(char i = 'a';i <= 'z'; i++){
//adding the alphabets to the stack
s1.push(i);
}
//should print in reverse
out.println(s1);
Stack s2 = new Stack();
s2.push("One");
s2.push("Two");
s2.push("Three");
out.println(s2);
s2.pop();
s2.push("Four");
out.println(s2);
out.println("\n************Randomized*************");
Stack s3 = new Stack();
Random r = new Random();
for(int i = 0; i <= 10;i++){
int n = r.nextInt(200);
if( (n & 1) == 0 ){
//indicates first bit is 0 so now is even
//even no
out.println("Pushing data = "+n);
s3.push(""+n);
}
else if(!s3.isEmpty()){
//stack is not empty then pop
out.println("Poping "+s3.pop());
}
}
out.println(s3);
out.println("***********************************");
Stack s4 = new Stack();
out.println("isEmpty: "+s4.isEmpty());
s4.pop();//should throw an null pointer exception
}
}