C# 数据结构与算法 -单链表

    interface IlistDS<T>
    {
        int GetLength();
        void Clear();
        bool IsEmpty();
        void Add(T item);
        void Insert(T item, int index);
        T Delete(int index);
        T this[int index] { get; }
        T GetEle(int index);
        int Locat(T item);
    }
    /// <summary>
    /// 单链表节点
    /// </summary>
    /// <typeparam name="T"></typeparam>
    class Node<T>
    {
        private T data;//存储数据
        private Node<T> next;//指针 用来指向下一个元素

        public Node()
        {
            this.data = default(T);
            this.next = null;
        }

        public Node(T Value)
        {
            this.data = Value;
            this.next = null;
        }

        public Node(T Value,Node<T> next)
        {
            this.data = Value;
            this.next = next;
        }

        public Node(Node<T> next)
        {
            this.next = next;
        }

        public T Data
        {
            get { return this.data; }
            set { this.data = value; }
        }

        public Node<T> Next
        {
            get { return this.next; }
            set { this.next = value; }
        }
    }
    class LinkList<T> : IlistDS<T>
    {
        private Node<T> head;//存储一个头结点

        public LinkList()
        {
            this.head = null;
        }

        public T this[int index]
        {
            get
            {
                Node<T> Temp = this.head;
                for (int i = 1; i <= index; i++)
                {
                    Temp = Temp.Next;
                }
                return Temp.Data;
            }
        }

        public void Add(T item)
        {
            Node<T> newNode = new Node<T>(item);//根据新的数据创建一个新的节点
            //如果头节点为空,那么这个新的节点就是头节点
            if (this.head == null)
            {
                this.head = newNode;
            }
            else//把新节点放到链表的尾部
            {
                //要访问到链表的尾节点
                Node<T> Temp = this.head;
                while (true)
                {
                    if (Temp.Next != null)
                    {
                        Temp = Temp.Next;
                    }
                    else
                    {
                        break;
                    }
                }
                Temp.Next = newNode;//把新节点放到链表的尾部
            }
        }

        public void Clear()
        {
            this.head = null;
        }

        public T Delete(int index)
        {
            T data = default(T);
            if (index == 0)//删除头节点
            {
                data = head.Data;
                this.head = head.Next;
            }
            else
            {
                Node<T> Temp = this.head;
                for (int i = 1; i <= index - 1; i++)
                {
                    Temp = Temp.Next;
                }
                data = Temp.Next.Data;
                Temp.Next = Temp.Next.Next;
            }
            return data;
        }

        public T GetEle(int index)
        {
            return this[index];
        }

        public int GetLength()
        {
            if (this.head == null) return 0;
            int Length = 1;
            Node<T> Temp = this.head;
            while (true)
            {
                if (Temp.Next != null)
                {
                    Temp = Temp.Next;
                    Length++;
                }
                else
                {
                    break;
                }
            }
            return Length;
        }

        public void Insert(T item, int index)
        {
            Node<T> newNode = new Node<T>(item);
            if (index == 0)
            {
                newNode.Next = head;
                head = newNode;
            }
            else
            {
                Node<T> Temp = this.head;
                for (int i = 1; i <= index - 1; i++)
                {
                    Temp = Temp.Next;
                }
                newNode.Next = Temp.Next;
                Temp.Next = newNode;
            }
        }

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

        public int Locat(T item)
        {
            Node<T> temp = head;
            int index = 0;
            if (temp == null)
            {
                return -1;
            }
            else
            {
                while (true)
                {
                    if (temp.Data.Equals(item))
                    {
                        return index;
                    }
                    else
                    {
                        if (temp.Next != null)
                        {
                            temp = temp.Next;
                            index++;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                return -1;
            }
        }
    }
        static void Main(string[] args)
        {
            //使用自己的顺序表
            LinkList<string> seqList = new LinkList<string>();

            seqList.Add("123");
            seqList.Add("456");
            seqList.Add("789");

            Console.WriteLine(seqList.GetEle(0));
            Console.WriteLine(seqList[0]);

            seqList.Insert("777", 1);
            for (int i = 0; i < seqList.GetLength(); i++)
            {
                Console.Write(seqList[i] + "  ");
            }
            Console.WriteLine();


            seqList.Delete(0);
            for (int i = 0; i < seqList.GetLength(); i++)
            {
                Console.Write(seqList[i] + "  ");
            }
            Console.WriteLine();

            seqList.Clear();
            Console.WriteLine(seqList.GetLength());

            Console.ReadKey();
        }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值