- 博客(46)
- 收藏
- 关注
原创 二叉堆的实现
#include <stdio.h>#include <stdlib.h>#define minsize 10#define mindata 0struct HeapStruct;//这就是一整个堆typedef struct HeapStruct* Heap;Heap InitializeHeap(int MaxSize);void Destroy(Heap H);void MakeEmpty(Heap H);void Insert(int x, Heap H.
2022-02-02 16:21:58
843
1
原创 哈希表的开放地址实现
#include <stdio.h>#include <stdlib.h>#define MinSize 10typedef unsigned int Index;typedef Index Position;struct HashTbl;typedef struct HashTbl* HashTable;HashTable InitializeTable(int TableSize);void DestroyTable(HashTable H);Posit.
2022-01-30 17:57:15
178
原创 哈希表的分散链实现
#include <stdio.h>#include <stdlib.h>#define MinSize 10struct ListNode;typedef struct ListNode* Position;struct HashTabl;typedef struct HashTbl* HashTable;struct ListNode { int element; Position next;};typedef Position List;struc.
2022-01-30 15:28:12
132
原创 AVL树的实现
struct AVLNode;typedef struct AVLNode* Position;typedef struct AVLNode* AT;AT MakeEmpty(AT T);Position Find(int x, AT T);Position FindMin(AT T);Position FindMax(AT T);AT Insert(int x, AT T);AT Delete(int x, AT T);struct AVLNode { int element;.
2022-01-30 09:50:53
109
原创 二叉树的实现
#include <stdio.h>#include <stdlib.h>struct TreeNode;typedef struct TreeNode* Position;typedef struct TreeNode* SearchTree;SearchTree MakeEmpty(SearchTree T);Position Find(int x, SearchTree T);Position FindMin(SearchTree T);Position .
2022-01-27 16:19:00
74
原创 DSAAC第三章课后习题前几题
//3.1 打印List Lvoid print_list(List L){ Position p = L->next; while (p->next != NULL) printf(p->data);}//3.2 根据List P打印List Lvoid PrintLots(List L, List P){ int dif = P->data, counter; while (P->next != NULL&&L->nex.
2022-01-27 11:21:37
897
原创 图的存储/DFS/BFS(自己写的)
#include <stdio.h>#include <stdlib.h>#define max 10typedef int VertexType;typedef int WeightType;struct am_graph{ VertexType vexs[max];//顶点表 WeightType edges[max][max];//邻接矩阵 int vex_num, arcnum;//顶点、边数};typedef struct am_graph* A.
2021-10-15 15:30:45
127
原创 Dijkstra算法(第一版)(邻接表)
#include <stdio.h>#include <stdbool.h>#define NotAVertex -1#define NumVertex 7typedef int Vertex;typedef int DistType;typedef int WeightType;typedef struct { int i; }list;struct TableEntry { list header; int known; Vertex path; D.
2021-10-11 15:34:50
160
原创 邻接表建立图
要吐了,typedef东西太多了,完全记不住......#include <stdio.h>#define MaxVNum 10typedef int Vertex;typedef int WeightType;typedef struct AdjNode* EdgeListNode;struct AdjNode { Vertex NodeName; WeightType weight; struct Adjnode* next;/*是指向下一个点,但点里面不是只有no
2021-10-08 21:01:40
373
原创 直接写出后序遍历(由先序遍历和中序遍历数组结果)
/*根据先序遍历和中序遍历的数组结果,直接写出后序遍历的数组*//*in_head和pre_head从0开始*/void solve(int root_in_pre, int in_head, int post_head, int current_tree_len){ /*pre_head用来决定什么写入post, 递归左边就要加+1, 递归右边要加上左子树长度*/ /*in_head决定从哪里开始找,拿根节点划分更新子树长度,递归左边不用动,递归右边就要+左子树长度+1*/ /*post...
2021-10-08 16:19:46
326
原创 用最小堆把一列数排成完全二叉树
其实少了一个排序算法,但我们假设从小到大排好了。/*完全二叉搜索树,假设0用上了,注意弄清下标和个数之间的关系*/#include <math.h>int GetLeftLen(int num){ int left, temp = 1; while (temp < num) temp *= 2; /*虽然多了,但恰好是层数*/ int k = (pow(2, temp) - 1) - pow(2, temp - 2); /*k代表了左子树最后一个下标,有t层至多
2021-10-08 16:17:39
144
原创 DSAAC习题3.25 数组实现队列的完整操作集
#ifndef _queue_hstruct queue_node;typedef struct queue_node* queue;int is_empty(queue Q);int is_full(queue Q);queue create_queue(int MaxElements);void dispose_queue(queue Q); void make_empty(queue Q); void enqueue(queue Q, int X);void dequeue(.
2021-09-25 16:10:29
82
原创 DSAAC习题3.21 一个数组实现2个stack
/*implement 2 stacks by 1 array*/#include <stdio.h>#include <stdlib.h>#define max 20struct stack_node { int top_left; int top_right; int* a;};typedef struct stack_node* stack;void make_empty(stack s){ s->top_left = -1; s->.
2021-09-25 11:48:01
67
原创 DSAAC习题3.12 倒转链表
用stack确实方便;先布置好开头再一直while也很方便,就是有点难想。/* reverse a singly linked list */#include <stdio.h>#include <stdlib.h>struct node { int data; struct node* next;};typedef struct node* list;typedef struct node* position;position attatch(posi
2021-09-25 11:00:38
75
原创 自动生成1-100单向链表(多练习几遍)
#include <stdio.h>#include <stdlib.h>struct node { int data; struct node* next;};typedef struct node* list;typedef struct node* position;position attatch(position p, int x){ position new = malloc(sizeof(struct node)); new->data.
2021-09-24 17:34:47
206
原创 DSAAC习题3.11 对递归的理解!!(学到了,学到了)
重点看find_recur(list l, int x)函数,理解递归。/*find a particular element*/#include <stdio.h>#include <stdlib.h>struct node { int data; struct node* next;};typedef struct node* list;typedef struct node* pos;pos find_non_recur(list l.
2021-09-24 16:19:56
78
原创 DSAAC习题3.10 约瑟夫环问题(击鼓传花)(未成功)
玩家数和传递击鼓次数m,n对于一定的数字组合无法运行,有的时候时access violat。目前只能感觉是底层包括堆栈的问题,等学过硬件知识要回头看。/*击鼓传花, Josephus*//*其实单链表也能做,反正都是一个方向传*//*只不过有指针pre更好做*/#include <stdio.h>#include <stdlib.h>struct node { int num; struct node* next; struct node* pre;};t
2021-09-24 15:13:58
183
原创 DSAAC习题3.6
/*默认header和NULL*//*默认数据输入是次数从高到底排序*/#include <stdio.h>#include <stdlib.h>struct node { int coef; int exp; struct node* next;};typedef struct node* term;typedef struct node* poly;void insert(int c, int e, term p){ term temp = mal.
2021-09-24 10:54:30
74
原创 DSAAC习题3.4和3.5
/*默认有header*//*FOR singly Linked Lists*//*swap two adjcent elements*/#include <stdio.h>typedef struct lnode* list;typedef struct lnode* position;struct lnode { int data; struct lnode* next;};void finish(position p){ p->next = NULL;}.
2021-09-23 17:32:33
67
原创 DSAAC习题 3.3
/*默认有header*//*FOR singly Linked Lists*//*swap two adjcent elements*/#include <stdio.h>typedef struct lnode* list;typedef struct lnode* position;struct lnode { int data; struct lnode* next;};position find_precell_by_pos(list l,position po.
2021-09-23 15:29:37
82
原创 DSAAC习题3.2
#include <stdio.h>/*习题3.2,这里我们认为有header*/typedef struct lnode* ptrnode;typedef ptrnode list;typedef ptrnode position;struct lnode { int data; struct lnode* next;};/*这是倒着插进去的*/void insert(int x, list positon){ list temp = malloc(sizeof(s.
2021-09-23 11:36:07
78
原创 DSAAC Stack的两种实现方法
#ifndef _stack_h_linked_liststruct node;typedef struct node* ptrnode;typedef ptrnode stack;int is_empty(stack s);stack create_stack(void);void dispose_stack(stack s);void make_empty(stack s);void push(int x, stack s);int top(stack s);void pop(.
2021-09-22 22:03:07
74
原创 判断二叉树是否同构(运行不成功)
/*判断两个二叉树是否同构*//*输入形式如a 1 2b 3 4c 5 -d - -e 6 -g 7 -f - -h - -*//**********************************/#define max 10#define tree int#define NULL -1#include <stdio.h>/*构建静态链表的结构数组单元*/struct TreeNode{ char data; tree left; tree right;.
2021-09-20 17:53:58
113
原创 二叉树 三种遍历的实现(递归)
要求:代码:/*完美通过先序遍历二叉树*//*输出应当为124583697*/#include <stdio.h>typedef struct TreeNode* position;struct TreeNode { int data; position left; position right;};void attatch(position a, position b);void preorder(position a);int main(void).
2021-09-19 22:09:29
90
原创 吐了吐了 多项式的addition和multiplication 有bug不想改了
/*Polynomials*/#include <stdio.h>#include <stdlib.h>struct node { int coef; int exp; struct node* next;};typedef struct node* Poly;void attach(int c, int e, Poly rear){ Poly new = malloc(sizeof(struct node)); new->coef = c; n.
2021-09-18 17:12:48
112
原创 一、DSAAC 链表的实现
#include <stdio.h>#ifndef _List_Hstruct Node;typedef struct Node* PtrToNode;typedef PtrToNode List;typedef PtrToNode Position;List make_empty(List l);int is_empty(List l);int is_last(Position p, List l);Position Find1(int x, List l);Pos.
2021-09-18 10:19:11
105
原创 五、队列的实现(有bug)
#define max 6struct QueueRecord;typedef struct QueueRecord* Queue;Queue Create_Queue(int maxsize);void MakeEmpty(Queue q);void DisposeQueue(Queue q);int Is_Empty(Queue q);int Is_Full(Queue q);void Enque(Queue q);int Dequeue(Queue q);struct Qu.
2021-09-14 11:47:08
80
原创 四、用序列链表实现堆栈
ptrls就是悬在外面,指明地址,本身的data没有用,这点区别强调一下。#include <stdio.h>/*用链表实现堆栈, O->O->O->O 那肯定用左边*/struct LSNode { int data; struct LSNode* next;};typedef struct LSNode* LS;/*建立空linked_stack*/LS make_empty_linked_stack(void){ LS ptrls = mallo
2021-09-13 17:24:45
65
原创 三、堆栈的一般(数组)实现
#include <stdio.h>#define max 20/*通常用一维数组来实现堆栈*/typedef struct SNode* stack;/*stack->stack*/struct SNode { int data[max]; int top;/*下标,就是最后一个数,不是指向它后面的空位置*/};/*建立空栈*/stack make_empty_stack(void){ stack ptrs = malloc(sizeof(struct SNode.
2021-09-13 16:13:15
104
原创 二、链表的一般实现
#include <stdio.h>/*链表的一般实现*/typedef struct LNode* list;struct LNode { int value; struct LNode* next;};/*建立一个空表*/list make_empty(void) { list head = malloc(sizeof(struct LNode)); head->next = NULL; return;}/*求表长*/int length(list p.
2021-09-13 12:31:19
57
原创 一、链表的数组实现
链表的数组实现#include <stdio.h>/*链表的数组实现*/#define max 20typedef struct LNode* list;struct LNode { int data[10];/*假设有10个*/ int last;/*从0开始,是数组下标,不是第几个位置*/};/*建立空表*/list make_empty(void){ list ptrL; ptrL = malloc(sizeof(struct LNode)); ptrL
2021-09-13 10:07:06
305
原创 CPAMA 利用结构函数更新时间输出(有地方没搞懂)
time updatetime(time now){ if (now.second == 59) { now.minute += 1; now.second = 0; if (now.minute == 60) { now.hour += 1; now.minute = 0; if (now.hour == 24) now.hour = 0; } } else now.second += 1; return now;}定义的返回time.
2021-09-03 21:53:36
78
原创 CPAMA 指针与多维数组的一点思考
#include <stdio.h>int main(void){ int b[2][3] = {{ 1,2,3 }, { 7, 5, 6 }}; int(*p)[3] = b;//这里b指向一个长度为3的一维数组; //b=&b[0][0]=*b;b+1=*(b+1)=&b[1][0]; printf("%d\n", b);//所以当b指向一个一维数组,b代表了首元素; printf("%d\n", &b[0][0]); printf("%d\n", .
2021-09-02 17:54:02
83
原创 CPAMA 编程题11.3 找出最大公约数化简分数
#include <stdio.h>void reduce(int fenzi, int fenmu, int* _fenzi, int* _fenmu);int fractor(big, small){ int t = small; while ((big % t) != 0||(small % t)!=0) { t--; } return t;}int main(void){ int fenzi, fenmu; scanf("%d/%d", &fe.
2021-08-30 12:07:58
82
原创 CPAMA 编程题11.1 用指针计算不同面值的纸币应该使用几张
有20、10、5、1元,编写函数,使得函数形式如下:void qian(int dollars, int* twenties, int* tens, int* fives, int* ones);#include <stdio.h>void qian(int dollars, int* twenties, int* tens, int* fives, int* ones);int main(void){ int dollars; printf("enter the numb
2021-08-30 10:45:53
99
原创 CPAMA 指针在字符组长度未知情况下的应用
#include <stdio.h>void print(char* c){ int i = 0; while (c[i] != '\0') { printf("%c", c[i]); i++; } printf("\n");}int main(void){ char x[20] = "hello"; print(x); return 0;}实际上,c[i]是*(c+i)='xxxxx',c被赋予了传进的字符串第一个的地址。只要*(c+i)里的.
2021-08-29 16:41:20
77
原创 CPAMA 关于指针的一点注意
#include <stdio.h>int sumofit(int a[],int size){ int i, sum = 0; for (i = 0; i < size; i++) { sum += a[i]; } return sum;}int main(void){ int a[] = { 1,2,3,4,5 }; int size = sizeof(a) / sizeof(a[0]); int total = sumofit(a, size);.
2021-08-29 15:27:56
69
原创 CPAMA 指针的数组索引
#include <stdio.h>int main(void){ int a[] = { 1,2,3,4,5,6,7,8,9,10 }; int* p = a; printf("指针索引\naddress value\n"); for (int i = 0; i < 10; i++) { printf("%d %d\n", a + i,*(a+i)); } printf("数组索引\naddress value.
2021-08-29 14:28:39
100
原创 CPAMA随手练习 关于指针的几点说明
#include <stdio.h>int main(void){ int a = 9; int* p; p = &a; printf("value of a = %d\n", a); printf("address of a = %d\n", &a); printf("p是啥 = %d\n", p); printf("&p = %d\n", &p); printf("value of p = %d\n", *p); *p = 12; p.
2021-08-21 21:29:03
67
原创 CPAMA练习题 猜随机数
/*Ask user to guess a hidden number*/#include <stdio.h>#include <stdlib.h>#include <time.h>#define max_number 20/*external variable*/int secret_number;/*prototypes*/void initialize_number_generator(void);void choose_new_secre.
2021-08-20 11:35:02
70
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人