-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackOperationUsingJava
More file actions
71 lines (54 loc) · 1.7 KB
/
StackOperationUsingJava
File metadata and controls
71 lines (54 loc) · 1.7 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
import java.util.Stack;
/**
* Created by Ajay RajPut on 29,December,2018
* Bjain Pvt. Ltd. ,
* Noida , India.
*/
public class StackOperationUsingJava {
//push data to stack
static void push_data(Stack<Integer> stack) {
for (int i = 1; i < 6; i++) {
stack.push(i);
}
}
//pop data from stack
static void pop_data(Stack<Integer> stack) {
System.out.println("pop data from stack one by one " );
for (int i = 1; i < 6; i++) {
Integer y = (Integer) stack.pop();
System.out.println("pop data from stack " + y);
}
}
//peek data from stack
static void peek_data(Stack<Integer> stack) {
if (!stack.empty()){
Integer y = (Integer) stack.peek();
System.out.println("peek data from stack " + y);
}
else{
System.out.println("stack is underflow");
}
}
//search data from stack
static void search_data(Stack<Integer> stack,int search) {
if (!stack.empty()) {
Integer y = (Integer) stack.search(search);
System.out.println("search data from stack element "+search +" at "+ y+" position");
}else{
System.out.println("stack is underflow");
}
}
public static void main() {
Stack<Integer> stack = new Stack<>();
System.out.println("push data to stack one by one " );
push_data(stack);
pop_data(stack);
System.out.println("push data to stack one by one " );
push_data(stack);
search_data(stack,4);
search_data(stack,6);
search_data(stack,5);
search_data(stack,2);
peek_data(stack);
}
}