示例#1
0
        IEnumerator <T> IEnumerable <T> .GetEnumerator()
        {
            DoubleNode <T> current = _head;

            while (current != null)
            {
                yield return(current.Data);

                current = current.Next;
            }
        }
示例#2
0
        public IEnumerable <T> BackEnumerator()
        {
            DoubleNode <T> current = _tail;

            while (current != null)
            {
                yield return(current.Data);

                current = current.Previous;
            }
        }
        public bool InsertAtHead(string n)
        {
            DoubleNode node = new DoubleNode();
            node.Name = n;

            node.next = head.next;
            node.next.prev = node;
            node.prev = head;
            head.next = node;
            count++;
            return true;
        }
示例#4
0
 public DoublyLinkedList()
 {
     head      = new DoubleNode();
     tail      = new DoubleNode();
     head.Name = "Head";
     head.prev = null;
     head.next = tail;
     tail.prev = head;
     tail.Name = "Tail";
     tail.next = null;
     count     = 0;
 }
 public DoublyLinkedList()
 {
     head = new DoubleNode();
     tail = new DoubleNode();
     head.Name = "Head";
     head.prev = null;
     head.next = tail;
     tail.prev = head;
     tail.Name = "Tail";
     tail.next = null;
     count = 0;
 }
示例#6
0
        public bool InsertAtHead(string n)
        {
            DoubleNode node = new DoubleNode();

            node.Name = n;

            node.next      = head.next;
            node.next.prev = node;
            node.prev      = head;
            head.next      = node;
            count++;
            return(true);
        }
示例#7
0
        public bool InsertAtTail(string n)
        {
            DoubleNode node = new DoubleNode();

            node.Name = n;

            node.next      = tail;
            node.prev      = tail.prev;
            node.prev.next = node;
            tail.prev      = node;
            count++;
            return(true);
        }
示例#8
0
 public void PrintFromHead()
 {
     if (head.next != null && head.next != tail)
     {
         DoubleNode cur = head.next;
         Console.WriteLine(cur.Name);
         while (cur.next != tail)
         {
             cur = cur.next;
             Console.WriteLine(cur.Name);
         }
         cur = null;
     }
 }
示例#9
0
 public void PrintFromTail()
 {
     if (tail.prev != null && tail.prev != head)
     {
         DoubleNode cur = tail.prev;
         Console.WriteLine(cur.Name);
         while (cur.prev != head)
         {
             cur = cur.prev;
             Console.WriteLine(cur.Name);
         }
         cur = null;
     }
 }
示例#10
0
        public bool Contains(T data)
        {
            DoubleNode <T> current = _head;

            while (current != null)
            {
                if (current.Data.Equals(data))
                {
                    return(true);
                }
                current = current.Next;
            }

            return(false);
        }
示例#11
0
        public void Add(T data)
        {
            DoubleNode <T> node = new DoubleNode <T>(data);

            if (_head == null)
            {
                _head = node;
            }
            else
            {
                _tail.Next    = node;
                node.Previous = _tail;
            }

            _tail = node;
            _count++;
        }
示例#12
0
        public void AddFirst(T data)
        {
            DoubleNode <T> node = new DoubleNode <T>(data);
            DoubleNode <T> temp = _head;

            node.Next = temp;
            _head     = node;

            if (_count == 0)
            {
                _tail = _head;
            }
            else
            {
                temp.Previous = node;
            }

            _count++;
        }
示例#13
0
        public bool RemoveNode(string n)
        {
            DoubleNode cur = head;

            while (cur != tail)
            {
                if (cur.Name == n)
                {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                    cur           = null;
                    count--;
                    return(true);
                }
                else
                {
                    cur = cur.next;
                }
            }
            cur = null;
            return(false);
        }
 public bool InsertAtPosition(int pos, string n)
 {
     if (pos <= count)
     {
         DoubleNode cur = head;
         DoubleNode newNode = new DoubleNode();
         newNode.Name = n;
         while (pos >= 0 && cur != tail)
         {
             cur = cur.next;
             pos--;
         }
         newNode.next = cur;
         newNode.prev = cur.prev;
         cur.prev.next = newNode;
         count++;
     }
     else
     {
         return false;
     }
     return true;
 }
示例#15
0
 public bool InsertAtPosition(int pos, string n)
 {
     if (pos <= count)
     {
         DoubleNode cur     = head;
         DoubleNode newNode = new DoubleNode();
         newNode.Name = n;
         while (pos >= 0 && cur != tail)
         {
             cur = cur.next;
             pos--;
         }
         newNode.next  = cur;
         newNode.prev  = cur.prev;
         cur.prev.next = newNode;
         count++;
     }
     else
     {
         return(false);
     }
     return(true);
 }
示例#16
0
        public bool Remove(T data)
        {
            DoubleNode <T> current = _head;

            while (current != null)
            {
                if (current.Data.Equals(data))
                {
                    break;
                }
                current = current.Next;
            }

            if (current != null)
            {
                if (current.Next != null)
                {
                    current.Next.Previous = current.Previous;
                }
                else
                {
                    _tail = current.Previous;
                }

                if (current.Previous != null)
                {
                    current.Previous.Next = current.Next;
                }
                else
                {
                    _head = current.Next;
                }
                _count--;
                return(true);
            }
            return(false);
        }
        public bool InsertAfter(string ToFind, string n)
        {
            DoubleNode cur = head;

            while (cur != tail)
            {
                if (cur.Name == ToFind)
                {
                    DoubleNode newNode = new DoubleNode();
                    newNode.Name = n;
                    newNode.next = cur.next;
                    newNode.prev = cur;
                    cur.next = newNode;
                    count++;
                    return true;
                }
                else
                {
                    cur = cur.next;
                }
            }
            cur = null;
            return false;
        }
示例#18
0
        public bool InsertAfter(string ToFind, string n)
        {
            DoubleNode cur = head;

            while (cur != tail)
            {
                if (cur.Name == ToFind)
                {
                    DoubleNode newNode = new DoubleNode();
                    newNode.Name = n;
                    newNode.next = cur.next;
                    newNode.prev = cur;
                    cur.next     = newNode;
                    count++;
                    return(true);
                }
                else
                {
                    cur = cur.next;
                }
            }
            cur = null;
            return(false);
        }
        public bool InsertAtTail(string n)
        {
            DoubleNode node = new DoubleNode();
            node.Name = n;

            node.next = tail;
            node.prev = tail.prev;
            node.prev.next = node;
            tail.prev = node;
            count++;
            return true;
        }
示例#20
0
 public void Clear()
 {
     _head  = null;
     _tail  = null;
     _count = 0;
 }