zoukankan      html  css  js  c++  java
  • 根据存放位置数据的链表P打印链表L的元素

    题目:给定一个链表L和另一个链表P,它们包含以升序排列的整数。操作printLots打印L中那些由P所指定的位置上的元素。写出过程printLots(L,P)。只可以使用公有的STL容器操作。该过程的运行时间是多少?

    我编写的程序如下:

    #include <iostream>
    
    using namespace std;
    
    template <typename Object>
    class List
    {
        private:
            struct Node                             //List内部维护的节点类
            {
                Object data;
                Node *prev;
                Node *next;
    
                Node( const Object &d = Object(),Node *p = NULL,Node *n = NULL )
                    : data( d ),prev( p ),next( n ) { }
            };
    
        public:
            class const_iterator                    //常量迭代器
            {
                public:
                    const_iterator() : current( NULL )
                    { }
    
                    const Object &operator*() const
                    { return retrieve(); }
    
                    const_iterator &operator++()
                    {
                        current = current->next;
                        return *this;
                    }
    
                    const_iterator operator++( int )
                    {
                        const_iterator old = *this;
                        ++( *this );
                        return old;
                    }
    
                    bool operator== ( const const_iterator &rhs ) const
                    { return current == rhs.current; }
    
                    bool operator!= ( const const_iterator &rhs ) const
                    { return !( *this == rhs ); }
    
                protected:
                    Node *current;
    
                    Object &retrieve() const
                    { return current->data; }
    
                    const_iterator( Node *p ) : current( p )
                    { }
    
                    friend class List<Object>;
            };
    
            class iterator : public const_iterator  //迭代器类继承自常量迭代器类
            {
                public:
                iterator()
                { }
    
                Object &operator*()
                { return const_iterator::retrieve(); }
    
                const Object &operator*() const
                { return const_iterator::operator*(); }
    
                iterator &operator++()
                {
                    const_iterator::current = const_iterator::current->next;
                    return *this;
                }
    
                iterator operator++( int )
                {
                    iterator old = *this;
                    ++( *this );
    
                    return old;
                }
    
                protected:
                    iterator( Node *p ) : const_iterator( p )
                    { }
    
                    friend class List<Object>;
            };
    
        public:
            List()
            { init(); }
    
            ~List()
            {
                clear();
                delete head;
                delete tail;
            }
    
            List( const List &rhs )
            {
                init();
                *this = rhs;
            }
    
            const List &operator= ( const List &rhs )
            {
                if ( this == &rhs )
                    return *this;
                clear();
                for ( const_iterator itr = rhs.begin() ; itr != rhs.end() ; ++itr )
                    push_back( *itr );
    
                return *this;
            }
    
            iterator begin()
            { return iterator( head->next ); }
            const_iterator begin() const
            { return const_iterator( head->next ); }
            iterator end()
            { return iterator( tail ); }
            const_iterator end() const
            { return const_iterator( tail ); }
    
            int size() const
            { return theSize; }
            bool empty() const
            { return size() == 0; }
    
            void clear()
            {
                while ( !empty() )
                    pop_front();
            }
    
            Object &front()
            { return *begin(); }
            const Object &front() const
            { return *begin(); }
            Object &back()
            { return *--end(); }
            const Object &back() const
            { return *--end(); }
            void push_front( const Object &x )
            { insert( begin(),x ); }
            void push_back( const Object &x )
            { insert( end(),x ); }
            void pop_front()
            { erase( begin() ); }
            void pop_back()
            { erase( --end() ); }
    
            iterator insert( iterator itr,const Object &x )
            {
                Node *p = itr.current;
                theSize++;
                return ( p->prev = p->prev->next = new Node( x,p->prev,p ) );
            }
    
            iterator erase( iterator itr )
            {
                Node *p = itr.current;
                iterator retVal( p->next );
                p->prev->next = p->next;
                p->next->prev = p->prev;
                delete p;
                theSize--;
    
                return retVal;
            }
    
            iterator erase( iterator start,iterator end )
            {
                for ( iterator itr = start ; itr != end; )
                    itr = erase( itr );
    
                return end;
            }
    
        private:
            int theSize;
            Node *head;
            Node *tail;
    
            void init()
            {
                theSize = 0;
                head = new Node;
                tail = new Node;
                head->next = tail;
                tail->prev = head;
            }
    };
    
    void printLots( List<int> &L,List<int> &P )         //打印出L链表 指定位置(P中存放)的值
    {
        List<int>::const_iterator itr = L.begin();
        int count = 0;                                  //P是升序排列那么不必每次遍历L,维护计数器一直递增迭代器即可
    
        for ( List<int>::const_iterator itr_P = P.begin() ; itr_P != P.end() ; ++itr_P ){
            int pos = *itr_P;
            while ( pos != count ){
                if ( itr == L.end() )
                    return ;
                count++;
                itr++;
            }
            if ( itr != L.end() )
                cout << *itr << endl;
        }
    
        return ;
    }
    
    int main( void )
    {
        List<int> p,temp;
        p.push_back( 0 );
        p.push_back( 5 );
    
        temp.push_back( 43 );
        temp.push_back( 4353 );
    
        cout << "The pos List:" << endl;
    
        for ( List<int>::iterator iter = p.begin() ; iter != p.end() ; ++iter )
            cout << *iter << " ";
    
        cout << endl;
    
        cout << "The temp List:" << endl;
        printLots( temp,p );
    
        return 0;
    }

    运行结果:

    printLots过程中temp链表迭代P中的最大值 次,所以运行时间是取决于P中最后一个元素N,O(N)。

    我们一路奋战,不是为了改变世界,而是不让世界改变我们 ——《熔炉》
  • 相关阅读:
    Kotlin Native
    大数据技术原理与应用【点个赞】
    TypeScript的概要和简介
    Windows 10 运行原生Bash【Ubuntu】
    Kotlin的参考资料
    Javascript前端和JAVA后端对加密库的处理实例
    bootstrap杂记
    原生JS实现各种经典网页特效——Banner图滚动、选项卡切换、广告弹窗等
    博文目录 | 杰瑞教育原创系列文章目录一览
    MUI框架开发HTML5手机APP(二)--页面跳转传值&底部选项卡切换
  • 原文地址:https://www.cnblogs.com/ZRBYYXDM/p/5142634.html
Copyright © 2011-2022 走看看