日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

顺序表的结构和9个基本运算算法

發(fā)布時(shí)間:2025/7/14 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 顺序表的结构和9个基本运算算法 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

書中的代碼:

1 #include <iostream> 2 using namespace std; 3 4 #define MaxSize 50 5 typedef int ElemType; 6 typedef struct 7 { 8 ElemType data[MaxSize]; 9 int length; 10 }Sqlist; 11 12 //1.初始化線性表 13 void InitList(Sqlist * &L) 14 { 15 L = (Sqlist *)malloc(sizeof(Sqlist)); 16 L->length = 0; 17 } 18 //2.銷毀線性表 19 void DestroyList(Sqlist * &L) 20 { 21 free(L); 22 } 23 //3.判斷線性表是否為空 24 bool ListEmpty(Sqlist * &L) 25 { 26 return (L->length == 0); 27 } 28 //4.返回線性表的長度 29 int ListLength(Sqlist * L) 30 { 31 return L->length; 32 } 33 //5.輸出線性表 34 void DispList(Sqlist * &L) 35 { 36 for (size_t i = 0; i < L->length; i++) 37 { 38 printf("%d", L->data[i]); 39 printf("\n"); 40 } 41 42 } 43 //6.求線性表中某個(gè)數(shù)據(jù)元素 44 bool GetElem(Sqlist * L, int i, ElemType &e) 45 { 46 if (i <1 || i>L->length) 47 { 48 return false; 49 } 50 e = L->data[i - 1]; 51 return true; 52 } 53 //7.按元素值進(jìn)行查找 54 int LocateElem(Sqlist * L, ElemType e) 55 { 56 int i = 0; 57 while (i < L->length && L->data[i] != e) 58 { 59 i++; 60 } 61 if (i > L->length) 62 { 63 return 0; 64 } 65 else 66 { 67 return i + 1; 68 } 69 } 70 //8.插入數(shù)據(jù)元素 71 bool ListInsert(Sqlist * &L, int i, ElemType e) 72 { 73 int j = 0; 74 75 if (i<1 || i >L->length+1) 76 { 77 return false; 78 } 79 i--; 80 for (j = L->length; j > i; j--) 81 { 82 L->data[j] = L->data[j - 1]; 83 } 84 L->data[i] = e; 85 L->length++; 86 87 88 return true; 89 } 90 //9.刪除數(shù)據(jù)元素 91 bool ListDelete(Sqlist * &L, int i, ElemType &e) 92 { 93 int j = 0; 94 if (i<1 || i>L->length) 95 { 96 return false; 97 } 98 i--; 99 e = L->data[i]; 100 for (j = i; j < L->length - 1; j++) 101 { 102 L->data[j] = L->data[j + 1]; 103 } 104 L->length--; 105 return true; 106 } 107 108 int main() 109 { 110 Sqlist * L; 111 InitList(L);//初始化順序表 112 ListInsert(L, 1, 1); 113 ListInsert(L, 2, 2); 114 ListInsert(L, 3, 3); 115 ListInsert(L, 4, 4); 116 ListInsert(L, 5, 5); 117 DispList(L); 118 printf("當(dāng)前的長度:%d",ListLength(L)); 119 getchar(); 120 return 0; 121 } View Code

?

?

轉(zhuǎn)載于:https://www.cnblogs.com/xifengmo/p/11149009.html

總結(jié)

以上是生活随笔為你收集整理的顺序表的结构和9个基本运算算法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。