Home > c# > C# Stack<T> with maximum limit

C# Stack<T> with maximum limit

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.

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.

So I wrote the MaxStack. Hope you like it. It uses a generic linked list to implement the stack like interface.

Note its also serializable as I am storing it in session.
(Sorry about the indenting wordpress has mashed it)

MaxStack

using System;
using System.Collections.Generic;

namespace MyArchitecture
{
/// <summary>
/// Generic stack implementation with a maximum limit
/// When something is pushed on the last item is removed from the list
/// </summary>
[Serializable]
public class MaxStack<T>
{
#region Fields

private int _limit;
private LinkedList<T> _list;

#endregion

#region Constructors

public MaxStack(int maxSize)
{
_limit = maxSize;
_list = new LinkedList<T>();

}

#endregion

#region Public Stack Implementation

public void Push(T value)
{
if (_list.Count == _limit)
{
_list.RemoveLast();
}
_list.AddFirst(value);
}

public T Pop()
{
if (_list.Count > 0)
{
T value = _list.Last.Value;
_list.RemoveFirst();
return value;
}
else
{
throw new InvalidOperationException("The Stack is empty");
}

}

public T Peek()
{
if (_list.Count > 0)
{
T value = _list.First.Value;
return value;
}
else
{
throw new InvalidOperationException("The Stack is empty");
}

}

public void Clear()
{
_list.Clear();

}

public int Count
{
get { return _list.Count; }
}

/// <summary>
/// Checks if the top object on the stack matches the value passed in
/// </summary>
/// <param name="value"></param>
/// <returns></returns>
public bool IsTop(T value)
{
bool result = false;
if (this.Count > 0)
{
result = Peek().Equals(value);
}
return result;
}

public bool Contains(T value)
{
bool result = false;
if (this.Count > 0)
{
result = _list.Contains(value);
}
return result;
}

#endregion

}
}

Advertisements
Categories: c#
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: