用c语言,将一个顺序表的所有元素逆置
时间: 2024-05-10 22:20:36 浏览: 68
```c
#include <stdio.h>
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int length;
} SqList;
void reverse(SqList *list) {
int temp;
int left = 0;
int right = list->length - 1;
while (left < right) {
temp = list->data[left];
list->data[left] = list->data[right];
list->data[right] = temp;
left++;
right--;
}
}
int main() {
SqList list = { {1, 2, 3, 4, 5}, 5 };
printf("Before reverse: ");
for (int i = 0; i < list.length; i++) {
printf("%d ", list.data[i]);
}
printf("\n");
reverse(&list);
printf("After reverse: ");
for (int i = 0; i < list.length; i++) {
printf("%d ", list.data[i]);
}
printf("\n");
return 0;
}
```
阅读全文