文章目录
一,时间复杂度:程序执行的次数
时间复杂度的计算
1,只看最高规模的;
2,省略其系数;
二,常见时间复杂度:
O(1)没有循环或者循环只执行一次
O(n)循环语句与问题规模之间存在关系并且控制循环变量每次以++或者--的方式来执行;
O(n^2)循环嵌套;
O(log2 n)有循环并且控制循环的的变量以*2或者/2的方式出现;
for(int i=0;i<n;i*2)
{
int a=i;
}
三,空间复杂度的定义:所需的额外存储空间(除了函数本身计算申请的内存空间)与问题规模之间的关系;
例如:下图额外空间度为O(1);
集合结构,线性结构(一对一),树形结构(一对多),图形结构或网状结构(多对多)
关于数据项,数据元素,数据对象的概念;
线性表 线性表:由n个数据元素构成的有限序列; 线性表的特点:线性表里的元素是连续的,线性表里的元素是一对一
定长顺序表的缺点:
大开小用,浪费空间;
改变其大小只能通过改变宏定义的大小;
自定义大小时有可能会超过栈帧的范围导致程序本崩溃;
顺序表的扩容:之所以乘2倍而不是4,5是因为如果顺序表的容量过大45就非常麻烦扩容的量过大
如何实现编程:
1,考虑你需要的的功能,结构和所需的资源;
2,根据你的功能和资源在脑海中构造算法逻辑;
3,写程序(根据语种写代码)
4,检查程序的错误;
5,输入值测试你的程序;
如何实现代码:
1,确定函数名称(注意用英文来写要见名知意)
2,确定正确的形参类型(根据需要传输的实参类型来确定);
3,确定函数的返回值及类型
4,编写函数
1,检查形参;
2,定义变量和指针;
3,编写循环体;
4,检查边界;
5,返回值;
存储单元:8个bit为一个存储单元(c语言里为一个字节)
存储单元给编号此编号称为地址
一,定长的顺序表结构
#define SIZE 10;
typedef int Elemtype;
typedef struct Seqlist
{
Elemtype data[SIZE];
int cursize;
}Seqlist;
二,不定长顺序表的结构
#define SIZE 10;
typedef int Elemtype;
typedef struct Seqlist
{
Elemtype *data;//通过malloc来分配空间
int capacity;
int cursize;
}Seqlist;
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include<string.h>
#define SIZE 10
#define MULTIPLE 2
typedef int Elemtype;
typedef struct Seqlist
{
Elemtype *data;
Elemtype capacity;
Elemtype cursize;
}Seqlist;
void Init(Seqlist* plist)
{
plist->capacity = SIZE;
plist->data =(Elemtype*) malloc(sizeof(Elemtype) *plist->capacity);
plist->cursize = 0;
printf("初始化成功\n");
}
int Getsize(Seqlist* plist)
{
assert(plist != nullptr);
return plist->cursize;
}
void Printf(Seqlist*plist)
{
assert(plist!=nullptr);
printf("capacity:%d\ncursize:%d\n",plist->capacity,plist->cursize);
for (int i = 0; i < Getsize(plist); i++)
{
printf("%d ", plist->data[i]);
}
printf("\n");
}
bool Isfull(Seqlist*plist)
{
assert(plist != nullptr);
return plist->capacity == plist->cursize;
}
bool Inc_capacity(Seqlist* plist)
{
assert(plist != nullptr);
int total = plist->capacity * MULTIPLE;
/*Elemtype* newdata = (Elemtype*)malloc(sizeof(Elemtype) * total);
if (newdata == nullptr)
{
return false;
}
memmove(newdata, plist->data, sizeof(Seqlist) * total);
free(plist->data);
plist->data = nullptr;
plist->data = newdata;
plist->capacity = total;
return true;*/
Elemtype* newdata = (Elemtype*)realloc(plist->data,sizeof(Elemtype)*total);
if (nullptr==newdata)
{
return false;
}
plist->data = newdata;
plist->capacity = total;
return true;
}
bool Insert_pos(Seqlist* plist,Elemtype val,int pos)
{
assert(plist!=nullptr);
if (pos<0 || pos>plist->cursize) return false;
if (Isfull(plist) && !Inc_capacity(plist))
{
return false;
}
for (int i=plist->cursize; i>pos; --i)
{
plist->data[i]=plist->data[i-1];
}
plist->data[pos] = val;
plist->cursize += 1;
return true;
}
bool Insert_front(Seqlist*plist,int val)
{
assert(plist != nullptr);
if (Isfull(plist) && !Inc_capacity(plist))
{
return false;
}
Insert_pos(plist,val,0);
}
bool Insert_back(Seqlist*plist,int val)
{
assert(plist != nullptr);
if (Isfull(plist) && !Inc_capacity(plist))
{
return false;
}
plist->data[plist->cursize] = val;
plist->cursize++;
return true;
}
bool Del_pos(Seqlist* plist, int pos)
{
assert(plist != nullptr);
if (pos<0 || pos>plist->cursize - 1)
{
return false;
}
for (int i = pos; i < plist->cursize - 1; i++)
{
plist->data[i] = plist->data[i + 1];
}
plist->cursize -= 1;
return true;
}
bool Del_front(Seqlist*plist)
{
assert(plist != nullptr);
return Del_pos(plist, 0);
}
bool Del_back(Seqlist*plist)
{
assert(plist != nullptr);
return Del_pos(plist,plist->cursize-1);
}
int Find_pos(Seqlist* plist, int val)
{
assert(plist != nullptr);
int pos = -1;
for (int i = 0; i < plist->cursize - 1; ++i)
{
if (plist->data[i] == val)
{
pos=i;
break;
}
}
return pos;
}
bool Del_val(Seqlist* plist, int val)
{
assert(plist != nullptr);
return Del_pos(plist,Find_pos(plist,val));
}
bool Del_allsame(Seqlist* plist, int val)
{
assert(plist != nullptr);
/*int j = -1;
for (int i = 0; i < plist->cursize; i++)
{
if (plist->data[i] != val)
{
j += 1;
plist->data[j] = plist->data[i];
}
}
plist->cursize=j+1;*/
int j = 0;
for (int i = 0; i < plist->cursize; i++)
{
if (plist->data[i] != val)
{
plist->data[j] = plist->data[i];
j += 1;
}
}
plist->cursize = j;
return true;
}
void Clear(Seqlist*plist)
{
assert(plist != nullptr);
plist->cursize = 0;
}
void Destroy(Seqlist* plist)
{
assert(plist != nullptr);
plist->cursize = 0;
plist->capacity = 0;
free(plist->data);
}
int main()
{
Seqlist mylist;
Init(&mylist);
int ar[] = {12,23,34,12,33,26,12};
int n = sizeof(ar) / sizeof(ar[0]);
for (int i = 0; i <n; i++)
{
Insert_back(&mylist,ar[i]);
}
Printf(&mylist);
Del_allsame(&mylist, 12);
Printf(&mylist);
Destroy(&mylist);
return 0;
}
初始化不定长顺序表
void Init(Seqlist*plist)
{
plist->data=(Elemtype*)malloc(sizeof(Elemtype)*plist->capacity)
plist->capacity=SIZE;
plist->cursize=0;
}
#pragma once
#define n 10
typedef struct sqlist
{
int ar[n] = {};
int length;
}sqlist,*SPsqlist;
void Init_sqlist(SPsqlist sqlist);//线性表的初始化
bool Insert_pos(sqlist*q,int pos,int val);//线性表的第几个数前插入数据
int Find_pos(sqlist* q, int val);//根据val查找下标
bool Del_val(sqlist*q,int val);//根据val删除数据
bool Del_pos(sqlist* q, int pos);//根据pos删除数据
sqlist* Search(sqlist *q,int val);
bool Is_empty(sqlist*q);
bool Is_full(sqlist*q);
void Clear(sqlist*q);
void Destory(sqlist*q);
#include<stdio.h>
#include<assert.h>
#include<stdlib.h>
#include"sqlist.h"
void Init_sqlist(sqlist*q)
{
assert(q != nullptr);
q->length = 0;
}
bool Is_empty(sqlist *q)
{
return q->length== 0;
}
bool Is_full(sqlist* q)
{
return q->length == n;
}
bool Insert_pos(sqlist*q,int pos,int val)
{
assert(q != nullptr);
/*if (pos<0 || pos>q->length+1) return false;
if(Is_full(q)!=1)
{
if (pos <= q->length)
{
for (int i = q->length-1; i >= pos - 1; i--)
{
q->ar[i + 1] = q->ar[i];
}
}
}*/
if (pos<0 || pos>q->length)
return false;
for (int i = q->length - 1; i >= pos; i--)
{
q->ar[i + 1] = q->ar[i];
}
q->ar[pos-1] = val;
q->length++;
return true;
}
int Find_pos(sqlist*q,int val)
{
assert(q != nullptr);
for (int i = 0; i < n; i++)
{
if (val == q->ar[i]) return i;
}
return NULL;
}
bool Del_pos(sqlist* q, int pos)
{
assert(q!=nullptr);
if (pos == NULL || pos<0 || pos>n - 1) return false;
else if (pos == q->length- 1)
{
q->ar[pos] == NULL;
q->length--;
return true;
}
else
{
while(pos < q->length - 1)
{
q->ar[pos] = q->ar[pos + 1];
++pos;
}
q->ar[pos] = NULL;
q->length--;
return true;
}
}
bool Del_val(sqlist*q,int val)
{
assert(q!=nullptr);
int pos=Find_pos(q, val);
Del_pos( q, pos);
}
void Clear(sqlist* q)
{
q->length = 0;
}
void Destory()
{
}