数据结构

数据结构

链表

链表是一种存储结构,指的是存储时候除了要存储数据元素外,还要数据元素一起的另外空间存储数据元素的关系。

单向链表

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct linklist {
int num;
linklist *next;
}linknode, *linklistp;
//在链表的头部插入节点
linklistp insert_head(linklistp head,linklistp newnode)
{
if (newnode!=nullptr)
{
    newnode->next = head;
    head = newnode;
}
return head;

}
//在链表的尾部插入节点
linklistp insert_tail(linklistp head, linklistp newnode)
{
if (newnode==nullptr)
    return head;
//当链表表头为没有元素时
if (head==nullptr)
{
    head = newnode;
}
else 
{   
    linklistp temp = head;
    while (temp->next!=nullptr)
        temp = temp->next;
    temp->next = newnode;
    newnode->next = nullptr;
}
return head;
}
linklistp insert_node(linklistp head, int a, int data)//在数据data前插入a
{
return head;
}//通过链表数据删除链表节点
linklistp delete_node(linklistp head, int data)
{
if (head==nullptr)
{
    return nullptr;
}
else if (head->num == data)
{
    head = head->next;
    return head;
}
linklistp temp = head;
linklistp pre = head;
while (temp!=nullptr && temp->num != data)
{
    pre = temp;//pre记录前一个节点
    temp = temp->next;
}
if (temp == nullptr)
    return nullptr;
else
    pre->next = temp->next;
free(temp);//释放temp所指对象的内存空间
return head;
}
//替换节点
linklistp replace_node(linklistp head,int data,int new_data)//在//用新节点替换data,新节点数据为new_data
{
linklistp temp = head;
if (temp->num == data)
{
    linklistp new_node = (linklistp)malloc(sizeof(linklist));
    new_node->num = new_data;
    new_node->next = temp->next;
    free(temp);
    return new_node;
}
linklistp pre=temp;

while (temp!= nullptr&&temp->num != data)
{
    pre = temp;
    temp = temp->next;
}
if (temp == nullptr)
{
    return nullptr;
}
else
{
    linklistp new_node = (linklistp)malloc(sizeof(linklist));
    new_node->num = new_data;
    new_node->next = temp->next;
    pre->next = new_node;
    free(temp);
}
return head;
}
void output(linklistp head)
{
linklistp temp = head;
while (temp)
{
    printf("%d  ", temp->num);
        temp = temp->next;
}
printf("\n");
}
linklistp cheak_node(linklistp head, int data)
{
linklistp temp = head;
while (temp != nullptr&&temp->num != data)
{
    temp = temp->next;
}
if (temp == nullptr)
    return nullptr;
return temp;
}
linklistp creat_list()
{
linklistp head = nullptr;
srand(time(nullptr));
for (int i = 1; i <6; i++)
{
    linklistp newnode = (linklistp)malloc(sizeof(linknode));
    newnode->num = rand() % 100;
    newnode->next = nullptr;//将节点的next赋值无空
    head = insert_tail(head, newnode);//插后面
    output(head);
    getchar();
}
for (int i = 1; i < 6; i++)
{
    linklistp newnode = (linklistp)malloc(sizeof(linknode));
    newnode->num = rand() % 100;
    newnode->next = nullptr;//将节点的next赋值无空
    head = insert_head(head, newnode);//插前面
    output(head);
    getchar();
}
return head;
}
int main()
{
linklistp head = creat_list();

int data = 0;
printf("please input num for delete");
scanf_s("%d",&data);
linklistp temp = delete_node(head, data);
if (temp==nullptr)
{
    printf("Can't seach data");
}
else
{
    head = temp;
    output(head);
}
printf("please input num for replace");
scanf_s("%d", &data);
printf("please input replace data");
int new_data = 0;
scanf_s("%d", &new_data);
temp = replace_node(head, data,new_data);
if (temp == nullptr)
{
    printf("Can't seach data for replace");
}
else
{
    head = temp;
    output(head);
}
printf("please input data fo cheak");
scanf_s("%d", &data);
temp=cheak_node(head, data);
if (temp == nullptr)
{
    printf("Can't find data");
}
else
{
    printf("found data");
}
return 0;
}

