
背景
前两篇文章介绍了 Queue 的实现,很多类库都引入了 Deque,Deque 可以两头添加和删除,然后在 Deque 之上构建 Queue 和 Stack。
Deque
代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DataStuctureStudy.Deques
{
public class Deque<T>
{
private T[] _items = new T[];
private int _size = ;
private int _head = -;
private int _tail = -; private void AllocateNewArray()
{
int newLength = (_size == ) ? : _size * ; T[] newArray = new T[newLength]; if (_size > )
{
// copy contents...
// if the array has no wrapping, just copy the valid range
// else copy from head to end of the array and then from 0 to the tail // if tail is less than head we've wrapped
var newIndex = ;
if (_tail < _head)
{
// copy the _items[head].._items[end] -> newArray[0]..newArray[N]
for (int index = _head; index < _items.Length; index++)
{
newArray[newIndex] = _items[index];
newIndex++;
} // copy _items[0].._items[tail] -> newArray[N+1]..
for (int index = ; index <= _tail; index++)
{
newArray[newIndex] = _items[index];
newIndex++;
}
}
else
{
// copy the _items[head].._items[tail] -> newArray[0]..newArray[N]
for (int index = _head; index <= _tail; index++)
{
newArray[newIndex] = _items[index];
newIndex++;
}
}
_head = ;
_tail = _size - ; // compensate for the extra bump
}
else
{
// nothing in the array
_head = -;
_tail = -;
} _items = newArray;
} public void EnqueueFirst(T item)
{
// if the array needs to grow
if (_items.Length == _size)
{
AllocateNewArray();
} // since we know the array isn't full and _head is greater than 0
// we know the slot in front of head is open
if (_head > )
{
_head--;
}
else
{
// otherwise we need to wrap around to the end of the array
_head = _items.Length - ;
if (_tail == -)
{
_tail = _head;
}
}
_items[_head] = item;
_size++;
} public void EnqueueLast(T item)
{
// if the array needs to grow
if (_items.Length == _size)
{
AllocateNewArray();
} // now we have a properly sized array and can focus on wrapping issues.
// if _tail is at the end of the array we need to wrap around
if (_tail == _items.Length - )
{
_tail = ;
}
else
{
_tail++; if (_head == -)
{
_head = _tail;
}
}
_items[_tail] = item;
_size++;
} public T DequeueFirst()
{
if (_size == )
{
throw new InvalidOperationException("The deque is empty");
} T value = _items[_head]; if (_head == _items.Length - )
{
// if the head is at the last index in the array - wrap around.
_head = ;
}
else
{
// move to the next slot
_head++;
}
_size--;
return value;
} public T DequeueLast()
{
if (_size == )
{
throw new InvalidOperationException("The deque is empty");
} T value = _items[_tail]; if (_tail == )
{
// if the tai is at the first index in the array - wrap around.
_tail = _items.Length - ;
}
else
{
// move to the previous slot
_tail--;
}
_size--;
return value;
} public T PeekFirst()
{
if (_size == )
{
throw new InvalidOperationException("The deque is empty");
}
return _items[_head];
} public T PeekLast()
{
if (_size == )
{
throw new InvalidOperationException("The deque is empty");
}
return _items[_tail];
} public int Count
{
get
{
return _size;
}
}
}
}