2017-04-14
2.设计一个高效的算法,将顺序表的所有元素逆置,要求算法的空间复杂度为O(1)。
1 #include<iostream> 2 #include<stdio.h> 3 #define MaxSize 50//定义线性表的最大长度 4 typedef int ElemType; 5 6 typedef struct { 7 ElemType data[MaxSize]; 8 int length; 9 }SqList; 10 11 void reverse(SqList &L) 12 { 13 ElemType temp; 14 for (int i = 0; i < L.length / 2; i++) 15 { 16 temp = L.data[i]; 17 L.data[i] = L.data[L.length - 1 - i]; 18 L.data[L.length - 1 - i] = temp; 19 } 20 } 21 22 int main() 23 { 24 SqList L; 25 //L.data = { 2,4,3,5,1,6,0,8,9,10 }; 26 L.data[0] = 2; 27 L.data[1] = 4; 28 L.data[2] = 3; 29 L.data[3] = 5; 30 L.data[4] = 1; 31 L.data[5] = 6; 32 L.data[6] = 0; 33 L.data[7] = 8; 34 L.data[8] = 9; 35 L.data[9] = 10; 36 L.length = 10; 37 38 reverse(L); 39 for (int i = 0; i < L.length; i++) 40 { 41 printf("%d\n", L.data[i]); 42 } 43 //ElemType e = DeleteMin(L); 44 //printf("%d", e); 45 46 system("pause"); 47 return 0; 48 }
时间: 2024-10-12 03:48:52