数据结构(五)单向/双向链表交换元素、反向迭代器批量增加/受限删除

本文详细介绍了单向和双向链表中如何交换相邻元素,利用反向迭代器进行批量增加操作,以及在特定条件下进行受限删除。通过实例代码演示了每一步的操作,并附带了运行结果。
摘要由CSDN通过智能技术生成

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;
			-
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值