//非泛型
//栈 LIFO
int[] arr = { 1, 2, 4, 5, 6 };
Stack stack = new Stack(arr); //栈顶为6,栈底是1
//入栈,无返回值
stack.Push("jack");
//获取栈顶元素
object b = stack.Peek();
Console.WriteLine(b);
//移除栈顶元素,并返回值.即出栈栈顶元素
string str =stack.Pop() as string;
Console.WriteLine(str);
//元素个数
//int a = stack.Count;
//Console.WriteLine(a);
////是否包含
//bool c = stack.Contains("jack");
//Console.WriteLine(c);
//遍历
int count = stack.Count;
for (int i = 0; i < count; i++)
{
//每次都移除一个,count变小
Console.WriteLine(stack.Pop());
}
//遍历不会改变stack
foreach (object d in stack)
{
Console.WriteLine(d);
}
//队列,FIFO
Queue queue = new Queue(arr);
//入队列
queue.Enqueue("jack");
Console.WriteLine(queue.Peek());
//出队列,先去除先进来的第一个
object obj = queue.Dequeue();
Console.WriteLine(obj);
//祛除所有元素
//queue.Clear();
object[] objArr = queue.ToArray();
Console.WriteLine(queue.Count);
//清除未满未用的内存
queue.TrimToSize();
//遍历
foreach (object o in queue)
{
Console.WriteLine(o);
}
//泛型栈和队列
Stack<int> stack_1 = new Stack<int>(arr);
Queue<string> queue_1 = new Queue<string>();
stack_1.Push(3);
queue_1.Enqueue("hello");
//栈 LIFO
int[] arr = { 1, 2, 4, 5, 6 };
Stack stack = new Stack(arr); //栈顶为6,栈底是1
//入栈,无返回值
stack.Push("jack");
//获取栈顶元素
object b = stack.Peek();
Console.WriteLine(b);
//移除栈顶元素,并返回值.即出栈栈顶元素
string str =stack.Pop() as string;
Console.WriteLine(str);
//元素个数
//int a = stack.Count;
//Console.WriteLine(a);
////是否包含
//bool c = stack.Contains("jack");
//Console.WriteLine(c);
//遍历
int count = stack.Count;
for (int i = 0; i < count; i++)
{
//每次都移除一个,count变小
Console.WriteLine(stack.Pop());
}
//遍历不会改变stack
foreach (object d in stack)
{
Console.WriteLine(d);
}
//队列,FIFO
Queue queue = new Queue(arr);
//入队列
queue.Enqueue("jack");
Console.WriteLine(queue.Peek());
//出队列,先去除先进来的第一个
object obj = queue.Dequeue();
Console.WriteLine(obj);
//祛除所有元素
//queue.Clear();
object[] objArr = queue.ToArray();
Console.WriteLine(queue.Count);
//清除未满未用的内存
queue.TrimToSize();
//遍历
foreach (object o in queue)
{
Console.WriteLine(o);
}
//泛型栈和队列
Stack<int> stack_1 = new Stack<int>(arr);
Queue<string> queue_1 = new Queue<string>();
stack_1.Push(3);
queue_1.Enqueue("hello");