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










share|improve this question




























    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










    share|improve this question


























      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










      share|improve this question















      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






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 5 at 2:03

























      asked Nov 5 at 1:57









      Yogesh Darji

      732720




      732720
























          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.






          share|improve this answer








          New contributor




          AbsoluteSpace is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • That makes sense, I tried this if((int)st.pop() == st2.peek()) and it works fine
            – Yogesh Darji
            Nov 5 at 2:39













          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














           

          draft saved


          draft discarded


















          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
































          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.






          share|improve this answer








          New contributor




          AbsoluteSpace is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • That makes sense, I tried this if((int)st.pop() == st2.peek()) and it works fine
            – Yogesh Darji
            Nov 5 at 2:39

















          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.






          share|improve this answer








          New contributor




          AbsoluteSpace is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.


















          • That makes sense, I tried this if((int)st.pop() == st2.peek()) and it works fine
            – Yogesh Darji
            Nov 5 at 2:39















          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.






          share|improve this answer








          New contributor




          AbsoluteSpace is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          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.







          share|improve this answer








          New contributor




          AbsoluteSpace is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          share|improve this answer



          share|improve this answer






          New contributor




          AbsoluteSpace is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.









          answered Nov 5 at 2:26









          AbsoluteSpace

          673




          673




          New contributor




          AbsoluteSpace is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.





          New contributor





          AbsoluteSpace is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          AbsoluteSpace is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.












          • 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


















          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




















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          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




















































































          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()