Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2020
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.57 KB | None | 0 0
  1. public class StackItem<T>
  2. {
  3. public T Value;
  4. public StackItem<T> Previous;
  5. }
  6.  
  7. public class Stack<T>
  8. {
  9. public StackItem<T> Tail;
  10. private int Count;
  11.  
  12. public void Push(T item)
  13. {
  14. var stackItem = new StackItem<T> { Value = item };
  15. if (Count == 0)
  16. {
  17. stackItem.Previous = null;
  18. Tail = stackItem;
  19. Count++;
  20. return;
  21. }
  22. stackItem.Previous = Tail;
  23. Tail = stackItem;
  24. Count++;
  25. }
  26.  
  27. public T Pop()
  28. {
  29. if (Count == 0) throw new InvalidOperationException();
  30. var item = Tail;
  31. Tail = Tail.Previous;
  32. Count--;
  33. return item.Value;
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement