Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Vezba_03_2017
- {
- class StackA
- {
- private int[] _items;
- private int _count;
- public StackA()
- {
- _items = new int[5];
- _count = 0;
- }
- public bool IsEmpty
- {
- get { return _count == 0; }
- }
- public void Push(int x)
- {
- if (_count == _items.Length)
- {
- int[] newItems = new int[_items.Length * 2];
- for (int i = 0; i < _items.Length; i++)
- newItems[i] = _items[i];
- _items = newItems;
- }
- _items[_count] = x;
- _count++;
- }
- public int Pop()
- {
- if (_count == 0)
- throw new InvalidOperationException("The stack is empty.");
- if (_count <= _items.Length / 4)
- {
- int[] newItems = new int[_items.Length / 2];
- for (int i = 0; i < newItems.Length; i++)
- newItems[i] = _items[i];
- _items = newItems;
- }
- _count--;
- return _items[_count];
- }
- public override string ToString()
- {
- StringBuilder sb = new StringBuilder();
- for (int i = _count - 1; i >= 0; i--)
- {
- sb.AppendFormat("{0} ", _items[i]);
- }
- return sb.ToString();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment