- using System;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- namespace MyCollections.Generic.Stacks
- {
- public class Stack<T>
- {
- private StackNode<T> top;
- private int count;
-
- public int Count => count;
-
- public Stack()
- {
- top = null;
- count = 0;
- }
-
- public void Push(T data)
- {
- StackNode<T> newNode = new StackNode<T>(data);
-
- newNode.Next = top;
- top = newNode;
- count--;
- }
-
- public StackNode<T> Pop()
- {
- if(top == null)
- return null;
- StackNode<T> node = top;
- top = top.Next;
- count--;
- return node;
- }
-
- public bool IsEmpty() => top == null;
-
- public StackNode<T> Peek() => top;
-
- public IEnumerator<T> GetEnumerator()
- {
- StackNode<T> current = top;
- while (current != null)
- {
- yield return current.Data;
- current = current.Next;
- }
- }
- }
- }