Ejemplo n.º 1
0
 //Add Last
 public void AddLast(object value)
 {
     LinkedListNode newNode = new LinkedListNode()
     {
         Value = value
     };
     //Check if empty
     if (Head == null)
     {
         Head = newNode;
         Tail = newNode;
     }
     else
     {
         Tail.Next = newNode;
         Tail = newNode;
     }
 }
Ejemplo n.º 2
0
 //Add in Begining
 public void AddFront(object value)
 {
     LinkedListNode newNode = new LinkedListNode()
     {
         Value = value,
         Next = null
     };
     //Check if empty
     if (Head == null)
     {
         Head = newNode;
         Tail = newNode;
     }
     else
     {
         newNode.Next = Head;
         Head = newNode;
     }
 }
Ejemplo n.º 3
0
 //Delete First
 public void DeleteFirst()
 {
     if (Head != null)
     {
         if (Head == Tail)
         {
             Head = null;
             Tail = null;
         }
         else
         {
             Head = Head.Next;
         }
     }
     else
     {
         Console.WriteLine("List is empty.");
         throw new Exception("List is Empty.");
     }
 }
Ejemplo n.º 4
0
 private void PrintNextValue(LinkedListNode node)
 {
     if (node.Next == null)
     {
         Console.WriteLine(node.Value);
     }
     else
     {
         PrintNextValue(node.Next);
         Console.WriteLine(node.Value);
     }
 }
Ejemplo n.º 5
0
        //Insert in Middle
        public void InsertAt(int index, object value)
        {
            if (Head != null)
            {
                var current = Head;
                for (int i = 1; i < index-1;i++)
                {
                    if (current.Next == null)
                    {
                        //Raise linked list excpetion.
                        Console.WriteLine("Invalid Index");
                        throw new Exception("Invalid Index");
                    }
                    current = current.Next;
                }

                LinkedListNode newNode = new LinkedListNode()
                {
                    Value = value
                };
                if (current == Tail)
                {
                    current.Next = newNode;
                    Tail = newNode;
                }
                else
                {
                    newNode.Next = current.Next;
                    current.Next = newNode;
                }
            }
            else
            {
                Console.WriteLine("Invalid Index.List is empty");
                throw new Exception("Invalid Index");
            }
        }
Ejemplo n.º 6
0
        //Delete Middle
        public void DeleteMiddle(int index)
        {
            if (Head != null)
            {
                var current = Head;
                for (int i = 1; i < index-1 ; i++)
                {
                    if (current.Next == null)
                    {
                        //Raise linked list excpetion.
                        throw new Exception("Invalid Index");
                    }
                    current = current.Next;
                }

                if (current == Tail)
                {
                    throw new Exception("Invalid Index");
                }
                else
                {
                    current.Next = current.Next.Next;
                    if (current.Next == null)
                    {
                        Tail = current;
                    }
                }
            }
            else
            {
                throw new Exception("Invalid Index");
            }
        }
Ejemplo n.º 7
0
 //Delete Last
 public void DeleteLast()
 {
     if (Tail != null)
     {
         if (Head == Tail)
         {
             Head = null;
             Tail = null;
         }
         else
         {
             var current = Head;
             while (current.Next != Tail)
             {
                 current = current.Next;
             }
             Tail = current;
             current.Next = null;
         }
     }
     else
     {
         Console.WriteLine("List is empty.");
         throw new Exception("List is Empty.");
     }
 }