69,373
社区成员
发帖
与我相关
我的任务
分享
#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()
{
int num;
struct ListNode *p,*head,*tail;
head=tail=NULL;
while(scanf("%d",&num)){
if(num==-1)break;
p=(struct ListNode *)malloc(sizeof(struct ListNode));
p->data=num;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
}
return head;
}
struct ListNode *getodd( struct ListNode **L )//L是头指针的地址 *L即为头指针
{
struct ListNode *p=NULL,*head=NULL,*tail=NULL,*p2=NULL,*p1=NULL;//此时的head是奇数链的头指针
p1=*L;
while(p1){
if(p1->data%2){
if(head==NULL)
head=p1;
else
tail->next=p1;
tail=p1;
//建立新的奇数链
if(p1==*L)
*L=p1->next;
else
p2->next=p1->next;
}
else{
p2=p1;
p1=p1->next;
}
}
return head;
}
#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()
{
int num;
struct ListNode *p,*head,*tail;
head=tail=NULL;
while(scanf("%d",&num)){
if(num==-1)break;
p=(struct ListNode *)malloc(sizeof(struct ListNode));
p->data=num;
p->next=NULL;
if(head==NULL){
head=p;
tail=p;
}else{
tail->next=p;
tail=p;
}
}
return head;
}
struct ListNode *getodd( struct ListNode **L )//L是头指针的地址 *L即为头指针
{
struct ListNode *head=NULL,*tail=NULL,*p2=NULL,*p1=NULL;
p1=*L; //此时的head是奇数链的头指针
while(p1){
if(p1->data%2!=0){
if(p1==*L){
*L=p2=p1->next;
}else{
p2->next=p1->next;
}
if(head==NULL){ //建立新的奇数链
head=p1;
tail=p1;
}else{
tail->next=p1;
tail=p1;
}
}else{
p2=p1;
}
p1=p1->next;
}
if(head!=NULL)tail->next=NULL;
if(*L !=NULL)p2->next=NULL;
return head;
}
#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()
{
int num;
struct ListNode *p,*head,*tail;
head=tail=NULL;
while(scanf("%d",&num)){
if(num==-1)
break;
p=(struct ListNode *)malloc(sizeof(struct ListNode));
p->data=num;
p->next=NULL;
if(head==NULL)
head=p;
else
tail->next=p;
tail=p;
}
return head;
}
struct ListNode *getodd( struct ListNode **L )//L是头指针的地址 *L即为头指针
{
#if 1
struct ListNode *podd_head = NULL, *p, *q, *tail;
p = q = *L;
while (p) {
if (p->data % 2) {
if (!podd_head)
podd_head = p;
else
tail->next = p;
tail = p;
if (p == *L) {
*L = (*L)->next;
p = q = *L;
} else {
q->next = p->next;
p = p->next;
}
} else {
q = p;
p = p->next;
}
}
tail->next = NULL;
return podd_head;
#else
struct ListNode *p=NULL,*head=NULL,*tail=NULL,*p2=NULL,*p1=NULL;//此时的head是奇数链的头指针
p1=*L;
while(p1){
if(p1->data%2){
if(head==NULL)
head=p1;
else
tail->next=p1;
tail=p1;
//建立新的奇数链
if(p1==*L)
*L=p1->next;
else
p2->next=p1->next;
}
else{
p2=p1;
p1=p1->next;
}
}
return head;
#endif
}