精华内容
下载资源
问答
  • 双向链表交换节点

    2019-09-20 22:53:48
    交换index // 更改prev next if (attrFromNode.prev) { attrFromNode.prev.next = targetNode; } if (attrTargetNode.next) { attrTargetNode.next.prev = fromNode; } targetNode.prev =...
            switchChainNode(fromNode, targetNode) {
                let attrFromNode = {
                    index: fromNode.index,
                    prev: fromNode.prev,
                    next: fromNode.next
                };
                let attrTargetNode = {
                    index: targetNode.index,
                    prev: targetNode.prev,
                    next: targetNode.next
                };
    
                let isSibling = attrTargetNode.prev === fromNode;
                // 交换index 
                // 更改prev next
    
                if (attrFromNode.prev) {
                    attrFromNode.prev.next = targetNode;
                }
    
                if (attrTargetNode.next) {
                    attrTargetNode.next.prev = fromNode;
                }
    
                targetNode.prev = attrFromNode.prev;
                fromNode.next = attrTargetNode.next;
    
                if (isSibling) {
                    targetNode.next = fromNode;
                    fromNode.prev = targetNode;
                } else {
                    targetNode.next = attrFromNode.next;
                    fromNode.prev = attrTargetNode.prev;
    
                    if (attrTargetNode.prev) {
                        attrTargetNode.prev.next = fromNode;
                    }
                    if (attrFromNode.next) {
                        attrFromNode.next.prev = targetNode;
                    }
                }
    
                fromNode.index = attrTargetNode.index;
                targetNode.index = attrFromNode.index;
            }

     

    转载于:https://www.cnblogs.com/vaal-water/p/10245611.html

    展开全文
  • 1、单向链表交换相邻元素 代码: //请在DEV中运行,先勾选“Tools" -勾选”Compailer Options"-再勾选"Add~~~“-框框中输入"-std=c++11” #include<iostream> #include &...

    1、单向链表交换相邻元素

    代码:

    //请在DEV中运行,先勾选“Tools" -勾选”Compailer Options"-再勾选"Add~~~“-框框中输入"-std=c++11” 
    #include<iostream>  
    #include <algorithm>
    using namespace std;
    template<typename Object> 
    //定义变量和指针变量 
    struct Node{
    	Object num;
    	Node *next;
    };
    typedef Node<int>PNODE;//实例化int对象PNODE 
    //创建链表,n为链表中int对象的个数 
    PNODE* Create(int n)
    {
    	PNODE *phead = new PNODE;
    	phead->next = nullptr;
    	
    	PNODE *PTlie = phead;
    	for(int i = 0; i < n; i++)
    	{
    		PNODE *pnew =new PNODE;
    		pnew->num = i;//顺向建立链表 
    		pnew->next =nullptr;
    		PTlie->next = pnew;
    		PTlie = pnew;
    	}
    	
    	return phead;
    }
    //交换
    void Swap(PNODE *p,int n)//交换是当前位置和后面一个位置交换
    {
    	int i = 0;
    	while(i != n - 1 && p->next != nullptr)//记录要交换结点的前一个结点 
    	{
    		p = p->next;
    		i++;
    	}
    	if(p->next == nullptr)
    	{
    		printf("无法交换");
    		return ;
    	}
    	
    	
    	PNODE *p1,*p2;//定义p1,p2分别记录要交换两个结点
    	p1 = p->next;
    	p2 = p1->next;
    	
    	p1->next = p2->next;
    	p->next = p2;
    	p2->next = p1;
    }
    //输出链表 
    void print(PNODE *p)
    {
    	p = p->next;
    	for(int i = 0;p != nullptr; i++)
    	{
    		printf("%d ",p->num);
    		p = p->next;
    	}
    	printf("\n\n");
    }
    
    int main(void)
    {
    	PNODE *p = Create(20);//创建一个含20位int的链表 
    	int n;
    	cout<<"输出原链表:"<<endl; 
    	print(p);
    	printf("输入要交换的位置:");
    	cin>>n;//交换是当前位置和后面一个位置交换
    	cout<<endl;
    	Swap(p,n);
    	cout<<"输出交换后的链表:"<<endl; 
    	print(p);
    	return 0;
    }

    运行:

    2、双向链表交换元素

    代码:

    //请在DEV中运行,先勾选“Tools" -勾选”Compailer Options"-再勾选"Add~~~“-框框中输入"-std=c++11” 
    #include<iostream>
    #include <algorithm>
    using namespace std;
    template<typename Object>
    
    struct Node{
    	Object num;
    	struct Node * next;
    	struct Node * last;
    };
    typedef Node<int> PNODE;
    int length;//不设置初始化数值,自定义链表 
    //创建一个双向循环链表
    PNODE* Create()
    {
    	PNODE *phead = new PNODE;
    	phead->next = phead;
    	phead->last = phead;
    	
    	PNODE *PTlie = phead;
    	cin>>length; 
    	for(int i = 0; i < length; i++)
    	{
    		PNODE *pnew = new PNODE;
    		
    		pnew->num = i;
    		pnew->next = phead;
    		
    		pnew->last = PTlie;
    		PTlie->next = pnew;
    		PTlie = pnew;
    	}
    	return phead;
    }
    //交换是当前位置和后面一个位置交换
    void swap(PNODE *p,int n)
    {
    	int i = 0;
    
    	while(i != n - 1 && p->next != NULL)//记录要交换结点的前一个结点 
    	{
    		p = p->next;
    		i++;
    	}
    
    	PNODE *p1,*p2;//定义p1,p2分别记录要交换两个结点
    	
    	p1 = p->next;
    	p2 = p1->next;
    
    	p1->next = p2->next;
    	p2->next->last = p1;//这里注意,如果是双向不循环链表的话,最后两个数交换,会导致程序错误。因为p2->next为空
    
    	p->next = p2;
    	p2->last = p;
    
    
    	p2->next = p1;
    	p1->last = p2;
    
    }
    //输出链表 
    void print(PNODE *p,int length)
    {
    	p = p->next;
    	for(int i = 0; i < length; i++)
    	{
    		cout<<p->num<<" ";
    		p = p->next;
    	}
    	cout<<endl;
    }
    
    int main()
    {   cout<<"输入length:";
    	PNODE *p = Create();//创建链表 
    	print(p,length);
    	int n;
    	cout<<"输入要交换的位置:";
    	cin>>n;
    	swap(p,n);
    	cout<<"输出交换后的链表"; 
    	print(p,length);
    	return 0;
    }
    

    运行:

     

    3、反向迭代器

    代码:

    //请在DEV中运行,先勾选“Tools" -勾选”Compailer Options"-再勾选"Add~~~“-框框中输入"-std=c++11” 
    #ifndef LIST_H
    #define LIST_H
    
    #include<iostream>
    #include <algorithm>
    using namespace std;
    
    template <typename Object>
    class List
    {
    private:
    	// The basic doubly linked list node.
    	// Nested inside of List, can be public
    	// because the Node is itself private
    	struct Node
    	{
    		Object  data;
    		Node   *prev;
    		Node   *next;
    
    		Node(const Object & d = Object{}, Node * p = nullptr, Node * n = nullptr)
    			: data{ d }, prev{ p }, next{ n } { }
    
    		Node(Object && d, Node * p = nullptr, Node * n = nullptr)
    			: data{ std::move(d) }, prev{ p }, next{ n } { }
    	};
    
    public:
    	
    	class const_iterator
    	{
    	public:
    
    		// Public constructor for const_iterator.
    		const_iterator() : current{ nullptr }
    		{ }
    
    		// Return the object stored at the current position.
    		// For const_iterator, this is an accessor with a
    		// const reference return type.
    		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;
    		}
    
    		const_iterator & operator-- ()
    		{
    			current = current->prev;
    			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;
    
    		// Protected helper in const_iterator that returns the object
    		// stored at the current position. Can be called by all
    		// three versions of operator* without any type conversions.
    		Object & retrieve() const
    		{
    			return current->data;
    		}
    
    		// Protected constructor for const_iterator.
    		// Expects a pointer that represents the current position.
    		const_iterator(Node *p) : current{ p }
    		{ }
    
    		friend class List<Object>;
    	};
    	/
    
    	class iterator : public const_iterator
    	{
    	public:
    
    		// Public constructor for iterator.
    		// Calls the base-class constructor.
    		// Must be provided because the private constructor
    		// is written; otherwise zero-parameter constructor
    		// would be disabled.
    		iterator()
    		{ }
    
    		Object & operator* ()
    		{
    			return const_iterator::retrieve();
    		}
    
    		// Return the object stored at the current position.
    		// For iterator, there is an accessor with a
    		// const reference return type and a mutator with
    		// a reference return type. The accessor is shown first.
    		const Object & operator* () const
    		{
    			return const_iterator::operator*();
    		}
    
    		iterator & operator++ ()
    		{
    			this->current = this->current->next;
    			return *this;
    		}
    
    		iterator operator++ (int)
    		{
    			iterator old = *this;
    			++(*this);
    			return old;
    		}
    
    		iterator & operator-- ()
    		{
    			this->current = this->current->prev;
    			return *this;
    		}
    
    		iterator operator-- (int)
    		{
    			iterator old = *this;
    			--(*this);
    			return old;
    		}
    
    	protected:
    		// Protected constructor for iterator.
    		// Expects the current position.
    		iterator(Node *p) : const_iterator{ p }
    		{ }
    
    		friend class List<Object>;
    	};
    	/
    //反向迭代器iterator 
    	class const_reverse_iterator
    	{
    	public:
    
    		// Public constructor for iterator.
    		// Calls the base-class constructor.
    		// Must be provided because the private constructor
    		// is written; otherwise zero-parameter constructor
    		// would be disabled.
    		const_reverse_iterator() : current{ nullptr }
    		{ }
    
    		// Return the object stored at the current position.
    		// For iterator, there is an accessor with a
    		// const reference return type and a mutator with
    		// a reference return type. The accessor is shown first.
    		const Object & operator* () const
    		{
    			return retrieve();
    		}
    
    		const_reverse_iterator & operator++ ()
    		{
    
    			this->current = this->current->prev;
    			return *this;
    
    		}
    
    		const_reverse_iterator operator++ (int)
    		{
    			const_reverse_iterator old = *this;
    			--(*this);
    			return old;
    		}
    
    		const_reverse_iterator & operator-- ()
    		{
    			current = current->next;
    			return *this;
    		}
    
    		const_reverse_iterator operator-- (int)
    		{
    			const_reverse_iterator old = *this;
    			++(*this);
    			return old;
    		}
    		bool operator== (const const_reverse_iterator & rhs) const
    		{
    			return current == rhs.current;
    		}
    
    		bool operator!= (const const_reverse_iterator & rhs) const
    		{
    			return !(*this == rhs);
    		}
    
    	protected:
    		Node *current;
    		Object & retrieve() const
    		{
    			return current->data;
    		}
    		// Protected constructor for iterator.
    		// Expects the current position.
    		const_reverse_iterator(Node *p) : current{ p }
    		{ }
    
    		friend class List<Object>;
    	};
    	///
    	class reverse_iterator : public const_reverse_iterator
    	{
    	public:
    
    		// Public constructor for iterator.
    		// Calls the base-class constructor.
    		// Must be provided because the private constructor
    		// is written; otherwise zero-parameter constructor
    		// would be disabled.
    		reverse_iterator()
    		{ }
    
    		Object & operator* ()
    		{
    			return const_reverse_iterator::retrieve();
    		}
    
    		// Return the object stored at the current position.
    		// For iterator, there is an accessor with a
    		// const reference return type and a mutator with
    		// a reference return type. The accessor is shown first.
    		const Object & operator* () const
    		{
    			return const_reverse_iterator::operator*();
    		}
    
    		reverse_iterator & operator++ ()
    		{
    			reverse_iterator old = *this;
    			--(*this);
    			return old;
    
    		}
    
    		reverse_iterator operator++ (int)
    		{
    			
    			this->current = this->current->prev;
    			return *this;
    
    		}
    
    		reverse_iterator & operator-- ()
    		{
    			
    			reverse_iterator old = *this;
    			++(*this);
    			
    		}
    
    		reverse_iterator operator-- (int)
    		{
    			this->current =this-> current->next;
    			return *this;
    		}
    
    	protected:
    		// Protected constructor for iterator.
    		// Expects the current position.
    		reverse_iterator(Node *p) : const_reverse_iterator{ p }
    		{ }
    
    		friend class List<Object>;
    	};
    public:
    	List()
    	{
    		init();
    	}
    
    	~List()
    	{
    		clear();
    		delete head;
    		delete tail;
    	}
    
    	List(const List & rhs)
    	{
    		init();
    		for (auto & x : rhs)
    			push_back(x);
    	}
    
    	List & operator= (const List & rhs)
    	{
    		List copy = rhs;
    		std::swap(*this, copy);
    		return *this;
    	}
    
    
    	List(List && rhs)
    		: theSize{ rhs.theSize }, head{ rhs.head }, tail{ rhs.tail }
    	{
    		rhs.theSize = 0;
    		rhs.head = nullptr;
    		rhs.tail = nullptr;
    	}
    
    	List & operator= (List && rhs)
    	{
    		std::swap(theSize, rhs.theSize);
    		std::swap(head, rhs.head);
    		std::swap(tail, rhs.tail);
    
    		return *this;
    	}
    
    	// Return iterator representing beginning of list.
    	// Mutator version is first, then accessor version.
    	iterator begin()
    	{
    		return iterator(head->next);
    	}
    
    	const_iterator begin() const
    	{
    		return const_iterator(head->next);
    	}
    
    	// Return iterator representing endmarker of list.
    	// Mutator version is first, then accessor version.
    	iterator end()
    	{
    		return iterator(tail);
    	}
    
    	const_iterator end() const
    	{
    		return const_iterator(tail);
    	}
    
    	reverse_iterator rbegin()
    	{
    		return reverse_iterator(tail->prev);
    	}
    
    	const_reverse_iterator rbegin() const
    	{
    		return const_reverse_iterator(tail->prev);
    	}
    
    	// Return iterator representing endmarker of list.
    	// Mutator version is first, then accessor version.
    	reverse_iterator rend()
    	{
    		return reverse_iterator(head);
    	}
    
    	const_reverse_iterator rend() const
    	{
    		return const_reverse_iterator(head);
    	}
    
    	// Return number of elements currently in the list.
    	int size() const
    	{
    		return theSize;
    	}
    
    	// Return true if the list is empty, false otherwise.
    	bool empty() const
    	{
    		return size() == 0;
    	}
    
    	void clear()
    	{
    		while (!empty())
    			pop_front();
    	}
    
    	// front, back, push_front, push_back, pop_front, and pop_back
    	// are the basic double-ended queue operations.
    	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 push_front(Object && x)
    	{
    		insert(begin(), std::move(x));
    	}
    
    	void push_back(Object && x)
    	{
    		insert(end(), std::move(x));
    	}
    
    	void pop_front()
    	{
    		erase(begin());
    	}
    
    	void pop_back()
    	{
    		erase(--end());
    	}
    
    	// Insert x before itr.
    	iterator insert(iterator itr, const Object & x)
    	{
    		Node *p = itr.current;
    		++theSize;
    		return iterator(p->prev = p->prev->next = new Node{ x, p->prev, p });
    	}
    
    	// Insert x before itr.
    	iterator insert(iterator itr, Object && x)
    	{
    		Node *p = itr.current;
    		++theSize;
    		return iterator(p->prev = p->prev->next = new Node{ std::move(x), p->prev, p });
    	}
    
    	// Erase item at itr.
    	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 from, iterator to)
    	{
    		for (iterator itr = from; itr != to;)
    			itr = erase(itr);
    
    		return to;
    	}
    
    private:
    	int   theSize;
    	Node *head;
    	Node *tail;
    
    	void init()
    	{
    		theSize = 0;
    		head = new Node;
    		tail = new Node;
    		head->next = tail;
    		tail->prev = head;
    	}
    };
    
    #endif
    int main()//测试程序 
    {
    	List<int>a1;
    	a1.push_back(1);
    	a1.push_back(2);
    	a1.push_back(3);
    	a1.push_back(4);
    	List<int>::iterator it;
    	for (it = a1.begin(); it != a1.end(); it++) {
    		cout << *it << "\t";
    	}
    	cout << endl;
    	a1.insert(a1.begin(), 0);
    	cout << "在a1[0]处插入0:" << endl;
    	for (it = a1.begin(); it != a1.end(); it++) {
    		cout << *it << "\t";
    	}
    	cout << endl;
    	a1.erase(a1.begin());
    	cout << "在a1[0]处删除0:" << endl;
    	for (it = a1.begin(); it != a1.end(); it++) {
    		cout << *it << "\t";
    	}
    	cout << endl;
    	
    	cout << "调用反向迭代器输出:" << endl;
    	List<int>::reverse_iterator itr;
    	for (itr = a1.rbegin(); itr != a1.rend(); itr++)
    	{
    		cout << *itr << "\t";
    	}
    	cout << endl;
    	a1.erase(a1.begin(), a1.end());
    	cout << "删除全部的元素:" << endl;
    	for (it = a1.begin(); it != a1.end(); it++) {
    		cout << *it << "\t";
    	}
    	cout << endl;
    	system("pause");
    	return 0;
    }
    
    

    运行:

    4、批量增加

    代码:

    //请在DEV中运行,先勾选“Tools" -勾选”Compailer Options"-再勾选"Add~~~“-框框中输入"-std=c++11” 
    #include<iostream> 
    #include <algorithm>
    using namespace std;
    
    template <typename Object>
    class List
    {
      private:    
        // The basic doubly linked list node.
        // Nested inside of List, can be public
        // because the Node is itself private
        struct Node
        {
            Object  data;
            Node   *prev;
            Node   *next;
    
            Node( const Object & d = Object{ }, Node * p = nullptr, Node * n = nullptr )
              : data{ d }, prev{ p }, next{ n } { }
            
            Node( Object && d, Node * p = nullptr, Node * n = nullptr )
              : data{ std::move( d ) }, prev{ p }, next{ n } { }
        };
    
      public:
        class const_iterator
        {
          public:
      
            // Public constructor for const_iterator.
            const_iterator( ) : current{ nullptr }
              { }
    
            // Return the object stored at the current position.
            // For const_iterator, this is an accessor with a
            // const reference return type.
            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;
            }
    
            const_iterator & operator-- ( )
            {
                current = current->prev;
                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;
    
            // Protected helper in const_iterator that returns the object
            // stored at the current position. Can be called by all
            // three versions of operator* without any type conversions.
            Object & retrieve( ) const
              { return current->data; }
    
            // Protected constructor for const_iterator.
            // Expects a pointer that represents the current position.
            const_iterator( Node *p ) :  current{ p }
              { }
            
            friend class List<Object>;
        };
    
        class iterator : public const_iterator
        {
          public:
    
            // Public constructor for iterator.
            // Calls the base-class constructor.
            // Must be provided because the private constructor
            // is written; otherwise zero-parameter constructor
            // would be disabled.
            iterator( )
              { }
    
            Object & operator* ( )
              { return const_iterator::retrieve( ); }
    
            // Return the object stored at the current position.
            // For iterator, there is an accessor with a
            // const reference return type and a mutator with
            // a reference return type. The accessor is shown first.
            const Object & operator* ( ) const
              { return const_iterator::operator*( ); }
            
            iterator & operator++ ( )
            {
                this->current = this->current->next;
                return *this;
            }
    
            iterator operator++ ( int )
            {
                iterator old = *this;
                ++( *this );
                return old;
            }
    
            iterator & operator-- ( )
            {
                this->current = this->current->prev;
                return *this;
            }
    
            iterator operator-- ( int )
            {
                iterator old = *this;
                --( *this );
                return old;
            }
    
          protected:
            // Protected constructor for iterator.
            // Expects the current position.
            iterator( Node *p ) : const_iterator{ p }
              { }
    
            friend class List<Object>;
        };
    
      public:
        List( )
          { init( ); }
    
        ~List( )
        {
            clear( );
            delete head;
            delete tail;
        }
    
        List( const List & rhs )
        {
            init( );
            for( auto & x : rhs )
                push_back( x );
        }
    
        List & operator= ( const List & rhs )
        {
            List copy = rhs;
            std::swap( *this, copy );
            return *this;
        }
    
        
        List( List && rhs )
          : theSize{ rhs.theSize }, head{ rhs.head }, tail{ rhs.tail }
        {
            rhs.theSize = 0;
            rhs.head = nullptr;
            rhs.tail = nullptr;
        }
       
        List & operator= ( List && rhs )
        {    
            std::swap( theSize, rhs.theSize );
            std::swap( head, rhs.head );
            std::swap( tail, rhs.tail );
            
            return *this;
        }
        
        // Return iterator representing beginning of list.
        // Mutator version is first, then accessor version.
        iterator begin( )
          { return iterator( head->next ); }
    
        const_iterator begin( ) const
          { return const_iterator( head->next ); }
    
        // Return iterator representing endmarker of list.
        // Mutator version is first, then accessor version.
        iterator end( )
          { return iterator( tail ); }
    
        const_iterator end( ) const
          { return const_iterator( tail ); }
    
        // Return number of elements currently in the list.
        int size( ) const
          { return theSize; }
    
        // Return true if the list is empty, false otherwise.
        bool empty( ) const
          { return size( ) == 0; }
    
        void clear( )
        {
            while( !empty( ) )
                pop_front( );
        }
     
        // front, back, push_front, push_back, pop_front, and pop_back
        // are the basic double-ended queue operations.
        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 push_front( Object && x )
          { insert( begin( ), std::move( x ) ); }
    
        void push_back( Object && x )
          { insert( end( ), std::move( x ) ); }
    
        void pop_front( )
          { erase( begin( ) ); }
    
        void pop_back( )
          { erase( --end( ) ); }
        void addAll(iterator position, List<Object> & lst)
    	{
    		this->theSize += lst.theSize;
    		iterator end1{ lst.end() };
    		iterator begin1{ lst.begin() };
    		end1--;
    		lst.init();
    		iterator tmp;
    		tmp = position;
    		tmp--;
    		position.current->prev = end1.current;
    		end1.current->next = position.current;
    		tmp.current->next = begin1.current;
    		begin1.current->prev = tmp.current;
    		this->theSize += lst.theSize;
    	}
    
        // Insert x before itr.
        iterator insert( iterator itr, const Object & x )
        {
            Node *p = itr.current;
            ++theSize;
            return iterator( p->prev = p->prev->next = new Node{ x, p->prev, p } );
        }
    
        // Insert x before itr.
        iterator insert( iterator itr, Object && x )
        {
            Node *p = itr.current;
            ++theSize;
            return iterator( p->prev = p->prev->next = new Node{ std::move( x ), p->prev, p } );
        }
        
        // Erase item at itr.
        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 from, iterator to )
        {
            for( iterator itr = from; itr != to; )
                itr = erase( itr );
    
            return to;
        }
    
      private:
        int   theSize;
        Node *head;
        Node *tail;
    
        void init( )
        {
            theSize = 0;
            head = new Node;
            tail = new Node;
            head->next = tail;
            tail->prev = head;
        }
    };
    int main()
    {
    	List<int> a,b;
    	a.push_back(1);
    	a.push_back(2);
    	a.push_back(3);
    	b.push_back(2);
    	
        cout<<"a的长度:"<<a.size()<<endl;
    	cout<<"b的长度:"<<b.size()<<endl;
    	List<int>::iterator it;
    	cout<<"输出a:"<<endl; 
    	for (it = a.begin(); it != a.end(); it++){
    		cout << *it << "\t";
    	}
    	cout << endl;
    	cout<<"输出b:"<<endl;
    	 for (it = b.begin(); it != b.end(); it++){
    		cout << *it << "\t";
    	}
    	cout << endl;
    	cout<<"addAll 操作"<<endl; 
    	a.addAll(a.begin(), b);
    	cout<<"a的长度:"<<a.size()<<endl;
    	cout<<"b的长度:"<<b.size()<<endl;
    	cout<<"输出a:"<<endl; 
        for (it = a.begin(); it != a.end(); it++){
    		cout << *it << "\t";
    	}
    	cout << endl;
    	cout<<"输出b:"<<endl;
    	 for (it = b.begin(); it != b.end(); it++){
    		cout << *it << "\t";
    	}
    	cout << endl;
    	return 0;
    }

    运行:

    5、受限删除

    代码:

    //请在DEV中运行,先勾选“Tools" -勾选”Compailer Options"-再勾选"Add~~~“-框框中输入"-std=c++11” 
    #include<iostream>
    using namespace std;
    
    template <typename Object>
    //定义变量和指针变量 
    struct Node
    {
    	Object data;
    	Node *next;
    	Node(const Object & d = Object{}, Node *n = nullptr) :data(d), next(n)
    	{}
    };
    template <typename Object>
    void pDelete(Node<Object>* ptrX)//定义一个删除函数 
    {
    	Node<Object>*p ;
    	p = ptrX->next;
    	ptrX->next = p->next;
    	ptrX->data = p->data;
    	delete p;
    	p = nullptr;
    
    }
    
    int main()
    {
    	typedef Node<int>node;
    	node *head = new node;
    	node *p, *q;
    	int n = 20;
    	p = head;
    	for (int i = 0; i < n; i++)
    	{
    		q = new node{ i,head };
    		p->next = q;
    		q->next = NULL;
    		p = q;
    	}
    	p = head->next;
    	while (p != nullptr)
    	{
    		cout << p->data << " ";
    		p = p->next;
    	}
    	cout << endl;
    	int m;
    	cout<<"请输出要删除的数据:"<<endl; 
    	cin>>m;
    	node *s=head->next;
    	while(s->data!=m)
    	s=s->next;
    	pDelete(s);
    	p = head->next;
    	while (p != nullptr)
    	{
    		cout << p->data << " ";
    		p = p->next;
    	}
    	
    	return 0;
    
    }
    
    
    

    运行:

    展开全文
  • 数据链表:双向循环链表交换结点

    千次阅读 2019-03-21 17:21:09
    要求:已知p指向双向循环链表中的一个结点,其结点结构为data、prior、next三个域,写出算法change§,交换p所指向的结点和它的前缀结点的顺序。 [题目分析] 知道双向循环链表中的一个结点,与前驱交换涉及到四个结点...

    要求:已知p指向双向循环链表中的一个结点,其结点结构为data、prior、next三个域,写出算法change§,交换p所指向的结点和它的前缀结点的顺序。
    [题目分析]
    知道双向循环链表中的一个结点,与前驱交换涉及到四个结点(p结点,前驱结点,前驱的前驱结点,后继结点)六条链。
    在这里插入图片描述
    算法描述:

    void change(LinkedList p)
    {//p是双向循环链表中的一个结点,将p所指结点与其前驱结点交换
    	q = p->pre;
    	q->pre->next = p;		∥p的前驱的前驱之后继为p
    	p = ->pre = q->pre;		∥p的前驱指向其前驱的前驱。
    	q->next = p->next;		∥p的前驱的后继为p的后继。
    	q->pre = p;				∥p与其前驱交换
    	p->next->pre = q;		∥p的后继的前驱指向原p的前驱
    	p->next = q;			∥p的后继指向其原来的前驱
    }
    
    展开全文
  • 双向链表的节点交换

    千次阅读 2019-09-19 17:25:28
    这两天关于双向链表的节点的交换,用了很长的时间去学习,理解,敲了好多次,总是达不到效果,就是能想明白,但是在写的时候不是出现这样就是那样的问题,最后参照另外一位CSDN博主的帖子,才写出一个完整的函数, ...

    这两天关于双向链表的节点的交换,用了很长的时间去学习,理解,敲了好多次,总是达不到效果,就是能想明白,但是在写的时候不是出现这样就是那样的问题,最后参照另外一位CSDN博主的帖子,才写出一个完整的函数,

    贴上代码,以便自己以后随时查看,没有图.

    void swap(USERINFO *head,USERINFO *left,USERINFO *right)
    {
    	USERINFO *temp;
    	if (right->next == NULL) //t结点是否为尾结点
    	{
    		if (left->next == right) //p,t结点是否相邻
    		{
    			//与尾结点相邻的交换代 
    			right->next = left;
    			right->pre = left->pre;
    			left->next = NULL;
    			left->pre->next = right;
    			left->pre = right;
    		}
    		else
    		{
    			//与尾结点不相邻的交换代 
    			right->next = left->next;
    			right->pre->next = left;
    			temp = right->pre;
    			right->pre = left->pre;
    			left->next->pre = right;
    			left->next = NULL;
    			left->pre->next = right;
    			left->pre = temp;
    		}
    	}
    	else
    	{
    		if (left->next == right) //p,t结点是否相邻
    		{
    			//相邻的交换代 
    			right->next->pre = left;
    			temp = right->next;
    			right->next = left;
    			right->pre = left->pre;
    			left->next = temp;
    			left->pre->next = right;
    			left->pre = right;
    		}
    		else
    		{
    			//不相邻的交换代 
    			right->next->pre = left;
    			temp = right->next;
    			right->next = left->next;
    			left->next->pre = right;
    			left->next = temp;
    			right->pre->next = left;
    			temp = right->pre;
    			right->pre = left->pre;
    			left->pre->next = right;
    			left->pre = temp;
    		}
    	}
    }
    

     

    展开全文
  • //双向链表初始化 Status ListInsert_DuL(DuLinkList &L,int i,ElemType e); //双向链表插入; void CreateList_DuL(DuLinkList &L,int n); //创建含有n个元素的双向链表; void ShowList_DuL(DuLinkList L); //...
  • 双向链表的定义上一节学习了单向链表单链表详解。今天学习双链表。学习之前先对单向链表和双向链表做个回顾。单向链表特点:  1.我们可以轻松的到达下一个节点, 但是回到前一个节点是很难的.   2.只能从头遍历到...
  • 双向链表中的交换节点

    千次阅读 2016-03-13 11:26:25
    双向链表交换两个节点的值 struct node* temp; //定义一个中间结构体存储q->last和p->next temp->last=q->last; temp->next=p->next; p->next=q->next; q->last->next=p; p->last->next=q; q->next=...
  • // 双向链表排序.cpp : 定义控制台应用程序的入口点。//#include &ltstdio.h&gt#include &ltstdlib.h&gt#include &ltstring.h&gt#define len sizeof(struct node)struct node{int data;...
  • 利用双向循环链表表示一个整数序列,指定一个结点位置用p指向该结点,交换p所指向的结点及其前驱结点的顺序。 输入 多组数据,每组数据有三行,第一行为链表的长度n,第二行为链表的n个元素(元素之间用空格分隔)...
  • 双向链表的排序

    千次阅读 2008-07-28 16:44:00
    以前写过双向链表交换任意结点的程序,后来写了个双向链表排序的程序,没用边输入边排序的思想,是输入完毕后我按照选择法排序的思想对链表的结点进行交换.是地址交换.#include typedef struct Link/*双向链表结构体*/{...
  • 插入排序需要从后往前遍历寻找可以插入的位置,所以会使用到双向链表 typedef struct Node//定义的结构体 { int data; struct Node* per; //记录前驱 struct Node* next; }*List; 创建带头节点的双链表 List ...
  • 高分悬赏:Java语言怎么交换两个双向链表,首先请给出链表的设计类,然后再交换
  • 双向链表反转

    千次阅读 2019-06-10 19:10:31
    本文中的双向链表,具有一个首指针h,但没有尾指针,不是循环链表。链表反转时,要做两件事情,一是将数据部分的pre指针和next指针交换值;二是将h指针指向反转后的头数据节点指针,并将新链表的尾数据节点指针的...
  • 根据题目建立好该双向链表(尾插法),然后用个指针依次查找,先从第一个节点往后走,找出最大节点max,再将max和最后一个元素交换,第一遍结束(即奇数遍正向查找);第二遍,从最后一个节点开始倒着往前找,找到...
  • 双向链表的C++代码实现(1)数据节点的代码编写(2)迭代器(3)链表初始化(4)获取节点(5)插入和删除节点(6)链表的交换与合并3.完整代码4.简单的测试代码 1.什么是双向链表 这里既然要实现双向链表,就首先要知道双向...
  • 头文件: #include class ListNode { public: ListNode* next; ListNode* pre; int val; ListNode(int x):val(x),next(NULL),pre(NULL){}; }; 源文件: #include "ListNode.h" ...using namespac
  • 双向链表前后项交换是不是这样的过程? 后项->临时交换, 前项->后项, 临时交换->前项 pTmpChg->CurItem = pList->CurItem; //后项->临时交换 pList->CurItem->next = pList->...
  • Boxes in a Line ACM汇总学习:...题目:模拟链表4中操作,x左插入y,x右插入y,x交换y,翻转链表,计算链表中奇数位置的和 解法:使用两个数组模拟双向链表,翻转操作不用真的翻转,...
  • 双向链表两个结点互换位置(链表–数据结构) 双向链表两个结点互换位置 /*struct Node { int value; Node *next; Node *prev; };*/ /*交换 buffer 和 i 两个结点(buffer在i右端)i---->buffer */ Node *temp; ...
  • 双向链表的选择排序算法

    千次阅读 2014-12-16 18:27:31
    前日遇到一个问题:对双向链表按关键字域进行排序。  在网上找了一下,都只一种算法,而且...于是自己想了一下,写了个带头结点的双向链表的选择排序算法,指针的交换浓缩到4种情况,而且自认为选择排序函数中的结
  • 两种方式实现双向链表的反转

    千次阅读 2019-08-26 18:42:28
    双向链表的反转 双向链表的反转,两种方式实现,1、利用指针遍历的方式实现;2、不采用指针,直接用数据交换的方式进行反转。 public class shuangxianglianbaiofanzhuan { public static void main(String[] args)...

空空如也

空空如也

1 2 3 4 5 ... 13
收藏数 251
精华内容 100
关键字:

双向链表交换