问题:利用链表将数组逆转
分析:链表结构里面定义数组,以及数组长度,在函数reverse中转换,主要的就是n-i-1这个算式,与数组逆转思想是一样的,很简单的一道题
结论:熟悉链表的结构,多多练习。
#include <iostream>
#include <algorithm>
#include <string.h>
#include <bits/stdc++.h>
#define Max 50
using namespace std;
char arr[10002];
struct SqList{
int a[Max] = {1,4,6,3,2,8,7};
int length = 7;
};
void reverse(SqList &L){
for(int i = 0;i <= L.length/2;i++){
int v = L.a[i];
L.a[i] = L.a[L.length -i-1];
L.a[L.length-i-1] = v;
}
}
int main(){
SqList L;
cout<<"原数组为:"<<endl;
for(int i = 0;i < L.length; i++)
cout<<L.a[i]<<" ";
cout<<endl;
reverse(L);
cout<<"逆转后数组为:"<<endl;
for(int i = 0;i<L.length;i++)
cout<<L.a[i]<<" ";
return 0;
}