Note
- 使用的工具是CodeBlocks 16.01
- 沒有實現(xiàn)元素無限添加
- 啟用了C99標準
CodeBlocks啟用C99支持
- 自己寫著玩的
線性表是有序的序列{a1,a2,...,an}冯痢,元素與元素之間有前后(先后)的關(guān)系,對于其中的任一個元素ai來說务傲,其前面有且只有一個或零個元素最冰,后面也是如此。這樣一來,這個定義就可以使用在循環(huán)鏈表中了。
單向鏈表
雙向的鏈表
循環(huán)的鏈表
array_list.h
定義了ArrayList
結(jié)構(gòu)體和可以對ArrayList及其元素進行的操作庇勃。
#include<stdbool.h>
#define DEFAULT_ARRAY_LIST_LEN 1024
typedef struct{
int *pArray; // 指向數(shù)組的指針
int totalLen; // 數(shù)組的總長度
int increase; // 增長率
int currentLen; //當前數(shù)組的長度
}ArrayList;
bool arrayListFull(const ArrayList * const pArray);
bool arrayListEmpty(const ArrayList* const pArray);
bool arrayListAppend(ArrayList *pArray,int e);
bool arrayListInsert(ArrayList* pArray,int i,int e);
int arrayListLen(const ArrayList*const pArray);
bool arrayListRemove(ArrayList* pArray,int index,int * e);
void arrayListShow(const ArrayList*const pArray);
void arrayListSort(ArrayList*pArray,bool rev);
void arrayListReverse(ArrayList*pArray);
bool arrayListContain(const ArrayList*const pArray,int e);
int arrayListIndex(const ArrayList* const pArray,int e);
array_list.c
array_list.h中定義的函數(shù)的實現(xiàn)代碼。
#include<stdio.h>
#include<stdbool.h>
#include<malloc.h>
#include"array_list.h"
/** \brief 初始化ArrayList.
*
* \param pList:指向需要初始化的ArrayList的指針
* \param length:ArrayList的長度槽驶。如果指定的長度小于或等于0,
則默認為DEFAULT_ARRAY_LIST_LEN
* \return 如果初始化成功鸳兽,返回true掂铐。
*/
bool arrayListInit(ArrayList *pList,int length){
if(length<=0){
length = DEFAULT_ARRAY_LIST_LEN;
}
pList->pArray=(int *)malloc(sizeof(int)*length);// 動態(tài)分配內(nèi)存
if(pList->pArray!=NULL){
pList->currentLen=0;
pList->totalLen=length;
return true;
}
printf("內(nèi)存分配失敗揍异!\n");
exit(-1);
}
/** \brief 在ArrayList的后面添加元素
*
* \param pArray:指向需要追加元素的ArrayList的指針
* \param e:需要添加的元素
* \return 如果添加成功則返回true全陨,否則返回false
*/
bool arrayListAppend(ArrayList *pArray,int e){
if(arrayListFull(pArray)){
printf("ArrayList滿了!\n");
return false;
}
if(pArray!=NULL){
pArray->pArray[pArray->currentLen]=e;
pArray->currentLen++;
return true;
}
printf("ArrayList沒有初始化!\n");
return false;
}
/** \brief 判斷ArrayList是否滿了
*
* \param pArray ArrayList* 需要判斷的ArrayList對象的指針
* \return bool 滿了則返回true衷掷,否則返回false
*
*/
bool arrayListFull(const ArrayList* const pArray){
return pArray->currentLen==pArray->totalLen?true:false;
}
/** \brief 判斷ArrayList是否為空
*
* \param pArray const ArrayList*const
* 需要判斷的ArrayList對象的指針
* \return bool
* 如果為空,則返回true戚嗅,否則返回false
*/
bool arrayListEmpty(const ArrayList* const pArray)
{
return pArray->currentLen==0?true:false;
}
/** \brief 往ArrayList中插入元素
*
* \param pArray ArrayList* 指向ArrayList對象的指針
* \param i int 插入的位置雨涛,i>=0
* \param e int 插入的元素
* \return bool 如果插入成功則返回true懦胞,否則返回false
*
*/
bool arrayListInsert(ArrayList* pArray,int index,int e)
{
if(arrayListFull(pArray)){
printf("ArrayList滿了!\n");
return false;
}else{
for(int i = pArray->currentLen;i > index;i--){
pArray->pArray[i]=pArray->pArray[i-1];
}
pArray->pArray[index]=e;
pArray->currentLen++;
return true;
}
}
/** \brief 求ArrayList的元素數(shù)量
*
* \param pArray const ArrayList*const 指向ArrayList對象的指針
* \return int 返回ArrayList中元素的數(shù)量
*
*/
int arrayListLen(const ArrayList*const pArray)
{
return pArray->currentLen;
}
/** \brief 移除ArrayList中指定位置的元素,位置從0開始
*
* \param pArray ArrayList* 指向ArrayList對象的指針
* \param index int 需要移除的位置
* \param e int* 保存被移除的元素
* \return bool 移除成功則返回true躏尉,否則返回false
*
*/
bool arrayListRemove(ArrayList* pArray,int index,int * e)
{
if(pArray==NULL){
printf("ArrayList沒有初始化!\n");
return false;
}
if(arrayListEmpty(pArray)){
printf("ArrayList是空的蚯根!\n");
return false;
}
if(index<0 || index>pArray->currentLen-1){
printf("下標越界!\n");
return false;
}
*e = pArray->pArray[index];
for(int i = index;i<pArray->currentLen;i++){
pArray->pArray[i]=pArray->pArray[i+1];
}
pArray->currentLen--;
return true;
}
/** \brief 輸出ArrayList對象
*
* \param pArray const ArrayList*const 指向ArrayList的指針
* \return void
*
*/
void arrayListShow(const ArrayList*const pArray)
{
if(arrayListEmpty(pArray)){
printf("ArrayList是空的!\n");
return;
}
for(int i = 0; i < pArray->currentLen;i++){
printf("%d:%d\n",i+1,pArray->pArray[i]);
}
return;
}
/** \brief 對ArrayList進行升序排序
*
* \param ArrayList*pArray 指向ArrayList對象的指針
* \param rev bool 是否降序
* \return void
*
*/
void arrayListSort(ArrayList*pArray,bool rev)
{
if(arrayListEmpty(pArray)){
printf("ArrayList是空的!\n");
return;
}
for(int i = 0; i < pArray->currentLen;i++){
for(int j = i+1;j<pArray->currentLen;j++){
if(pArray->pArray[j]<pArray->pArray[i]){
int temp = pArray->pArray[i];
pArray->pArray[i]=pArray->pArray[j];
pArray->pArray[j]=temp;
}
}
}
if(rev==true){
arrayListReverse(pArray);
}
return;
}
/** \brief 原地翻轉(zhuǎn)ArrayList
*
* \param ArrayList*pArray 指向ArrayList對象的指針
* \return void
*
*/
void arrayListReverse(ArrayList*pArray)
{
if(arrayListEmpty(pArray)){
return;
}
int s=0,e=pArray->currentLen-1,temp;
while(s<e){
temp = pArray->pArray[s];
pArray->pArray[s] = pArray->pArray[e];
pArray->pArray[e]=temp;
s++;
e--;
}
return;
}
/** \brief 判斷ArrayList中是否包含某個元素e
*
* \param pArray const ArrayList*const 指向ArrayList對象的指針
* \param e int 要查找的元素
* \return bool 如果包含該元素,則返回true胀糜,否則返回false
*
*/
bool arrayListContain(const ArrayList*const pArray,int e)
{
if(arrayListIndex(pArray,e)!=-1){
return true;
}
return false;
}
/** \brief 得到元素e的index
*
* \param pArray const ArrayList*const 指向ArrayList對象的指針
* \param e int 需要獲取index的元素
* \return int 返回e在ArrayList中的index颅拦,如果不存在,則返回-1
*
*/
int arrayListIndex(const ArrayList* const pArray,int e)
{
if(arrayListEmpty(pArray)){
printf("ArrayList是空的!\n");
return -1;
}else{
int len = pArray->currentLen;
for(int i = 0;i < len;i++){
if(pArray->pArray[i]==e){
return i;
}
}
printf("沒有這個元素!\n");
return -1;
}
}
array_list_test.c
測試ArrayList
的代碼教藻。
#include"array_list.h"
#include<stdio.h>
#include<stdbool.h>
/** \brief 測試ArrayList的代碼
*
* \param void
* \return int
*
*/
int main(void)
{
ArrayList myList; // 創(chuàng)建ArrayList對象
arrayListInit(&myList,400); // 初始化
arrayListShow(&myList);
// 追加元素
arrayListAppend(&myList,56);
arrayListAppend(&myList,32);
arrayListAppend(&myList,24);
arrayListAppend(&myList,12);
arrayListAppend(&myList,58);
arrayListShow(&myList);
printf("-------\n");
arrayListInsert(&myList,1,24); // 在指定的位置插入元素
arrayListShow(&myList);
printf("-------\n");
int e;
arrayListRemove(&myList,2,&e); // 刪除指定位置的元素
printf("被刪除的元素是:%d\n",e);
arrayListShow(&myList);
printf("排序以后\n");
arrayListSort(&myList,false);
arrayListShow(&myList);
printf("翻轉(zhuǎn)\n");
arrayListReverse(&myList);
arrayListShow(&myList);
printf("在第一個位置插入一個元素10\n");
arrayListInsert(&myList,0,10);
arrayListShow(&myList);
return 0;
}