//*********************************** //功能:按元素位序的奇偶分割单链表,利用原结点的内存空间,保持原相对次序 //日期:2017年10月10日 //作者:Ryan2019 //*********************************** #include using namespace std; typedef int LElemType; typedef struct LNode { LElemType data; LNode *next; }* LList; void ListCreate(LList &L,int n,LElemType a[]);//创建链表 void Listshow(LList &L);//显示链表 void Divide(LList &L,LList &A,LList &B);//分割单链表 int main() { LList A,B,L; int m=6; LElemType a[6]={1,2,3,4,5,6}; for(int i=0;i<=m;i++) { ListCreate(L,i,a); cout<<"原链表为:";Listshow(L); Divide(L,A,B); cout<<"分割后的单链表A为:"; Listshow(A); cout<<"分割后的单链表B为:"; Listshow(B); } return 0; } void ListCreate(LList &L,int n,LElemType a[]) { LList p; int i; L=new LNode; L->next=NULL; for(i=n-1;i>=0;i--) { p=new LNode; p->data=a[i]; p->next=L->next; L->next=p; } } void Listshow(LList &L) { LList p; for(p=L->next;p;p=p->next) { cout<data<<" "; } cout<next || !L->next->next) { A=L; B->next=NULL; return; } LList p,q,r; p=L->next; q=p->next; B->next=q; while(p && q && p->next && q->next) { p->next=q->next; r=q; p=p->next; q=p->next; r->next=q; } p->next=NULL; if(!q) { r->next=NULL; } A=L; }