蓝鸥Unity开发基础二——课时25 栈和队列
一、栈和队列
栈遵循后进先出的原则
队列遵循先进后出的原则
栈和队列根据需要容量自动增加
栈和队列都允许重复元素
推荐视频讲师博客:http://11165165.blog.51cto.com/
using System;
using System.Collections.Generic;
namespace Lesson_25
{
class MainClass
{
public static void Main (string[] args)
{
Stack<string> s = new Stack<string> ();
int count = s.Count;
s.Clear ();
bool b = s.Contains ("老王");
//把元素入栈
s.Push("老王");
s.Push("老张");
s.Push("小明");
//Pop把元素出栈,栈中就没有这个元素了
string s1 = s.Pop ();
Console.WriteLine (s1);
string s2 = s.Pop ();
Console.WriteLine (s2);
string s3= s.Pop ();
Console.WriteLine (s3);
}
}
}
using System;
using System.Collections.Generic;
namespace Lesson_25
{
class MainClass
{
public static void Main (string[] args)
{
// Stack<string> s = new Stack<string> ();
// int count = s.Count;
// s.Clear ();
// bool b = s.Contains ("老王");
//把元素入栈
// s.Push("老王");
// s.Push("老张");
// s.Push("小明");
//Pop把元素出栈,栈中就没有这个元素了
// string s1 = s.Pop ();
// Console.WriteLine (s1);
// string s2 = s.Pop ();
// Console.WriteLine (s2);
// string s3= s.Pop ();
// Console.WriteLine (s3);
Queue<string> q = new Queue<string> ();
q.Clear ();
int count = q.Count;
bool b = q.Contains ("老王");
//向队列中添加元素
q.Enqueue("老王");
q.Enqueue("老张");
q.Enqueue("小明");
//获取队列中元素
string s1=q.Dequeue();
Console.WriteLine (s1);
string s2 = q.Dequeue ();
Console.WriteLine (s2);
string s3 = q.Dequeue ();
Console.WriteLine (s3);
}
}
}
原文地址:http://11131960.blog.51cto.com/11121960/1850591