{"id":133,"date":"2010-05-06T15:06:44","date_gmt":"2010-05-06T15:06:44","guid":{"rendered":"http:\/\/jcrawfor74.wordpress.com\/?p=133"},"modified":"2014-08-06T15:43:33","modified_gmt":"2014-08-06T05:43:33","slug":"c-stack-with-maximum-limit","status":"publish","type":"post","link":"https:\/\/ntsblog.homedev.com.au\/index.php\/2010\/05\/06\/c-stack-with-maximum-limit\/","title":{"rendered":"C# Stack<T> with maximum limit"},"content":{"rendered":"<div id=\"ntsbl-180205168\" class=\"ntsbl-before-content ntsbl-entity-placement\"><script async src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js?client=ca-pub-6288941070289539\" crossorigin=\"anonymous\"><\/script><ins class=\"adsbygoogle\" style=\"display:inline-block;width:728px;height:90px;\" \ndata-ad-client=\"ca-pub-6288941070289539\" \ndata-ad-slot=\"9356781486\"><\/ins> \n<script> \n(adsbygoogle = window.adsbygoogle || []).push({}); \n<\/script>\n<\/div><p>I am writing a web based app at the moment and I am wanting to maintain a history of the persons movements around the site to enable some business logic for handling back button clicks. <\/p>\n<p>I wanted to use a Stack but I did not want to let it get too out of control in memory if people take weird navigation choices around the site. So I wanted a stack that would push on the top but have a maximum limit so that when the maximum was reached the oldest items on the stack just fell out the bottom.<\/p>\n<p>So I wrote the MaxStack. Hope you like it. It uses a generic linked list to implement the stack like interface.<\/p>\n<p>Note its also serializable as I am storing it in session.<br \/>\n(Sorry about the indenting wordpress has mashed it)<\/p>\n<p><strong>MaxStack<\/strong><br \/>\n[csharp]<br \/>\nusing System;<br \/>\nusing System.Collections.Generic;<\/p>\n<p>namespace MyArchitecture<br \/>\n{<br \/>\n    \/\/\/ <\/p>\n<summary>\n    \/\/\/ Generic stack implementation with a maximum limit<br \/>\n    \/\/\/ When something is pushed on the last item is removed from the list<br \/>\n    \/\/\/ <\/summary>\n<p>    [Serializable]<br \/>\n    public class MaxStack<T><br \/>\n    {<br \/>\n        #region Fields<\/p>\n<p>        private int _limit;<br \/>\n        private LinkedList<T> _list;<\/p>\n<p>        #endregion<\/p>\n<p>        #region Constructors<\/p>\n<p>        public MaxStack(int maxSize)<br \/>\n        {<br \/>\n            _limit = maxSize;<br \/>\n            _list = new LinkedList<T>();<\/p>\n<p>        }<\/p>\n<p>        #endregion<\/p>\n<p>        #region Public Stack Implementation<\/p>\n<p>        public void Push(T value)<br \/>\n        {<br \/>\n            if (_list.Count == _limit)<br \/>\n            {<br \/>\n                _list.RemoveLast();<br \/>\n            }<br \/>\n            _list.AddFirst(value);<br \/>\n        }<\/p>\n<p>        public T Pop()<br \/>\n        {<br \/>\n            if (_list.Count > 0)<br \/>\n            {<br \/>\n                T value = _list.First.Value;<br \/>\n                _list.RemoveFirst();<br \/>\n                return value;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                throw new InvalidOperationException(&#8220;The Stack is empty&#8221;);<br \/>\n            }<\/p>\n<p>        }<\/p>\n<p>        public T Peek()<br \/>\n        {<br \/>\n            if (_list.Count > 0)<br \/>\n            {<br \/>\n                T value = _list.First.Value;<br \/>\n                return value;<br \/>\n            }<br \/>\n            else<br \/>\n            {<br \/>\n                throw new InvalidOperationException(&#8220;The Stack is empty&#8221;);<br \/>\n            }<\/p>\n<p>        }<\/p>\n<p>        public void Clear()<br \/>\n        {<br \/>\n            _list.Clear();<\/p>\n<p>        }<\/p>\n<p>        public int Count<br \/>\n        {<br \/>\n            get { return _list.Count; }<br \/>\n        }<\/p>\n<p>        \/\/\/ <\/p>\n<summary>\n        \/\/\/ Checks if the top object on the stack matches the value passed in<br \/>\n        \/\/\/ <\/summary>\n<p>        \/\/\/ <param name=\"value\"><\/param>\n        \/\/\/ <returns><\/returns><br \/>\n        public bool IsTop(T value)<br \/>\n        {<br \/>\n            bool result = false;<br \/>\n            if (this.Count > 0)<br \/>\n            {<br \/>\n                result = Peek().Equals(value);<br \/>\n            }<br \/>\n            return result;<br \/>\n        }<\/p>\n<p>        public bool Contains(T value)<br \/>\n        {<br \/>\n            bool result = false;<br \/>\n            if (this.Count > 0)<br \/>\n            {<br \/>\n                result = _list.Contains(value);<br \/>\n            }<br \/>\n            return result;<br \/>\n        }<\/p>\n<p>\tpublic IEnumerator GetEnumerator()<br \/>\n\t{<br \/>\n            return _list.GetEnumerator();<br \/>\n\t}<\/p>\n<p>        #endregion<\/p>\n<p>    }<br \/>\n}<br \/>\n[\/csharp]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I am writing a web based app at the moment and I am wanting to maintain a history of the persons movements around the site [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[5],"tags":[],"class_list":["post-133","post","type-post","status-publish","format-standard","hentry","category-c"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/comments?post=133"}],"version-history":[{"count":0,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/posts\/133\/revisions"}],"wp:attachment":[{"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/media?parent=133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/categories?post=133"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ntsblog.homedev.com.au\/index.php\/wp-json\/wp\/v2\/tags?post=133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}