企业绿色发展助力/seo交流qq群
算法的定义
数据结构研究的是数据的存储和数据的操作的一门学问。
数据的存储分为两个部分:
1. 个体的存储
2. 个体关系的存储
从某个角度而言,数据的存储最核心的就是个体关系的存储
个体的存储可以忽略不计
狭义的算法是与数据的存数方式密切相关
广义的算法是与数据的存储方式无关
泛型:利用某种技术达到的效果就是:不同的存数方式,执行的操作是一样的。
同一种逻辑结构,无论该逻辑结构物理存储是什么,我们都可以对它执行相同的操作。
数组
线性结构:可以把所有节点用一根直线串起来,包括数组和链表。
数据结构中的数组并不是我们印象中的编程语言中包含的具体数据结构。
数组,在内存中以一组连续的数据集合的形式存储相同数据类型的数据。数组内的数据可以随机访问。
数组的优缺点
优点:存取速度很快。
缺点:事先必须知道数组的长度;
插入删除元素很慢;
空间通常是有限制的;
需要大块连续的内存块。
我们对数组进行二次封装,在内存上申请10个int长度进行数据的操作,并封装初始化、添加、插入、显示、删除、排序和反序接口。
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>typedef unsigned int BOOL;typedef struct ArrayList
{int * pArrayBaseAddr;int length;int currentLeng;
}ARRAY,*PARRAY;void init_arr(PARRAY pArray, int arrlength);
BOOL is_fullarr(PARRAY pArray);
BOOL is_emptyarr(PARRAY pArray);
BOOL append_arr(PARRAY pArray, int addNumber);
BOOL insert_arr(PARRAY pArray, int addNumber, int port); //port位置标号从1开始
void show_arr(PARRAY pArray);
BOOL delete_arr(PARRAY pArray, int port, int *pval);
BOOL sort_arr(PARRAY pArray, int mode);
void inversion_arr(PARRAY pArray);int main()
{PARRAY parraylist;int length = 10;int val = 0;init_arr(parraylist, length);append_arr(parraylist, 19);append_arr(parraylist, 2);append_arr(parraylist, 90);append_arr(parraylist, 76);append_arr(parraylist, 34);append_arr(parraylist, 82);append_arr(parraylist, 100);show_arr(parraylist);printf("\n");
// insert_arr(parraylist, 8, 8);
/* if(delete_arr(parraylist, 7, &val)){printf("delete succeed!!\n");printf("the delete number is %d\n", val);}*/sort_arr(parraylist, 1);
// inversion_arr(parraylist);show_arr(parraylist);return 0;
}void init_arr(PARRAY pArray, int arrlength)
{pArray->pArrayBaseAddr = (int *)malloc(sizeof(int)* arrlength);if(pArray->pArrayBaseAddr != NULL){printf("ArrayList init is OK!\n");pArray->length = arrlength;pArray->currentLeng = 0;}else{printf("ArrayList init is Error!\n");}
}BOOL is_fullarr(PARRAY pArray)
{if (pArray ->currentLeng == pArray ->length){return true;}else{return false;}
}BOOL is_emptyarr(PARRAY pArray)
{if (pArray ->currentLeng == 0){return true;}else{return false;}
}BOOL append_arr(PARRAY pArray, int addNumber)
{if ( is_fullarr( pArray ) ) //不需要==true{printf ("Array is Full!!\n");return false;}else{(pArray->pArrayBaseAddr)[pArray->currentLeng] = addNumber;pArray->currentLeng ++ ;return true;}
}BOOL insert_arr(PARRAY pArray, int addNumber, int port)
{if ( is_fullarr( pArray ) ){printf ("Array is Full!!\n");return false;}else if( (port < 0) || (port > (pArray->currentLeng +1)) ){printf("the port is error!\n");return false;}else{for(int i = pArray->currentLeng; i >= port; i-- ){(pArray->pArrayBaseAddr)[ i ] = (pArray->pArrayBaseAddr)[ i-1 ];}(pArray->pArrayBaseAddr)[port-1] = addNumber;pArray->currentLeng ++ ;return true;}
}void show_arr(PARRAY pArray)
{if ( is_emptyarr( pArray ) ){printf(" Array is Empty!!\n ");}else{for(int i = 0; i < (pArray->currentLeng); i++){printf("num %d is %d\n", i+1, (pArray->pArrayBaseAddr)[i]);}}
}BOOL delete_arr(PARRAY pArray, int port, int *pval)
{if (is_emptyarr(pArray)){printf(" Array is Empty!!\n ");return false;}else if( (port < 0) || (port > (pArray->currentLeng)) ){printf("the port is error!\n");return false;}/*如果port与currentLeng相等就不需要移动,直接将currentLeng减1*/else{*pval = (pArray->pArrayBaseAddr)[port -1];for (int i =port; i < pArray->currentLeng; i++){(pArray->pArrayBaseAddr)[i-1] = (pArray->pArrayBaseAddr)[i];}pArray->currentLeng -- ;return true;}
}BOOL sort_arr(PARRAY pArray, int mode)
{int swap = 0;if (is_emptyarr(pArray)){printf("Array is empty!!\n");return false;}else if (mode == 0){for(int i = 0; i < (pArray->currentLeng-1); i ++){for(int j = i + 1; j < (pArray->currentLeng); j++){if( (pArray->pArrayBaseAddr)[i] > (pArray->pArrayBaseAddr)[j] ){swap = (pArray->pArrayBaseAddr)[j];(pArray->pArrayBaseAddr)[j] = (pArray->pArrayBaseAddr)[i];(pArray->pArrayBaseAddr)[i] = swap;}}}return true;}else if (mode == 1){for(int i = 0; i < (pArray->currentLeng-1); i ++){for(int j = i + 1; j < (pArray->currentLeng); j++){if( (pArray->pArrayBaseAddr)[i] < (pArray->pArrayBaseAddr)[j] ){swap = (pArray->pArrayBaseAddr)[j];(pArray->pArrayBaseAddr)[j] = (pArray->pArrayBaseAddr)[i];(pArray->pArrayBaseAddr)[i] = swap;}}}return true;}
}void inversion_arr(PARRAY pArray)
{int i = 0;int j = pArray->currentLeng -1;int swap = 0;while(i<j){swap = (pArray->pArrayBaseAddr)[i];(pArray->pArrayBaseAddr)[i] = (pArray->pArrayBaseAddr)[j];(pArray->pArrayBaseAddr)[j] = swap;i ++;j --;}
}