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;
-