Exemple #1
0
        public void DeleteAt(T key)
        {
            if (this.Head == null)
            {
                return;
            }
            //handle head deletions.
            else if (Head.Data.CompareTo(key) == 0)
            {
                Head = Head.Next;
            }
            else
            {
                //handle deletions from middle.
                NodeSLL <T> tmp = Head;
                while (tmp.Next != null && tmp.Next.Data.CompareTo(key) != 0)
                {
                    tmp = tmp.Next;
                }

                //handle miss
                if (tmp.Next == null)
                {
                    return;
                }

                tmp.Next = tmp.Next.Next;
            }
        }
Exemple #2
0
        public void AddBefore(T key, T input)
        {
            if (this.Head == null)
            {
                return;
            }
            //handle insert before head
            if (Head.Data.CompareTo(key) == 0)
            {
                this.AddFirst(input);
            }
            else
            // handle insertions after head
            {
                NodeSLL <T> tmp = Head;
                while (tmp.Next != null && tmp.Next.Data.CompareTo(key) != 0)
                {
                    tmp = tmp.Next;
                }

                //handle miss
                if (tmp.Next == null && tmp.Data.CompareTo(key) != 0)
                {
                    return;
                }

                //do insert
                tmp.Next = new NodeSLL <T>(input, tmp.Next);
            }
        }
Exemple #3
0
        public void AddAfter(T key, T input)
        {
            NodeSLL <T> tmp = Head;

            while (tmp.Data.CompareTo(key) != 0 && tmp.Next != null)
            {
                tmp = tmp.Next;
            }
            if (tmp != null && tmp.Data.CompareTo(key) == 0)
            {
                tmp.Next = new NodeSLL <T>(input, tmp.Next);
            }
        }
Exemple #4
0
 public T GetLast()
 {
     try
     {
         NodeSLL <T> tmp = Head;
         while (tmp.Next != null)
         {
             tmp = tmp.Next;
         }
         return(tmp.Data);
     } catch
     {
         throw new ArgumentNullException();
     }
 }
Exemple #5
0
        public void Reverse()
        {
            NodeSLL <T> curr    = Head;
            NodeSLL <T> prev    = null;
            NodeSLL <T> currPtr = null;

            while (curr != null)
            {
                currPtr   = curr.Next;
                curr.Next = prev;
                prev      = curr;
                curr      = currPtr;
            }
            this.Head = prev;
        }
Exemple #6
0
        public int Count()
        {
            if (this.Head == null)
            {
                return(0);
            }
            int         ctr = 1;
            NodeSLL <T> tmp = this.Head;

            while (tmp.Next != null)
            {
                tmp = tmp.Next;
                ctr++;
            }
            return(ctr);
        }
Exemple #7
0
 public void AddFirst(T data)
 {
     Head = new NodeSLL <T>(data, this.Head);
 }
Exemple #8
0
 public NodeSLL(T data, NodeSLL <T> next)
 {
     this.Data = data;
     this.Next = next;
 }
Exemple #9
0
 public MyLinkedList()
 {
     Head = null;
 }