結構_奇前偶後
user_3763047219
c_cpp
3 years ago
1.6 kB
9
Indexable
#include <stdio.h>
#include <stdlib.h>
struct ListNode
{
int val;
struct ListNode* next;
};
struct ListNode* oddEvenList(struct ListNode* head);
struct ListNode* oddEvenList(struct ListNode* head)
{
struct ListNode *ptr;
ptr = head;
int arr[1000] = {};
int count = 0;
while (ptr != NULL) {
arr[count] = ptr->val;
count++;
ptr=ptr->next;
}
int count2 = 0;
ptr = head;
while (count2<count) {
ptr->val = arr[count2];
count2 = count2 + 2;
ptr = ptr->next;
}
count2 = 1;
while (ptr!=NULL) {
ptr->val = arr[count2];
count2 = count2 + 2;
ptr = ptr->next;
}
return head;
}
void Construct(struct ListNode* node, int length, int num)
{
int a;
scanf("%d", &a);
node->val = a;
if (num >= length)
{
node->next = NULL;
return;
}
node->next = (struct ListNode*)malloc(sizeof(struct ListNode));
num++;
Construct(node->next, length, num);
}
int main()
{
struct ListNode* head;
int n, a, length;
head = (struct ListNode*)malloc(sizeof(struct ListNode));
scanf("%d", &n);
while (n--)
{
scanf("%d", &length);
Construct(head, length, 1);
struct ListNode* ans = oddEvenList(head);
struct ListNode* ptr;
ptr = ans;
while (ptr != NULL)
{
printf("%d ", ptr->val);
ptr = ptr->next;
}
printf("\n");
}
}Editor is loading...