Unity - A计划(一年有效期) 扫二维码继续学习 二维码时效为半小时

(61评价)
价格: 2208.00元
C#数据结构_单链表_节点赋值异常
homodeus发起了问答2018-12-25
3
回复
374
浏览

在创建单链表时,使用

Node<T> temp = head;

for (int i = 0; i < index; i++){

temp = temp.Next;

}时,

其中的index值应该就是temp的index,但实际运行测代码时,temp的index为index-1,导致报错,老师视频中就是对的,以下涉及index的方法都有返回null的异常,有的把index-1就正常了(但temp的索引的确是index,我理解单链表错了吗),有的不行,不知道是哪里出问题了

以下源码:

接口:

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 Locate(T value);

    }

Node类:

class Node<T>
    {
        private T date;
        private Node<T> next;

        public Node()
        {
            date = default(T);
        }

        public Node(T value)
        {
            this.date = value;
            this.next = null;
        }

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

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

        public T Date
        {
            get { return date; }
            set { date = value; }
        }

        public Node<T> Next
        {
            get { return next; }
            set { next = value; }
        }
    }

单链表:

class LinkList<T> : IListDS<T>
    {
        private Node<T> head;

        public LinkList()
        {
            head = null;
        }

        public T this[int index]
        {
            get
            {
                T date = default(T);
                Node<T> temp = head;
                for (int i = 0; i <= index; i++)
                {
                    temp = temp.Next;
                }
                date = temp.Date;
                return date;
            }
        }

        public void Add(T item)
        {
            Node<T> newNode = new Node<T>(item);
            if (head == null)
            {
                head = newNode;
            }
            else
            {
                Node<T> temp = head;
                while (true)
                {
                    if (temp.Next != null)
                    {
                        temp = temp.Next;
                    }
                    else
                    {
                        break;
                    }
                }

                temp.Next = newNode;
            }
        }

        public void Clear()
        {
            head = null;
        }

        public T Delete(int index)
        {
            T date = default(T);
            if (index == 0)
            {
                date = head.Date;
                head = head.Next;
            }
            else
            {
                Node<T> temp = head;
                for (int i = 0; i < index; i++)
                {
                    temp = temp.Next;
                }

                Node<T> preNode = temp;
                Node<T> currentNode = temp.Next;
                date = currentNode.Date;
                Node<T> nextNode = temp.Next.Next;
                if (nextNode == null)
                {
                    preNode.Next = null;
                }
                else
                {
                    preNode.Next = nextNode;
                }
               
            }

            return date;
        }

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

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

            return count;
        }

        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 = head;
                for (int i = 0; i < index; i++)
                {
                    temp = temp.Next;
                }

                Node<T> preNode = temp;
                Node<T> nextNode = temp.Next;
                preNode.Next = newNode;
                newNode.Next = nextNode;
            }
        }

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

        public int Locate(T value)
        {
            int index = 0;
            if (head == null)
            {
                index = -1;
            }
            else
            {
                Node<T> temp = head;
                for (int i = 0; i < GetLength(); i++)
                {
                    temp = temp.Next;
                    if (temp.Date.Equals(value))
                    {
                        index = i;
                    }
                }
            }

            return index;
        }
    }

测试码:

class Program
    {
        static void Main(string[] args)
        {
            LinkList<int> list = new LinkList<int>();
            list.Add(12);
            list.Add(13);
            list.Add(14);
            Console.WriteLine(list[1]);
            Console.WriteLine(list.GetEle(1));
            list.Delete(2);
            list.Insert(15, 2);
            Console.WriteLine(list.GetLength());
            Console.WriteLine(list.IsEmpty());
            Console.WriteLine(list.Locate(15));
            for (int  i = 0;  i < list.GetLength();  i++)
            {
                Console.Write(list[i] + " ");
            }
            list.Clear();
            Console.WriteLine(list.GetLength());
            Console.ReadKey();

        }
    }

所有回复
  • 老师_Trigger 2018-12-25

    同学你好,可以下载一下老师的源码看一下,同学节点的构造方法有问题,删除方法也有问题。可以拿笔画一画理解修改一下。

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • homodeus 2018-12-25

    错误原因是for循环的起点定为了0,0处索引到的temp是head,所以i要从1开始

    还有一点不明白的地方

    public Node<T> Next
            {
                get { return next; }
                set { next = value; }
            }

    基于以上代码,temp = temp.Next中,temp接收到的值是temp.Next中return的next值吗?如果是,那是调用前的next还是调用后的?如果不是,那get里为什么要return next?

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
  • 老师_Trigger 2018-12-26

    是的,是传递进来的值,也就是执行temp=temp.Next这句之后,temp存贮的就是已经完成赋值之后的节点了。

    还有-5条回复,点击查看
    你还没有登录,请先登录注册
发表回复
你还没有登录,请先 登录或 注册!