Advertisement
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 Stosik
- {
- class Stosik
- {
- private int _rozmiar = 0;
- private List<int> _outLittleStack;
- //konstruktor prywatny zeby korzystać z Init
- private Stosik(int rozmiar)
- {
- _rozmiar = rozmiar;
- _outLittleStack = new List<int>(rozmiar);
- }
- //metoda statyczna tworzy nowy obiekt i zwraca go
- public static Stosik Init(int rozmiar)
- {
- return new Stosik(rozmiar);
- }
- public Stosik Clone()
- {
- //tworzymy nowy obiekt stosu
- Stosik stosik = Stosik.Init(_rozmiar);
- //przepisujemy wartości
- foreach (var number in _outLittleStack)
- {
- stosik._outLittleStack.Add(number);
- }
- //zwracamy sklonowany stosik
- return stosik;
- }
- public void Destroy()
- {
- _outLittleStack.Clear();
- _rozmiar = 0;
- }
- public void Push(int liczba)
- {
- if (_rozmiar == 0)
- throw new Exception("Stos nie zainicjalizowany");
- if (_outLittleStack.Count() < _rozmiar)
- _outLittleStack.Add(liczba);
- else
- {
- Console.WriteLine("O TY W MORDE STOSIK PELNY NIE DODASZ:///");
- }
- }
- public int Pop()
- {
- if (_rozmiar == 0)
- throw new Exception("Stos nie zainicjalizowany");
- if (_outLittleStack.Any())
- {
- var number = _outLittleStack.Last();
- _outLittleStack.Remove(number);
- return number;
- }
- else
- {
- throw new Exception("MORDO JAK STOS PUSTY!!!!");
- }
- }
- public int Top()
- {
- if (_rozmiar == 0)
- throw new Exception("Stos nie zainicjalizowany");
- if (_outLittleStack.Any())
- return _outLittleStack.Last();
- else
- {
- throw new Exception("MORDO JAK STOS PUSTY!!!!");
- }
- }
- public bool Empty()
- {
- if (_rozmiar == 0)
- throw new Exception("Stos nie zainicjalizowany");
- return _outLittleStack.Count == 0;
- }
- public bool Full()
- {
- if (_rozmiar == 0)
- throw new Exception("Stos nie zainicjalizowany");
- return _outLittleStack.Count == _rozmiar;
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- //stworzenie 1 stosu
- Stosik stosik1 = Stosik.Init(10);
- for (int i = 0; i < 10; i++)
- stosik1.Push(i);
- //zrobienie kopii
- Stosik stosik2 = stosik1.Clone();
- //wypisanie stosu 2 całego
- Console.WriteLine("WYpisanie sklonowanego stosu");
- while(!stosik2.Empty())
- Console.WriteLine(stosik2.Pop());
- //sprawdzenie, czy stos 1 jest cały
- Console.WriteLine("Sprawdzenie elementu ze stosu pierwotnego");
- Console.WriteLine(stosik1.Top());
- //jak widac, wypisuje 9 wiec stosik1 jest git
- Console.ReadKey();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement