赞
踩
目录
此专栏包含408考研数据结构全部内容,除其中使用到C++引用外,全为C语言代码。使用C++引用主要是为了简化指针的使用,避免二重指针的出现。
[数据结构]:01-顺序表(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:02-单链表(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:03-栈(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:04-循环队列(数组)(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:05-循环队列(链表)(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:06-队列(链表带头结点)(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:07-二叉树(无头结点)(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:08-顺序查找(顺序表指针实现形式)(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:09-二分查找(顺序表指针实现形式)(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:10-二叉排序树(无头结点)(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:11-冒泡排序(顺序表指针实现形式)(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:12-快速排序(顺序表指针实现形式)(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:13-插入排序(顺序表指针实现形式)(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:14-选择排序(顺序表指针实现形式)(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:15-堆排序(顺序表指针实现形式)(C语言实现)_Chandni.的博客-CSDN博客
[数据结构]:16-归并排序(顺序表指针实现形式)(C语言实现)_Chandni.的博客-CSDN博客
语言:C/C++14
编译器:MinGW64
集成开发环境:CLion2022.1.3
请在CLion集成开发环境中创建C++可执行程序,否则无法运行,原因上面已解释。
用于测试双链表。
- #include "./Head/LinkedListData.h"
- #include "./Source/LinkedListCommon.cpp"
- #include "./Source/LinkedListFunction.cpp"
-
- int main() {
- LinkedList L;
- InitLinkedList(L);
-
- // 为方便测试,这里选择使用数组形式创建单链表
- int array[MaxSize] = {1, 2, 3, 4, 5, 6, 0};
- int select = 0;// 0表示头插法创建;1表示尾插法创建
- LinkedListCreate(L, array, select);
- LinkedListPrint(L);
- printf("-----------------------------\n");
-
- // 增删改查
- // 查找
- LNode *CurrentNode;
- LinkedListSearch(L, 2, CurrentNode);
- printf("CurrentNode Value = %d\n", CurrentNode->data);
- printf("-----------------------------\n");
-
- // 插入
- LinkedListInsert(L, 2, 20);
- LinkedListPrint(L);
- printf("-----------------------------\n");
-
- // 删除
- bool flag = LinkedListDelete(L, 7);
- if (flag) {
- printf("true\n");
- } else {
- printf("false\n");
- }
- LinkedListPrint(L);
- printf("-----------------------------\n");
-
- // 修改
- LinkedListModify(L, 3, 90);
- LinkedListPrint(L);
- printf("-----------------------------\n");
- return 0;
- }

用于存储结构体和常量等。
- //
- // Created by 24955 on 2023-02-22.
- //
-
- #ifndef LINKEDLIST_LINKEDLISTDATA_H
- #define LINKEDLIST_LINKEDLISTDATA_H
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
- // 常量
- #define MaxSize 50
- typedef int ElemType;
-
- // 结构体
- typedef struct Node {
- ElemType data;
- struct Node *prior, *next;
- } LNode, *LinkedList;
- #endif //LINKEDLIST_LINKEDLISTDATA_H

用于存储双链表初始化和打印输出等公用函数。
- //
- // Created by 24955 on 2023-02-22.
- // 带头结点(第一个元素值域不存值)
- //
- // 初始化双链表
- void InitLinkedList(LinkedList &List) {
- List = (LNode *) malloc(sizeof(LNode));
- List->prior = NULL;
- List->next = NULL;
- }
-
- // 后插操作
- void BackWordInsert(LNode *&LN, ElemType InsertValue) {
- LNode *NewNode = (LNode *) calloc(1, sizeof(LNode));
- NewNode->data = InsertValue;
- if (LN->next != NULL) {
- // 尾部插入时不需要链接新结点next部分
- NewNode->next = LN->next;
- LN->next->prior = NewNode;
- }
- NewNode->prior = LN;
- LN->next = NewNode;
- }
-
- // 头插法创建双链表
- void LinkedListHeadCreate(LinkedList &List, int array[]) {
- /*
- * 1. 初始化新结点
- * 2. 先链接新结点next部分
- * 3. 再链接新结点prior部分*/
- for (int i = 0; array[i]; i++) {
- BackWordInsert(List, array[i]);
- }
- }
-
- // 尾插法创建双链表
- void LinkedListTailCreate(LinkedList &List, int array[]) {
- /*
- * 1. 初始化新结点
- * 2. 先链接新结点next部分
- * 3. 再链接新结点prior部分*/
- LinkedList tailPointer;
- tailPointer = List;
- for (int i = 0; array[i]; i++) {
- BackWordInsert(tailPointer, array[i]);
- // 修改尾指针指向
- tailPointer = tailPointer->next;
- }
- }
-
- // 创建模式选择函数
- void LinkedListCreate(LinkedList &List, int array[], int HeadTail) {
- /*
- * 1. 选择创建双链表的模式
- * 2. 调用相应函数创建双链表*/
- if (HeadTail == 0) {
- LinkedListHeadCreate(List, array);
- } else {
- LinkedListTailCreate(List, array);
- }
- }
-
- // 打印输出函数
- void LinkedListPrint(LinkedList List) {
- List = List->next;
- while (List) {
- printf("%3d", List->data);
- List = List->next;
- }
- printf("\n");
- }

用于存储双链表增删改查等操作。
- //
- // Created by 24955 on 2023-02-22.
- // 带头结点(第一个元素值域不存值)
- //
- // 按位置查找,并返回查找到的值
- void LinkedListSearch(LinkedList List, int position, LNode *&CurrentNode) {
- /*
- * 1. 判断位置是否合法,单链表是否创建
- * 2. 将制定定位到指定位置
- * 3. 判断指针状态,并赋值或输出*/
- if (position > 0 && List) {
- for (int i = 0; i < position && List; i++) {
- List = List->next;
- }
- // 获取所给位置结点
- CurrentNode = List;
- }
- }
-
- // 按位置插入元素
- void LinkedListInsert(LinkedList List, int position, ElemType InsertValue) {
- /*
- * 1. 定位到插入位置前一个指针处
- * 2. 判断指针状态并插入元素*/
- LNode *CurrentNode;
- if (position > 0 && List) {
- // 获取要插入结点的前驱结点
- LinkedListSearch(List, position - 1, CurrentNode);
- // 在该节点后面插入元素
- BackWordInsert(CurrentNode, InsertValue);
- }
- }
-
- // 按位置删除元素
- bool LinkedListDelete(LinkedList List, int position) {
- /*
- * 1. 定位到要删除位置前一个结点出
- * 2. 定义新指针并指向要删除元素
- * 3. 若要删除元素非NULL,则获取当前元素指针域并释放当前元素*/
- if (position > 0 && List) {
- LNode *PriorNode, *CurrentNode;
- LinkedListSearch(List, position - 1, PriorNode);
- // 需要删除的当前结点
- CurrentNode = PriorNode->next;
- // 修改指针
- PriorNode->next = CurrentNode->next;
- if (CurrentNode->next != NULL) {
- CurrentNode->next->prior = PriorNode;
- }
- free(CurrentNode);
- return true;
- } else {
- return false;
- }
- }
-
- // 按位置修改元素
- void LinkedListModify(LinkedList List, int position, ElemType modifyValue) {
- /*
- * 1. 定位到要修改位置获取其指针
- * 2. 修改元素值*/
- if (position > 0 && List) {
- LNode *CurrentNode;
- LinkedListSearch(List, position, CurrentNode);
- if (CurrentNode) {
- CurrentNode->data = modifyValue;
- }
- }
- }

此博客主要用于408考研数据结构C语言实现记录,内有不足,可留言,可讨论。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。