单向循环链表

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct linklist {
int num;
linklist *next;
}linknode, *linklistp;
void output(linklistp head)
{
linklistp temp = head;
while(temp->next != head)
{
    printf("%d  ", temp->num);
    temp = temp->next;
}
if (temp->next == head)
{
    printf("%d  ", temp ->num);
}
printf("\n");
}
linklistp insert_node(linklistp head,linklistp new_node)
{
if (new_node == nullptr)
    return head;
//当链表表头为没有元素时
if (head == nullptr)
{
    head = new_node;
    head->next = new_node;
}
else
{
    linklistp temp = head;
    while (temp->next != head)
        temp = temp->next;
    temp->next = new_node;
    new_node->next = head;
}
return head;
}
linklistp cheak_node(linklistp head, int data)
{
if (head == nullptr)
    return nullptr;
else
{
    linklistp temp = head;
    while (temp->next != head&&temp->num != data)
        temp = temp->next;
    if (temp->num == data)
        return temp;
    return nullptr;
}
}
linklistp delete_node(linklistp head, int data)
{
    if (head == nullptr)
    {
        return nullptr;
    }
    linklistp temp = head->next;
    linklistp pre = head;
    while (temp != head&&temp->num != data)
    {
        pre = temp;
        temp = temp->next;
    }
    if (temp->num != data)//当temp的值不为data说明没找到data
        return nullptr;
    else
    {
        if (temp->num == head->num)//判断temp是否为头节点
        {
            pre->next = temp->next;
            free(temp);
            return pre->next;
        }
        else
        {
            pre->next = temp->next;
            free(temp);
        }
        return head;
    }
}

linklistp replace_node(linklistp head, int data, int new_data)//用新节点替换data数据
{
if (head == nullptr)
{
    return nullptr;
}
linklistp temp = head->next;
linklistp pre = head;
while (temp != head&&temp->num != data)
{
    pre = temp;
    temp = temp->next;
}
if (temp->num != data)//当temp的值不为data说明没找到data
    return nullptr;
else
{
    linklistp new_node = (linklistp)malloc(sizeof(linklist));
    new_node->num = new_data;
    new_node->next = nullptr;
        if (temp->num == head->num)//判断temp是否为头节点
        {
        pre->next = new_node;
        new_node->next = temp->next;
        free(temp);
        return pre->next;
        }
    else
    {
        pre->next = new_node;
        new_node->next = temp->next;
        free(temp);
    }
}
return head;
}
linklistp creat_list()
{
linklistp head =nullptr;
srand(time(nullptr));
for (int i = 0; i < 8; i++)
{
    linklistp new_node = (linklistp)malloc(sizeof(linklist));
    new_node->num = rand() % 100;
    new_node->next = nullptr;
    head = insert_node(head, new_node);
    output(head);
    getchar();
}
return head;
}
int main()

{
linklistp head = creat_list();
int data;
linklistp temp = nullptr;
printf("please input a data for cheak.");
scanf_s("%d",&data);
temp = cheak_node(head, data);
if (temp)
    printf("Yes");
else
    printf("No");
printf("\n");
printf("please input a data for delete.");
printf("\n");
scanf_s("%d", &data);
temp = delete_node(head, data);
if (temp)
{
    head = temp;
    output(head);
}
else
    printf("No");
printf("\n");
printf("please input num for replace");
printf("\n");
scanf_s("%d", &data);
printf("please input replace data");
int new_data = 0;
scanf_s("%d", &new_data);
temp = replace_node(head, data, new_data);
if (temp == nullptr)
{
    printf("Can't seach data for replace");
}
else
{
    head = temp;
    output(head);
}
return 0;
}

