Overflow Stack : Stack « Collections Data Structure « C# / C Sharp






Overflow Stack

        
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace CrackSoft.Collections
{
    class OverflowStack<T> : IEnumerable<T>, ICollection
    {
        object syncRoot = new object();
        LinkedList<T> items;
        int maxItems;

        public OverflowStack(int capacity)
        {
            maxItems = capacity;
            items = new LinkedList<T>();
        }

        public OverflowStack(IEnumerable<T> collection)
        {
            items = new LinkedList<T>(collection);
            maxItems = items.Count;
        }

        public void Push(T item)
        {
            if (items.Count == maxItems)
                items.RemoveLast();
            items.AddFirst(item);
        }

        public T Pop()
        {
            T item = items.First.Value;
            items.RemoveFirst();
            return item;
        }

        public T Peek()
        {
            return items.First.Value;
        }

        public void Clear()
        {
            items.Clear();
        }

        public bool IsEmpty
        {
            get { return Count == 0; }
        }        

        #region IEnumerable Members

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

        #endregion

        #region IEnumerable<T> Members

        public IEnumerator<T> GetEnumerator()
        {
            return items.GetEnumerator();
        }

        #endregion

        #region ICollection Members

        public void CopyTo(Array array, int index)
        {
            if (array is T[])
                items.CopyTo((T[])array, index);
        }
        
        public int Count
        {
            get { return items.Count; }
        }

        public bool IsSynchronized
        {
            get { return false; }
        }

        public object SyncRoot
        {
            get { return syncRoot; }
        }

        #endregion        
    }
}

   
    
    
    
    
    
    
    
  








Related examples in the same category

1.new Stack(new int[] { 1, 2, 3, 4, 5, 6 })
2.new Stack())
3.Implements the stack data type using an arrayImplements the stack data type using an array
4.Stack demo Stack demo
5.Stack to arrayStack to array
6.illustrates the use of a Stackillustrates the use of a Stack
7.A stack class for charactersA stack class for characters
8.Demonstrate the Stack classDemonstrate the Stack class
9.Stack(T) Class represents a variable size last-in-first-out (LIFO) collection of instances of the same arbitrary type.
10.Thread Safe Stack
11.A Stacked array is an integer array that contains a sequence of ascending integers.
12.FixedSizeStack provides an easy Stack implementation width a fixed size to prevent Stack Overflows in another sense.
13.Stack abstraction that also supports the IList interface