Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace Stack
- {
- class Program
- {
- class Stack
- {
- int[] tab;
- int apex = 0;
- public void Init(int size)
- {
- tab = new int[size];
- apex = -1;
- }
- public void Destroy()
- {
- tab = null;
- }
- public void Push(int x)
- {
- tab[++apex] = x;
- }
- public void Pop()
- {
- if (apex >= 0)
- --apex;
- else
- Console.WriteLine("Brak elementów na stosie!");
- }
- public int Top()
- {
- return tab[apex];
- }
- public bool isEmpty()
- {
- return apex == -1;
- }
- public bool isFull()
- {
- return apex == tab.Length - 1;
- }
- }
- static int Main()
- {
- int size;
- Stack stack = new Stack();
- Console.Write("Wczytaj rozmiar stosu: ");
- size = int.Parse(Console.ReadLine());
- if (size > 100)
- {
- Console.WriteLine("Za duży rozmiar stosu!");
- return 0;
- }
- stack.Init(size);
- Console.WriteLine("Wczytaj {0} liczb które mają być wrzucone na stos", size);
- for (int i = 0; i < size; i++)
- stack.Push(int.Parse(Console.ReadLine()));
- Stack temporary = new Stack();
- temporary.Init(size);
- Console.WriteLine("");
- Console.Write("Liczby które znajdują się na stosie\n");
- while (!stack.isEmpty())
- {
- Console.Write("{0}\n", stack.Top());
- temporary.Push(stack.Top());
- stack.Pop();
- }
- while (!stack.isFull())
- {
- stack.Push(temporary.Top());
- temporary.Pop();
- }
- Console.WriteLine("");
- stack.Destroy();
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement