单链表


  public interface IListDS
    {
        int GetLength(); //求长度 
        void Clear(); //清空操作 
        bool IsEmpty(); //判断线性表是否为空 
        void Append(T item); //附加操作 
        void Insert(T item, int i); //插入操作 
        T Delete(int i); //删除操作 
        T GetElem(int i); //取表元 
        int Locate(T value); //按值查找 
    }
 public  class Node
    {
        private T data;
        private Node next;
        public Node(T val,Node p)
        {
            data = val;
            next = p;
        }
        public Node(T val)
        {
            data = val;
            next = null;
        }
        public Node(Node p)
        {
            next = p;
        }
        public Node()
        {
            data = default(T);
            next = null;
        }

        public T Data
        {
            get { return data; }
            set { data = value; }
        }
        public Node Next
        {
            get { return next; }
            set { next = value; }
        }
    }
  public class LinkList : IListDS
    {
        //头引用
        public Node Head { get; set; }
        public LinkList()
        {
            Head = null;
        }
        public void Append(T item)
        {
            if (Head == null)
            {
                Head = new Node(item);
                return;
            }

            Node p = Head;
            while (p.Next != null)
            {
                p = p.Next;
            }
            p.Next = new Node(item);
        }

        public void Clear()
        {
            Head = null;
        }

        public T Delete(int i)
        {

            if (IsEmpty() || i < 0)
            {
                Console.WriteLine("Link is empty or Position is error!"); 
                return default(T);
            }
            Node q = new Node();

            if (i == 1)
            {
                q = Head;
                Head = Head.Next;
                return q.Data;
            }
            Node p = Head;
            int j = 1;
            while (p.Next != null && j < i)
            {
                ++j;
                q = p;
                p = p.Next;
            }
            if (j == i)
            {
                q.Next = p.Next;
                return p.Data;
            }
            else
            {
                Console.WriteLine("The ith node is not exist!");
                return default(T);
            }

        }

        public T GetElem(int i)
        {
            if (IsEmpty())
            {
                Console.WriteLine("List is Empty");
                return default(T);
            }
            Node p = Head;
            int j = 1;
            while (p.Next != null && j < i)
            {
                ++j;
                p = p.Next;
            }
            if (j == i)
            {
                return p.Data;
            }
            else
            {
                Console.WriteLine("The ith node is not exist");
                return default(T);
            }
        }

        public int GetLength()
        {
            int len = 0;
            Node p = Head;
            while (p != null)
            {
                len++;
                p = p.Next;
            }
            return len;
        }

        public void Insert(T item, int i)
        {
            if (IsEmpty() || i < 1)
            {
                Console.WriteLine("List is empty or Posotion is error");
                return;
            }
            if (i == 1)
            {
                Node q = new Node(item);
                q.Next = Head;
                Head = q;
                return;
            }
            Node p = Head;
            Node r = new Node();
            int j = 1;
            while (j < i && p.Next != null)
            {
                r = p;
                p = p.Next;
                ++j;
            }
            if (j == i)
            {
                Node q = new Node(item);
                q.Next = p;
                r.Next = q;
            }
        }

        public bool IsEmpty()
        {
            return Head == null;
        }

        public int Locate(T value)
        {
            if (IsEmpty())
            {
                Console.WriteLine("List is empty");
                return -1;
            }
            Node p = Head;
            int j = 1;
            while (!p.Data.Equals(value) && p.Next != null)
            {
                ++j;
                p = p.Next;
            }
            return j;
        }
    }

test:

  class Program
    {
        static void Main(string[] args)
        {
            LinkList<int> linkList = new LinkList<int>();
            linkList.Append(1);
            linkList.Append(2);
            linkList.Append(4);
            int len = linkList.GetLength();
            linkList.Insert(3, 3);
            bool e = linkList.IsEmpty();
            int value = linkList.Locate(4);
            int d = linkList.Delete(2);
        }
    }