#include<stdio.h>
#include<stdlib.h>
struct Node{
int Data;
struct Node *Next;
};
void insert_tail(struct Node *L,int num){/*尾插法*/
struct Node *p,*q;
p=L;
while(p->Next!=NULL){
p=p->Next;
}
q=(struct Node *)malloc(sizeof(struct Node));
q->Data=num;
q->Next=p->Next;
p->Next=q;
}
void show(struct Node *L){
struct Node *p;
p=L;
while(p->Next!=NULL){
p=p->Next;
printf("%d\t",p->Data);
}
printf("\n");
}
struct Node *Split(struct Node *L){/*正负链表分离*/
struct Node *p,*q,*Neg,*Np;
Neg=(struct Node *)malloc(sizeof(struct Node));
Neg->Next=NULL;
p=L;
Np=Neg;
while(p!=NULL){
while(p->Next!=NULL&&p->Next->Data<0){
q=p->Next;
p->Next=q->Next;
Np->Next=q;
q->Next=NULL;
Np=Np->Next;
}
p=p->Next;
}
return Neg;
}
int main(){
struct Node *L,*Neg;
L=(struct Node *)malloc(sizeof(struct Node));
L->Next=NULL;
int i;
for(i=1;i<=6;i++){
insert_tail(L,-i);
insert_tail(L,i);
}
show(L);/*打印原有链表*/
Neg=Split(L);
show(L);/*打印分离后的正链表*/
show(Neg);/*打印分离后的负链表*/
return 0;
}
05-22
1887