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 TodoApplication
- {
- public class LimitedSizeStack<T>
- {
- private int size;
- private LinkedList<T> linkList = new LinkedList<T>();
- public LimitedSizeStack(int size)
- {
- if (size <= 0)
- throw new ArgumentException();
- this.size = size;
- }
- public void Push(T item)
- {
- linkList.AddLast(item);
- if(linkList.Count > size)
- linkList.RemoveFirst();
- }
- public T Pop()
- {
- if (linkList.Count == 0)
- throw new InvalidOperationException();
- var result = linkList.Last.Value;
- linkList.RemoveLast();
- return result;
- }
- public int Count
- {
- get
- {
- return linkList.Count;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement