順序表的就地逆置
阿新 • • 發佈:2018-11-05
#include <stdio.h> /* 題目:資料結構題集18頁2.21 試寫一個演算法,實現順序表的就地逆置,即利用原表的儲存空間將線性表(a1,a2,...,an)逆置為(an,...,a2,a1) */ #define maxlen 50 #define DATATYPE int typedef struct{ DATATYPE arr[maxlen]; int length; }Seqlist; void print_SQ(Seqlist list) { for(int i=0;i<list.length;i++){ printf("%d ",list.arr[i]); } printf("\n"); } int main(void) { Seqlist list = {{1,2,3,4,5},5}; int tmp; for(int i=0,j=list.length-1;i<j;i++,j--){ //list.arr[i]與list.arr[j]進行交換 tmp = list.arr[i]; list.arr[i] = list.arr[j]; list.arr[j] = tmp; } print_SQ(list); }