单链表的实现

在单链表中,我们需要在内部有一个头节点,我们可以通过这个头节点找到其他的节点,相当于一个线索。

纵观顺序结构的线性表和单链表的实现,难点基本上都在于添加和删除操作。基于数组的线性表中,数组的索引就相当于是线性表的序号,但在单链表中,我们没有序号这个东西,所以在添加和删除操作中,我们需要先找到指定的元素,然后才能继续进行操作。在插入操作中,我们需要同时保存有当前节点的前一个节点和当前的节点,因为当前要插入的节点要放在前一个节点的Next引用域,而当前节点要放在要插入节点的next域。删除节点与此相似。

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace DataStructure

{

    class LinkList<T> : IListDS<T>

    {

 

        class Node<T>

        {

 

            private T data;

            /// <summary>

            /// 数据域

            /// </summary>

            public T Data

            {

                get { return data; }

                set { data = value; }

            }

 

 

            private Node<T> next;

            /// <summary>

            /// 引用域

            /// </summary>

            public Node<T> Next

            {

                get { return next; }

                set { next = value; }

            }

 

            //头结点构造函数

            public Node(Node<T> node)

                : this(default(T), node)

            {

            }

            //普通结点构造函数

            public Node(T data, Node<T> node)

            {

                this.Data = data;

                this.Next = node;

            }

            /// <summary>

            /// 空构造函数

            /// </summary>

            public Node()

                : this(default(T), null)

            {

            }

            //尾结点构造函数

            public Node(T data)

                : this(data, null)

            {

 

            }

        }

 

        private Node<T> Head;

     

        public LinkList()

        {

 

        }

 

        public int Lenth

        {

            get { return this.GetLength(); }

        }

       

        public LinkList(T[] t)

        {

            this.Head = new Node<T>(t[0]);

            Node<T> Last;

            Last = Head;

            for (int i = 1; i < t.Length; i++)

            {

                //尾插入法

                Last.Next = new Node<T>(t[i]);

                Last = Last.Next;

            }

        }

        public States Append(T item)

        {

            //(1)头结点没有

            if (Head==null)

            {

                Head = new Node<T>(item);

            }

            //(2)正常的插入情况

            Node<T> Last;

            Last=Head;

            //遍历到最后一个结点,在最后一个结点附加上

            while (null!=Last.Next)

            {

                Last = Last.Next;

            }

            Last.Next = new Node<T>(item);

            return States.Success;

        }

 

        public void Clear()

        {

            this.Head = null;

        }

 

        public T Delete(int index, out States states)

        {

            bool isIndexTrue = index < 0 || index >= this.GetLength();

            if (IsEmpty() == true || isIndexTrue)

            {

                states = States.Fail;

                return default(T);

            }

            //找到指定的结点

            Node<T> Current = Head;

            Node<T> Previous = Head;

            int Count = 0;

            while (Count != index && Current.Next != null)

            {

                Previous = Current;

                Current = Current.Next;

                Count++;

            }

            T ValueToDel = Current.Data;

            //是否是头结点

            if (Count == 0)

            {

                Head = Current.Next;

            }

            //是否是普通的结点

            if (Count != 0 && Current.Next != null)

            {

                Previous.Next = Current.Next;

            }

            //是否是最后一个结点

            if (Count != 0 && Current.Next == null)

            {

                Previous.Next = null;

            }

 

            //删除结点

            states = States.Success;

            return ValueToDel;

        }

 

        public T GetElem(int i)

        {

            if (i < 0 || i >= this.GetLength())

            {

                return default(T);

            }

            if (IsEmpty() == true)

            {

                return default(T);

            }

 

            Node<T> Last = Head;

            int Count = 0;

            while (Count != i && Last.Next != null)

            {

                Last = Last.Next;

                Count++;

            }

            return Last.Data;

        }

 

        public int GetLength()

        {

            if (Head==null)

            {

                return 0;

            }

            Node<T> Last;

            Last = Head;

            int Count=0;

            while (Last.Next!=null)

            {

                Last = Last.Next;

                Count++;

            }

            return ++Count;

            //throw new NotImplementedException();

        }

 

        public States Insert(T item, int index)

        {

            bool isIndexTrue = index < 0 || index > this.GetLength();

            if (isIndexTrue)

            {

               return  States.Fail;

               

            }

            //如果链表为空

            if (IsEmpty() == true )

            {

                Head.Data = item;

            }

            //如果是第一个结点

            if (index==0)

            {

                Node<T> node = new Node<T>(item);

                node.Next = Head;

                Head = node;

            }

            //如果是普通的结点

            Node<T> Previous = Head;

          //  Node<T> Previous = Head;

            int Count = 0;

            while (Count != index1 && Previous.Next != null)

            {

            //    Previous = Current;

                Previous = Previous.Next;

                Count++;

            }

            if (Count == index1)

            {

                Node<T> node=new Node<T>(item);

                node.Next = Previous.Next;

                Previous.Next = node;

            }

            //如果是最后一个结点

            if (this.GetLength()==index)

            {

                Previous.Next = new Node<T>(item);

            }

            return States.Success;

        }

 

        public bool IsEmpty()

        {

            return Head == null ? true : false;

        }

 

        public int Locate(T value, out States states)

        {

            if (IsEmpty() == true)

            {

                states = States.Fail;

                return 0;

            }

 

            Node<T> Last = Head;

            int Count = 0;

            while (Last.Next != null &&( !value.Equals(Last.Data)))

            {

                Last = Last.Next;

                Count++;

            }

            states = States.Success;

            return Count;

        }

    }

}

 

    原文作者:陈哈哈
    原文地址: https://www.cnblogs.com/kissazi2/p/3193957.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