六、连续存储数组
1.什么是线性结构?
把所有的结点用一根直线穿起来
2.线性结构分为几种?
连续存储【数组】
(1)什么叫数组
元素类型相同,大小相等
(2)数组的优缺点:
离散存储【链表】
arraylist是连续存储数组
linkedlist是离散存储链表
arraylist的方法
3.Arraylist的示例
头文件
#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);
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 * pVal);
int get(struct Arr * pArr, int pos);
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);
tips:
这样写无法改变arr中成员的值
所以应该使用指针来传参
如果内存满了,那么是无给配pBase分配动态内存
这时pBase为NULL
完成初始化,return可以终止程序
init_arr方法最终形式
void init_arr(struct Arr * pArr, int length)
{
pArr->pBase = (int )malloc(sizeof(int) length);
if (NULL == pArr->pBase)
{
printf(“动态内存分配失败!n”);
exit(-1); //终止整个程序
}
else
{
pArr->len = length;
pArr->cnt = 0;
}
return; //完成要return让人知道程序终止了
}
这里最好写地址,形参为指针类型只占4个字节
show_arr的思路
编写is_empty方法,如果有效个数cnt为0则为空
bool is_empty(struct Arr * pArr)
{
if (0 == pArr->cnt)
return true;
else
return false;
}
show_arr方法
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]);//pBase是第一个元素的地址,所以pBase[i]得到的是第i个元素的值
printf(“n”);
}
}
full方法
bool is_full(struct Arr * pArr)
{
if (pArr->cnt == pArr->len)
return true;
else
return false;
}
append_arr方法
bool append_arr(struct Arr * pArr, int val)
{
//满时返回false
if (is_full(pArr))
return false;
//不满时追加
pArr->pBase[pArr->cnt] = val;
pArr->cnt++;
return true;
}
insert_arr方法
现将其他数往后移动,再插入数据
bool insert_arr(struct Arr * pArr, int pos, int val)
{
if (is_full(pArr))
return false;
if (pos < 1 || pos > pArr->cnt)
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;
}
delete方法
bool delete_arr(struct Arr * pArr, int pos, int * pVal)
{
//如果数组为空则返回
if (is_empty(pArr))
return false;
//如果pos不在有效范围内返回
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;
}
get方法
int get(struct Arr * pArr, int pos)
{
return pArr->pBase[pos - 1];
}
inversion_arr方法
void inversion_arr(struct Arr * pArr)
{
if (is_empty(pArr))
{
return;
}
int i = 0;
int j = pArr->cnt - 1;
//利用t来给它们互换
int t = 0;
//互换的之后两边依次接近中间的数,最后中间那个数是不用转换的!
while (i < j)
{
t = pArr->pBase[i];
pArr->pBase[i] = pArr->pBase[j];
pArr->pBase[j] = t;
i++;
j–;
}
return;
}
sort_arr方法
使用了冒泡pa
void sort_arr(struct Arr * pArr)
{
int i, j, t;
for (i = 0; i < pArr->cnt; i++)
{
for (j = i + 1; j < pArr->cnt; j++)
{
if (pArr->pBase[i] > pArr->pBase[j])
{
t = pArr->pBase[j];
pArr->pBase[j] = pArr->pBase[i];
pArr->pBase[i] = t;
}
}
}
return;
}
本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!