c#自定义LinkedList Queue Stack

来源:互联网 发布:python中的编码问题 编辑:程序博客网 时间:2024/05/16 04:57
        //MyLinkedList
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);
        MyLinkedList linkedList = new MyLinkedList();
        linkedList.AddFirst(node1);
        linkedList.AddLast (node2);
        linkedList.AddLast (node5);
        Debug.Log ("添加125" +linkedList);
        linkedList.AddBefore (node2, node3);
        Debug.Log ("2之前添加3" +linkedList);
        linkedList.AddAfter (node2, node4);
        Debug.Log ("2之后添加4" +linkedList);
        Debug.Log ("获取fist:" +linkedList.Fist.Data);
        Debug.Log ("获取last:" +linkedList.Last.Data);
        Debug.Log ("查找find3 :" + (linkedList.Find(3)).Data);
        linkedList.Delete (node1);
        Debug.Log ("删除1" + linkedList);
        linkedList.Delete (node4);
        Debug.Log ("删除4" +linkedList);
        Node node6 = new Node(6);
        Debug.Log ("contains5 " + linkedList.Contains (node5));
        Debug.Log("contains6 " + linkedList.Contains (node6));


public class Node
{
    public object Data;
     Node next;
     Node prev;

    public Node Next{
        get{return next;}
        set{next = value;}
    }
    public Node Prev{
        get {return prev;}
        set{prev = value;}
    }
    public Node(object Data){
        this.Data = Data;
    }
}

//自定义MyLinkedList
public class MyLinkedList
{
    public Node head;
    public Node tail;
    public object LinkData;
    int count;
    public int Count{
        get{return count;}
    }
    public Node Fist{
        get{return head;}
    }
    public Node Last{
        get{ return tail;}
    }
    public Node Find(object data){
        if (count == 0) {
            throw new NullReferenceException ("链表元素为空");
        }
        if (data.Equals(tail.Data)) {
            return tail;
        }
        Node finde = head;
        while (!(finde.Data.Equals(data))) {
            finde = finde.Next;
            if (finde == null) {
                throw new NullReferenceException ("链表中未找到该元素");
            }
        }
        return finde;
    }

    public void Delete(Node node){
        if (count == 0) {
            throw new NullReferenceException ("链表元素为空不可删除");
        }
        count--;
        if (node == head) {
            Node temp = head.Next ;
            head = temp;
            temp.Prev = null;
            return;
        }
        if (node == tail) {
            Node temp = tail.Prev;
            tail = temp;
            tail.Next = null;
            return;
        }
        Node delete = head;
        while (delete != node) {
            delete = delete.Next;
            if (delete == null) {
                throw new NullReferenceException ("链表中无该元素");
            }
        }
        Node preNode = delete.Prev;
        Node nextNode = delete.Next;
        delete.Prev.Next = nextNode;
        delete.Next.Prev = preNode;
    }

    public void AddBefore (Node node, Node newNode)
    {
        if (node == head) {
            AddFirst (newNode);
            return;
        }

        Node temp = head;
        while (temp != node) {
            temp = temp.Next;
        }
        Node preNode = temp.Prev;
        Node nextNode = temp;
        temp = newNode;
        preNode.Next = newNode;
        newNode.Next = nextNode;
        newNode.Prev = preNode;
        newNode.Prev.Next = newNode;
        newNode.Next.Prev = newNode;
        count++;
    }


    public void AddAfter (Node node, Node newNode)
    {
        if (node == tail) {
            AddLast (newNode);
            return;
        }
        Node temp = head;
        while (temp != node) {
            temp = temp.Next;
        }
        Node preNode = temp;
        Node nextNode = temp.Next;
        temp = newNode;
        preNode.Next = newNode;
        newNode.Next = nextNode;
        newNode.Prev = preNode;

        newNode.Prev.Next = newNode;
        newNode.Next.Prev = newNode;
        count++;
    }

    public void AddFirst (Node node)
    {
        count++;
        if (head == null) {
            head = node;
            tail = node;
            head.Prev = null;
            head.Next = null;
            tail.Prev = null;
            tail.Next = null;
        } else {
            head.Prev = node;
            Node nextNode = head;
            Node preNode = null;
            head = node;
            head.Next = nextNode;
            head.Prev = preNode;
        }
    }

    public void AddLast (Node node)
    {
        count++;
        if (tail == null) {
            tail = node;
            head = node;
            tail.Prev = null;
            tail.Next = null;
            head.Prev = null;
            tail.Prev = null;
        } else {
            tail.Next = node;
            Node preNode = tail;
            Node nextNode = null;
            tail = node;
            node.Prev = preNode;
            node.Next = nextNode;
        }
    }
    public bool Contains(Node node){
        if (count == 0) {
            throw new NullReferenceException ("链表元素为空");
        }
        if (node == head || node == tail) {
            return true;
        }
        Node find = head;
        while (find != node) {
            if (find == null) {
                return false;
            }
            find = find.Next;
        }
        return true;
    }

    public override string ToString ()
    {
        string str = "";
        Node temp = head;
        while (temp!= null) {
            str += (temp.Data.ToString() + " ");
            temp = temp.Next;
        }
        return string.Format ("[MyLinkedList:[{0}] count:{1}]",str,count);
    }
}

//自定义queue
public class MyQueue
{
    object[] queue;
    int count = 0;

    public int Count {
        get{ return count; }
    }

    public MyQueue ()
    {
    }

    public MyQueue (int capacity)
    {
        queue = new object[capacity];
    }

    public void Enqueue (object obj)
    {
        if (count == 0) {
            queue = new object[32];
        }
        if (count >= queue.Length) {
            object[] temp = queue;
            queue = new object[temp.Length * 2];
            for (int i = 0; i < count; i++) {
                queue [i] = temp [i];
            }
        }
        queue [count++] = obj;
    }

    public object Dequeue ()
    {
        if (count < 1) {
            throw new NullReferenceException ("数据已为空");
        }
        object[] temp = queue;
        queue = new object[temp.Length];
        for (int i = 1; i < count; i++) {
            queue [i - 1] = temp [i];
        }
        count--;
        return temp [0];
    }

    public object Peek ()
    {
        if (count < 1) {
            throw new NullReferenceException ("数据已为空");
        }
        return queue [0];
    }

    public override string ToString ()
    {
        string str = "";
        foreach (object obj in queue) {
            str += obj;
        }
        return string.Format ("[MyQueue: Count={0}] queue:{1}", Count, str);
    }
}


//自定义stack
public class MyStack
{
    object[] stack;
    int count = 0;

    public int Count {
        get{ return count; }
    }

    public MyStack ()
    {
    }

    public MyStack (int initialCapacity)
    {
        stack = new object[initialCapacity];
    }

    public void Push (object obj)
    {
        if (count == 0) {
            stack = new object[10];
        }
        if (count >= stack.Length) {
            object[] temp = stack;
            stack = new object[temp.Length * 2];
            for (int i = 0; i < count; i++) {
                stack [i] = temp [i];
            }
        }
        stack [count++] = obj;
    }

    public object Pop ()
    {
        if (count < 1) {
            throw new NullReferenceException ("数据已为空");
        }
        object[] temp = stack;
        stack = new object[temp.Length];
        for (int i = 0; i < count - 1; i++) {
            stack [i] = temp [i];
        }
        object value = temp [count - 1];
        count--;
        return value;
    }

    public object Peek ()
    {
        if (count < 1) {
            throw new NullReferenceException ("数据已为空");
        }
        return stack [count - 1];
    }

    public override string ToString ()
    {
        string str = "";
        foreach (object obj in stack) {
            str += (obj);
        }
        return string.Format ("[MyStack: Count={0} stack{1}]", Count, str);
    }
}

//public static interface IMyCompara{
    
//}

public interface IMyComparable
{
    int ComparaTo (object obj);
}

public  class Person: IMyComparable
{
    int age;

    public int Age {
        get{ return age; }
        set{ age = value; }
    }

    public Person ()
    {
    }

    public Person (int age)
    {
        this.age = age;
    }

    public override string ToString ()
    {
        return string.Format ("[Person: Age={0}]", Age);
    }

    public    int ComparaTo (object obj)
    {
        if (obj is Person) {
            Person other = (Person)obj;
            if (age > other.age)
                return 1;
            else if (age < other.age)
                return -1;
            else if (age == other.age)
                return 0;
        }
        throw new NullReferenceException ("比较识别不是Person的类型");
    }

    public    static int Compara (object obj1, object obj2)
    {
        if (obj1 is Person && obj2 is Person) {
            Person p1 = (Person)obj1;
            Person p2 = (Person)obj2;
            if (p1.Age > p2.Age)
                return 1;
            else if (p1.Age < p2.Age)
                return -1;
            else if (p1.Age == p2.Age)
                return 0;    
        }
        throw new NullReferenceException ("比较识别不是Person的类型");
    }

}

0 0
原创粉丝点击