Reverse a doubly linked list in O(1) time

本文详细介绍了双向链表的实现,并通过一系列测试验证了其实现的有效性和正确性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#ifndef GYM_CLRS_V3_10_2_8_LINKED_LIST_H_6D4EBDB0_5B18_4FC1_A34A_CBBF36FE95E4
#define GYM_CLRS_V3_10_2_8_LINKED_LIST_H_6D4EBDB0_5B18_4FC1_A34A_CBBF36FE95E4

#include <assert.h>
#include <stdint.h>
#include <boost/call_traits.hpp>

namespace pratique
{
    namespace clrs_version
    {
        namespace detail
        {
            template <typename T>
            struct Node
            {
                T data;
                uintptr_t link;
            };

            template <typename T>
            inline Node<T> * next( Node<T> * previous, Node<T> * node )
            {
                uintptr_t n = reinterpret_cast<uintptr_t>( previous ) ^ node->link;
                return reinterpret_cast<Node<T>*>( n );
            }

            template <typename T>
            inline Node<T> * previous( Node<T> * node, Node<T> * next )
            {
                uintptr_t p = node->link ^ reinterpret_cast<uintptr_t>( next );
                return reinterpret_cast<Node<T>*>( p );
            }

            template <typename T>
            inline uintptr_t compound( Node<T> * previous, Node<T> * next )
            {
                return reinterpret_cast<uintptr_t>( previous ) ^ reinterpret_cast<uintptr_t>( next );
            }
        }

        template <typename T>
        class DoublyLinkedList
        {
            detail::Node<T> * m_head, * m_last;

            inline static detail::Node<T> * forward( detail::Node<T> ** previous, detail::Node<T> ** node )
            {
                detail::Node<T> * tmp = *node;
                *node = detail::next( *previous, *node );
                *previous = tmp;
                return tmp;
            }

            inline static detail::Node<T> * backward( detail::Node<T> ** node, detail::Node<T> ** next )
            {
                detail::Node<T> * tmp = *node;
                *node = detail::previous( *node, *next );
                *next = tmp;
                return tmp;
            }

            inline void free()
            {
                if ( m_head != NULL ) {
                    detail::Node<T> * p = NULL, * n = m_head;
                    do {
                        delete forward( &p, &n );
                    } while ( n != m_head );
                    m_head = m_last = NULL;
                }
            }
        public:
	        typedef size_t size_type;

            DoublyLinkedList() : m_head( NULL ), m_last( NULL ) {}

            ~DoublyLinkedList()
            {
                free();
            }

            DoublyLinkedList( DoublyLinkedList<T> const & dll ) : m_head( NULL ), m_last( NULL )
            {
                if ( dll.m_head != NULL ) {
                    assert( dll.m_last != NULL );
                    detail::Node<T> * p = NULL, * n = dll.m_head, * tmp;
                    detail::Node<T> * mya = NULL, * myp = NULL, * myn = NULL;
                    ScopeGuard copy_destructor(
                        [&]() {
                            for ( detail::Node<T> * p = NULL, * n = m_head; n != myp; ) {
                                delete forward( &p, &n );
                            }
                            delete myp;
                        }
                    );
                    do {
                        tmp = forward( &p, &n );
                        myn = new detail::Node<T>;
                        myn->data = tmp->data;
                        if ( m_head == NULL ) {
                            m_head = myn;
                        }
                        if ( myp != NULL ) {
                            myp->link = detail::compound( mya, myn );
                        }
                        mya = myp;
                        myp = myn;
                    } while ( n != dll.m_head );
                    copy_destructor.dismiss();
                    myp->link = detail::compound( mya, m_head );
                }
            }
            DoublyLinkedList<T> & operator=( DoublyLinkedList<T> const & dll )
            {
                if ( this != &dll ) {
                    DoublyLinkedList<T>( dll ).swap( *this );
                }
                return *this;
            }

            void swap( DoublyLinkedList<T> & dll )
            {
                using std::swap;
                swap( m_head, dll.m_head );
                swap( m_last, dll.m_last );
            }

            bool operator==( DoublyLinkedList<T> const & dll ) const
            {
                bool equals = false;
                detail::Node<T> * p = NULL, * n = dll.m_head;
                detail::Node<T> * myp = NULL, * myn = m_head;
                if ( n != NULL && myn != NULL ) {
                    do {
                        if ( n->data == myn->data ) {
                            forward( &p, &n );
                            forward( &myp, &myn );
                        } else {
                            break;
                        }
                    } while ( n != dll.m_head && myn != m_head );
                    equals = ( n == dll.m_head && myn == m_head );
                } else {
                    equals = true;
                }
                return equals;
            }

            inline bool operator!=( DoublyLinkedList<T> const & dll ) const
            {
                return !( operator==( dll ) );
            }

            inline bool empty() const
            {
                return ( m_head == NULL );
            }

            size_type size() const
            {
                size_t count = 0;
                if ( m_head != NULL ) {
                    detail::Node<T> * previous = NULL, * current = m_head, * tmp;
                    do {
                        ++count;
                        tmp = current;
                        current = next( previous, current );
                        previous = tmp;
                    } while ( current != m_head );
                }
                return count;
            }

            void push_back( typename boost::call_traits<T>::param_type v )
            {
                detail::Node<T> * p = new detail::Node<T>;
                p->data = v;
                if ( m_head != NULL ) {
                    detail::Node<T> * previous = detail::previous( m_last, m_head );
                    m_last->link = detail::compound( previous, p );
                    p->link = detail::compound( m_last, m_head );
                } else {
                    detail::Node<T> * previous = NULL;
                    m_head = p;
                    m_head->link = detail::compound( previous, p );
                }
                m_last = p;
            }

            void pop_back()
            {
                if ( m_head != NULL ) {
                    detail::Node<T> * previous = detail::previous( m_last, m_head );
                    if ( previous != NULL ) {
                        detail::Node<T> * ancestor = detail::previous( previous, m_last );
                        delete m_last;
                        previous->link = detail::compound( ancestor, m_head );
                        m_last = previous;
                    } else {
                        assert( m_last == m_head );
                        delete m_head;
                        m_last = m_head = NULL;
                    }
                }
            }

            void push_front( typename boost::call_traits<T>::param_type v )
            {
                detail::Node<T> * null_previous = NULL;
                detail::Node<T> * p = new detail::Node<T>;
                p->data = v;
                if ( m_head != NULL ) {
                    detail::Node<T> * previous = detail::previous( m_last, m_head );
                    detail::Node<T> * next = detail::next( null_previous, m_head );
                    p->link = detail::compound( null_previous, m_head );
                    if ( previous != NULL ) {
                        m_last->link = detail::compound( previous, p );
                        m_head->link = detail::compound( p, next );
                    } else {
                        m_head->link = detail::compound( p, p );
                        m_last = m_head;
                    }
                } else {
                    p->link = detail::compound( null_previous, p );
                    m_last = p;
                }
                m_head = p;
            }

            void pop_front()
            {
                if ( m_head != NULL ) {
                    detail::Node<T> * null_previous = NULL;
                    detail::Node<T> * previous = detail::previous( m_last, m_head );
                    if ( previous != NULL ) {
                        detail::Node<T> * next = detail::next( null_previous, m_head );
                        if ( next != m_last ) {
                            detail::Node<T> * third = detail::next( m_head, next );
                            m_last->link = detail::compound( previous, next );
                            next->link = detail::compound( null_previous, third );
                        } else {
                            m_last->link = detail::compound( null_previous, m_last );
                        }
                        delete m_head;
                        m_head = next;
                    } else {
                        delete m_head;
                        m_head = m_last = NULL;
                    }
                }
            }

            void reverse()
            {
                if ( m_head != m_last ) {
                    detail::Node<T> * null_previous = NULL;
                    detail::Node<T> * previous = detail::previous( m_last, m_head );
                    detail::Node<T> * next = detail::next( null_previous, m_head );
                    m_last->link = detail::compound( null_previous, previous );
                    m_head->link = detail::compound( m_last, next );
                    std::swap( m_last, m_head );
                }
            }

            T const & front() const
            {
                assert( !empty() );
                return m_head->data;
            }

