楼主你好
具体代码如下:
#include
#include
typedef struct node
{
int data;//数据域
struct node *next;//指针域
}L;
void Initial_List(L * &h)
{
L *p;//用于插入
L *r;//尾结点
h=(L *)malloc(sizeof(L));
h->next = NULL;
r=h;//尾结点 开始指向头结点
printf("输入数据(空格间隔数据 回车结束):\n");//尾插入结点
do{
p=(L *)malloc(sizeof(L));
scanf("%d",&(p->data));
r->next = p;
r = p;
}while(getchar()!='\n');
r->next = NULL;
}
void Disp_List(L * h)
{
L *p=h->next;
if(!p)
{
printf("空!\n");
return;
}
while(p)
{
printf("%-3d",p->data);
p=p->next;
}
printf("\n");
}
void Separate(L * &l1,L * &l2,L *l3)//将链表l3分为l1和l2 l1保存奇数号结点 l2保存偶数号结点
{
int flag=0;//结点号标记
L *p = l3->next;//用于遍历链表l3
L *r1,*r2,*x,*y;
l1=(L *)malloc(sizeof(L));
l2=(L *)malloc(sizeof(L));
r1=l1;
r2=l2;
l1->next = l2->next =NULL;
while(p)
{
flag++;
if(flag%2 == 1)
{
x=(L *)malloc(sizeof(L));
x->data = p->data;
r1->next = x;
r1 = x;
}
else
{
y = (L *)malloc(sizeof(L));
y->data = p->data;
r2->next = y;
r2 = y;
}
p=p->next;
}
r1->next = r2->next = NULL;
}
int main()
{
L *l1,*l2,*l3;
Initial_List(l3);
Separate(l1,l2,l3);
printf("l3:\n");
Disp_List(l3);
printf("l1:\n");
Disp_List(l1);
printf("l2:\n");
Disp_List(l2);
return 0;
}
输入示例:
输入数据:
1 2 3 4 5 6
l3:
1 2 3 4 5 6
l1:
1 3 5
l2:
2 4 6
希望能帮助你哈
最好你反LinkList的所有方法者贴出来,因为要用到它。