namespace Zasobnik { public class Stack { private int[] data; private int head = -1; public static int defaultValue = int.MaxValue; private static int resizeFactor = 2; public Stack(uint initCapacity) { data = new int[initCapacity]; } public int Size() { return head + 1; } public int Pop() { if (!IsEmpty()) { int value = data[head]; // zjistíme prvek head--; // posuneme hlavu return value; } return defaultValue; } public int Peek() { if (!IsEmpty()) { return data[head]; // pouze zjistíme prvek } return defaultValue; } public bool Push(int number) { int nextPos = ++head; if (nextPos >= data.Length) { ResizeDataStructure(); } data[nextPos] = number; return true; } private void ResizeDataStructure() { int[] nextData = new int[data.Length * resizeFactor]; data.CopyTo(nextData, 0); data = nextData; } private bool IsEmpty() { return head <= -1; } public void Print() { for (int i = Size() - 1; i >= 0; i--) { Console.Write(data[i]); Console.Write(", "); } Console.WriteLine(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Zasobnik { public class Stack { private class ListNode { public int value; public ListNode next = null; public ListNode(int val, ListNode next) { this.value = val; this.next = next; } } private ListNode headNode = null; private int size = 0; private static int defaultValue = int.MaxValue; public Stack(uint initCapacity) { } public int Size() { return size; } public int Pop() { if (!IsEmpty()) { int value = headNode.value; // zjistíme prvek headNode = headNode.next; // posuneme hlavu size--; // úprava velikosti return value; } return defaultValue; } public int Peek() { if (!IsEmpty()) { return headNode.value; // pouze zjistíme prvek } return defaultValue; } public bool Push(int number) { ListNode newHead = new ListNode(number, headNode); headNode = newHead; size++; return true; } private bool IsEmpty() { return headNode == null; } public void Print() { ListNode node = headNode; while (node != null) { Console.Write(node.value); Console.Write(","); node = node.next; } Console.WriteLine(); } } }