You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.5 KiB

6 years ago
  1. // The linked list based implementation for the NumberStack ADT
  2. // Your name here
  3. public class LinkedNumberStack implements NumberStack
  4. {
  5. // instance variable
  6. private LNode m_top;
  7. // check whether the stack is empty
  8. public boolean isEmpty()
  9. {
  10. if (m_top == null)
  11. return true;
  12. else
  13. return false;
  14. }
  15. // check whether the stack is full
  16. public boolean isFull()
  17. {
  18. return false;
  19. }
  20. // return the element at the top of the stack
  21. public int top()
  22. {
  23. if (isEmpty())
  24. throw new RuntimeException("top attempted on an empty stack");
  25. else
  26. return m_top.getInfo();
  27. }
  28. // push a value onto the stack
  29. public void push(int v)
  30. {
  31. // TODO: implement this method
  32. }
  33. // remove and return the value at the top of the stack
  34. public int pop()
  35. {
  36. // TODO: implement this method
  37. return -1; // replace this statement with your own return
  38. }
  39. // return the number of elements on the stack
  40. public int size()
  41. {
  42. // TODO: implement this method
  43. return -1; // replace this statement with your own return
  44. }
  45. // return a string representation of the stack
  46. @Override
  47. public String toString()
  48. {
  49. String stackContent = "";
  50. LNode current = m_top;
  51. while (current != null)
  52. {
  53. stackContent += current.getInfo() + " ";
  54. current = current.getLink();
  55. }
  56. return stackContent;
  57. }
  58. }