            T const & back() const
            {
                assert( !empty() );
                return m_last->data;
            }

            class Iterator
            {
                friend class DoublyLinkedList<T>;
                detail::Node<T> * m_previous, * m_current, * m_last;
                Iterator( DoublyLinkedList<T> const * dll ) : m_previous( NULL ), m_current( dll->m_head ), m_last( dll->m_last )  {}
            public:
                Iterator() : m_previous( NULL ), m_current( NULL ), m_last( NULL ) {}

                typedef std::bidirectional_iterator_tag iterator_category;

                typedef T value_type;
                typedef detail::Node<T> * pointer;
                typedef ptrdiff_t difference_type;
                typedef T const & reference;

                reference operator*() const
                {
                    return m_current->data;
                }

                Iterator & operator++()
                {
                    DoublyLinkedList<T>::forward( &m_previous, &m_current );
                    return (*this);
                }

                Iterator operator++(int)
                {
                    Iterator tmp( *this );
                    ++*this;
                    return tmp;
                }

                Iterator & operator--()
                {
                    DoublyLinkedList<T>::backward( &m_previous, &m_current );
                    return (*this);
                }

                Iterator operator--(int)
                {
                    Iterator tmp( *this );
                    --*this;
                    return tmp;
                }

                bool operator==( const Iterator & i ) const
                {
                    return ( m_current == i.m_current && m_previous == i.m_previous );
                }

                bool operator!=( const Iterator& i ) const
                {
                    return ( m_current != i.m_current || m_previous != i.m_previous );
                }
            };

            typedef Iterator iterator;

            iterator begin() const { return Iterator( this ); }
            iterator end() const
            {
                Iterator i;
                i.m_previous = this->m_last;
                i.m_current = this->m_head;
                i.m_last = this->m_last;
                return i;
            }
        };
    }
}

#endif //GYM_CLRS_V3_10_2_8_LINKED_LIST_H_6D4EBDB0_5B18_4FC1_A34A_CBBF36FE95E4


The testing code is:

    typedef pratique::clrs_version::DoublyLinkedList<int> mydll;

    void must_be_equals( mydll const & l, mydll const & r )
    {
        if ( l != r ) {
            fprintf( stderr, "Bad implementation for the problem CRLS 10-2-8\n" );
            DebugBreak();
        }
    }

    void clrs_list_test()
    {
        {
            mydll dll;

            {
                mydll copy( dll );
                must_be_equals( dll, copy );
            }

            dll.push_back( 1 );
            dll.pop_back();

            {
                mydll copy( dll );
                must_be_equals( dll, copy );
            }
        }

        {
            mydll dll;

            dll.push_back( 1 );
            {
                mydll copy( dll );
                must_be_equals( dll, copy );
            }

            dll.push_back( 2 );
            dll.pop_back();

            {
                mydll copy( dll );
                must_be_equals( dll, copy );
            }
        }

        {
            mydll dll;
            const int limits = 128;
            int to_be_compared[ limits ];
            for ( int i = 0; i < limits; ++i ) {
                to_be_compared[i] = i;
            }

            for ( int i = 0; i < limits; ++i ) {
                dll.push_back( i );
                if ( !std::equal( dll.begin(), dll.end(), std::begin( to_be_compared ) ) ) {
                    fprintf( stderr, "Bad implementation for the problem CRLS 10-2-8\n" );
                    DebugBreak();
                    break;
                }
            }

            while ( !dll.empty() ) {
                dll.pop_back();
            }

            for ( int i = 0; i < limits; ++i ) {
                dll.push_back( i );
                if ( !std::equal( dll.begin(), dll.end(), std::begin( to_be_compared ) ) ) {
                    fprintf( stderr, "Bad implementation for the problem CRLS 10-2-8\n" );
                    DebugBreak();
                    break;
                }
            }

            dll.reverse();

            int count = 0;
            std::vector<int> result;
            while ( !dll.empty() ) {
                ++count;
                result.push_back( dll.back() );
                dll.pop_back();
            }

            for ( int i = 0; i < limits; ++i ) {
                if ( result[i] != i ) {
                    fprintf( stderr, "Bad implementation for the problem CRLS 10-2-8\n" );
                    DebugBreak();
                    break;
                }
            }

            mydll copy( dll );
            must_be_equals( dll, copy );
        }

        {
            mydll dll;

            dll.push_front( 1 );
            dll.pop_front();

            {
                mydll copy( dll );
                must_be_equals( dll, copy );
            }
        }

        {
            mydll dll;

            dll.push_front( 1 );
            {
                mydll copy( dll );
                must_be_equals( dll, copy );
            }

            dll.push_front( 2 );
            dll.pop_front();

            {
                mydll copy( dll );
                must_be_equals( dll, copy );
            }
        }

        {
            mydll dll;
            const int limits = 128;
            int to_be_compared[ limits ];
            for ( int i = 0; i < limits; ++i ) {
                to_be_compared[i] = i;
            }

            for ( int i = limits - 1; i >= 0; --i ) {
                dll.push_front( i );
                if ( !std::equal( dll.begin(), dll.end(), std::begin( to_be_compared ) + i ) ) {
                    fprintf( stderr, "Bad implementation for the problem CRLS 10-2-8\n" );
                    DebugBreak();
                    break;
                }
            }

            while ( !dll.empty() ) {
                dll.pop_back();
            }

            for ( int i = limits - 1; i >= 0; --i ) {
                dll.push_front( i );
                if ( !std::equal( dll.begin(), dll.end(), std::begin( to_be_compared ) + i ) ) {
                    fprintf( stderr, "Bad implementation for the problem CRLS 10-2-8\n" );
                    DebugBreak();
                    break;
                }
            }

            dll.reverse();

            int count = 0;
            std::vector<int> result;
            while ( !dll.empty() ) {
                ++count;
                result.push_back( dll.back() );
                dll.pop_back();
            }

            for ( int i = 0; i < limits; ++i ) {
                if ( result[i] != i ) {
                    fprintf( stderr, "Bad implementation for the problem CRLS 10-2-8\n" );
                    DebugBreak();
                    break;
                }
            }

            mydll copy( dll );
            must_be_equals( dll, copy );
        }
    }


 

