Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 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 TodoApplication
  8. {
  9. public class Item<T>
  10. {
  11. public T Value { get; set; }
  12. public Item<T> Previous { get; set; }
  13. public Item<T> Next { get; set; }
  14. }
  15.  
  16. public class LimitedSizeStack<T>
  17. {
  18. public int Limit;
  19. public int CountVar;
  20. public Item<T> Start = new Item<T> { Previous = null, Next = null };
  21. public Item<T> Final = new Item<T> { Previous = null, Next = null };
  22.  
  23. public LimitedSizeStack(int limit)
  24. {
  25. Limit = limit;
  26. }
  27.  
  28. public void Push(T item)
  29. {
  30. if (Start != null)
  31. {
  32. if (CountVar < Limit)
  33. {
  34. CountVar++;
  35. Final.Next = new Item<T> { Value = item, Previous = Final };
  36. Final = Final.Next;
  37. }
  38. else if (CountVar == Limit)
  39. {
  40. Final.Next = new Item<T> { Value = item, Previous = Final };
  41. Final = Final.Next;
  42.  
  43. Start = Start.Next;
  44. Start.Previous = null;
  45. }
  46. }
  47. else
  48. {
  49. CountVar++;
  50. Start = Final = new Item<T> { Value = item };
  51. }
  52. }
  53.  
  54. public T Pop()
  55. {
  56. if (Start != Final)
  57. {
  58. var value = Final.Value;
  59. Final = Final.Previous;
  60. Final.Next = null;
  61. CountVar--;
  62. return value;
  63. }
  64. else
  65. {
  66. var value = Start.Value;
  67. Start = Final = null;
  68. CountVar--;
  69. return value;
  70. }
  71. }
  72.  
  73. public int Count
  74. {
  75. get
  76. {
  77. return CountVar;
  78. }
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement