泛型学习二之顺序表,单链表。

首先自定义一个顺序表接口。接口中的操作有:获取长度,判空,清除,根据位置查找,索引,插入,添加,删除。

public interface ILinearList<T>
{
int Length{ get ; }
bool IsEmpty ();
void clear();
T GetItem (int index);
int LocateItem (T t);//
void Insert(T item,int index);
void Add(T item);
void Delete (int index);

}

再定义一个单链表的接口,其实两个可以共用。功能为:添加,取长,清空,插入,移除,查找值,查找位置,判空。

public interface LianbiaoList<T>{
        void Add (T item);
        int Count{ get;}
        void Clear ();
        void Insert (int index, T item);
        void RemoveAt (int index);
        T GetValue (int indexer);
        int GetIndex (T item);
        bool IsEmpty ();        
    }

 

然后构建顺序表和单链表的元素组成,顺序表感觉不用,可能学的太浅,单链表元素由数据域和指针域两部分组成,该元素应该有带参数(T item)和不带参数()两种形式。构建如下:

class Node<T>{
        public T Value;
        public Node<T>NextNode;
        public Node(T value){
            Value = value;
            NextNode = null;
        }
        public Node(){
            Value = default(T);
        }
    }

根据接口定义的方法,在操作层对其一一实现。

顺序表如下:

public class UseCube<T>:ILinearList<T>
    {
        public UseCube(int maxsize){
            if(maxsize<=0){
                throw new UnityException("the max size can not be zore");

            }
            MaxSize = maxsize;
            list =new T[MaxSize];
        }
        private T[] list;
        private int length;
        public int MaxSize {
            private set;
            get;

        }
        public int Length{get {return length;}}

        public bool IsEmpty(){
            return length == 0;
        }
        public void clear(){
            length = 0;
        }
        public T GetItem(int index){
            return list [index];
        }
        public int GetPosition(T value){
            if (isFull ()) {
                return -1;
            } else {
                int i = 0;
                while (!list [i].Equals (value) && i < list.Length) {
                    i++;
                }
                return i;
            }

        }
        public int LocateItem(T t){
            for (int i = 0; i < list.Length; i++) {
                if (list [i].Equals (t)) {
                    return i;
                }
            }
            return -1;
        }
        public void Insert(T item ,int index){
            if (index < 0 || index > length-1) {
                throw new UnityException ("this index is wrong!");
            }
            length++;
            if (isFull ()) {
                throw new UnityException ("this linear list is full");
            }
            for (int i = length-1; i > index; i--) {
                list [i] = list [i - 1];

            }
            list [index] = item;

        
        }
        public void Add(T item){
            length++;
            if (isFull ()) {
                throw new UnityException ("this linear list is full");

            } else {
                list [length - 1] = item;
            }
        }
        public void Delete(int index){
            if (index < 0 || index > length - 1) {
            
                throw new UnityException ("the index is wrong !");}
            length--;
            
            for (int i = index; i <length; i++) {
                list [i] = list [i + 1];
            }

        }
        bool isFull(){
            return length >= MaxSize;
        }
    }

单链表的实现如下:

class TestList<T>:LianbiaoList<T>{
        private Node<T>head = null;
        private int _count = 0;
        private int maxSize =20;
        public bool IsEmpty(){
            return _count > maxSize;
        }
        public void Add(T item){
            
            if (head == null) {
                head = new Node<T> (item);
                _count++;
            } else {
                Node<T> node = head;

                while (node.NextNode != null) {
                    node = node.NextNode;
                }
                _count++;
                node.NextNode = new Node<T> (item);

            }

        
    }
        public int Count{get{ return _count;}}
        public void Clear(){
            head = null;
            _count = 0;
        }
        public void Insert(int index,T item){
            if (index >= 0 && index < _count) {
                Node<T> node = head;
                Node<T> prev = null;
                Node<T> next = null;
                if (index == 0) {
                    next = head;
                    head = new Node<T> (item);
                    head.NextNode = next;
                } else {
                    for (int i = 0; i < index; i++) {
                        prev = node;
                        node = node.NextNode;
                    }
                    next = node;
                    node = new Node<T> (item);
                    node.NextNode = next;
                    prev.NextNode = node;
                }
                _count++;
            } /*else {
                throw new UnityException ("Out of Range!");
            }*/
        }
        public void RemoveAt(int index){
            if (index >= 0 && index < _count) {
                Node<T> node = head;
                Node<T> prev = null;
                Node<T> next = null;
                for (int i = 0; i < index; i++) {
                    prev = node;
                    node = node.NextNode;
                }
                prev.NextNode = node.NextNode;
                _count--;
            }
        }
        public IEnumerator<T> GetEnumerator(){
            Node<T> node = head;
            Node<T> result = new Node<T> ();
            while (node != null) {
                result = node;
                node = node.NextNode;
                yield return result.Value;
            }
        }
        public T GetValue(int index){
            if (index >= 0 && index < _count) {
                Node<T> node = head;
                for (int i = 0; i < index; i++) {
                    node = node.NextNode;
                }
                return node.Value;
            }
            return default(T);
        }
        public int GetIndex(T item){
            Node<T> node = head;
            int i = 1;
            while (!node.Value.Equals (item) && node.NextNode != null) {
                node = node.NextNode;
                i++;
            }
            if (i == _count) {
                return -1;
            } else {
                return i;
            }    
            }
                
        }

今天的就到这了,后面将学习unity  GameObject在顺序表和单链表中的操作。

 

posted on 2018-03-08 15:56  work-liuwei  阅读(173)  评论(0编辑  收藏  举报

导航