题目:轮转数组
代码:
#include<stdio.h>
#define MaxSize 10
typedef struct {
int data[MaxSize];
int length;
}SqList;
void InitList(SqList* L) {
for (int i = 0; i < MaxSize; i++) {
L->data[i] = 0;
L->length = 0;
}
}
void Reverse(SqList* L, int left, int right) {
while (left <= right) {
int temp = L->data[left];
L->data[left] = L->data[right];
L->data[right] = temp;
left++;
right--;
}
}
int main()
{
SqList L;
InitList(&L);
for (int i = 0; i < MaxSize; i++) {
L.data[i] = i + 1;
L.length++;
}
int p = 3;
Reverse(&L, 0, L.length - 1);
Reverse(&L, 0, L.length - 1 - p);
Reverse(&L, L.length - p, L.length - 1);
for (int i = 0; i < L.length; i++) {
printf("%d ", L.data[i]);
}
return 0;
}