随笔分类 -  算法:C语言实现

摘要:#include #include "Item.h"static Item *q;static int N, head, tail;void QUEUEinit(int maxN){ q = (Item *)malloc((maxN+1)*sizeof(Item)); N = maxN+1; head = N; tail = 0;}int QUEUEempty(){ return (head%N == tail);}void QUEUEput(Item item){ q[tail++] = item; tail = tail%N;}Item QUEUE... 阅读全文
posted @ 2013-11-20 13:14 CJin 阅读(1399) 评论(0) 推荐(0)
摘要:这里就附了一个实现文件#include #include "item.h"static Item *s;static int N;void STACKinit(int maxN){ s = (Item*)malloc(maxN*sizeof(Item)); N = 0;}int STACKempty(){ return N == 0;}void STACKpush(Item item){ s[N++] = item;}Item STACKpop(){ return s[--N];} 阅读全文
posted @ 2013-11-18 23:41 CJin 阅读(559) 评论(0) 推荐(0)
摘要:一 通用数据类型的定义不依赖于对象类型的代码,其中我们使用typedef来制定项的类型.typedef int Item;#define eq(A,B) (A == B)二 接口的定义接口中并不包含如何实现函数的信息,甚至也没有关于任何关于任何运行的信息。p79页重点讲解“接口”,还有就是接口的作用..在接口中明确定义了算法在通用对象上执行所需的操作。这种机制允许我们无需向客户程序提供关于数据表示的任何信息,也就真正给了我们一个真实的ADT。void STACKinit(int);int STACKempty(void);void STACKpush(Item);Item STACKpop() 阅读全文
posted @ 2013-11-18 23:39 CJin 阅读(394) 评论(0) 推荐(0)
摘要:/*注解: 在处理字符串时, 我们通常使用一个指向包含那个字符串的缓冲区的指针, 如上图, 应为指针要比长度大小可变的字符串自身更容易操纵输入完后: 对于这个程序理解,1. 首先定义了一个用于存储字符串数组的 缓冲区字符串2. a是个字符串指针类型的数组3. a[N]表示了含有N个字符串的指针如果这个文件后缀名写为cpp, 那么编译的时候会出错, 因为CPP 和C的函数的压栈顺序不一样, 所以qsort( particles, n, sizeof( particle ), &cmp );这一行一直没能编译通过,报的错误是error C2664: “qsort” : 不... 阅读全文
posted @ 2013-11-14 23:25 CJin 阅读(9791) 评论(0) 推荐(0)
摘要:#include #include #define N 10000int main(char argc, char* argv[]){ int i, j, t; /* 字符串的最大长度为N, 然后查找p所指向的字符串在a串中出现过的次数 当输入终止符ctrl+z时,终止继续读入 */ char a[N], *p = argv[1]; for (i = 0; i %d\n",i); for (i = 0; a[i] != 0; i++) { for (j = 0; p[j] != 0; j++) ... 阅读全文
posted @ 2013-11-01 23:50 CJin 阅读(1499) 评论(0) 推荐(0)
摘要:#include #include typedef struct node *link;struct node { int item; link next;};/* 初始化一个节点数为n的一个链表*/void init(link a, int n){ int i; for (a, i = 0; i next = malloc(sizeof(*a))); a->item = rand()%100; a->next = NULL; }}/* 遍历链表*/void traverse(link p){ while (p != NULL) ... 阅读全文
posted @ 2013-10-30 00:02 CJin 阅读(620) 评论(0) 推荐(0)
摘要:#include #include typedef struct node *link;struct node { int item; link next;};link reverse(link x){ link r = NULL, y = x, t; while (y != NULL) { t = y->next; y->next = r; r = y; y = t; } return r;}int main(){ int i, N = 9; struct node head... 阅读全文
posted @ 2013-10-27 14:01 CJin 阅读(514) 评论(0) 推荐(0)
摘要:数组的使用:求素数 埃拉托色尼筛法:/*************************************************************** 这个程序展示了数组的应用,快速的访问数组内的元素 用数组的值作为标志位, 而使用了下表来保存素数, 这样的设计很奇妙 ****************************************************************/#include #include #define N 100int main(){ int i,j; int *f = (int *)malloc(N*sizeof(... 阅读全文
posted @ 2013-10-25 19:08 CJin 阅读(2098) 评论(0) 推荐(0)