Exemple #1
0
        /*在单链表的第i个位置的后面插入一个元素*/
        public void insertPost(T item, int i)
        {
            if (isEmpty() || i < 1)
            {
                Console.WriteLine("链表为空或者位置错误");
                return;
            }
            int j = 1;
            Code3_6_Node <T> p = head;

            //遍历寻找第i个节点,判断条件中,p最多是第i-1个节点。循环体中,p最多是第i个节点
            while (p != null && j < i)
            {
                p = p.Next;
                ++j;
            }
            //???这里的j>i判断有没有意义
            //判断条件中,p最多是第i个节点
            if (p == null || j > i)
            {
                System.Console.WriteLine("第{0}个节点不存在", i);
                return;
            }
            Code3_6_Node <T> q = new Code3_6_Node <T>(item);

            q.Next = p.Next;
            p.Next = q;
            return;
        }
Exemple #2
0
        /*在单链表中查找值为value的元素的节点(的位置)*/
        public int locate(T value)
        {
            if (isEmpty())
            {
                Console.WriteLine("链表为空或者位置错误");
                return(-1);
            }
            Code3_6_Node <T> p = new Code3_6_Node <T>();

            p = head;
            int i = 1;

            //这里发现问题,原代码条件中是p.Next!=null,出现的问题是,认为不存在的结点位于最后一个位置(逻辑错误)
            //(解决方案),条件中应为p!=null
            while (p != null && !p.Data.Equals(value))
            {
                p = p.Next;
                i++;
            }
            if (p == null)
            {
                Console.WriteLine("不存在这样的节点");
                return(-1);
            }
            else
            {
                return(i);
            }
        }
Exemple #3
0
        public T delete(int i)
        {
            if (isEmpty() || i < 1)
            {
                Console.WriteLine("链表为空,或者位置错误");
                return(default(T));
            }
            int j = 1;
            Code3_6_Node <T> p = head;

            //遍历寻找第i-1个节点,判断条件中,最后一次判断条件成立时,p.Next最多是第i-2个节点。循环体,最后一次判断条件成立时,p最多是
            //第i-1个节点
            while (p.Next != null && j < i - 1)
            {
                p = p.Next;
                ++j;
            }
            //???这个j>=i的判断有没有意义
            //判断条件中,p.Next最多是第i个节点
            if (p.Next == null || j > i)
            {
                System.Console.WriteLine("第{0}个节点不存在", i);
                return(default(T));
            }
            Code3_6_Node <T> q = new Code3_6_Node <T>();

            //???这里本应该释放掉q(第i个结点)的,但现在没有释放,现在第i+1个结点有两个结点指向它,一个是原来的第i-1个节点,一个是要被删掉的第i个节点
            //第i个结点没有人指向它了。不知道系统会不会回收原来的第i个结点
            q      = p.Next;
            p.Next = q.Next;
            return(q.Data);
        }
Exemple #4
0
        /*获取单链表长度*/
        public int getLength()
        {
            Code3_6_Node <T> p = head;
            int len            = 0;

            while (p != null)
            {
                p = p.Next;
                len++;
            }
            return(len);
        }
Exemple #5
0
        void bll(Code3_6_LL <int> ll)
        {
            //遍历链表
            Code3_6_Node <int> p = ll.Head;
            int i = 1;

            while (p != null)
            {
                System.Console.WriteLine("链表第{0}个结点为:{1}", i, p.Data);
                p = p.Next;
                i++;
            }
        }
Exemple #6
0
        /*在单链表的末尾添加新的元素*/
        /*思路是从头遍历到尾,然后将元素添到最后*/
        public void append(T item)
        {
            Code3_6_Node <T> q = new Code3_6_Node <T>(item);
            Code3_6_Node <T> p = new Code3_6_Node <T>();

            if (head == null)
            {
                head = q;
                return;
            }
            p = head;
            while (p.Next != null)
            {
                p = p.Next;
            }
            p.Next = q;
        }
Exemple #7
0
        /// <summary>
        /// create List by Tail,尾插法创建一个链表
        /// </summary>
        /// <param name="ll"></param>
        /// <param name="n"></param>
        void cLT(Code3_6_LL <int> ll, int n)
        {
            Random rd = new Random();

            ll.Head = new Code3_6_Node <int>(rd.Next(0, 100));
            Code3_6_Node <int> p = ll.Head;

            int i;

            for (i = 0; i < n - 1; i++)
            {
                Code3_6_Node <int> q = new Code3_6_Node <int>(rd.Next(0, 100));
                q.Next = p.Next;
                p.Next = q;
                p      = q;
            }
        }
Exemple #8
0
        /*在第i个位置前面插入一个元素节点,即数据域为item的节点*/
        public void insert(T item, int i)
        {
            if (isEmpty() || i < 1)
            {
                Console.WriteLine("链表为空或者位置错误");
                return;
            }
            Code3_6_Node <T> q = new Code3_6_Node <T>(item);

            if (i == 1)
            {
                q.Next = head;
                head   = q;
                return;
            }
            int j = 1;
            Code3_6_Node <T> p = head;

            //遍历寻找第i-1个节点,判断条件中,最后一次判断条件成立时,p.Next最多是第i-2个节点。循环体重,最后一次判断条件成立时,p最多是
            //第i-1个节点
            while (p.Next != null && j < i - 1)
            {
                p = p.Next;
                ++j;
            }
            //???这个j>=i的判断有没有意义
            //判断条件中,p.Next最多是第i个节点
            if (p.Next == null || j > i)
            {
                System.Console.WriteLine("第{0}个节点不存在", i);
                return;
            }
            q.Next = p.Next;
            p.Next = q;
            return;
        }
Exemple #9
0
        /*获取单链表的第i个元素*/
        public T getItem(int i)
        {
            if (isEmpty() || i < 1)
            {
                Console.WriteLine("链表为空或者位置错误");
                return(default(T));
            }
            Code3_6_Node <T> p = new Code3_6_Node <T>();

            p = head;
            int j = 1;

            while (p != null && j < i)
            {
                p = p.Next;
                ++j;
            }
            if (p == null || j > i)
            {
                System.Console.WriteLine("第{0}个节点不存在", i);
                return(default(T));
            }
            return(p.Data);
        }
Exemple #10
0
 /*清空单链表*/
 //在清空表的时候,dhsjjg中循环遍历节点并将其释放掉
 //!!!后续通过循环遍历手动释放结点
 public void clear()
 {
     head = null;
 }
Exemple #11
0
 /*构造器*/
 public Code3_6_LL()
 {
     head = null;
 }
Exemple #12
0
 public Code3_6_Node()
 {
     data = default(T);
     next = null;
 }
Exemple #13
0
 /*构造器*/
 public Code3_6_Node(Code3_6_Node <T> p)
 {
     next = p;
 }
Exemple #14
0
 /*构造器*/
 public Code3_6_Node(T val, Code3_6_Node <T> p)
 {
     data = val;
     next = p;
 }