数据结构时间复杂度
描述程序运行的时间复杂度BigO即大O表示法,计算的是程序运行的次数而不是程序运行的时间,时间复杂度 = 程序运行的次数
公式: T(n) = O(f(n))
T(n) : 表示算法的渐进时间复杂度
f(n) : 程序运行的次数
O : 表示正比关系
例子1:
for(int i = 1; i <= n; i++)
{
x++;
}
O(1+3n)--->如果n是无限大,1没有意义,3也没有意义,时间复杂度是O(N)
例子2:
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
x++;
}
}
O(N*N)=O(N^2)
例子3:
for(int i = 1; i <= n; i++)
{
x++;
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
x++;
}
}
O(N+N^2)==>n无限大的时候,n为常数,所以时间复杂度为O(N^2),当n为无限大的时候,常数可以忽略
顺序表基本操作
// demo.c
//对顺序表的管理,初始化,插入数据,显示数据,判断所插入数据是否已满,删除数据,销毁顺序表(非常重要)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>typedef int dataType;
// 顺序表管理结构体
typedef struct sequeList
{
int cap; // 顺序表总大小
int last; // 顺序表元素下标
dataType *data; // 指向顺序表的首元素地址
}sequeList;// 初始化顺序表
sequeList *init_seque(int cap)
{
if(cap <= 0)
return NULL;
// 给顺序表管理结构体分配空间
sequeList *head = malloc(sizeof(sequeList));//这个堆空间有一个,这一个字节为sizeof(sequeList)的字节数(4+4+4)
if(head == NULL)
return NULL;
head->cap = cap;
head->last = -1;
head->data = calloc(cap,sizeof(dataType));
if(head->data == NULL)
return NULL;return head;
}// 判断表是否满
bool isFull(sequeList *head)
{
if(head->cap == head->last+1)
return true;
return false;
}// 插入数据
bool insert(sequeList *head, dataType data)
{
if(head == NULL)
return false;
// 判断数据是否已满
if(isFull(head))
return false;head->data[++head->last] = data;
return true;
}// 判断表为空
bool isEmpty(sequeList *head)
{
if(head->last == -1)
return true;
return false;
}//删除指定数据,removeData
sequeList *removeData(sequeList *head,dataType n)
{
for(int i=0;i<=head->last;i++)
{
if(head->data[i]==n)
{
//是从i开始后一个赋值给前一个,切记
for(int j=i;j<=head->last;j++)
{
head->data[j]=head->data[j+1];
}
head->last--;}
}
return head;}
// 显示插入的数据
void showList(sequeList *head)
{
// 判断表为空
if(isEmpty(head))
return;
for(int i = 0; i < head->last+1; i++)
{
printf("%d\t",head->data[i]);
}
printf("\n");
}// 销毁顺序表
void deStory(sequeList *head)
{
free(head->data);
head->data = NULL;
free(head);
head = NULL;
}int main(int argc, char const *argv[])
{
// 初始化表
sequeList *head = init_seque(10);
if(head == NULL)
{
printf("init failed\n");
return -1;
}
for(int i = 0;i < 5; i++)
{
dataType data;
scanf("%d",&data);
// 将输入插入到表
insert(head,data);
}
removeData(head,1);
removeData(head,3);
removeData(head,5);
// 显示
showList(head);// 销毁顺序表
deStory(head);
return 0;
}
链表基本操作
1.实现头插法
2.实现链表的修改节点和删除节点
3.实现链表数据在任意位置添加
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
typedef int dataType;
// 节点
struct node
{
dataType data;// 指针域
struct node *next; // 指向下一个节点
};
// 创建新节点
struct node *create_newNode(dataType data)
{
struct node *pnew = malloc(sizeof(struct node));
if(pnew == NULL)
return NULL;
pnew->data = data;
pnew->next = NULL;
return pnew;
}
// 创建链表
struct node *create_list(void)
{
// 设置空链表
struct node *head = NULL;
struct node *tail = NULL;
while(1)
{
dataType data;
if(scanf("%d",&data) == 0)
break;
// 创建新节点
struct node *pnew = create_newNode(data);
if(pnew == NULL)
{
printf("create new node failed:\n");
//exit(0); // 不管再哪里调用,整个程序结束
return NULL; // 只退出本函数
}
// 从无到有
if(head == NULL)
{
head = pnew;
tail = pnew;
printf("pre:%d\n",head->data);
}
else// 从少到多
{
#if 1
// 尾插法
tail->next = pnew;
tail = pnew;
#else
// 头插法
pnew->next = head;
head = pnew;
#endif
}
}
return head;
}
// 修改节点
struct node *update_node(struct node *head,dataType oldData,dataType newData)
{
if(head == NULL)
return NULL;
// 找位置替换
struct node *p = head;
while(p)
{
if(p->data == oldData)
{
p->data = newData;
}
else
{
p = p->next;
}
}
return head;
}
// 显示链表
void show_list(struct node *head)
{
if(head == NULL)
return;
for(struct node *p = head; p != NULL; p = p->next)
{
printf("%d\t",p->data);
}
printf("\n");
}
/*
* 功能 :删除一个节点,data对应的是需要删除的节点
* 思路 :定义pre指针指向p的前驱节点,p指向需要删除的节点,注意去头去尾需要防止段错误
*/
struct node *del_node(struct node *head, dataType data)//切记删除和插入都要分情况,看删的是哪个部分的节点(头或尾或中间)如果不清楚一定要画图理解。
{
struct node *p = head;
struct node *pre = NULL; // 指向p的前一个节点(在循环中p->next会比pre->next多执行一次)
// 找节点
while (p)
{
// 找到删除的节点
if(p->data == data)
{
break;
}
else
{
pre = p;//别忘了更新pre指针
p = p->next;
}
}
// 找不到
if(p == NULL)
{
printf("没有需要删除的节点\n");
return head;
}
// 删除的是首节点
if(p->data == head->data)
{
head = p->next;
p->next = NULL;
free(p);//删除节点别忘了要释放所删除的节点
}
//p是尾节点
else if(p->next == NULL)
{
pre->next = NULL;
free(p);
}
// p是中间节点
else
{
pre->next = p->next;
p->next = NULL;
free(p);
}
return head;
}
/*
* 任意位置data前面添加新节点newData
* 思路 :1.创建新节点,2.定位p位置
*/
struct node *add_node(struct node *head, dataType newData, dataType data)//切记删除和插入都要分情况,看删的是哪个部分的节点(头或尾或中间),如果不清楚一定要画图理解
{
// 创建新节点
struct node *pnew = create_newNode(newData);
if(pnew == NULL)
return NULL;
struct node *p = head;//p是用来找插入位置的
struct node *pre = NULL;
// 找位置
while (p)
{
if(p->data == data)
{
break;
}
else
{
pre = p;//别忘了更新pre指针
p = p->next;
}
}
if(p == NULL)
{
// 尾插
pre->next = pnew;
}
else if(head->data == p->data)//如果想放在第一个
{
// 头插
pnew->next = head;
head = pnew;
}
else//其它情况
{
// 中间
pnew->next = p;
pre->next = pnew;
}
return head;
}
int main(int argc, char const *argv[])
{
// 创建链表
struct node *head = create_list();
if(head == NULL)
return -1;
//这里打注释是因为这里最终输出都是在show里的(会依次覆盖上一次的结果,如果想得到各个部分的输出要么注释掉调用函数语句,要么在各个子函数中都加上printf),并非在各个子函数中输出
// 更新节点
//head = update_node(head,3,8);
// 删除节点
//head = del_node(head,3);
// 任意位置插入数数据
head = add_node(head,8,9);
// 显示链表
show_list(head);
return 0;
}
链表实现冒泡排序
// LinkedHeadList.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>typedef int dataType;
// 数据节点
struct node
{
dataType data;
struct node *next;
};// 头节点,管理结构体
struct headNode
{
struct node *first; // 指向首节点
struct node *tail; // 指向尾节点
int nodeNumber; // 链表节点个数
};struct headNode *init_headNode(void)
{
struct headNode *head = malloc(sizeof(struct headNode));
if(head == NULL)
return NULL;
head->first = NULL;
head->tail = NULL;
head->nodeNumber = 0;return head;
}// 创建新节点
struct node *create_newNode(dataType data)
{
struct node *pnew = malloc(sizeof(struct node));
if(pnew == NULL)
return NULL;
pnew->data = data;
pnew->next = NULL;
return pnew;
}// 判断链表是否为空
bool isEmpty(struct headNode *head)
{
if(head->nodeNumber == 0)
return true;
return false;
}// 头插法
void addHead(struct headNode *head, struct node *pnew)
{
pnew->next = head->first;//一定是新的节点去主动去连接头节点(反则会造成数据丢失)
head->first = pnew;//更新头节点
}// 尾插
void addTail(struct headNode *head, struct node *pnew)
{
head->tail->next = pnew;//尾巴主动连接新节点,把尾巴指针指过去
head->tail = pnew;//更新尾巴指针
}// 创建带头节点的单向链表
struct headNode* create_list(void)
{
// 初始化管理结构体
struct headNode *head = init_headNode();
if(head == NULL)
{
printf("init head node failed\n");
return NULL;
}while(1)//输入循环时不能用for,不然无法正常输出
{
dataType data;
if(scanf("%d",&data) == 0)// 退出
{
// 将缓冲区里面的字符取走(重要)
while(getchar() != '\n');
break;
}// 创建新节点
struct node *pnew = create_newNode(data);
// 从无到有
if(head->first == NULL)
{
head->first = pnew;
head->tail = pnew;
}
else // 从少到多
{
// 头插法
//addHead(head,pnew);// 尾插法
addTail(head,pnew);
}
// 新增节点
head->nodeNumber++;
}
return head;
}// 更新节点
struct headNode *update_node(struct headNode *head, dataType newData, dataType data)
{
// 找节点
struct node *p = head->first;
while (p)
{
if(p->data == data)
{
p->data = newData;
}
else // 继续找
{
p = p->next;
}
}
if(p == NULL)
{
printf("没有需要替换的数据\n");
}
return head;
}// 添加新节点
struct headNode *add_node(struct headNode *head, dataType newData, dataType data)
{
// 创建新节点
struct node *pnew = create_newNode(newData);
if(pnew == NULL)
return NULL;// 找位置
struct node *p = head->first; // 遍历指针
struct node *pre = NULL;// p的前驱指针while (p)//找位置,只能用while,不要用for
{
if(p->data == data)
{
break;
}
else
{
pre = p;//千万别忘了更新pre
p = p->next;
}
}
// 找不到,尾巴插入,就是尾插法
if(p == NULL)
{
addTail(head,pnew);
}
else if(head->first->data == p->data) // 头插
{
addHead(head,pnew);
}
else //中间插入(即在p和pre之间插入一个),不清楚就画图看
{
pnew->next = p;
pre->next = pnew;
}
// 更新节点数
head->nodeNumber++;return head;
}// 删除节点
struct headNode *del_node(struct headNode *head, dataType data)
{
// 找节点
struct node *p = head->first;
struct node *pre = NULL;while(p)//删除不能使用for(会错),他和while不一样
{
if(p->data == data)//一定是双=才是等于号千万不能只写一个=
{
break;
}
else
{
pre = p;
p = p->next;
}
}
if(p == NULL)
{
printf("找不到剔除的节点\n");
}
else if(p == head->first)// 首节点
{
head->first = p->next;
p->next = NULL;
free(p);
}
else if(p == head->tail) // 末尾
{
pre->next = NULL;
head->tail = pre;
free(p);
}
else
{
pre->next = p->next;
p->next = NULL;
free(p);
}
// 节点数减一
head->nodeNumber--;return head;
}// 冒泡排序
struct headNode* bubble_sort_list(struct headNode *head)
{
for(int i = 0; i < head->nodeNumber-1; i++)
{
struct node *p = head->first;//因为没有数组下标了,所以每次将p指向首节点,以从头开始比较
for(int j = 0; j < head->nodeNumber-1-i; j++)
{
if(p->data > p->next->data)//p和p的下一个(指针所指)中的数据进行比较
{
int temp = p->data;
p->data = p->next->data;
p->next->data = temp;
}
p = p->next;//别忘了每次要更新p
}
}
return head;
}// 显示
void show_list(struct headNode *head)
{
if(isEmpty(head))
{
printf("链表为空\n");
return ;
}
for(struct node *p = head->first; p != NULL; p = p->next)
printf("%d\t",p->data);
printf("\n");
printf("链表已有%d个节点\n",head->nodeNumber);
}
// 销毁链表
void destroy_list(struct headNode *head)
{
if(isEmpty(head))
{
return;
}
struct node *tmp = NULL;
//一定要注意再把temp赋值给p(不然null),及时更新p
for(struct node *p = head->first; p != NULL; p = tmp)
{
//销毁链表时一定一定要用一个临时指针存放p->next,然后再把p->next逐个赋值null再释放p,最后number--。如果不定义个临时指针的话(会段错误):第一个p->next赋值null后(释放),会失去第一个节点与后面一串的联系
tmp = p->next;
p->next = NULL;
free(p);
head->nodeNumber--;
}
}int main(int argc, char const *argv[])
{
// 创建链表
struct headNode *head = create_list();
if(head == NULL)
return -1;// 更新节点
//head = update_node(head, 8, 3);
// 添加节点
//head = add_node(head,8,3);
// 删除节点
//head = del_node(head, 3);
//排序
head = bubble_sort_list(head);
// 显示
show_list(head);
// 销毁链表,头节点没有被销毁
destroy_list(head);
//show_list(head);
//free(head);
return 0;
}
双向链表操作
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>typedef int dataType;
// 节点
struct node
{
dataType data; // 数据
struct node *pre; // 指向上一个节点
struct node *next; // 指向下一个节点
};// 头节点,管理链表
struct headNode
{
struct node *first; // 指向链表的首节点
struct node *tail; // 指向链表的尾节点
int nodeNumber; // 链表节点数
};// 初始化头节点
struct headNode *init_headNode()
{
struct headNode *head = malloc(sizeof(struct headNode));
if(head == NULL)
return NULL;head->first = NULL;
head->tail = NULL;
head->nodeNumber = 0;return head;
}// 创建节点
struct node *create_node(dataType data)
{
struct node *pnew = malloc(sizeof(struct node));
if(pnew == NULL)
return NULL;pnew->data = data;
pnew->next = NULL;
pnew->pre = NULL;return pnew;
}//双向的头插和尾插(理解重要)
void addTail(struct headNode *head,struct node *pnew)
{ //头插入和尾插入操作
//如果不清楚就画图辅助理解,顺序不能错
//等号左边的东西(出发点)完完全全要写出来
//等号右边是一个指针(节点全部的本身)
head->tail->next = pnew;//双向箭头中的上面一个
pnew->pre = head->tail;//双向箭头的下面一个
head->tail = pnew;//更新tail指针}
void addHead(struct headNode *head,struct node *pnew)
{ //等号左边的东西(出发点)完完全全要写出来,顺序不能错,不然数据丢失
//等号右边是一个指针(节点全部的本身)
pnew->next = head->first;//双向箭头中的下面一个
head->first->pre = pnew;//双向箭头的上面一个
pnew->pre = head->first->pre;//头插多一句话,
//把新指针的pre指针也指向first的pre
head->first = pnew;//更新first指针}
void show_list(struct headNode *head)
{
if(head->nodeNumber == 0)
{
printf("链表为空\n");}
for (struct node *p = head->first;p != NULL;p = p->next)
{
printf("%d\t",p->data);
}
printf("\n");
}
// 创建双向链表
struct headNode *create_list()
{
// 初始化头节点
struct headNode *head = init_headNode();
if(head == NULL)
return NULL;while (1)
{
dataType data;
if(scanf("%d",&data) == 0)
break;// 创建新节点
struct node *pnew = create_node(data);
if(pnew == NULL)
return NULL;if(head->first == NULL) // 从无到有
{
head->first = pnew;
head->tail = pnew;
}
else // 从少到多
{
// 尾插
addTail(head,pnew);
// 头插
//addHead(head,pnew);
}}
head->nodeNumber++;
return head;
}
//修改
struct headNode *upnode(struct headNode *head,dataType olddata,dataType newdata)
{
if(head == NULL)
{
return NULL;
}
for(struct node *p = head->first;p != NULL;p = p->next)
{
if(p->data == olddata)
{
p->data = newdata;
}
}
return head;
}
//增加
struct headNode *addnode(struct headNode *head,dataType data,dataType newdata)
{
struct node *pnew = create_node(newdata);
struct node *p = head->first;
if(pnew == NULL)
return NULL;while(p)
{
if(data == p->data)
{
break;
}
else
{
p = p->next;
}
}if(p == head->first)//记住不能比较值,因为可能会出现null,短板错误,所以要用指针比较
{
addHead(head,pnew);
}
else if(p == NULL)
{
addTail(head,pnew);
}
else
{ //增加中间的节点操作:等号右边是指向节点(整个本身),等号右边是完完全全的出发点
//3和4句顺序不能错,画图理解
pnew->pre = p->pre;
pnew->next = p;
p->pre->next = pnew;
p->pre = pnew;
}
head->nodeNumber++;
return head;}
void denode(struct headNode *head,dataType data)
{
struct node *p = head->first;
while(p)
{
if(p->data == data)
{
break;
}
else
{
p = p->next;
}
}//删除的时候是头尾指针先动(放等式左边),插入的时候是右边
if(p == head->first)
{
head->first = p->next;//上面的箭头
p->next->pre = head->first;//下面的箭头
p->pre = NULL;
p->next = NULL;
free(p);
}
else if(p == head->tail)
{
head->tail = p->pre;
p->pre->next = NULL;//尾节点的next都是null
p->pre=NULL;
p->next=NULL;
free(p);}
else if(p == NULL)//删除节点多考虑一个null的情况
{
printf("null\n");
}
else
{
p->pre->next = p->next;
p->next->pre = p->pre;
p->pre=NULL;
p->next=NULL;
free(p);
}}
bool isnull(struct headNode *head)
{
if(head->nodeNumber == 0)
{
return true;
}
}
void desnode(struct headNode *head)
{
if(isnull(head))//销毁是否成功是要看这个的
{
printf("成功销毁");
return ;}
struct node *temp =NULL;
for(struct node *p=head->first;p != NULL;p = temp)
{
temp = p->next;
p->next=NULL;
free(p);
head->nodeNumber--;
}
}
int main(int argc, char const *argv[])
{
// 创建链表
struct headNode *head = create_list();
if(head == NULL)
{
printf("create list failed:");
return -1;
}
//修改
//upnode(head,3,33);//添加
//addnode(head,3,66);//删除
//denode(head,3);
// 显示链表信息
show_list(head);
//销毁
desnode(head);
show_list(head);
return 0;
}
使用链式栈,实现十进制转八进制:键盘输入一个十进制数,经过链式栈的相关算法,输出八进制数。
// 入栈
bool push(linkStack *ls, dataType data)
{
// 创建节点
node *pnew = create_node(data);
if(pnew == NULL)
return false;// 头插
pnew->next = ls->top;
ls->top = pnew;
ls->size++;return true;
}
// 获取栈顶节点元素
node *top(linkStack *ls)
{
if(is_empty(ls))
return NULL;
return ls->top;
}// 弹栈
node *pop(linkStack *ls)
{
node *p = top(ls);
if(p == NULL)
return NULL;
ls->top = p->next;
p->next = NULL;
ls->size--;
return p;
}int main(int argc, char const *argv[])
{int data;
scanf("%d",&data);
while (data > 0)
{
push(ls,data%8);
data /= 8;
}
printf("0"); // 八进制标记while (1)
{
if(is_empty(ls))
{
break;
}
node *p = pop(ls);
printf("%d",p->data);
}
printf("\n");
return 0;
}