数据结构代码
顺序表
静态存储SqList
#include<stdio.h>
#include<stdlib.h>
#define InitSize 5
#define MaxSize 10
typedef struct{
int data[MaxSize];
int length;
}SqList;
void InitList(SqList &L){
L.length=0;
}
bool Insert(SqList &L,int i,int e){
//the range of i
if(i<1||i>L.length+1)
return false;
//beyond the max,cause waste
if(L.length>MaxSize){
return false;
}
//back move
for(int j=L.length;j>=i;j--){
L.data[j]=L.data[j-1];
}
L.data[i-1]=e;
L.length++;
return true;
}
bool Delete(SqList &L,int i,int &e){
//the range of i
if(i<1||i>L.length){
return false;
}
//give e the value
e=L.data[i-1];
//cover the data
for(int j=i;j<L.length;j++){
L.data[j-1]=L.data[j];
}
return true;
}
int GetElemByA(SqList L,int i){
if(i<1||i>L.length) return 0;
return L.data[i-1];
}
int GetElemByD(SqList L,int e){
for(int i=0;i<L.length;i++){
if(L.data[i]==e) return i+1;
}
return 0;
}
int main(){
SqList L;
InitList(L);
int e;
Insert(L,3,3);
Delete(L,3,e);
return 0;
}
动态存储SqList
#include<stdio.h>
#include<stdlib.h>
#define InitSize 5
#define MaxSize 10
typedef struct{
int *data;
int length;
int maxsize;
}SqList;
void InitList(SqList &L){
L.data=(int *)malloc(InitSize*sizeof(int));
L.length=0;
L.maxsize=InitSize;
}
void IncreaseSize(SqList &L,int len){
int *p=L.data;
L.data=(int *)malloc(len*sizeof(int));
for(int i=0;i<L.length;i++){
L.data[i]=p[i];
}
L.maxsize+=len;
}
int main(){
SqList L;
InitList(L);
IncreaseSize(L,5);
return 0;
}
单链表LinkList
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
int data;
//int *next; zhelidezhizheng shijiegoutizhizheng
struct LNode* next;
}LNode,*LinkList;
//No head node
bool InitList(LinkList &L){
L=NULL;
return true;
}
//Has a head node (In practice, I usually write this
bool InitListHead(LinkList &L){
L=(LNode *)malloc(sizeof(LNode));
if(L==NULL) return false;
L->next=NULL;
return true;
}
bool ListInsert(LinkList &L,int i,int e){
if(i<1) return false;
LNode *p=L;
int j=0;
for(;p!=NULL&&j<i-1;j++){
p=p->next;
}
if(p==NULL) return true;//dang i beyond length
LNode *s=(LNode *)malloc(sizeof(LNode));
s.data=e;
s.next=p->next;
p->next=s;
return true;
}
int main(){
LinkList L;
InitListHead(L);
return 0;
}
双链表DLinkList
#include<stdio.h>
#include<stdlib.h>
typedef struct DNode{
int data;
DNode *prior,*next;
}DNode, *DLinkList;
//初始化双链表
bool InitDLinkList(DLinkList &L){
L=(DNode *)malloc(sizeof(DNode));
if(L==NULL) return false;
L->prior=NULL;
L->next=NULL;
return true;
}
//判断为空
bool Empty(DLinkList &L){
if(L->next==NULL) return true;
else return false;
}
//双链表的插入 ,在p节点后插入s节点
bool InsertNextDNode(DNode *p,DNode *s){
if(s==NULL||p==NULL){
return false;
}
s->next=p->next;
/********************这一步很关键*/
if(p->next!=NULL){
p->next->prior=s;
}
/************************* */
p->next=s;
s->prior=p;
return true;
}
//双链表的删除,删除p节点的后继节点
bool DeleteNextDNode(DNode *p){
if(p==NULL) return false;
DNode *q=p->next;
if(q==NULL) return false;
p->next=q->next;
if(q->next!=NULL){
q->next->prior=p ;
}
free(q);
return true;
}
//双链表的删除
void Destory(DLinkList &L){
while(L->next!=NULL){
DeleteNextDNode(L->next);
}
free(L);
L=NULL;
}
int main(){
DLinkList L;
InitDLinkList(L);
return 0;
}
循环单链表LinkList
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
int data;
LNode *next;
}LNode,*LinkList;
//初始化循环单链表
bool InitLinkList(LinkList &L){
L=(LNode*)malloc(sizeof(LNode));
if(L==NULL) return false;
L->next=L;
return true;
}
//空表是指头节点后面没有其他节点的线性表
bool Empty(LinkList L){
if(L->next==NULL) return true;
else return false;
}
int main(){
DLinkList L;
InitDLinkList(L);
return 0;
}
循环双链表DLinkList
#include<stdio.h>
#include<stdlib.h>
typedef struct DNode{
int data;
DNode *prior,*next;
}DNode,*DLinkList;
//初始化循环单链表
bool InitDLinkList(DLinkList &L){
L=(DNode*)malloc(sizeof(DNode));
if(L==NULL) return false;
L->next=L;
L->prior=L;
return true;
}
//判断双链表是否为空
bool Empty(DLinkList L){
if(L->next==NULL) return true;
else return false;
}
//判断p是否为循环双链表的表尾节点
bool IsTail(DLinkList L,DNode *p){
if(p->next==L) return true;
else false;
}
//双链表的插入。在p节点后插入s节点
bool InsertNextDNode(DNode *p,DNode *s){
s->next=p->next;
p->next->prior=s;
p->next=s;
s->prior=p;
}
//双链表的删除(删除p节点的后继节点
bool DeleteDNode(DNode *p){
if(p==NULL) return false;
DNode *q=p->next;
p->next=q->next;
q->next->prior=p;
free(q);
}
int main(){
DLinkList L;
InitDLinkList(L);
return 0;
}
顺序栈SqStack
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
#define ElemType int
typedef struct{
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack &S){
S.top=-1;
}
//top 指针永远指向当前数据的位置
bool Push(SqStack &S,ElemType x){
//判断栈满
if(S.top==MaxSize-1){
return false;
}
S.top=S.top+1;
S.data[S.top]=x;
//S.data[++S.top]=x;
return true;
}
bool Pop(SqStack &S,ElemType &x){
if(S.top==-1){
return false;
}
x=S.data[S.top--];
return true;
}
top指针指向下一个可以插入的位置
//bool Push1(SqStack &S,ElemType x){
// //判断栈满
// if(S.top==MaxSize)return false;
// S.data[S.top++]=x;
// return true;
//}
//bool Pop1(SqStack &S,ElemType &x){
// if(S.top==0)return false;
// x=S.data[--S.top];
//}
int main(){
SqStack S;
}
链栈LiStack(这里王道用了不带头节点的单链表,确实好)
#include<stdio.h>
#include<stdlib.h>
#define ElemType int
typedef struct LinkNode{
ElemType data;
struct LinkNode *next;
} *LiStack;
//不带头节点
bool InitLiStack(LiStack &S){
S=NULL;
return true;
}
bool Push(LiStack &S,ElemType x){
LinkNode *p;
p=(LinkNode *)malloc(sizeof(LinkNode));
if(p==NULL) return false;
p->data=x;
p->next=S->next;
S->next=p;
return true;
}
bool Pop(LiStack &S,ElemType x){
if(S==NULL){
return false;
}
LinkNode *q=S;
S=q->next;
free(q);
return true;
}
bool GetTop(LiStack S,ElemType &x){
if(S==NULL) return false;
x=S->data;
return true;
}
int main(){
return 0;
}
顺序队列SqQueue
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
#define ElemType int
typedef struct {
ElemType data[MaxSize];
int front,rear;//rear 指向下一个可以存放数据的单元
}SqQueue;
void InitQueue(SqQueue &Q){
Q.front=Q.rear=0;
}
bool QueueEmpty(SqQueue &Q){
if(Q.rear==Q.front) return true;
else return false;
}
//入队操作
bool EnQueue(SqQueue &Q,ElemType x){
//判断队满
if((Q.rear+1)%MaxSize==Q.front) return false; //入队操作牺牲了一个存储单元,换取队满的判断条件,否则会与队空条件重复
/*
解决一个浪费的方法有
1、队列里再加一个size变量 ,记录当前队列长度
2、队列里再加一个tag变量记录上一步删除(0)/插入(1)
*/
Q.data[Q.rear]=x;
Q.rear=(Q.rear+1)%MaxSize;//如果只是Q.rear++ ,那么这个队列无法实现循环使用
return true;
}
//出队操作
bool DeQueue(SqQueue &Q,ElemType &x){
if(QueueEmpty(Q)) return false;
x=Q.data[Q.front];
Q.rear=(Q.rear-1)%MaxSize;
return true;
}
//查
bool GetHead(SqQueue Q,ElemType &x){
if(Q.front==Q.rear) return false;
x=Q.data[Q.front];
return true;
}
int main(){
SqQueue Q;
InitQueue(Q);
return 0;
}
链式队列LinkQueue
#include<stdio.h>
#include<stdlib.h>
#define ElemType int
/*
队头删除元素,队尾添加元素
入队注意第一个,出队注意最后一个
数据结构是一种工具
*/
//当结构体内部引用了本身时,必须要给结构体起名字
typedef struct LinkNode{
ElemType data;
struct LinkNode *next;
}LinkNode;
typedef struct{
LinkNode *front;
LinkNode *rear;
}LinkQueue;
//初始化带头节点(推荐)
bool InitQueue(LinkQueue &Q){
Q.front=(LinkNode*)malloc(sizeof(LinkNode));
Q.rear=Q.front;
Q.front->next=NULL;
return true;
}
//初始化不带头结点
bool InitQueue1(LinkQueue &Q){
Q.front=Q.rear=NULL;
return true;
}
bool QueueEmpty(LinkQueue Q){
if(Q.front==Q.rear) return true;
else return false;
}
//新元素入队,带头节点
bool EnQueue(LinkQueue &Q,ElemType x){
LinkNode *s=(LinkNode* )malloc(sizeof(LinkNode));
s->data=x;
s->next=NULL;
Q.rear->next=s;
Q.rear=s;
return true;
}
//新元素入队,不带头节点
bool EnQueue1(LinkQueue &Q,ElemType x){
LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=x;
s->next=NULL;
if(Q.front==Q.rear){
Q.front=Q.rear=s;
}
else{
Q.rear->next=s;
Q.rear=s;
}
}
//新元素出队(带头节点)
bool DeQueue(LinkQueue &Q,ElemType &x){
if(Q.front==Q.rear) return false;
else{
LinkNode *q=Q.front->next;
x=q->data;
Q.front->next=q->next;
//如果是最后一个元素出队,需要修改尾指针
if(Q.rear==q){
Q.rear=Q.front
}
free(q);
return true;
}
}
//新元素出队(不带头节点)
bool DeQueue1(LinkQueue &Q,ElemType &x){
if(Q.front==NULL) return false;
else{
LinkNode *q=Q.front;
x=q.data;
Q.front=q->next;
if(Q.rear==q){ //这个判断条件很重要,q就是队尾
Q.front=NULL;
Q.rear=NULL;
}
free(q);
return true;
}
}
int main(){
return 0;
}
应用
(1)括号匹配
/*
括号匹配
这里栈的操作都采用了简写
*/
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
#define ElemType char
typedef struct{
ElemType data[MaxSize];
int top;
}SqStack;
void InitStack(SqStack &S){
S.top=0;
}
void Push(SqStack &S,ElemType x){
S.data[S.top++]=x;
}
void Pop(SqStack &S,ElemType &x){
x=S.data[--S.top];
}
bool StackEmpty(SqStack S){
if(S.top==0) return true;
else return false;
}
bool testBracketCheck(){
SqStack S;
InitStack(S);
char a[50];
gets(a);
int i=0;
while(a[i]!='\0'){
ElemType topdata;
if(a[i]=='{'||a[i]=='['||a[i]=='(') Push(S,a[i]);
else{
if(a[i]=='}') {
Pop(S,topdata);
if(topdata!='{') return false;
}
else if(a[i]==']') {
Pop(S,topdata);
if(topdata!='[') return false;
}
else if(a[i]==')') {
Pop(S,topdata);
if(topdata!='(') return false;
}
}
i++;
}
return StackEmpty(S);
}
int main(){
if(testBracketCheck()) printf("Success!\n");
else printf("Error!\n");
return 0;
}
(2)中缀表达式
(3)递归
/*
Recursive factorial
Fibonacci sequence
*/
#include<stdio.h>
int Factorial(int n){
if(n==0||n==1) return 1;
else return n*Factorial(n-1);
}
int Fib(int n){
if(n==0) return 0;
else if(n==1) return 1;
else return Fib(n-1)+Fib(n-2);
}
int main(){
printf("Factorial ->%d\n",Factorial(5));
printf("Fib ->%d\n",Fib(5));
return 0;
}
串
顺序存储
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
//静态数组实现
typedef struct{
char ch[MaxSize];
int length;
}SString;
//动态数组实现
typedef struct{
char* ch;
int length;
}HString;
//求子串(返回从pos开始的len个元素的子串
bool SubString(SString &Sub,SString S,int pos,int len){
if(pos+len-1>S.length)return false;
for(int i=pos;i<pos+len;i++){
Sub.ch[i-pos+1]=S.ch[i];
}
Sub.length=len;
return true;
}
//比较两个字串
int StrCompare(SString S,SString T){
for(int i=1;i<S.length&&i<T.length;i++){
if(S.ch[i]!=T.ch[i])
return S.ch[i]-T.ch[i];
}
return S.length-T.length;
}
//定位操作,主串是S
int Index(SString S,SString T){
int i=1,m=S.length,n=T.length;
SString sub;
while(i<=m-n+1){
SubString(sub,S,i,n);
if(StrCompare(sub,T)!=0) ++i;
else return i;
}
return 0;
}
void testSString(){
HString S;
S.ch=(char *)malloc(sizeof(char)*MaxSize);
S.length=0;
}
int main(){
testSString();
return 0;
}
链式存储
#include<stdio.h>
#include<stdlib.h>
typedef struct StringNode{
char ch[4];
struct StringNode* next;
}StringNode,*String;
模式匹配
朴素模式匹配
//朴素模式匹配
int IndexPlus(SString S,SString T){
int i=1,j=1;
while(i<=S.length&&j<=T.length){
if(S.ch[i]==T.ch[i]){
++i;
++j;
}
else {
i=i-j+2;
j=1;
}
}
if(j>T.length) return i-T.length;
else return 0;
}
KMP算法
//KMP算法
int Index_KMP(SString S,SString T,int next[]) {
int i=1,j=1;
while(i<=S.length&&j<=T.length){
//匹配字串
if(S.ch[i]==T.ch[j]||j==0){
//匹配成功,继续比较后继字符
++i;
++j;
}
//模式串向右移动
else j=next[j];//匹配失败,主串指针不回溯
}
if(j>T.length) return i-T.length;
else return 0;
}
树
顺序存储
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
struct TreeNode{
ElemType value;//节点中的数据元素
bool IsEmpty;//节点是否为空
};
int main(){
TreeNode t[MaxSize];
for(int i=0;i<MaxSize;i++){
t[i].IsEmpty=true;
}
return 0;
}
链式存储
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
#define ElemType BiTNode
//二叉树结点
struct ElemType{
int value;
};
typedef struct BiTNode{
ElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode,*BiTree;
//链式队列结点
typedef struct LinkNode{
BiTNode *data;
struct LinkNode *next;
}LinkNode;
typedef struct {
LinkNode *front,*rear;
}LinkQueue;
//访问节点
void visit(BiTree T){
printf("%d\n",T->data.value);
}
//前序遍历
void PreOrder(BiTree T){
if(T!=NULL){
visit(T);
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
//不带头节点
bool InitQueue(LinkQueue &Q){
Q.front=Q.rear=NULL;
return true;
}
bool QueueEmpty(LinkQueue Q){
if(Q.front==Q.rear) return true;
else return false;
}
//新元素入队,不带头节点
bool EnQueue(LinkQueue &Q,ElemType x){
LinkNode *s=(LinkNode*)malloc(sizeof(LinkNode));
s->data=x;
s->next=NULL;
if(Q.front==Q.rear){
Q.front=Q.rear=s;
}
else{
Q.rear->next=s;
Q.rear=s;
}
}
//新元素出队(不带头节点)
bool DeQueue(LinkQueue &Q,ElemType &x){
if(Q.front==NULL) return false;
else{
LinkNode *q=Q.front;
x=q.data;
Q.front=q->next;
if(Q.rear==q){ //这个判断条件很重要,q就是队尾
Q.front=NULL;
Q.rear=NULL;
}
free(q);
return true;
}
}
//层序遍历
void LevelOrder(BiTree T){
LinkQueue Q;
InitQueue(Q);
//设置一个中间结点指针备用
BiTree p;
//根结点入队
EnQueue(Q,T);
while(!QueueEmpty(Q)){
DeQueue(Q,p);
//访问出队结点
visit(p);
if(p->lchild!=NULL)EnQueue(Q,p->lchild);
if(p->rchild!=NULL)EnQueue(Q,p->rchild);
}
}
//求树的深度
int treeDepth(BiTree T){
if(T==NULL) return 0;
else{
int l=treeDepth(T->lchild);
int r=treeDepth(T->rchild);
return l>r?l+1:r+1;
}
}
int main(){
//定义一个空树
BiTree root=NULL;
//插入根节点
root=(BiTree)malloc(sizeof(BiTNode));
root->data={1};
root->lchild=NULL;
root->rchild=NULL;
//插入左孩子
BiTNode *p=(BiTNode *)malloc(sizeof(BiTNode));
p->data={2};
p->lchild=NULL;
p->rchild=NULL;
root->lchild=p;
return 0;
}
线索二叉树
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 10
//线索二叉树的结点
struct ElemType{
int value;
};
typedef struct ThreadNode{
ElemType data;
struct ThreadNode *lchild,*rchild;
int ltag,rtag;
}ThreadNode,*ThreadTree;
//指向当前访问结点的前驱
ThreadNode *pre=NULL;
//访问结点,同时修改tag值
void visit(ThreadNode *q){
if(q->lchild==NULL){ //左子树为空,建立前驱线索
q->lchild=pre;
q->ltag=1;
}
if(pre->rchild==NULL&&pre!=NULL){
pre->rchild=q; //建立后继线索
pre->rtag=1;
}
pre=q;//修改pre指针到q,访问操作完成
}
//先序线索化
void PreThread(ThreadTree T){
if(T!=NULL){
visit(T); //先处理根节点
if(T->ltag==0){
PreThread(T->lchild);
}
PreThread(T->rchild);
}
}
//中序遍历二叉树,一边遍历一遍线索化
void InThread(ThreadTree T){
if(T!=NULL){
InThread(T->lchild);
visit(T);
InThread(T->rchild);
}
}
//中序线索化二叉树T
void CreateInThread(ThreadTree T){
pre=NULL; //pre指针初始化为空
if(T!=NULL){
InThread(T);
if(pre->rchild==NULL){
pre->rtag=1;
}
}
}
int main(){
return 0;
}
中序线索二叉树实现中序遍历
#include<stdio.h>
#include<stdlib.h>
struct ElemType{
int value;
};
typedef struct ThreadNode{
ElemType data;
struct ThreadNode *lchild,*rchild;
int ltag,rtag;//0为子树,1为前驱后继(线索化)
}ThreadNode,*ThreadTree;
//找到中序第一个结点
ThreadNode *FristNode(ThreadNode *p){
while(p!=NULL) p=p->lchild;
return p;
}
ThreadNode *NextNode(ThreadNode *p){
if(p->rtag==0) return FristNode(p->rchild);
else return p->rchild;
}
///中序线索二叉树实现中序遍历
void InOrder(ThreadTree T){
for(ThreadNode *p=FristNode(T);p!=NULL;p=NextNode(T))
visit(T);
}
int main(){
return 0;
}
树
双亲存储法
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100
#define ElemType int
//顺序存储
typedef struct{ //树的结点定义
ElemType data;
int parent; //双亲的位置域,不存在双亲置为-1
}PTNode;
typedef struct{ //树的类型定义
PTNode nodes[MaxSize];
int n;
}PTree;
int main(){
return 0;
}
孩子表示法(顺序+链式存储)
#include<stdio.h>
#include<stdlib.h>
#define MaxSize 100
#define ElemType int
//孩子表示法,在顺序存储的基础上,用链表的方式存储它的孩子节点的位置
struct CTNode{ //定义链表结点
int child;
struct CTNode*next;
};
struct CTBox{
int data;
CTNode *firstNode;//一个记录该结点所有孩子的链表;
};
struct CTree{ //顺序存储所有结点
CTBox nodes[MaxSize];
int n;
};
int main(){
return 0;
}
孩子兄弟表示法(链式存储)
#include<stdio.h>
typedef struct CSNode{
ElemType data;
struct CSNode *firstchild,*nextsibling;//第一个孩子和右兄弟指针
}CSNode,*CSTree;
二叉查找树BST(二叉排序树)
#include<stdio.h>
#include<stdlib.h>
typedef struct BSTNode{
int key;
struct BSTNode *lchild,*rchild;
}BSTNode,*BSTree;
//在二叉查找树中找到值为key的结点(非递归,迭代实现)
BSTNode *BST_Search(BSTree T,int key){
while(T!=NULL&&T->key!=key){
if(T->key<key) T=T->lchild;
else return T=T->rchild;
}
return T;
}
//在二叉查找树中找到值为key的结点(递归实现)
BSTNode *BST_Search1(BSTree T,int key){
if(T==NULL) return NULL;
if(key==T->key) return T;
else if(key<T->key) return BST_Search(T->lchild,key);
else return BST_Search(T->rchild,key);
}
//在二叉排序树中插入值为k的结点(递归实现)
int BST_Insert(BSTree &T,int k){
//子树为空,新插入结点
if(T==NULL){
T=(BSTNode *)malloc(sizeof(BSTNode));
T->key=k;
T->lchild=T->rchild=NULL;
return 1;
}
//树中存在相同关键字的结点,插入失败
else if(T->key==k) return 0;
//插入到左子树
else if(k<T->key) return BST_Insert(T->lchild,k);
//插入到右子树
else return BST_Insert(T->lchild,k);
}
//按照str[]关键字序列建立二叉排序树
void Create_BST(BSTree &T,int str[],int n){
//建立根结点
T=NULL;
int i=0;
//依次插入每个结点
while(i<n){
BST_Insert(T,str[i]);
i++;
}
}
平衡二叉树(AVL树)
#include<stdio.h>
#include<stdlib.h>
//平衡二叉树的结点
typedef struct AVLNode{
int key;
int balance;
struct AVLNode *lchild,*rchild;
}AVLNode,*AVLNode;
图
邻接矩阵
#define MaxVertexNum 100
#define INFINITY 2147483647//最大的int值
typedef char VertexType;
typedef int EdgeType;
typedef struct{
VertexType Vex[MaxVertexNum]; //顶点表
EdgeType Edge[MaxVertexNum][MaxVertexNum]; //边表
int vexnum,arcnum; //图的顶点数和弧数
}MGraph;
邻接表
#include<stdio.h>
#include<stdlib.h>
#define MaxVertexNum 100
typedef struct ArcNode{ //边表结点
int adjvex;
struct ArcNode *next;
}ArcNode;
typedef struct VNode{ //顶点表结点
VertexType data;
ArcNode *first;
}VNode,AdjList[MaxVertexNum];
typedef struct{ //邻接表
AdjList vertices;
int vexnum,arcnum;
}ALGraph;
查找
顺序查找
#include<stdio.h>
#include<stdlib.h>
#define MaxVertexNum 100
typedef struct{
ElemType *elem;//0号单元留空
int TableLen;//表的长度
}SSTable;
int Search_Seq(SSTable ST,ElemType key){
ST.elem[0]=key; //哨兵
for(i=ST.TableLen;ST.elem[i]!=key;--i);
return i;
}
折半查找
#include<stdio.h>
#include<stdlib.h>
#define MaxVertexNum 100
typedef struct{
ElemType *elem;//0号单元留空
int TableLen;//表的长度
}SeqList;
int Binary_Search(SeqList L,ElemType key){
int low=0,high=L.TableLen,mid;
while(low<=high){
mid=(low+high)/2;
if(L.elem[mid]==key)
return mid;
else if(L.elem[mid]>key)
high=mid-1;
else
low=mid+1;
}
return -1;
}
排序
堆排序
#include <stdio.h>
#include <stdlib.h>
void swap(int* a, int* b)
{
int temp = *b;
*b = *a;
*a = temp;
}
void max_heapify(int arr[], int start, int end)
{
//建立父节点指标和子节点指标
int dad = start;
int son = dad * 2 + 1;
while (son <= end) //若子节点指标在范围内才做比较
{
if (son + 1 <= end && arr[son] < arr[son + 1])
//先比较两个子节点大小,选择最大的
son++;
if (arr[dad] > arr[son]) //如果父节点大於子节点代表调整完毕,直接跳出函数
return;
else //否则交换父子内容再继续子节点和孙节点比较
{
swap(&arr[dad], &arr[son]);
dad = son;
son = dad * 2 + 1;
}
}
}
void heap_sort(int arr[], int len)
{
int i;
//初始化,i从最後一个父节点开始调整
for (i = len / 2 - 1; i >= 0; i--)
max_heapify(arr, i, len - 1);
//先将第一个元素和已排好元素前一位做交换,再重新调整,直到排序完毕
for (i = len - 1; i > 0; i--)
{
swap(&arr[0], &arr[i]);
max_heapify(arr, 0, i - 1);
}
}
int main() {
int arr[] = { 3, 5, 3, 0 };
int len = (int) sizeof(arr) / sizeof(*arr);
heap_sort(arr, len);
int i;
for (i = 0; i < len; i++)
printf("%d ", arr[i]);
printf("\n");
return 0;
}