Evyatar12

stack

Jan 8th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Stacks
  8. {
  9. class Stack<T>
  10. {
  11. private Node<T> head;
  12.  
  13. public Stack()
  14. {
  15. head = null;
  16. }
  17.  
  18. public bool isEmpty()
  19. {
  20. return head == null;
  21. }
  22.  
  23. public void push(T x)
  24. {
  25. head = new Node<T>(x, head);
  26. }
  27.  
  28. public T pop()
  29. {
  30. if (isEmpty())
  31. return default(T);
  32.  
  33. T val = head.getValue();
  34.  
  35. head = head.getNext();
  36.  
  37. return val;
  38. }
  39.  
  40. public T top()
  41. {
  42. if (isEmpty())
  43. return default(T);
  44.  
  45. return head.getValue();
  46. }
  47.  
  48. public override string ToString()
  49. {
  50. string baseStr = "[";
  51.  
  52. while (!isEmpty())
  53. {
  54. if (baseStr.Length > 1)
  55. baseStr += ", ";
  56.  
  57. baseStr += pop().ToString();
  58. }
  59.  
  60. baseStr += "]";
  61.  
  62. return baseStr;
  63. }
  64. }
  65. }
Add Comment
Please, Sign In to add comment