Implementing a stack with Java

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












I am trying to implement a stack with Java:



public class Stack<E> 

Node top = null;

public void push(E data)
Node node = new Node(data);
if (null != top)
node.next = top;

top = node;


public E pop()
if (null == top)
throw new IllegalStateException("Stack is empty");
else
E data = top.data;
top = top.next;
return data;



public E peek()
if (null == top)
throw new IllegalStateException("Stack is empty");
else
E data = top.data;
return data;



public void print()
if (null == top)
throw new IllegalStateException("Stack is empty");
else
Node node = top;
while(null != node)
System.out.println(node.data);
node = node.next;




private class Node

Node next;
E data;

Node(E data)
this.data = data;






I am looking for feedback for the implementation. Also, I wanted to know if the pop() implementation can cause a memory leak?







share|improve this question



























    up vote
    3
    down vote

    favorite












    I am trying to implement a stack with Java:



    public class Stack<E> 

    Node top = null;

    public void push(E data)
    Node node = new Node(data);
    if (null != top)
    node.next = top;

    top = node;


    public E pop()
    if (null == top)
    throw new IllegalStateException("Stack is empty");
    else
    E data = top.data;
    top = top.next;
    return data;



    public E peek()
    if (null == top)
    throw new IllegalStateException("Stack is empty");
    else
    E data = top.data;
    return data;



    public void print()
    if (null == top)
    throw new IllegalStateException("Stack is empty");
    else
    Node node = top;
    while(null != node)
    System.out.println(node.data);
    node = node.next;




    private class Node

    Node next;
    E data;

    Node(E data)
    this.data = data;






    I am looking for feedback for the implementation. Also, I wanted to know if the pop() implementation can cause a memory leak?







    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I am trying to implement a stack with Java:



      public class Stack<E> 

      Node top = null;

      public void push(E data)
      Node node = new Node(data);
      if (null != top)
      node.next = top;

      top = node;


      public E pop()
      if (null == top)
      throw new IllegalStateException("Stack is empty");
      else
      E data = top.data;
      top = top.next;
      return data;



      public E peek()
      if (null == top)
      throw new IllegalStateException("Stack is empty");
      else
      E data = top.data;
      return data;



      public void print()
      if (null == top)
      throw new IllegalStateException("Stack is empty");
      else
      Node node = top;
      while(null != node)
      System.out.println(node.data);
      node = node.next;




      private class Node

      Node next;
      E data;

      Node(E data)
      this.data = data;






      I am looking for feedback for the implementation. Also, I wanted to know if the pop() implementation can cause a memory leak?







      share|improve this question













      I am trying to implement a stack with Java:



      public class Stack<E> 

      Node top = null;

      public void push(E data)
      Node node = new Node(data);
      if (null != top)
      node.next = top;

      top = node;


      public E pop()
      if (null == top)
      throw new IllegalStateException("Stack is empty");
      else
      E data = top.data;
      top = top.next;
      return data;



      public E peek()
      if (null == top)
      throw new IllegalStateException("Stack is empty");
      else
      E data = top.data;
      return data;



      public void print()
      if (null == top)
      throw new IllegalStateException("Stack is empty");
      else
      Node node = top;
      while(null != node)
      System.out.println(node.data);
      node = node.next;




      private class Node

      Node next;
      E data;

      Node(E data)
      this.data = data;






      I am looking for feedback for the implementation. Also, I wanted to know if the pop() implementation can cause a memory leak?









      share|improve this question












      share|improve this question




      share|improve this question








      edited Jul 14 at 19:24









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Jul 14 at 18:48









      Karan Khanna

      1213




      1213




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          5
          down vote














          I wanted to know if the pop() implementation can cause a memory leak?




          It will not cause a memory leak, because java is a garbage collected language. When top is no longer accessible the garbale collecter will free top if it sees fit.



          feedback:




          1. In push() the null check is unnessecary, because node.next is already null by default.



            public void push(E data) 
            Node node = new Node(data);
            node.next = top;
            top = node;



          2. Rather then have a print() method, create a toString() method. toString() is a method of Object that is used across java to get a string representation of an object. This will make it easier to print your object to different streams.


          3. In Node, create a getData() (and optionally setData()) method to control access to the data class, or make data final.


          4. make top private to control use of your class to the supported methods.






          share|improve this answer





















          • Got it. Thank You.
            – Karan Khanna
            Jul 14 at 22:30

















          up vote
          2
          down vote













          Printing an empty stack should not cause an exception. It should print nothing (or possibly print something that represents an empty stack).



          There is no way to tell if the stack contains anything, other than peeking and catching an exception. Add a size() method, or at the very least isEmpty().






          share|improve this answer





















          • Got it. Thank You.
            – Karan Khanna
            Jul 14 at 22:30










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199507%2fimplementing-a-stack-with-java%23new-answer', 'question_page');

          );

          Post as a guest






























          2 Answers
          2






          active

          oldest

          votes








          2 Answers
          2






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          5
          down vote














          I wanted to know if the pop() implementation can cause a memory leak?




          It will not cause a memory leak, because java is a garbage collected language. When top is no longer accessible the garbale collecter will free top if it sees fit.



          feedback:




          1. In push() the null check is unnessecary, because node.next is already null by default.



            public void push(E data) 
            Node node = new Node(data);
            node.next = top;
            top = node;



          2. Rather then have a print() method, create a toString() method. toString() is a method of Object that is used across java to get a string representation of an object. This will make it easier to print your object to different streams.


          3. In Node, create a getData() (and optionally setData()) method to control access to the data class, or make data final.


          4. make top private to control use of your class to the supported methods.






          share|improve this answer





















          • Got it. Thank You.
            – Karan Khanna
            Jul 14 at 22:30














          up vote
          5
          down vote














          I wanted to know if the pop() implementation can cause a memory leak?




          It will not cause a memory leak, because java is a garbage collected language. When top is no longer accessible the garbale collecter will free top if it sees fit.



          feedback:




          1. In push() the null check is unnessecary, because node.next is already null by default.



            public void push(E data) 
            Node node = new Node(data);
            node.next = top;
            top = node;



          2. Rather then have a print() method, create a toString() method. toString() is a method of Object that is used across java to get a string representation of an object. This will make it easier to print your object to different streams.


          3. In Node, create a getData() (and optionally setData()) method to control access to the data class, or make data final.


          4. make top private to control use of your class to the supported methods.






          share|improve this answer





















          • Got it. Thank You.
            – Karan Khanna
            Jul 14 at 22:30












          up vote
          5
          down vote










          up vote
          5
          down vote










          I wanted to know if the pop() implementation can cause a memory leak?




          It will not cause a memory leak, because java is a garbage collected language. When top is no longer accessible the garbale collecter will free top if it sees fit.



          feedback:




          1. In push() the null check is unnessecary, because node.next is already null by default.



            public void push(E data) 
            Node node = new Node(data);
            node.next = top;
            top = node;



          2. Rather then have a print() method, create a toString() method. toString() is a method of Object that is used across java to get a string representation of an object. This will make it easier to print your object to different streams.


          3. In Node, create a getData() (and optionally setData()) method to control access to the data class, or make data final.


          4. make top private to control use of your class to the supported methods.






          share|improve this answer














          I wanted to know if the pop() implementation can cause a memory leak?




          It will not cause a memory leak, because java is a garbage collected language. When top is no longer accessible the garbale collecter will free top if it sees fit.



          feedback:




          1. In push() the null check is unnessecary, because node.next is already null by default.



            public void push(E data) 
            Node node = new Node(data);
            node.next = top;
            top = node;



          2. Rather then have a print() method, create a toString() method. toString() is a method of Object that is used across java to get a string representation of an object. This will make it easier to print your object to different streams.


          3. In Node, create a getData() (and optionally setData()) method to control access to the data class, or make data final.


          4. make top private to control use of your class to the supported methods.







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jul 14 at 20:27









          Peter

          59410




          59410











          • Got it. Thank You.
            – Karan Khanna
            Jul 14 at 22:30
















          • Got it. Thank You.
            – Karan Khanna
            Jul 14 at 22:30















          Got it. Thank You.
          – Karan Khanna
          Jul 14 at 22:30




          Got it. Thank You.
          – Karan Khanna
          Jul 14 at 22:30












          up vote
          2
          down vote













          Printing an empty stack should not cause an exception. It should print nothing (or possibly print something that represents an empty stack).



          There is no way to tell if the stack contains anything, other than peeking and catching an exception. Add a size() method, or at the very least isEmpty().






          share|improve this answer





















          • Got it. Thank You.
            – Karan Khanna
            Jul 14 at 22:30














          up vote
          2
          down vote













          Printing an empty stack should not cause an exception. It should print nothing (or possibly print something that represents an empty stack).



          There is no way to tell if the stack contains anything, other than peeking and catching an exception. Add a size() method, or at the very least isEmpty().






          share|improve this answer





















          • Got it. Thank You.
            – Karan Khanna
            Jul 14 at 22:30












          up vote
          2
          down vote










          up vote
          2
          down vote









          Printing an empty stack should not cause an exception. It should print nothing (or possibly print something that represents an empty stack).



          There is no way to tell if the stack contains anything, other than peeking and catching an exception. Add a size() method, or at the very least isEmpty().






          share|improve this answer













          Printing an empty stack should not cause an exception. It should print nothing (or possibly print something that represents an empty stack).



          There is no way to tell if the stack contains anything, other than peeking and catching an exception. Add a size() method, or at the very least isEmpty().







          share|improve this answer













          share|improve this answer



          share|improve this answer











          answered Jul 14 at 22:26









          AJNeufeld

          1,358312




          1,358312











          • Got it. Thank You.
            – Karan Khanna
            Jul 14 at 22:30
















          • Got it. Thank You.
            – Karan Khanna
            Jul 14 at 22:30















          Got it. Thank You.
          – Karan Khanna
          Jul 14 at 22:30




          Got it. Thank You.
          – Karan Khanna
          Jul 14 at 22:30












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199507%2fimplementing-a-stack-with-java%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Chat program with C++ and SFML

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          Will my employers contract hold up in court?