public void Add(int val)
        {
            DllNode newNode = new DllNode(val);

            if (Head == null)
            {
                Head = newNode;
                Tail = newNode;
                Size = 1;
            }
            else
            {
                int     count  = 1;
                DllNode runner = Head;
                while (runner.Next != null)
                {
                    runner = runner.Next;
                    count++;
                }
                runner.Next      = newNode;
                runner.Next.Prev = runner;
                Tail             = runner.Next;
                Size             = count + 1;
            }
        }
Beispiel #2
0
        public bool Remove(int value)
        {
            DllNode runner  = Head;
            int     counter = 0;

            if (Head.Value == value && runner.Next != null)
            {
                Head = runner.Next;
                Console.WriteLine($"{ value } was removed from index { counter }");
                return(true);
            }
            else if (Head.Value == value)
            {
                Head = null;
                Console.WriteLine($"{ value } was removed from index { counter }");
                return(true);
            }
            while (runner.Next.Value != value)
            {
                if (runner.Next == null)
                {
                    Console.WriteLine("That number does not exist in your list.");
                    return(false);
                }
                runner = runner.Next;
                counter++;
            }
            runner.Next = runner.Next.Next;
            Console.WriteLine($"{ value } was removed from index { counter + 1 }");
            return(true);
        }
        public void DisplayAll()
        {
            DllNode runner = Head;

            while (runner != null)
            {
                Console.WriteLine(runner.Value);
                runner = runner.Next;
            }
            Console.WriteLine($"DLL size: {Size}");
        }
Beispiel #4
0
        public void PrintValues()
        {
            DllNode runner = Head;
            DllNode prev   = Head;

            while (runner.Next != null)
            {
                Console.WriteLine(runner.Value);
                prev   = runner;
                runner = runner.Next;
            }
            Console.WriteLine(runner.Value);
        }
 public bool Remove(int val)
 {
     //list empty
     if (Head == null)
     {
         Console.WriteLine("List is empty!");
         return(false);
     }
     //list not empty
     else
     {
         //In Head
         if (Head.Value == val)
         {
             Head      = Head.Next;
             Head.Prev = null;
             Size--;
             return(true);
         }
         //In Tail
         else if (Tail.Value == val)
         {
             Tail      = Tail.Prev;
             Tail.Next = null;
             Size--;
             return(true);
         }
         else
         {
             DllNode runner = Head;
             while (runner != null)
             {
                 if (runner.Value == val)
                 {
                     runner.Prev.Next = runner.Next;
                     runner.Next.Prev = runner.Prev;
                     Size--;
                     return(true);
                 }
                 runner = runner.Next;
             }
             return(false);
         }
     }
 }
Beispiel #6
0
        public void Add(int value)
        {
            DllNode newNode = new DllNode(value);

            if (Head == null)
            {
                Head = newNode;
            }
            else
            {
                DllNode runner = Head;
                DllNode prev   = Head;
                while (runner.Next != null)
                {
                    prev   = runner;
                    runner = runner.Next;
                }
                runner.Next = newNode;
            }
        }
 public void Reverse()
 {
     if (Head == null)
     {
         Console.WriteLine("List is empty!");
     }
     else
     {
         DllNode runner = Head;
         DllNode temp   = null;
         while (runner != null)
         {
             temp        = runner.Prev;
             runner.Prev = runner.Next;
             runner.Next = temp;//remember its not temp.prev (because its already set to that)
             runner      = runner.Prev;
         }
         temp = Head;
         Head = Tail;
         Tail = temp;
     }
 }
Beispiel #8
0
 public DoublyLinked()
 {
     Head = null;
 }
 public DLList()
 {
     Head = null;
     Tail = null;
     Size = 0;
 }
Beispiel #10
0
 public DllNode(int Val)
 {
     Value     = Val;
     this.Next = null;
     this.Prev = null;
 }