原题链接
考察链表创建,链表删除
#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 *l=NULL,*r=NULL,*s;
int x;
while (scanf ("%d",&x),x!=-1)
{
s = (struct ListNode *)malloc(sizeof (struct ListNode));
s->data = x;
if (r)
{
r->next = s;
r = s;
}
else
l = r = s;
}
r->next = NULL;
return l;
}
struct ListNode *getodd( struct ListNode **L )
{
struct ListNode *st1=NULL,*ed1=NULL,*st2=NULL,*ed2=NULL,*l = *L;
while (l!=NULL)
{
if (l->data%2!=0)
{
if (ed1==NULL)
{
st1 = l;
ed1 = l;
}
else
{
ed1->next = l;
ed1 = l;
}
}
else
{
if (ed2==NULL)
{
st2 = l;
ed2 = l;
*L = l;
}
else
{
ed2->next = l;
ed2 = l;
}
}
l = l->next;
}
if (ed1) ed1->next = NULL;
if (ed2) ed2->next = NULL;
else *L = NULL;
return st1;
}