一,扩展数组链表
这几个步骤
- 初始化数组的信息
- 插入数据
- 删除数据(位置,数组)
销毁数组
//定义保存数组的信息的结构体 struct ListNode { //保存数组的首地址 void** addr; int Capcatity //数组的长度 int m_size; //数组的大小 } ListNode;
// 2017/9/3 星期日
#ifndef _win_DynamicArray_H
#define _win_DynamicArray_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//保存动态数组的信息动态数组的结构体
typedef struct DynamicArrary
{
void** addr; //地址
int m_Capcatiy; //容量
int m_size; //大小
} DynamicArrary;
//1,初始化
DynamicArrary* Init_DynamicArrary(int catcaptiy);
//2,插入数据
void Insert_DynamicArraryData(DynamicArrary* arr, int pos, void* data);
//3,删除数据
void Remove_DynamicArraryData(DynamicArrary* arr, int pos);
//4,打印数据
void Printf_DaynamicArraryData(DynamicArrary* arr, void (MyFunc)(void*));
//5,销毁链表
void Destory_DaynamicArrary(DynamicArrary* arr);
//用户
typedef struct Pos
{
char name[40];
int age;
} Pos;
void MyPrintf(void* p);
void test();
#endif
#define _CRT_SECURE_NO_WARNINGS
#include "DynamicArray.h"
//1,初始化
DynamicArrary* Init_DynamicArrary(int catcaptiy)
{
//1,判断条件
if (catcaptiy <= 0) return NULL;
//2,开辟内存初始化数组的信息
DynamicArrary* arr = (DynamicArrary*)malloc(sizeof(DynamicArrary));
arr->addr = (void**)malloc(sizeof(void*) * catcaptiy);
arr->m_Capcatiy = catcaptiy;
arr->m_size = 0;
return arr;
}
//2,插入数据
void Insert_DynamicArraryData(DynamicArrary* arr, int pos, void* data)
{
//1,判断条件
if (!arr) return; if (!data) return;
if (pos < 0 || pos > arr->m_size)
pos = arr->m_size;
//2,判断数组是否越界,越界的处理
if (arr->m_size >= arr->m_Capcatiy)
{
int newCapcatiy = arr->m_Capcatiy * 2;
//申请新的一块内存
void** arrary = (void**)malloc(sizeof(void*) * newCapcatiy);
//拷贝内存地址
memcpy(arrary, arr->addr, sizeof(void*) * arr->m_Capcatiy);
//改变数组的信息
free(arr->addr);
arr->addr = arrary;
arr->m_Capcatiy = newCapcatiy;
}
//3,插入数组
int i;
for (i = arr->m_size; i >= pos; i--)
arr->addr[i] = arr->addr[i - 1];
arr->addr[pos] = data;
arr->m_size++;
}
//3,删除数据
void Remove_DynamicArraryData(DynamicArrary* arr, int pos)
{
//1,条件判断
if (!arr) return;
if (pos < 0 || pos > arr->m_size) return;
int i;
for (i = pos; i < arr->m_size; i++)
{
arr->addr[i] = arr->addr[i + 1];
}
arr->m_size--;
}
//4,打印数据
void Printf_DaynamicArraryData(DynamicArrary* arr, void (*MyFunc)(void*))
{
//1,判断条件
if (!arr) return;
if (!MyFunc) return;
int i;
for (i = 0; i < arr->m_size; i++)
MyFunc(arr->addr[i]);
}
//5,销毁链表
void Destory_DaynamicArrary(DynamicArrary* arr)
{
//条件判断
if (!arr) return;
if (arr->addr)
{
free(arr->addr);
arr->addr = NULL;
}
free(arr);
}
//用户
void MyPrintf(void* p)
{
Pos* p1 = (Pos*)p;
printf("%s, %d\n", p1->name, p1->age);
}
void test()
{
Pos p1 = { "sdlkfj1", 1 };
Pos p2 = { "sdlkfj2", 2 };
Pos p3 = { "sdlkfj3", 3 };
Pos p4 = { "sdlkfj4", 4 };
Pos p5 = { "sdlkfj5", 5 };
Pos p6 = { "sdlkfj6", 6 };
DynamicArrary* arr = Init_DynamicArrary(5);
Insert_DynamicArraryData(arr, 0, &p1);
Insert_DynamicArraryData(arr, 0, &p2);
Insert_DynamicArraryData(arr, 0, &p3);
Insert_DynamicArraryData(arr, 0, &p4);
Insert_DynamicArraryData(arr, 0, &p5);
Insert_DynamicArraryData(arr, 0, &p6);
Printf_DaynamicArraryData(arr, MyPrintf);
Remove_DynamicArraryData(arr, 3);
printf("------------------\n");
Printf_DaynamicArraryData(arr, MyPrintf);
Destory_DaynamicArrary(arr);
}