Implementation of minstack with two stacks which is failing at Pop condition
up vote
1
down vote
favorite
I am implementing a minstack algorithm and across something strange, Maybe I am missing either 'Some Stack concept' or 'Some Java concept' here. I am making using of two stacks(st and st1) to perform my minstack operations. Below is my pop method which fails at if condition:
Method 1 - FAILS:
public void pop() {
if(st.pop() == st2.peek())
st2.pop();
}
Below is the Pop method which works fine when I store the st.pop() results in a variable 'a':
Method 2 - PASSES:
public void pop() {
int a = st.pop();
if(a == st2.peek())
st2.pop();
}
Please assume that there are integer elements in both the stacks and the value from st.pop()
is equal to st2.peek()
. I would like the understand the difference as to why the st.pop()
would not work in if state of method 1 and works in method 2 after storing results of st.pop()
into a temporary variable a
java algorithm data-structures stack
add a comment |
up vote
1
down vote
favorite
I am implementing a minstack algorithm and across something strange, Maybe I am missing either 'Some Stack concept' or 'Some Java concept' here. I am making using of two stacks(st and st1) to perform my minstack operations. Below is my pop method which fails at if condition:
Method 1 - FAILS:
public void pop() {
if(st.pop() == st2.peek())
st2.pop();
}
Below is the Pop method which works fine when I store the st.pop() results in a variable 'a':
Method 2 - PASSES:
public void pop() {
int a = st.pop();
if(a == st2.peek())
st2.pop();
}
Please assume that there are integer elements in both the stacks and the value from st.pop()
is equal to st2.peek()
. I would like the understand the difference as to why the st.pop()
would not work in if state of method 1 and works in method 2 after storing results of st.pop()
into a temporary variable a
java algorithm data-structures stack
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I am implementing a minstack algorithm and across something strange, Maybe I am missing either 'Some Stack concept' or 'Some Java concept' here. I am making using of two stacks(st and st1) to perform my minstack operations. Below is my pop method which fails at if condition:
Method 1 - FAILS:
public void pop() {
if(st.pop() == st2.peek())
st2.pop();
}
Below is the Pop method which works fine when I store the st.pop() results in a variable 'a':
Method 2 - PASSES:
public void pop() {
int a = st.pop();
if(a == st2.peek())
st2.pop();
}
Please assume that there are integer elements in both the stacks and the value from st.pop()
is equal to st2.peek()
. I would like the understand the difference as to why the st.pop()
would not work in if state of method 1 and works in method 2 after storing results of st.pop()
into a temporary variable a
java algorithm data-structures stack
I am implementing a minstack algorithm and across something strange, Maybe I am missing either 'Some Stack concept' or 'Some Java concept' here. I am making using of two stacks(st and st1) to perform my minstack operations. Below is my pop method which fails at if condition:
Method 1 - FAILS:
public void pop() {
if(st.pop() == st2.peek())
st2.pop();
}
Below is the Pop method which works fine when I store the st.pop() results in a variable 'a':
Method 2 - PASSES:
public void pop() {
int a = st.pop();
if(a == st2.peek())
st2.pop();
}
Please assume that there are integer elements in both the stacks and the value from st.pop()
is equal to st2.peek()
. I would like the understand the difference as to why the st.pop()
would not work in if state of method 1 and works in method 2 after storing results of st.pop()
into a temporary variable a
java algorithm data-structures stack
java algorithm data-structures stack
edited Nov 5 at 2:03
asked Nov 5 at 1:57
Yogesh Darji
732720
732720
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
If I understand the question correct, st.pop()
doesn't work in the first method because the type of elements in st are never explicitly stated (ie. st could be initialized like Stack st = new Stack();
).
As a result, any object (doesn't have to be an int) could be pushed onto the stack, so when the stack is popped, it's unclear what data type is returned, which causes the if statement to fail.
In the second method, the popped item is explicitly defined as an int in the variable a, so when the comparison is made with a, the if statement works as expected.
New contributor
That makes sense, I tried thisif((int)st.pop() == st2.peek())
and it works fine
– Yogesh Darji
Nov 5 at 2:39
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
If I understand the question correct, st.pop()
doesn't work in the first method because the type of elements in st are never explicitly stated (ie. st could be initialized like Stack st = new Stack();
).
As a result, any object (doesn't have to be an int) could be pushed onto the stack, so when the stack is popped, it's unclear what data type is returned, which causes the if statement to fail.
In the second method, the popped item is explicitly defined as an int in the variable a, so when the comparison is made with a, the if statement works as expected.
New contributor
That makes sense, I tried thisif((int)st.pop() == st2.peek())
and it works fine
– Yogesh Darji
Nov 5 at 2:39
add a comment |
up vote
1
down vote
accepted
If I understand the question correct, st.pop()
doesn't work in the first method because the type of elements in st are never explicitly stated (ie. st could be initialized like Stack st = new Stack();
).
As a result, any object (doesn't have to be an int) could be pushed onto the stack, so when the stack is popped, it's unclear what data type is returned, which causes the if statement to fail.
In the second method, the popped item is explicitly defined as an int in the variable a, so when the comparison is made with a, the if statement works as expected.
New contributor
That makes sense, I tried thisif((int)st.pop() == st2.peek())
and it works fine
– Yogesh Darji
Nov 5 at 2:39
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
If I understand the question correct, st.pop()
doesn't work in the first method because the type of elements in st are never explicitly stated (ie. st could be initialized like Stack st = new Stack();
).
As a result, any object (doesn't have to be an int) could be pushed onto the stack, so when the stack is popped, it's unclear what data type is returned, which causes the if statement to fail.
In the second method, the popped item is explicitly defined as an int in the variable a, so when the comparison is made with a, the if statement works as expected.
New contributor
If I understand the question correct, st.pop()
doesn't work in the first method because the type of elements in st are never explicitly stated (ie. st could be initialized like Stack st = new Stack();
).
As a result, any object (doesn't have to be an int) could be pushed onto the stack, so when the stack is popped, it's unclear what data type is returned, which causes the if statement to fail.
In the second method, the popped item is explicitly defined as an int in the variable a, so when the comparison is made with a, the if statement works as expected.
New contributor
New contributor
answered Nov 5 at 2:26
AbsoluteSpace
673
673
New contributor
New contributor
That makes sense, I tried thisif((int)st.pop() == st2.peek())
and it works fine
– Yogesh Darji
Nov 5 at 2:39
add a comment |
That makes sense, I tried thisif((int)st.pop() == st2.peek())
and it works fine
– Yogesh Darji
Nov 5 at 2:39
That makes sense, I tried this
if((int)st.pop() == st2.peek())
and it works fine– Yogesh Darji
Nov 5 at 2:39
That makes sense, I tried this
if((int)st.pop() == st2.peek())
and it works fine– Yogesh Darji
Nov 5 at 2:39
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53147363%2fimplementation-of-minstack-with-two-stacks-which-is-failing-at-pop-condition%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password