栈
namespace ConsoleApp1024pm
{
public class Person
{
public string name;
}
internal class Program
{
static void Main(string[] args)
{
Stack stack = new Stack();
stack.Push(1);
stack.Push(2);
stack.Push(new Person());
int length=stack.Count;
for(int j = 0; j < length; j++)
{
object i = stack.Peek;
Console.WriteLine(i);
stack.Pop();
}
while(stack.Count > 0)
{
object i =stack.Peek();
Console.WriteLine(i);
}
Console.WriteLine(stack.Contains(1));
static void getBin(int num)
{
Stack stack1 = new Stack();
while (num >0)
{
int remanier = num % 2;
stack1.Push(remanier);
num /= 2;
}
while (stack1.Count > 0)
{
Console.Write(stack1.Peek());
stack1.Pop();
}
}
Console.WriteLine("请输入一个十进制数");
int num = Convert.ToInt32(Console.ReadLine());
getBin(num);
}
}
}
队列
namespace ConsoleApp1024pm
{
public class Person
{
public string name;
}
internal class Program
{
static void Main(string[] args)
{
Queue queue = new Queue();
queue.Enqueue("dfdf");
queue.Enqueue(34);
queue.Enqueue("name");
queue.Dequeue();
Console.WriteLine(queue.Peek());
Console.WriteLine(queue.Contains(34));
queue.Clear();
Console.WriteLine(queue);
Random random = new Random();
List<string> news = new List<string>() { "喜迎二十大","研究出克制癌症的疫苗","俄乌冲突持续发酵", };
Queue queue = new Queue();
for(int i= 0; i < 10; i++)
{
string str = news[random.Next(0, news.Count)];
queue.Enqueue(str);
}
while(queue.Count > 0)
{
Console.WriteLine(queue.Peek());
queue.Dequeue();
Thread.Sleep(1000);
}
}
}
}