Stacks and Queues
1.Stack
public class CStack
{
private int p_index;
private ArrayList list;
public CStack()
{
list = new ArrayList();
p_index = -1;
}
public void push(object item)
{
list.Add(item);
p_index++;
}
public object pop()
{
object obj = this.list[p_index];
list.RemoveAt(p_index);
p_index--;
return obj;
}
public void clear()
{
list.Clear();
p_index = -1;
}
public object peek()
{
return list[p_index];
}
public int count
{
get { return this.list.Count; }
}
}
Stack class tips:
There are three ways to instantiate a stack object. The default constructor
instantiates an empty stack with an initial capacity of 10 values. The default
constructor is called as follows:
Stack myStack = new Stack();
A generic stack is instantiated as follows:
Stack<string> myStack = new Stack<string>();
Each time the stack reaches full capacity, the capacity is doubled.
The second Stack constructor method allows you to create a stack object
from another collection object. For example, you can pass the constructor as
an array and a stack is built from the existing array elements:
string[] names = newstring[]{"Raymond","David", "Mike"};
Stack nameStack = new Stack(names);
Executing the Pop method will remove “Mike” from the stack first.
The code for instantiating a Stack object with an initial capacity looks like this:
Stack myStack = new Stack(25);
2.Queue
public class CQueue
{
private ArrayList pQueue;
public CQueue()
{
pQueue = new ArrayList();
}
public void EnQueue(object item)
{
pQueue.Add(item);
}
public object DeQueue()
{
object obj = pQueue[0];
pQueue.RemoveAt(0);
return obj;
}
public object Peek()
{
return pQueue[0];
}
public void ClearQueue()
{
pQueue.Clear();
}
public int Count()
{
return pQueue.Count;
}
}
Queue Class Tips
You are not limited to these numbers however. You can
specify a different initial capacity when you instantiate a queue. Here’s how:
Queue myQueue = new Queue(100);
This sets the queue’s capacity to 100 items. You can change the growth
factor as well. It is the second argument passed to the constructor, as in:
Queue myQueue = new Queue(32, 3);
A generic Queue is instantiated like this:
Queue<int> numbers = new Queue<int>();
This line specifies a growth rate of 3 with the default initial capacity. You have
to specify the capacity even if it’s the same as the default capacity since the
constructor is looking for a method with a different signature.
3.Priority Queue Class
public class PQueue : Queue
{
public PQueue()
{
}
public override object Dequeue()
{
object[] items;
int min;
items = this.ToArray();
min = ((Model)items[0]).Priority;
for (int x = 1; x <= items.GetUpperBound(0); x++)
{
if (((Model)items[x]).Priority > min)
{
min = ((Model)items[x]).Priority;
}
}
this.Clear();
int x2;
for (x2 = 0; x2 <= items.GetUpperBound(0); x2++)
{
if (((Model)items[x2]).Priority == min && ((Model)items[x2]).Id != "")
{
this.Enqueue(items[x2]);
}
}
for (x2 = 0; x2 <= items.GetUpperBound(0); x2++)
{
if (((Model)items[x2]).Priority != min && ((Model)items[x2]).Id != "")
{
this.Enqueue(items[x2]);
}
}
return base.Dequeue();
}
public override string ToString()
{
return base.ToString();
}
}
课后练习:
作 者:doku
出 处:http://www.cnblogs.com/kulong995/
关于作者:喜欢编程,喜欢美食,专注于.NET项目开发。
版权声明:本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接。
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是作者坚持原创和持续写作的最大动力!


浙公网安备 33010602011771号