示例#1
0
    /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
    public bool DeleteLast()
    {
        if (CurrentLength <= 0)
        {
            return(false);
        }
        else
        {
            if (StartCircularDeque.Next == null)
            {
                if (StartCircularDeque.Previous == null)
                {
                    StartCircularDeque = null;
                }
                else
                {
                    CircularDeque PreviousCircularDeque = StartCircularDeque.Previous;
                    StartCircularDeque.Val      = PreviousCircularDeque.Val;
                    StartCircularDeque.Previous = PreviousCircularDeque.Previous;
                }
            }
            else
            {
                CircularDeque _CircularDeque = StartCircularDeque;

                while (_CircularDeque.Next != null)
                {
                    _CircularDeque = _CircularDeque.Next;
                }

                if (_CircularDeque.Previous != null)//队列中元素超过2个
                {
                    CircularDeque PreviousCircularDeque = _CircularDeque.Previous;
                    PreviousCircularDeque.Next = null;
                }
                else//队列中元素不足2个
                {
                    StartCircularDeque = null;
                    CurrentLength      = 0;
                }
            }

            if (CurrentLength >= 1)
            {
                CurrentLength--;
            }

            return(true);
        }
    }
示例#2
0
    /** Get the last item from the deque. */
    public int GetRear()
    {
        if (CurrentLength <= 0)
        {
            return(-1);
        }
        else
        {
            CircularDeque _CircularDeque = StartCircularDeque;

            while (_CircularDeque.Next != null)
            {
                _CircularDeque = _CircularDeque.Next;
            }

            return(_CircularDeque.Val);
        }
    }
示例#3
0
    /** Get the front item from the deque. */
    public int GetFront()
    {
        if (CurrentLength <= 0)
        {
            return(-1);
        }
        else
        {
            CircularDeque _CircularDeque = StartCircularDeque;

            while (_CircularDeque.Previous != null)
            {
                _CircularDeque = _CircularDeque.Previous;
            }

            return(_CircularDeque.Val);
        }
    }
示例#4
0
    /** Adds an item at the rear of Deque. Return true if the operation is successful. */
    public bool InsertLast(int value)
    {
        if (CurrentLength >= Length)
        {
            return(false);
        }
        else
        {
            if (StartCircularDeque == null)
            {
                StartCircularDeque          = new CircularDeque();
                StartCircularDeque.Previous = null;
                StartCircularDeque.Val      = value;
                StartCircularDeque.Next     = null;
            }
            else
            {
                CircularDeque _CircularDeque = StartCircularDeque;

                while (_CircularDeque.Next != null)
                {
                    _CircularDeque = _CircularDeque.Next;
                }

                CircularDeque LastCircularDeque = new CircularDeque();
                LastCircularDeque.Val      = value;
                LastCircularDeque.Previous = _CircularDeque;
                LastCircularDeque.Next     = null;

                _CircularDeque.Next = LastCircularDeque;
            }


            if (CurrentLength < Length)
            {
                CurrentLength++;
            }

            return(true);
        }
    }