数据结构是计算机存储、组织数据的方式,是相互之间存在一种或多种特定关系的数据元素集合
目录
算法
算法和数据结构的区别
数据结构分类
动态数组
单向链表
栈
算法是特定问题求解步骤的描述,在计算机中表示为指令的有限序列,算法是独立存在的一种解决问题的方法和思想。
对于算法而言,语言不重要,重要的是思想。
算法具有五个基本的特性:输入、输出、有穷性、确定性和可行性
数据结构分为逻辑结构(集合结构,线性结构,树形结构,图形结构)和物理结构/存储结构(顺序存储,链式存储)
线性 1:1关系 除了第一个元素没有前驱,最后一个元素没有后继,其他元素都有唯一的前驱和唯一的后继。
树形 1:n关系
图形 n:n关系
初始化,插入数据,遍历数据,
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
//动态数组的结构体
struct dynamicArray
{
void ** pAddr;//维护在堆区真实数组指针
int m_capacity;//数组容量
int m_size;//数组大小
};
//初始化数组
struct dynamicArray * init_DynamicArray(int capacity)
{
if (capacity <= 0)
{
return NULL;
}
struct dynamicArray * array = (dynamicArray*)malloc(sizeof(struct dynamicArray));
//判断内存是否申请成功
if (array == NULL)
{
return NULL;
}
//设置容量
array->m_capacity = capacity;
//设置大小
array->m_size = 0;
//维护在堆区数组的指针
array->pAddr =(void**) malloc(sizeof(void*) * array->m_capacity);
return array;
}
//插入功能
void insert_dynamicArray(struct dynamicArray* array, int pos, void* data)
{
if (array == NULL)
{
return;
}
if (data == NULL)
{
return;
}
if (pos < 0 || pos>array->m_size)
{//无效的位置 进行尾插
pos = array->m_size;
}
//先判断是否已经满载,满了就动态开辟
if (array->m_size >= array->m_capacity)
{
//申请一个更大的空间
int newCapacity = array->m_capacity * 2;
//创建新空间
void** newSpace = (void**) malloc(sizeof(void*) * newCapacity);
//将原来的数据拷到新空间
memcpy(newSpace, array->pAddr, sizeof(void*) * array->m_capacity);
//释放原有的空间
free(array->pAddr);
//更改指针指向
array->pAddr = newSpace;
//更新容量大小
array->m_capacity = newCapacity;
}
//插入新数据,从最后一个数据 依次后移
for (int i = array->m_size - 1; i >= pos; i--)
{
array->pAddr[i + 1] = array->pAddr[i];
}
//将新元素插入指定位置
array->pAddr[pos] = data;
//更新size大小
array->m_size++;
}
//遍历数组
void foreach_DynamicArray(struct dynamicArray* array,void (*myForeach)(void*))
{
if (array == NULL) {
return;
}
if (myForeach == NULL) {
return;
}
for (int i = 0; i < array->m_size; i++) {
myForeach(array->pAddr[i]);
}
}
//删除数组中元素 按照指定位置
void removebyPos_DynamicArray(struct dynamicArray * arr,int pos)
{
if (arr == NULL) {
return;
}
if (pos<0 || pos>arr->m_size - 1) {//无效位置
return;
}
for (int i = pos; i < arr->m_size - 1; i++)//从pos位置开始到数组尾 数据前移
{
arr->pAddr[i] = arr->pAddr[i + 1];
}
arr->m_size--;
}
//删除数组中元素 按照值
void removebyvalue_DynamicArray(struct dynamicArray * arr,void* data,int (*myCompare)(void*,void*)) {
if (arr == NULL||data==NULL) { return; }
for (int i = 0; i < arr->m_size; i++) {
if (myCompare(arr->pAddr[i], data)) {
removebyPos_DynamicArray(arr, 1);
break;
}
}
}
//销毁
void destroy_DynamicArray(struct dynamicArray * arr) {
if (arr == NULL) { return; }
if (arr ->pAddr != NULL) {
free(arr->pAddr);
arr->pAddr = NULL;
}
free(arr);
arr = NULL;
}
struct Person {
char name[64];
int age;
};
void myPrintPerson (void * data) {
struct Person * p =(Person*) data;
printf("姓名,%s 年龄%d\n", p->name, p->age);
}
int myComparePerson(void* data1,void*data2) {
struct Person* p1 =(Person*) data1;
struct Person* p2 = (Person*)data2;
return strcmp(p1->name, p2->name) == 0 && p1->age == p2->age;
}
void test01() {
//创建动态数组
struct dynamicArray* arr = init_DynamicArray(5);
struct Person p1 = { "a",18 };
struct Person p2 = { "b",15 };
struct Person p3 = { "c",28 };
struct Person p4 = { "d",13 };
struct Person p5 = { "e",16 };
struct Person p6 = { "f",19 };
//将数据插入动态数组中
printf("当前的容量为:%d\n", arr->m_capacity);
insert_dynamicArray(arr, 0, &p1);
insert_dynamicArray(arr, 0, &p2);
insert_dynamicArray(arr, 0, &p3);
insert_dynamicArray(arr, 0, &p4);
insert_dynamicArray(arr, 0, &p5);
insert_dynamicArray(arr, 0, &p6);
// f e d c b a
//遍历
foreach_DynamicArray(arr, myPrintPerson);
printf("插入数据后的容量为:%d\n", arr->m_capacity);
//删除
removebyPos_DynamicArray(arr, 1);
printf("删除第一个位置的元素");
foreach_DynamicArray(arr, myPrintPerson);
struct Person p = { "f",19 };
removebyvalue_DynamicArray(arr, &p, myComparePerson);
printf("------------------------------");
foreach_DynamicArray(arr, myPrintPerson);
//销毁
destroy_DynamicArray(arr);
arr = NULL;
}
; int main() {
test01();
system("pause");
return EXIT_SUCCESS;
}
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
//链表结点
struct LinkNode {
void* data;//数据域
struct LinkNode* next;//指针域
};
//链表结构体
struct LList {
struct LinkNode pHeader;//头节点
int m_size;//链表长度
};
typedef void* LinkList;
//初始化链表
LinkList init_LinkList() {
struct LList* myList = (LList* )malloc(sizeof(struct LList));
if (myList == NULL) {
return NULL;
}
myList->pHeader.data = NULL;
myList->pHeader.next = NULL;
myList->m_size =0;
return myList;
}
//插入结点
void insert_LinkList(LinkList list, int pos, void* data) {
if (list == NULL || data == NULL) { return; }
struct LList* myList =(LList *) list;
if (pos < 0||pos> myList->m_size) {
//无效位置 尾插
pos = myList->m_size;
}
//创建临时节点
struct LinkNode* pCurrent = &myList->pHeader;
//通过循环找到插入位置的前驱结点
for (int i = 0; i < pos; i++) {
pCurrent = pCurrent->next;
}
//创建新结点
struct LinkNode*newNode = (LinkNode*)malloc(sizeof(struct LinkNode));
newNode->data = data;
newNode->next= NULL;
//新结点插入链表中
newNode->next = pCurrent->next;
pCurrent->next = newNode;
myList->m_size++;//更新链表长度
}
//遍历链表
void foreach_LinkList(LinkList list ,void (*myPrint)(void*) ) {
if(list == NULL) { return; }
struct LList* myList = (LList*)list;
struct LinkNode* pCurrent = myList->pHeader.next;//找到第一个有数据的结点
for (int i = 0; i < myList->m_size; i++) {
myPrint(pCurrent->data);
pCurrent = pCurrent->next;
}
}
//删除结点
void removeby_LinkList(LinkList list, int pos) {
if (list == NULL) { return; }
struct LList* myList =(LList*) list;
if (pos<0||pos>myList->m_size-1) {
return;
}
//找到删除结点前驱
struct LinkNode* pCurrent = &myList->pHeader;
for (int i = 0; i < pos; i++) {
pCurrent = pCurrent->next;
}
//缓存中 待删除
struct LinkNode* pDel = pCurrent->next;
//建立关系
pCurrent->next = pDel->next;
//释放
free(pDel);
pDel = NULL;
//更新长度
myList->m_size--;
}
//删除 值
void removebyValue_LinkList(LinkList list,void* data,int (*myCompare)(void*,void*) ) {
if (list == NULL||data==NULL) { return; }
struct LList* myList =(LList*) list;
//创建两个辅助指针变量
struct LinkNode* pPrev = &myList->pHeader;
struct LinkNode* pCurrent = pPrev->next;
for (int i = 0; i < myList->m_size; i++) {//给用户来对比
if (myCompare(pCurrent->data, data)) {
//更改指针指向
pPrev->next = pCurrent->next;
//释放
free(pCurrent);
pCurrent = NULL;
myList->m_size--;
break;
}
//后移
pPrev = pCurrent;
pCurrent=pCurrent->next;
}
}
//清空
void clear_LinkList(LinkList list) {
if (list == NULL) { return; }
struct LList* myList =(LList*) list;
struct LinkNode* pCurrent= myList->pHeader.next;
for (int i = 0; i < myList->m_size;i++) {
//记录后继结点
struct LinkNode* pNext =(LinkNode*) pCurrent->next;
free(pCurrent);
pCurrent = pNext;
}
myList->pHeader.next = NULL;
myList->m_size = 0;
}
//返回长度
int size_LinkList(LinkList list) {
if (list == NULL) { return -1; }
struct LList* myList =(LList*) list;
return myList->m_size;
}
//销毁
void destroy_LinkList(LinkList list) {
if (list == NULL) { return; }
clear_LinkList(list);
free(list);
list = NULL;
}
struct Person {
char name[64];
int age;
};
void myPPerson(void* data) {
struct Person* p = (Person*)data;
printf("字母: %s 数字: %d\n", p->name, p->age);
}
int myCompare(void* data1, void * data2) {
struct Person* p1 = (Person*) data1;
struct Person* p2 = (Person*) data2;
return strcmp(p1->name, p2->name) == 0 && p1->age == p2->age;
}
//调试
void test01() {
//初始化
LinkList list = init_LinkList();
//插入数据
struct Person p1 = { "a",18 };
struct Person p2 = { "b",15 };
struct Person p3 = { "c",28 };
struct Person p4 = { "d",13 };
struct Person p5 = { "e",16 };
struct Person p6 = { "f",19 };
//插入数据
insert_LinkList(list, 0, &p1);
insert_LinkList(list, 0, &p2);
insert_LinkList(list, 1, &p3);
insert_LinkList(list, 0, &p4);
insert_LinkList(list, 1, &p5);
insert_LinkList(list, 100, &p6);
// d e b c a f
printf("长度为: %d\n", size_LinkList(list));
foreach_LinkList(list, myPPerson);
//删除c
removeby_LinkList(list, 3);
printf("------------------\n");
foreach_LinkList(list, myPPerson);
printf("长度为: %d\n", size_LinkList(list));
//删除e
struct Person p = { "e", 16 };
removebyValue_LinkList(list, &p, myCompare);
printf("------------------\n");
foreach_LinkList(list, myPPerson);
printf("长度为: %d\n", size_LinkList(list));
//清空
clear_LinkList(list);
printf("------------------\n");
foreach_LinkList(list, myPPerson);
//销毁
destroy_LinkList(list);
}
int main() {
test01();
system("pause");
return EXIT_SUCCESS;
}
属于 先进后出的数据结构。一种特殊的线性表,始终在栈顶进行。
不能遍历(只有栈顶元素被外界访问到)
栈的顺序存储(数组)和栈的链式存储(链表)。
顺序结构
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#define MAX 1024
//栈的结构体
struct SStack {
void* data[MAX];//数组
//元素个数
int m_size;
};
typedef void* seqStack;
//初始化
seqStack init_SeqStack() {
struct SStack* stack = (SStack*)malloc(sizeof(struct SStack));
if (stack == NULL) { return NULL; }
//清空数组
memset(stack->data, 0, sizeof(void*) * MAX);
stack->m_size = 0;
return stack;
}
//入栈
void push_SeqStack(seqStack stack, void* data) {
if (stack == NULL||data==NULL) { return; }
//判断是否栈满
struct SStack* myStack = (SStack*) stack;
if (myStack->m_size == MAX) {
return;
}
myStack->data[myStack->m_size] = data;//尾插
myStack->m_size++;//更新大小
}
//出栈
void pop_SeqStack(seqStack stack ) {
if (stack == NULL ) { return; }
struct SStack* myStack = (SStack*)stack;
if (myStack->m_size <= 0) { return; }
myStack->data[myStack->m_size - 1] = NULL;//执行出栈
myStack->m_size--;//更新大小
}
//获取栈顶元素
void* top_SeqStack(seqStack stack) {
if (stack == NULL) { return NULL; }
struct SStack* myStack = (SStack*)stack;
if (myStack->m_size == 0) { return NULL; }
return myStack->data[myStack->m_size-1];
}
//栈的大小
int size_SeqStack(seqStack stack) {
if (stack == NULL) { return -1; }
struct SStack* myStack = (SStack*)stack;
return myStack->m_size;
}
//判空
int isEmpty_SeqStack(seqStack stack) {
if (stack == NULL) { return -1; }//真
struct SStack* myStack = (SStack*)stack;
if (myStack->m_size <= 0) {
return 1;//真
}
return 0;//假,不为空
}
//销毁
void destroy_SeqStack(seqStack stack){
if (stack == NULL) { return ; }
free(stack);
stack = NULL;
}
//测试
struct Person {
char name[64];
int age;
};
void test() {
//数据
struct Person p1 = { "aaa",10 };
struct Person p2 = { "bbb",20 };
struct Person p3 = { "ccc",30 };
struct Person p4 = { "ddd",40 };
struct Person p5 = { "eee",50 };
//初始化栈
seqStack init_SeqStack();
//入栈
//出栈
//。。。
}
; int main() {
void test();
system("pause");
return EXIT_SUCCESS;
}