Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions MyStack.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Stack
{

public class Node<T>
{
public Node(T data)
{
Data = data;
}
public T Data { get; set; }
public Node<T> Next { get; set; }
}
public class NodeStack<T> : IEnumerable<T>
{
Node<T> head;
int count;
public bool IsEmpty
{
get { return count == 0; }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably get => count == 0 should do

}
public int Count
{
get { return count; }
}

public void Push(T item)
{
Node<T> node = new Node<T>(item);
node.Next = head;
head = node;
count++;
}
public T Pop()
{
if (IsEmpty)
throw new InvalidOperationException("Стек пуст");
Node<T> temp = head;
head = head.Next;
count--;
return temp.Data;
}
public T Peek()
{
if (IsEmpty)
throw new InvalidOperationException("Стек пуст");
return head.Data;
}

IEnumerator IEnumerable.GetEnumerator()
{
return ((IEnumerable)this).GetEnumerator();
}

IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
Node<T> current = head;
while (current != null)
{
yield return current.Data;
current = current.Next;
}
}

}
}