-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStack.java
More file actions
59 lines (57 loc) · 926 Bytes
/
MyStack.java
File metadata and controls
59 lines (57 loc) · 926 Bytes
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
class Stack{
private int tos, size;
private int stck[];
Stack(){
stck = new int[10];
size = 10;
tos = -1;
}
Stack(int a){
stck = new int[a];
size = a;
tos = -1;
}
void push(int item){
if(tos == (size - 1)){
System.out.println("STACK IS FULL");
}
else{
++tos;
stck[tos] = item;
System.out.println(item+" added to stack successfully");
}
}
int pop(){
if(tos == -1){
System.out.println("STACK IS EMPTY");
return -999;
}
else
return stck[tos--];
}
void print(){
if(tos == -1){
System.out.println("STACK IS EMPTY");
}
else{
for(int i=0;i<=tos;i++){
System.out.println(stck[i]);
}
}
}
}
class MyStack{
public static void main(String[] args){
Stack s1 = new Stack(Integer.parseInt(args[0]));
Stack s2 = new Stack();
s1.push(30);
s1.push(20);
s2.push(10);
s2.push(20);
s2.pop();
s2.pop();
s2.pop();
s1.print();
s2.print();
}
}