双向链表

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct Dlinklist {
int num;
Dlinklist *next;
Dlinklist *pre;
}Dlinknode, *Dlinklistp;
void output(Dlinklistp head)
{
Dlinklistp temp = head;
    do
    {
        printf("%d  ", temp->num);  
        temp = temp->next;
    } while (temp->next);
    printf("\n");
    do
    {
        printf("%d  ", temp->num);
        temp = temp->pre;
    } while (temp);
}
Dlinklistp insert_node(Dlinklistp head, Dlinklistp new_node)
{
if (new_node == nullptr)
    return head;
if (head == nullptr)
    head = new_node;
else
{
    Dlinklistp temp = head;
    while (temp->next!=nullptr)
        temp = temp->next;
    temp->next = new_node;
    new_node->pre = temp;
}
return head;
}
Dlinklistp cheak_node(Dlinklistp head, int data)
{
Dlinklistp temp = head;
while (temp != nullptr&&temp->num != data)
{
    temp = temp->next;
}
if (temp == nullptr)
    return nullptr;
return temp;
}
Dlinklistp delete_node(Dlinklistp head, int data)
{
if (head == nullptr)
{
    return nullptr;
}
Dlinklistp temp = head;
if (temp->num == data)
{
    temp = temp->next;
    temp->pre = nullptr;
    free(head);
    return temp;
}
while(temp!=nullptr&&temp->num!=data)
    temp = temp->next;
if (temp == nullptr)
    return nullptr;
if (temp->next == nullptr)
{
    temp->pre->next = nullptr;
    return head;
}
temp->pre->next = temp->next;
temp->next->pre = temp->pre;
free(temp);
return head;
}
Dlinklistp replece_node(Dlinklistp head, int data, int new_data)
{
Dlinklistp temp = head;
if (temp) 
{
    Dlinklistp new_node = (Dlinklistp)malloc(sizeof(Dlinklist));
    new_node->num = new_data;
    new_node->next = nullptr;
    new_node->pre = nullptr;
    if (temp->num == data)
    {
        new_node->next = temp->next;
        temp->next->pre = new_node;
        free(temp);
        return new_node;
    }
    while (temp != nullptr&&temp->num != data)
    {
        temp = temp->next;
    }
    if (temp == nullptr)
        return nullptr;
    if (temp->next == nullptr)//当data在末尾时
    {
        temp->pre->next = new_node;
        new_node->pre = temp->pre;
        free(temp);
        return head;
    }
    //非末尾
    temp->pre->next = new_node;
    new_node->next = temp->next;
    new_node->pre = temp->pre;
    temp->next->pre = new_node;
    free(temp);
    return head;
}
return nullptr;
}
Dlinklistp creat_Dlist()
{
Dlinklistp head = nullptr;
srand(time(nullptr));
for (int i = 1; i <6; i++)
{
    Dlinklistp new_node = (Dlinklistp)malloc(sizeof(Dlinknode));
    new_node->num = rand() % 100;
    new_node->pre = nullptr; //将节点的pre赋值为空
    new_node->next = nullptr;//将节点的next赋值为空
    head = insert_node(head, new_node);
    output(head);
    getchar();
}
return head;
}
int main()
{
Dlinklistp head = creat_Dlist();
int data;
printf("please input a data for cheak");
printf("\n");
scanf_s("%d", &data);
Dlinklistp temp = cheak_node(head,data);
if (temp)
    printf("Yes");
else
    printf("No");
printf("\n");
printf("please input a data for delete");
printf("\n");
scanf_s("%d", &data);
temp = delete_node(head, data);
if (temp)
{
    head = temp;
    output(head);
}
else
    printf("No");
printf("\n");
printf("please input a data for delete");
printf("\n");
scanf_s("%d", &data);
int new_data;
scanf_s("%d", &new_data);
temp = replece_node(head, data,new_data);
if (temp)
{
    head = temp;
    output(head);
}
else
    printf("No");
printf("\n");
return 0;
}

双向循环链表

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct Dlinklist {
int num;
Dlinklist *next;
Dlinklist *pre;
}Dlinknode, *Dlinklistp;
void output(Dlinklistp head)
{
Dlinklistp temp = head;
if (temp)
{
    printf("%d  ", temp->num);
    while (temp->next != head)
    {
        temp = temp->next;
        printf("%d  ", temp->num);
    }
    printf("\n");
    while (temp != head)
    {
        printf("%d  ", temp->num);
        temp = temp->pre;
    } 
    printf("%d  ", temp->num);
}
}
Dlinklistp insert_node(Dlinklistp head, Dlinklistp new_node)
{
if (new_node == nullptr)
    return head;
if (head == nullptr)//头部自己形成一个环
{
    head = new_node;
    new_node->next = new_node;
    new_node->pre = new_node;
}
else
{
    Dlinklistp temp = head;
    while (temp->next != head)//遍历到与head相连的节点
        temp = temp->next;
    temp->next = new_node;
    new_node->pre = temp;
    new_node->next = head;
    head->pre = new_node;
}
return head;
}
Dlinklistp cheak_node(Dlinklistp head, int data)
{
if (head == nullptr)
    return nullptr;
else
{
    Dlinklistp temp = head;
    while(temp->next!=head&&temp->num!=data)
        temp = temp->next;
    if (temp->num == data)
        return temp;
    return nullptr;
}
}
Dlinklistp delete_node(Dlinklistp head, int data)
{
Dlinklistp temp = head;
if (temp)
{
    if (temp->num==data)//因为头部删除不能返回head, 所以分类改变他的返回值
    {
        temp->pre->next = temp->next;
        temp->next->pre = temp->pre;
        temp = temp->next;
        free(head);
        return temp;
    }
        while (temp->next != head&&temp->num != data)
            temp = temp->next;
        if (temp->num == data)
        {
            temp->pre->next = temp->next;
            temp->next->pre = temp->pre;
            return head;
        }
}
return nullptr;
}
Dlinklistp replece_node(Dlinklistp head, int data, int new_data)//将已有的data用新的节点替换成new_data
{
Dlinklistp temp = head;

if (temp)//判断temp是否为空
{   
    Dlinklistp new_node = (Dlinklistp)malloc(sizeof(Dlinklist));
    new_node->num = new_data;
    new_node->next = nullptr;
    new_node->pre = nullptr;
    if (temp->num == data)//因为头部替换不能返回head,所以分类改变他的返回值
    {
        temp->pre->next = new_node;
        new_node->pre = temp->pre;
        new_node->next = temp->next;
        temp->next->pre = new_node;
        free(head);
        return new_node;
    }
    while (temp->next != head&&temp->num != data)//遍历查找
        temp = temp->next;
    if (temp->num == data)
    {
        temp->pre->next = new_node;
        new_node->pre = temp->pre;
        new_node->next = temp->next;
        temp->next->pre = new_node;
        free(temp);
        return head;
    }
}
return nullptr;
}
Dlinklistp creat_Dlist()
{
Dlinklistp head = nullptr;
srand(time(nullptr));
for (int i = 1; i <6; i++)
{
    Dlinklistp new_node = (Dlinklistp)malloc(sizeof(Dlinknode));
    new_node->num = rand() % 100;
    new_node->pre = nullptr;//将节点的pre赋值为空
    new_node->next = nullptr;//将节点的next赋值无空
    head = insert_node(head, new_node);
    output(head);
    getchar();
}
return head;
}
int main()
{
Dlinklistp head = creat_Dlist();
int data;
printf("please input a data for cheak");
printf("\n");
scanf_s("%d", &data);
Dlinklistp temp = cheak_node(head, data);
if (temp)
    printf("Yes");
else
    printf("No");
printf("\n");
printf("please input a data for delete");
printf("\n");
scanf_s("%d", &data);
temp = delete_node(head, data);
if (temp)
{
    head = temp;
    output(head);
}
else
    printf("No");
printf("\n");
printf("please input a data for replece");
printf("\n");
scanf_s("%d", &data);
int new_data;
scanf_s("%d", &new_data);
temp = replece_node(head, data, new_data);
if (temp)
{
    head = temp;
    output(head);
}
else
    printf("No");
printf("\n");
return 0;
}

