1.背包(Bag)——不可删掉元素
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //迭代器命名空间
public class Bag<Item> : IEnumerable<Item>
{
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<Item> GetEnumerator()
{
Node<Item> current = first;
while (current != null)
{
yield return current.item;
current = current.next;
}
}
private Node<Item> first; // top of stack
private Node<Item> current; // top of stack
public static int n; // size of the stack
// helper linked list class
private class Node<Item>
{
public Item item;
public Node<Item> next;
}
public Bag()
{
first = null;
n = 0;
}
public bool isEmpty()
{
return first == null;
}
public int size()
{
return n;
}
public void Add(Item item)
{
Node<Item> oldfirst = first;
first = new Node<Item>();
first.item = item;
first.next = oldfirst;
n++;
}
public Item peek()
{
if (isEmpty()) throw new System.Exception("Stack underflow");
return first.item;
}
2.队列(Queue)先进先出.......................................................................................
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //迭代器命名空间
public class Queue<Item> : IEnumerable<Item>
{
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<Item> GetEnumerator()
{
Node<Item> current = first;
while (current != null)
{
yield return current.item;
current = current.next;
}
}
private Node<Item> first; // top of stack
private Node<Item> last;
private Node<Item> current; // top of stack
public static int n; // size of the stack
// helper linked list class
private class Node<Item>
{
public Item item;
public Node<Item> next;
}
public Queue()
{
first = null;
n = 0;
}
public bool isEmpty()
{
return first == null;
}
public int size()
{
return n;
}
public void Enqueue(Item item)
{
Node<Item> oldlast = last;
last = new Node<Item>();
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else
oldlast.next = last;
n++;
}
public Item Dequeue()
{
Item item = first.item; // save item to return
first = first.next; // delete first node
if (isEmpty()) first = null;
n--;
return item; // return the saved item
}
public Item peek()
{
if (isEmpty()) throw new System.Exception("Stack underflow");
return first.item;
}
}
3.栈 (Stack) 后进先出........................................................................................
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //迭代器命名空间
public class Stack<Item> : IEnumerable<Item>
{
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<Item> GetEnumerator()
{
Node<Item> current = first;
while (current != null)
{
yield return current.item;
current = current.next;
}
}
private Node<Item> first; // top of stack
private Node<Item> current; // top of stack
public static int n; // size of the stack
// helper linked list class
private class Node<Item>
{
public Item item;
public Node<Item> next;
}
public Stack()
{
first = null;
n = 0;
}
public bool isEmpty()
{
return first == null;
}
public int size()
{
return n;
}
public void push(Item item)
{
Node<Item> oldfirst = first;
first = new Node<Item>();
first.item = item;
first.next = oldfirst;
n++;
}
public Item pop()
{
if (isEmpty()) throw new System.Exception("Stack underflow");
Item item = first.item; // save item to return
first = first.next; // delete first node
n--;
return item; // return the saved item
}
public Item peek()
{
if (isEmpty()) throw new System.Exception("Stack underflow");
return first.item;
}