C# “我确信它提供了一个方便的信息,即后进先出”队列被称为堆栈。@斯宾德:还有其他人也认为这是有用的信息。他们留下了评论。嗯。。。看起来不错。。。但当堆栈“满”时,它停止推它的堆栈。。。bit在“已满”时应删除“底部”项。堆栈是否有迭代器?@KeesC.Bak
C# “我确信它提供了一个方便的信息,即后进先出”队列被称为堆栈。@斯宾德:还有其他人也认为这是有用的信息。他们留下了评论。嗯。。。看起来不错。。。但当堆栈“满”时,它停止推它的堆栈。。。bit在“已满”时应删除“底部”项。堆栈是否有迭代器?@KeesC.Bak,c#,.net,generics,data-structures,C#,.net,Generics,Data Structures,“我确信它提供了一个方便的信息,即后进先出”队列被称为堆栈。@斯宾德:还有其他人也认为这是有用的信息。他们留下了评论。嗯。。。看起来不错。。。但当堆栈“满”时,它停止推它的堆栈。。。bit在“已满”时应删除“底部”项。堆栈是否有迭代器?@KeesC.Bakker,是的,它有,并按您指定的顺序(LCFS)枚举。。魔法:)。我唯一需要的是增加容量:P@KeesC.Bakker产能增加?比如在创建堆栈后增加容量,同时保留项目?是的,这可能会稍微困难一些。@GetEnumerator()中的Euphor
“我确信它提供了一个方便的信息,即后进先出”队列被称为堆栈。@斯宾德:还有其他人也认为这是有用的信息。他们留下了评论。嗯。。。看起来不错。。。但当堆栈“满”时,它停止推它的堆栈。。。bit在“已满”时应删除“底部”项。堆栈是否有迭代器?@KeesC.Bakker,是的,它有,并按您指定的顺序(LCFS)枚举。。魔法:)。我唯一需要的是增加容量:P@KeesC.Bakker产能增加?比如在创建堆栈后增加容量,同时保留项目?是的,这可能会稍微困难一些。@GetEnumerator()中的Euphoric,如果/when begin==0,i==1,在尝试将“array”索引到-1时是否会导致错误?我们是否有可用的结构,或者我是否必须自己创建结构?
class OverflowingStack<T>
{
private T[] items;
private int currentIndex;
private int count;
public OverflowingStack(int size)
{
this.items = new T[size];
this.currentIndex = 0;
this.count = 0;
}
public void Push(T item)
{
items[currentIndex] = item;
currentIndex++;
currentIndex %= items.Length;
count++;
count = count > items.Length ? items.Length : count;
}
public T Pop()
{
if (count == 0) throw new Exception("stack is empty");
currentIndex--;
while (currentIndex < 0) {currentIndex += items.Length;}
count--;
return items[currentIndex];
}
}
public class DroppingStack<T> : IEnumerable<T>
{
T[] array;
int cap;
int begin;
int end;
public DroppingStack (int capacity)
{
cap = capacity+1;
array = new T[cap];
begin = 0;
end = 0;
}
public T pop()
{
if (begin == end) throw new Exception("No item");
begin--;
if (begin < 0)
begin += cap;
return array[begin];
}
public void push(T value)
{
array[begin] = value;
begin = (begin+1)%cap;
if (begin == end)
end = (end + 1) % cap;
}
public IEnumerator<T> GetEnumerator()
{
int i = begin-1;
while (i != end-1)
{
yield return array[i];
i--;
if (i < 0)
i += cap;
}
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
class CyclicStack<T>
{
private T[] stack;
private int capacity;
private int curIndex = 0;
public int Count { get; private set; }
public CyclicStack(int capacity)
{
this.capacity = capacity;
stack = new T[capacity];
this.Count = 0;
}
public T this[int index]
{
get
{
if (index >= capacity)
throw new Exception("Index is out of bounds");
return this.stack[(curIndex + index) % capacity];
}
}
public void Push(T item)
{
curIndex = (curIndex + capacity - 1) % capacity;
stack[curIndex] = item;
this.Count++;
}
public T Pop()
{
if (this.Count == 0)
throw new Exception("Collection is empty");
int oldIndex = curIndex;
curIndex = (curIndex + capacity + 1) % capacity;
this.Count--;
return stack[oldIndex];
}
}
public class LifoBuffer<T> : LinkedList<T>
{
private int capacity;
public LifoBuffer(int capacity)
{
this.capacity = capacity;
}
public void Add(T item)
{
if (Count == capacity) RemoveLast();
AddFirst(item);
}
}
void Main()
{
var lifoBuffer = new LifoBuffer<int>(3);
lifoBuffer.Add(1);
lifoBuffer.Add(2);
lifoBuffer.Add(3);
lifoBuffer.Add(4);
lifoBuffer.Add(5);
foreach (var item in lifoBuffer) Console.WriteLine(item); // outputs: 5, 4, 3
}