コード例 #1
0
        public DoublyLinkedList(T initialValue)
        {
            DoublyNode <T> initialNode = new DoublyNode <T>
            {
                Value    = initialValue,
                Next     = null,
                Previous = null
            };

            Head = initialNode;
            Tail = initialNode;
        }
コード例 #2
0
        public DoublyNode <T> Find(T val)
        {
            DoublyNode <T> pos = Head;

            while (pos != null)
            {
                if (pos.Value.Equals(val))
                {
                    return(pos);
                }
                pos = pos.Next;
            }
            return(null);
        }
コード例 #3
0
        //removefirst
        public bool RemoveFirst()
        {
            if (Count == 0)
            {
                return(false);
            }
            DoublyNode <T> temp = Head;

            Head      = Head.Next;
            temp.Next = null;
            Head.Prev = Tail;
            Tail.Next = Head;
            Count--;
            return(true);
        }
コード例 #4
0
        public void PrintListForwards()
        {
            Console.WriteLine("---------------------------------------------->");
            DoublyNode <T> temp = Head;

            while (true)
            {
                Console.WriteLine(temp.Value);
                temp = temp.Next;

                if (temp == Head)
                {
                    break;
                }
            }
            Console.WriteLine("<----------------------------------------------");
        }
コード例 #5
0
        public void PrintListBackwards()
        {
            Console.WriteLine("---------------------------------------------->");

            DoublyNode <T> temp = Tail;

            while (true)
            {
                Console.WriteLine(temp.Value);
                temp = temp.Prev;

                if (temp == Tail)
                {
                    break;
                }
            }
            Console.WriteLine("<----------------------------------------------");
        }
コード例 #6
0
        public void InsertBegin <U>(U value) where U : T
        {
            DoublyNode <T> newNode = new DoublyNode <T>()
            {
                Value = value,
            };

            if (Head == null)
            {
                Head             = newNode;
                Tail             = newNode;
                newNode.Previous = null;
                newNode.Next     = null;
            }
            else
            {
                InsertBefore(Head, value);
            }
        }
コード例 #7
0
        public void InsertAfter(DoublyNode <T> node, T value)
        {
            DoublyNode <T> newNode = new DoublyNode <T>()
            {
                Value    = value,
                Previous = node
            };

            if (node.Next == null)
            {
                Tail = newNode;
            }
            else
            {
                newNode.Next       = node.Next;
                node.Next.Previous = newNode;
            }

            node.Next = newNode;
        }
コード例 #8
0
 //addlast
 public void AddLast(T val)
 {
     if (Head == null)
     {
         Head      = new DoublyNode <T>(val);
         Tail      = Head;
         Head.Prev = Tail;
         Tail.Next = Head;
     }
     else
     {
         var temp = new DoublyNode <T>(val);
         Tail.Next = temp;
         temp.Prev = Tail;
         Tail      = temp;
         Tail.Next = Head;
         Head.Prev = Tail;
     }
     Count++;
 }
コード例 #9
0
        // addfirst

        public void AddFirst(T val)
        {
            if (Head == null)
            {
                Head      = new DoublyNode <T>(val);
                Tail      = Head;
                Tail.Next = Head;
                Head.Prev = Tail;
            }
            else
            {
                DoublyNode <T> temp = new DoublyNode <T>(val);
                temp.Next = Head;
                Head.Prev = temp;
                Head      = temp;
                Head.Prev = Tail;
                Tail.Next = Head;
            }
            Count++;
        }
コード例 #10
0
        public void InsertBefore(DoublyNode <T> node, T value)
        {
            DoublyNode <T> newNode = new DoublyNode <T>()
            {
                Value = value,
                Next  = node
            };

            if (node.Previous == null)
            {
                newNode.Previous = null;
                Head             = newNode;
            }
            else
            {
                newNode.Next       = node;
                node.Previous.Next = newNode;
            }

            node.Previous = newNode;
        }
コード例 #11
0
        //addafter
        public void AddAfter(DoublyNode <T> Node, T val)
        {
            if (Node == null)
            {
                AddLast(val);
                return;
            }
            else if (Node == Tail)
            {
                AddLast(val);
                return;
            }


            DoublyNode <T> temp = new DoublyNode <T>(val);

            temp.Prev      = Node;
            temp.Next      = Node.Next;
            Node.Next.Prev = temp;
            Node.Next      = temp;
            Count++;
        }
コード例 #12
0
        public bool RemoveLast()
        {
            if (Head == null)
            {
                return(false);
            }
            else if (Head == Tail)
            {
                Head = null;
                Tail = Head;
                return(true);
            }

            DoublyNode <T> temp = Tail.Prev;

            temp.Next = null;
            Tail      = temp;
            Tail.Next = Head;
            Head.Prev = Tail;
            Count--;
            return(true);
        }
コード例 #13
0
        //addbefore
        public void AddBefore(DoublyNode <T> Node, T val)
        {
            if (Node == null)
            {
                AddLast(val);
                return;
            }
            else if (Node == Head)
            {
                AddFirst(val);
                return;
            }


            DoublyNode <T> temp = new DoublyNode <T>(val);

            var nodeBefore = Node.Prev;

            nodeBefore.Next = temp;
            temp.Next       = Node;
            temp.Prev       = nodeBefore;
            Node.Prev       = temp;
            Count++;
        }
コード例 #14
0
        // remove
        public bool Remove(T val)
        {
            if (val.Equals(Head.Value))
            {
                RemoveFirst();
                return(true);
            }
            else if (val.Equals(Tail.Value))
            {
                RemoveLast();
                return(true);
            }
            else if (val == null)
            {
                throw new ArgumentException();
            }

            DoublyNode <T> item = Find(val);

            item.Prev.Next = item.Next;
            item.Next.Prev = item.Prev;
            Count--;
            return(true);
        }
コード例 #15
0
        /// <summary>
        /// Return whether or not the queue contains a certain value somewhere in the queue
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool Contains(T value)
        {
            if (list.Head == null)
            {
                return(false);
            }
            DoublyNode <T> currentNode = list.Head;

            if (list.Head.Value.Equals(value))
            {
                return(true);
            }
            while (currentNode.Next != null)
            {
                if (currentNode.Next.Value.Equals(value))
                {
                    return(true);
                }

                currentNode = currentNode.Next;
            }

            return(false);
        }
コード例 #16
0
 public DoublyNode(T val)
 {
     Value = val;
     Next  = null;
 }
コード例 #17
0
 public DoublyLinkedLIists()
 {
     Count = 0;
     Head  = null;
     Tail  = null;
 }