diff --git "a/2017-1/zjc/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/C`@Q8B]~@~$M)%[LS]QSRL0.png" "b/2017-1/zjc/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/C`@Q8B]~@~$M)%[LS]QSRL0.png" new file mode 100644 index 00000000..b0eb56d5 Binary files /dev/null and "b/2017-1/zjc/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/C`@Q8B]~@~$M)%[LS]QSRL0.png" differ diff --git "a/2017-1/zjc/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/main.c" "b/2017-1/zjc/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/main.c" new file mode 100644 index 00000000..6a7a5d2a --- /dev/null +++ "b/2017-1/zjc/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/main.c" @@ -0,0 +1,221 @@ +#include +#include +#include + +#define INFINITY INT_MAX //最大值为无穷 +#define MAX_VERTEX_NUM 20 //最大顶点数 + +typedef enum { + DG,DN,UGD,UDN +}GraphKind;//{有向图,有向网,无向图,无向网} + +typedef int VRType, InfoType; + +typedef struct ArcCell { + VRType adj;//VRType是顶点关系类型,对无权图,用1或0 + //表示相邻否;对有权图,则为权值类型 + InfoType *info;//该弧相关信息的指针 +}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; + +typedef struct { + AdjMatrix arcs; // 邻接矩阵 + int vexnum, arcnum; // 图的当前顶点数和弧数 +}Graph; + +typedef enum { + OK, + ERROR, + OVERFLOW +}Status; + +Status CreateGraph(Graph *G) { + //采用邻接矩阵法表示法,构建已知的图 + G->vexnum = 9; //该图的顶点数 + G->arcnum = 12; //该图的弧数 + for (int i = 0; i < G->vexnum; i++) { + for (int j = 0; j < G->vexnum; j++) { + G->arcs[i][j].adj = INFINITY; //初始化邻接矩阵,INFINITY表示不相邻 + } + }//初始化图 + //相邻的顶点赋值 + Add(G, 0, 1); Add(G, 0, 2); Add(G, 0, 3); Add(G, 0, 6); + Add(G, 1, 2); Add(G, 3, 4); Add(G, 3, 5); Add(G, 4, 5); + Add(G, 5, 7); Add(G, 6, 7); Add(G, 6, 8); Add(G, 7, 8); + //表示0-1,0-2.。。。。。相邻 + return OK; +} + +Status Add(Graph*G, int x, int y) { + //构造邻接矩阵,相邻赋值为1 + if (x >= MAX_VERTEX_NUM || y >= MAX_VERTEX_NUM) { + return ERROR;//当超出了图的范围,就返回ERROR + } + G->arcs[x][y].adj = G->arcs[y][x].adj = 1;//无向图两边邻接都为1,且当两个连结时,给弧赋值 + return OK; +} +/*-------------图的生成------------------*/ + +typedef int ElemType; + +#define MAXQSIZE 100 //最大的容量 + +typedef struct QNode { + ElemType data; + struct QNode *priou;//节点的上一个节点 + struct QNode *next;//节点的下一个节点 +}QNode, LinkList, *QueuePtr; + +typedef struct { + QueuePtr front;//前面的 + QueuePtr rear;//后面的 +}LinkQueue; + +Status InitQueue(LinkQueue *Q) { + //队列的初始化 + Q->front = Q->rear = (QueuePtr)malloc(MAXQSIZE * sizeof(QNode)); + if (!(Q->front)) { + return ERROR; + } + Q->front->next = Q->rear->next = NULL;//使队列为空 + return OK; +} + +Status EnQueue(LinkQueue *Q, ElemType e) { + //入队列的操作 + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) { + return ERROR; + } + p->data = e;//储存到队列里的数据 + p->next = NULL;//下一个节点为空 + p->priou = Q->front; + Q->rear->next = p; + Q->rear = p; + return OK; +} + +Status DeQueue(LinkQueue *Q, ElemType *e) { + //出队列操作 + if (QueueEmpty(*Q)) { + //队列为空时停止操作并返回ERROR + return ERROR; + } + Q->front = Q->front->next;//头结点指向它的下一个节点 + *e = Q->front->data; + return OK; +} + +typedef enum { + false, + true +}bool; + +bool QueueEmpty(LinkQueue Q) { + //判断这是不是一个空队列 + if (Q.front == Q.rear) { + return true;//头结点和后一个节点为同一个,那么这个队列为空 + } + return false;//否则队列不为空 +} + +int FirstAdjVex(Graph G, int i) { + //返回第一个邻接顶点,没有的话返回-1 + for (int k = 0; k < G.vexnum; k++) { + if (G.arcs[i][k].adj == 1) { + return k;//如果是邻接节点,返回这个邻接的节点 + } + } + return -1; +} + +int NextAdjVex(Graph G, int i, int j) { + //返回下一个邻接顶点,没有的话返回-1 + for (int k = j + 1; k < G.vexnum; k++) { + if (G.arcs[i][k].adj == 1) { + return k; + } + } + return -1; +} +/*----------------队列的操作------------*/ +#define FALSE 0 +#define TRUE 1 + +int visited[MAX_VERTEX_NUM]; + +void BFSTraverse(Graph G, int a, int b) { + //按广度优先的非递归遍历图G。使用辅助队列Q和访问标志数组visited. + LinkQueue Q; //辅助队列 + for (int v = 0; v < G.vexnum; ++v) { + visited[v] = FALSE; //初始化访问标志 + } + InitQueue(&Q); // 置空的辅助队列Q + EnQueue(&Q, a); //起点入列 + visited[a] = TRUE; + bool flag = false; + int u, w; + while (!QueueEmpty(Q)) //队列不为空时执行 + { + DeQueue(&Q, &u);//出队列操作 + for (w = FirstAdjVex(G, u); w >= 0; w = NextAdjVex(G, u, w)) + { + //依次将相邻顶点入列 + if (w == b) { + EnQueue(&Q, w); //入队列操作 + PrintFoot(Q, a); //输出路径 + flag = true; + break; + }//if + if (!visited[w]) { + //w为u的未访问的邻接顶点 + EnQueue(&Q, w); //入队列 + visited[w] = true; + }//if + }//for + if (flag) { + break; + }//if + }//while +}//BFSTraverse +/*---------图的遍历--------------*/ + +Status PrintFoot(LinkQueue Q, int start) { + //输出最短路径 + int foot[MAX_VERTEX_NUM];///最大顶点数 + int i; + QueuePtr p;//定义一个队列 + p = Q.rear;//p指向队尾节点 + for (i = 0; i < MAX_VERTEX_NUM; i++) { + foot[i] = -1; + }//队列的初始化 + foot[0] = p->data; + p = p->priou; + for (i = 1; p->data != start; i++) { + foot[i] = p->data; + p = p->priou; + } + foot[i] = p->data; + for (; i >= 0; i--) { + //倒序输出 + if (foot[i] >= 0) { + printf("%d ", foot[i] + 1); + } + } +} + +int main() +{ + Graph G; + CreateGraph(&G);//创建一个图图,并且初始化它1 + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + if (j != i) { + printf("%d<->%d:", i + 1, j + 1); + BFSTraverse(G, i, j);//广度遍历 + printf("\n"); + }//if + }//for j + }//for i + return 0; +} \ No newline at end of file diff --git "a/2017-1/zjc/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/2M~(VF$R1JTW~@OYDTV%32H.png" "b/2017-1/zjc/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/2M~(VF$R1JTW~@OYDTV%32H.png" new file mode 100644 index 00000000..2b1ba69e Binary files /dev/null and "b/2017-1/zjc/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/2M~(VF$R1JTW~@OYDTV%32H.png" differ diff --git "a/2017-1/zjc/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/3.12.c" "b/2017-1/zjc/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/3.12.c" new file mode 100644 index 00000000..fcaa6fe1 --- /dev/null +++ "b/2017-1/zjc/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/3.12.c" @@ -0,0 +1,308 @@ +#include +#include +#include +#define MAX 20 /*线性表中最多元素个数*/ +typedef int KeyType; +typedef char InfoType[10]; +typedef struct +{ + KeyType key; + InfoType data; +}RecType; + +void InsertSort(RecType R[], int n,int compare,int move) +{ + + int i, j, k; + RecType temp; + for (i = 1; i < n; i++) + { + temp = R[i]; + j = i - 1; + while (j >= 0 && temp.key < R[j].key&&compare++) + { + R[j + 1] = R[j]; + j--; + move++; + } + R[j + 1] = temp; + printf(" i=%d ", i); + for (k = 0; k < n; k++) + printf("%3d", R[k].key); + printf("\n"); + } +} + +void ShellSort(RecType R[], int n,int compare,int move) +{ + int i, j, d, k; + + RecType temp; + d = n / 2; + while (d > 0) + { + for (i = d; i < n; i++) + { + j = i - d; + while (j >= 0 && R[j].key > R[j + d].key&&compare++) + { + temp = R[j]; + R[j] = R[j + d]; + R[j + d] = temp; + j = j - d; + move++; + } + } + printf(" d=%d: ", d); + for (k = 0; k < n; k++) + printf("%3d", R[k].key); + printf("\n"); + d = d / 2; + } +} + +void BubbleSort(RecType R[], int n,int compare,int move) +{ + int i, j, k; + RecType temp; + for (i = 0; i < n-1 ; i++) + { + for (j = n - 1; j > i; j--) + if (R[j].key < R[j - 1].key&&compare++) + { + temp = R[j]; + R[j] = R[j - 1]; + R[j - 1] = temp; + move++; + } + printf(" i=%d ", i); + for (k = 0; k < n; k++) + printf("%3d", R[k].key); + printf("\n"); + } +} + +void QuickSort(RecType R[], int s, int t, int n,int compare,int move) +{ + int i = s, j = t, k; + RecType temp; + if (s < t) + { + temp = R[s]; + while (i != j) + { + while (j > i && R[j].key > temp.key&&compare++) + j--; + if (i < j) + { + R[i] = R[j]; + i++; + move++; + } + while (i < j && R[i].key < temp.key&&compare++) + i++; + if (i < j) + { + R[j] = R[i]; + j--; + move++; + } + } + R[i] = temp; + printf(" "); + for (k = 0; k < n; k++) + if (k == i) + printf(" [%d]", R[k].key); + else + printf("%4d", R[k].key); + printf("\n"); + QuickSort(R, s, i - 1, n,compare,move); + QuickSort(R, i + 1, t, n,compare,move); + } +} + +void SelectSort(RecType R[], int n,int compare,int move) +{ + int i, j, k, l; + RecType temp; + compare=move=0; + for (i = 0; i < n - 1; i++) + { + k = i; + for (j = i + 1; j < n; j++) + if (R[j].key < R[k].key&&compare++) + k = j; + if (k != i) + { + temp = R[i]; + R[i] = R[k]; + R[k] = temp; + move++; + } + printf(" i=%d ", i); + for (l = 0; l < n; l++) + printf("%3d", R[l].key); + printf("\n"); + } +} + +void DispHeap(RecType R[], int i, int n) /*以括号表示法输出建立的堆*/ +{ + if (i <= n) + printf("%d", R[i].key); + if (2 * i <= n) + + + { + printf("("); + DispHeap(R, 2 * i, n); + printf(","); + if (2 * i + 1 <= n) + DispHeap(R, 2 * i + 1, n); + printf(")"); + } +} +void Sift(RecType R[], int low, int high,int compare,int move) /*调整堆*/ +{ + int i = low, j = 2 * i; + RecType temp = R[i]; + while (j <= high) + { + if (j < high && R[j].key < R[j + 1].key&&compare++) + j++; + if (temp.key < R[j].key&&compare++) + { + R[i] = R[j]; + i = j; + j = 2 * i; + move++; + } + else + break; + } + R[i] = temp; +} +void HeapSort(RecType R[], int n,int compare,int move) /*对R[1]到R[n]元素实现堆排序*/ +{ + int i; + RecType temp; + for (i = n / 2; i >= 1; i--) + Sift(R, i, n,compare,move); + printf("初始堆:"); + DispHeap(R, 1, n); + printf("\n"); + for (i = n; i >= 2; i--) + { + printf("交换%d与%d,输出%d\n", R[i].key, R[1].key, R[1].key); + temp = R[1]; + R[1] = R[i]; + R[i] = temp; + Sift(R, 1, i - 1,compare,move); + printf("筛选调整得到堆:"); + DispHeap(R, 1, i - 1); + printf("\n"); + } +} + +void PrintArray(RecType R[], int n) +{ + int i; + for (i = 0; i < n; i++) + printf("%3d", R[i].key); + printf("\n"); +} + +void main() +{ + + int i, n = 10, cord; + RecType R[MAX], S[MAX]; + srand(time(0)); /*用系统时间作为随机数种子*/ + for (i = 0; i < n; i++) + S[i].key = 100 * rand() / RAND_MAX; + /*利用随机函数产生10个100以内的随机整数*/ + int compare=0,move=0; + printf("*****************************\n"); + printf("1-直接插入排序\n"); + printf("初始关键字:"); + PrintArray(S, n); + for (i = 0; i < n; i++) + R[i].key = S[i].key; + InsertSort(R, n,compare,move); + printf("最后结果 :"); + PrintArray(R, n); + printf("总比较次数:%d\n",compare); + printf("总移动次数:%d\n",move); + printf("两者之和:%d\n\n",move+compare); + for (i = 0; i < n; i++) + S[i].key = 100 * rand() / RAND_MAX; + compare=move=0; + printf("2-希尔排序\n"); + printf("初始关键字:"); + PrintArray(S, n); + for (i = 0; i < n; i++) + R[i].key = S[i].key; + ShellSort(R, n,compare,move); + + printf("最后结果 :"); + PrintArray(R, n); + printf("总比较次数:%d\n",compare); + printf("总移动次数:%d\n",move); + printf("两者之和:%d\n\n",move+compare); + for (i = 0; i < n; i++) + S[i].key = 100 * rand() / RAND_MAX; + compare=move=0 ; + printf("3-冒泡排序\n"); + printf("初始关键字:"); + PrintArray(S, n); + for (i = 0; i < n; i++) + R[i].key = S[i].key; + BubbleSort(R, n,compare,move); + printf("最后结果 :"); + PrintArray(R, n); + printf("总比较次数:%d\n",compare); + printf("总移动次数:%d\n",move); + printf("两者之和:%d\n\n",move+compare); + for (i = 0; i < n; i++) + S[i].key = 100 * rand() / RAND_MAX; + compare=move=0; + printf("4-快速排序\n"); + printf("初始关键字:"); + PrintArray(S, n); + for (i = 0; i < n; i++) + R[i].key = S[i].key; + QuickSort(R, 0, n - 1, n,compare,move); + printf("最后结果 :"); + PrintArray(R, n); + printf("总比较次数:%d\n",compare); + printf("总移动次数:%d\n",move); + printf("两者之和:%d\n\n",move+compare); + for (i = 0; i < n; i++) + S[i].key = 100 * rand() / RAND_MAX; + compare=move=0 ; + printf("5-简单选择排序\n"); + printf("初始关键字:"); + PrintArray(S, n); + for (i = 0; i < n; i++) + R[i].key = S[i].key; + SelectSort(R, n,compare,move); + printf("最后结果 :"); + PrintArray(R, n); + printf("总比较次数:%d\n",compare); + printf("总移动次数:%d\n",move); + printf("两者之和:%d\n\n",move+compare); + for (i = 0; i < n; i++) + S[i].key = 100 * rand() / RAND_MAX; + compare=move=0; + printf("6-堆排序\n"); + printf("初始关键字:"); + PrintArray(S, n); + for (i = 1; i <= n; i++) + R[i].key = S[i - 1].key; + HeapSort(R, n,compare,move); + printf("最后结果 :"); + PrintArray(R, n); + printf("总比较次数:%d\n",compare); + printf("总移动次数:%d\n",move); + printf("两者之和:%d\n\n",move+compare); +} diff --git "a/2017-1/zjc/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/9(UIHP$2FGQFJX3`941))L5.png" "b/2017-1/zjc/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/9(UIHP$2FGQFJX3`941))L5.png" new file mode 100644 index 00000000..ccfa7f17 Binary files /dev/null and "b/2017-1/zjc/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/9(UIHP$2FGQFJX3`941))L5.png" differ diff --git "a/2017-1/zjc/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/main.c" "b/2017-1/zjc/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/main.c" new file mode 100644 index 00000000..e8cd7987 --- /dev/null +++ "b/2017-1/zjc/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/main.c" @@ -0,0 +1,206 @@ +锘#include +#include +#include + +typedef enum { + FALSE, + TRUE +}bool; + +typedef enum { + ERROR, + OK +}Status; + +typedef struct BiTNode { + int value; + struct BiTNode *lchild, *rchild; +}*BiTree,BiTNode; + +bool LT(int a, int b)//LessThan灏忎簬聽聽 +{ + if (avalue =data; + p->lchild = p->rchild = NULL; + return TRUE; + } + if (data==p->value) // BST涓笉鑳芥湁鐩哥瓑鐨勫 + { + return FALSE; + }if (data< p->value) // 閫掑綊 + { + return InsertBST(p->lchild,data); + } + return InsertBST(p->rchild,data); // 閫掑綊 + return TRUE; +} + +Status createBST(BiTree *T, int a[], int n) + { + T = NULL; + int i; + for (i = 0; i < n; i ) + { + InsertBST(T, a[i]); + } + return OK; + } +void PreOrderTraverse(BiTree root)//鍏堝簭閬嶅巻 +{ + if (root) + { + printf("%d ", root->value); + PreOrderTraverse(root->lchild); + PreOrderTraverse(root->rchild); + } +} +void InOrderTraverse(BiTree root)//涓簭閬嶅巻 +{ + if (root) + { + InOrderTraverse(root->lchild); + printf("%d ", root->value); + InOrderTraverse(root->rchild); + } +} +void PostOrderTraverse(BiTree root)//鍚庡簭閬嶅巻 +{ + if (root) + { + PostOrderTraverse(root->lchild); + PostOrderTraverse(root->rchild); + printf("%d ", root->value); + } +} +bool SearchBST(BiTree root, int data) + +{ + if (!root) + { + return FALSE; + } + else if(data== root->value)//鏌ユ壘鎴愬姛 + { + return TRUE; + } + else if(data< root->value) //鍦ㄥ乏瀛愭爲缁х画鏌ユ壘 + return SearchBST(root->lchild, data); + else if(data> root->value) //鍦ㄥ彸瀛愭爲缁х画鏌ユ壘 + return SearchBST(root->rchild, data); + return TRUE; +} + + Status Delete(BiTree *p) + { + + /* 浠庝簩鍙夋帓搴忔爲涓垹闄よ妭鐐筽锛 骞堕噸鎺ュ畠鐨勫乏鎴栧彸瀛愭爲 */ + BiTree q, s; + if (!(*p)->lchild && !(*p)->rchild) /* p涓哄彾瀛愯妭鐐 */ + { + *p = NULL; + } + else if (!(*p)->lchild) /* 宸﹀瓙鏍戜负绌猴紝閲嶆帴鍙冲瓙鏍 */ + { + q = *p; + *p = (*p)->rchild; + free(q); + } + else if (!(*p)->rchild) /* 鍙冲瓙鏍戜负绌猴紝閲嶆帴宸﹀瓙鏍 */ + { + q = *p; + *p = (*p)->lchild; + free(q); + } + else /* 宸﹀彸瀛愭爲鍧囦笉涓虹┖ */ + { + q = *p; + s = (*p)->lchild; + while (s->rchild) /* 杞乏锛岀劧鍚庡悜鍙宠蛋鍒板敖澶*/ + { + q = s; + s = s->rchild; + } + (*p)->value= s->value; + if (q != *p) /* 鍒ゆ柇鏄惁鎵ц涓婅堪while寰幆 */ + { + q->rchild = s->lchild; /* 鎵ц涓婅堪while寰幆锛岄噸鎺ュ彸瀛愭爲 */ + } + else { + q->lchild = s->lchild; /* 鏈墽琛屼笂杩皐hile寰幆锛岄噸鎺ュ乏瀛愭爲 */ + } + free(s); + } + return OK; + } + + Status DeleteBST(BiTree *T, int key) + { + /* 鑻ヤ簩鍙夋帓搴忔爲T涓瓨鍦ㄥ叧閿瓧绛変簬key鐨勬暟鎹厓绱犳椂锛屽垯鍒犻櫎璇ユ暟鎹厓绱犺妭鐐 */ + /* 骞惰繑鍥濼RUE锛涘惁鍒欒繑鍥濬ALSE */ + if (!(*T)) { + return ERROR; /* 涓嶅瓨鍦ㄥ叧閿瓧绛変簬key鐨勬暟鎹厓绱 */ + } + else + { + if (key == (*T)->value) { + Delete(T); + } + else if (key < (*T)->value) { + return DeleteBST(&(*T)->lchild, key); + } + else { + return DeleteBST(&(*T)->rchild, key); + } + } + } +int main() { + int a[12] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + int b[5] = { 13, 8, 5, 20, 6 }; + int n = 10; + int i = 0;//loop counter + //BSTtree + BiTree T; + printf("BiTtree:\n"); + if (createBST(T, a, n)) { + printf("鍒涘缓浜嗕竴妫垫爲\n"); + PreOrderTraverse(T); + printf("\n"); + } + else { + printf("鍒涘缓鏍戝け璐"); + } + for (i = 0; i < 5; i ++ ) + { + if (SearchBST(T, b[i])) { + printf("鍦ㄦ暟涓壘鍒 %d ! 鐜板湪鎶婅繖涓暟浠庢爲涓垹闄ゃ俓n", b[i]); + printf("鍒犻櫎浠ュ悗: "); + DeleteBST(&T, b[i]); + PreOrderTraverse(T); + printf("\n\n"); + } + else { + + printf("%d 娌℃湁鎵惧埌:( 鐜板湪鎶婅繖涓暟鍔犲叆鏍戜腑銆俓n", b[i]); + if (InsertBST(T, b[i])) { + printf("鎻掑叆鍚: "); + PreOrderTraverse(T); + printf("\n\n"); + } + else { + printf("鎻掑叆澶辫触"); + } + } + } + return 0; +} \ No newline at end of file