java单链表的奇偶拆分_【把一个单链表奇偶性拆成两个链表,原链表不变。】作业帮...

# include# define SIZE sizeof(struct student )

struct student

{

int data;

struct student * next;

};

struct student * creatlist()

{

struct student * head;

struct student * p1;

struct student * p2;

head = (struct student *)malloc(SIZE);

head->next = NULL;

p2 = head;

p1 = (struct student *)malloc(SIZE);

scanf("%d",&p1->data);

while(p1->data!=0)

{

p1->next = p2->next;

p2->next = p1;

p2 = p2->next;

p1 = (struct student *)malloc(SIZE);

scanf("%d",&p1->data);

}

p2->next = NULL;

free(p1);

return head;

}

struct student *creatlistjs(struct student * mhead)

{

struct student * head;

struct student * p1;

struct student * p2;

struct student * p3;

struct student * p4;

head =(struct student *)malloc(SIZE);

head->next = NULL;

p1 =(struct student *)malloc(SIZE);

p4 = head;

p3 = mhead;

p2 = mhead->next ;

while(p2!=NULL)

{

p1 = p3;

p1->next = p4->next ;

p4->next = p1;

p4 = p4->next ;

p3 = p3->next ;

p3 = p3->next ;

p2 = p2->next ;

p2 = p2->next ;

}

p4->next = NULL;

free(p1);

return head;

}

struct student * creatlistos(struct student *mhead)

{

struct student * head;

struct student * p1;

struct student * p2;

struct student * p3;

struct student * p4;

head =(struct student *)malloc(SIZE);

head->next = NULL;

p1 =(struct student *)malloc(SIZE);

p4 = head;

p3 = mhead;

p2 = mhead->next ;

while(p2!=NULL)

{

p1 = p2;

p1->next = p4->next ;

p4->next = p1;

p4 = p4->next ;

p3 = p3->next ;

p3 = p3->next ;

p2 = p2->next ;

p2 = p2->next ;

}

p4->next = NULL;

free(p1);

return head;

}

void printlist(struct student *head)

{

struct student * phead;

phead = head->next ;

while(phead)

{

printf("%4d",phead->data);

phead = phead->next;

}

}

main()

{

struct student * mhead;

struct student * jshead;

struct student * oshead;

mhead = creatlist();

printlist(mhead);

jshead = creatlistjs(mhead);

printlist(jshead);

oshead = creatlistos(mhead);

printlist(oshead);

}

大体思路对了,具体的实现还有点问题,楼主可以自己改改

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值