Exemplo n.º 1
0
        public override T Remove(T value)
        {
            Assertion.Pre(value != null);

            SinglyLinkedListElement <T> finger   = this.head;
            SinglyLinkedListElement <T> previous = null;

            while (finger != null && !value.Equals(finger.GetValue()))
            {
                previous = finger;
                finger   = finger.GetNext();
            }

            if (finger == null)
            {
                return(default(T));
            }

            if (previous == null)
            {
                this.head = finger.GetNext();
            }
            else
            {
                previous.SetNext(finger.GetNext());
            }

            this.count--;

            return(finger.GetValue());
        }
Exemplo n.º 2
0
        public override T RemoveFromTail()
        {
            Assertion.Pre(this.tail != null);

            SinglyLinkedListElement <T> finger  = this.tail;
            SinglyLinkedListElement <T> oldTail = this.tail;

            while (finger.GetNext() != this.tail)
            {
                finger = finger.GetNext();
            }

            if (finger == this.tail)
            {
                this.tail = null;
            }
            else
            {
                finger.SetNext(this.tail.GetNext());
                this.tail = finger;
            }

            this.count--;
            return(oldTail.GetValue());
        }
Exemplo n.º 3
0
        public void Insert(T value)
        {
            Assertion.Pre(!IsValid());
            BinaryTreeNode <T> node = new BinaryTreeNode <T>(value);

            if (root == null)
            {
                Assertion.Assert(this.prior == null);
                this.root = this.cursor = node;
            }
            else
            {
                Assertion.Assert(this.prior != null);

                if (wentLeft)
                {
                    this.prior.SetLeft(this.cursor = node);
                }
                else
                {
                    this.prior.SetRight(this.cursor = node);
                }
            }
            this.size++;
        }
Exemplo n.º 4
0
        public override T RemoveFromTail()
        {
            Assertion.Pre(this.head != null);

            SinglyLinkedListElement <T> previous = null;
            SinglyLinkedListElement <T> finger   = this.head;

            while (finger.GetNext() != null)
            {
                previous = finger;
                finger   = finger.GetNext();
            }

            if (previous == null)
            {
                this.head = null;
            }
            else
            {
                previous.SetNext(finger.GetNext());
            }

            this.count--;

            return(finger.GetValue());
        }
Exemplo n.º 5
0
 public void MoveLeft()
 {
     Assertion.Pre(IsValid());
     this.prior    = this.cursor;
     this.cursor   = this.cursor.GetLeft();
     this.wentLeft = true;
 }
Exemplo n.º 6
0
 public void MoveUp()
 {
     Assertion.Pre(IsValid());
     Assertion.Pre(HasParent());
     this.prior  = null;
     this.cursor = this.cursor.GetParent();
 }
Exemplo n.º 7
0
 public void MoveRight()
 {
     Assertion.Pre(IsValid());
     this.prior    = this.cursor;
     this.cursor   = this.cursor.GetRight();
     this.wentLeft = false;
 }
Exemplo n.º 8
0
        public T Remove()
        {
            Assertion.Pre(IsValid());
            Assertion.Pre(!(HasLeft() || HasRight()));

            T value = GetValue();

            if (IsLeft())
            {
                MoveUp();
                this.cursor.SetLeft(null);
            }
            else if (IsRight())
            {
                MoveUp();
                this.cursor.SetRight(null);
            }
            else
            {
                this.root = this.cursor = this.prior = null;
            }

            this.size--;
            return(value);
        }
Exemplo n.º 9
0
        public override T Next()
        {
            Assertion.Pre(HasMore());

            BinaryTreeNode <T> node = this.todo.Pop();
            T value = node.GetValue();

            if (!this.todo.IsEmpty())
            {
                BinaryTreeNode <T> parent = this.todo.Peek();

                if (node == parent.GetLeft())
                {
                    node = parent.GetRight();

                    while (node != null)
                    {
                        this.todo.Push(node);

                        if (node.GetLeft() != null)
                        {
                            node = node.GetLeft();
                        }
                        else
                        {
                            node = node.GetRight();
                        }
                    }
                }
            }

            return(value);
        }
Exemplo n.º 10
0
        private static void Swap(int[] data, int i, int j)
        {
            Assertion.Pre(i < data.Length && j < data.Length && i >= 0 && j >= 0);
            int tmp = data[i];

            data[i] = data[j];
            data[j] = tmp;
        }
Exemplo n.º 11
0
        public override void Enqueue(T value)
        {
            Assertion.Pre(!this.IsFull());
            int tail = (this.head + this.count) % this.data.Length;

            this.data[tail] = value;
            this.count++;
        }
Exemplo n.º 12
0
        public Vector <T> RemoveRowAt(int r)
        {
            Assertion.Pre(r < this.Height);

            Vector <T> row = this.rows.RemoveElementAt(r);

            return(row);
        }
Exemplo n.º 13
0
        public override T Dequeue()
        {
            Assertion.Pre(!this.IsEmpty());
            T toReturn = this.data[this.head];

            this.head = (this.head + 1) % this.data.Length;
            this.count--;
            return(toReturn);
        }
Exemplo n.º 14
0
        public void SetElementAt(T value, int r, int c)
        {
            Assertion.Pre(r < this.Height);
            Assertion.Pre(c < this.Width);

            Vector <T> row = this.rows.GetElementAt(r);

            row.SetElementAt(value, c);
        }
