Пример #1
0
        public DoublyLinkedList(T initialValue)
        {
            DoublyNode <T> initialNode = new DoublyNode <T>
            {
                Value    = initialValue,
                Next     = null,
                Previous = null
            };

            Head = initialNode;
            Tail = initialNode;
        }
        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);
        }
        //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);
        }
        public void PrintListForwards()
        {
            Console.WriteLine("---------------------------------------------->");
            DoublyNode <T> temp = Head;

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

                if (temp == Head)
                {
                    break;
                }
            }
            Console.WriteLine("<----------------------------------------------");
        }
        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;
        }
 //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++;
 }
        // 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;
        }
        //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++;
        }
        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);
        }
        //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++;
        }
        // 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;
 }
 public DoublyLinkedLIists()
 {
     Count = 0;
     Head  = null;
     Tail  = null;
 }