Guest User

Untitled

a guest
Sep 16th, 2019
92
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class StackInt
  2. {
  3. private int[] elements; // Array für Stackspeicher
  4. private readonly int size; // Größe des Stacks
  5. private int pointer; // Zeiger auf das nächste freie Element
  6.  
  7. public StackInt(int size)
  8. {
  9. this.size = size;
  10. elements = new int[size];
  11. }
  12.  
  13. public void Push(int element)
  14. {
  15. if (pointer >= size)
  16. {
  17. throw new OverflowException();
  18. }
  19. elements[pointer] = element;
  20. pointer++;
  21. }
  22.  
  23. public int Pop()
  24. {
  25. pointer--;
  26. if (pointer >= 0)
  27. {
  28. return elements[pointer];
  29. }
  30. else
  31. {
  32. pointer = 0;
  33. throw new InvalidOperationException("Der Stack ist leer");
  34. }
  35. }
  36.  
  37. public int Peek()
  38. {
  39. pointer--;
  40. if (pointer >= 0)
  41. {
  42. int obj = elements[pointer];
  43. pointer++;
  44. return obj;
  45. }
  46. else
  47. {
  48. pointer++;
  49. throw new InvalidOperationException("Der Stack ist leer");
  50. }
  51. }
  52. }
  53.  
  54. public class StackDouble
  55. {
  56. private double[] elements; // Array für Stackspeicher
  57. private readonly int size; // Größe des Stacks
  58. private int pointer; // Zeiger auf das nächste freie Element
  59.  
  60. public StackDouble(int size)
  61. {
  62. this.size = size;
  63. elements = new double[size];
  64. }
  65.  
  66. public void Push(double element)
  67. {
  68. if (pointer >= size)
  69. {
  70. throw new OverflowException();
  71. }
  72. elements[pointer] = element;
  73. pointer++;
  74. }
  75.  
  76. public double Pop()
  77. {
  78. pointer--;
  79. if (pointer >= 0)
  80. {
  81. return elements[pointer];
  82. }
  83. else
  84. {
  85. pointer = 0;
  86. throw new InvalidOperationException("Der Stack ist leer");
  87. }
  88. }
  89.  
  90. public double Peek()
  91. {
  92. pointer--;
  93. if (pointer >= 0)
  94. {
  95. double obj = elements[pointer];
  96. pointer++;
  97. return obj;
  98. }
  99. else
  100. {
  101. pointer++;
  102. throw new InvalidOperationException("Der Stack ist leer");
  103. }
  104. }
  105. }
  106.  
  107. public class StackString
  108. {
  109. private string[] elements; // Array für Stackspeicher
  110. private readonly int size; // Größe des Stacks
  111. private int pointer; // Zeiger auf das nächste freie Element
  112.  
  113. public StackString(int size)
  114. {
  115. this.size = size;
  116. elements = new string[size];
  117. }
  118.  
  119. public void Push(string element)
  120. {
  121. if (pointer >= size)
  122. {
  123. throw new OverflowException();
  124. }
  125. elements[pointer] = element;
  126. pointer++;
  127. }
  128.  
  129. public string Pop()
  130. {
  131. pointer--;
  132. if (pointer >= 0)
  133. {
  134. return elements[pointer];
  135. }
  136. else
  137. {
  138. pointer = 0;
  139. throw new InvalidOperationException("Der Stack ist leer");
  140. }
  141. }
  142.  
  143. public string Peek()
  144. {
  145. pointer--;
  146. if (pointer >= 0)
  147. {
  148. string obj = elements[pointer];
  149. pointer++;
  150. return obj;
  151. }
  152. else
  153. {
  154. pointer++;
  155. throw new InvalidOperationException("Der Stack ist leer");
  156. }
  157. }
  158. }
RAW Paste Data