【Solution】 To convert a binary search tree into a sorted circular doubly linked list, we can use the following steps: 1. Inorder traversal of the binary search tree to get the elements in sorted order. 2. Create a doubly linked list and add the elements from the inorder traversal to it. 3. Make the list circular by connecting the head and tail nodes. 4. Return the head node of the circular doubly linked list. Here's the Python code for the solution: ``` class Node: def __init__(self, val): self.val = val self.prev = None self.next = None def tree_to_doubly_list(root): if not root: return None stack = [] cur = root head = None prev = None while cur or stack: while cur: stack.append(cur) cur = cur.left cur = stack.pop() if not head: head = cur if prev: prev.right = cur cur.left = prev prev = cur cur = cur.right head.left = prev prev.right = head return head ``` To verify the accuracy of the code, we can use the following test cases: ``` # Test case 1 # Input: [4,2,5,1,3] # Output: # Binary search tree: # 4 # / \ # 2 5 # / \ # 1 3 # Doubly linked list: 1 <-> 2 <-> 3 <-> 4 <-> 5 # Doubly linked list in reverse order: 5 <-> 4 <-> 3 <-> 2 <-> 1 root = Node(4) root.left = Node(2) root.right = Node(5) root.left.left = Node(1) root.left.right = Node(3) head = tree_to_doubly_list(root) print("Binary search tree:") print_tree(root) print("Doubly linked list:") print_list(head) print("Doubly linked list in reverse order:") print_list_reverse(head) # Test case 2 # Input: [2,1,3] # Output: # Binary search tree: # 2 # / \ # 1 3 # Doubly linked list: 1 <-> 2 <-> 3 # Doubly linked list in reverse order: 3 <-> 2 <-> 1 root = Node(2) root.left = Node(1) root.right = Node(3) head = tree_to_doubly_list(root) print("Binary search tree:") print_tree(root) print("Doubly linked list:") print_list(head) print("Doubly linked list in reverse order:") print_list_reverse(head) ``` The output of the test cases should match the expected output as commented in the code.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值