算法与数据结构
算法的参考,多种语言
MACHINE_128
这个作者很懒,什么都没留下…
展开
-
冒泡排序时间性能测量(最差情形)
#include<stdio.h>#include<time.h>#define MAX_SIZE 1001#define SWAP(x,y,t) ((t)=(x),(x)=(y),(y)=t)void sort(int [], int);int main(void){ int step=10; int a[MAX_SIZE]; double dur...原创 2017-12-31 22:05:26 · 483 阅读 · 0 评论 -
约瑟夫问题 链表实现
list.h (链表函数接口)typedef struct node* link;struct node { int item; link next;};typedef link Node;void initNodes(int);void freeNode(link);link newNode(int);void insertNext(link,link);...原创 2019-08-16 11:12:26 · 401 阅读 · 0 评论 -
FIFO队列 ADT接口 链表实现
FIFO.h (接口)#include "Item.h"void QUEUinit(int);int QUEUempty(void);void QUEUput(Item);Item QUEUget(void);Item.h (自定义类型)typedef char Item;FIFO.c (接口实现)#include "FIFO.h"#include <s...原创 2019-08-16 11:10:36 · 165 阅读 · 0 评论 -
FIFO队列 ADT接口 数组实现
FIFO.h (接口)#include "Item.h"#include <stdlib.h>typedef struct STACKnode *link;struct STACKnode{ Item item; link next;};void QUEUEinit(int);int QUEUEempty(void);void QUEUEput...原创 2019-08-14 14:15:56 · 184 阅读 · 0 评论 -
N个点中寻找多个最近两点的计算O(N²)
#include<math.h>#include<stdio.h>#include<stdlib.h>typedef struct point{ float x; float y;}point;float distance(point a,point b);float randFloat(void);int main(void){ ...原创 2018-03-05 17:06:07 · 407 阅读 · 0 评论 -
对字符串数组进行排序
#include<stdio.h>#include<stdlib.h>#include<string.h>#define Nmax 1000#define Mmax 10000char buf[Mmax]; int M=0;int compare(void *i, void *j);int main(void){ int N; ch...原创 2018-03-01 17:03:39 · 1627 阅读 · 0 评论 -
链表插入排序 链表指针实现
#include<stdlib.h>#include<stdio.h>typedef struct node* link; //链接struct node{ int item; link next; };//节点int main(void){ struct node heada, headb; link t, u, x, a=&he...原创 2018-02-14 17:56:36 · 293 阅读 · 0 评论 -
循环链表范例(约瑟夫问题)指针实现
#include<stdlib.h>#include<stdio.h>#include<math.h>typedef struct node* link; //链接struct node{ int item; link next; };//节点int main(void){ int N,M; printf("输入人数N\n...原创 2018-02-03 10:25:30 · 326 阅读 · 0 评论 -
循环链表范例(约瑟夫问题) 数组实现
#include<stdio.h>#include<math.h>#include <stdlib.h>#define CALLOC(P, N, S)\ if(!((P)=calloc(N, S))){\ printf( stderr, "Insufficient memory");\ exit(EXIT_FAILURE);\ } in...原创 2018-02-14 10:59:36 · 445 阅读 · 0 评论 -
图 ADT接口 遍历运算 常规运算 邻接矩阵实现
Graph.h (图的结构, 遍历, 常规操作接口)/*定义图的最大定点数, 它要大于等于具体图的顶点树n*/#define MaxVertexNum 12/*定义图的最大边数,它要大于等于具体图的边数e*/#define MaxEdgeNum 20 /* 定义MaxValue 为一个符号常量,其值要大于邻接矩阵中所有有效值之和*/#define MaxValue 1000...原创 2019-08-12 19:04:43 · 339 阅读 · 0 评论 -
双端队列 ADT接口 数组实现
Deque ADT接口 DEQUEUE.h#include <stdlib.h>#include "Item.h"void DEQUEUEinit(int);void DEQUEUEerror(void);Item DEQUEUEheadget(void);Item DEQUEUEtailget(void);void DEQUEUEheadput(Item);vo...原创 2019-08-12 19:40:11 · 205 阅读 · 0 评论 -
双端队列 ADT接口 链表实现
Deque ADT接口 DEQUEUE.h#include <stdlib.h>#include "Item.h"typedef struct DEQUEUEnode *link;struct DEQUEUEnode{ Item item; link next; link last;};void DEQUEUEinit(int);void...原创 2019-08-12 19:35:34 · 211 阅读 · 0 评论 -
随机队列 ADT接口 数组实现
Random queue ADT接口 RANDOM.h#include <stdlib.h>#include "Item.h"#include <time.h>#include <stdbool.h>void RANDOMQUEUEtailput(int);void RANDOMQUEUEinit(int);bool RANDOMQUEUEi...原创 2019-08-12 19:31:12 · 180 阅读 · 0 评论 -
复数 一级ADT实现
COMPLEX.h/*typedef struct{ float RE; //实部 float IM; //虚部}Complex;*/typedef struct complex * Complex;Complex COMPLEXinit(float, float);float Re(Complex);float Im(Complex);Complex C...原创 2019-08-12 19:28:29 · 1067 阅读 · 0 评论 -
LIFO栈 ADT接口 数组实现
LIFO 栈结构typedef int ElemenType;struct seqStack{ ElemeType data[MaxSize]; int top; };typedef struct seqStack* Stack;LIFO 栈基本操作//LIFO 顺序栈 初始化void InitStack(Stack s){ ...原创 2019-08-12 19:23:39 · 135 阅读 · 0 评论 -
LIFO栈 ADT接口 链表实现
LIFO 链栈结构typedef int ElemType;struct node{ ElemType data; struct node* next; };typedef struct node* Stack;LIFO 链栈基本操作//LIFO 链栈初始化void InitStack(Stack top){ top = NULL;}//...原创 2019-08-12 19:20:16 · 127 阅读 · 0 评论 -
LIFO栈 ADT接口 实现十进制转其他进制
LIFO 接口 Stack.h//LIFO 链栈初始化void InitStack(Stack top){//LIFO 链栈判断栈空 boolean StackKEmpty(Stack top){//LIFO 链栈进栈 void Push(Stack top, ElemType x){//LIFO 链栈出栈 ElemType Pop(Stack top){//LIFO 链栈读...原创 2019-08-12 19:17:11 · 174 阅读 · 0 评论 -
二叉树 ADT接口 遍历算法 常规运算
BTree.h (结构定义, 基本操作, 遍历)#define MS 10typedef struct BTreeNode{ char data; struct BTreeNode * left struct BTreeNode * right;}BTreeNode;BTreeN...原创 2019-08-12 19:08:42 · 439 阅读 · 0 评论 -
静态栈抽象数据类型stack实现
#include#include#include#define MAX_STACK_SIZE 10 //堆栈的最大大小typedef struct{ int key; //其他内容}Element;//模板类型void push( Element item, Element *stack, short *top);//向堆栈压入,入栈.成功返回1失败返回0Eleme原创 2018-01-26 21:54:56 · 298 阅读 · 0 评论 -
多项式_计算 动态内存存储
#include#include#include#include#define CALLOC(P, N, S)\ if(!((P)=calloc(N, S))){\ printf( stderr, "Insufficient memory");\ exit(EXIT_FAILURE);\ }#define REALLOC(P, S)\ if(!((P)=realloc(P原创 2018-01-09 16:01:17 · 324 阅读 · 0 评论 -
按升序打印X,Y,Z的整数值
#include #define TRUE 1#define FALSE 0int main(){ int x,y,z; printf("x: "); scanf("%d", &x); printf("y: "); scanf ("%d", &y); printf("z: "); scanf("%d", &z); if ((x<y)&&(y<z)) { p原创 2017-12-30 10:40:36 · 298 阅读 · 0 评论 -
打印N个真值的所有真值组合
例:N=2 (true,true),(false,true),(true,false),(false,false)#includeint count=0;void perm(char *list,int n,int i);int main(void){ size_t n; printf("Enter the size:\n"); scanf("%d",&n);原创 2017-12-30 10:37:11 · 326 阅读 · 0 评论 -
Horner规则求多项式
/* Horner *//*多项式:A(x)=a[n]X^n+a[n-1]x^n-1+...+a[1]X^1+a[0]X^0*/#include <stdio.h>long int horner(int coefficient[], int n, int x)/*coefficient[]为待求多项式的系数数组,n为数组大小,x为多项式中未知数x的具体值*/{...原创 2017-12-30 10:33:16 · 390 阅读 · 0 评论 -
矩阵转置 动态内存实现
#include#include#define SWAP(X,Y,TEMP) ((TEMP)=(X),(X)=(Y),(Y)=(TEMP))#define MAX_SIZE 3#define CALLOC(P, N, S)\ if(!((P)=calloc(N, S))){\ printf(stderr, "Insufficient memory");\ exit(EXIT_原创 2018-01-03 09:52:48 · 524 阅读 · 0 评论 -
矩阵加法 时间性能测量
#include #include #define MAX_SIZE 100typedef struct { long repetitions; double duration;}times;void timer(void (*add)( int a[][MAX_SIZE], int b[][MAX_SIZE], int c[][MAX_SIZE], int n),原创 2018-01-01 17:09:32 · 337 阅读 · 0 评论 -
折半排序 循环&递归 时间性能测试
#include #include #define COMPARE(x, y) (((x) <(y)) ?-1:((x)==(y))? 0:1)#define MAX_SIZE 1000000typedef struct { double duration; long repetitions;}times;int binsearch_f(int list[],int sear原创 2018-01-01 15:36:25 · 356 阅读 · 0 评论 -
数组求和(递归和循环)函数 时间性能测量(最差情形)
#include #include #define MAX_SIZE 1000typedef struct { double duration; long repetitions;}times;double sum_f(double list[], int n);double sum_r(double list[], int n);void timer(double(*原创 2017-12-31 22:12:25 · 374 阅读 · 0 评论 -
矩阵转置函数时间性能测量(最差情形)
#include#include#define MAX_SIZE 100#define SWAP(x, y, t) ((t)=(x), (x)=(y),(y)=t)void transpose(int a[][MAX_SIZE],int n);int main(void){ int step=10; double duration; clock_t start; lon原创 2017-12-31 22:09:22 · 303 阅读 · 0 评论 -
Fibonacci使用递归和循环实现
#includedouble Fibonacci(int i);double Fibonacci_(int i);int main(void){ int i; printf("Enter the number i:\n"); scanf("%d", &i); printf("%f %f",Fibonacci(i),Fibonacci_(i));原创 2017-12-30 10:54:21 · 395 阅读 · 0 评论 -
计算二项式系数使用循环和递归实现
#include <stdio.h>double recurBinom(int, int);double iterBinom(int, int);double recurFact(int n);int main(){ int n,m; printf("n:(>=0): "); scanf("%d", &n); while (n <...原创 2017-12-30 10:56:01 · 978 阅读 · 0 评论 -
抛硬币的模拟
#include#include#define CALLOC(P, N, S)\ if(!((P)=calloc(N, S))){\ printf( stderr, "Insufficient memory");\ exit(EXIT_FAILURE);\ }int heads(void); int main(void){ int *F; int N,M,cnt=原创 2018-02-01 14:06:57 · 1267 阅读 · 0 评论 -
埃拉托色尼筛算法查找素数(动态内存实现)
#include#include#define CALLOC(P, N, S)\ if(!((P)=calloc(N, S))){\ printf( stderr, "Insufficient memory");\ exit(EXIT_FAILURE);\ } int main(void){ int *a; int N; printf("输入素数范围:");原创 2018-02-01 12:19:50 · 241 阅读 · 0 评论 -
计算矩阵转置函数的步总数公式
#includeint count=0;#define MAX_SIZE 2#define SWAP(x, y, t) ((t)=(x), (x)=(y),(y)=t)void transpose(int a[][MAX_SIZE]);int main(void){ int a [MAX_SIZE][MAX_SIZE]={{1,2},原创 2017-12-30 11:36:42 · 560 阅读 · 0 评论 -
计算矩阵点乘函数步总数的公式
#includeint count=0;#define MAX_SIZE 2void dot_p(int a[][MAX_SIZE], int b[][MAX_SIZE], int c[][MAX_SIZE], int rows, int cols);int main(void){ int a[MAX_SIZE][MAX_SIZE]={{9,8},{2,3}}; i原创 2017-12-30 11:31:44 · 2105 阅读 · 0 评论 -
计算矩阵乘法函数步总数的公式
#includeint count=0;#define MAX_SIZE 2void mult(int a[][MAX_SIZE], int b[][MAX_SIZE], int c[][MAX_SIZE], int rows, int cols);int main(void){ int a[MAX_SIZE][MAX_SIZE]={{9,8},{2,3}}; in原创 2017-12-30 11:26:29 · 411 阅读 · 0 评论 -
计算打印矩阵的函数步总数公式
#includeint count=0;#define MAX_SIZE 2void printf_(int a[][MAX_SIZE], int rows, int cols);int main(void){ int a[MAX_SIZE][MAX_SIZE]={{2,3},{4,5}}; printf_(a, MAX_SIZE,MAX_SIZE); prin原创 2017-12-30 11:17:29 · 232 阅读 · 0 评论 -
Hanoi塔
#include <stdio.h>#define Tower1 1#define Tower2 2#define Tower3 3void towers_of_hanoi(int, int, int, int);int main(){ int n_disks; printf("Number of disks: "); scanf("%d", &n_dis...原创 2017-12-30 10:57:58 · 180 阅读 · 0 评论