# include <stdio.h>
# include <malloc.h>
# include <stdlib.h>
//定義數組結構體
struct arr
{
int * pBase; //數組首地址(數組名)
int len; //數組長度
int cnt; //有效數組長度
};
//初始化數組
void init(struct arr * pArr,int length);
//遍歷數組中元素
void show(struct arr * pArr);
//添加一個數組元素
bool add(struct arr * pArr,int val);
//判斷數組元素是否為空數組
bool is_empty(struct arr * pArr);
//判斷數組元素是否是滿的
bool is_full(struct arr * pArr);
//插入數組元素,指定位置
bool add_index(struct arr * pArr,int index,int val);
//倒置數組元素
void reverse(struct arr * pArr);
//刪除特定位置的元素
bool delete_index(struct arr * pArr,int index);
//數組排序
void sort(struct arr * pArr);
//獲取特定位置元素
int get(struct arr * pArr,int index);
int main(void)
{
struct arr array;
printf("---------------------初始化數組--------------------\n");
init(&array,5);
show(&array);
printf("---------------------初始化數組--------------------\n\n\n");
printf("--------------添加數組元素數組--------------------\n");
add(&array,1);
add(&array,2);
add(&array,3);
add(&array,4);
show(&array);
printf("--------------添加數組元素數組--------------------\n\n\n");
printf("--------------指定位置插入數組元素數組-------------\n");
add_index(&array,2,5);
show(&array);
printf("--------------指定位置插入數組元素數組-------------\n\n\n");
printf("---------------------刪除數組元素--------------------\n");
if(delete_index(&array,2)){
printf("刪除成功\n");
show(&array);
}else{
printf("刪除失敗\n");
}
printf("---------------------刪除數組元素--------------------\n\n\n");
printf("---------------------倒置數組元素--------------------\n");
reverse(&array);
show(&array);
printf("---------------------倒置數組元素--------------------\n\n\n");
printf("---------------------升序數組元素--------------------\n");
sort(&array);
show(&array);
printf("---------------------升序數組元素--------------------\n\n\n");
printf("---------------------獲取數組元素指定元素--------------------\n");
int t = get(&array,3);
if(t == -1){
printf("無法獲取 \n");
}else{
printf("%d \n",t);
}
printf("---------------------獲取數組元素指定元素--------------------\n\n\n");
return 0;
}
void init(struct arr * pArr,int length)
{
pArr->pBase = (int *) malloc(sizeof(int) * length); //動態開辟空間
if(NULL == pArr->pBase)
{
printf("動態分配內存空間失敗");
exit(-1);
}else{
pArr->len = length;
pArr->cnt =0;
}
return;
}
bool is_empty(struct arr * pArr)
{
if(pArr->cnt == 0)
{
return true;
}else{
return false;
}
}
bool is_full(struct arr * pArr)
{
if(pArr->cnt == pArr->len)
{ //數組有效長度和數組開辟空間相等
return true;
}else{
return false;
}
}
void show(struct arr * pArr)
{
if(is_empty(pArr))
{
printf("數組為空\n");
}else{
for(int i =0; i < pArr->cnt;i++)
{
printf("%d \n", pArr->pBase[i]);
}
}
}
bool add(struct arr * pArr, int val)
{
//若是滿的就不再添加
if(is_full(pArr))
{
return false;
}
//添加元素
pArr->pBase[pArr->cnt] = val;
(pArr->cnt)++;
return true;
}
bool add_index(struct arr * pArr, int index, int val)
{
//若是滿的就不再添加
if(is_full(pArr))
{
return false;
}
//若是不在范圍內
if(index <1 || index >= pArr->cnt)
{
return false;
}
for(int i = pArr->cnt+1; i >= index-1;i--){ //從插入的位置開始,到結束 從最后一個開始
pArr->pBase[i+1] = pArr->pBase[i];
}
//插入位置
pArr->pBase[index-1] = val;
return true;
}
void reverse(struct arr * pArr)
{
int i = 0;
int j = pArr->cnt-1;
int temp;
while(i < j)
{
temp = pArr->pBase[i]; //第一個位置存起來
pArr->pBase[i] = pArr->pBase[j]; //然后第一個位置存最后一個位置的數
pArr->pBase[j] = temp; //最后一個位置存一個位置的數
i++;
j--;
}
return;
}
bool delete_index(struct arr * pArr,int index)
{
//若是為空不刪除
if(is_empty(pArr))
{
return false;
}
//若是不在范圍內
if(index <1 || index >= pArr->cnt)
{
return false;
}
for(int i = index;i<pArr->cnt;i++){
pArr->pBase[i-1] = pArr->pBase[i]; //后面的值賦值到前面
}
(pArr->cnt)--;
return true;
}
void sort(struct arr * pArr)
{
int temp,i,j;
for(i = 0; i< pArr->cnt;i++)
{
for(j = i+1; j< pArr->cnt;j++)
{
if(pArr->pBase[i] > pArr->pBase[j])
{
temp = pArr->pBase[i];
pArr->pBase[i] = pArr->pBase[j];
pArr->pBase[j] = temp;
}
}
}
}
int get(struct arr * pArr,int index)
{
//若是為空
if(is_empty(pArr))
{
return -1;
}
//若是不在范圍內
if(index <1 || index >= pArr->cnt)
{
return -1;
}
return pArr->pBase[index];
}
連續存儲數組(C語言)
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
推薦閱讀更多精彩內容
- 項目中如果采用ICE請求數據的結構,有時候后臺會傳送以NSData形式的long數組。下方法可以直接轉成Byte數...