Exemplo n.º 1
0
        public object RemoveBack()
        {
            if (Length == 0)
            {
                throw new Exception("List is empty");
            }

            object data = LastNode.Data;

            if (FirstNode == LastNode)
            {
                FirstNode = LastNode = null;
                Length--;
            }
            else
            {
                CustomNode curr = FirstNode;
                while (curr.Next != LastNode)
                {
                    curr = curr.Next;
                }

                LastNode      = curr;
                LastNode.Next = null;
                Length--;
            }
            return(data);
        }
Exemplo n.º 2
0
 public void InsertBack(object node)
 {
     if (Length == 0)
     {
         FirstNode = LastNode = new CustomNode(node);
         Length++;
     }
     else
     {
         LastNode = LastNode.Next = new CustomNode(node);
         Length++;
     }
 }
Exemplo n.º 3
0
 public void InsertFront(object node)
 {
     if (Length == 0)
     {
         FirstNode = LastNode = new CustomNode(node);
         Length++;
     }
     else
     {
         CustomNode prevFirstNode = FirstNode;
         FirstNode      = new CustomNode(node);
         FirstNode.Next = prevFirstNode;
         Length++;
     }
 }
Exemplo n.º 4
0
 public void Print()
 {
     if (Length == 0)
     {
         Console.WriteLine("List is empty");
     }
     else
     {
         CustomNode curr = FirstNode;
         while (curr != null)
         {
             Console.Write($" {curr.Data} |");
             curr = curr.Next;
         }
         Console.Write("\n");
     }
 }
Exemplo n.º 5
0
        public object RemoveFront()
        {
            if (Length == 0)
            {
                throw new Exception("List is empty");
            }

            object data = FirstNode.Data;

            if (FirstNode == LastNode)
            {
                FirstNode = LastNode = null;
                Length--;
            }
            else
            {
                FirstNode = FirstNode.Next;
                Length--;
            }
            return(data);
        }
Exemplo n.º 6
0
 public CustomLinkedList()
 {
     FirstNode = LastNode = null;
     Length    = 0;
 }