天天看點

c語言動态建立數組

數組實作及相關操作

        • 1.資料結構
        • 2.資料初始化
        • 3.輸出數組
        • 4.添加元素
        • 5.插入元素
        • 6.删除元素
        • 7.數組倒置
        • 8.排序
        • 9.完整代碼

使用malloc函數動态配置設定記憶體建立數組,與傳統的數組相比,可以手動釋放記憶體,順便練習一下C語言資料結構。

1.資料結構

struct Arr {
	int * pBase;//存儲的第一個元素的位址
	int len;//數組的長度
	int cnt;//數組的有效長度
};
           

2.資料初始化

//初始化數組 
void init_arr(struct Arr * pArr,int length) 
{
	pArr->pBase = (int*) malloc(sizeof(int)*length);//動态配置設定堆記憶體 ,并用pBase存放第一個元素位址 
	if(NULL == pArr->pBase)
	{
		printf("配置設定記憶體失敗!\n");
		exit(-1);
		
	}
	else
	{
		pArr->len = length;//初始化數組長度 
		pArr->cnt = 0;//初始化數組有效長度 
		printf("初始化完成,數組長度為%d\n", length); 
	}
	return;
}
           

3.輸出數組

//輸出數組 (有效長度的數組) 
void show_arr(struct Arr * pArr)
{

	if(is_empty(pArr))
	{
		printf("數組有效長度為0,請添加元素到數組!\n");
	}
	else
	{
		printf("目前數組為:");

		for(int i = 0; i < pArr->cnt; i++)
		{
			printf("%d ", pArr->pBase[i]);	
		}
		printf("\n");
		
	}
}
           

4.添加元素

bool append_arr(struct Arr * pArr, int val)
{
	if(is_full(pArr))//數組已滿,傳回false 
	{
		printf("數組已滿,添加元素失敗!\n");
		return false;
	}
	else
	{
		pArr->pBase[pArr->cnt] = val;//在數組有效長度末尾追加元素 
		(pArr->cnt)++;
		return true;
	}
}
           

5.插入元素

//插入元素 
bool insert_arr(struct Arr * pArr, int pos, int val)
{
	if(is_full(pArr))
	{
		printf("數組已滿,插入元素失敗!\n");
		return false;
	}
	if(pos < 1 || pos > pArr->cnt+1)
		return false;
	for(int i = pArr->cnt - 1; i >= pos - 1; i--)//在指定位置插入元素 
	{
		pArr->pBase[i+1] = pArr->pBase[i];
	}
	pArr->pBase[pos - 1] = val;
	pArr->cnt++;
	return true;
}
           

6.删除元素

bool delete_arr(struct Arr * pArr, int pos, int * pVal)
{
	if (is_empty(pArr))
	{
		printf("數組有效長度為0,删除失敗!\n");
		return false;
	}
	if(pos < 1 || pos > pArr->cnt)
	{
		return false;
	}
	*pVal = pArr->pBase[pos-1];

	for(int i = pos; i < pArr->cnt; i++)
	{
		pArr->pBase[i-1] = pArr->pBase[i];
	} 
	pArr->cnt--;
	return true;
}
           

7.數組倒置

//倒置 ,将第一個元素與最後一個元素的值進行交換,以此類推 
void inversion_arr(struct Arr * pArr)
{
	if(is_empty(pArr))
	{
		printf("數組有效長度為0,請添加元素到數組!\n");
		return;
	}
	int i = 0;
	int j = pArr->cnt-1;
	int t;
	while(i<j)
	{
		t = pArr->pBase[i];
		pArr->pBase[i] = pArr->pBase[j];
		pArr->pBase[j] = t;
		i++;
		j--;
	}
	return;
}

           

8.排序

//排序(簡單雙重for循環) 
void sort_arr(struct Arr * pArr)
{
	if(is_empty(pArr))
	{
		printf("數組有效長度為0,請添加元素到數組!\n");
		return;
	}
	int t;
	for(int i = 0; i < pArr->cnt; i++)
	{
		for(int j = i+1; j < pArr->cnt; j++)
		{
			if(pArr->pBase[i] > pArr->pBase[j])
			{
				t = pArr->pBase[i];
				pArr->pBase[i] = pArr->pBase[j];
				pArr->pBase[j] = t; 
			}

		}	
	} 
}
           

9.完整代碼

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>


struct Arr {
	int * pBase;//存儲的第一個元素的位址
	int len;//數組的長度
	int cnt;//數組的有效長度
};

void init_arr(struct Arr * pArr ,int length);//初始化數組 
void show_arr(struct Arr * pArr );//輸出數組 
bool is_empty(struct Arr * pArr );//判斷數組是否為空 
bool is_full(struct Arr * pArr);//判斷數組是否已滿 
bool append_arr(struct Arr * pArr, int val);//追加元素() 
bool insert_arr(struct Arr * pArr, int pos, int val);//插入元素 
bool delete_arr(struct Arr * pArr, int pos, int * val);//删除元素 
void inversion_arr(struct Arr * pArr);//倒置 
void sort_arr(struct Arr * pArr);//排序(簡單雙重for循環排序) 



