分解链表
来源:互联网 发布:偶像梦幻祭卡牌数据 编辑:程序博客网 时间:2024/04/19 12:55
编写程序,将单链表A分解成两个单链表A和B,其头指针分别为head和head1,使得A链表中含原链表A中序号为奇数的元素,而B链表中含原链表A中序号为偶数的元素,且保持原来的相对顺序。
#include<stdio.h>
#include<stdlib.h>
//#include<malloc.h>
struct node
{
int data;
struct node *next;
}*head, *head1;
int main()
{
int i;
struct node *p, *q;
head=NULL;
for(i=0;i<10;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=i+1;
p->next=head;
head=p;
}
p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
p=head;
q=p->next;
head1=q;
while(p!=NULL)
{
p->next=q->next;
p=p->next;
if(p!=NULL)
{ q->next=p->next;
q=q->next;
}
else
break;
}
q->next=NULL;
p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
p=head1;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
system("pause");
return 0;
}
/*
#include<stdio.h>
#include<stdlib.h>
//#include<malloc.h>
struct node
{
char data;
struct node *link;
}*head;
void ins(struct node *q)
{
if(head==NULL)
{
q->link=NULL;
head=q;
}
else
{
q->link=head;
head=q;
}
}
int main()
{
char ch;
struct node *p;
head=NULL;
while((ch=getchar())!='\n')
{
p=(struct node *)malloc(sizeof(struct node));
p->data=ch;
ins(p);
}
p=head;
while(p!=NULL)
{
printf("%c",p->data);
p=p->link;
}
printf("\n");
system("pause");
return 0;
}
*/
/*
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
int i,j;
if((fp=fopen("zheng.dat","wb"))==NULL)
exit(0);
for(i=0;i<5;i++)
{
scanf("%d",&j);
fwrite(&j,sizeof(int),1,fp);
}
fclose(fp);
system("pause");
return 0;
}
*/
若n为奇数
#include<stdio.h>
#include<stdlib.h>
//#include<malloc.h>
struct node
{
int data;
struct node *next;
}*head, *head1;
int main()
{
int i,n;
struct node *p, *q;
head=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=i+1;
p->next=head;
head=p;
}
p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
p=head;
q=p->next;
head1=q;
while(p!=NULL)
{
p->next=q->next;
p=p->next;
if(p->next!=NULL)
{
q->next=p->next;
q=q->next;
}
else
break;
// }
// else
//break;
}
q->next=NULL;
p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
p=head1;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
system("pause");
return 0;
}
若n为偶数
#include<stdio.h>
#include<stdlib.h>
//#include<malloc.h>
struct node
{
int data;
struct node *next;
}*head, *head1;
int main()
{
int i,n;
struct node *p, *q;
head=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=i+1;
p->next=head;
head=p;
}
p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
p=head;
q=p->next;
head1=q;
while(p!=NULL)
{
p->next=q->next;
p=p->next;
if(q->next!=NULL)
{
q->next=p->next;
q=q->next;
}
else
break;
// }
// else
//break;
}
q->next=NULL;
p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
p=head1;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
system("pause");
return 0;
}
编写程序,将单链表A分解成两个单链表A和B,其头指针分别为head和head1,使得A链表中含原链表A中序号为奇数的元素,而B链表中含原链表A中序号为偶数的元素,且保持原来的相对顺序。(n不分奇数与偶数)
#include<stdio.h>
#include<stdlib.h>
//#include<malloc.h>
struct node
{
int data;
struct node *next;
}*head, *head1;
int main()
{
int i,n;
struct node *p, *q;
head=NULL;
scanf("%d",&n);
for(i=0;i<n;i++)
{
p=(struct node*)malloc(sizeof(struct node));
p->data=i+1;
p->next=head;
head=p;
}
p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
p=head;
q=p->next;
head1=q;
while(p!=NULL)
{
p->next=q->next;
p=p->next;
if((p!=NULL)&&(p->next!=NULL))
{
q->next=p->next;
q=q->next;
}
else
break;
}
q->next=NULL;
p=head;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
p=head1;
while(p!=NULL)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");
system("pause");
return 0;
}
- 分解链表
- 分解链表
- 分解链表,la表中存放字母,lb中存放数字,lc中存放其它字符
- 求解链表不能正常输出
- 一份源代码 直观了解链表的功能
- 让你更好的了解链表-链表解决猴子,小朋友报数问题
- 分库 分表
- 分表
- 分表
- sql分库、分表
- 数据库分库、分表
- 分表和分库
- 初始分库--分表
- mysql 分表,分库
- 数据库分库、分表
- mysql分库 分表
- 13.4.17 链接/解链的实现(文件共享)
- mysql-水平分表-垂直分表
- SourceInsight中文字体
- windows 7下安装Android模拟器
- 蓝莓
- ural 1057 Amount of Degrees(数位dp)
- Linux命令学习手册-crontab命令
- 分解链表
- Java小程序:十进制转二进制
- python中的条件和循环
- Property Animation要点总结
- QT事件处理(二) 之 事件过滤器
- 看90后开发的游戏如何闯进AppStore前十
- [Erlang 0100] make_ref 与 Selective Receive
- 高性能、高流量Java Web站点打造的22条建议
- Block的学习记录