Example #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;
            }
        }
Example #2
0
        public virtual void Reverse()
        {
            SingleLinkedNode <T> p = head.Next, q = null, front = null;

            while (p != null)
            {
                q      = p.Next;
                p.Next = front;
                front  = p;
                p      = q;
            }
            head.Next = front;
        }
Example #3
0
        public override string ToString()
        {
            SingleLinkedNode <T> q = head.Next;

            if (q != null)
            {
                return(q.ToString());
            }
            else
            {
                return(null);
            }
        }
Example #4
0
        public virtual void Show(bool showTypeName)
        {
            if (showTypeName)
            {
                Console.Write("SingleLinkedList: ");
            }
            SingleLinkedNode <T> q = head.Next;

            if (q != null)
            {
                q.Show();
            }
        }
Example #5
0
        public void Remove(T k)
        {
            SingleLinkedNode <T> p = head;
            SingleLinkedNode <T> q = p.Next;

            while (q != null)
            {
                if (k.Equals(q.Item))
                {
                    p.Next = q.Next;
                    return;
                }
                p = q;
                q = p.Next;
            }
            Console.WriteLine(k + " is not found,you can't remove it!");
        }
Example #6
0
        public void Show()
        {
            SingleLinkedNode <T> p = this;

            while (p != null)
            {
                Console.Write(p.Item);
                p = p.Next;
                if (p != null)
                {
                    Console.Write("-> ");
                }
                else
                {
                    Console.WriteLine(".");
                }
            }
        }
Example #7
0
        public void RemoveAt(int i)
        {
            int j = 0;
            SingleLinkedNode <T> p = head;
            SingleLinkedNode <T> q = p.Next;

            while (q != null)
            {
                if (j == i)
                {
                    p.Next = q.Next;
                    return;
                }
                p = q;
                q = q.Next;
                j++;
            }
            throw new IndexOutOfRangeException("Index Out Of Range in " + this.GetType());
        }
Example #8
0
        public override string ToString()
        {
            StringBuilder        s = new StringBuilder();
            SingleLinkedNode <T> p = this;

            while (p != null)
            {
                s.Append(p.Item);
                p = p.Next;
                if (p != null)
                {
                    s.Append("-> ");
                }
                else
                {
                    s.Append(".");
                }
            }
            return(s.ToString());
        }
Example #9
0
 public virtual T this[int i] {
     get{
         if (i < 0)
         {
             throw new IndexOutOfRangeException("Index is negative in " + this.GetType());
         }
         int n = 0;
         SingleLinkedNode <T> q = head.Next;
         while (q != null && i != n)
         {
             n++;
             q = q.Next;
         }
         if (q == null)
         {
             throw new IndexOutOfRangeException("Index Out Of Range Exception in " + this.GetType());
         }
         return(q.Item);
     }
     set{
         if (i < 0)
         {
             throw new IndexOutOfRangeException("Index is negative in " + this.GetType());
         }
         int n = 0;
         SingleLinkedNode <T> q = head.Next;
         while (q != null && i != n)
         {
             n++;
             q = q.Next;
         }
         if (q == null)
         {
             throw new IndexOutOfRangeException("Index Out of Range Exception in " + this.GetType());
         }
         q.Item = value;
     }
 }
Example #10
0
 public SingleLinkedList()
 {
     head = new SingleLinkedNode <T>();
 }
Example #11
0
 public SingleLinkedNode()
 {
     next = null;
 }
Example #12
0
 public SingleLinkedNode(T k)
 {
     item = k;
     next = null;
 }
Example #13
0
 public SingleLinkedList(SingleLinkedNode <T> f) : this()
 {
     head.Next = f;
 }