Skip to content

Commit 4125b55

Browse files
committed
Added java program for Stack problem Design a Stack with Increment Operation
1 parent 2dbc65b commit 4125b55

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class CustomStack {
2+
int[] stack;
3+
public int top=-1;
4+
public CustomStack(int maxSize) {
5+
stack=new int[maxSize];
6+
}
7+
8+
public void push(int x) {
9+
if(top<stack.length-1)
10+
{
11+
stack[++top]=x;
12+
}
13+
}
14+
15+
public int pop() {
16+
if(top==-1)
17+
return -1;
18+
top--;
19+
return stack[top+1];
20+
}
21+
22+
public void increment(int k, int val) {
23+
for(int i=0;i<(k>top+1?top+1:k);i++) {
24+
stack[i]+=val;
25+
}
26+
}
27+
}
28+
29+
/**
30+
* Your CustomStack object will be instantiated and called as such:
31+
* CustomStack obj = new CustomStack(maxSize);
32+
* obj.push(x);
33+
* int param_2 = obj.pop();
34+
* obj.increment(k,val);
35+
*/

0 commit comments

Comments
 (0)