FAO: Anyone who was thoroughly puzzled by my geekage earlier........

Well here's the solution........

using System;
using System.Collections;

namespace System.Collections
{
///
/// Stack class which when it reaches a given size drops the oldest item in the stack to allow the newest item to be pushed on.
///
public class MaxSizedStack: Stack
{
private int maxSize;

public MaxSizedStack(int maxSize)
{
this.maxSize = maxSize;
}

public int MaxSize {
get
{
return maxSize;
}
set
{
maxSize = value;
}
}

public override void Push(object obj)
{
if(base.Count==maxSize)
{
System.Collections.Stack invert = new System.Collections.Stack();
do
{
invert.Push(base.Pop());
} while(base.Count>0);
//Discard oldest item
invert.Pop();

do
{
base.Push(invert.Pop());
} while(invert.Count>0);
}
base.Push(obj);
}
}
}

So stick that in your pipe and smoke it.

Posted By: Johnny Comecardiff, Jan 7, 14:08:28

Follow Ups

Reply to Message

Log in


Written & Designed By Ben Graves 1999-2025