Known sequence table L The data element type in is int. Design the algorithm and adjust it to the left and right parts , Element on the left （ That is, the one in front ） All are odd numbers , All elements on the right （ That is, the one behind ） Even numbers , The time complexity of the algorithm is required to be O(n), The space complexity is O（1）.

The function prototype is as follows ：
void odd_even(SeqList *L);

Relevant definitions are as follows ：
struct _seqlist{ ElemType elem[MAXSIZE]; int last; }; typedef struct _seqlist
SeqList;
Provide code
#include <stdio.h> #include <stdlib.h> #include "list.h" // Please do not delete , Otherwise, the inspection fails void
odd_even(SeqList *L) { }
Reference code
/* Sequence table Data adjustment Known sequence table L The data element type in is int. Design the algorithm and adjust it to the left and right parts , Element on the left （ In front ） All are odd numbers ,
All elements on the right （ That is, the one behind ） Even numbers , The time complexity of the algorithm is required to be O(n), The space complexity is O（1）. */ /* Algorithm idea ：
The space complexity is O（1） Indicates that only one intermediate temporary variable can be used Define the left and right pointers , Compare , Decide whether to exchange */ #include <stdio.h> #include
<stdlib.h> //#include "list.h" // Please do not delete , Otherwise, the inspection fails #define ElemType int #define
MAXSIZE 10 struct _seqlist { ElemType elem[MAXSIZE]; int last; }; typedef
struct _seqlist SeqList; // Odd left and even right required void odd_even(SeqList* L) { int* left =
&(L->elem[0]); int* right = &(L->elem[L->last]); int tmp; while (left < right)
{ // On the left is an even number if (*left % 2 == 0) { // On the right is an odd number if (*right % 2 == 1) { // exchange tmp =
*left; *left = *right; *right = tmp; left++; right--; } else { right--; } }
else { // On the left is an odd number left++; } } } void print(const SeqList* L) { int i = 0; for (i =
0; i <= L->last; i++) { printf("%d ", L->elem[i]); } } int main() { SeqList L =
{ {0,1,2,3,4,5,6,7,8,9},4 };// test case odd_even(&L); print(&L);// Print to see if the deletion is successful return
0; }

Technology
Daily Recommendation
views 1