Data structure --- continuous storage array algorithm

Module one of data structure: linear structure

  • Linear structure: put all nodes in a straight line and
    store them continuously (array)
    (1) What is an array: the element type is the same, the size is the same
    (2) The advantages and disadvantages of the array:
    advantages :
  • The array definition is simple and easy to access.
    Disadvantages :
  • All element array element types must be the same
  • The size of the array must be given when it is defined, and in most cases, once the size of the array space is determined, it cannot be changed
  • The space of the array must be contiguous , which causes the array to find a contiguous memory space when allocating space in the memory. Therefore, the array cannot be defined too large, because there can not be so much continuous memory space in the memory , and the way to solve this problem is to use a linked list .

Continuous storage array algorithm implementation

#include <stdio.h>
#include <malloc.h>  //包含了malloc
#include <stdlib.h>   //包含了exit

typedef enum __bool {
    
     false = 0, true = 1, } bool;

//定义了一个数据类型,该数据类型名为 struct Arr
//该数据类型含有三个成员,分别是pBase,len,cnt
struct Arr
{
    
    
	int * pBase;  //存储的是数组第一个元素的地址
	int len;  //数组所能容纳的最大元素的个数
	int cnt;  //当前数组有效元素的个数
	//int increment;   //自动增长因子
};

void init_arr(struct Arr *pArr,int length);   //初始化
bool append_arr(struct Arr *pArr,int val);  //追加
bool insert_arr(struct Arr *pArr,int pos,int val); //插入 pos的值从1开始
bool delete_arr(struct Arr *pArr,int pos,int * pVal);   //删除
//int get();      //获取值
bool is_empty(struct Arr * pArr); //为空?
bool is_full(struct Arr * pArr);   //满了?
void sort_arr(struct Arr * pArr);   //排序
void show_arr(struct Arr * pArr);  //显示
void inversion_arr(struct Arr * pArr);  //倒置

void init_arr(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(0 == pArr->cnt)
		return true;
	else
		return false;
}

void show_arr(struct Arr * pArr)
{
    
    
	if(is_empty(pArr))
	{
    
    
		printf("数组为空,请输入数组内容\n");
	}
	else
	{
    
    
		for (int i = 0; i < pArr->cnt; ++i)
		{
    
    
			printf("%d  ",pArr->pBase[i]);
		}
		printf("\n");
	}
}

bool is_full(struct Arr * pArr)
{
    
    
    if(pArr->cnt == pArr->len)
        return true;
    else
        return false;
}

bool append_arr(struct Arr *pArr,int val)
{
    
    
    //满时返回false
    if(is_full(pArr))
    {
    
    
        return false;
    }

    //不满时追加
    pArr->pBase[pArr->cnt] = val;
    (pArr->cnt)++;
    return true;
}

bool insert_arr(struct Arr *pArr,int pos,int val)
{
    
    
    int i;

    if(is_full(pArr))
        return false;

    if(pos < 1 || pos > pArr->cnt+1)
        return false;

    for(i = pArr->cnt-1; i >= pos-1; --i)
    {
    
    
        pArr->pBase[i+1] = pArr->pBase[i];
    }
    pArr->pBase[pos-1] = val;
    pArr->cnt ++;   //插入完成,个数加1

    return true;
}

bool delete_arr(struct Arr *pArr,int pos,int * pVal)
{
    
    
    if(is_empty(pArr))
        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)
{
    
    
    int i = 0;
    int j = pArr->cnt-1;

    while(i < j)
    {
    
    
        int tmp = pArr->pBase[i];
        pArr->pBase[i] = pArr->pBase[j];
        pArr->pBase[j] = tmp;
        ++i;
        --j;
    }
}

//排序
void sort_arr(struct Arr * pArr)
{
    
    
    int i,j;

    for(i = 0; i < pArr->cnt; ++i)
    {
    
    
        for(j = i+1; j < pArr->cnt; ++j)
        {
    
    
            if(pArr->pBase[i] > pArr->pBase[j])
            {
    
    
                int tmp = pArr->pBase[i];
                pArr->pBase[i] = pArr->pBase[j];
                pArr->pBase[j] = tmp;
            }
        }
    }
}

//一个简单的测试,用户可以根据实际情况进行选择使用
//这个连续数组存储还不够完善,用户可以自行添加 查找,删除所有等方法
//方法添加方法与上述类似
int main(void)
{
    
    
	struct Arr arr;
    int val;

	init_arr(&arr,6);
	show_arr(&arr);
	append_arr(&arr,1);
	//delete_arr(&arr,1,&val);
	append_arr(&arr,4);
	append_arr(&arr,3);
	append_arr(&arr,6);
	append_arr(&arr,2);
	if(delete_arr(&arr,1,&val))
    {
    
    
        printf("删除成功\n");
        printf("您删除的元素是:%d\n",val);
    }
	//append_arr(&arr,5);
	//insert_arr(&arr,1,99);

	/*insert_arr(&arr,6,99);
	if(!insert_arr(&arr,7,100))
    {
        printf("插入失败\n");
    }
	//append_arr(&arr,6);
	//append_arr(&arr,7);
	if(append_arr(&arr,8))
    {
        printf("追加成功\n");
    }
    else
    {
        printf("追加失败\n");
    }*/

    //倒置测试
    inversion_arr(&arr);
	show_arr(&arr);

	//排序测试
	sort_arr(&arr);
    show_arr(&arr);

	//printf("%d\n",arr.len);
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_41782149/article/details/91345917