#include<stdio.h>
struct node{
int data;
struct node *next;
}*h1,*h2,*las,*p,*H1,*H2;
int x;
void insert(struct node *now,int x){
struct node *p;
for (;now->next && x>now->next->data;now=now->next);
p=(struct node*)malloc(sizeof(struct node));
p->next=now->next;
now->next=p;
p->data=x;
}
void print(struct node *now){
las=now;
now=now->next;
if (now) printf("%d",now->data);
else{
puts("");
return;
}
for (las=now,now=now->next;now;las=now,now=now->next)
printf("->%d",now->data);
puts("");
return;
}
void destroy(struct node *now){
if (!now) return;
struct node *las=now;
for (now=now->next;now;free(las),las=now,now=now->next);
free(las);
return;
}
struct node *reverse(struct node *head){
struct node *p,*q;
q=(struct node*)malloc(sizeof(struct node));
q=NULL;
for (;head;head=head->next){
p=(struct node*)malloc(sizeof(struct node));
p->data=head->data;
p->next=q;
q=p;
}
p=(struct node*)malloc(sizeof(struct node));
p->next=q,q=p;
return q;
}
void split(struct node **h1,struct node **h2){
struct node*las=*h1;
*h1=(*h1)->next;
for (;*h1;las=*h1,*h1=(*h1)->next)
if (!((*h1)->data&1)){
(*h2)->next=(struct node*)malloc(sizeof(struct node));
*h2=(*h2)->next;
(*h2)->data=(*h1)->data;
(*h2)->next=NULL;
las->next=(*h1)->next;
free(*h1);
*h1=las;
}
}
int main(){
h1=(struct node*)malloc(sizeof(struct node));
h2=(struct node*)malloc(sizeof(struct node));
h1->next=h2->next=NULL;
while (~scanf("%d",&x)) insert(h1,x);
print(h1);
H1=h1,H2=h2;
split(&h1,&h2);
print(H1);
print(H2);
destroy(H1);
destroy(H2);
// p=reverse(h1->next); next不能忘啊!!
}
PTA作业10单链表7-4
最新推荐文章于 2023-08-18 22:59:13 发布