From 5bf1e2d92e1c528a289323dfe4aba16c5a6cb193 Mon Sep 17 00:00:00 2001 From: HuanWoWeiLan <962871150@qq.com> Date: Wed, 17 May 2017 22:48:29 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=AC=AC=E4=B8=83=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...203\346\254\241\344\275\234\344\270\232.c" | 241 ++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 "2017-1/HuanWoWeiLan/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.c" diff --git "a/2017-1/HuanWoWeiLan/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.c" "b/2017-1/HuanWoWeiLan/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.c" new file mode 100644 index 00000000..bca33694 --- /dev/null +++ "b/2017-1/HuanWoWeiLan/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.c" @@ -0,0 +1,241 @@ +#include +#include + +//图的数组存储表示 +#define INFINITY 99999 //最大值无穷 +#define MAX_VERTEX_NUM 20 //最大顶点个数 +int visited[MAX_VERTEX_NUM ]; //访问标志数组,初始值为FALSE(0),一旦某个顶点被访问,则令其值为TRUE(1). +typedef struct ArcCell //弧的定义 +{ + int adj; //用1和0表示是否相邻 +}ArcCell,AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; +typedef struct //图的定义 +{ + AdjMatrix arcs; //邻接矩阵 + int vexnum,arcnum; //顶点数和弧数 +}Graph; + +//-------------队列------------ +#define MAXQSIZE 100 +typedef struct QNode +{ + int data; + struct QNode *prious; + struct QNode *next; +}QNode,LinkList,*QueuePtr; + +typedef struct +{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; + + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef enum +{ + false, + true +}bool; + +//图的基本操作 + +//顶点赋值 +Status Add(Graph *G, int x, int y) +{ + if(x >= MAX_VERTEX_NUM || y >= MAX_VERTEX_NUM) + { + return ERROR; + } + G -> arcs[x][y].adj = G ->arcs[y][x].adj = 1; + return OK; +} +//构建图(用数组表示法) +Status CreateGraph(Graph *G) +{ + int i ,j; + G -> vexnum = 9;//顶点数 + G -> arcnum = 12;//弧数 + + for(i = 0; i < G -> vexnum; i++) + { + for(j = 0; j < G ->arcnum; j++) + { + G -> arcs[i][j].adj = 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); + + return OK; +} +//返回第一个邻接顶点,没有的话返回-1 +int FirstAdjVex(Graph G, int i) +{ + int k; + for (k = 0; k < G.vexnum; k++) + { + if (G.arcs[i][k].adj == 1) //邻接矩阵同一行中为1的点都是它的邻接点,从0开始遍历,第一个为1的就是邻接点 + { + return k; + } + } + return -1; +} +//返回下一个邻接顶点,没有的话返回-1 +int NextAdjVex(Graph G, int i, int j) +{ + int k; + for (k = j + 1; k < G.vexnum; k++) + { + if (G.arcs[i][k].adj == 1)//k从j+1开始,下一个为1的就是它的下一个邻接点 + { + return k; + } + } + return -1; +} +//广度优先求路径 +void ShortestPath(Graph G,int a,int b) +{ + int u,v,w; + bool flag = false;//用flag来进行退出while循环的判断,若为true则退出while + LinkQueue Q; + for(v = 0; v < G.vexnum; ++v) + { + visited[v] = false; //先初始化访问标志数组为FALSE + } + InitQueue(&Q);//初始化一个队列,来存储已被访问的路径长度为1,2,。。。的顶点,即存储最短路径的顶点 + EnQueue(&Q,a);//将a进入队列 + visited[a] = true;//访问了a,则将它赋值为TRUE,表示已经被访问 + while (!QueueEmpty(Q))//队列不为空 + { + DeQueue(&Q,&u);//此函数出队列时,仅移动队头指针,而不将队头结点从链表中删除 + for(w = FirstAdjVex(G,u);w >=0;w = NextAdjVex(G, u, w)) //w为u的邻接点,直到遍历到b时for循环停止 + { + if(w == b)//若w=b,则说明最小路径已经找到 + { + EnQueue(&Q,w);//把最后一个结点进入队列 + PrintFoot(Q,a); + flag = true; + } + if(!visited[w])//若u的邻接点没有被访问 + { + EnQueue(&Q,w); + visited[w] = true; + } + } + + if(flag) + { + break;//跳出while循环 + } + } +} + +//输出路径 +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数组 + } + foot[0] = p->data;//路径的最后一个 + p = p->prious; + for(i = 1;p->data != start; i++) + { + foot[i] = p->data; + p = p->prious; + } + foot[i] = start;//foot[i] = p->data; + for(;i >= 0; i--) + { + if(foot[i] >= 0) + printf("%d ",foot[i] + 1);//输出路径 + } +} + +//队列基本操作 +//初始化队列 +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; +} +//判断是否为空队列 +bool QueueEmpty(LinkQueue Q) +{ + if (Q.front == Q.rear) + { + return true; + } + return false; +} +//入列 +Status EnQueue(LinkQueue *Q,int e) +{ + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode));//令其priou域的指针指向刚刚出队列的结点,即当前的队头指针所指结点; + if(!p) + { + return ERROR; + } + p->data = e; + p->next = NULL; + p->prious = Q->front;//指向当前的队头指针所指结点 + Q->rear->next = p; + Q->rear = p; + return OK; +} +//出列 +Status DeQueue(LinkQueue *Q, int *e) +{ + if (QueueEmpty(*Q)) + { + return ERROR; + } + Q->front = Q->front->next; + *e = Q->front->data; + return OK; +} + + + + + +int main() +{ + int i,j; + Graph h ; + CreateGraph(&h);//构建一个无向图,并用邻接矩阵初始化图 + + for(i = 0;i < 9; i++) + { + for(j = 0;j < 9;j++) + { + if(i != j) + { + printf("%d -> %d :",i+1,j+1); + ShortestPath(h,i,j);//寻找最短路径 + printf("\n"); + } + } + } + return 0; +} + From 9d2d88bfea5267650088a894a52de699fffbebf0 Mon Sep 17 00:00:00 2001 From: HuanWoWeiLan <962871150@qq.com> Date: Wed, 24 May 2017 22:21:43 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=AC=AC=E4=B9=9D=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A=E2=80=A6=E2=80=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\346\254\241\344\275\234\344\270\232.cpp" | 170 ++++++++++++++++++ 1 file changed, 170 insertions(+) create mode 100644 "2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232.cpp" diff --git "a/2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232.cpp" "b/2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232.cpp" new file mode 100644 index 00000000..c03e47bc --- /dev/null +++ "b/2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232.cpp" @@ -0,0 +1,170 @@ +#include +#include +typedef struct node +{ + int key; + struct node *lchild, *rchild; +}BSTNode, *BSTree; + +//插入 +int InsertBST(BSTree *bst, int k) +{ + BSTree r, s, pre; + r = (BSTree)malloc(sizeof(BSTNode)); + r->key = k; + r->lchild = NULL; + r->rchild = NULL; + if(*bst == NULL) + { + *bst = r; + return 1; + } + pre = NULL; + s = *bst; + while(s) + { + if(k == s->key) + return 0; + else if(k < s->key) + { + pre = s; + s = s->lchild; + } + else + { + pre = s; + s = s->rchild; + } + } + if(k < pre->key) + pre->lchild = r; + else + pre->rchild = r; + return 1; +} +BSTree DeleteBST(BSTree T, int k) +{ + BSTree p, q, s, f; + p = T; + f = NULL; + while (p) + { + if(p->key == k) + break; + f = p; + if(p->key > k) + p =p->lchild; + else if(p->key< k) + p =p->rchild; + + } + + q = p; + + if ((p->lchild)&& (p->rchild)) + { + s =p->lchild; + while(s->rchild) + { + q =s; + s =s->rchild; + } + p->key =s->key; + if (p != q) + + q->rchild= s->lchild; + + else if (p == q) + q->lchild= s->lchild; + free(s); + return T; + } + else if (!p->lchild) + + p =p->rchild; + + else if (!p->rchild) + p =p->lchild; + if (!f) + + T = p; + + else if (q == f->lchild) + f->lchild= p; + else + f->rchild= p; + + free(q); + return T; + +} + +void CreateBST(BSTree *bst) +{ + int key; + *bst = NULL; + scanf("%d", &key); + while(key != -1) + { + InsertBST(bst, key); + scanf("%d", &key); + } +} +/*打印二叉树:中序遍历*/ +void InOrder(BSTree root) +{ + if(root != NULL) + { + InOrder(root->lchild); + printf( "%d ", root->key); + InOrder(root->rchild); + } +} + +/*搜索*/ +BSTree SearchBST(BSTree bst, int key) +{ + BSTree q; + q = bst; + //递归 + while(q) + { + if(q->key == key) + return q; + if(q->key > key) + q=q->lchild; + else + q=q->rchild; + } + return NULL; //查找失败 +} + +int main() +{ + BSTree T; + int tag = 1; + int m, n; + printf("建立二叉排序树,请输入序列以-1结束:"); + CreateBST(&T); + printf("中序遍历二叉树,序列为:"); + InOrder(T); + while(tag != 0) + { + printf("请输入你要查找的元素:"); + scanf("%d", &n); + if(SearchBST(T, n) == NULL) + { + printf("抱歉查找失败!"); + InsertBST(&T, n); + } + else + { + printf("查找成功!查找的数字为%d\n", SearchBST(T,n)->key); + DeleteBST(SearchBST(T,n),n); + } + InOrder(T); + printf("\n是否继续查找 是 :请按 1 否则按 0:"); + scanf("%d", &tag); + } + return 0; +} \ No newline at end of file From 6f332e55835f949dbd4ebe775598aedc5000d328 Mon Sep 17 00:00:00 2001 From: HuanWoWeiLan <962871150@qq.com> Date: Wed, 24 May 2017 22:28:45 +0800 Subject: [PATCH 3/3] =?UTF-8?q?Rename=20=E7=AC=AC=E4=B9=9D=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A.cpp=20to=20=E7=AC=AC=E5=85=AB=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A.cpp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 棰濈鍏浣滀笟鈥︹﹀啓閿欎簡鈥︹ --- ...3\346\254\241\344\275\234\344\270\232.cpp" | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) rename "2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232.cpp" => "2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232.cpp" (81%) diff --git "a/2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232.cpp" "b/2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232.cpp" similarity index 81% rename from "2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232.cpp" rename to "2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232.cpp" index c03e47bc..8bc74863 100644 --- "a/2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232.cpp" +++ "b/2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232.cpp" @@ -6,7 +6,7 @@ typedef struct node struct node *lchild, *rchild; }BSTNode, *BSTree; -//插入 +//鎻掑叆 int InsertBST(BSTree *bst, int k) { BSTree r, s, pre; @@ -110,7 +110,7 @@ void CreateBST(BSTree *bst) scanf("%d", &key); } } -/*打印二叉树:中序遍历*/ +/*鎵撳嵃浜屽弶鏍戯細涓簭閬嶅巻*/ void InOrder(BSTree root) { if(root != NULL) @@ -121,12 +121,12 @@ void InOrder(BSTree root) } } -/*搜索*/ +/*鎼滅储*/ BSTree SearchBST(BSTree bst, int key) { BSTree q; q = bst; - //递归 + //閫掑綊 while(q) { if(q->key == key) @@ -136,7 +136,7 @@ BSTree SearchBST(BSTree bst, int key) else q=q->rchild; } - return NULL; //查找失败 + return NULL; //鏌ユ壘澶辫触 } int main() @@ -144,27 +144,27 @@ int main() BSTree T; int tag = 1; int m, n; - printf("建立二叉排序树,请输入序列以-1结束:"); + printf("寤虹珛浜屽弶鎺掑簭鏍戯紝璇疯緭鍏ュ簭鍒椾互-1缁撴潫锛"); CreateBST(&T); - printf("中序遍历二叉树,序列为:"); + printf("涓簭閬嶅巻浜屽弶鏍戯紝搴忓垪涓猴細"); InOrder(T); while(tag != 0) { - printf("请输入你要查找的元素:"); + printf("璇疯緭鍏ヤ綘瑕佹煡鎵剧殑鍏冪礌:"); scanf("%d", &n); if(SearchBST(T, n) == NULL) { - printf("抱歉查找失败!"); + printf("鎶辨瓑鏌ユ壘澶辫触!"); InsertBST(&T, n); } else { - printf("查找成功!查找的数字为%d\n", SearchBST(T,n)->key); + printf("鏌ユ壘鎴愬姛锛佹煡鎵剧殑鏁板瓧涓%d\n", SearchBST(T,n)->key); DeleteBST(SearchBST(T,n),n); } InOrder(T); - printf("\n是否继续查找 是 :请按 1 否则按 0:"); + printf("\n鏄惁缁х画鏌ユ壘 鏄 锛氳鎸 1 鍚﹀垯鎸 0:"); scanf("%d", &tag); } return 0; -} \ No newline at end of file +}