数据结构内存对齐

结构(struct)的数据成员,第一个数据成员放在offset为0的地方,以后每个数据成员的对齐按照#pragma pack指定的数值和这个数据成员自身长度中,比较小的那个进行。例如。

#include<stdio.h>
#include<stdlib.h>
#pragma pack(1) /* n = 1, 2, 4, 8, 16 */
struct test {
int a;
char b;
short c;
char d[6];
};
int main()
{
printf("%d \n", sizeof(int));
printf("%d \n", sizeof(char));
printf("%d \n", sizeof(short));
printf("%d \n", sizeof(test));
return 0;
}

当为pack(1)时,依次排列,test占13个字节,当pack(2)是int a占4个字节,char b占1个字节后因为short b为2个字节大于剩下的 所以此时char b后面空个了1个字节,说最后test占14个字节。

二叉排序树

暂无

hash散列

哈希表(Hash table,也叫散列表),是根据key而直接进行访问的数据结构。也就是说,它通过把key映射到表中一个位置来访问记录,以加快查找的速度。这个映射函数叫做散列函数,存放记录的数组叫做散列表。 哈希表的做法其实很简单,就是把key通过一个固定的算法函数即所谓的哈希函数转换成一个整型数字,然后就将该数字对数组长度进行取余,取余结果就当作数组的下标,将value存储在以该数字为下标的数组空间里。

队列 栈 链表

链表是一种存储结构,指的是存储时候除了要存储数据元素外,还要数据元素一起的另外空间存储数据元素的关系
队列和栈都是线性表,属于逻辑结构范畴,都是访问点受到限制,并且限制在线性表端点的线性表
栈被限定为在线性表的同一个(唯一的一个)端点插入删除(先进后出)。
队列被限定为在线性表的一端插入,另外一个端点删除(先进先出)。

基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。 智能教学辅助系统 这是一个智能教学辅助系统的前端项目,基于 Vue3+TypeScript 开发,使用 Ant Design Vue 作为 UI 组件库。 功能模块 用户模块 登录/注册功能,支持学生和教师角色 毛玻璃效果的登录界面 教师模块 备课与设计:根据课程大纲自动设计教学内容 考核内容生成:自动生成多样化考核题目及参考答案 学情数据分析:自动化检测学生答案,提供数据分析 学生模块 在线学习助手:结合教学内容解答问题 实时练习评测助手:生成随练题目并纠错 管理模块 用户管理:管理员/教师/学生等用户基本管理 课件资源管理:按学科列表管理教师备课资源 大屏概览:使用统计、效率指数、学习效果等 技术栈 Vue3 TypeScript Pinia 状态管理 Ant Design Vue 组件库 Axios 请求库 ByteMD 编辑器 ECharts 图表库 Monaco 编辑器 双主题支持(专业科技风/暗黑风) 开发指南 # 安装依赖 npm install # 启动开发服务器 npm run dev # 构建生产版本 npm run build 简介 本项目旨在开发一个基于开源大模型的教学实训智能体软件,帮助教师生成课前备课设计、课后检测问答,提升效率与效果,提供学生全时在线练习与指导,实现教学相长。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值