代码模块,检索,排序,插入,删除,修改,函数实现.
算法与程序
算法不依赖计算机语言,通用
程序依赖计算机语言.
/*===============================================
* 文件名称:2.c
* 创 建 者:
* 创建日期:2022年06月15日
* 描 述:
================================================*/
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#define N 6
int* value_seqlist();
int find_seqlist();
typedef int data_t;
//申明结构体
typedef struct list {
data_t data[N];
int last;
}seqlist;
//创建顺序表
seqlist *create_seqlist()
{
seqlist *head=(seqlist*) malloc(sizeof(seqlist));//分配空间
if(NULL==head)
return NULL;
memset(head->data,0,sizeof(head->data));//清空表
head->last=-1;//清空后下标分配为数组起始位置前一位
return head;
}
//判空
int is_empty(seqlist *head)
{
if(NULL==head)
return -1;
if(1==head->last)
return 1;
else
return 0;
}
//判满
int is_full(seqlist *head)
{
if(NULL==head)
return -1;
return (N==head->last+1?1:0);//通过下标判断实际元素与表长度
}
//求元素个数(表的长度)
int lenth_seqlist(seqlist *head)
{
int len;
if(NULL==head)
return -1;
len=head->last+1;
return len;
}
//添加元素
int add_seqlist(seqlist *head,int place, data_t value)
{
if(NULL==head)
return -1;
if(1==is_full(head))
return -2;
//判断插入位置palce
int len =lenth_seqlist(head);
if(place<0||place>len)
return -3;
for(int i=len-1;i>place;i--)
head->data[i+1]=head->data[i];
head->data[place]=value;
head->last++;
return 0;
}
//打印data;
void show_list(seqlist *head)
{
int i;
if(NULL==head) return;
if(1==is_empty(head)) return;
for(i=0;i<head->last+1;i++)
printf("%d ",head->data[i]);
puts("");
}
//删除函数,输入下标删除
int del_seqlist(seqlist* head,int delet_index)
{
if(NULL==head)
return -1;
if(1==is_empty(head))
return -2;
int len=lenth_seqlist(head);
if(delet_index<0||delet_index>len)
return -3;
for(int i=delet_index;i<head->last;i++)
head->data[i]=head->data[i+1];
head->last--;
return 0;
}
//按值删除
int del_value_seqlist(seqlist *head, int value)
{
if(NULL==head) return -1;
if(1==is_empty(head)) return -2;
int*p=value_seqlist(head,value);
int i;
for(i=0;i<head->last+1;i++)
del_seqlist(head,*(p+i));
return 0;
}
//按位置查询
int find_seqlist(seqlist *head,int place)
{
if(NULL==head)
return -1;
if(1==is_empty(head))
return -2;
if(place<0||place>head->last+1)
return -3;
printf("%d\n",head->data[place]);
return 0;
}
//按值查询
int* value_seqlist(seqlist *head,int value)
{
if(NULL==head)
return NULL;
if(1==is_empty(head))
return NULL;
int i,a[10]={0},j=0;
int *p=a;
for(i=0;i<head->last+1;i++)
{
if(value==head->data[i])
{
a[j]=i;
j++;
}
}
return p;
}
//修改表
int change_seqlist(seqlist *head,int place ,data_t val)
{
if(NULL==head) return -1;
if(1==is_empty(head)) return -2;
if(place<0||place>head->last+1)
return -3;
head->data[place]=val;
}
//清空表
int clear_seqlist(seqlist* head)
{
if(NULL==head) return -1;
head->last=-1;
return 0;
}
//销毁表
void destroy_seqlist(seqlist **seq)
{
free(*seq);
*seq=NULL;
}
//冒泡排序
int qiuk_seqlist(seqlist *head)
{
if(NULL==head) return -1;
if(1==is_empty(head)) return -2;
int i,j,t;
int len=lenth_seqlist(head);
for(i=0;i<len-1;i++)
{
for(j=0;j<len-1-i;j++)
{
if((head->data[j])>(head->data[j+1]))
{
t=head->data[j];
head->data[j]=head->data[j+1];
head->data[j+1]=t;
}
}
}
return 0;
}
int main(int argc, char *argv[])
{
seqlist *head=create_seqlist();
int i,j;
//for(i=0,j=5;i<5,j<0;i++,j--)
add_seqlist(head,0,101);
add_seqlist(head,1,99);
add_seqlist(head,2,11);
add_seqlist(head,3,1);
add_seqlist(head,4,6);
add_seqlist(head,5,9);
show_list(head);
change_seqlist(head,0,10);
show_list(head);
del_seqlist(head,5);
show_list(head);
del_value_seqlist(head,10);
show_list(head);
printf("排序结果为\n");
qiuk_seqlist(head);
show_list(head);
clear_seqlist(head);
show_list(head);
destroy_seqlist( &head);
show_list(head);
return 0;
}
-
数据:
-
数据元素
-
数据项 :数据的最小组成单位
-
ds = (D*R)
-
逻辑结构:集合、 线性表、树 、图
-
存储结构:顺序存储、链式存储、索引存储、散列存储
-
算法:
-
算法的特性:有穷、确定、输入(0-n),输出(1-n)、可行、
-
算法的好坏:时间花费越少越好、空间消耗越少越好、易阅读、易移植、方便维护、调试、 时间复杂度
-
验证算法:事前分析法、事后测试法、
-
时间复杂度计算:1.根据算法写出表达式、 2.常数部分变为1 3、保留最高阶、其他项舍去 4、如果最高阶项不为 1 ,把他化为1.
-
程序:
-
线性表:
特点:表头无前驱、表尾无后继、中间有且只有一个前驱和后继
-
数据结构的基本运算:增删改查
今日内容:
-
顺序表:
-
特点:顺序表和数组类似;
-
存储方式顺序存储:申请连续的一片空间
-
空间大小:设定后无法修改大小
-
存储的数据:相同数据类型
-
查询:直接通过下标查询,很方便、快速
-
增加:插入位置之后的数据会大批量后移。
优点:查询和修改快速方便
缺点:
1. 需要一片 连续的空间
-
增加和删除时涉及大量数据移动,耗时等
-
当顺序表大小确定后 不能再次修改
-
-
顺序单链表;
链表的出现就是为了解决 顺序单项链表的缺点;
(1)储存时需要申请一片连续的空间,容易造成空间浪费;
(2)插入时候不方便麻烦;
单链表也有自己的缺点;
显著的缺点查找数据麻烦,需要函数取出数据;
上图是三种插入的图解;
小结一下;
代码的编写所先要列出算法步骤;
数的构建尤为特殊,怎么说了,要是用递归构建,区别区与队列栈。
算法的实现为代码,就很奇怪,但是例如head->left=head->next->left;
等式左值一般为地址,右值一般模块代表值;
已知 先序 中序 求 后序:
1.都是先找到根,
2.然后通过中序判断左右子树,
3.再从先序货后序确定左右子树的跟;递归可得解;
最优二叉树的构造:
1.先对权值进行排序;
2.取其中两个最小权值,构造新的二叉树,求权值的和参与下次的排序,参加的权值,不进去下次排序。
3递归,直到所有权值结束。
2022.6.24,知不可乎骤得,托遗响与悲风。
今天我们学习了数据结构的最后一部分,
1.图的遍历,红黑树,平衡二叉树的了解;
2.hash查找算法,快速排序,
3.自己对标志位有了新的理解,不积硅步,无以至千里。