链表
链表中插入元素
LinkedList<int> ll = new LinkedList<int>();
LinkedListNode<int> node0 = ll.AddFirst(0);
LinkedListNode<int> node1 = ll.AddAfter(node0, 1);
LinkedListNode<int> node2 = ll.AddAfter(node1, 2);
输出链表元素
foreach(int i in ll){
Console.WriteLine(i); }
for(LinkedListNode<int> node = ll.First; node != null; node = node.Next) {
Console.WriteLine(node.Value); }
自己写一个泛化链表:
class Node<T>
{
public T data;//数据域
public Node<T> next;//指针域
}
class MyLinkedList<T>
{
private Node<T> head;
public void Insert(int i,T data)
{
if (i < 0)
throw new IndexOutOfRangeException();
//头插
if (i == 0)
{
Node<T> p = new Node<T>();
p.data = data;
p.next = head;
head = p;
return;
}
//定位到i的前一个节点
int cnt = 0;
for(Node<T> p = head; p != null; p = p.next)
{
if (cnt == i - 1)
{
Node<T> pNew = new Node<T>();
pNew.data = data;
pNew.next = p.next;
return;
}
cnt++;
}
throw new IndexOutOfRangeException();
}
public void Delete(int i)
{
if (i < 0)
throw new IndexOutOfRangeException();
if (head == null)
throw new IndexOutOfRangeException();
if (i == 0)
{
head = head.next;
return;
}
int m = 0;
for(Node<T> p = head; p.next != null; p = p.next)
{
if (m == i - 1)
{
p.next = p.next.next;
return;
}
m++;
}
throw new IndexOutOfRangeException();
}
public Node<T> Find(T data)
{
for(Node<T> p = head; p != null; p = p.next)
{
if (p.data.Equals(data))
{
return p;
}
}
return null;
}
public void Print()
{
for (Node<T> p = head; p != null; p = p.next)
Console.WriteLine(p.data);
}
}
堆栈
应用:浏览器前进、后退历史记录;用户编辑文档的undo/redo操作;字符串逆序;游戏菜单层级打开、关闭。
Stack1.peek() 返回栈顶元素,但不在堆栈中删除它。
Stack2.pop() 返回栈顶元素,并弹出该元素。
队列
应用:服务器排队;释放组合捷能;订单队列
Queue.Enqueue():入队
Queue.Dequeue():出队
利用线程同步解决订单问题
对争用的代码段加锁
static Queue<string> orders = new Queue<string>();
static object consoleLock = new object();
static void WriteString(int left,int top,string s)
{
lock (consoleLock) //解决线程争用
{
Console.SetCursorPosition(left, top);
Console.WriteLine(s);
}
}
static void OrderFood()
{
while (true)
{
WriteString(0,0,"------------Menu------------");
WriteString(0,1,"1.Pizza");
WriteString(0,2,"2.Hamberg");
WriteString(0,3,"3.Coffer");
WriteString(0,4,"4.Cola");
if (Console.KeyAvailable)
{
switch (Console.ReadKey(true).Key)
{
case ConsoleKey.D1:
orders.Enqueue("Pizza");
break;
case ConsoleKey.D2:
orders.Enqueue("Humberg");
break;
case ConsoleKey.D3:
orders.Enqueue("Coffee");
break;
case ConsoleKey.D4:
orders.Enqueue("Cola");
break;
}
}
}
}
static void CookFood()
{
while (true)
{
if (orders.Count > 0)
{
int sleepTime = new Random().Next(500, 3000);
Thread.Sleep(sleepTime);
string s = string.Format("已为您准备好:{0,20},用时:{1,10}秒", orders.Peek(), sleepTime / 1000.0f);
WriteString(0, 10, s);
orders.Dequeue();
}
}
}
static void Main(string[] args)
{
//辅助线程
Thread t = new Thread(OrderFood);
t.Start();
//主线程
CookFood();
Console.ReadLine();
}
线程同步的问题:线程争用耗费CPU;在被同步的代码段出现瓶颈;
循环队列
public class MyCircleQueue<T>
{
private T[] elems;
private int front = 0;
private int rear = 0;
public MyCircleQueue(int maxN)
{
elems = new T[maxN];
}
public bool IsFull
{
get
{
return (rear+1)%elems.Length==front;
}
}
public bool IsEmpty
{
get
{
return rear == front;
}
}
public int Count
{
get
{
return (rear - front + elems.Length) % elems.Length;
}
}
public T Peek()
{
return elems[front];
}
public void Enqueue(T elem)
{
elems[rear] = elem;
rear = (rear + 1) % elems.Length;
}
public T Dequeue()
{
T ret = elems[front];
front = (front + 1) % elems.Length;
return ret;
}
}