链表-结点间交换
来源:互联网 发布:ec6108v9 安装软件 编辑:程序博客网 时间:2024/06/06 00:29
//节点交换 定向排序
//环形链表 双向链表
//中间 首位 前面两个节点间的交换
#include<iostream>
using namespace std;
struct node
{
int data;
node*pr;
node*next;
} ;
node*Creatnode(int d)
{
node*p;
p=new node;
p->data=d;
p->pr=p->next=NULL;
return p;
}
void Display1(node* head)
{
node*p=head;
while(p)
{
cout<<p->data<<",";
p=p->next;
}
cout<<"\n";
}
void Display2(node* tail)
{
node*p=tail;
while(p)
{
cout<<p->data<<",";
p=p->pr;
}
cout<<"\n";
}
void sort(node* head)
{
node*q,*p=head;
while(p)
{
q=p->next;
if(p==NULL)
{
cout<<"NULL list"<<endl;
}
while(q)
{
if(p->data>q->data)
{
int n=p->data;
p->data=q->data;
q->data=n;
}
q=q->next;
}
p=p->next;
}
}
void SwapHead(node*head)
{
node*p;
p=head;
if(p==NULL)
{
cout<<"NULL List"<<endl;
}
if(p==head)
{
int n=p->data;
p->data=p->next->data;
p->next->data=n;
}
}
void SwapTail(node*tail)
{
node*p=tail;
if(p==NULL)
{
cout<<"NULL List"<<endl;
}
if(p==tail)
{
int n=p->data;
p->data=p->pr->data;
p->pr->data=n;
}
}
void SwapInside(node* head)
{
node*p=head;
while(p)
{
if(p->data==9)
{
int n=p->data;
p->data=p->pr->data;
p->pr->data=n;
}
p=p->next;
}
}
void SwapHT(node*head,node*tail)
{
int n=head->data;
head->data=tail->data;
tail->data=n;
}
int main(void)
{
node*head,*tail,*p,*q;
head=tail=Creatnode(0);
int i,a[6]={1,9,2,6,7,3};
for(i=0;i<6;i++)
{
p=Creatnode(a[i]);
p->next=head;
head->pr=p;
head=p;
}
//正向输出
cout<<"follw direction output"<<endl;
Display1(head);
//反向输出
cout<<"opposite direction output"<<endl;
Display2(tail);
//cout<<"the sorted arry"<<endl; //整体排序
//sort(head);
//Display1(head);
//前两位交换
cout<<"swap between the prior two"<<endl;
SwapHead(head);
Display1(head);
//末尾两位交换
cout<<"swap between the Last two"<<endl;
SwapTail(tail);
Display1(head);
// 其中两位交换
cout<<"swap between the inside two"<<endl;
SwapInside(head);
Display1(head);
//首位交换
cout<<"swap between Head and Tail"<<endl;
SwapHT(head,tail);
Display1(head);
return 0;
}
//环形链表 双向链表
//中间 首位 前面两个节点间的交换
#include<iostream>
using namespace std;
struct node
{
int data;
node*pr;
node*next;
} ;
node*Creatnode(int d)
{
node*p;
p=new node;
p->data=d;
p->pr=p->next=NULL;
return p;
}
void Display1(node* head)
{
node*p=head;
while(p)
{
cout<<p->data<<",";
p=p->next;
}
cout<<"\n";
}
void Display2(node* tail)
{
node*p=tail;
while(p)
{
cout<<p->data<<",";
p=p->pr;
}
cout<<"\n";
}
void sort(node* head)
{
node*q,*p=head;
while(p)
{
q=p->next;
if(p==NULL)
{
cout<<"NULL list"<<endl;
}
while(q)
{
if(p->data>q->data)
{
int n=p->data;
p->data=q->data;
q->data=n;
}
q=q->next;
}
p=p->next;
}
}
void SwapHead(node*head)
{
node*p;
p=head;
if(p==NULL)
{
cout<<"NULL List"<<endl;
}
if(p==head)
{
int n=p->data;
p->data=p->next->data;
p->next->data=n;
}
}
void SwapTail(node*tail)
{
node*p=tail;
if(p==NULL)
{
cout<<"NULL List"<<endl;
}
if(p==tail)
{
int n=p->data;
p->data=p->pr->data;
p->pr->data=n;
}
}
void SwapInside(node* head)
{
node*p=head;
while(p)
{
if(p->data==9)
{
int n=p->data;
p->data=p->pr->data;
p->pr->data=n;
}
p=p->next;
}
}
void SwapHT(node*head,node*tail)
{
int n=head->data;
head->data=tail->data;
tail->data=n;
}
int main(void)
{
node*head,*tail,*p,*q;
head=tail=Creatnode(0);
int i,a[6]={1,9,2,6,7,3};
for(i=0;i<6;i++)
{
p=Creatnode(a[i]);
p->next=head;
head->pr=p;
head=p;
}
//正向输出
cout<<"follw direction output"<<endl;
Display1(head);
//反向输出
cout<<"opposite direction output"<<endl;
Display2(tail);
//cout<<"the sorted arry"<<endl; //整体排序
//sort(head);
//Display1(head);
//前两位交换
cout<<"swap between the prior two"<<endl;
SwapHead(head);
Display1(head);
//末尾两位交换
cout<<"swap between the Last two"<<endl;
SwapTail(tail);
Display1(head);
// 其中两位交换
cout<<"swap between the inside two"<<endl;
SwapInside(head);
Display1(head);
//首位交换
cout<<"swap between Head and Tail"<<endl;
SwapHT(head,tail);
Display1(head);
return 0;
}
1 0
- 链表-结点间交换
- 交换链表结点
- 交换二叉树的左右子结点
- Swap Nodes in Pairs 成对交换结点
- 两两交换链表的结点
- 成对交换双向链表中的结点
- 两两交换链表中的结点
- 交换二叉树的左右结点
- 链表--结点
- 226.Invert Binary Tree(交换二叉树左右结点)
- java 交换链表中所有相邻结点的顺序
- (LeetCode)Swap Nodes in Pairs --- 交换两个相邻的结点
- leetcode_24. Swap Nodes in Pairs 单链表中成对交换结点位置
- 【数据结构_树_Tree_1105】交换二叉树的孩子结点
- LeetCode-99-Recover Binary Search Tree 二叉树交换结点
- 《数据结构》交换双向循环链表的结点p和它的前驱结点
- 结点 链表 练习
- 拷贝二叉树;交换二叉树中所有结点的左、右子树
- Android开发效率—Eclipse快捷键
- uploadify的使用
- 37 Spark之Task执行原理及结果
- mysql出现innodb缺失错误 #1286
- 拼音库
- 链表-结点间交换
- 适配器模式(Adapter):类适配器、对象适配器
- SpringMVC中使用Interceptor拦截器
- 神兵利器——Alfred
- Androin学习笔记二十五:InetAddress(示例,出错代码)
- [LeetCode] Intersection of Two Arrays
- 网络基础知识
- 【Java工具类】 POI操作word文档模版可修改文字图片
- SQL语句中case when的使用