集合类 链表类 定义链表 LinkedList
LinkedList<string> list = new LinkedList<string>();
list.AddFirst("头");
Console.WriteLine(list.First.Value);
list.AddLast("尾结点");
Console.WriteLine(list.Last.Value);
list.AddBefore(list.First, "新头");
Console.WriteLine(list.First.Value);
list.AddAfter(list.Last, "新的尾结点");
LinkedListNode<string> node = list.Find("尾结点");
Console.WriteLine(node.Value);
list.Remove("新的尾");
Console.WriteLine(list.Last.Value);
list.RemoveLast();
Console.WriteLine(list.Last.Value);
Console.WriteLine(list.Contains("新头"));
foreach(string i in list)
{
Console.WriteLine(i);
}
Console.WriteLine(list.Last.Previous.Value);
线程 进程
线程 操作系统能够进行运算调度的最小单位,包含在进程中,是进程中的实际运作单位,一个线程指的是进程中的单一顺序的控制流,一个进程可以并发多个线程,我们目前写的程序都在主线程中,简单理解就是代码从到下运行的一条“管道”
进程 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,简单理解就是我们每打开一个应用程序就是在操作系统上开启了一个进程。
线程学习
# 题目 控制台有一个■通过键盘上的↑,↓,←,→键控制方块的移动,通过开启一个多线程来检测输入控制它的转向,而且要判断边界,方块不能超出边界。
namespace 线程_进程
{
internal class Program
{
public static bool isRuning = true;
public static int exp = 0;
public static object obj=new object();
static void Main(string[] args)
{
Thread T = new Thread(ThreadLogic);
T.Start();
while(true)
{
Console.BackgroundColor = ConsoleColor.Red;
lock(obj)
{
Console.SetCursorPosition(0,0);
Console.Write("*");
}
}
T.IsBackground = true;
Console.WriteLine("sfs");
Console.ReadKey();
}
public static void ThreadLogic()
{
}
}
}
namespace 线程练习
{
//创建一个方块类
public class Block
{
public Block(int x, int y)
{
this.x = x * 2
this.y = y
}
public int x
public int y
public ConsoleColor color = ConsoleColor.White
//方块都有一个画的方法
public void draw()
{
Console.SetCursorPosition(this.x, this.y)
Console.ForegroundColor = color
Console.Write("■")
}
//清除的方法
public void clear()
{
Console.SetCursorPosition(this.x, this.y)
Console.Write(" ")
}
}
//创建一个子类 头
public class Head : Block
{
public Head(int x, int y, Dir dir = Dir.right) : base(x, y)
{
this.dir = dir
base.color = ConsoleColor.Red
}
public Dir dir
//移动的方法
public void move()
{
switch (dir)
{
case Dir.up:
y--
break
case Dir.down:
y++
break
case Dir.left:
x -= 2
break
case Dir.right:
x += 2
break
}
}
//碰撞检测的方法
public bool crash(Block block)
{
if (block.x == this.x && block.y == this.y)
{
return true
}
return false
}
}
//创建一个枚举来存方向
public enum Dir
{
up, down, left, right
}
internal class Program
{
public static object obj = new object()
public static int width = Console.BufferWidth
public static int height = Console.BufferHeight
//创建一个不动方块对象
public static Block block = new Block(5, 5)
//创建一个头
public static Head head = new Head(0, 0)
//创建一个列表存每一个方块
public static List<Head> list = new List<Head>()
//更新不动的方块
public static void update()
{
Random random = new Random()
int x = random.Next(0, 30) * 2
int y = random.Next(0, 30)
block.x = x
block.y = y
block.draw()
}
static void Main(string[] args)
{
Console.CursorVisible = false
list.Add(head)
list[0].draw()
//创建一个线程
Thread T = new Thread(ThreadLogic)
Thread T2 = new Thread(ThreadLogic2)
T2.Start()
T.Start()
T.IsBackground = true
T2.IsBackground = true
while (true)
{
lock (obj)
{
Thread.Sleep(200)
list[0].clear()
list[0].move()
list[0].draw()
}
}
}
static void ThreadLogic2()
{
block.draw()
while (true)
{
lock (obj)
{
if (list[0].crash(block))
{
update()
}
}
}
}
static void ThreadLogic()
{
while (true)
{
switch (Console.ReadKey(true).Key)//检测用户输入的按键
{
case ConsoleKey.UpArrow:
//只有一个头随便跑 如果不是一个方块那么不能往反方向走
if (head.dir != Dir.down || list.Count == 1) head.dir = Dir.up
break
case ConsoleKey.DownArrow:
//只有一个头随便跑 如果不是一个方块那么不能往反方向走
if (head.dir != Dir.up || list.Count == 1) head.dir = Dir.down
break
case ConsoleKey.LeftArrow:
//只有一个头随便跑 如果不是一个方块那么不能往反方向走
if (head.dir != Dir.right || list.Count == 1) head.dir = Dir.left
break
case ConsoleKey.RightArrow:
//只有一个头随便跑 如果不是一个方块那么不能往反方向走
if (head.dir != Dir.left || list.Count == 1) head.dir = Dir.right
break
}
}
}
}
}