Ejemplo n.º 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;
            }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 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);
            }
        }
Ejemplo n.º 4
0
 public T GetLast()
 {
     try
     {
         NodeSLL <T> tmp = Head;
         while (tmp.Next != null)
         {
             tmp = tmp.Next;
         }
         return(tmp.Data);
     } catch
     {
         throw new ArgumentNullException();
     }
 }
Ejemplo n.º 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;
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 7
0
 public void AddFirst(T data)
 {
     Head = new NodeSLL <T>(data, this.Head);
 }
Ejemplo n.º 8
0
 public NodeSLL(T data, NodeSLL <T> next)
 {
     this.Data = data;
     this.Next = next;
 }
Ejemplo n.º 9
0
 public MyLinkedList()
 {
     Head = null;
 }