#include<stdio.h>
#include<stdlib.h>
#define MaxSize 8
struct SeqList {
int data[MaxSize] = { 1,2,3,4,5,6,7,8 };
int* data1=data;
};
//法1:
void InvertedList1(SeqList& L) {
int* ptr = L.data1;
int j = 0;
L.data1 = (int*)malloc(sizeof(int)*MaxSize);
for (int i = MaxSize-1; i >= 0; i--) {
L.data1[j] = ptr[i];
j++;
}
// free(ptr);
}
//法2:
void InvertedList2(SeqList& L) {
int temp;
for (int i = 0; i < MaxSize / 2; i++) {
temp = L.data[i];
L.data[i] = L.data[MaxSize - i - 1];
L.data[MaxSize - i - 1] = temp;
}
}
int main() {
SeqList L;
printf("逆置前: ");
for (int i = 0; i < MaxSize; i++) {
printf("%d ", L.data[i]);
}
printf("\n");
printf("逆置后: ");
//法1调用且输出
/** InvertedList1(L);
* for (int i = 0; i < MaxSize; i++) {
printf("%d ", L.data1[i]);
}
}
**/
//法2调用且输出
InvertedList2(L);
for (int i = 0; i < MaxSize; i++) {
printf("%d ", L.data[i]);
}
}
逆置顺序表
最新推荐文章于 2023-03-30 17:40:33 发布