Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Stack
- {
- public class Stack
- {
- private int[] stack;
- private int index;
- public Stack (int n)
- {
- stack = new int[n];
- index = 0;
- }
- public void Clear ()
- {
- for (int i = 0; i < stack.Length; i++)
- stack[i] = 0;
- index = 0;
- }
- public int Pop ()
- {
- try {
- index--;
- return stack [index];
- }
- catch (IndexOutOfRangeException) {
- Clear();
- throw new index0 ("Ошибка! Попытка чтения из пустого стека");
- return 0;
- }
- }
- public void Print ()
- {
- if (index == 0)
- Console.WriteLine ("Стек пуст");
- else {
- Console.Write ("Стек: ");
- for (int i = 0; i < index-1; i++)
- Console.Write ("{0}, ", stack[i]);
- Console.Write (stack[index-1]+";");
- Console.WriteLine();
- }
- }
- public void Push (int elem)
- {
- try {
- stack[index] = elem;
- index++;
- }
- catch (IndexOutOfRangeException) {
- throw new overkill("Ошибка! Индекс вышел за пределы. Часть данных была потеряна. Увеличьте стек.");
- }
- }
- public int Top()
- {
- return index;
- }
- }
- class index0 : System.Exception
- {
- string message;
- public index0 (string _message)
- {
- message = _message;
- Console.WriteLine(message);
- }
- }
- class overkill : System.Exception
- {
- string message;
- public overkill(string _message)
- {
- message = _message;
- Console.WriteLine(message);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement