#include <stdio.h>
#include <stdlib.h>
using namespace std;
typedef struct LinkedList{
int data;
struct LinkedList *next;
}LinkedList,*List;
int a[10]={1,2,3,4,5,6,7,8,9,10};
int n=10;
void build(List &L){
L=(List)malloc(sizeof(LinkedList));
LinkedList *r=L;
LinkedList *s;
for(int i=0;i<10;i++){
s=(LinkedList *)malloc(sizeof(LinkedList));
s->data=a[i];
r->next=s;
r=r->next;
}
r->next=NULL;
}
void show(List &L){
LinkedList *s=L->next;
while(s){
printf("%d ",s->data);
s=s->next;
}
}
void devide(List &L,List &B){
B=(List)malloc(sizeof(LinkedList));
B->next=NULL;
LinkedList *b=B;
LinkedList *s=L;
LinkedList *r=L->next;
LinkedList *r2=r->next;
L->next=NULL;
int i=0;
while(r){
i++;
if(i%2==0){//采用头插法
r->next=b->next;
b->next=r;
}else{//采用尾插法
s->next=r;
s=s->next;
}
r=r2;
if(r!=NULL){//这里需要判断一下r的位置如果r已经是空的了那就不要再把r2给r赋值了不然在执行
//头插法的时候头结点就执行空了
r2=r2->next;
}
}
s->next=NULL;
//这里B的指针不要置空因为是头插法
}
int main(){
List L,B;
build(L);
show(L);
printf("\n");
devide(L,B);
show(L);
printf("\n");
show(B);
return 0;
}