Пример #1
0
        public SingleLinkedList(T[] itemArray) : this()//使用一个数组创建线性表
        {
            SingleLinkedNode <T> rear, q;

            rear = head;//指向链表尾结点
            for (int i = 0; i < itemArray.Length; i++)
            {
                q         = new SingleLinkedNode <T>(itemArray[i]);//新建一个结点
                rear.Next = q;
                rear      = q;
            }
        }
Пример #2
0
        public bool Contain(T item)
        {
            bool isContain         = false;
            SingleLinkedNode <T> p = head;

            while ((p.Next != null) && (!isContain))//一旦找到就可以提前退出循环
            {
                isContain = item.Equals(p.Next.Item);
                p         = p.Next;
            }
            return(isContain);
        }
Пример #3
0
        public override string ToString()
        {
            StringBuilder        s = new StringBuilder();
            SingleLinkedNode <T> p = head;

            while (p.Next != null)
            {
                s.Append(p.Next);
                p = p.Next;
            }
            return(s.ToString());
        }
Пример #4
0
        public T[] ToArray()
        {
            T[] array = new T[Count];
            SingleLinkedNode <T> p = head;
            int i = 0;

            while (p.Next != null)
            {
                array[i] = p.Next.Item;
                p        = p.Next;
                i++;
            }
            return(array);
        }
Пример #5
0
        public void AddRange(T[] itemArray)
        {
            SingleLinkedNode <T> p = head;

            while (p.Next != null)
            {
                p = p.Next;
            }
            for (int i = 0; i < itemArray.Length; i++)
            {
                SingleLinkedNode <T> new_item = new SingleLinkedNode <T>(itemArray[i]);
                p.Next = new_item;
                p      = p.Next;
            }
        }
Пример #6
0
        public void Remove(T item)
        {
            SingleLinkedNode <T> p = head;
            SingleLinkedNode <T> q = head.Next;

            while (q.Next != null)
            {
                if (item.Equals(q.Item))
                {
                    p.Next = q.Next;
                    break;
                }
                p = p.Next;
                q = q.Next;
            }
        }
Пример #7
0
 public void RemoveAt(int index)
 {
     if ((index < 0) || (index >= Count))
     {
         throw new IndexOutOfRangeException("Index out of Range!!!");
     }
     else
     {
         SingleLinkedNode <T> p = head;
         SingleLinkedNode <T> q = head.Next;
         for (int i = 0; i < index; i++)
         {
             p = p.Next;
             q = q.Next;
         }
         p.Next = q.Next;
     }
 }
Пример #8
0
        public int IndexOf(T item)
        {
            int index = 0;
            SingleLinkedNode <T> p = head;

            while (p.Next != null)
            {
                if (item.Equals(p.Next.Item))//一旦找到匹配的就立刻退出函数
                {
                    return(index);
                }
                else
                {
                    index++;
                    p = p.Next;
                }
            }
            return(-1);
        }
Пример #9
0
 //索引器
 public virtual T this[int i]
 {
     get
     {
         if ((i < 0) || (i >= Count))
         {
             throw new IndexOutOfRangeException("Index is out of range " + this.GetType());
         }
         else
         {
             SingleLinkedNode <T> p = head.Next;
             while (i > 0)
             {
                 p = p.Next;
                 i--;
             }
             return(p.Item);
         }
     }
 }
Пример #10
0
 public void Insert(int index, T item)
 {
     if ((index < 0) || (index >= Count))
     {
         throw new IndexOutOfRangeException("Index is out of range " + this.GetType());
     }
     else
     {
         SingleLinkedNode <T> p        = head;
         SingleLinkedNode <T> q        = head.Next;
         SingleLinkedNode <T> new_item = new SingleLinkedNode <T>(item);
         for (int i = 0; i < index; i++)
         {
             p = p.Next;
             q = q.Next;
         }
         p.Next        = new_item;
         new_item.Next = q;
     }
 }
Пример #11
0
 public void RemoveRange(int index, int cnt)
 {
     if ((index < 0) || (index >= Count))
     {
         throw new IndexOutOfRangeException("Index out of Range!!!");
     }
     else
     {
         SingleLinkedNode <T> p = head;
         SingleLinkedNode <T> q;
         //定位到待删除范围最前面的元素的前一个元素
         for (int i = 0; i < index; i++)
         {
             p = p.Next;
         }
         q = p;
         //定位到待删除范围最后面的元素的后一个元素
         for (int i = 0; i < cnt; i++)
         {
             q = q.Next;
         }
         p.Next = q.Next;
     }
 }
Пример #12
0
 public SingleLinkedList(SingleLinkedNode <T> f) : this()//使用this()告诉电脑调用无参数构造函数
 {
     head.Next = f;
 }
Пример #13
0
 public SingleLinkedList()
 {
     head = new SingleLinkedNode <T>();
 }
Пример #14
0
 public SingleLinkedNode()
 {
     item = default(T);
     next = null;
 }
Пример #15
0
        private SingleLinkedNode <T> next; //指针域

        public SingleLinkedNode(T k)
        {
            item = k;
            next = null;
        }