int main(void)
{
	struct Arr arr;//聲明結構體變量 
	int val;//接受删除元素 
	init_arr(&arr,6);//初始化數組長度為6; 
	show_arr(&arr);
	printf("***************************************************************\n");
	printf("添加元素功能測試:\n");  //[6,3,1,9,7]
	append_arr(&arr,6);
	show_arr(&arr);
	append_arr(&arr,3);
	show_arr(&arr);
	append_arr(&arr,1);
	show_arr(&arr);
	append_arr(&arr,9);
	show_arr(&arr);
	append_arr(&arr,7);
	show_arr(&arr);
	printf("***************************************************************\n");
	printf("插入元素功能測試:\n");
	insert_arr(&arr,2,99);//在第二個位置插入99 
	show_arr(&arr);
	printf("***************************************************************\n");
	printf("删除元素功能測試:\n");
	delete_arr(&arr,3,&val);
	show_arr(&arr);
	printf("删除元素為:");
	printf("%d\n", val);
	printf("***************************************************************\n");
	printf("倒置元素功能測試:\n");
	inversion_arr(&arr);
	show_arr(&arr);
	printf("***************************************************************\n");
	printf("排序功能測試:\n");
	sort_arr(&arr);
	show_arr(&arr);
//	printf("%d", val);
	return 0;
}

//初始化數組 
void init_arr(struct Arr * pArr,int length) 
{
	pArr->pBase = (int*) malloc(sizeof(int)*length);//動态配置設定堆記憶體 ,并用pBase存放第一個元素位址 
	if(NULL == pArr->pBase)
	{
		printf("配置設定記憶體失敗!\n");
		exit(-1);
		
	}
	else
	{
		pArr->len = length;//初始化數組長度 
		pArr->cnt = 0;//初始化數組有效長度 
		printf("初始化完成,數組長度為%d\n", length); 

	}
	return;
	
}

//判斷數組是否為空 
bool is_empty(struct Arr * pArr)
{
	if(pArr->cnt == 0)//有效長度為0,則數組為空 
	{
		return true;
		
	}
	else
	{
		return false;
	}
 } 

//輸出數組 (有效長度的數組) 
void show_arr(struct Arr * pArr)
{

	if(is_empty(pArr))
	{
		printf("數組有效長度為0,請添加元素到數組!\n");
	}
	else
	{
		printf("目前數組為:");

		for(int i = 0; i < pArr->cnt; i++)
		{
			printf("%d ", pArr->pBase[i]);	
		}
		printf("\n");
		
	}
}
//數組是否已滿 
bool is_full(struct Arr * pArr)
{
	if(pArr->len == pArr->cnt)//數組的有效長度是否等于數組的長度 
	{
		return true;
	}
	else
	{
		return false;
	}
}
//添加元素 
bool append_arr(struct Arr * pArr, int val)
{
	if(is_full(pArr))//數組已滿,傳回false 
	{
		printf("數組已滿,添加元素失敗!\n");
		return false;
	}
	else
	{
		pArr->pBase[pArr->cnt] = val;//在數組有效長度末尾追加元素 
		(pArr->cnt)++;
		return true;
	}
}
//插入元素 
bool insert_arr(struct Arr * pArr, int pos, int val)
{
	if(is_full(pArr))
	{
		printf("數組已滿,插入元素失敗!\n");
		return false;
	}
	if(pos < 1 || pos > pArr->cnt+1)
		return false;
	for(int i = pArr->cnt - 1; i >= pos - 1; i--)//在指定位置插入元素 
	{
		pArr->pBase[i+1] = pArr->pBase[i];
	}
	pArr->pBase[pos - 1] = val;
	pArr->cnt++;
	return true;
}
//删除元素 (使用指針變量接受删除的值) 存在val中 
bool delete_arr(struct Arr * pArr, int pos, int * pVal)
{
	if (is_empty(pArr))
	{
		printf("數組有效長度為0,删除失敗!\n");

		return false;
	}
	if(pos < 1 || pos > pArr->cnt)
	{
		return false;
	}
	*pVal = pArr->pBase[pos-1];

	for(int i = pos; i < pArr->cnt; i++)
	{
		pArr->pBase[i-1] = pArr->pBase[i];
		
	} 
	pArr->cnt--;
	return true;
	 
}
//倒置 ,将第一個元素與最後一個元素的值進行交換,以此類推 
void inversion_arr(struct Arr * pArr)
{
	if(is_empty(pArr))
	{
		printf("數組有效長度為0,請添加元素到數組!\n");
		return;
	}
	int i = 0;
	int j = pArr->cnt-1;
	int t;
	while(i<j)
	{
		t = pArr->pBase[i];
		pArr->pBase[i] = pArr->pBase[j];
		pArr->pBase[j] = t;
		i++;
		j--;
	}
	return;
}

//排序(簡單雙重for循環) 
void sort_arr(struct Arr * pArr)
{
	if(is_empty(pArr))
	{
		printf("數組有效長度為0,請添加元素到數組!\n");
		return;
	}
	int t;
	for(int i = 0; i < pArr->cnt; i++)
	{
		for(int j = i+1; j < pArr->cnt; j++)
		{
			if(pArr->pBase[i] > pArr->pBase[j])
			{
				t = pArr->pBase[i];
				pArr->pBase[i] = pArr->pBase[j];
				pArr->pBase[j] = t; 
			}

		}	
	} 
}
           

繼續閱讀