#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
struct LNode *next;
int data;
}LNode;
//创建链表
struct LNode *Creatlist(){
int x;
LNode *head,*p,*s;
head=(LNode *)malloc(sizeof(LNode));
p=head;
printf("请输入X的值:");
scanf("%d",&x);
while(1){
if(x!=-1){
s=(LNode *)malloc(sizeof(LNode));
s->data=x;
p->next=s;
p=s;
printf("继续输入x的值:");
scanf("%d",&x);
}
else{
break;
}
}
p->next=NULL;
return head;
}
void printList(LNode *head){
LNode *p=head->next;
while(p){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
void Splitlist(LNode *head){
//创建B,C链表头指针
LNode *B=(LNode *)malloc(sizeof(LNode));
LNode *C=(LNode *)malloc(sizeof(LNode));
B->next=NULL;
C->next=NULL;
LNode *p=B,*q=C;
LNode *r=head->next,*s;
while(r){
if(r->data>0){
s=r->next;
q->next=r;
q=r;
r=s;
}
else if(r->data<0){
s=r->next;
p->next=r;
p=r;
r=s;
}
else{
s=r->next;
r=s;
}
}
p->next=NULL;
q->next=NULL;
printList(B);
printList(C);
}
int main(){
LNode *head1;
head1=Creatlist();
Splitlist(head1);
return 0;
}
带头结点的链表拆分
最新推荐文章于 2024-11-09 14:41:27 发布