单链表的链表拆分
来源:互联网 发布:菜鸟网络cfo 编辑:程序博客网 时间:2024/05/22 04:28
1.定义三的指针变量*p、*q、*t,p指向原链表的头结点head1,新建另一个头结点head2,q指向head2,t指向head1的next结点,两个头结点的next都设为空。
2.按照条件分配t指向的结点,如果将这个结点连接到head1的链表中,(1)让p的next指向t(2)p和t都向后移,既p指向p的next,t指向t的next。(3)再让p的next为空。
//链表拆分#include <iostream>#include<stdlib.h>using namespace std;struct node{ int x; struct node *next;};//顺序建表struct node *creatListShun(int lenth){ struct node *head,*t,*p; int i; head=(struct node *)malloc(sizeof(struct node)); head->next=NULL; t=head; for(i=0; i<lenth; i++) { p=(struct node *)malloc(sizeof(struct node)); p->x=i; p->next=NULL; t->next=p; t=p; } return head;}struct node * chaifen(struct node *head1){ struct node *p,*q,*t,*head2; t=head1->next; head1->next=NULL; head2=(struct node *)malloc(sizeof(struct node)); head2->next=NULL; p=head1; q=head2; while(t) { if(t->x%2==0) { p->next=t; t=t->next; p=p->next; p->next=NULL; } else { q->next=t; t=t->next; q=q->next; q->next=NULL; } } return head2;};int main(){ struct node *head1,*t1,*head2,*t2; head1=creatListShun(10);//顺序建链表 t1=head1->next; cout<<"顺序建链表 :"; while(t1!=NULL) { cout<<t1->x<<" "; t1=t1->next; } cout<<endl; //链表拆分 cout<<"链表拆分"<<endl; head2=chaifen(head1); t1=head1->next; while(t1) { cout<<t1->x<<" "; t1=t1->next; } cout<<endl; t2=head2->next; while(t2) { cout<<t2->x<<" "; t2=t2->next; } cout<<endl; return 0;}
0 0
- 单链表的链表拆分
- 链表的拆分
- 有序链表的拆分
- 数据结构——链表之单链表的拆分
- 表的垂直拆分和水平拆分
- 表的垂直拆分和水平拆分
- 表的垂直拆分和水平拆分
- 表的垂直拆分和水平拆分
- ytu 2213 链表的拆分
- 进阶项目12-链表的拆分
- 单链表的拆分
- 单链表的拆分
- 单链表的拆分
- 单链表的拆分
- 单链表的拆分
- 单链表的拆分
- 数据库表的拆分
- 拆分链表
- Python-被调用函数中获取调用函数信息(转)
- poj 1330 Nearest Common Ancestors
- 数据结构实验之图论四:迷宫探索
- java反射机制简单认知
- 父类引用指向子类对象
- 单链表的链表拆分
- 继承执行顺序
- js正则表达式学习笔记
- JDK配置环境变量要点
- 从零学React Native之04自定义对话框
- 动态规划(经典背包问题)
- Activity的生命周期
- 视频参数(流媒体系统,封装格式,视频编码,音频编码,播放器)对比
- java.util.Collections.synchronizedMap()方法实例