Home > Back-end >  Pintia on zhejiang university list title, for help!
Pintia on zhejiang university list title, for help!

Time:04-07

Topic request: implement two function struct ListNode * readlist () and struct ListNode * getodd (struct ListNode * * L), respectively, will read the data is stored as a singly linked list, will be an odd number of values of nodes in the list to make a new list,
Input the sample: 1 2 3 4 5 6 7 2-1
The output sample: 1 3 5 7
2 2 4 6
The following is my code
 # include & lt; stdio.h> 
#include

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 - & gt; The data);
P=p - & gt; Next;
}
printf("\n");
}

Int main ()
{
Struct ListNode * L * Odd;
L=readlist ();
Odd=getodd (& amp; L);
Printlist (Odd);
Printlist (L);

return 0;
}
Struct ListNode * readlist ()
{
int num;
Struct ListNode * p, * head, * tail;
The head tail of==NULL;
While (the scanf (" % d ", & amp; Num)) {
If (num==1) break;
P=(struct ListNode *) malloc (sizeof (struct ListNode));
P - & gt; data=https://bbs.csdn.net/topics/num;
P - & gt; Next=NULL;
If (head==NULL)
The head=p;
The else
Tail - & gt; Next=p;
Tail=p;
}
return head;
}
Struct ListNode * getodd (struct ListNode * * L)//L is the head pointer address * L is the head pointer
{
Struct ListNode * p=NULL, * head=NULL, * tail=NULL, * p2=NULL, * p=NULL;//at this time of the head is an odd number of chain head pointer
P1=* L;
While (p1) {
If (p1 - & gt; Data % 2) {
If (head==NULL)
The head=p1;
The else
Tail - & gt; Next=p;
Tail=p1;
//set up new odd chain
If (p1==* L)
* L=p1 - & gt; Next;
The else
The p2 - & gt; Next=p1 - & gt; Next;
}
The else {
P1 p2=;
P1=p1 - & gt; Next;
}
}
return head;

}

Evaluation results following

Small white one, bosses feel free to point out problems!
  • Related