#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int data;
struct ListNode *next;
};
struct ListNode *readlist();
struct ListNode *getodd(struct ListNode **L);
void printlist(struct ListNode *L)
{
struct ListNode *p = L;
while (p)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
struct ListNode *L, *Odd;
L = readlist();
Odd = getodd(&L);
printlist(Odd);
printlist(L);
return 0;
}
struct ListNode *readlist()
{
struct ListNode *h, *p, *pre;
int da, fst = 1;
while (~scanf("%d", &da) && da != -1)
{
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = da;
if (!fst)
pre->next = p, pre->next->next = NULL, pre = pre->next;
if (fst)
pre = h = p, fst = 0;
}
return h;
}
struct ListNode *getodd(struct ListNode **L)
{
struct ListNode *p, *pre1, *pre2, *h1 = NULL, *h2 = NULL;
int f1 = 1, f2 = 1, da;
while (*L)
{
da = (*L)->data;
if (da % 2)
{
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = da;
if (!f1)
pre1->next = p, pre1->next->next = NULL, pre1 = pre1->next;
if (f1)
pre1 = h1 = p, f1 = 0;
}
else
{
p = (struct ListNode *)malloc(sizeof(struct ListNode));
p->data = da;
if (!f2)
pre2->next = p, pre2->next->next = NULL, pre2 = pre2->next;
if (f2)
pre2 = h2 = p, f2 = 0;
}
*L = (*L)->next;
}
*L = h2;
return h1;
}
浙大版《C语言程序设计(第3版)》题目集 习题11-7 奇数值结点链表 (20分)
猜你喜欢
转载自blog.csdn.net/qq_44458489/article/details/105376885
今日推荐
周排行