
c语言数据结构与算法
在mooc上学习浙江大学的数据结构与算法,特别感谢mooc这个平台以及授课老师
行躬
纸上得来终觉浅,绝知此事要躬行。
展开
-
堆--优先队列的完全二叉树--采用数组存储模拟实现(浙)
#include <stdio.h>#include <stdlib.h>#include <stdbool.h>#define MaxData 10000#define ERROR -1 /* 错误标识应根据具体情况定义为堆中不可能出现的元素值 */typedef struct HeapStruct *MaxHeap;typedef int ElementType;struct HeapStruct { ElementType *Elements;/原创 2021-11-30 16:57:54 · 161 阅读 · 0 评论 -
[Error] ‘for‘ loop initial declarations are only allowed in C99 or C11 mode解决
原创 2021-11-30 15:35:45 · 1119 阅读 · 0 评论 -
devc++没法调试解决
原创 2021-11-28 16:48:51 · 800 阅读 · 0 评论 -
两个序列是否对应相同搜索树的判别(浙--建议采用“调试”体会函数的调用过程)
#include <stdio.h>#include <stdlib.h> typedef struct TreeNode *Tree;struct TreeNode{ int v; Tree Left,Right; int flag;//设立标志判断是否为找过的函数,为函数check()与Judge()做准备 };//如何建立搜索树Tree MakeTree (int N);//根据首个元素创建一个结点,形成树根或创建一个新的结点 Tree NewNod原创 2021-11-28 13:10:10 · 497 阅读 · 0 评论 -
栈的链式存储的实现(浙)
#include <stdio.h>#include <malloc.h>typedef struct SNode *Stack;struct SNode { int Data; Stack Next;};//构建一个堆栈的头结点,返回指针Stack CreateStack() { Stack S; // S是一个指针 S = (Stack)malloc(sizeof (struct SNode)); S->Next = NULL; return原创 2021-11-17 00:18:00 · 791 阅读 · 0 评论 -
栈的顺序存储实现----用一个数组实现两个堆栈(浙)
#include <stdio.h>#include <malloc.h>#include <stdbool.h>#define MaxSizeArray 16//该程序的缺陷之处:调用CreateStack(int MaxSize)函数时,需与MaxSize保持一致(重在理解堆栈,哈哈) //用一个数组实现两个堆栈 typedef struct DStack *Stack;typedef int Position; struct DStack { in原创 2021-11-16 16:40:46 · 944 阅读 · 0 评论 -
线性表的链式存储实现(浙)
#include <stdio.h>#include <malloc.h>typedef struct LNode *List;struct LNode { int Data; List Next;};List PtrL;//创建指向空的结点 //函数声明int Length(List PtrL); List Find(int x,List Ptrl);List FindKth(int K, List Ptrl);List Insert(int x,int原创 2021-11-09 21:32:36 · 498 阅读 · 0 评论 -
数据结构--求最大子列和问题(浙)
#include <stdio.h>int MaxConsequnceSum(int arr[],int n);/*给定以序列数:序列{ -2, 11, -4, 13, -5, -2 } ,求其最大之列和 连续子列{ 11, -4, 13 }有最大的和20;思路:用一个数组存储该序列,用2个for循环,第一个for循环用于确定一个子列的最左边的元素,第2个for循环用于从确定的元素开始向右进行遍历,找到以该元素确定的子列最大的序列和,如何找到呢,只需要定义2个变量,一个变量用于进原创 2021-10-31 19:14:06 · 140 阅读 · 0 评论 -
c语言队列的顺序存储实现
#include <stdio.h>#include <malloc.h>#include <stdbool.h>//队列的顺序存储实现 typedef int Position;struct QNode { int *Data; Position Front,Rear;//定义队列的头部以及尾部 int MaxSize;//数组的大小 };typedef struct QNode *Queue;Queue CreateQueue(int Ma原创 2021-10-31 13:42:12 · 444 阅读 · 0 评论 -
二分查找的程序实现中,如果left和right的更新不是取mid+1和mid-1而是都取mid
原因:会导致程序进入死循环验证:以查找数组中不存在的元素进行验证原创 2021-10-26 16:52:51 · 694 阅读 · 1 评论 -
c语言-用数组模拟 --实现顺序表的-增,删,改,查
#include <stdio.h>#include <malloc.h>/*采用数组模拟线性表*/#define MAXSIZE 20typedef struct LNode * List;//定义一个指向LNode结构的指针 struct LNode{ int Data[MAXSIZE];//创建一个int类型的数组 int Last;//指向数组最后一个元素的下标,最后一个元素并不代表该数组最后一个位置 }; //struct LNode L;List原创 2021-10-14 21:02:33 · 327 阅读 · 0 评论 -
typedef用于创建自定义的指针类型
参考:c primer plus总结:将代码中的char改为别的类型(例如:自定义的结构类型也可)原创 2021-10-12 11:25:43 · 237 阅读 · 0 评论 -
关于typedef用于结构
选自c primes plus原创 2021-10-12 10:36:35 · 93 阅读 · 0 评论 -
计算给定多项式在给定点处的值--探究算法设计效率问题
#include <stdio.h>#include <time.h>//引入clock()计时函数#include <math.h>clock_t start,stop;double duration;#define MAXN 10//多项式的最大项系数,注意从0开始#define MAXK 1e7//设置重复调用次数,从而有时间积累,看到函数所花费的时间 double f1(int n,double a[], double x);double f2原创 2021-10-10 15:44:55 · 304 阅读 · 0 评论 -
递归与循环对比--递归有时候太吃内存
#include <stdio.h>void PrintN1(int N) {//采用for循环 int i; for(i = 1; i <= N; i++) { printf("%d\n",i); } return;}void PrintN2(int N) {//采用递归 if(N) { PrintN2(N - 1); printf("%d\n",N); } return;}int main(void) { int N; scanf("%d"原创 2021-10-10 14:49:17 · 240 阅读 · 0 评论 -
冒泡排序及其优化(c语言)
#include <stdio.h>#include <stdbool.h>void bubbleSort(int *array, int length) { bool flag = false;//优化 for(int i = 0; i < length - 1; i++) {//最多只需遍历length-1次就可以将其排序好 for(int j = 0; j < length - 1 - i; j++) {原创 2021-10-04 15:43:06 · 219 阅读 · 0 评论 -
c-二分查找运用
#include <stdio.h>int binsearch(int x, int v[], int n);int main(void){ int v[8] = {1,3,5,7,9,15,18,23}; printf("%d\n",binsearch(15, v,sizeof(v) / sizeof(v[0]))); return 0;} /*法1:二分查找运用 */ int binsearch(int x, int v[], int n) { int low,hig原创 2021-07-30 11:01:00 · 94 阅读 · 0 评论