数据结构趣题——顺序表就地逆置
生活随笔
收集整理的這篇文章主要介紹了
数据结构趣题——顺序表就地逆置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
利用原表的存儲空間將順序表(a1,a2,……,an)逆置為(an,an-1,………a1)。
1: #include <stdio.h> 2: #include <stdlib.h> 3:? 4: # define MAXSIZE 10 5: typedef struct { 6: int * base; 7: int length; 8: } sqlist ; 9:? 10: void reverseSQ(sqlist *l) { 11: int low = 0 , high = l->length - 1; 12: int buf , i; 13:? 14: for(i = 0; i < l->length / 2; i++) 15: { 16: buf = l->base[low] ; 17: l->base[low] = l->base[high]; 18: l->base[high] = buf; 19: low++; 20: high--; 21: } 22: } 23:? 24: int main() 25: { 26: sqlist l; 27: int a , i = 0; 28: /*創建一個順序表*/ 29: l.base = (int *)malloc(sizeof(int) * MAXSIZE); 30: l.length = 0; 31:? 32: /*輸入數據*/ 33: printf("Please input below 10 integer into the sqlist\n") ; 34: printf("Type -1 for stopping input\n"); 35: scanf("%d", &a); 36:? 37: while(a != -1 && i <= 9) 38: { 39: l.base[i] = a; 40: l.length++; 41: i++; 42: scanf("%d", &a); 43: } 44:? 45: /*輸出原順序表中的數據*/ 46: printf("The contents of the sqlist are\n"); 47:? 48: for(i = 0; i < l.length; i++) 49: printf("%d ", l.base[i]); 50:? 51: printf("\n"); 52:? 53: reverseSQ(&l); /*就地逆置順序表*/ 54:? 55:? 56: /*輸出逆置后的順序表中的數據*/ 57: printf("The contents of the reversed sqlist are\n"); 58:? 59: for(i = 0; i < l.length; i++) 60: printf("%d ", l.base[i]); 61:? 62: return 0; 63: }轉載于:https://www.cnblogs.com/steven_oyj/archive/2010/05/28/1745941.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的数据结构趣题——顺序表就地逆置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: head first Design Pa
- 下一篇: Eclipse+MyEclipse+To