Exemplo n.º 15
0
        public T GetElementAt(int r, int c)
        {
            Assertion.Pre(r < this.Height);
            Assertion.Pre(c < this.Width);

            Vector <T> row = this.rows.GetElementAt(r);

            return(row.GetElementAt(c));
        }
Exemplo n.º 16
0
        public void InsertColumnAt(int c)
        {
            Assertion.Pre(c < this.Width);

            for (int i = 0; i < this.Height; i++)
            {
                Vector <T> row = this.rows.GetElementAt(i);
                row.InsertElementAt(default(T), c);
            }
        }
Exemplo n.º 17
0
        public override T RemoveFromHead()
        {
            Assertion.Pre(this.head != null);

            SinglyLinkedListElement <T> oldHead = this.head;

            this.head = this.head.GetNext();

            this.count--;

            return(oldHead.GetValue());
        }
Exemplo n.º 18
0
        public void InsertRowAt(int r)
        {
            Assertion.Pre(r < this.Height);

            Vector <T> row = new Vector <T>(this.Width);

            for (int i = 0; i < this.Width; i++)
            {
                row.AddElement(default(T));
            }

            this.rows.InsertElementAt(row, r);
        }
Exemplo n.º 19
0
        public override T PeekTail()
        {
            Assertion.Pre(this.head != null);

            SinglyLinkedListElement <T> finger = this.head;

            while (finger.GetNext() != null)
            {
                finger = finger.GetNext();
            }

            return(finger.GetValue());
        }
Exemplo n.º 20
0
        public override bool Contains(T value)
        {
            Assertion.Pre(value != null);

            SinglyLinkedListElement <T> finger = this.head;

            while (finger != null && !value.Equals(finger.GetValue()))
            {
                finger = finger.GetNext();
            }

            return(finger != null);
        }
Exemplo n.º 21
0
        public Vector <T> RemoveColumnAt(int c)
        {
            Assertion.Pre(c < this.Width);

            Vector <T> column = new Vector <T>();

            for (int i = 0; i < this.Height; i++)
            {
                Vector <T> row = this.rows.GetElementAt(i);
                column.AddElement(row.RemoveElementAt(c));
            }

            return(column);
        }
Exemplo n.º 22
0
        public T RemoveElementAt(int index)
        {
            Assertion.Pre(index < this.count);

            this.count--;

            T o = this.data[index];

            for (int i = index; i < this.count; i++)
            {
                this.data[i] = this.data[i + 1];
            }

            return(o);
        }
Exemplo n.º 23
0
        public void InsertElementAt(T value, int index)
        {
            Assertion.Pre(index < this.count);

            EnsureCapacity(this.count + 1);

            for (int i = this.count; i > index; i--)
            {
                this.data[i] = this.data[i - 1];
            }

            this.data[index] = value;

            this.count++;
        }
Exemplo n.º 24
0
        public override T Remove()
        {
            Assertion.Pre(!IsEmpty());

            T value = Peek();

            this.data.SetElementAt(this.data.GetElementAt(this.Size - 1), 0);
            this.data.RemoveElementAt(this.Size - 1);

            if (this.Size > 1)
            {
                PushDownRoot(0);
            }

            return(value);
        }
Exemplo n.º 25
0
        public override T Next()
        {
            Assertion.Pre(HasMore());

            BinaryTreeNode <T> node = this.todo.Dequeue();

            if (node.GetLeft() != null)
            {
                this.todo.Enqueue(node.GetLeft());
            }
            if (node.GetRight() != null)
            {
                this.todo.Enqueue(node.GetRight());
            }

            return(node.GetValue());
        }
Exemplo n.º 26
0
        public override T Next()
        {
            Assertion.Pre(HasMore());

            BinaryTreeNode<T> node = this.todo.Pop();
            T value = node.GetValue();

            node = node.GetRight();

            while (node != null)
            {
                this.todo.Push(node);
                node = node.GetLeft();
            }

            return value;
        }
Exemplo n.º 27
0
        public override T RemoveFromHead()
        {
            Assertion.Pre(this.head != null);

            DoublyLinkedListElement <T> oldHead = this.head;

            this.head = this.head.GetNext();

            if (this.head == null)
            {
                this.tail = null;
            }
            else
            {
                this.head.SetPrevious(null);
            }

            return(oldHead.GetValue());
        }
Exemplo n.º 28
0
        public override T RemoveFromHead()
        {
            Assertion.Pre(this.tail != null);

            SinglyLinkedListElement <T> head = this.tail.GetNext();

            if (this.tail == head)
            {
                this.tail = null;
            }
            else
            {
                this.tail.SetNext(head.GetNext());
            }

            this.count--;

            return(head.GetValue());
        }
Exemplo n.º 29
0
        public override T RemoveFromTail()
        {
            Assertion.Pre(this.tail != null);

            DoublyLinkedListElement <T> oldTail = this.tail;

            this.tail = this.tail.GetPrevious();

            if (this.tail == null)
            {
                this.head = null;
            }
            else
            {
                this.tail.SetNext(null);
            }

            return(oldTail.GetValue());
        }
Exemplo n.º 30
0
        public override bool Contains(T value)
        {
            Assertion.Pre(value != null);

            SinglyLinkedListElement <T> finger = this.tail;

            if (finger == null)
            {
                return(false);
            }

            do
            {
                if (value.Equals(finger.GetValue()))
                {
                    return(true);
                }

                finger = finger.GetNext();
            } while (finger != this.tail);

            return(false);
        }