diff --git a/.gitignore b/.gitignore index 1332d909..91072b79 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +*.sln +*.vcxproj +*.vcxproj.filters ## Ignore Visual Studio temporary files, build results, and ## files generated by popular Visual Studio add-ons. ## diff --git "a/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/LinkList.c" "b/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/LinkList.c" new file mode 100644 index 00000000..6369443f --- /dev/null +++ "b/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/LinkList.c" @@ -0,0 +1,69 @@ +#include "LinkList.h" + +/* 链表的遍历 */ +void TraverseList(LinkList p) { + LinkList l = p; + l = l->next; + while ( l != p) { + printf("%d ",l->data); + l = l->next; + } + printf("\n"); +} + +/* 创建链表 */ +LinkList CreatList( int n) { + LNode *L,* p; + int i; + L = (LinkList)malloc(sizeof(LNode)); + L->next = L; + L->data = n; + if (!L) + exit(1); + for (i = 0; i < n ; i++) { + p = (LinkList)malloc(sizeof(LNode)); + if (!p) + exit(1); + p->data = i; + p->next = L->next; + L->next = p; + } + return L; +} + +/*初始化链表*/ +void InitList(LinkList a) { + LinkList p = a; + for (; p->next != a; p = p->next) { + p->data = 0; + + } +} + +/* 拆分链表 */ +void SeparateList(LinkList A, LinkList b, LinkList c) { +//算法分析:算法的的时间复杂度为 O(n),空间复杂度为O(1), +//在保证算法的正确性的前提下,使用了最少的时间复杂度和最少的空间复杂度 + LinkList p = A,p1 = b,p2 = c; + InitList(b); + InitList(c); + p = p->next; + p1 = p1->next; + p2 = p2->next; + while ( p != A) { + p1->data = p->data; + b->data++; + p1 = p1->next; + p = p->next; + if (p != A) { + p2->data = p->data; + c->data++; + p2 = p2->next; + p = p->next; + } + } + return; +} + + + diff --git "a/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/LinkList.h" "b/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/LinkList.h" new file mode 100644 index 00000000..838452cd --- /dev/null +++ "b/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/LinkList.h" @@ -0,0 +1,30 @@ +#pragma once +#ifndef LINKLIST_H +#define LINKLIST_H +#include +#include +#define length 10 + +typedef struct LNode { + int data; + struct LNode *next; +}LNode, *LinkList; + +typedef enum { + true, + false +}bool; + +/* 链表的遍历 */ +void TraverseList(LNode * p); + +/* 创建链表 */ +LinkList CreatList(int n); + +/* 拆分链表 */ +void SeparateList(LinkList A, LinkList b, LinkList c); + +/*初始化链表*/ +void InitList(LinkList); + +#endif \ No newline at end of file diff --git "a/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main_LinkList.c" "b/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main_LinkList.c" new file mode 100644 index 00000000..14387f01 --- /dev/null +++ "b/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main_LinkList.c" @@ -0,0 +1,13 @@ +#include +#include "LinkList.h" +#define length 20 +int main() { + LinkList A = CreatList(length), + a1 = CreatList(length/2), + a2 = CreatList(length/2); + TraverseList(A); + SeparateList(A, a1, a2); + TraverseList(a1); + TraverseList(a2); + return 0; +} \ No newline at end of file diff --git "a/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main_LinkList.txt" "b/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main_LinkList.txt" new file mode 100644 index 00000000..aaed159d --- /dev/null +++ "b/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main_LinkList.txt" @@ -0,0 +1,3 @@ +19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 +19 17 15 13 11 9 7 5 3 1 +18 16 14 12 10 8 6 4 2 0 diff --git "a/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/test2-12.c" "b/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/test2-12.c" new file mode 100644 index 00000000..4be45822 --- /dev/null +++ "b/2017-1/Cloud/1.\347\272\277\346\200\247\350\241\250\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/test2-12.c" @@ -0,0 +1,87 @@ +#include +#include +#define length 10 + +typedef struct LNode{ + int data; + struct LNode *next ; +}LNode; +typedef struct LNode *LinkList; + +typedef enum { + true, + false +}bool; + +void travle(LinkList p); +void CreatList(LinkList L,int n); +void MergeList(LinkList La,LinkList Lb,LinkList Lc); + +void travle(LinkList p){ + + int i; + printf("有序单链表:"); + //p = L_->next + for( i = 0 ;i data); + printf("%d ",p->data ); + p = p->next ; + } + printf("\n"); +} + +void CreatList(LinkList L,int n){ + LNode *p; + int i; + L = (LinkList)malloc( sizeof(LNode) ); + L->next = NULL;//建立单链表的头结点 + if(L) + printf("头节点创建成功!\n"); + for(i = n; i > 0;--i){ + p = (LinkList)malloc(sizeof(LNode)); + if(p) + printf("成功分配一个节点空间,请输入一个整数:"); + scanf("%d",& p->data ); + p->next = L->next ;//第一次连入时L->next,第一次插入的节点在整个链表的最后。? + L->next = p;//两步把一个新生成的结点连上去了 + } + travle(L); + //printf("生成一个有序单链表"); + //for(int i ;i next ; + pb = Lb->next ; + Lc = pc = La; + while(pa && pb){ + if( pa->data <= pb->data ){ + pc->next = pa; + pc = pa; + pa = pa->next ; + } + else{ + pc->next = pb; + pc = pb; + pb = pc->next; + } + } + pc->next = pa ? pa : pb; + free(Lb); +} + +int main(){ + LNode list_a,list_b,list_c; + + CreatList(&list_a,length); + CreatList(&list_b,length); + MergeList(&list_a,&list_b,&list_c); + travle(&list_c); + return 0; +} + + diff --git "a/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/Queue.c" "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/Queue.c" new file mode 100644 index 00000000..f7d87c15 --- /dev/null +++ "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/Queue.c" @@ -0,0 +1,106 @@ +#pragma once +#include "Queue.h" + +/*初始化队列*/ +Status InitQueue(LinkQueue *q) { + q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!q->front) { + exit(OVERFLOW); + } + q->front->next = NULL; + q->rear->next = NULL; + return OK; +} + +/*添加一个元素,用e传递其值*/ +Status EnQueue(LinkQueue *q, QElemType e) { + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) { + return OVERFLOW; + } + p->data = e; + p->next = NULL; + q->rear->next = p; + q->rear = p; + return OK; +} + +/*检查队列是否为空*/ +bool QueueEmpty(LinkQueue q) { + if (q.front == q.rear) { + return true; + } + else { + return false; + } +} + +/*删除队头元素, 用e返回其值*/ +Status DeQueue(LinkQueue *q, QElemType *e) { + if (QueueEmpty(*q)) { + return ERROR; + } + QueuePtr p; + p = q->front->next;//q->front为头节点,q->front->next为队头元素 + e = p->data; + q->front->next = p->next; + if (q->rear == p)//如果删除的是队列的最后一个元素,需要修改尾指针 + q->rear = q->front; + return OK; +} + +/*遍历队列*/ +Status TraverseQueue(LinkQueue q) { + QueuePtr p; + if (QueueEmpty(q)) { + printf("队列为空\n"); + return OK; + } + p = q.front->next; + while (p != q.rear) { + printf("%d ", p->data); + p = p->next; + } + printf("%d", p->data); + printf("\n"); + return OK; +} + +/*销毁队列*/ +Status DestroyQueue(LinkQueue *q) { + while (q->front) { + q->rear = q->front->next; + free(q->front); + q->front = q->rear; + } + return OK; +} + +/*将队列q清空为空队列*/ +Status ClearQueue(LinkQueue *q) { + if (!QueueEmpty(*q)) { + q->rear = q->front; + return OK; + } + return false; +} + +/*返回队列的长度*/ +int QueueLength(LinkQueue *q) { + int l; + QueuePtr lq; + for (l = 0, lq = q->front->next; lq != q->rear; l++) { + lq = lq->next; + } + return l + 1; +} + +/*返回队列的队头元素*/ +Status GetHead(LinkQueue *q, QElemType *e) { + if (!QueueEmpty(*q)) { + *e = q->front->next->data; + return OK; + } + return false; +} \ No newline at end of file diff --git "a/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/Queue.h" "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/Queue.h" new file mode 100644 index 00000000..736f420e --- /dev/null +++ "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/Queue.h" @@ -0,0 +1,43 @@ +#ifndef QUEUE_H +#define QUEUE_H +#include +#include +#include +typedef int QElemType; +typedef enum { + false, + true, +}bool; +typedef enum { + OK, + ERROR, + OVERFLOW +} Status; +typedef struct QNode { + QElemType data; + struct QNode *next; +}QNode, *QueuePtr; +typedef struct _LinkQueue { + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +/*初始化队列*/ +Status InitQueue(LinkQueue *q); +/*添加一个结点,用e传递其值*/ +Status EnQueue(LinkQueue *q, QElemType e); +/*检查队列是否为空*/ +bool QueueEmpty(LinkQueue q); +/*删除一个结点, 用e返回其值*/ +Status DeQueue(LinkQueue *q, QElemType *e); +/*遍历队列*/ +Status TraverseQueue(LinkQueue q); +/*销毁队列*/ +Status DestroyQueue(LinkQueue *q); +/*将队列q清空为空队列*/ +Status ClearQueue(LinkQueue *q); +/*返回队列的长度*/ +int QueueLength(LinkQueue *q); +/*返回队列的队头元素*/ +Status GetHead(LinkQueue *q, QElemType *e); +#endif \ No newline at end of file diff --git "a/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main.c" "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main.c" new file mode 100644 index 00000000..d1a4ec5e --- /dev/null +++ "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main.c" @@ -0,0 +1,36 @@ +#include +#include "Queue.h" +int main() { + int _queueLen; + QElemType _e; + LinkQueue lk; + printf("队列初始化...\n"); + InitQueue(&lk); + printf("遍历队列:"); + TraverseQueue(lk); + printf("\n插入新的元素 2\n遍历队列:"); + EnQueue(&lk, 2); + TraverseQueue(lk); + printf("\n插入新的元素 4\n遍历队列:"); + EnQueue(&lk, 4); + TraverseQueue(lk); + printf("\n插入新的元素 6\n遍历队列:"); + EnQueue(&lk, 6); + TraverseQueue(lk); + + GetHead(&lk, &_e); + _queueLen = QueueLength(&lk); + printf("\n*当前头节点为 %d ,队列长度为 %d \n", _e, _queueLen); + DeQueue(&lk, &_e); + printf("\n删除队头元素 %d \n遍历队列:", _e); + TraverseQueue(lk); + + GetHead(&lk, &_e); + _queueLen = QueueLength(&lk); + printf("\n*当前头节点为 %d ,队列长度为 %d \n", _e, _queueLen); + ClearQueue(&lk); + printf("\n清空队列...\n遍历队列:"); + TraverseQueue(lk); + DestroyQueue(&lk); + return 0; +} \ No newline at end of file diff --git "a/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main.png" "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main.png" new file mode 100644 index 00000000..cef62f12 Binary files /dev/null and "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main.png" differ diff --git "a/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/tesr3-1_\346\225\260\345\210\266\350\275\254\346\215\242.c" "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/tesr3-1_\346\225\260\345\210\266\350\275\254\346\215\242.c" new file mode 100644 index 00000000..0d1ea4b7 --- /dev/null +++ "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/tesr3-1_\346\225\260\345\210\266\350\275\254\346\215\242.c" @@ -0,0 +1,109 @@ +#include +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +#define SElemType int + +typedef struct _Sqstack +{ + SElemType *base; + SElemType *top; + int Stacksize; +}Sqstack; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum{ + true, + false +}bool; + +Status InitStack(Sqstack *s); +void Push(Sqstack* ,SElemType ); +Status Pop(Sqstack* , SElemType* ); +void conversion (Sqstack , int ); +bool StackEmpty(Sqstack* ); + + +Status InitStack(Sqstack *s) +{ + s->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); + /*if(StackEmpty)*/ + if(!s->base){ + return OVERFLOW; + } + s->top = s->base; + s->Stacksize = STACK_INIT_SIZE; + return OK; +} + +void Push(Sqstack *s, SElemType e) +{ + //e新插入的栈顶元素 + if(s->top - s->base >= s->Stacksize){ + s->base = (SElemType *)realloc(s->base ,(STACK_INIT_SIZE + STACKINCREMENT)*sizeof(SElemType)); + while(!s->base) + { + s->base = (SElemType *)realloc(s->base ,(STACK_INIT_SIZE + STACKINCREMENT)*sizeof(SElemType)); + }//存储分配失败,进行重新分配 + s->top = s->base + s->Stacksize; + s->Stacksize += STACKINCREMENT; + } + *(s->top )= e; + s->top ++; +} + +bool StackEmpty(Sqstack *s) +{//如果栈为空,返回true 不空返回false + /*if(s->top == s->base) + return true; + else + return false;*///WWW1 + if(s){ + return s->base == s->top; + } + return true;//HDD +} + +Status Pop(Sqstack *s, SElemType* e) +{ + if(StackEmpty(s)) + return OVERFLOW; + *e = *(--s->top); + //s->top--;//WWW2--此处需要free吗? + return OK; +} + +void conversion (Sqstack _s, int _d) +{ + int e; + int number = rand()%1024; + //int number = 1348; + printf("生成的随机数:%d (10) = ",number); + while (number) { + Push(&_s, number % _d); + number = number/_d; + } + while (!StackEmpty(&_s)) { + Pop(&_s, &e); + printf ( "%d",e); + } + printf("(8)\n"); +} +int main() +{ + Sqstack S; + int d = 8; //进制数 + int input = rand()%1024; + srand(time(NULL)); + InitStack(&S); + conversion (S,d) ; + return 0; + +} diff --git "a/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/test3-2_\346\213\254\345\217\267\345\214\271\351\205\215.c" "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/test3-2_\346\213\254\345\217\267\345\214\271\351\205\215.c" new file mode 100644 index 00000000..16899fb1 --- /dev/null +++ "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/test3-2_\346\213\254\345\217\267\345\214\271\351\205\215.c" @@ -0,0 +1,146 @@ +#include +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +#define SElemType char +typedef struct _Sqstack +{ + SElemType *base; + SElemType *top; + int Stacksize; +}Sqstack; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum{ + true, + false +}bool; + +Status InitStack(Sqstack *s); +void Push(Sqstack* ,SElemType* ); +Status Pop(Sqstack* , SElemType* ); +bool StackEmpty(Sqstack* ); + + +Status InitStack(Sqstack *s) +{ + s->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); + /*if(StackEmpty)*/ + if(!s->base){ + return OVERFLOW; + } + s->top = s->base; + s->Stacksize = STACK_INIT_SIZE; + return OK; +} + +void Push(Sqstack *s, SElemType *e) +{ + //e新插入的栈顶元素 + if(s->top - s->base >= s->Stacksize){ + s->base = (SElemType *)realloc(s->base ,(STACK_INIT_SIZE + STACKINCREMENT)*sizeof(SElemType)); + while(!s->base) + { + s->base = (SElemType *)realloc(s->base ,(STACK_INIT_SIZE + STACKINCREMENT)*sizeof(SElemType)); + }//存储分配失败,进行重新分配 + s->top = s->base + s->Stacksize; + s->Stacksize += STACKINCREMENT; + } + *(s->top )= *e; + s->top ++; +} + +bool StackEmpty(Sqstack *s) +{//如果栈为空,返回true 不空返回false + /*if(s->top == s->base) + return true; + else + return false;*///WWW1 + if(s){ + return s->base == s->top; + } + return true;//HDD +} + +Status Pop(Sqstack *s, SElemType* e) +{ + if(StackEmpty(s)) + return OVERFLOW; + *e = *(s->top - 1); + s->top--;//WWW2--此处需要free吗? + return OK; +} + + +Status ParenthesisJudge(char f, char l){ + switch(f){ + case '(' :if(l == ')'){ + return OK; + } + else { + return ERROR; + } + break; + case '[' :if(l == ']'){ + return OK; + } + else { + return ERROR; + } + break; + case '{' :if(l == '}'){ + return OK; + } + else { + return ERROR; + } + break; + + default: + break; + } + + +} + +Status ParenthesisTest(Sqstack *_s){ + char now,_t; + char * t = &_t;//仅仅是为了迎合下文Pop的调用 + printf("请输入括号(输入‘!’以结束输入)....\n"); + scanf("%c",&now); + if(now != '!'){ + Push(_s,&now); + }//栈底元素 + scanf("%c",&now); + while(now != '!'){ + if( ParenthesisJudge( *(_s->top - 1),now) == OK ){ + Pop(_s,t); + } + else{ + Push(_s, &now); + } + scanf("%c",&now); + } + if(StackEmpty(_s)){ + return OK; + }else{ + return ERROR; + } +} +int main(){ + Sqstack S; + InitStack(&S); + if (!ParenthesisTest(&S)){ + printf("括号输入无误!"); + }else{ + printf("括号输入不匹配。"); + } + return 0; +} diff --git "a/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/test3-3_\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217.c" "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/test3-3_\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217.c" new file mode 100644 index 00000000..ba30c9f9 --- /dev/null +++ "b/2017-1/Cloud/2.\346\240\210\345\222\214\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/test3-3_\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217.c" @@ -0,0 +1,139 @@ +#include +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +#define SElemType char +typedef enum{ + true, + false +}bool; + +typedef struct _Sqstack +{ + SElemType *base; + SElemType *top; + int Stacksize; +}Sqstack; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +Status InitStack(Sqstack *s); +void Push(Sqstack* ,SElemType ); +Status Pop(Sqstack* , SElemType* ); +bool StackEmpty(Sqstack* ); +void ClearStack(Sqstack *s); +void DestroyStack(Sqstack *s); +void LineEdit(); + +Status InitStack(Sqstack *s) +{ + s->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); + /*if(StackEmpty)*/ + if(!s->base){ + return OVERFLOW; + } + s->top = s->base; + s->Stacksize = STACK_INIT_SIZE; + return OK; +} + +void Push(Sqstack *s, SElemType e) +{ + //e新插入的栈顶元素 + if(s->top - s->base >= s->Stacksize){ + s->base = (SElemType *)realloc(s->base ,(STACK_INIT_SIZE + STACKINCREMENT)*sizeof(SElemType)); + while(!s->base) + { + s->base = (SElemType *)realloc(s->base ,(STACK_INIT_SIZE + STACKINCREMENT)*sizeof(SElemType)); + }//存储分配失败,进行重新分配 + s->top = s->base + s->Stacksize; + s->Stacksize += STACKINCREMENT; + } + *(s->top )= e; + s->top ++; +} + +bool StackEmpty(Sqstack *s) +{//如果栈为空,返回true 不空返回false + /*if(s->top == s->base) + return true; + else + return false;*///WWW1 + if(s){ + return s->base == s->top; + } + return true;//HDD +} + +Status Pop(Sqstack *s, SElemType* e) +{ + if(StackEmpty(s)) + return OVERFLOW; + *e = *(s->top); + s->top--;//WWW2--此处需要free吗? + return OK; +} + +void ClearStack(Sqstack *s){ + s->top --; + for( ;s->top >= s->base ;s->top--) + { + *(s->top) = '\0'; + } + s->base = s->top ; +} + +void DestroyStack(Sqstack *s){ + + for(;s->top >= s->base ;s->top--) + { + free(s->top); + } +} + +void PrintStack(Sqstack s){ + for(;s.base <= s.top ;s.base ++ ){ + printf("%c",*s.base); + } + printf("\n"); +} + +void LineEdit(){ + Sqstack _s; + char ch,t; + InitStack(&_s); + ch = getchar(); + while(ch != EOF){ + while(ch != EOF && ch != '\n'){ + switch(ch){ + case '#':Pop(&_s,&t); + break; + case '@':ClearStack( &_s); + break; + default:Push(&_s, ch); + }//endswitch + ch = getchar(); + } + PrintStack(_s); + ClearStack(&_s); + //if(ch != '\n'){ + // ch = getchar(); + //}// + if(ch != EOF){ + ch = getchar(); + }//如何才能使ch == EOF? + } + DestroyStack(&_s); +} + +int main(){ + LineEdit(); + return 0; + +} \ No newline at end of file diff --git "a/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/BiTree.c" "b/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/BiTree.c" new file mode 100644 index 00000000..9ff37184 --- /dev/null +++ "b/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/BiTree.c" @@ -0,0 +1,94 @@ +#include "BiTree.h" +int count = 0; +/* 建立一个二叉树 */ +Status CreateBiTree(BiTree *T ,TElemType* d){ + + if(d[count++] == ' '){ + *T = NULL; + }else{ + if(!(*T = (BiTNode *)malloc(sizeof(BiTNode)))) { + exit(1); + } + (*T)->data = d[count-1]; + CreateBiTree(&(*T)->lchild,d); + CreateBiTree(&(*T)->rchild,d); + } + return OK; +} + +/* 先序遍历二叉树 */ +int Postorder(BiTree T) { + if(T == NULL){ + /* printf("-");*/ + return OK; + } + if(T) { + Postorder(T->lchild); /* 遍历左子树*/ + Postorder(T->rchild); /* 遍历右子树*/ + printf("%c",T->data ); /* 访问结点*/ + } + return 0; +} + +/* 求二叉树的深度*/ +int Depth(BiTree T){ + int depthval = 0; + int depthLeft = 0; + int depthRight = 0; + if(!T) { + depthval = 0; + } else { + depthLeft = Depth(T->lchild); + depthRight= Depth(T->rchild); + depthval = 1 + (depthLeft > depthRight ? depthLeft : depthRight); + } + return depthval; +} + +/* 求二叉树的最大宽度*/ +void _Width(BiTree T,int* max,int * countperlayer,int i){ + if(T == NULL) + return ; + i++; + countperlayer[i]++; + if(*max < countperlayer[i]) + *max = countperlayer[i]; + _Width(T -> lchild,max,countperlayer,i); + _Width(T -> rchild,max,countperlayer,i); + i--; +} + +int Width(BiTree T) +{ + int max = 0;/* 记录当前每层的最大节点个数 */ + int countperlayer[100] = {0}; /* 每层的节点个数 */ + static int i = 0;/*标记层数*/ + _Width(T,&max,countperlayer,i); + return max; +}//zz友情协助^-^ + +/* 计算二叉树中所有叶子结点个数 */ +int CountLeafNode(BiTree T) { + + if(!T) return 0; + if( (!T->lchild) && (!T->rchild )) { + return 1; + } else { + return CountLeafNode(T->lchild) + CountLeafNode(T->rchild); + } +} + +/* 求二叉树中所有结点个数 */ +int CountAllNode (BiTree T) { + if (!T) return 0; + if (!T->lchild && !T->rchild) { + return 1; + } else { + return CountAllNode(T->lchild) + CountAllNode(T->rchild) + 1 ; + } +} + +/* 计算二叉树中所有非叶子结点个数 */ +int CountNonLeafNode(BiTree T){ + return CountAllNode(T) - CountLeafNode(T); +} \ No newline at end of file diff --git "a/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/BiTree.h" "b/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/BiTree.h" new file mode 100644 index 00000000..64b57e51 --- /dev/null +++ "b/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/BiTree.h" @@ -0,0 +1,22 @@ +#include +#include +#define MAX_TREE_SIZE 100 // 二叉树的最大结点数 +#define TElemType char +extern int count ; +typedef enum{ + OK, + ERROR, + OVERFLOW +}Status; +typedef struct BiTNode { // 结点结构 + TElemType data; + struct BiTNode *lchild, *rchild; // 左右孩子指针 +} BiTNode, *BiTree; + +Status CreateBiTree(BiTree *T ,TElemType* d); +int Postorder(BiTree T); +int Depth(BiTree T); +int Width(BiTree T); +int CountLeafNode(BiTree T); +int CountAllNode (BiTree T); +int CountNonLeafNode(BiTree T); diff --git "a/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/main.c" "b/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/main.c" new file mode 100644 index 00000000..3a99e4d9 --- /dev/null +++ "b/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/main.c" @@ -0,0 +1,25 @@ +#include +#include +#include "BiTree.h" +TElemType tr_A[MAX_TREE_SIZE] = {'A','B','D','G',' ',' ',' ','E','H',' ',' ', 'I',' ','K',' ',' ', 'C', ' ', 'F' ,' ', ' ' }; +TElemType tr_B[MAX_TREE_SIZE] = {'A','B','D',' ','F',' ',' ',' ','C','E',' ', ' ',' '}; +int main(){ + BiTree treeA , treeB; + printf("%s","Creating binary treeA..."); + CreateBiTree(&treeA,tr_A); + printf("\nTreeA in preorder: ABDG---EH--I-K--C-F--"); + printf("\nTreeA's depth : %d",Depth(treeA)); + printf("\nTreeA's width : %d",Width(treeA)); + printf("\nThe number of TreeA's leaf node : %d",CountLeafNode(treeA)); + printf("\nThe number of TreeA's non-leaf node : %d\n",CountNonLeafNode(treeA)); + printf("%s","\nCreating binary treeB..."); + count = 0 ; + CreateBiTree(&treeB,tr_B); + printf("\nTreeB in preorder: ABD-F---CE---"); + printf("\nTreeB's depth : %d",Depth(treeB)); + printf("\nTreeB's width : %d",Width(treeB)); + printf("\nThe number of TreeB's leaf node : %d",CountLeafNode(treeB)); + printf("\nThe number of TreeB's non-leaf node : %d\n",CountNonLeafNode(treeB)); + return 0; + +} \ No newline at end of file diff --git "a/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/main.png" "b/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/main.png" new file mode 100644 index 00000000..4bf565f7 Binary files /dev/null and "b/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/main.png" differ diff --git "a/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/main_createtree.c" "b/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/main_createtree.c" new file mode 100644 index 00000000..c148c9ef --- /dev/null +++ "b/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/main_createtree.c" @@ -0,0 +1,19 @@ +#include +#include +#include "BiTree.h" +TElemType da[MAX_TREE_SIZE] = {'A','B','D','G',' ',' ',' ','E','H',' ',' ', 'I',' ','K',' ',' ', 'C', ' ', 'F' ,' ', ' ' }; +TElemType db[MAX_TREE_SIZE] = {'A','B','D',' ','F',' ',' ',' ','C','E',' ', ' ',' '}; +int main(){ + BiTree tree1 , tree2; + CreateBiTree(&tree1,da); + printf("%s","Tree 1 in preorder: ABDG---EH--I-K--C-F--"); + printf("%s","\nTree 1 in postorder: "); + Postorder(tree1); + count = 0; + CreateBiTree(&tree2,db); + printf("%s","\nTree 2 in preorder: ABD-F---CE---"); + printf("%s","\nTree 2 in postorder: "); + Postorder(tree2); + return 0; + +} \ No newline at end of file diff --git "a/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/main_createtree.png" "b/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/main_createtree.png" new file mode 100644 index 00000000..6e3c06ff Binary files /dev/null and "b/2017-1/Cloud/3.\344\272\214\345\217\211\346\240\221\347\232\204\351\201\215\345\216\206/main_createtree.png" differ diff --git "a/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/BST.c" "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/BST.c" new file mode 100644 index 00000000..1580e568 --- /dev/null +++ "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/BST.c" @@ -0,0 +1,138 @@ +#include "BST.h" + +/* 查找表的初始化建立 */ +Status _CreateBST(BiTree *T, TElemType * c) { + int i; + for (i = 0; c[i] != -1; i++) { + InsertBST(T, c[i]); + } + return OK; +} + +/* 在T树中查找关键字key */ +Status SearchBST(BiTree T, TElemType key, BiTree f, BiTree * p) { + if (!T) { + *p = f; + return FALSE; + } + else if (key == T->data) { + *p = T; + return TRUE; + } + else if (key < (*T).data) { + return SearchBST((*T).lchild, key, T, p); + } + else { + return SearchBST((*T).rchild, key, T, p); + } +} + +/* c插入关键字key */ +Status InsertBST(BiTree *T, TElemType key) { + BiTree p, s; + if (!SearchBST(*T, key, NULL, &p)) { + s = (BiTree)malloc(sizeof(BiTNode)); + s->data = key; + s->lchild = NULL; + s->rchild = NULL; + s->parent = NULL; + if (!p) { + *T = s; + } + else if (key < p->data) { + p->lchild = s; + s->parent = p; + } + else { + p->rchild = s; + s->parent = p; + } + return TRUE; + } + else { + return FALSE; + } +} + +/*查找表的初始化建立和查找 */ +Status _Search(BiTree T, TElemType * a) { + //如果未找到则将当前关键字添加到当前查找表中,如果找到,则删除该关键字所在结点 + BiTree p, f = NULL; + int i; + for (i = 0; a[i] != -1; i++) { + if (SearchBST(T, a[i], f, &p)) { + Delete(p, f); + } + else { + InsertBST(&T, a[i]); + } + _print(T); + } + return OK; +} + +/* 先序遍历二叉树 */ +Status PreOrderTraverse(BiTree T, int i) { + if (T == NULL) { + return OK; + } + if (T) { + if (i == 0) { /* 访问结点*/ + printf("%d", T->data); + i++; + } + else + printf(", %d", T->data); + PreOrderTraverse(T->lchild, i); /* 遍历左子树*/ + PreOrderTraverse(T->rchild, i); /* 遍历右子树*/ + } + return 0; +} + +/* 删除一个结点 */ +Status Delete(BiTree p, BiTree f) { + BiTree q, s; + q = p; + if (!p->rchild) { + if (p->parent->rchild == p) { + p->parent->rchild = p->lchild; + } + else { + p->parent->lchild = p->lchild; + } + free(q); + } + else if (!p->lchild) { + if (p->parent->rchild == p) { + p->parent->rchild = p->rchild; + } + else { + p->parent->lchild = p->rchild; + } + free(q); + } + else { + s = p->lchild; + while (s->rchild) { + q = s; + s = s->rchild; + } + p->data = s->data; + if (q != p) { + q->rchild = s->lchild; + } + else { + q->lchild = s->lchild; + } + free(s); + } + return OK; +} + +/* 打印一遍查找表 */ +Status _print(BiTree T) { + static int i = 0; + PreOrderTraverse(T, i); + printf("\n"); + return OK; +} \ No newline at end of file diff --git "a/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/BST.h" "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/BST.h" new file mode 100644 index 00000000..32756c6b --- /dev/null +++ "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/BST.h" @@ -0,0 +1,44 @@ +#ifndef BST_H +#define BST_H +#include +#include +#define TElemType int +#define KeyType int + +typedef struct BiTNode { + TElemType data; + struct BiTNode *lchild, *rchild, *parent; + +}BiTNode, *BiTree; +typedef enum { + FALSE, + TRUE +}bool; +typedef enum { + OK, + ERROR, + OVERFLOW +}Status; + +/* 查找表的初始化建立 */ +Status _CreateBST(BiTree *T, TElemType * c); + +/* 在T树中查找关键字key */ +Status SearchBST(BiTree T, TElemType key, BiTree f, BiTree * p); + +/* c插入关键字key */ +Status InsertBST(BiTree *T, TElemType key); + +/* 打印一遍查找表 */ +Status _print(BiTree T); + +/* 先序遍历二叉树 */ +Status PreOrderTraverse(BiTree T, int i); + +/* 删除一个结点 */ +Status Delete(BiTree p, BiTree f); + +/* 在查找表中查找 */ +Status _Search(BiTree T, TElemType * a); + +#endif // !1 diff --git "a/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/BSTOutput.txt" "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/BSTOutput.txt" new file mode 100644 index 00000000..eea55310 --- /dev/null +++ "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/BSTOutput.txt" @@ -0,0 +1,6 @@ +8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30 +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30 +7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30 \ No newline at end of file diff --git "a/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/Hash.c" "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/Hash.c" new file mode 100644 index 00000000..1d64c91f --- /dev/null +++ "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/Hash.c" @@ -0,0 +1,158 @@ +#include "Hash.h" + +/* 初始化哈希表 */ +void InitHashTable(HashTable *H) { + int i; + H->count = 0; + H->sizeindex = SizeIndex; + H->elem = (ElemType*)malloc(H->sizeindex * sizeof(ElemType)); + for (i = 0; i < H->sizeindex; i++) { + H->elem[i].key = NULLKEY; + H->elem[i].val = NULLKEY; + } +} + +/* 用随机生成的正整数序列填充Hash表 */ +void CreateHashTable(HashTable * H) { + int i; + ElemType e; + for (i = 0; i < H->sizeindex; i++) { + e.val = rand() % 1024; + e.key = e.val % H->sizeindex; + InsertHash(H, e); + } + return; +} + +/* 查找for本题 */ +void _Search(HashTable T) { + int i, p = 0, c = 0; + ElemType e; + printf("\n"); + for (i = 0; i < 5; i++) + { + e.val = (T.elem[i].key + 1 + rand())%1024; + e.key = e.val % T.sizeindex; + if (SearchHash(T, e, &p, &c)) { + printf("关键字%d查找成功! 冲突次数%d\n", e.key, c); + } + else { + printf("关键字%d查找失败!\n", e.key); + } + } + +} + +/* 返回哈希地址 */ +int Hash(HashTable H,ValueType k) { + int p; + p = k%H.sizeindex; + return p; +} + +/* 解决哈希冲突 */ +void collision(HashTable H,int *p, int *c) { + *c++; + *p = (*p + 1) % H.sizeindex; +} + +/* 哈希表的查找 */ +Status SearchHash(HashTable H, ElemType e, int *p, int *c) { + // 在开放定址哈希表H中查找关键码为K的记录 + // c用于记录冲突次数,初值置0 + *p = e.key; // 求得哈希地址 + while (H.elem[*p].key != NULLKEY && e.key != H.elem[*p].key) { + if(*p == H.sizeindex -1 ) + return FAILED; + collision(H,p, c); // 求得下一探查地址 p + } + if (e.key == H.elem[*p].key) { // 查找成功,p返回待查数据元素位置 + return SUCCESS; + } + else { + return FAILED; // 查找不成功 + } +} + +/* 向哈希表中插入数据 */ +Status InsertHash(HashTable *H, ElemType e) { + // 查找不成功时插入数据元素e到开放地址哈希表H中,并返回OK + // 若冲突次数过大,则重建哈希表 + int p, c; + c = 0;//记录冲突次数 + if (SearchHash(*H, e, &p, &c) == SUCCESS) { + collision(*H,&p, &c); // 表中已有与 e 有 相同关键字的元素 + while ((*H).elem[p].key != NULLKEY /*&& e.key != (*H).elem[p].key*/) { + collision(*H,&p, &c); // 求得下一探查地址 p + } + H->elem[p].val = e.val; + H->elem[p].key = e.key; + ++H->count; + return DUPLICATE; + } + else if (c < H->sizeindex / 2) { + // 冲突次数 c 未达到上限,(阀值可调,此处仅为示例) + H->elem[e.key].val = e.val; + H->elem[e.key].key = e.key; + ++H->count; + return SUCCESS; + } + else { + // 重建哈希表,极端情况下可能哈希表“已满”。 + // 所以通常的重建过程都是先增大新哈希表的容量 + RecreateHashTable(H); + } + return SUCCESS; +} + +/* 重建哈希表 */ +void RecreateHashTable(HashTable *H) // 重建哈希表代码参考了csdn徐留根的发表 -【数据结构】哈希表 +{ + printf("\n重建哈希表:\n"); + int i, count = (*H).count,n = (*H).sizeindex; + ElemType *p , *elem = (ElemType*)malloc(count * sizeof(ElemType)); + p = elem; + + (*H).count = 0; + for (i = 0; i < n; i++) { // 保存原有的数据到elem中 + if (((*H).elem + i)->key != NULLKEY) { // 该单元有数据 + *p++ = *((*H).elem + i); + /*p->val = ((*H).elem + i)->val; + p->key = Hash(*H, ((*H).elem + i)->val); + p++;*/ + } + } + + (*H).sizeindex += SizeIndex; + for (i = 0; i < n; i++) { + if (((*H).elem + i)->key != NULLKEY) { + p->val = ((*H).elem + i)->val; + p->key = Hash(*H, ((*H).elem + i)->val); + p++; + } + } + // 增大存储容量 + p = (ElemType*)malloc((*H).sizeindex * sizeof(ElemType)); + if (!p) + exit(1); // 存储分配失败 + (*H).elem = p; + for (i = 0; i < (*H).sizeindex; i++) { + (*H).elem[i].key = -1; // 初始化 + (*H).elem[i].val = -1; + } + for (p = elem; p < elem + count ; p++) // 将原有的数据按照新的表长插入到重建的哈希表中 + InsertHash(H, *p); +} + +/* 打印哈希表 */ +void PrintHashTable(HashTable H){ + int i = 0,j = 0; + for (; i < H.sizeindex; i++) { + // {[元素所在Hash表中的索引位置] : 关键字->值} + printf("{[%d] : %d->%d} ", i, H.elem[i].key, H.elem[i].val); + j++; + if(j%3 == 0) + printf("\n"); + } + printf("\n"); +} diff --git "a/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/Hash.h" "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/Hash.h" new file mode 100644 index 00000000..5bbaca22 --- /dev/null +++ "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/Hash.h" @@ -0,0 +1,58 @@ +#pragma once +#ifndef Hash_h +#define Hash_h +#include +#include +# +#include +#define SUCCESS 1 +#define FAILED 0 +#define DUPLICATE -1 +#define NULLKEY -1 +#define SizeIndex 7 // hash表的表长 + +typedef int KeyType; +typedef int ValueType; +typedef int Status; + +typedef struct _ElemType { + KeyType key; // 关键字 + ValueType val; // 值 +#ifdef CHAINED_HASH + struct _ElemType *next; +#endif +} ElemType; + +typedef struct { + ElemType *elem; + int count; // 当前数据元素个数 + int sizeindex; // hashsize[sizeindex]为当前容量 +} HashTable; + +/* 初始化哈希表 */ +void InitHashTable(HashTable *H); + +/* 用随机生成的正整数序列填充Hash表(for本题) */ +void CreateHashTable(HashTable * H); + +/* 查找函数(for本题) */ +void _Search(HashTable T); + +/* 解决哈希冲突 */ +void collision(HashTable H, int *p, int *c); + +/* 返回哈希地址 */ +int Hash(HashTable H, ValueType k); + +/* 哈希表的查找 */ +Status SearchHash(HashTable H, ElemType e, int *p, int *c); + +/* 向哈希表中插入数据 */ +Status InsertHash(HashTable *H, ElemType e); + +/* 重建哈希表 */ +void RecreateHashTable(HashTable *H); + +/* 打印哈希表 */ +void PrintHashTable(HashTable H); +#endif // !1 \ No newline at end of file diff --git "a/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/main_BST.c" "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/main_BST.c" new file mode 100644 index 00000000..3c63f45d --- /dev/null +++ "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/main_BST.c" @@ -0,0 +1,11 @@ +#include +#include "BST.h" +int main() { + BiTree T = NULL; + TElemType c[] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 ,-1 }, + s[] = { 13, 8, 5, 20, 6 ,-1 }; + _CreateBST(&T, c); + _print(T); + _Search(T, s); + return 0; +} \ No newline at end of file diff --git "a/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/main_Hash.c" "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/main_Hash.c" new file mode 100644 index 00000000..1c49555a --- /dev/null +++ "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/main_Hash.c" @@ -0,0 +1,13 @@ +#include +#include "Hash.h" +int main() { + srand(time(NULL)); + HashTable H; + InitHashTable(&H); + CreateHashTable(&H); + PrintHashTable(H); + _Search(H); + RecreateHashTable(&H); + PrintHashTable(H); + return 0; +} \ No newline at end of file diff --git "a/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/main_Hash.png" "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/main_Hash.png" new file mode 100644 index 00000000..a627b7e9 Binary files /dev/null and "b/2017-1/Cloud/5.\345\220\204\347\247\215\346\237\245\346\211\276\346\223\215\344\275\234/main_Hash.png" differ diff --git "a/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/Sort.c" "b/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/Sort.c" new file mode 100644 index 00000000..a0d08cc9 --- /dev/null +++ "b/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/Sort.c" @@ -0,0 +1,142 @@ +#include "Sort.h" + +/* 打印数组 */ +Status PrintArray(ElemeyType *a, int l, int move_count, int compare_count) { + int i; + for (i = 0; i < l; i++) { + printf("%d ", a[i + 1]); + } + if (a[0] == 1) + printf("\n该算法执行的 总比较次数为:%d 总移动记录次数为:%d 二者次数之和为:%d\n", + compare_count, move_count, compare_count + move_count); + else + printf("\n"); + return OK; +} + +/* 直接插入排序 */ +Status InsSort(ElemeyType *r, int length, int *move_count, int *compare_count) { + int i, j; + for (i = 2; i <= length; i++) { + *r = *(r + i); /* 将待插入记录存放到临时变量中 */ + j = i - 1; /* 最近一次排序结束的边界位置 */ + (*compare_count)++; + while (r[0] < r[j]) { /* 寻找插入位置 */ + (*move_count)++; + *(r + j + 1) = *(r + j);/* 从小到大排序,大元素右移 */ + j = j - 1; /* 待插入元素与已排序区下一个(左移一位)元素继续进行比较 */ + } + *(r + j + 1) = r[0]; /* 将待插入记录插入到已排序的序列中 */ + } + *r = 1; + return OK; +} + +/* 一趟希尔插入排序 */ +void ShellInsert(ElemeyType *r, int length, int delta, int *move_count, int *compare_count) { + //length为数组的长度, delta 为增量 + int i, j; + for (i = 1 + delta; i <= length; i++) { /* 1+delta为第一个子序列的第二个元素的下标 */ + (*compare_count)++; + if (r[i] < r[i - delta]) { + *r = r[i]; /* 备份r[i] (不做监视哨) */ + for (j = i - delta; j > 0 && r[0] < r[j]; j -= delta) { + (*compare_count)++; + *(r + j + delta) = r[j]; + (*move_count)++; + } + *(r + j + delta) = r[0]; + } + } +} + +/* 希尔排序 */ +void ShellSort(ElemeyType *r, int length, int *move_count, int *compare_count) { + //length为数组的长度 + int d = length / 2; + while (d >= 1) { + ShellInsert(r, length, d, move_count, compare_count); + d = d / 2; + } + *r = 1; +} + +/* 冒泡排序 */ +void BubbleSort(ElemeyType *r, int length, int *move_count, int *compare_count) { + //length为数组的长度 + int n = length, i, j, x; + bool change = TRUE; + for (i = 1; i <= n - 1 && change; ++i) { + change = FALSE; + for (j = 1; j <= n - i; ++j) { + (*compare_count)++; + if (r[j] > r[j + 1]) { + (*move_count)++; + x = r[j]; + *(r + j) = r[j + 1]; + *(r + j + 1) = x; + change = TRUE; + } + } + } + *r = 1; +} + +/* 快速排序 */ +void QKSort(ElemeyType *r, int low, int high, int *move_count, int *compare_count) { + int pivot; + if (low < high) { + pivot = QKPass(r, low, high, move_count, compare_count); + QKSort(r, low, pivot - 1, move_count, compare_count); + QKSort(r, pivot + 1, high, move_count, compare_count); + } + *r = 1; +} + +/* 一趟快速排序 */ +int QKPass(ElemeyType *r, int left, int right, int *move_count, int *compare_count) { + //对记录数组r 中的r[left]至r[right]部分进行一趟排序,并得到基准的位置,使得排序后的结果满足其之后(前) + //的记录的关键字均不小于(大于)于基准记录 + int x, low, high; + x = r[left]; /* 选择基准记录*/ + low = left; + high = right; + while (low < high) { + while (low < high && r[high] >= x) { /* high从右到左找小于x的记录 */ + (*compare_count)++; + high--; + } + *(r + low) = r[high]; /* 找到小于x.key的记录,则进行交换 */ + (*move_count)++; + while (low < high && r[low] < x) { /* low从左到右找不小于x的记录 */ + *(compare_count)++; + low++; + } + *(r + high) = r[low]; /* 找到不小于x的记录,则交换*/ + (*move_count)++; + } + + *(r + low) = x; /* 将基准记录保存到low=high的位置 */ + return low; /*返回基准记录的位置*/ +} + +/* 选择排序 */ +void SelectSort(ElemeyType *r, int length, int *move_count, int *compare_count) { + //length为数组的长度 + int n = length, i, k, j, x; + for (i = 1; i <= n - 1; ++i) { + k = i; + for (j = i + 1; j <= n; ++j) { + (*compare_count)++; + if (r[j] < r[k]) { + k = j; + } + } + if (k != i) { + (*move_count)++; + x = r[i]; + *(r + i) = r[k]; + *(r + k) = x; + } + } +} \ No newline at end of file diff --git "a/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/Sort.h" "b/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/Sort.h" new file mode 100644 index 00000000..4ef01518 --- /dev/null +++ "b/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/Sort.h" @@ -0,0 +1,41 @@ +#pragma once +#ifndef SORT_H +#define SORT_H +#include +#define ElemeyType int +#define len 5 +typedef enum { + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum { + FALSE, + TRUE +}bool; +/* 打印数组 */ +Status PrintArray(ElemeyType *a, int l, int move_count, int compare_count); + +/* 直接插入排序 */ +Status InsSort(ElemeyType *r, int length, int *move_count, int *compare_count); + +/* 一趟希尔插入排序 */ +void ShellInsert(ElemeyType *r, int length, int delta, int *move_count, int *compare_count); + +/* 希尔排序 */ +void ShellSort(ElemeyType *r, int length, int *move_count, int *compare_count); + +/* 冒泡排序 */ +void BubbleSort(ElemeyType *r, int length, int *move_count, int *compare_count); + +/* 快速排序 */ +void QKSort(ElemeyType *r, int low, int high, int *move_count, int *compare_count); + +/* 一趟快速排序 */ +int QKPass(ElemeyType *r, int left, int right, int *move_count, int *compare_count); + +/* 选择排序 */ +void SelectSort(ElemeyType *r, int length, int *move_count, int *compare_count); + +#endif diff --git "a/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/main_Sort.c" "b/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/main_Sort.c" new file mode 100644 index 00000000..49a7d6b9 --- /dev/null +++ "b/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/main_Sort.c" @@ -0,0 +1,53 @@ +#include +#include +#include +#include "Sort.h" +int main() { + int a[20], b[20], i; + srand(time(NULL)); + for (i = 1; i < len + 1; i++) { + a[i] = rand() % 128; + b[i] = a[i]; + } + int cCount = 0, mCount = 0;//记录比较次数和移动次数 + PrintArray(a, len, mCount, cCount); + + printf("\n希尔排序: "); + ShellSort(a, len, &mCount, &cCount); + PrintArray(a, len, mCount, cCount); + + printf("\n直接插入排序: "); + for (i = 1; i < len + 1; i++) { + a[i] = b[i]; + } + cCount = 0; + mCount = 0; + InsSort(a, len, &mCount, &cCount); + PrintArray(a, len, mCount, cCount); + + printf("\n冒泡排序: "); + for (i = 1; i < len + 1; i++) { + a[i] = b[i]; + } + cCount = 0, mCount = 0; + BubbleSort(a, len, &mCount, &cCount); + PrintArray(a, len, mCount, cCount); + + printf("\n快速排序: "); + for (i = 1; i < len + 1; i++) { + a[i] = b[i]; + } + cCount = 0, mCount = 0; + QKSort(a, 1, len, &mCount, &cCount); + PrintArray(a, len, mCount, cCount); + + printf("\n选择排序: "); + for (i = 1; i < len + 1; i++) { + a[i] = b[i]; + } + cCount = 0, mCount = 0; + SelectSort(a, len, &mCount, &cCount); + PrintArray(a, len, mCount, cCount); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/main_Sort.png" "b/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/main_Sort.png" new file mode 100644 index 00000000..0083515f Binary files /dev/null and "b/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/main_Sort.png" differ diff --git "a/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/main_Sort.txt" "b/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/main_Sort.txt" new file mode 100644 index 00000000..e4df38b4 --- /dev/null +++ "b/2017-1/Cloud/6.\345\220\204\347\247\215\346\216\222\345\272\217\346\223\215\344\275\234/main_Sort.txt" @@ -0,0 +1,16 @@ +106 19 10 63 127 + +希尔排序: 10 19 63 106 127 +该算法执行的 总比较次数为:9 总移动记录次数为:2 二者次数之和为:11 + +直接插入排序: 10 19 63 106 127 +该算法执行的 总比较次数为:4 总移动记录次数为:4 二者次数之和为:8 + +冒泡排序: 10 19 63 106 127 +该算法执行的 总比较次数为:9 总移动记录次数为:4 二者次数之和为:13 + +快速排序: 10 19 63 106 127 +该算法执行的 总比较次数为:2 总移动记录次数为:6 二者次数之和为:8 + +选择排序: 10 19 63 106 127 +该算法执行的 总比较次数为:10 总移动记录次数为:2 二者次数之和为:12 diff --git a/2017-1/DZM/READ ME.MD b/2017-1/DZM/READ ME.MD new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/2017-1/DZM/READ ME.MD @@ -0,0 +1 @@ + diff --git "a/2017-1/DZM/\350\241\250\350\276\276\345\274\217.cpp" "b/2017-1/DZM/\350\241\250\350\276\276\345\274\217.cpp" new file mode 100644 index 00000000..83b15a18 --- /dev/null +++ "b/2017-1/DZM/\350\241\250\350\276\276\345\274\217.cpp" @@ -0,0 +1,236 @@ +#include +#include +#include +using namespace std; +#define SIZE 50 + +/********************运算符的栈********************/ +typedef struct stack +{ //定义栈指针 + char *base; //栈底 + char *top; //栈顶 +}SqStack; + +int InitStack(SqStack &S) +{ //初始化栈 + S.base = (char *)malloc(SIZE*sizeof(char)); + S.top = S.base; + return 0; +} + +char GetTop(SqStack S) +{ //获取栈顶值并返回给e + char e; + e = *(S.top - 1); + return e; +} + +int Push(SqStack &S, char e) +{ //入栈 + *S.top = e; + S.top++; + return 0; +} + +char Pop(SqStack &S) +{ //出栈 + char e; + S.top--; + e = *S.top; + return e; +} + +bool isEmpty(SqStack S) +{ //判断栈是否为空 + if (S.top == S.base) + return true; + else + return false; +} + +/********************常量的栈********************/ +typedef struct Istack +{ //定义栈指针 + double *base; //栈底 + double *top; //栈顶 +}Istack; + + +int InitStack(Istack &S) +{ //栈初始化 + S.base = (double *)malloc(SIZE*sizeof(double)); + S.top = S.base; + return 0; +} + +double GetTop(Istack S) +{ //获取栈顶值并返回给e + double e; + e = *(S.top - 1); + return e; +} + +int Push(Istack &S, float e) +{ //入栈 + *S.top = e; + S.top++; + return 0; +} + +double Pop(Istack &S) +{ //出栈 + double e; + S.top--; + e = *S.top; + return e; +} + +bool isEmpty(Istack S) +{ //判断栈是否为空 + if (S.top == S.base) + return true; + else + return false; +} + +int Change(char S[],char OPS[],int &len) //S为输入的中缀表达式,OPS为转化后的后缀表达式 +{ //将中缀表达式转化为后缀表达式的函数 + SqStack OPE; //定义OPE为运算符栈 + InitStack (OPE); //初始化OPE + int i,j=0; + for (i = 0; i < strlen(S); i++) + { + switch (S[i]) + { + case'+': + case'-': //若为“+”、“-”,则执行 + if (GetTop(OPE) == '*' || GetTop(OPE) == '/') //若栈顶是乘除号,优先数低于栈顶运算符 + { + OPS[j++] = Pop(OPE); //则中缀式符号栈出栈,放入后缀式中 + i--; + } + else + Push(OPE, S[i]); //若优先数高于栈顶运算符,则进栈 + break; + case'*': + case'/': + Push(OPE, S[i]); + break; + case'(': + Push(OPE, S[i]); + break; + case')': + while (GetTop(OPE) != '(') + { + OPS[j++] = Pop(OPE); + } + Pop(OPE); + break; + default: + while (S[i] >= '0'&&S[i] <= '9' || S[i] == '.') //将操作数直接放入后缀式 + { + OPS[j++] = S[i]; + i++; + } + i--; + OPS[j++] = '#'; //数字中的间隔符 + break; + } + } + while (!isEmpty(OPE)) //将剩余的操作符放入后缀式 + { + OPS[j++] = Pop(OPE); + } + len = j; //len为后缀表达式的长度 + return 0; +} + +int EXchange(char B[], int len, double &result,bool &flag) //B为后缀式,len为后缀表达式的长度 +{ //通过后缀表达式求值的函数 + int i; + double a; + double b; + double c; + Istack SZ; + InitStack(SZ); + + for (i = 0; i < len; i++) + { + switch (B[i]) + { + case'+': + { + a = Pop(SZ); //操作数出栈1 + b = Pop(SZ); //操作数出栈2 + c = b + a; //相加 + Push(SZ, c); //将相加之和入栈 + } + break; + case'-': + { + a = Pop(SZ); //操作数出栈1 + b = Pop(SZ); //操作数出栈2 + c = b - a; //相减 + Push(SZ, c); //将差入栈 + } + break; + case'*': + { + a = Pop(SZ); //操作数出栈1 + b = Pop(SZ); //操作数出栈2 + c = b * a; //相乘 + Push(SZ, c); //将积入栈 + } + break; + case'/': + { + a = Pop(SZ); //操作数出栈1 + b = Pop(SZ); //操作数出栈2 + if (a == 0) //若被除数为0则出错 + { + flag = false; + return 0; + } + c = b / a; //相除 + Push(SZ, c); //入栈 + } + break; + default: + int j = 0; + float d; + char *st = new char[10]; + while (B[i] != '#') + { + st[j++] = B[i]; + i++; + } + d = atof(st); + Push(SZ, d); + delete st; + break; + } + } + result=GetTop(SZ); //将栈顶元素返回给结果 + return 0; +} + +int main() +{ + char S[SIZE]; //定义中缀表达式 + char B[SIZE]; + int len; + bool flag = true; + double result; + + cout <<"输入表达式,不用以#结束(注意括号大小写)"<> S; //输入表达式 + Change(S,B,len); + EXchange(B, len, result,flag); + + if (flag) + cout << result; + else + cout << "The expression is not vaild" << endl; + system("pause"); + return 0; +} diff --git "a/2017-1/DZM/\350\277\267\345\256\253\346\261\202\350\247\243.cpp" "b/2017-1/DZM/\350\277\267\345\256\253\346\261\202\350\247\243.cpp" new file mode 100644 index 00000000..1e50fddc --- /dev/null +++ "b/2017-1/DZM/\350\277\267\345\256\253\346\261\202\350\247\243.cpp" @@ -0,0 +1,325 @@ +锘#include +#include + +#define OK 1 +#define ERROR 0 +#define TRUE 1 +#define FALSE 0 +#define OVERFLOW -2 +#define STACK_INIT_SIZE 100 //瀛樺偍绌洪棿鍒濆鍒嗛厤閲 +#define STACKINCREMENT 10 //瀛樺偍绌洪棿鍒嗛厤澧為噺 +#define COL 10 //杩峰琛屾暟 +#define ROW 10 //杩峰鍒楁暟 +#define RANGE 10 //杩峰鍒楁暟 + +typedef int Status; //瀹氫箟Status涓篿nt绫诲瀷 + +typedef struct{ + //杩峰绫诲瀷 + char **maze; //杩峰鏁版嵁 + int **footprint;//瓒宠抗鏁版嵁 + int row; //琛屾暟 + int col; //鍒楁暟 +}MazeType; + +typedef struct{ + //杩峰浣嶇疆鍧愭爣 + int x; + int y; +}PosType; + +typedef struct{ + //鏍堝厓绱犵被鍨 + int ord; //閫氶亾鍧楀湪璺緞涓婄殑鈥滃簭鍙封 + PosType seat; //閫氶亾鍧楀湪杩峰涓殑鈥滃潗鏍囦綅缃 + int di; //浠庢閫氫俊鍧楄蛋鍚戜笅涓閫氶亾鍧楃殑"鏂瑰悜" +}SElemType; + +typedef struct{ + //椤哄簭鏍堢粨鏋勫畾涔 + SElemType *base; //鏍堝簳鎸囬拡 + SElemType *top; //鏍堥《鎸囬拡 + int stacksize; //褰撳墠宸插垎閰嶇殑瀛樺偍绌洪棿 +}SqStack; + +/*-------------鍩烘湰鎿嶄綔鐨勫嚱鏁板師鍨嬭鏄-----------*/ +Status InitStack(SqStack *S); //鏋勯犱竴涓┖鏍圫 +Status StackEmpty(SqStack S); //鑻ユ爤S涓虹┖鏍堬紝鍒欒繑鍥濼RUE锛屽惁鍒欒繑鍥濬ALSE +Status GetTop(SqStack S,SElemType *e); //鑻ユ爤涓嶄负绌猴紝鍒欑敤e杩斿洖S鐨勬爤椤跺厓绱狅紝骞惰繑鍥濷K锛涘惁鍒欒繑鍥濬ALSE +Status Push(SqStack *S,SElemType e); //鎻掑叆鍏冪礌e涓烘柊鐨勬爤椤跺厓绱 +Status Pop(SqStack *S,SElemType *e); //鑻ユ爤S涓嶄负绌猴紝鍒欏垹闄鐨勬爤椤跺厓绱狅紝鐢╡杩斿洖鍏跺硷紝骞惰繑鍥濷K,鍚﹀垯杩斿洖ERROR + + +/*------------鍩烘湰鎿嶄綔鐨勭畻娉曟弿杩-----------*/ +Status InitStack(SqStack *S){ + //鏋勯犱竴涓┖鏍圫 + S->base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if(!S->base) exit(OVERFLOW);//鍒嗛厤澶辫触 + S->top=S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +}//InitStack + +Status StackEmpty(SqStack S){ + //鑻ユ爤S涓虹┖鏍堬紝鍒欒繑鍥濼RUE锛屽惁鍒欒繑鍥濬ALSE + if(S.top == S.base) + return TRUE; + else + return FALSE; +}//StackEmpty + +Status GetTop(SqStack S,SElemType *e){ + //鑻ユ爤涓嶄负绌猴紝鍒欑敤e杩斿洖S鐨勬爤椤跺厓绱狅紝骞惰繑鍥濷K锛涘惁鍒欒繑鍥濫RROR + if(S.top == S.base){ + return ERROR; + } + else{ + *e=*(S.top-1); + return OK; + } +}//GetTop + +Status Push(SqStack *S,SElemType e){ + //鎻掑叆鍏冪礌e涓烘柊鐨勬爤椤跺厓绱 + if((S->top - S->base) >= S->stacksize){ + //鏍堝凡婊★紝杩藉姞瀛樺偍绌洪棿 + S->base = (SElemType *)realloc(S->base,(S->stacksize + STACKINCREMENT)*sizeof(SElemType)); + if(!S->base) exit(OVERFLOW); + S->top = S->base + S->stacksize;//鏇存敼鏍堥《鎸囬拡 + S->stacksize += STACKINCREMENT; + } + *S->top++=e; + return OK; +}//Push + +Status Pop(SqStack *S,SElemType *e){ + //鑻ユ爤S涓嶄负绌猴紝鍒欏垹闄鐨勬爤椤跺厓绱狅紝鐢╡杩斿洖鍏跺硷紝骞惰繑鍥濷K,鍚﹀垯杩斿洖ERROR + if(S->top==S->base) return ERROR; + *e=*(--S->top); + return OK; +}//Pop + + +/*-----------璧拌糠瀹▼搴-----------*/ +Status InitMaze(MazeType *M) +{ + //鍒濆鍖栬糠瀹暟鎹 + char mz[ROW][COL]={ + '#','#','#','#','#','#','#','#','#','#', + '#',' ',' ','#',' ',' ',' ','#',' ','#', + '#',' ',' ','#',' ',' ',' ','#',' ','#', + '#',' ',' ',' ',' ','#','#',' ',' ','#', + '#',' ','#','#','#',' ',' ',' ',' ','#', + '#',' ',' ',' ','#',' ','#',' ','#','#', + '#',' ','#',' ',' ',' ','#',' ',' ','#', + '#',' ','#','#','#',' ','#','#',' ','#', + '#','#',' ',' ',' ',' ',' ',' ',' ','#', + '#','#','#','#','#','#','#','#','#','#' + }; + + M->maze = (char **)malloc(sizeof(char *)*ROW); + M->footprint=(int **)malloc(sizeof(int *)*ROW); + if(!M->maze||!M->footprint){ + printf("鐢宠绌洪棿澶辫触锛岃糠瀹棤娉曞垵濮嬪寲.\n"); + return ERROR; + exit(0); + } + + for(int i=0;imaze[i]=(char *)malloc(sizeof(char)*COL); + M->footprint[i]=(int *)malloc(sizeof(int)*COL); + if(!M->maze[i]||!M->footprint[i]) + { + printf("鐢宠绌洪棿澶辫触锛岃糠瀹垵濮嬪寲澶辫触.\n"); + return ERROR; + exit(0); + } + } + + for(int i=0;imaze[i][j]=mz[i][j]; //灏嗘暟鎹啓鍏ュ埌鏁版嵁缁撴瀯 + M->footprint[i][j]=0; + } + } + M->row = ROW;//琛 + M->col = COL;//鍒 + return OK; +} + +Status PrintMaze(MazeType *M){ + //杈撳嚭杩峰 + int i,j; + for(i=0;irow;i++){ + for(j=0;jcol;j++){ + switch(M->maze[i][j]) + { + case ' ': + printf("鈻"); + break; + case '#': + printf("鈻"); + break; + } + } + + printf("\n"); + } + return OK; +} + +Status FootPrint(MazeType *mase,PosType pos){ + //灏嗚糠瀹殑褰撳墠浣嶇疆pos璁剧疆涓"璧拌繃"锛屽嵆footprint璇ヤ綅缃负1 + mase->footprint[pos.x][pos.y]=1; + return TRUE; +} + +Status Pass(MazeType *M,PosType pos){ + //鍒ゆ柇褰撳墠浣嶇疆鏄惁鍙氾紝鍗充负璧拌繃鐨勯氶亾鍧 + if((0==M->footprint[pos.x][pos.y])&&(M->maze[pos.x][pos.y]==' ')) + return TRUE; + else + return FALSE; +} + +SElemType NewSElem(int step,PosType pos,int d){ + //鍒涘缓鏂扮粨鐐癸紝鐢╯tep锛宲os锛宒鍒濆鍖栬缁撶偣 + SElemType e; + e.ord=step; + e.seat=pos; + e.di=d; + return e; +} + +PosType NextPos(PosType pos,int d){ + //鑾峰彇pos浣嶇疆d鏂瑰悜鐨勪綅缃 + switch(d){ + case 1://涓 + pos.x++; + break; + case 2://鍗 + pos.y++; + break; + case 3://瑗 + pos.x--; + break; + case 4://鍖 + pos.y--; + break; + default: + printf("浣嶇疆缂栧彿鍑洪敊.\n"); + } + return pos; +} + +Status MarkPrint(MazeType *M,PosType pos){ + //灏嗚糠瀹玀鐨刾os浣嶇疆锛岃涓哄凡璧帮紝鎴愬姛杩斿洖OK锛涘惁鍒欒繑鍥濫RROR + if(pos.x>M->row||pos.y>M->col){ + printf("鎵瑕佹爣璁颁綅缃秺浣.\n"); + return ERROR; + } + M->footprint[pos.x][pos.y]=1; + return OK; +} + +Status PrintFoot(MazeType *M,SqStack *S){ + //杈撳嚭杩峰鐨勮矾寰 + int i,j; + SElemType *p; + for(i=0;irow;i++){ + for(j=0;jcol;j++){ + M->footprint[i][j]=0; + } + } + p=S->base; + if(S->base==S->top) + { + printf("鏍堜负绌.\n"); + return FALSE; + } + while(p!=S->top) + { + M->footprint[p->seat.x][p->seat.y]=1; + *p++; + } + for(i=0;irow;i++){ + for(j=0;jcol;j++){ + switch(M->footprint[i][j]) + { + case 0: + printf("鈻"); + break; + case 1: + printf("鈽"); + break; + } + } + printf("\n"); + } + + return OK; +} + +Status MazePath(SqStack *S,MazeType maze,PosType start,PosType end){ + //鑻ヨ糠瀹玬aze涓瓨鍦ㄤ粠鍏ュ彛start鍒板嚭鍙nd鐨勯氶亾锛屽垯姹傚緱涓鏉″瓨鏀惧湪鏍堜腑锛堜粠鏍堝簳 + //鍒版爤椤讹級锛屽苟杩斿洖TRUE锛涘惁鍒欒繑鍥濬ALSE + int curstep=1;//褰撳墠姝ユ暟 + SElemType e; + PosType curpos=start;//褰撳墠浣嶇疆 + InitStack(S);//鍒濆鍖栨爤 + do{ + if(Pass(&maze,curpos)) + {//濡傛灉褰撳墠浣嶇疆鍙互閫氳繃,鍒欐槸鏈浘璧板埌鐨勯氶亾鍧 + FootPrint(&maze,curpos); //鐣欎笅瓒宠抗 + e=NewSElem(curstep,curpos,1); //鍒涘缓鍏冪礌 + Push(S,e); //灏嗗厓绱犳斁鍏ユ爤涓 + if((curpos.x==end.x)&&(curpos.y==end.y)){//鍒拌揪缁堢偣锛堝嚭鍙o級 + printf("杩峰璺緞:\n"); + PrintFoot(&maze,S); //鎵撳嵃瓒宠抗 + return TRUE; + } + curpos=NextPos(curpos,1); //鑾峰緱涓嬩竴鑺傜偣 + curstep++; //姝ユ暟鍔犱竴 + + }//if + else{//褰撳墠浣嶇疆涓嶈兘閫氳繃 + if(!StackEmpty(*S)){ + Pop(S,&e); //鍑烘爤 + while(e.di==4&&!StackEmpty(*S)){ //褰撳垽鏂洓涓柟鍚戦兘涓嶈兘閫氳繃鏃 + MarkPrint(&maze,e.seat); //鏍囪褰撳墠浣嶇疆 + Pop(S,&e); //鍑烘爤 + }//while + if(e.di<4){ //褰撳垽鏂柟鍚戞病鏈夊洓涓椂 + e.di++; //鎹釜鏂瑰悜缁х画鍒ゆ柇 + Push(S,e); + curpos=NextPos(e.seat,e.di); + }//if + }//if + }//else + //PrintFoot(&maze,S); + }while(!StackEmpty(*S)); + return FALSE; +} + + + +void main() +{ + MazeType maze;//杩峰缁撴瀯 + SqStack stack;//椤哄簭鏍堬紝瀛樺偍杩峰璺緞 + PosType start,end;//杩峰鐨勮捣鐐瑰拰缁堢偣锛 + + start.x=1; + start.y=1;//杩峰鐨勮捣鐐 + end.x=8; + end.y=8;//杩峰鐨勭粓鐐 + InitMaze(&maze);//杩峰鍒濆鍖 + + printf("杩峰褰㈢姸锛歕n"); + PrintMaze(&maze);//鎵撳嵃杩峰褰㈢姸 + + if(MazePath(&stack,maze,start,end)) + printf("杩峰鍙В.\n"); + else + printf("杩峰涓嶅彲瑙.\n"); +} \ No newline at end of file diff --git "a/2017-1/DZM/\351\223\276\345\274\217\346\230\240\345\203\217\345\256\236\347\216\260.cpp" "b/2017-1/DZM/\351\223\276\345\274\217\346\230\240\345\203\217\345\256\236\347\216\260.cpp" new file mode 100644 index 00000000..db79b28b --- /dev/null +++ "b/2017-1/DZM/\351\223\276\345\274\217\346\230\240\345\203\217\345\256\236\347\216\260.cpp" @@ -0,0 +1,112 @@ +/* 单链队列——队列的链式存储结构 */ +typedef struct QNode +{ + QElemType data; //数据域 + struct QNode *next; //指针域 +}QNode,*QueuePtr; + +typedef struct +{ + QueuePtr front,rear; //队头、队尾指针 +}LinkQueue; + +/* 链队列的基本操作*/ +void InitQueue(LinkQueue *Q) +{ //构造一个空队列Q + Q->front=Q->rear=malloc(sizeof(QNode)); //动态分配空间 + if(!Q->front) + exit(OVERFLOW); + Q->front->next=NULL; //空队列 +} + +void DestroyQueue(LinkQueue *Q) +{ //销毁队列Q(无论空否均可) + while(Q->front) //释放元素空间 + { + Q->rear=Q->front->next; + free(Q->front); + Q->front=Q->rear; + } +} + +void ClearQueue(LinkQueue *Q) +{ //将Q清为空队列 + QueuePtr p,q; + Q->rear=Q->front; + p=Q->front->next; //p指向队头元素 + Q->front->next=NULL; //删除队头元素 + while(p) + { + q=p; + p=p->next; + free(q); + } +} + +Status QueueEmpty(LinkQueue Q) +{ //若Q为空队列,则返回TRUE,否则返回FALSE + if(Q.front->next==NULL) + return TRUE; + else + return FALSE; +} + +int QueueLength(LinkQueue Q) +{ //求队列的长度 + int i=0; + QueuePtr p; + p=Q.front; + while(Q.rear!=p) //当p指针的值与队尾不同时,继续执行 + { + i++; + p=p->next; //指向下一数据 + } + return i; +} + +Status GetHead_Q(LinkQueue Q,QElemType *e) +{ //若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR + QueuePtr p; + if(Q.front==Q.rear) //空队列 + return ERROR; + p=Q.front->next; //p指向队头元素 + *e=p->data; //用e返回队头元素的数据 + return OK; +} + +void EnQueue(LinkQueue *Q,QElemType e) +{ //插入元素e为Q的新的队尾元素 + QueuePtr p=malloc(sizeof(QNode)); + if(!p) //存储分配失败 + exit(OVERFLOW); + p->data=e; + p->next=NULL; + Q->rear->next=p; + Q->rear=p; +} + +Status DeQueue(LinkQueue *Q,QElemType *e) +{ //若队列不空,删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR + QueuePtr p; + if(Q->front==Q->rear) + return ERROR; + p=Q->front->next; + *e=p->data; + Q->front->next=p->next; + if(Q->rear==p) + Q->rear=Q->front; + free(p); + return OK; +} + +void QueueTraverse(LinkQueue Q,void(*vi)(QElemType)) +{ //从队头到队尾依次对队列Q中每个元素调用函数vi() + QueuePtr p; + p=Q.front->next; + while(p) + { + vi(p->data); + p=p->next; + } + printf("\n"); +} diff --git "a/2017-1/DZM/\351\223\276\350\241\250\345\210\206\350\247\243.cpp" "b/2017-1/DZM/\351\223\276\350\241\250\345\210\206\350\247\243.cpp" new file mode 100644 index 00000000..b3cc3484 --- /dev/null +++ "b/2017-1/DZM/\351\223\276\350\241\250\345\210\206\350\247\243.cpp" @@ -0,0 +1,133 @@ +/* + 时间复杂度: + 本方法的时间复杂度为O(n) + 若每遍历一遍链表提出一个数则复杂度为O(n^2) + 所以为最低时间复杂度 + 空间复杂度: + 该方法和每遍历一遍链表提出一个数的空间复杂度差不多 +*/ +#include +#define MAX 100 + +typedef struct LNode +{ //定义链表 + int data; //数据域 + struct LNode *next; //指针域 +}LNode,*LinkList; + +int InitList(LinkList &L) +{ //初始化链表 + L=new LNode; + L->next=NULL; + return 1; +} + +int ListLength(LinkList L) +{ //返回链表长度 + int length=0; + struct LNode *p; + p=L->next; + while(p){ + ++length; + p=p->next; + } + return length; +} + +void TraveList(LinkList L) +{ //打印链表 + struct LNode *p; + p=L->next; + while(p){ + printf("%d ",p->data); + p=p->next; + } + printf("\n"); +} + +void CreateList(LinkList &L,int n) +{ //输入链表的值 + L=new LNode; + L->next=NULL; + struct LNode *p; + p=L; + for(int i=0;idata); + s->next=NULL; + p->next=s; + p=s; + } +} + +void Resolve(LinkList &LA,LinkList &LB,LinkList &LC) +{ //分解链表函数 + struct LNode *pa,*pb,*pc; + pa=LA->next; + LB=new LNode; + LC=new LNode; //生成两个新节点 + pb=LB; + pc=LC; + + while(pa){ + if( (pa->data) % 2 == 0 ) //若数据为偶数 + { + pb->next=pa; + pa=pa->next; //指向下一节点 + pb=pb->next; + pb->next=NULL; //该节点为尾节点 + } + else + { + pc->next=pa; + pa=pa->next; + pc=pc->next; + pc->next=NULL; + } + } +} + +int main(){ + LinkList LA; + LinkList LB; + LinkList LC; + + InitList(LA); + InitList(LB); + InitList(LC); + + if(LinkList(LA)){ + printf("LA初始化成功!\n"); + }else{ + printf("LA初始化失败!\n"); + } + + if(LinkList(LB)){ + printf("LB初始化成功!\n"); + }else{ + printf("LB初始化失败!\n"); + } + + if(LinkList(LC)){ + printf("LC初始化成功!\n"); + }else{ + printf("LC初始化失败!\n"); + } + + printf("\n请输入LA的长度:"); + int n1; + scanf("%d",&n1); + CreateList(LA,n1); + printf("\n链表A:\n"); + TraveList(LA); + + Resolve(LA,LB,LC); + printf("链表B:\n"); + TraveList(LB); + printf("链表C:\n"); + TraveList(LC); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/DZM/\351\223\276\350\241\250\345\220\210\345\271\266.cpp" "b/2017-1/DZM/\351\223\276\350\241\250\345\220\210\345\271\266.cpp" new file mode 100644 index 00000000..4d7e14aa --- /dev/null +++ "b/2017-1/DZM/\351\223\276\350\241\250\345\220\210\345\271\266.cpp" @@ -0,0 +1,83 @@ +#include +#include +#include + +typedef struct Node +{ + struct Node *next; //定义指针指向下一节点 + int data; //定义链表的值 + int listsize; //定义链表长度 +}Node,*List; + +void Merge(List &La,List &Lb,List &Lc) //合并链表 +{ + Node *pa,*pb,*pc; + pa = La->next; + pb = Lb->next; + Lc = pc = La; //用La的头结点作为Lc的头结点 + while(pa && pb) + { + if(pa->data <= pb->data) + { + pc->next = pa; //使Lc下一节点等于La中的pa节点的值 + pc = pa; //将pa的地址给pc,实现pc节点后移 + pa = pa->next; //pa节点的后移 + } + else + { + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + pc->next = pa ? pa : pb; //若pa为非空指针就把pa的赋值给pc->next,否则把pb的值赋给pc->next + //插入剩余段 + free(Lb); +} + +List getList(int a[], int len) //初始化链表 +{ + Node *head = (Node*)malloc(sizeof(Node)); //头结点初始化 + Node *p = head; //定义p为头结点 + head->next = NULL; + for(int i=0; idata = a[i]; + p->next = n; + p = n; + } + p->next = NULL; + return head; +} + +void print(List a) //打印链表 +{ + if(!a) return; + Node *p = a->next; + while(p) + { + printf("%d ", p->data); + p = p->next; + } + putchar('\n'); +} + +void main() +{ + int a[] = {1, 2, 3, 4}; //定义链表a,b中的数据 + int b[] = {0, 1, 9}; + + List la = getList(a, 4); //初始化链表 + List lb = getList(b, 3); + List lc = NULL; + + printf("链表La中的数据:\n"); + print(la); + printf("链表Lb中的数据:\n"); + print(lb); + Merge(la, lb, lc); + printf("链表合并后的数据:\n"); + print(lc); + system("pause"); +} \ No newline at end of file diff --git "a/2017-1/FllyFly-H2/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/1.c" "b/2017-1/FllyFly-H2/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/1.c" new file mode 100644 index 00000000..eb04c032 --- /dev/null +++ "b/2017-1/FllyFly-H2/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/1.c" @@ -0,0 +1,214 @@ +锘#include +#include +#include + +#define STACK_INIT_SIZE 100 //瀛樺偍绌洪棿鍒濆鍒嗛厤閲 +#define STACKINCREMENT 10 //瀛樺偍绌洪棿鍒嗛厤澧為噺 +typedef char SElemType; + +typedef struct Sq_Stack { + SElemType *base; + SElemType *top; + int stacksize; + +} SqStack;//瀛樺偍杩愮畻鏁板拰杩愮畻绗︾殑缁撴瀯浣 +typedef enum { + OK, + ERROR, + OVERFLOW, + +} Status; +typedef enum { + true, + false + +}bool; +unsigned char opchartab[7][7]= + { + '>', '>', '<', '<', '<', '>', '>', + '>', '>', '<', '<', '<', '>', '>', + '>', '>', '>', '>', '<', '>', '>', + '>', '>', '>', '>', '<', '>', '>', + '<', '<', '<', '<', '<', '=', ' ', + '>', '>', '>', '>', ' ', '>', '>', + '<', '<', '<', '<', '<', ' ', '=' + + }; +char OPERATORS[]={'+','-','*','/','(',')','$'};//杩愮畻绗︽暟缁 + +Status InitStack(SqStack *stack);//鏋勯犱竴涓繍绠楁暟绌烘爤 +void DestroyStack(SqStack *stack);//閿姣佸凡缁忓瓨鍦ㄧ殑杩愮畻鏁版爤 +Status Push(SqStack *stack, SElemType e);//鎶婁竴涓柊鐨勫厓绱犳坊鍔犲埌杩愮畻鏁版爤涓 +Status Pop(SqStack *stack, SElemType *e);//鍒犻櫎杩愮畻鏁版爤椤剁殑鍏冪礌,骞剁敤e杩斿洖鍏跺 +bool StackEmpty(SqStack *stack);//鍒ゆ柇涓涓繍绠楁暟鏍堟槸鍚︿负绌 +SElemType GetTop(SqStack s); + +bool IsOperator(SElemType e);//鍒ゆ柇璇诲叆瀛楃鏄惁涓鸿繍绠楃 +SElemType Precede(SElemType a,SElemType b);//姣旇緝杩愮畻绗︾殑浼樺厛绾 +SElemType Operate(SElemType a,SElemType Operator,SElemType b);//杩愮畻 +int EvaluateExpression(); + +Status InitStack(SqStack *stack)//鏋勯犱竴涓繍绠楃绌烘爤 +{ + //stack=(SqStack*)malloc(STACK_INIT_SIZE * sizeof(int)); + stack->base = (SElemType *) malloc(STACK_INIT_SIZE * sizeof(int)); + if (!stack->base) + // exit(OVERFLOW); + { + return OVERFLOW; + }// 瀛樺偍鍒嗛厤澶辫触 + stack->stacksize = STACK_INIT_SIZE; + stack->top = stack->base; + return OK; +} +void DestroyStack(SqStack *stack)//閿姣佸凡缁忓瓨鍦ㄧ殑鏍 +{ + + /*stack = NULL;*/ + +} +Status Push(SqStack *stack, SElemType e)//鎶婁竴涓柊鐨勫厓绱犳坊鍔犲埌鏍堜腑 +{ + if (stack->top - stack->base > stack->stacksize) { //stack->top + stack->base = (SElemType *) realloc(stack->base, (stack->stacksize + STACKINCREMENT) * sizeof(SElemType *)); + if (!stack->base) { + return OVERFLOW; + } + stack->top = stack->base + stack->stacksize; + stack->stacksize = stack->stacksize + STACKINCREMENT; + + } + *stack->top = e; + stack->top++; + return OK; +} +Status Pop(SqStack *stack, SElemType *e)//鍒犻櫎杩愮畻鏁版爤椤剁殑鍏冪礌,骞剁敤e杩斿洖鍏跺 +{ + if (StackEmpty(stack)) { + return ERROR; + } + stack->top--; + *e = *stack->top; + + return OK; +} +bool StackEmpty(SqStack *stack)// 鍒ゆ柇涓涓繍绠楁暟鏍堟槸鍚︿负绌烘爤 +{ + + if (stack->base == stack->top) + return true; + else + return false; + +} +SElemType GetTop(SqStack stack) {// 杩斿洖杩愮畻鏁版爤椤跺厓绱 + if (!StackEmpty(&stack)) { + stack.top--; + } + + return *stack.top; +} +bool IsOperator(SElemType e)//鍒ゆ柇璇诲叆瀛楃鏄惁涓鸿繍绠楃 +{ + if(e=='+'||e=='-'||e=='*'||e=='/'||e=='('||e==')'||e=='#') + return true; + else + return false; +} +SElemType Precede(SElemType a,SElemType b)//姣旇緝杩愮畻绗︾殑浼樺厛绾 +{ + SElemType ch; + if(a=='+'||a=='-') + { + if(b=='+'||b=='-'||b==')'||b=='#') + ch='>'; + else if(b=='*'||b=='/'||b=='(') + ch='<'; + } + else if(a=='*'||a=='/') + { + if(b=='+'||b=='-'||b=='*'||b=='/'||b==')'||b=='#') + ch='>'; + else if(b=='(') + ch='<'; + } + else if(a=='(') + { + if(b=='+'||b=='-'||b=='*'||b=='/'||b=='(') + ch='<'; + else if(b==')') + ch='='; + } + else if(a==')') + { + if(b=='+'||b=='-'||b=='*'||b=='/'||b==')'||b=='#') + ch='>'; + } + else if(a=='#') + { + if(b=='+'||b=='-'||b=='*'||b=='/'||b=='(') + ch='<'; + else if(b=='#') + ch='='; + } + return ch; +} +SElemType Operate(SElemType a,SElemType Operator,SElemType b)//杩愮畻 +{ + SElemType ch; + a=a-'0';// 杞寲鎴恑nt绫 + b=b-'0';//杞寲鎴恑nt绫 + if(Operator=='+') + ch=a+b+'0';//鍦ㄨ绠楃殑鍚屾椂杞寲鎴恈har绫 + else if(Operator=='-') + ch=a-b+'0'; + else if(Operator=='*') + ch=a*b+'0'; + else if(Operator=='/') + ch=a/b+'0'; + return ch; +} + int EvaluateExpression() +{ + SqStack Operator,Oprand; + SElemType ch,a,b,theta,x; + InitStack(&Operator);//鍒濆鍖栨搷浣滅鏍 + InitStack(&Oprand);//鍒濆鍖栨搷浣滄暟鏍 + Push(&Operator,'#'); + ch=getchar(); + while(ch!='#'||GetTop(Operator)!='#') + { + if(!IsOperator(ch)) + { + Push(&Oprand,ch); + ch=getchar(); + } + else + { + switch(Precede(GetTop(Operator),ch)) + { + case '<': + Push(&Operator,ch); + ch=getchar(); + break; + case '>': + Pop(&Operator,&theta); + Pop(&Oprand,&b); + Pop(&Oprand,&a); + Push(&Oprand,Operate(a,theta,b)); + break; + case '=': + Pop(&Operator,x); + ch=getchar(); + break; + } + } + } + return GetTop(Oprand)-'0'; +} +int main() +{ + printf("璇疯緭鍏ョ畻鏈〃杈惧紡,骞朵互#缁撴潫\n"); + printf("缁撴灉鏄: %d\n",EvaluateExpression()); + return 0; +} diff --git "a/2017-1/FllyFly-H2/\351\230\237\345\210\227\345\237\272\346\234\254\350\277\220\347\256\227/2.c" "b/2017-1/FllyFly-H2/\351\230\237\345\210\227\345\237\272\346\234\254\350\277\220\347\256\227/2.c" new file mode 100644 index 00000000..77281581 --- /dev/null +++ "b/2017-1/FllyFly-H2/\351\230\237\345\210\227\345\237\272\346\234\254\350\277\220\347\256\227/2.c" @@ -0,0 +1,168 @@ +#include +#include +#include +#include + +typedef int QElemType; +typedef enum +{ + OK , + OVERFLOW, + ERROR +} Status; + +typedef enum +{ + false, + true +}bool; + +typedef struct QNode +{ + QElemType data; + struct QNode *next; +}*QueuePtr,QNode; +typedef struct LinkQueue +{ + QueuePtr front, rear;//队头,队尾指针 +}LinkQueue; + +Status InitQueue(LinkQueue *Q) +{//初始化一个队列 + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!Q->front)//生成头结点失败 + return OVERFLOW; + Q->front->next = NULL; + return OK; +} +Status DestoryQueue(LinkQueue *Q) +{ //销毁队列 + while (Q->front) + { + Q->rear = Q->front->next;//Q.rear指向Q.front的下一个结点 + free(Q->front);//释放Q.front所指结点 + Q->front = Q->rear;//Q.front指向Q.front的下一个结点 + } + return OK; +} +Status ClearQueue(LinkQueue *Q) +{ //将队列清为空 + DestoryQueue(Q);//销毁队列 + InitQueue(Q);//重新构造队列 + return OK; +} +bool QueueEmpty(LinkQueue Q) +{ //判断队列是否为空 + if (Q.front->next == NULL) + return true; + else return false; +} +int QueueLength(LinkQueue Q) +{ //求队列的长度 + int i = 0;//计数器清0 + QueuePtr p = Q.front;//p指向结点 + while (Q.rear != p)//p所指向的不是尾结点 + { + i++;//计数器加1 + p = p->next; + } + return i; +} +Status GetHead(LinkQueue Q, QElemType *e) +{ + QueuePtr p; + if (Q.front == Q.rear) return ERROR; + p = Q.front->next;//p指向队头结点 + *e = p->data;//将队头元素的值赋给e + return OK; +} +Status EnQueue(LinkQueue *Q, QElemType e) +{ //插入元素e为队列Q的新的队尾元素 + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + //动态生成新结点 + if (!p) + exit(OVERFLOW); + p->data = e;//将e的值赋给新结点 + p->next = NULL;//新结点的指针为空 + Q->rear->next = p;//原队尾结点的指针域为指向新结点 + Q->rear = p;//尾指针指向新结点 + return OK; +} +Status DeQueue(LinkQueue *Q, QElemType *e) +{ //若队列不为空,删除Q的队头元素,用e返回其值 + QueuePtr p; + if (Q->front == Q->rear)//队列为空 + return ERROR; + p = Q->front->next;//p指向队头结点 + *e = p->data;//队头元素赋给e + Q->front->next = p->next;//头结点指向下一个结点 + if (Q->rear == p)//如果删除的队尾结点 + Q->rear = Q->front;//修改队尾指针指向头结点 + free(p); + return OK; +} +Status QueueTraverse(LinkQueue Q, void(*visit)(QElemType)) +{ //对队头到队尾依次对队列中每个元素调用函数visit() + QueuePtr p = Q.front->next; + while (p) + { + visit(p->data);//对p所指元素调用visit() + p = p->next; + } + printf("\n"); + return OK; +} +Status print(QElemType e) +{ + printf("%2d", e); + printf(" "); + return OK; +} + + +int main() +{ + int i, k,s,cha; + QElemType d; + LinkQueue q; + InitQueue(&q);//构造一个空栈 + srand((unsigned)time(NULL)); + s = rand() % 21+1; + for (i = 1; i <= s; i++) + { + EnQueue(&q, rand()%101); + } + printf("构建了一个空栈\n"); + printf("栈的元素为:"); + QueueTraverse(q, print); + k = QueueEmpty(q); + printf("判断栈是否为空:"); + if (k == 0) + printf(" 不为空\n"); + else + printf(" 为空\n"); + k = GetHead(q, &d); + printf("此时队头元素为: %d\n", d); + printf("删除这个队头元素\n"); + DeQueue(&q, &d); + k = GetHead(q, &d); + printf("删除后新的队头元素为:%d\n", d); + printf("此时队列的长度为%d\n", QueueLength(q)); + cha = rand() % 101; + EnQueue(&q, cha); + printf("插入一个新的元素:%d\n", cha); + printf("此时栈的元素为:"); + QueueTraverse(q, print); + ClearQueue(&q); + printf("清空队列后q.front=%u,q.rear=%u,q.front->next=%u", q.front, q.rear, q.front->next); + QueueTraverse(q, print); + k = QueueEmpty(q); + printf("判断栈是否为空:"); + if (k == 0) + printf(" 不为空\n"); + else + printf(" 为空\n"); + DestoryQueue(&q); + printf("销毁队列后,q.front=%u,q.rear=%u\n", q.front, q.rear); +} diff --git a/2017-1/FlyFly-H/1.cpp b/2017-1/FlyFly-H/1.cpp new file mode 100644 index 00000000..ad6d7c28 --- /dev/null +++ b/2017-1/FlyFly-H/1.cpp @@ -0,0 +1,89 @@ +//2.12 + +#include +#include +#include + +typedef struct LNode +{ + int data; + struct LNode *next; +}LNode, *LinkList; + +void CreateList (LinkList *L,int n) +{ + LNode* p; + int data = 20; + int i; + (*L) = (LinkList)malloc(sizeof(LNode)); + (*L)->next = NULL; + srand((unsigned int)time(NULL)); + printf("随机产生数据\n"); + for(i=0;idata=data-=rand()%5+1; //使前一个数据必定比后一个数据大,且两者之差再1到5; + printf("%d ",p->data); + p->next = (*L)->next; + (*L)->next=p; + } + printf("\n"); +} + +void MergeList ( LinkList *La,LinkList *Lb,LinkList *Lc) +{ + LNode *pa,*pb,*pc; + pa = (*La)->next; + pb = (*Lb)->next; + *Lc = pc = *La; + while(pa && pb) + { + if(pa->data <= pb->data) + { + pc ->next = pa; + pc = pa; + pa = pa->next; + } + else + { + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + pc->next = pa ? pa :pb; + if(*Lb != NULL) + free(*Lb); +} + +void PrintList (LinkList Lc ) // 此部分参照了C语言课件进行修改 +{ + LNode *p; + p = Lc->next; + printf("输出合并链表Lc中各结点数据:\n"); + while( p!=NULL ) + { + printf("%d ",p->data) ; + p=p->next; + } + printf("\n"); +} + + +int main() +{ + LinkList La,Lb,Lc; + int a,b; + srand((unsigned int)time(NULL)); + a=10+rand()%5; + printf("创建链表La的长度为%d\n",a); + b=10+rand()%5; + printf("创建链表Lb的长度为%d\n",b); + printf("开始创建La\n"); + CreateList( &La ,a); + printf("开始创建Lb\n"); + CreateList( &Lb ,b); + MergeList( &La, &Lb ,&Lc); + PrintList( Lc); + return 0; +} \ No newline at end of file diff --git "a/2017-1/FlyFly-H/2.12\357\274\210\344\277\256\346\224\271\344\271\213\345\220\216\357\274\211.c" "b/2017-1/FlyFly-H/2.12\357\274\210\344\277\256\346\224\271\344\271\213\345\220\216\357\274\211.c" new file mode 100644 index 00000000..38745595 --- /dev/null +++ "b/2017-1/FlyFly-H/2.12\357\274\210\344\277\256\346\224\271\344\271\213\345\220\216\357\274\211.c" @@ -0,0 +1,89 @@ +#include +#include +#include + +typedef struct LNode +{ + int data; + struct LNode *next; +}LNode, *LinkList; + +void CreateList (LinkList *L,int n) +{ + LNode* p; + int data = 20; + int i; + (*L) = (LinkList)malloc(sizeof(LNode)); + (*L)->next = NULL; + srand((unsigned int)time(NULL)); + printf("随机产生数据\n"); + for(i=0;idata=data-=rand()%5+1; //使前一个数据必定比后一个数据大,且两者之差再1到5; + printf("%d ",p->data); + p->next = (*L)->next; + (*L)->next=p; + } + printf("\n"); +} + +void MergeList ( LinkList *La,LinkList *Lb,LinkList *Lc) +{ + LNode *pa,*pb,*pc; + pa = (*La)->next; + pb = (*Lb)->next; + *Lc = pc = *La; + while(pa && pb) + { + if(pa->data <= pb->data) + { + pc ->next = pa; + pc = pa; + pa = pa->next; + } + else + { + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + pc->next = pa ? pa :pb; + if(*Lb != NULL) + free(*Lb); +} + +void PrintList (LinkList Lc ) // 此部分参照了C语言课件进行修改 +{ + LNode *p; + p = Lc->next; + printf("输出合并链表Lc中各结点数据:\n"); + while( p!=NULL ) + { + printf("%d ",p->data) ; + p=p->next; + } + printf("\n"); +} + + +int main() +{ + LinkList La,Lb,Lc; + int a,b; + //La = (LinkList)malloc(sizeof(LNode)); + //Lb = (LinkList)malloc(sizeof(LNode)); + srand((unsigned int)time(NULL)); + a=10+rand()%5; + printf("创建链表La的长度为%d\n",a); + b=10+rand()%5; + printf("创建链表Lb的长度为%d\n",b); + printf("开始创建La\n"); + CreateList( &La ,a); + printf("开始创建Lb\n"); + CreateList( &Lb ,b); + MergeList( &La, &Lb ,&Lc); + PrintList( Lc); + return 0; +} \ No newline at end of file diff --git "a/2017-1/FlyFly-H/LineEdit/lineedit \346\265\213\350\257\225\347\224\250\344\276\213.txt" "b/2017-1/FlyFly-H/LineEdit/lineedit \346\265\213\350\257\225\347\224\250\344\276\213.txt" new file mode 100644 index 00000000..e7ddf3b6 --- /dev/null +++ "b/2017-1/FlyFly-H/LineEdit/lineedit \346\265\213\350\257\225\347\224\250\344\276\213.txt" @@ -0,0 +1,7 @@ +锘垮疄楠屼笁鐨勬祴璇曠敤渚嬩负锛 + +杈撳叆锛歸hli##ilr#e(s#*s) +杈撳嚭锛歸hile锛*s锛 + +杈撳叆锛歰utchar@puthar(*s=#++锛 +杈撳嚭锛歱utchar(*s++) \ No newline at end of file diff --git a/2017-1/FlyFly-H/LineEdit/lineedit.c b/2017-1/FlyFly-H/LineEdit/lineedit.c new file mode 100644 index 00000000..c7e1f2dc --- /dev/null +++ b/2017-1/FlyFly-H/LineEdit/lineedit.c @@ -0,0 +1,123 @@ +//lineedit + +#include +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配量 +#define STACKINCREMENT 10 //存储空间分配增量 + +typedef enum +{ + OK , + OVERFLOW, + ERROR +} Status; + +typedef struct +{ + char *base; + char *top; + char stacksize; +} SqStack; + +Status InitStack (SqStack *S) //函数功能为构造一个空栈 +{ + S->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char)); + if (!S->base) + { + return (OVERFLOW);//分配失败 + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status Push (SqStack *S,char e) //插入元素e为新的栈顶元素 +{ + if( S->top - S->base >= S->stacksize) //栈满,追加存储空间; + { + S->base = (char *) realloc (S->base,(S->stacksize + STACKINCREMENT) * sizeof(char)); + if(!S->base) + return OVERFLOW; + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *(S->top++) = e; + return OK; +} + +Status Pop (SqStack *S,char* pa) //若栈不为空,删除S的栈顶元素,用e返回其值,并返回OK ;否则返回ERROR; +{ + if(S->top == S->base) + return ERROR; + *pa =*(--S->top); + return OK; +} + +Status ClearStack(SqStack *S) +{ + S->top = S->base; + return OK; +} + +Status DestroyStack(SqStack *S) +{ + free(S->base); + S->top = NULL; + S->base = NULL; + S->stacksize = 0; + return OK; +} + +Status LineEdit (SqStack *S) +{ + char ch,c; + char *p; + ch = getchar(); + while (ch != EOF) + { + while (ch != '\n') + { + switch (ch) + { + case '#': + { + Pop(S,&c); + break; + } + case '@': + { + ClearStack(S); + break; + } + default: + { + Push(S,ch); + break; + } + } + ch = getchar(); + } + p = S->base; + while(p <= S->top) + { + printf("%c",*p); + ++p; + } + ClearStack(S); + if(ch!=EOF) + ch = getchar(); + } + return OK; + } + + + +int main() +{ + SqStack S; + InitStack(&S); + LineEdit(&S);//进行括行编辑 + DestroyStack(&S);//将栈销毁 +} diff --git "a/2017-1/FlyFly-H/QQ\345\233\276\347\211\20720170321221024.png" "b/2017-1/FlyFly-H/QQ\345\233\276\347\211\20720170321221024.png" new file mode 100644 index 00000000..5104c59b Binary files /dev/null and "b/2017-1/FlyFly-H/QQ\345\233\276\347\211\20720170321221024.png" differ diff --git a/2017-1/FlyFly-H/conversion.c b/2017-1/FlyFly-H/conversion.c new file mode 100644 index 00000000..d1207396 --- /dev/null +++ b/2017-1/FlyFly-H/conversion.c @@ -0,0 +1,104 @@ +//数制转换 + + +#include +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配量 +#define STACKINCREMENT 10 //存储空间分配增量 + +typedef enum +{ + OK , + OVERFLOW, + ERROR +} Status; + +typedef enum +{ + false, + true +}bool; + +typedef struct +{ + int *base; + int *top; + int stacksize; +} SqStack; + +Status InitStack (SqStack *S) //函数功能为构造一个空栈 +{ + S->base = (int *)malloc(STACK_INIT_SIZE * sizeof(int)); + if (!S->base) + { + return (OVERFLOW);//分配失败 + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status Push (SqStack *S,int e) //插入元素e为新的栈顶元素 +{ + if( S->top - S->base >= S->stacksize) //栈满,追加存储空间; + { + S->base = (int *) realloc (S->base,(S->stacksize + STACKINCREMENT) * sizeof(int)); + if(!S->base) + return OVERFLOW; + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *(S->top++) = e; + return OK; +} + +Status Pop (SqStack *S,int* pa) //若栈不为空,删除S的栈顶元素,用e返回其值,并返回OK ;否则返回ERROR; +{ + if(S->top == S->base) + return ERROR; + *pa =*(--S->top); + return OK; +} + +bool StackEmpty(SqStack *S) +{ + if(S->top == S->base) + return true; + else + return false; +} + +void conversion (SqStack *S,int input,int d) +{ + int e; + while (input) + { + Push( S,input % d); + input = input/d; + } + while (!StackEmpty(S)) + { + Pop(S,&e); + printf("%d",e); + } +} + +int main() +{ + int n; + int d; + SqStack S; + srand( (unsigned int)time(NULL)); + n = rand()%1024; + printf("随机生成检测数字为:"); + printf("%d\n",n); + printf("随机生成进制数为:\n"); + d = rand()%17; + printf("%d\n",d); + InitStack(&S); //构造一个空栈; + conversion(&S,n,d); +} + + diff --git a/2017-1/FlyFly-H/matching.c b/2017-1/FlyFly-H/matching.c new file mode 100644 index 00000000..0f63fa5f --- /dev/null +++ b/2017-1/FlyFly-H/matching.c @@ -0,0 +1,164 @@ +//matching + + +#include +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配量 +#define STACKINCREMENT 10 //存储空间分配增量 + +typedef enum +{ + OK , + OVERFLOW, + ERROR +} Status; + +typedef struct +{ + char *base; + char *top; + char stacksize; +} SqStack; + +typedef enum +{ + false, + true +}bool; + + +Status InitStack (SqStack *S) //函数功能为构造一个空栈 +{ + S->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char)); + if (!S->base) + { + return (OVERFLOW);//分配失败 + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status Push (SqStack *S,char e) //插入元素e为新的栈顶元素 +{ + if( S->top - S->base >= S->stacksize) //栈满,追加存储空间; + { + S->base = (char *) realloc (S->base,(S->stacksize + STACKINCREMENT) * sizeof(char)); + if(!S->base) + return OVERFLOW; + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *(S->top++) = e; + return OK; +} + +Status Pop (SqStack *S,char* pa) //若栈不为空,删除S的栈顶元素,用e返回其值,并返回OK ;否则返回ERROR; +{ + if(S->top == S->base) + return ERROR; + *pa =*(--S->top); + return OK; +} + +bool StackEmpty(SqStack *S) +{ + if(S->top == S->base) + return true; + else + return false; +} + +Status GetTop(SqStack *S,char *e) +{ + if(S->top == S->base) + return ERROR; + *e = *(S->top - 1); + return OK; +} + +Status matching (char *e,SqStack *S) +{ + int state = 1; + int i=0; + char z; + while ( e[i] != '\0') + { + switch (e[i]) + { + case '(': + case '{': + case '[': + { + Push(S, e[i]); + i++; + break ; + } + case ')': + { + GetTop(S,&z); + if(!StackEmpty(S) && z == '(') + { + Pop(S,&z); + i++; + } + else + state = 0; + break; + } + case '}': + { + GetTop(S,&z); + if(!StackEmpty(S) && z == '{') + { + Pop(S,&z); + i++; + } + else + state = 0; + break; + } + case ']': + { + GetTop(S,&z); + if(!StackEmpty(S) && z == '[') + { + Pop(S,&z); + i++; + } + else + state = 0; + break; + } + } + } + if(StackEmpty(S) && state)//空栈且state不为0则全部匹配 + { + printf("括号全部匹配\n"); + return OK; + } + else + { + printf("括号不匹配\n"); + return ERROR; + } +} + + +int main() +{ + char test[]="(){}[]"; + char hhhh[]="({}{"; + SqStack S,B; + InitStack(&S); + InitStack(&B); + printf("测试用例:"); + puts(test); + matching(test,&S); + printf("测试用例:"); + puts(hhhh); + matching(hhhh,&B); + return 0; +} \ No newline at end of file diff --git "a/2017-1/FlyFly-H/\344\272\214\345\217\211\346\216\222\345\210\227\346\225\260\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/1.c" "b/2017-1/FlyFly-H/\344\272\214\345\217\211\346\216\222\345\210\227\346\225\260\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/1.c" new file mode 100644 index 00000000..0706b120 --- /dev/null +++ "b/2017-1/FlyFly-H/\344\272\214\345\217\211\346\216\222\345\210\227\346\225\260\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/1.c" @@ -0,0 +1,226 @@ +锘#include +#include +int flag; + +typedef enum +{ + true, + false +}bool; + +typedef enum status +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef struct BiTNode +{ + int data; + struct BiTNode *lchild, *rchild; +} BiTNode, *BiTree; + +bool SearchBST(BiTree T, int key, BiTree f, BiTree *p) +{ + if (!T) + { + *p = f; + return false; + } + else + { + if (key == T->data) + { + p = &T; + return true; + } + else if (key > T->data) + { + return SearchBST(T->rchild, key, T, p); + } + else + { + return SearchBST(T->lchild, key, T, p); + } + } +}; + + + +//鍚戜簩鍙夋帓搴忔爲鎻掑叆涓涓柊鐨勭粨鐐 +bool InsertBST(BiTree *T, int e) +{ + if (*T == NULL) + { + *T = (BiTree)malloc(sizeof(BiTNode)); + (*T)->data = e; + (*T)->lchild = (*T)->rchild = NULL;; + return true; + } + if (e == (*T)->data) + { + return false; + } + if (e < (*T)->data) + { + return InsertBST(&(*T)->lchild, e); + } + if (e > (*T)->data) + { + return InsertBST(&(*T)->rchild, e); + } +}; + +//寤虹珛BST +Status CreatBST(BiTree *T, int a[], int n) +{ + int i; + *T = NULL; + for (i = 0; i < n; i++) + { + InsertBST(&(*T), a[i]); + } + return OK; +}; + +bool Delete(BiTree *T) +{ + BiTree L, S; + if (!(*T)->lchild && !(*T)->rchild) + { + *T = NULL; + } + else if (!(*T)->lchild) + { + L = *T; + *T = (*T)->rchild; + free(L); + } + else if (!(*T)->rchild) + { + L = *T; + *T = (*T)->lchild; + free(L); + } + else + { + L = *T; + S = (*T)->lchild; + while (S->rchild) + { + L = S; + S = S->rchild; + } + (*T)->data = S->data; + if (L != *T) + { + L->rchild = S->lchild; + } + else + { + L->lchild = S->lchild; + } + free(S); + } + return true; +}; + +bool DeleteBST(BiTree *T, int key) +{ + if (!T) + { + return false; + } + else + { + if (key == (*T)->data) + { + Delete(T); + return true; + } + else if (key < (*T)->data) + { + return DeleteBST(&(*T)->lchild, key); + } + else + { + return DeleteBST(&(*T)->rchild, key); + } + } +}; + +Status print(int data, FILE*pfile) +{ + + char *d = ", "; + if (NULL == pfile) + { + return ERROR; + } + if (NULL != pfile) + { + if (flag == 1) + { + fwrite(d, sizeof(d), 1, pfile); + } + fprintf(pfile, "%d", data); + flag = 1; + + } +} + +void preOrderTraverse(BiTree T, FILE*pfile) +{ + if (T) + { + print(T->data, pfile); + preOrderTraverse(T->lchild, pfile); + preOrderTraverse(T->rchild, pfile); + } +}; + +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 = 12, i; + bool m; + char d = '\n'; + FILE *pfile; + BiTree T, f, p; + T = (BiTree)malloc(sizeof(BiTNode)); + p = (BiTree)malloc(sizeof(BiTree)); + f = (BiTree)malloc(sizeof(BiTree)); + pfile = fopen("output.txt", "a"); + CreatBST(&T, a, n); + preOrderTraverse(T, pfile); + fwrite(&d, sizeof(d), 1, pfile); + for (i = 0; i < 5; i++) + { + m = SearchBST(T, b[i], f, &p); + //printf("鐜板湪鏌ユ壘%d: ",b[i]); + if (m == true) + { + //printf("鎵惧埌%d,鍦ㄤ簩鍙夋帓鍒楁爲涓垹闄%d\n",b[i],b[i]); + DeleteBST(&T, b[i]); + } + else + { + //printf("鏈壘鍒%d,鍦ㄤ簩鍙夋帓鍒楁爲涓彃鍏%d\n",b[i],b[i]); + InsertBST(&T, b[i]); + } + //printf("鏌ユ壘杩%d鐨勪簩鍙夋帓鍒楁爲涓猴細",b[i]); + flag = 0; + preOrderTraverse(T, pfile); + fwrite(&d, sizeof(d), 1, pfile); + } + return 0; +} + + + + + + + diff --git "a/2017-1/FlyFly-H/\344\272\214\345\217\211\346\216\222\345\210\227\346\225\260\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/output.txt" "b/2017-1/FlyFly-H/\344\272\214\345\217\211\346\216\222\345\210\227\346\225\260\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/output.txt" new file mode 100644 index 00000000..e69de29b diff --git "a/2017-1/FlyFly-H/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/1.c" "b/2017-1/FlyFly-H/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/1.c" new file mode 100644 index 00000000..819212a2 --- /dev/null +++ "b/2017-1/FlyFly-H/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/1.c" @@ -0,0 +1,250 @@ +锘#include +#include +int flag ; + +typedef enum +{ + true, + false +}bool; + +typedef enum status +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef struct BiTNode +{ + int data; + struct BiTNode *lchild,*rchild; +} BiTNode,*BiTree; + +bool SearchBST(BiTree T,int key,BiTree f,BiTree *p) +{ + if( !T ) + { + *p = f; + return false; + } + else + { + if(key == T->data) + { + p = &T; + return true; + } + else if( key > T->data) + { + return SearchBST( T->rchild,key,T,p); + } + else + { + return SearchBST( T->lchild,key,T,p); + } + } +}; + + + +//鍚戜簩鍙夋帓搴忔爲鎻掑叆涓涓柊鐨勭粨鐐 +bool InsertBST (BiTree *T,int e) +{ + if( *T == NULL) + { + *T = (BiTree)malloc(sizeof(BiTNode)); + (*T)->data = e; + (*T)->lchild = (*T)->rchild = NULL;; + return true; + } + if(e == (*T)->data) + { + return false; + } + if(e < (*T)->data) + { + return InsertBST(&(*T)->lchild,e); + } + if(e > (*T)->data) + { + return InsertBST(&(*T)->rchild,e); + } +}; + +//寤虹珛BST +Status CreatBST(BiTree *T,int a[],int n) +{ + int i; + *T = NULL; + for(i = 0; i < n; i++) + { + InsertBST(&(*T),a[i]); + } + return OK; +}; + +bool Delete(BiTree *T) +{ + BiTree L,S; + if(!(*T)->lchild && !(*T)->rchild) + { + *T = NULL; + } + else if(!(*T)->lchild) + { + L = *T; + *T = (*T)->rchild; + free(L); + } + else if(!(*T)->rchild) + { + L = *T; + *T = (*T)->lchild; + free(L); + } + else + { + L = *T; + S = (*T)->lchild; + while(S->rchild) + { + L = S; + S = S->rchild; + } + (*T)->data = S->data; + if( L != *T ) + { + L->rchild = S->lchild; + } + else + { + L->lchild = S->lchild; + } + free(S); + } + return true; +}; + +bool DeleteBST(BiTree *T,int key) +{ + if(!T) + { + return false; + } + else + { + if(key == (*T)->data) + { + Delete(T); + return true; + } + else if(key<(*T)->data) + { + return DeleteBST(&(*T)->lchild,key); + } + else + { + return DeleteBST(&(*T)->rchild,key); + } + } +}; + +Status print(int data,FILE*pfile) +{ + + char *d = ", "; + if (NULL == pfile) + { + return ERROR; + } + if (NULL != pfile) + { + if (flag == 1) + { + fwrite(d, sizeof(d), 1, pfile); + } + fprintf(pfile, "%d",data); + flag = 1; + + } +} + +void preOrderTraverse(BiTree T,FILE*pfile) +{ + if(T) + { + print(T->data,pfile); + preOrderTraverse(T->lchild,pfile); + preOrderTraverse(T->rchild,pfile); + } +}; + +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 = 12,i; + bool m; + char d = '\n'; + FILE *pfile; + BiTree T,f,p; + T = (BiTree)malloc(sizeof(BiTNode)); + p = (BiTree)malloc(sizeof(BiTree)); + f = (BiTree)malloc(sizeof(BiTree)); + pfile = fopen("output.txt", "a"); + CreatBST(&T,a,n); + preOrderTraverse(T,pfile); + fwrite(&d, sizeof(d), 1, pfile); + for(i = 0;i < 5;i++) + { + m = SearchBST(T,b[i],f,&p); + //printf("鐜板湪鏌ユ壘%d: ",b[i]); + if( m == true ) + { + //printf("鎵惧埌%d,鍦ㄤ簩鍙夋帓鍒楁爲涓垹闄%d\n",b[i],b[i]); + DeleteBST(&T, b[i]); + } + else + { + //printf("鏈壘鍒%d,鍦ㄤ簩鍙夋帓鍒楁爲涓彃鍏%d\n",b[i],b[i]); + InsertBST(&T,b[i]); + } + //printf("鏌ユ壘杩%d鐨勪簩鍙夋帓鍒楁爲涓猴細",b[i]); + flag = 0; + preOrderTraverse(T,pfile); + fwrite(&d, sizeof(d), 1, pfile); + } + return 0; +} + + + + + + + + //BiTree p, s; + //if( !SearchBST( *T, e, NULL, &p ) ) + //{ + // s = (BiTree)malloc(sizeof(BiTNode)); + // s->data = e; + // s->lchild = s->rchild = NULL; + // if( !p ) + // { + // *T = s;/*聽鎻掑叆s涓烘牴鑺傜偣锛屾鍓嶆爲涓虹┖鏍懧*/ + // } + // else if( e > p->data ) + // { + // p->rchild = s;/*聽鎻掑叆s涓哄彸瀛╁瓙聽*/ + // } + // else if( e < p->data) + // { + // p->lchild = s;/*聽鎻掑叆s涓哄乏瀛╁瓙聽*/ + // } + // return true; + //} + // else + // { + // return false; + // } \ No newline at end of file diff --git "a/2017-1/FlyFly-H/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/output.txt" "b/2017-1/FlyFly-H/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/output.txt" new file mode 100644 index 00000000..e69de29b diff --git "a/2017-1/FlyFly-H/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\347\232\204\344\270\244\347\202\271\344\271\213\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204/1.c" "b/2017-1/FlyFly-H/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\347\232\204\344\270\244\347\202\271\344\271\213\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204/1.c" new file mode 100644 index 00000000..184e0dc0 --- /dev/null +++ "b/2017-1/FlyFly-H/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\347\232\204\344\270\244\347\202\271\344\271\213\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204/1.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; //鏃犲悜鍥剧殑閭绘帴鐭╅樀鏄绉扮殑锛屼负鏃犲悜鍥鹃《鐐硅祴鍊硷紝璧嬪间负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鎿嶄綔涓洪噸澶嶇殑锛岀敤涓涓嚱鏁版潵鍖呰杩欎釜鎿嶄綔 + 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浠巎+1寮濮嬶紝涓嬩竴涓负1鐨勫氨鏄畠鐨勪笅涓涓偦鎺ョ偣 + { + return k; + } + } + return -1; +} +//骞垮害浼樺厛姹傝矾寰 +void ShortestPath(Graph G,int a,int b) +{ + int u,v,w; + bool flag = false;//鐢╢lag鏉ヨ繘琛岄鍑簑hile寰幆鐨勫垽鏂紝鑻ヤ负true鍒欓鍑簑hile + LinkQueue Q; + for(v = 0; v < G.vexnum; ++v) + { + visited[v] = false; //鍏堝垵濮嬪寲璁块棶鏍囧織鏁扮粍涓篎ALSE + } + InitQueue(&Q);//鍒濆鍖栦竴涓槦鍒楋紝鏉ュ瓨鍌ㄥ凡琚闂殑璺緞闀垮害涓1锛2锛屻傘傘傜殑椤剁偣锛屽嵆瀛樺偍鏈鐭矾寰勭殑椤剁偣 + EnQueue(&Q,a);//灏哸杩涘叆闃熷垪 + visited[a] = true;//璁块棶浜哸,鍒欏皢瀹冭祴鍊间负TRUE锛岃〃绀哄凡缁忚璁块棶 + while (!QueueEmpty(Q))//闃熷垪涓嶄负绌 + { + DeQueue(&Q,&u);//姝ゅ嚱鏁板嚭闃熷垪鏃讹紝浠呯Щ鍔ㄩ槦澶存寚閽堬紝鑰屼笉灏嗛槦澶寸粨鐐逛粠閾捐〃涓垹闄 + for(w = FirstAdjVex(G,u);w >=0;w = NextAdjVex(G, u, w)) //w涓簎鐨勯偦鎺ョ偣锛岀洿鍒伴亶鍘嗗埌b鏃秄or寰幆鍋滄 + { + if(w == b)//鑻=b锛屽垯璇存槑鏈灏忚矾寰勫凡缁忔壘鍒 + { + EnQueue(&Q,w);//鎶婃渶鍚庝竴涓粨鐐硅繘鍏ラ槦鍒 + PrintFoot(Q,a);//鍙互杈撳嚭璺緞浜 + flag = true; + } + if(!visited[w])//鑻鐨勯偦鎺ョ偣娌℃湁琚闂 + { + EnQueue(&Q,w);//璁﹚杩涘叆闃熷垪 + visited[w] = true; + } + } + + if(flag) + { + break;//璺冲嚭while寰幆 + } + } +} + +//杈撳嚭璺緞 +Status PrintFoot(LinkQueue Q,int start) +{ + int foot[MAX_VERTEX_NUM];//MAX_VERTEX_NUM鍙互鍦ㄨ繖涓棰樹腑鍙互鎹㈡垚9锛屼絾涓轰簡鍑芥暟鐨勯氱敤鎬э紝鐢∕AX_VERTEX_NUM鏇村ソ + int i; //foot鏁扮粍鏉ュ瓨鍌ㄨ矾寰 + QueuePtr p; + p = Q.rear;//p鏄槦灏剧粨鐐 + for(i=0;i < MAX_VERTEX_NUM; i++) + { + foot[i] = -1;//鍒濆鍖杅oot鏁扮粍 + } + 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; //娌℃湁free锛坋锛 + 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; +} + diff --git "a/2017-1/FlyFly-H/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\347\232\204\344\270\244\347\202\271\344\271\213\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204/1plus.c" "b/2017-1/FlyFly-H/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\347\232\204\344\270\244\347\202\271\344\271\213\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204/1plus.c" new file mode 100644 index 00000000..469b472b --- /dev/null +++ "b/2017-1/FlyFly-H/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\347\232\204\344\270\244\347\202\271\344\271\213\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204/1plus.c" @@ -0,0 +1,242 @@ +锘#include +#include +#define INFINITY 99999 //鏈澶у兼棤绌 +#define MAX_VERTEX_NUM 20 //鏈澶ч《鐐逛釜鏁 +#define MAXQSIZE 100 + +//-----鍥剧殑鏁扮粍瀛樺偍琛ㄧず-------- +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; + +//-------------闃熷垪------------ +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; //鏃犲悜鍥剧殑閭绘帴鐭╅樀鏄绉扮殑锛屼负鏃犲悜鍥鹃《鐐硅祴鍊硷紝璧嬪间负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鎿嶄綔涓洪噸澶嶇殑锛岀敤涓涓嚱鏁版潵鍖呰杩欎釜鎿嶄綔 + 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浠巎+1寮濮嬶紝涓嬩竴涓负1鐨勫氨鏄畠鐨勪笅涓涓偦鎺ョ偣 + { + return k; + } + } + return -1; +} +//骞垮害浼樺厛姹傝矾寰 +void ShortestPath(Graph G, int a, int b) +{ + int u, v, w; + bool flag = false;//鐢╢lag鏉ヨ繘琛岄鍑簑hile寰幆鐨勫垽鏂紝鑻ヤ负true鍒欓鍑簑hile + LinkQueue Q; + for (v = 0; v < G.vexnum; ++v) + { + visited[v] = false; //鍏堝垵濮嬪寲璁块棶鏍囧織鏁扮粍涓篎ALSE + } + InitQueue(&Q);//鍒濆鍖栦竴涓槦鍒楋紝鏉ュ瓨鍌ㄥ凡琚闂殑璺緞闀垮害涓1锛2锛屻傘傘傜殑椤剁偣锛屽嵆瀛樺偍鏈鐭矾寰勭殑椤剁偣 + EnQueue(&Q, a);//灏哸杩涘叆闃熷垪 + visited[a] = true;//璁块棶浜哸,鍒欏皢瀹冭祴鍊间负TRUE锛岃〃绀哄凡缁忚璁块棶 + while (!QueueEmpty(Q))//闃熷垪涓嶄负绌 + { + DeQueue(&Q, &u);//姝ゅ嚱鏁板嚭闃熷垪鏃讹紝浠呯Щ鍔ㄩ槦澶存寚閽堬紝鑰屼笉灏嗛槦澶寸粨鐐逛粠閾捐〃涓垹闄 + for (w = FirstAdjVex(G, u); w >= 0; w = NextAdjVex(G, u, w)) //w涓簎鐨勯偦鎺ョ偣锛岀洿鍒伴亶鍘嗗埌b鏃秄or寰幆鍋滄 + { + if (w == b)//鑻=b锛屽垯璇存槑鏈灏忚矾寰勫凡缁忔壘鍒 + { + EnQueue(&Q, w);//鎶婃渶鍚庝竴涓粨鐐硅繘鍏ラ槦鍒 + PrintFoot(Q, a);//鍙互杈撳嚭璺緞浜 + flag = true; + } + if (!visited[w])//鑻鐨勯偦鎺ョ偣娌℃湁琚闂 + { + EnQueue(&Q, w);//璁﹚杩涘叆闃熷垪 + visited[w] = true; + } + } + + if (flag) + { + break;//璺冲嚭while寰幆 + } + } +} + +//杈撳嚭璺緞 +Status PrintFoot(LinkQueue Q, int start) +{ + int foot[MAX_VERTEX_NUM];//MAX_VERTEX_NUM鍙互鍦ㄨ繖涓棰樹腑鍙互鎹㈡垚9锛屼絾涓轰簡鍑芥暟鐨勯氱敤鎬э紝鐢∕AX_VERTEX_NUM鏇村ソ + int i; //foot鏁扮粍鏉ュ瓨鍌ㄨ矾寰 + QueuePtr p; + p = Q.rear;//p鏄槦灏剧粨鐐 + for (i = 0; i < MAX_VERTEX_NUM; i++) + { + foot[i] = -1;//鍒濆鍖杅oot鏁扮粍 + } + 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; //娌℃湁free锛坋锛 + return OK; +} +int main() +{ + int i, j; + Status a; + Graph h; + a = OK; + CreateGraph(&h);//鏋勫缓涓涓棤鍚戝浘锛屽苟鐢ㄩ偦鎺ョ煩闃靛垵濮嬪寲鍥 + if(a == CreateGraph(&h)) + { + printf("鏃犲悜鍥惧缓绔嬫垚鍔焅n"); + } + printf("\n杈撳嚭浠绘剰涓ょ偣涔嬮棿鐨勬渶灏忚矾寰:\n\n"); + 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; +} + diff --git "a/2017-1/FlyFly-H/\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/1.c" "b/2017-1/FlyFly-H/\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/1.c" new file mode 100644 index 00000000..18707c08 --- /dev/null +++ "b/2017-1/FlyFly-H/\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/1.c" @@ -0,0 +1,79 @@ +#include +#include + +typedef enum status +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef char TElemType; + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild,*rchild; +}BiTNode,*BiTree; + + int i = 0; +Status CreateBiTree(BiTree *T,TElemType * p) +{ + + TElemType ch; + ch = p[i]; + i++; + + if(ch == '*') + { + *T = NULL; + } + else + { + if(!(*T = (BiTree)malloc(sizeof(BiTNode)))) + { + return OVERFLOW; + } + (*T)->data = ch; + CreateBiTree(&(*T)->lchild,p); + CreateBiTree(&(*T)->rchild,p); + } + return OK; +} + +void PostOrderTraverseTree(BiTree T) +{ + if(T) + { + PostOrderTraverseTree(T->lchild); + PostOrderTraverseTree(T->rchild); + printf("%c",T->data); + } +} + + +int main() +{ + BiTree T = NULL; + BiTree B = NULL; + + TElemType str1[30] = "ABDG***EH**I*K**C*F**"; + TElemType str2[30] = "ABD*F***CE***"; + + printf("以先序的方式建立二叉树:ABDG***EH**I*K**C*F**\n"); + CreateBiTree(&T,str1); + printf("二叉树建立成功\n"); + printf("以后序的方式输出二叉树:\n"); + PostOrderTraverseTree(T); + + printf("\n\n"); + + i=0; + printf("以先序的方式建立二叉树:ABD*F***CE***\n"); + CreateBiTree(&T,str2); + printf("二叉树建立成功\n"); + printf("以后序的方式输出二叉树:\n"); + PostOrderTraverseTree(T); + printf("\n"); + return 0; +} diff --git "a/2017-1/FlyFly-H/\346\213\206\345\210\206\351\223\276\350\241\250/1.c" "b/2017-1/FlyFly-H/\346\213\206\345\210\206\351\223\276\350\241\250/1.c" new file mode 100644 index 00000000..bf54b503 --- /dev/null +++ "b/2017-1/FlyFly-H/\346\213\206\345\210\206\351\223\276\350\241\250/1.c" @@ -0,0 +1,104 @@ +#include +#include +#include + +typedef struct LNode +{ + int data; + struct LNode *next; +}LNode,*LinkList; + +void CreateList (LinkList *L,int n) +{ + LNode* p = *L; + int i; + (*L) = (LinkList)malloc(sizeof(LNode)); + (*L)->next = NULL; + srand((unsigned int)time(NULL)); + printf("随机产生数据\n"); + for(i=0;idata=rand()%25+1; + p->next = (*L)->next; + (*L)->next=p; + } +} + +void PrintList (LinkList Lc ) // 此部分参照了C语言课件进行修改 +{ + LNode *p; + p = Lc->next; + while( p!=NULL ) + { + printf("%d ",p->data) ; + p=p->next; + if(p == Lc->next) + { + printf("\n"); + break; + } + } + printf("\n"); +} + +void split(LinkList list,LinkList *list1,LinkList *list2) +{ + LNode *p,*la,*lb; + int count = 1; + *list1 = (LNode *)malloc (sizeof(LNode)); + *list2 = (LNode *)malloc (sizeof(LNode)); + + p = list; + la = *list1; + lb = *list2; + (*list1)->data = 0; + (*list2)->data = 0; + (*list1)->next = NULL; + //(*list2)->next = NULL; + p=p->next; + + while(p) + { + int a = count%2; + if(a) + { + (*list1)->data = p->data; + la->next = p; + la = p; + p = p->next; + count++; + } + else + { + (*list2)->data = p->data; + lb->next = p; + lb = p; + p = p->next; + count++; + } + } + + la->next = (*list1)->next; + lb->next = (*list2)->next; + +} + + +int main() +{ + LinkList list,list1,list2; + int a; + srand((unsigned int)time(NULL)); + a=15+rand()%5; + printf("创建链表List的长度为%d\n",a); + printf("开始创建List\n"); + CreateList( &list ,a); + PrintList(list); + split( list, &list1 ,&list2); + printf("输出list1的数据\n"); + PrintList( list1); + printf("输出list2的数据\n"); + PrintList( list2); + return 0; +} diff --git "a/2017-1/FlyFly-H/\346\213\206\350\247\243\351\223\276\350\241\250/1.c" "b/2017-1/FlyFly-H/\346\213\206\350\247\243\351\223\276\350\241\250/1.c" new file mode 100644 index 00000000..bf54b503 --- /dev/null +++ "b/2017-1/FlyFly-H/\346\213\206\350\247\243\351\223\276\350\241\250/1.c" @@ -0,0 +1,104 @@ +#include +#include +#include + +typedef struct LNode +{ + int data; + struct LNode *next; +}LNode,*LinkList; + +void CreateList (LinkList *L,int n) +{ + LNode* p = *L; + int i; + (*L) = (LinkList)malloc(sizeof(LNode)); + (*L)->next = NULL; + srand((unsigned int)time(NULL)); + printf("随机产生数据\n"); + for(i=0;idata=rand()%25+1; + p->next = (*L)->next; + (*L)->next=p; + } +} + +void PrintList (LinkList Lc ) // 此部分参照了C语言课件进行修改 +{ + LNode *p; + p = Lc->next; + while( p!=NULL ) + { + printf("%d ",p->data) ; + p=p->next; + if(p == Lc->next) + { + printf("\n"); + break; + } + } + printf("\n"); +} + +void split(LinkList list,LinkList *list1,LinkList *list2) +{ + LNode *p,*la,*lb; + int count = 1; + *list1 = (LNode *)malloc (sizeof(LNode)); + *list2 = (LNode *)malloc (sizeof(LNode)); + + p = list; + la = *list1; + lb = *list2; + (*list1)->data = 0; + (*list2)->data = 0; + (*list1)->next = NULL; + //(*list2)->next = NULL; + p=p->next; + + while(p) + { + int a = count%2; + if(a) + { + (*list1)->data = p->data; + la->next = p; + la = p; + p = p->next; + count++; + } + else + { + (*list2)->data = p->data; + lb->next = p; + lb = p; + p = p->next; + count++; + } + } + + la->next = (*list1)->next; + lb->next = (*list2)->next; + +} + + +int main() +{ + LinkList list,list1,list2; + int a; + srand((unsigned int)time(NULL)); + a=15+rand()%5; + printf("创建链表List的长度为%d\n",a); + printf("开始创建List\n"); + CreateList( &list ,a); + PrintList(list); + split( list, &list1 ,&list2); + printf("输出list1的数据\n"); + PrintList( list1); + printf("输出list2的数据\n"); + PrintList( list2); + return 0; +} diff --git "a/2017-1/FlyFly-H/\346\213\254\345\217\267\345\214\271\351\205\215\347\232\204\346\243\200\351\252\214.c" "b/2017-1/FlyFly-H/\346\213\254\345\217\267\345\214\271\351\205\215\347\232\204\346\243\200\351\252\214.c" new file mode 100644 index 00000000..7c10a440 --- /dev/null +++ "b/2017-1/FlyFly-H/\346\213\254\345\217\267\345\214\271\351\205\215\347\232\204\346\243\200\351\252\214.c" @@ -0,0 +1,161 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配量 +#define STACKINCREMENT 10 //存储空间分配增量 + +typedef enum +{ + OK , + OVERFLOW, + ERROR +} Status; + +typedef struct +{ + char *base; + char *top; + char stacksize; +} SqStack; + +typedef enum +{ + false, + true +}bool; + + +Status InitStack (SqStack *S) //函数功能为构造一个空栈 +{ + S->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char)); + if (!S->base) + { + return (OVERFLOW);//分配失败 + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status Push (SqStack *S,char e) //插入元素e为新的栈顶元素 +{ + if( S->top - S->base >= S->stacksize) //栈满,追加存储空间; + { + S->base = (char *) realloc (S->base,(S->stacksize + STACKINCREMENT) * sizeof(char)); + if(!S->base) + return OVERFLOW; + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *(S->top++) = e; + return OK; +} + +Status Pop (SqStack *S,char* pa) //若栈不为空,删除S的栈顶元素,用e返回其值,并返回OK ;否则返回ERROR; +{ + if(S->top == S->base) + return ERROR; + *pa =*(--S->top); + return OK; +} + +bool StackEmpty(SqStack *S) +{ + if(S->top == S->base) + return true; + else + return false; +} + +Status GetTop(SqStack *S,char *e) +{ + if(S->top == S->base) + return ERROR; + *e = *(S->top - 1); + return OK; +} + +Status matching (char *e,SqStack *S) +{ + int state = 1; + int i=0; + char z; + while ( e[i] != '\0') + { + switch (e[i]) + { + case '(': + case '{': + case '[': + { + Push(S, e[i]); + i++; + break ; + } + case ')': + { + GetTop(S,&z); + if(!StackEmpty(S) && z == '(') + { + Pop(S,&z); + i++; + } + else + state = 0; + break; + } + case '}': + { + GetTop(S,&z); + if(!StackEmpty(S) && z == '{') + { + Pop(S,&z); + i++; + } + else + state = 0; + break; + } + case ']': + { + GetTop(S,&z); + if(!StackEmpty(S) && z == '[') + { + Pop(S,&z); + i++; + } + else + state = 0; + break; + } + } + } + if(StackEmpty(S) && state)//空栈且state不为0则全部匹配 + { + printf("括号全部匹配\n"); + return OK; + } + else + { + printf("括号不匹配\n"); + return ERROR; + } +} + + +int main() +{ + char test[]="(){}[]"; + char hhhh[]="({}{"; + SqStack S,B; + InitStack(&S); + InitStack(&B); + printf("测试用例:"); + puts(test); + matching(test,&S); + printf("测试用例:"); + puts(hhhh); + matching(hhhh,&B); + return 0; +} \ No newline at end of file diff --git "a/2017-1/FlyFly-H/\346\216\222\345\272\217/1.c" "b/2017-1/FlyFly-H/\346\216\222\345\272\217/1.c" new file mode 100644 index 00000000..72b975a4 --- /dev/null +++ "b/2017-1/FlyFly-H/\346\216\222\345\272\217/1.c" @@ -0,0 +1,241 @@ +锘#include +#include +#include + +#define maxsize 5 +typedef enum +{ + false, + true +}bool; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef struct +{ + int r[maxsize]; + int length; +}SqList; + +Status output(SqList a) +{ + int i; + for(i = 0;i < maxsize;i++) + { + printf("%d ",a.r[i]); + } + printf("\n"); + return OK; +}; + +Status Initarray(SqList* a,SqList* b,int* comtimes,int* movetimes) +{ + int i; + for(i = 0;i < maxsize;i++) + { + b->r[i] = a->r[i] ; + } + *comtimes = 0; + *movetimes = 0; + return OK; +}; + +Status InsertSort(SqList* L,int* comtimes,int* movetimes) +{ + int j,i; + int temp; + for(i = 1;i < L->length;i++) + { + if(L->r[i] < L->r[i-1]) + { + j = i-1; + (*comtimes)++; + temp = L->r[i]; + L->r[i] = L->r[i-1]; + (*movetimes)++; + while(temp < L->r[j]) + { + L->r[j+1] = L->r[j]; + (*movetimes)++; + j--; + } + + L->r[j+1] = temp; + (*comtimes)++; + } + } + return OK; +}; + + +Status ShellSort(SqList* L,int *comtimes,int* movetimes) +{ + int j, gap,temp,k; + for(gap = L->length/2; gap > 0;gap /= 2) + for(j = gap;j < L->length;j++)//浠庢暟缁勭gap涓厓绱犲紑濮 + { + if(L->r[j] < L->r[j-gap])//姣忎釜鍏冪礌涓庤嚜宸辩粍鍐呯殑鏁版嵁杩涜鐩存帴鎻掑叆鎺掑簭 + { + (*comtimes)++; + temp = L->r[j]; + k = j-gap; + while(k >= 0 && L->r[k] > temp) + { + L->r[k+gap] = L->r[k]; + k -= gap; + (*movetimes)++; + } + L->r[k+gap] = temp; + } + } + return OK; +}; + +Status BubbleSort(SqList* L,int* comtimes,int* movetimes) +{ + int temp,i,j; + for(i = 0;i < L->length;i++) + { + for(j = 0;j < L->length-i-1;j++) + { + if(L->r[j] > L->r[j+1]) + { + (*comtimes)++; + temp = L->r[j]; + L->r[j] = L->r[j+1]; + L->r[j+1] = temp; + (*movetimes) += 3; + } + } + } + return OK; +} +int QKPass(SqList *L, int left, int right, int* comtimes,int* movetimes) +{ + int key = L->r[left]; + int low = left; + int high = right; + while (low < high) + { + while (low < high&&L->r[high] >= key&&(*comtimes)++) + { + high--; + } + L->r[low] = L->r[high]; + ++(*movetimes); + while (low < high&&L->r[low] < key && ((*comtimes)++)) + { + low++; + } + L->r[high] = L->r[low]; + ++(*movetimes); + } + L->r[low] = key; + (*movetimes)++; + return low; +}; + +Status QuickSort(SqList* L, int low, int high, int *comtimes, int *movetimes) +{ + int pivotlo; + if (low < high) + { + pivotlo = QKPass(L, low, high, comtimes, movetimes); + QuickSort(L, low, pivotlo - 1, comtimes, movetimes); + QuickSort(L, pivotlo + 1, high, comtimes, movetimes); + } + return OK; +} + +int SelectMinKey(SqList* L , int i,int* comtimes,int* movetimes) +{ + int k = i; + int j; + for(j = i+1 ;j < L->length; ++j) + { + if(L->r[k] > L->r[j]) + { + (*comtimes)++; + k = j; + } + } + return k; +}; + +Status SelectSort(SqList* L,int* comtimes,int* movetimes) +{ + int i; + int key,temp; + for(i=0;ilength;i++) + { + key = SelectMinKey(L,i,comtimes,movetimes); + if(key!=1) + { + temp = L->r[i]; + L->r[i] = L->r[key]; + L->r[key] = temp; + (*movetimes)+=3; + } + } + return OK; +}; + +int main() +{ + int comtimes, movetimes; + int i; + SqList a,b; + comtimes = movetimes = 0; + + srand((unsigned)time(NULL)); + a.length = b.length = 0; + for(i = 0;i < maxsize;i++) + { + a.r[i] = rand() % 100 + 1; + b.r[i] = a.r[i]; + a.length++; + b.length++; + } + + printf("娴嬭瘯鏁扮粍涓猴細\n"); + output(a); + + printf("缁忚繃鐩存帴鎺掑簭鍚庣殑鏁扮粍涓猴細"); + InsertSort(&b,&comtimes,&movetimes); + output(b); + printf("鎬绘瘮杈冩鏁帮細%d\n鎬绘帓搴忔鏁帮細%d\n涓よ呬箣鍜岋細%d\n\n",comtimes,movetimes,comtimes+movetimes); + + Initarray(&a,&b,&comtimes,&movetimes); + + + printf("缁忚繃甯屽皵鎺掑簭鍚庣殑鏁扮粍涓猴細"); + ShellSort(&b,&comtimes,&movetimes); + output(b); + printf("鎬绘瘮杈冩鏁帮細%d\n鎬绘帓搴忔鏁帮細%d\n涓よ呬箣鍜岋細%d\n\n",comtimes,movetimes,comtimes+movetimes); + + Initarray(&a,&b,&comtimes,&movetimes); + + printf("缁忚繃姘旀场鎺掑簭鍚庣殑鏁扮粍涓猴細"); + BubbleSort(&b,&comtimes,&movetimes); + output(b); + printf("鎬绘瘮杈冩鏁帮細%d\n鎬绘帓搴忔鏁帮細%d\n涓よ呬箣鍜岋細%d\n\n",comtimes,movetimes,comtimes+movetimes); + + Initarray(&a,&b,&comtimes,&movetimes); + + printf("缁忚繃蹇熸帓搴忓悗鐨勬暟缁勪负锛"); + QuickSort(&b,0,4,&comtimes,&movetimes); + output(b); + printf("鎬绘瘮杈冩鏁帮細%d\n鎬绘帓搴忔鏁帮細%d\n涓よ呬箣鍜岋細%d\n\n",comtimes,movetimes,comtimes+movetimes); + + Initarray(&a,&b,&comtimes,&movetimes); + + printf("缁忚繃绠鍗曟帓搴忓悗鐨勬暟缁勪负锛"); + SelectSort(&b,&comtimes,&movetimes); + output(b); + printf("鎬绘瘮杈冩鏁帮細%d\n鎬绘帓搴忔鏁帮細%d\n涓よ呬箣鍜岋細%d\n\n",comtimes,movetimes,comtimes+movetimes); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/FlyFly-H/\346\225\260\345\210\266\350\275\254\346\215\242.c" "b/2017-1/FlyFly-H/\346\225\260\345\210\266\350\275\254\346\215\242.c" new file mode 100644 index 00000000..4eec3f8e --- /dev/null +++ "b/2017-1/FlyFly-H/\346\225\260\345\210\266\350\275\254\346\215\242.c" @@ -0,0 +1,100 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配量 +#define STACKINCREMENT 10 //存储空间分配增量 + +typedef enum +{ + OK , + OVERFLOW, + ERROR +} Status; + +typedef enum +{ + false, + true +}bool; + +typedef struct +{ + int *base; + int *top; + int stacksize; +} SqStack; + +Status InitStack (SqStack *S) //函数功能为构造一个空栈 +{ + S->base = (int *)malloc(STACK_INIT_SIZE * sizeof(int)); + if (!S->base) + { + return (OVERFLOW);//分配失败 + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status Push (SqStack *S,int e) //插入元素e为新的栈顶元素 +{ + if( S->top - S->base >= S->stacksize) //栈满,追加存储空间; + { + S->base = (int *) realloc (S->base,(S->stacksize + STACKINCREMENT) * sizeof(int)); + if(!S->base) + return OVERFLOW; + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *(S->top++) = e; + return OK; +} + +Status Pop (SqStack *S,int* pa) //若栈不为空,删除S的栈顶元素,用e返回其值,并返回OK ;否则返回ERROR; +{ + if(S->top == S->base) + return ERROR; + *pa =*(--S->top); + return OK; +} + +bool StackEmpty(SqStack *S) +{ + if(S->top == S->base) + return true; + else + return false; +} + +void conversion (SqStack *S,int input,int d) +{ + int e; + while (input) + { + Push( S,input % d); + input = input/d; + } + while (!StackEmpty(S)) + { + Pop(S,&e); + printf("%d",e); + } +} + +int main() +{ + int n; + int d; + SqStack S; + srand( (unsigned int)time(NULL)); + n = rand()%1024; + printf("随机生成检测数字为:"); + printf("%d\n",n); + printf("请输入进制数\n"); + scanf_s("%d", &d); + InitStack(&S); //构造一个空栈; + conversion(&S,n,d); +} + + diff --git "a/2017-1/FlyFly-H/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/1.c" "b/2017-1/FlyFly-H/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/1.c" new file mode 100644 index 00000000..c15be1d4 --- /dev/null +++ "b/2017-1/FlyFly-H/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/1.c" @@ -0,0 +1,136 @@ +#include +#include + +typedef enum status +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef char TElemType; + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild,*rchild; +}BiTNode,*BiTree; + + int i = 0; + Status CreateBiTree(BiTree *T,TElemType * p) //构建二叉树 +{ + + TElemType ch; + ch = p[i]; + i++; + + if(ch == '*') + { + *T = NULL; + } + else + { + if(!(*T = (BiTree)malloc(sizeof(BiTNode)))) + { + return OVERFLOW; + } + (*T)->data = ch; + CreateBiTree(&(*T)->lchild,p); + CreateBiTree(&(*T)->rchild,p); + } + return OK; +} + +void PostOrderTraverseTree(BiTree T) //输出二叉树 +{ + if(T) + { + PostOrderTraverseTree(T->lchild); + PostOrderTraverseTree(T->rchild); + printf("%c",T->data); + } +} + +int Depth(BiTree T)// 返回二叉树的深度 +{ + int depthval,depthLeft,depthRight; + if(!T) + { + depthval = 0; + } + else + { + depthLeft = Depth(T->lchild); + depthRight= Depth(T->rchild); + depthval = 1 + (depthLeft > depthRight ? depthLeft : depthRight); + } + return depthval; +} + +int count[50];//存放各层结点数; +int max = 0; //最大宽度; +int j=0;// + +int Width(BiTree T)//用递归求最大宽度 +{ + if(T == NULL) + return 0; + i++; + count[i]++; + if(max < count[i]) + max = count[i]; + Width(T -> lchild); + Width(T -> rchild); + i--; + return max; +} + +int CountLeaf(BiTree T) // 返回指针T所指二叉树中所有叶子结点个数 +{ + int m=0,n=0; + if(!T) return 0; + if(!T->lchild && !T->rchild) { + return 1; + } else { + m = CountLeaf(T->lchild); + n = CountLeaf(T->rchild); + return m+n; + } +} + +int Count (BiTree T) //返回指针T所指二叉树中所有结点个数 +{ + int m,n; + if (!T) return 0; + if (!T->lchild && !T->rchild) { + return 1; + } else { + m = Count(T->lchild); + n = Count(T->rchild); + return m + n + 1; + } +} +int main() +{ + BiTree T = NULL; + BiTree B = NULL; + + TElemType str1[30] = "ABDG***EH**I*K**C*F**"; + TElemType str2[30] = "ABD*F***CE***"; + + printf("测试用例1:ABDG***EH**I*K**C*F**\n"); + CreateBiTree(&T,str1); + printf("此二叉树高度为%d,最大宽度为%d\n",Depth(T),Width(T)); + printf("此二叉树结点为%d,叶子结点为%d",Count(T),CountLeaf(T)); + + printf("\n\n"); + + i=0; + j=0; + printf("测试用例2:ABD*F***CE***\n"); + CreateBiTree(&T,str2); + printf("此二叉树高度为%d,最大宽度为%d\n",Depth(T),Width(T)); + printf("此二叉树结点为%d,叶子结点为%d",Count(T),CountLeaf(T)); + printf("\n"); + return 0; +} diff --git "a/2017-1/FlyFly-H/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/1plus.c" "b/2017-1/FlyFly-H/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/1plus.c" new file mode 100644 index 00000000..2c21f34c --- /dev/null +++ "b/2017-1/FlyFly-H/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/1plus.c" @@ -0,0 +1,136 @@ +#include +#include + +typedef enum status +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef char TElemType; + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild,*rchild; +}BiTNode,*BiTree; + + int i = 0; + Status CreateBiTree(BiTree *T,TElemType * p) //构建二叉树 +{ + + TElemType ch; + ch = p[i]; + i++; + + if(ch == '*') + { + *T = NULL; + } + else + { + if(!(*T = (BiTree)malloc(sizeof(BiTNode)))) + { + return OVERFLOW; + } + (*T)->data = ch; + CreateBiTree(&(*T)->lchild,p); + CreateBiTree(&(*T)->rchild,p); + } + return OK; +} + +void PostOrderTraverseTree(BiTree T) //输出二叉树 +{ + if(T) + { + PostOrderTraverseTree(T->lchild); + PostOrderTraverseTree(T->rchild); + printf("%c",T->data); + } +} + +int Depth(BiTree T)// 返回二叉树的深度 +{ + int depthval,depthLeft,depthRight; + if(!T) + { + depthval = 0; + } + else + { + depthLeft = Depth(T->lchild); + depthRight= Depth(T->rchild); + depthval = 1 + (depthLeft > depthRight ? depthLeft : depthRight); + } + return depthval; +} + +int count[50];//存放各层结点数; +int max = 0; //最大宽度; +int j=0;// + +int Width(BiTree T)//用递归求最大宽度 +{ + if(T == NULL) + return 0; + i++; + count[i]++; + if(max < count[i]) + max = count[i]; + Width(T -> lchild); + Width(T -> rchild); + i--; + return max; +} + +int CountLeaf(BiTree T) // 返回指针T所指二叉树中所有叶子结点个数 +{ + int m=0,n=0; + if(!T) return 0; + if(!T->lchild && !T->rchild) { + return 1; + } else { + m = CountLeaf(T->lchild); + n = CountLeaf(T->rchild); + return m+n; + } +} + +int Count (BiTree T) //返回指针T所指二叉树中所有结点个数 +{ + int m,n; + if (!T) return 0; + if (!T->lchild && !T->rchild) { + return 1; + } else { + m = Count(T->lchild); + n = Count(T->rchild); + return m + n + 1; + } +} +int main() +{ + BiTree T = NULL; + BiTree B = NULL; + + TElemType str1[30] = "ABDG***EH**I*K**C*F**"; + TElemType str2[30] = "ABD*F***CE***"; + + printf("测试用例1:ABDG***EH**I*K**C*F**\n"); + CreateBiTree(&T,str1); + printf("此二叉树高度为%d,最大宽度为%d\n",Depth(T),Width(T)); + printf("此二叉树结点个数为%d,叶子结点个数为%d",Count(T),CountLeaf(T)); + + printf("\n\n"); + + i=0; + j=0; + printf("测试用例2:ABD*F***CE***\n"); + CreateBiTree(&T,str2); + printf("此二叉树高度为%d,最大宽度为%d\n",Depth(T),Width(T)); + printf("此二叉树结点个数为%d,叶子结点个数为%d",Count(T),CountLeaf(T)); + printf("\n"); + return 0; +} diff --git "a/2017-1/FlyFly-H/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/YK[6S9KQD17~1Z$V}8OWSVA.png" "b/2017-1/FlyFly-H/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/YK[6S9KQD17~1Z$V}8OWSVA.png" new file mode 100644 index 00000000..31dc4d5f Binary files /dev/null and "b/2017-1/FlyFly-H/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/YK[6S9KQD17~1Z$V}8OWSVA.png" differ diff --git "a/2017-1/FlyFly-H/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/third.c" "b/2017-1/FlyFly-H/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/third.c" new file mode 100644 index 00000000..7dbbb2fa --- /dev/null +++ "b/2017-1/FlyFly-H/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/third.c" @@ -0,0 +1,137 @@ +#include +#include + +typedef enum status +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef char TElemType; + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild,*rchild; +}BiTNode,*BiTree; + + int i = 0; + Status CreateBiTree(BiTree *T,TElemType * p) //构建二叉树 +{ + + TElemType ch; + ch = p[i]; + i++; + + if(ch == '*') + { + *T = NULL; + } + else + { + if(!(*T = (BiTree)malloc(sizeof(BiTNode)))) + { + return OVERFLOW; + } + (*T)->data = ch; + CreateBiTree(&(*T)->lchild,p); + CreateBiTree(&(*T)->rchild,p); + } + return OK; +} + +void PostOrderTraverseTree(BiTree T) //输出二叉树 +{ + if(T) + { + PostOrderTraverseTree(T->lchild); + PostOrderTraverseTree(T->rchild); + printf("%c",T->data); + } +} + +int Depth(BiTree T)// 返回二叉树的深度 +{ + int depthval,depthLeft,depthRight; + if(!T) + { + depthval = 0; + } + else + { + depthLeft = Depth(T->lchild); + depthRight= Depth(T->rchild); + depthval = 1 + (depthLeft > depthRight ? depthLeft : depthRight); + } + return depthval; +} + +int count[50];//存放各层结点数; +int max = 0; //最大宽度; +int j=0;// + +int Width(BiTree T)//用递归求最大宽度 +{ + if(T == NULL) + return 0; + i++; + count[i]++; + if(max < count[i]) + max = count[i]; + Width(T -> lchild); + Width(T -> rchild); + i--; + return max; +} + +int CountLeaf(BiTree T) // 返回指针T所指二叉树中所有叶子结点个数 +{ + int m=0,n=0; + if(!T) return 0; + if(!T->lchild && !T->rchild) { + return 1; + } else { + m = CountLeaf(T->lchild); + n = CountLeaf(T->rchild); + return m+n; + } +} + +int Count (BiTree T) //返回指针T所指二叉树中所有结点个数 +{ + int m,n; + if (!T) return 0; + if (!T->lchild && !T->rchild) { + return 1; + } else { + m = Count(T->lchild); + n = Count(T->rchild); + return m + n + 1; + } +} +int main() +{ + + BiTree T = NULL; + + TElemType str1[30] = "ABDG***EH**I*K**C*F**"; + TElemType str2[30] = "ABD*F***CE***"; + + printf("测试用例1:ABDG***EH**I*K**C*F**\n"); + CreateBiTree(&T,str1); + printf("此二叉树高度为%d,最大宽度为%d\n",Depth(T), Width(T)); + printf("此二叉树叶子结点个数为%d,非叶子结点个数为%d",CountLeaf(T),Count(T)-CountLeaf(T)); + + printf("\n\n"); + + i=0; + j=0; + max = 0; + printf("测试用例2:ABD*F***CE***\n"); + CreateBiTree(&T,str2); + printf("此二叉树高度为%d,最大宽度为%d\n",Depth(T), Width(T)); + printf("此二叉树叶子结点个数为%d,非叶子结点个数为%d",CountLeaf(T),Count(T)-CountLeaf(T)); + printf("\n"); + return 0; +} diff --git "a/2017-1/FlyFly-H/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/\346\265\213\350\257\225\347\224\250\344\276\213.txt" "b/2017-1/FlyFly-H/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/\346\265\213\350\257\225\347\224\250\344\276\213.txt" new file mode 100644 index 00000000..64003daf --- /dev/null +++ "b/2017-1/FlyFly-H/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/\346\265\213\350\257\225\347\224\250\344\276\213.txt" @@ -0,0 +1,7 @@ +实验三的测试用例为: + +输入:whli##ilr#e(s#*s) +输出:while(*s) + +输入:outchar@puthar(*s=#++) +输出:putchar(*s++) \ No newline at end of file diff --git "a/2017-1/FlyFly-H/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217.c" "b/2017-1/FlyFly-H/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217.c" new file mode 100644 index 00000000..8c7f6f4a --- /dev/null +++ "b/2017-1/FlyFly-H/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217.c" @@ -0,0 +1,121 @@ +锘#include +#include +#include + +#define STACK_INIT_SIZE 100 //瀛樺偍绌洪棿鍒濆鍒嗛厤閲 +#define STACKINCREMENT 10 //瀛樺偍绌洪棿鍒嗛厤澧為噺 + +typedef enum +{ + OK , + OVERFLOW, + ERROR +} Status; + +typedef struct +{ + char *base; + char *top; + char stacksize; +} SqStack; + +Status InitStack (SqStack *S) //鍑芥暟鍔熻兘涓烘瀯閫犱竴涓┖鏍 +{ + S->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char)); + if (!S->base) + { + return (OVERFLOW);//鍒嗛厤澶辫触 + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status Push (SqStack *S,char e) //鎻掑叆鍏冪礌e涓烘柊鐨勬爤椤跺厓绱 +{ + if( S->top - S->base >= S->stacksize) //鏍堟弧锛岃拷鍔犲瓨鍌ㄧ┖闂达紱 + { + S->base = (char *) realloc (S->base,(S->stacksize + STACKINCREMENT) * sizeof(char)); + if(!S->base) + return OVERFLOW; + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *(S->top++) = e; + return OK; +} + +Status Pop (SqStack *S,char* pa) //鑻ユ爤涓嶄负绌猴紝鍒犻櫎S鐨勬爤椤跺厓绱狅紝鐢╡杩斿洖鍏跺硷紝骞惰繑鍥濷K ;鍚﹀垯杩斿洖ERROR; +{ + if(S->top == S->base) + return ERROR; + *pa =*(--S->top); + return OK; +} + +Status ClearStack(SqStack *S) +{ + S->top = S->base; + return OK; +} + +Status DestroyStack(SqStack *S) +{ + free(S->base); + S->top = NULL; + S->base = NULL; + S->stacksize = 0; + return OK; +} + +Status LineEdit (SqStack *S) +{ + char ch,c; + char *p; + ch = getchar(); + while (ch != EOF) + { + while (ch != '\n') + { + switch (ch) + { + case '#': + { + Pop(S,&c); + break; + } + case '@': + { + ClearStack(S); + break; + } + default: + { + Push(S,ch); + break; + } + } + ch = getchar(); + } + p = S->base; + while(p <= S->top) + { + printf("%c",*p); + ++p; + } + ClearStack(S); + if(ch!=EOF) + ch = getchar(); + } + return OK; + } + + + +int main() +{ + SqStack S; + InitStack(&S); + LineEdit(&S);//杩涜鎷缂栬緫 + DestroyStack(&S);//灏嗘爤閿姣 +} diff --git a/2017-1/HuanWoWeiLan/SeperateList/homework13.cpp b/2017-1/HuanWoWeiLan/SeperateList/homework13.cpp new file mode 100644 index 00000000..1ab4e406 --- /dev/null +++ b/2017-1/HuanWoWeiLan/SeperateList/homework13.cpp @@ -0,0 +1,125 @@ +#include +#include +#include +typedef struct Node +{ + int data; + struct Node *next; + +}LNode,*LinkList; +//链表输出 +void OutPutList(LinkList head) +{ + + LNode *temp; + int count=head->data; + temp=head->next; + + while(count) + { + printf("%d——>",temp->data); + temp=temp->next; + count--; + } + printf("NULL"); + printf("\n\n"); + + +} +//建立链表 +LinkList CreateList(int n) +{ + int i=0,number; + LinkList head,current,previous; + head=NULL; + current=NULL; + previous=NULL; + srand(time(NULL)); + while( inext=current; + } + current->data = number; + current->next = NULL; + previous = current; + i++; + } + + return(head); //返回创建链表的首指针 + +} +void PrintList(LinkList x)//输出分解前的链表 +{ + printf("链表分解前为:\n"); + while(x!=NULL) + { + printf("%d ——>",x->data); + x=x->next; + } + printf("NULL \n\n"); +} +//分解链表 +void SeperateList(LinkList list,LinkList list1,LinkList list2) +{ + + LNode*p1 = list1; + LNode*p2 = list2; + LNode*p = list; + + list1->data = 0; + list2->data = 0; + while (p!=NULL) + { + + p1->next = p; + p1 = p1->next; + list1->data++; + p = p->next; + if (p!= NULL) { + + p2->next = p; + p2 = p2->next; + list2->data++; p = p->next;} + + + + } + p1->next = list1; + p2->next = list2; +} +int main() + +{ + + LinkList list,list1,list2; + int n;//链表生成的长度 + srand(time(NULL)); + n=rand()%10+1; + list=CreateList(n); + PrintList(list); + + + list1=(LNode *)malloc(sizeof(LNode)); + list2=(LNode *)malloc(sizeof(LNode)); + + SeperateList(list,list1,list2); + + printf("链表1——序号为奇数: 元素个数为%d\n",list1->data); + + OutPutList(list1); + + printf("链表2——序号为偶数: 元素个数为%d\n",list2->data); + + OutPutList(list2); + + return 0; + +} \ No newline at end of file diff --git "a/2017-1/HuanWoWeiLan/SeperateList/\350\277\220\350\241\214\346\210\252\345\233\276.PNG" "b/2017-1/HuanWoWeiLan/SeperateList/\350\277\220\350\241\214\346\210\252\345\233\276.PNG" new file mode 100644 index 00000000..5bc8a801 Binary files /dev/null and "b/2017-1/HuanWoWeiLan/SeperateList/\350\277\220\350\241\214\346\210\252\345\233\276.PNG" differ diff --git "a/2017-1/HuanWoWeiLan/\344\272\214\345\217\211\346\240\221\345\220\204\344\270\252\345\207\275\346\225\260\345\256\236\347\216\260/\344\272\214\345\217\211\346\240\221\345\220\204\344\270\252\345\207\275\346\225\260\345\256\236\347\216\260.c" "b/2017-1/HuanWoWeiLan/\344\272\214\345\217\211\346\240\221\345\220\204\344\270\252\345\207\275\346\225\260\345\256\236\347\216\260/\344\272\214\345\217\211\346\240\221\345\220\204\344\270\252\345\207\275\346\225\260\345\256\236\347\216\260.c" new file mode 100644 index 00000000..644c4b1d --- /dev/null +++ "b/2017-1/HuanWoWeiLan/\344\272\214\345\217\211\346\240\221\345\220\204\344\270\252\345\207\275\346\225\260\345\256\236\347\216\260/\344\272\214\345\217\211\346\240\221\345\220\204\344\270\252\345\207\275\346\225\260\345\256\236\347\216\260.c" @@ -0,0 +1,116 @@ +#include +#include +#include//用于判断是否为完全二叉树中的幂次计算 + +#define OVERFLOW 0 +#define OK 1 + + + +typedef int Status; +typedef char ElemType; +typedef struct BiTree +{ + ElemType data; + struct BiTree *Lchild; + struct BiTree *Rchild; +}BiTree,*Binary_Tree; +//创建一个二叉树 +int count =0;//计算总结点数 +Status CreateBiTree(Binary_Tree *T) +{ + char ch; + *T=(Binary_Tree)malloc(sizeof(BiTree)); + if(!(*T)) + exit(OVERFLOW); + scanf("%c",&ch); + if(ch=='0') + *T=NULL; + else + { + (*T)->data=ch; + count++; + CreateBiTree(&((*T)->Lchild)); + CreateBiTree(&((*T)->Rchild)); + } + return OK; +} + + +int TreeDepth(Binary_Tree T) { + if (T == NULL) + return 0; + int nLeft = TreeDepth(T->Lchild); + int nRight = TreeDepth(T->Rchild); + return (nLeft>nRight) ? (nLeft+1):(nRight+1); +} + +int x[100]; //存放各层结点数 +int max = 0; //最大宽度 + +int TreeWidth(Binary_Tree T) //递归求最大宽度 +{ + + if(T == NULL) + { + return 0; + } + count++; + x[count]++; + if(max < x[count]) + { + max = x[count]; + } + TreeWidth(T->Lchild); + TreeWidth(T->Rchild); + count--; + return max; +} +int LeafCount(Binary_Tree T){ + if(T == NULL){ + return 0; + } + else if ((T->Lchild==NULL) && (T->Rchild==NULL)){ + return 1; + } + else{ + return LeafCount(T->Lchild)+ LeafCount(T->Rchild); + } +} + + + + +int main() +{ + BiTree *T=NULL; + + + printf("请创建第一个二叉树,0代表空 :\n"); + CreateBiTree(&T); + int depth= TreeDepth(T); + int width=TreeWidth(T); + int count1=LeafCount(T); + int notleaf1=count-count1; + int n=1; + for(int i=0;i +#include + +#define OVERFLOW 0 +#define OK 1 + +typedef int Status; +typedef char ElemType; +typedef struct BiTree +{ + ElemType data; + struct BiTree *Lchild; + struct BiTree *Rchild; +}BiTree,*Binary_Tree; +//创建一个二叉树 +Status CreateBiTree(Binary_Tree *T) +{ + char ch; + *T=(Binary_Tree)malloc(sizeof(BiTree)); + if(!(*T)) + exit(OVERFLOW); + scanf("%c",&ch); + if(ch=='0') + *T=NULL; + else + { + (*T)->data=ch; + CreateBiTree(&((*T)->Lchild)); + CreateBiTree(&((*T)->Rchild)); + } + return OK; +} +//先遍历二叉树 +Status PreShowBiTree(Binary_Tree T) +{ + if(T!=NULL) + { + printf("%c ",T->data); + PreShowBiTree(T->Lchild); + PreShowBiTree(T->Rchild); + } + return OK; +} + +//中遍历二叉树 +Status MidShowBiTree(Binary_Tree T) +{ + if(T!=NULL) + { + MidShowBiTree(T->Lchild); + printf("%c ",T->data); + MidShowBiTree(T->Rchild); + } + return OK; +} + +//后遍历二叉树 +Status BehShowBiTree(Binary_Tree T) +{ + if(T!=NULL) + { + BehShowBiTree(T->Lchild); + BehShowBiTree(T->Rchild); + printf("%c ",T->data); + } + return OK; +} + +int main() +{ + BiTree *T=NULL; + printf("请创建一个二叉树,0代表空 :\n"); + CreateBiTree(&T); + printf("先序遍历: \n"); + PreShowBiTree(T); + printf("\n"); + printf("中序遍历: \n"); + MidShowBiTree(T); + printf("\n"); + printf("后序遍历: \n"); + BehShowBiTree(T); + printf("\n"); + +} \ No newline at end of file diff --git "a/2017-1/HuanWoWeiLan/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/\350\277\220\350\241\214\346\210\252\345\233\276.PNG" "b/2017-1/HuanWoWeiLan/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/\350\277\220\350\241\214\346\210\252\345\233\276.PNG" new file mode 100644 index 00000000..11a23b24 Binary files /dev/null and "b/2017-1/HuanWoWeiLan/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/\350\277\220\350\241\214\346\210\252\345\233\276.PNG" differ diff --git "a/2017-1/HuanWoWeiLan/\345\220\204\347\247\215\346\216\222\345\272\217/Sort.c" "b/2017-1/HuanWoWeiLan/\345\220\204\347\247\215\346\216\222\345\272\217/Sort.c" new file mode 100644 index 00000000..a81f1f07 --- /dev/null +++ "b/2017-1/HuanWoWeiLan/\345\220\204\347\247\215\346\216\222\345\272\217/Sort.c" @@ -0,0 +1,241 @@ +#include +#include +#include + +#define maxsize 5 +typedef enum +{ + false, + true +}bool; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef struct +{ + int r[maxsize]; + int length; +}SqList; + +Status output(SqList a) +{ + int i; + for(i = 0;i < maxsize;i++) + { + printf("%d ",a.r[i]); + } + printf("\n"); + return OK; +}; + +Status Initarray(SqList* a,SqList* b,int* comtimes,int* movetimes) +{ + int i; + for(i = 0;i < maxsize;i++) + { + b->r[i] = a->r[i] ; + } + *comtimes = 0; + *movetimes = 0; + return OK; +}; + +Status InsertSort(SqList* L,int* comtimes,int* movetimes) +{ + int j,i; + int temp; + for(i = 1;i < L->length;i++) + { + if(L->r[i] < L->r[i-1]) + { + j = i-1; + (*comtimes)++; + temp = L->r[i]; + L->r[i] = L->r[i-1]; + (*movetimes)++; + while(temp < L->r[j]) + { + L->r[j+1] = L->r[j]; + (*movetimes)++; + j--; + } + + L->r[j+1] = temp; + (*comtimes)++; + } + } + return OK; +}; + + +Status ShellSort(SqList* L,int *comtimes,int* movetimes) +{ + int j, gap,temp,k; + for(gap = L->length/2; gap > 0;gap /= 2) + for(j = gap;j < L->length;j++)//从数组第gap个元素开始 + { + if(L->r[j] < L->r[j-gap])//每个元素与自己组内的数据进行直接插入排序 + { + (*comtimes)++; + temp = L->r[j]; + k = j-gap; + while(k >= 0 && L->r[k] > temp) + { + L->r[k+gap] = L->r[k]; + k -= gap; + (*movetimes)++; + } + L->r[k+gap] = temp; + } + } + return OK; +}; + +Status BubbleSort(SqList* L,int* comtimes,int* movetimes) +{ + int temp,i,j; + for(i = 0;i < L->length;i++) + { + for(j = 0;j < L->length-i-1;j++) + { + if(L->r[j] > L->r[j+1]) + { + (*comtimes)++; + temp = L->r[j]; + L->r[j] = L->r[j+1]; + L->r[j+1] = temp; + (*movetimes) += 3; + } + } + } + return OK; +} +int QKPass(SqList *L, int left, int right, int* comtimes,int* movetimes) +{ + int key = L->r[left]; + int low = left; + int high = right; + while (low < high) + { + while (low < high&&L->r[high] >= key&&(*comtimes)++) + { + high--; + } + L->r[low] = L->r[high]; + ++(*movetimes); + while (low < high&&L->r[low] < key && ((*comtimes)++)) + { + low++; + } + L->r[high] = L->r[low]; + ++(*movetimes); + } + L->r[low] = key; + (*movetimes)++; + return low; +}; + +Status QuickSort(SqList* L, int low, int high, int *comtimes, int *movetimes) +{ + int pivotlo; + if (low < high) + { + pivotlo = QKPass(L, low, high, comtimes, movetimes); + QuickSort(L, low, pivotlo - 1, comtimes, movetimes); + QuickSort(L, pivotlo + 1, high, comtimes, movetimes); + } + return OK; +} + +int SelectMinKey(SqList* L , int i,int* comtimes,int* movetimes) +{ + int k = i; + int j; + for(j = i+1 ;j < L->length; ++j) + { + if(L->r[k] > L->r[j]) + { + (*comtimes)++; + k = j; + } + } + return k; +}; + +Status SelectSort(SqList* L,int* comtimes,int* movetimes) +{ + int i; + int key,temp; + for(i=0;ilength;i++) + { + key = SelectMinKey(L,i,comtimes,movetimes); + if(key!=1) + { + temp = L->r[i]; + L->r[i] = L->r[key]; + L->r[key] = temp; + (*movetimes)+=3; + } + } + return OK; +}; + +int main() +{ + int comtimes, movetimes; + int i; + SqList a,b; + comtimes = movetimes = 0; + + srand((unsigned)time(NULL)); + a.length = b.length = 0; + for(i = 0;i < maxsize;i++) + { + a.r[i] = rand() % 100 + 1; + b.r[i] = a.r[i]; + a.length++; + b.length++; + } + + printf("测试数组为:\n"); + output(a); + + printf("直接排序:"); + InsertSort(&b,&comtimes,&movetimes); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",comtimes,movetimes,comtimes+movetimes); + + Initarray(&a,&b,&comtimes,&movetimes); + + + printf("希尔排序:"); + ShellSort(&b,&comtimes,&movetimes); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",comtimes,movetimes,comtimes+movetimes); + + Initarray(&a,&b,&comtimes,&movetimes); + + printf("气泡排序:"); + BubbleSort(&b,&comtimes,&movetimes); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",comtimes,movetimes,comtimes+movetimes); + + Initarray(&a,&b,&comtimes,&movetimes); + + printf("快速排序:"); + QuickSort(&b,0,4,&comtimes,&movetimes); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",comtimes,movetimes,comtimes+movetimes); + + Initarray(&a,&b,&comtimes,&movetimes); + + printf("简单排序:"); + SelectSort(&b,&comtimes,&movetimes); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",comtimes,movetimes,comtimes+movetimes); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/HuanWoWeiLan/\345\223\210\345\270\214\350\241\250 - \345\211\257\346\234\254/Hash.cpp" "b/2017-1/HuanWoWeiLan/\345\223\210\345\270\214\350\241\250 - \345\211\257\346\234\254/Hash.cpp" new file mode 100644 index 00000000..086feeb3 --- /dev/null +++ "b/2017-1/HuanWoWeiLan/\345\223\210\345\270\214\350\241\250 - \345\211\257\346\234\254/Hash.cpp" @@ -0,0 +1,235 @@ +#include +#include +#include +typedef int Status; +typedef int KeyType; +typedef int ValueType; +typedef struct _ElemType { + KeyType key; // 关键字 + ValueType val; // 值 +#ifdef CHAINED_HASH + struct _ElemType *next; +#endif +} ElemType; +typedef struct +{ + ElemType *elem; + int count;//当前元素个数 + int sizeindex;//哈希表表长 +}HashTable; + +static int m; +static int hashsize[] = { 11,19,29,37,49 }; +int hashsize_count = 0; +int hash_(KeyType e, int l); +Status RecreateHashTable(HashTable *H); +Status InsertHash(HashTable *H, KeyType e, ValueType v); +Status SearchHash(HashTable H, KeyType e, ValueType v, int* p, int *c); +Status InitHashTable(HashTable *H); +void travel_(int *q, int l); +void TraverHash(HashTable H); +void collision(int *p, int c, KeyType k, int l); + +//制造数组 +Status make(int *q,int l) +{ + int temp; + int length; + int i; + length = l; + for (i = 0; i elem = (ElemType*)malloc(m * sizeof(ElemType)); + if (!(*H).elem) + exit(0);//return 0; // 存储分配失败 + for (i = 0; i < (*H).sizeindex; i++) + { + (*H).elem[i].key =-1; // 未填记录的标志 + (*H).elem[i].val = 0; + } + hashsize_count++; + return 1; +} +//进行插入操作 +Status InsertHash(HashTable *h, KeyType e, ValueType v) +{ + int c = 0;//用于计数冲突次数 + int p;//用于返回插入的位置 + if (SearchHash(*h, e, v, &p, &c)) + { + return -1;//如果已有该元素 返回 + } + else if (c < h->sizeindex / 2)//冲突次数达到则进行扩表 c的值可调 + { + (*h).elem[p].val = v; + (*h).elem[p].key = e; + ++(*h).count; + return 1;//插入成功 + } + else + {//如果哈希表过于小 + printf("哈希表分配过小 重建表\n"); + RecreateHashTable(h); + return 0; + } +} +Status SearchHash(HashTable h, KeyType e, ValueType v, int* p, int *c) +{//如果查找成功 p为所在的下标 否则p为待插入的位置 + *p = hash_(e, h.sizeindex);//返回下标 + while (h.elem[*p].key != -1 && v != h.elem[*p].val) + { + (*c)++; + if ((*c) < h.sizeindex) + {//如果冲突次数还能继续处理 + collision(p, *c,e,h.sizeindex); + } + else + { + break; + } + } + if (v == h.elem[*p].val) + return 1; + else + return 0; +} +int hash_(KeyType e,int l) +{//除留余数法构造简单的哈希表 + int i = e%l; + return i; +} +void collision(int *p,int c, KeyType k,int l) +{ + *p = (k + c) % l;//线性探索再散列:di = 1,2,3,...,m-1 +} +//打印哈希表 +void TraverHash(HashTable h) +{ + for (int i = 0; i < h.sizeindex; i++) + { + printf("{[%d] : ",i); + printf("%d->%d} ",h.elem[i].key, h.elem[i].val); + } + printf("\n"); +} +//重建哈希表 +Status RecreateHashTable(HashTable *H) +{ + int i, count = (*H).sizeindex;//H中原有记录个数 + ElemType *p, *elem = (ElemType *)malloc(count * sizeof(ElemType));//动态生成存放哈希表H原有数据的空间 + p = elem; + for (i = 0; i < (*H).sizeindex; ++i) + {//将原有的所有记录,保存到elem中 + //*p++ = (*H).elem[i];//将记录依次存入elem + p[i].key = (*H).elem[i].key; + p[i].val = (*H).elem[i].val; + } + (*H).count = 0;//将原有记录数置为0,为下面调用InserHash做准备 + (*H).sizeindex = hashsize[hashsize_count]; + (*H).elem = (ElemType *)realloc((*H).elem, (*H).sizeindex * sizeof(ElemType));//以新的存储容量重新生成空哈希表H + for (i = 0; i <(*H).sizeindex; ++i) + {//初始化新的哈希表 + (*H).elem[i].key = -1;//未填记录 + (*H).elem[i].val = 0; + } + for (i=0;i +#include +#include +typedef int Status; +typedef int KeyType; +typedef int ValueType; +typedef struct _ElemType { + KeyType key; // 关键字 + ValueType val; // 值 +#ifdef CHAINED_HASH + struct _ElemType *next; +#endif +} ElemType; +typedef struct +{ + ElemType *elem; + int count;//当前元素个数 + int sizeindex;//哈希表表长 +}HashTable; + +static int m; +static int hashsize[] = { 11,19,29,37,49 }; +int hashsize_count = 0; +int hash_(KeyType e, int l); +Status RecreateHashTable(HashTable *H); +Status InsertHash(HashTable *H, KeyType e, ValueType v); +Status SearchHash(HashTable H, KeyType e, ValueType v, int* p, int *c); +Status InitHashTable(HashTable *H); +void travel_(int *q, int l); +void TraverHash(HashTable H); +void collision(int *p, int c, KeyType k, int l); + +//制造数组 +Status make(int *q,int l) +{ + int temp; + int length; + int i; + length = l; + for (i = 0; i elem = (ElemType*)malloc(m * sizeof(ElemType)); + if (!(*H).elem) + exit(0);//return 0; // 存储分配失败 + for (i = 0; i < (*H).sizeindex; i++) + { + (*H).elem[i].key =-1; // 未填记录的标志 + (*H).elem[i].val = 0; + } + hashsize_count++; + return 1; +} +//进行插入操作 +Status InsertHash(HashTable *h, KeyType e, ValueType v) +{ + int c = 0;//用于计数冲突次数 + int p;//用于返回插入的位置 + if (SearchHash(*h, e, v, &p, &c)) + { + return -1;//如果已有该元素 返回 + } + else if (c < h->sizeindex / 2)//冲突次数达到则进行扩表 c的值可调 + { + (*h).elem[p].val = v; + (*h).elem[p].key = e; + ++(*h).count; + return 1;//插入成功 + } + else + {//如果哈希表过于小 + printf("哈希表分配过小 重建表\n"); + RecreateHashTable(h); + return 0; + } +} +Status SearchHash(HashTable h, KeyType e, ValueType v, int* p, int *c) +{//如果查找成功 p为所在的下标 否则p为待插入的位置 + *p = hash_(e, h.sizeindex);//返回下标 + while (h.elem[*p].key != -1 && v != h.elem[*p].val) + { + (*c)++; + if ((*c) < h.sizeindex) + {//如果冲突次数还能继续处理 + collision(p, *c,e,h.sizeindex); + } + else + { + break; + } + } + if (v == h.elem[*p].val) + return 1; + else + return 0; +} +int hash_(KeyType e,int l) +{//除留余数法构造简单的哈希表 + int i = e%l; + return i; +} +void collision(int *p,int c, KeyType k,int l) +{ + *p = (k + c) % l;//线性探索再散列:di = 1,2,3,...,m-1 +} +//打印哈希表 +void TraverHash(HashTable h) +{ + for (int i = 0; i < h.sizeindex; i++) + { + printf("{[%d] : ",i); + printf("%d->%d} ",h.elem[i].key, h.elem[i].val); + } + printf("\n"); +} +//重建哈希表 +Status RecreateHashTable(HashTable *H) +{ + int i, count = (*H).sizeindex;//H中原有记录个数 + ElemType *p, *elem = (ElemType *)malloc(count * sizeof(ElemType));//动态生成存放哈希表H原有数据的空间 + p = elem; + for (i = 0; i < (*H).sizeindex; ++i) + {//将原有的所有记录,保存到elem中 + //*p++ = (*H).elem[i];//将记录依次存入elem + p[i].key = (*H).elem[i].key; + p[i].val = (*H).elem[i].val; + } + (*H).count = 0;//将原有记录数置为0,为下面调用InserHash做准备 + (*H).sizeindex = hashsize[hashsize_count]; + (*H).elem = (ElemType *)realloc((*H).elem, (*H).sizeindex * sizeof(ElemType));//以新的存储容量重新生成空哈希表H + for (i = 0; i <(*H).sizeindex; ++i) + {//初始化新的哈希表 + (*H).elem[i].key = -1;//未填记录 + (*H).elem[i].val = 0; + } + for (i=0;i +#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; +} + 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 diff --git "a/2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\346\216\222\345\272\217\344\275\234\344\270\232/Sort.c" "b/2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\346\216\222\345\272\217\344\275\234\344\270\232/Sort.c" new file mode 100644 index 00000000..a81f1f07 --- /dev/null +++ "b/2017-1/HuanWoWeiLan/\347\254\254\344\271\235\346\254\241\346\216\222\345\272\217\344\275\234\344\270\232/Sort.c" @@ -0,0 +1,241 @@ +#include +#include +#include + +#define maxsize 5 +typedef enum +{ + false, + true +}bool; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef struct +{ + int r[maxsize]; + int length; +}SqList; + +Status output(SqList a) +{ + int i; + for(i = 0;i < maxsize;i++) + { + printf("%d ",a.r[i]); + } + printf("\n"); + return OK; +}; + +Status Initarray(SqList* a,SqList* b,int* comtimes,int* movetimes) +{ + int i; + for(i = 0;i < maxsize;i++) + { + b->r[i] = a->r[i] ; + } + *comtimes = 0; + *movetimes = 0; + return OK; +}; + +Status InsertSort(SqList* L,int* comtimes,int* movetimes) +{ + int j,i; + int temp; + for(i = 1;i < L->length;i++) + { + if(L->r[i] < L->r[i-1]) + { + j = i-1; + (*comtimes)++; + temp = L->r[i]; + L->r[i] = L->r[i-1]; + (*movetimes)++; + while(temp < L->r[j]) + { + L->r[j+1] = L->r[j]; + (*movetimes)++; + j--; + } + + L->r[j+1] = temp; + (*comtimes)++; + } + } + return OK; +}; + + +Status ShellSort(SqList* L,int *comtimes,int* movetimes) +{ + int j, gap,temp,k; + for(gap = L->length/2; gap > 0;gap /= 2) + for(j = gap;j < L->length;j++)//从数组第gap个元素开始 + { + if(L->r[j] < L->r[j-gap])//每个元素与自己组内的数据进行直接插入排序 + { + (*comtimes)++; + temp = L->r[j]; + k = j-gap; + while(k >= 0 && L->r[k] > temp) + { + L->r[k+gap] = L->r[k]; + k -= gap; + (*movetimes)++; + } + L->r[k+gap] = temp; + } + } + return OK; +}; + +Status BubbleSort(SqList* L,int* comtimes,int* movetimes) +{ + int temp,i,j; + for(i = 0;i < L->length;i++) + { + for(j = 0;j < L->length-i-1;j++) + { + if(L->r[j] > L->r[j+1]) + { + (*comtimes)++; + temp = L->r[j]; + L->r[j] = L->r[j+1]; + L->r[j+1] = temp; + (*movetimes) += 3; + } + } + } + return OK; +} +int QKPass(SqList *L, int left, int right, int* comtimes,int* movetimes) +{ + int key = L->r[left]; + int low = left; + int high = right; + while (low < high) + { + while (low < high&&L->r[high] >= key&&(*comtimes)++) + { + high--; + } + L->r[low] = L->r[high]; + ++(*movetimes); + while (low < high&&L->r[low] < key && ((*comtimes)++)) + { + low++; + } + L->r[high] = L->r[low]; + ++(*movetimes); + } + L->r[low] = key; + (*movetimes)++; + return low; +}; + +Status QuickSort(SqList* L, int low, int high, int *comtimes, int *movetimes) +{ + int pivotlo; + if (low < high) + { + pivotlo = QKPass(L, low, high, comtimes, movetimes); + QuickSort(L, low, pivotlo - 1, comtimes, movetimes); + QuickSort(L, pivotlo + 1, high, comtimes, movetimes); + } + return OK; +} + +int SelectMinKey(SqList* L , int i,int* comtimes,int* movetimes) +{ + int k = i; + int j; + for(j = i+1 ;j < L->length; ++j) + { + if(L->r[k] > L->r[j]) + { + (*comtimes)++; + k = j; + } + } + return k; +}; + +Status SelectSort(SqList* L,int* comtimes,int* movetimes) +{ + int i; + int key,temp; + for(i=0;ilength;i++) + { + key = SelectMinKey(L,i,comtimes,movetimes); + if(key!=1) + { + temp = L->r[i]; + L->r[i] = L->r[key]; + L->r[key] = temp; + (*movetimes)+=3; + } + } + return OK; +}; + +int main() +{ + int comtimes, movetimes; + int i; + SqList a,b; + comtimes = movetimes = 0; + + srand((unsigned)time(NULL)); + a.length = b.length = 0; + for(i = 0;i < maxsize;i++) + { + a.r[i] = rand() % 100 + 1; + b.r[i] = a.r[i]; + a.length++; + b.length++; + } + + printf("测试数组为:\n"); + output(a); + + printf("直接排序:"); + InsertSort(&b,&comtimes,&movetimes); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",comtimes,movetimes,comtimes+movetimes); + + Initarray(&a,&b,&comtimes,&movetimes); + + + printf("希尔排序:"); + ShellSort(&b,&comtimes,&movetimes); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",comtimes,movetimes,comtimes+movetimes); + + Initarray(&a,&b,&comtimes,&movetimes); + + printf("气泡排序:"); + BubbleSort(&b,&comtimes,&movetimes); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",comtimes,movetimes,comtimes+movetimes); + + Initarray(&a,&b,&comtimes,&movetimes); + + printf("快速排序:"); + QuickSort(&b,0,4,&comtimes,&movetimes); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",comtimes,movetimes,comtimes+movetimes); + + Initarray(&a,&b,&comtimes,&movetimes); + + printf("简单排序:"); + SelectSort(&b,&comtimes,&movetimes); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",comtimes,movetimes,comtimes+movetimes); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/HuanWoWeiLan/\347\256\227\346\263\2252.12/\350\277\220\350\241\214\346\210\252\345\233\276.PNG" "b/2017-1/HuanWoWeiLan/\347\256\227\346\263\2252.12/\350\277\220\350\241\214\346\210\252\345\233\276.PNG" new file mode 100644 index 00000000..64e47b45 Binary files /dev/null and "b/2017-1/HuanWoWeiLan/\347\256\227\346\263\2252.12/\350\277\220\350\241\214\346\210\252\345\233\276.PNG" differ diff --git "a/2017-1/HuanWoWeiLan/\347\256\227\346\263\2252.12/\351\223\276\350\241\250\345\220\210\345\271\2662.0.c" "b/2017-1/HuanWoWeiLan/\347\256\227\346\263\2252.12/\351\223\276\350\241\250\345\220\210\345\271\2662.0.c" new file mode 100644 index 00000000..41e59940 --- /dev/null +++ "b/2017-1/HuanWoWeiLan/\347\256\227\346\263\2252.12/\351\223\276\350\241\250\345\220\210\345\271\2662.0.c" @@ -0,0 +1,123 @@ +#include +#include +#include +typedef struct LNode{ + int data; + struct LNode *next; +}LNode,*LinkList; +LinkList Insert(LinkList head, LinkList p);//将p指向的结点插入链表, 结果链表保持有序,返回值是新链表的首指针。 +LinkList CreateList1();//创建一条新的有序链表 一共包含十个数据 +LinkList CreateList2(); +void PrintList(LinkList head);//打印链表 +LinkList Merge(LinkList pHead1, LinkList pHead2);//合并链表 + +LinkList CreateList1() +{ + LinkList p1,head=0; + int a,i=0; + printf("产生第一条排序链表,共包含10个数: \n"); + srand(time(NULL)); + while(i<10) + { + a=rand()%100+1; + p1=(LinkList)malloc(sizeof(LNode)); + p1->data = a; + head=Insert(head,p1); + i++; + } + return(head); +} +LinkList CreateList2() +{ + LinkList p1, head=0; + int a,i=0; + printf("产生第二条排序链表,共包含10个数: \n"); + srand(time(NULL)); + while(i<10) + { + a=rand()%80; + p1=(LinkList)malloc(sizeof(LNode)); + p1->data = a; + head=Insert(head,p1); + i++; + } + return(head); +} +LinkList Insert(LinkList head, LinkList p) // +{ + LinkList p1, p2; + if(head == NULL) // 原链表为空链表,对应情况① + { head = p; + p->next = NULL; + return(head); + } + p1 = head; + while( (p->data) > (p1->data) && p1->next != NULL ) + { // 寻找待插入位置 + p2 = p1; p1 = p1->next; + // p2指向的结点在p1指向的结点之前 + } + if( (p->data) <= (p1->data) ) // 插在p1之前 + { + p->next = p1; + if(head == p1) + head = p; // 插在链表首部,对应情况② + else + p2->next = p; // 插在链表中间,对应情况③ + } + else // 插在链表尾结点之后,对应情况④ + { + p1->next = p; + p->next = NULL; + } + return(head); +} +void PrintList(LinkList x) +{ + while(x!=NULL) + { + printf("%d ——>",x->data); + x=x->next; + } + printf("NULL \n\n"); +} +LinkList Merge(LinkList pHead1, LinkList pHead2) +{ + if(pHead1 == NULL)//判断二者中是否有空表的情况 + return pHead2; + else if(pHead2 == NULL) + return pHead1; + + LinkList pMergedHead=NULL;//合并后链表的首节点 + + + if(pHead1->data < pHead2->data) + { + pMergedHead = pHead1; + pMergedHead->next = Merge(pHead1->next, pHead2); + } + else + { + pMergedHead = pHead2; + pMergedHead->next = Merge(pHead1, pHead2->next); + } + + return pMergedHead; +} + +int main() +{ + LinkList x,y,z; + x=CreateList1(); + PrintList(x); + + y=CreateList2(); + PrintList(y); + + printf("\n合并后的链表: \n"); + z=Merge(x,y); + PrintList(z); + + return 0; + +} diff --git "a/2017-1/HuanWoWeiLan/\347\256\227\346\263\2253.2.1/\347\256\227\346\263\2253.2.1 \346\225\260\345\210\266\350\275\254\346\215\242.c" "b/2017-1/HuanWoWeiLan/\347\256\227\346\263\2253.2.1/\347\256\227\346\263\2253.2.1 \346\225\260\345\210\266\350\275\254\346\215\242.c" new file mode 100644 index 00000000..44c3eb82 --- /dev/null +++ "b/2017-1/HuanWoWeiLan/\347\256\227\346\263\2253.2.1/\347\256\227\346\263\2253.2.1 \346\225\260\345\210\266\350\275\254\346\215\242.c" @@ -0,0 +1,79 @@ +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +typedef int SElemType; +typedef struct{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; +typedef enum{ + OK, + ERROR, + OVERFLOW +}Status; +Status InitStack(SqStack *); +Status StackEmpty(SqStack ); +Status Push(SqStack *,SElemType ); +Status Pop(SqStack *,SElemType ); +int main() +{ + SElemType e; + SElemType N; + SqStack sq; + SqStack *S; + S=&sq; + + InitStack(S); + scanf("%d",&N); + while(N) + { + Push(S,N%8); + N=N/8; + } + while(!StackEmpty(S)) + { + Pop(S,e); + printf("%d",e); + } + + return 0; +} +Status InitStack(SqStack *s) +{ + s->base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if(!s->base)exit(OVERFLOW); + s->top=s->base; + s->stacksize=STACK_INIT_SIZE ; + return OK; +} +Status StackEmpty(SqStack *s) +{ + if(s->base==s->top) + return OK; + else + return ERROR; +} +Status Push(SqStack *s,SElemType e) +{ + if(s->top-s->base>=s->stacksize)//栈满,追加存储空间 + { + s->base=(SElemType *)realloc(s->base,(s->stacksize+STACKINCREMENT) * sizeof(SElemType)); + if(!s->base) + return OVERFLOW;//存储分配失败 + s->top=s->base+s->stacksize; + s->stacksize+=STACKINCREMENT; + } + else + *s->top++=e; + + +} +Status Pop(SqStack *s,SElemType e) +{ + if(s->top==s->base)//判断是否为空栈 + return ERROR; + e=*(s->top-1); + return OK; +} \ No newline at end of file diff --git "a/2017-1/HuanWoWeiLan/\347\256\227\346\263\2253.2.2/\347\256\227\346\263\2253.2.2.c" "b/2017-1/HuanWoWeiLan/\347\256\227\346\263\2253.2.2/\347\256\227\346\263\2253.2.2.c" new file mode 100644 index 00000000..e4c381ef --- /dev/null +++ "b/2017-1/HuanWoWeiLan/\347\256\227\346\263\2253.2.2/\347\256\227\346\263\2253.2.2.c" @@ -0,0 +1,82 @@ +#include +#include +#define MAX_STACK 100 + +struct stStack +{ + char szStack[MAX_STACK]; + int nTop; +}; + +void InitStack(stStack *s) +{ + s->nTop = -1; +} + +char Push(stStack *s, char c) +{ + if(s->nTop == MAX_STACK - 1) + return 0; + + s->nTop ++; + s->szStack[s->nTop] = c; + return c; +} + +char Pop(stStack *s) +{ + if (s->nTop == -1) + { + return 0; + } + char c = s->szStack[s->nTop]; + s->nTop--; + return c; +} + +int Check(char* szText) +{ + stStack *s; + InitStack(s); + int nLen = strlen(szText); + for (int i = 0; i < nLen; i++) + { + char c = szText[i]; + + switch (c) + { + case '(': + case '{': + case '[': + Push(s, c); + break; + + case ')': + if (Pop(s) != '(') + return 0; + break; + case '}': + if (Pop(s) != '{') + return 0; + break; + case ']': + if (Pop(s) != '[') + return 0; + break; + } + } + return 1; +} + +int main() +{ + char szText[100]; + scanf("%s", szText); + if (Check(szText)){ + printf("合法\n"); } + else + { + printf("失败\n"); + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/HuanWoWeiLan/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274" "b/2017-1/HuanWoWeiLan/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274" new file mode 160000 index 00000000..1a2adb24 --- /dev/null +++ "b/2017-1/HuanWoWeiLan/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274" @@ -0,0 +1 @@ +Subproject commit 1a2adb2469ef2dba4b6278c8257abdba4def5464 diff --git "a/2017-1/HuanWoWeiLan/\351\230\237\345\210\227\347\233\270\345\205\263\345\207\275\346\225\260\345\256\236\347\216\260" "b/2017-1/HuanWoWeiLan/\351\230\237\345\210\227\347\233\270\345\205\263\345\207\275\346\225\260\345\256\236\347\216\260" new file mode 160000 index 00000000..a02cc559 --- /dev/null +++ "b/2017-1/HuanWoWeiLan/\351\230\237\345\210\227\347\233\270\345\205\263\345\207\275\346\225\260\345\256\236\347\216\260" @@ -0,0 +1 @@ +Subproject commit a02cc559bfc9958a36d6d9976e463bc747c978e1 diff --git a/2017-1/Jasmine/exp01/Homework.c b/2017-1/Jasmine/exp01/Homework.c new file mode 100644 index 00000000..76431b5b --- /dev/null +++ b/2017-1/Jasmine/exp01/Homework.c @@ -0,0 +1,102 @@ +#include +#include +typedef struct list +{ + int data; + struct list *next; +}List; +List *Creat(int m[],int n)//创造一条已知长度的有序链表 +{ + List *head, *next, *present; + int a; + head = (List*)malloc(sizeof(List)); + head->next = NULL; + present = head; + next = NULL; + for (int i = 0;i < n;i++) + { + a=m[i]; + next = (List*)malloc(sizeof(List)); + next->data = a; + present->next = next; + present = next; + } + present->next = NULL; + return head; +} +List* MergeList(List *LA, List *LB)//采用尾插法建表的链表归并 算法 (A,B为递增链表,C要求为单调不减链表) +{ + List *pc = NULL; + List *pa = LA->next; + List *pb = LB->next; + List *LC; + LC = LA; + LC->next = NULL; + pc = LC; + free(LB); + //LB=NULL; + + while (pa != NULL && pb != NULL) + { + if (pa->data <= pb->data) + { + pc->next = pa; + + pc = pa; + pa = pa->next; + } + else + { + pc->next = pb; + + pc = pb; + pb = pb->next; + } + pc->next = NULL; + } + + if (pa == NULL) + { + pc->next = pb; + + } + else + { + pc->next = pa; + } + return LC; +} +void Print(List *head) +{ + List *p; + p = head->next; + while (p != 0) + { + printf("%d ", p->data); + p = p->next; + } + printf("\n"); +} +int main() +{ + List *head1, *head2, *head3; + int a[4] = { 3,5,8,11 }; + int b[7] = { 2,6,8,9,11,15,20 }; + printf("线性表LA:"); + head1 = Creat(a,4); + Print(head1); + + printf("线性表LB:"); + head2 = Creat(b,7); + Print(head2); + + + + + head3=MergeList(head1, head2); + Print(head3); + + + return 0; + +} \ No newline at end of file diff --git a/2017-1/Jasmine/exp01/Homework.cpp b/2017-1/Jasmine/exp01/Homework.cpp new file mode 100644 index 00000000..0b63cc6d --- /dev/null +++ b/2017-1/Jasmine/exp01/Homework.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +typedef struct list +{ + int data; + struct list *next; +}List; +List *Creat(int n)//创造一条已知长度的有序链表 +{ + List *head, *next, *present; + int a; + head = (List*)malloc(sizeof(List)); + head->next = NULL; + present = head; + next = NULL; + for (int i = 0;i < n;i++) + { + a = rand() % 20; + next = (List*)malloc(sizeof(List)); + next->data = a; + present->next = next; + present = next; + } + present->next = NULL; + return head; +} +List* MergeList(List *LA, List *LB)//采用尾插法建表的链表归并 算法 (A,B为递增链表,C要求为单调不减链表) +{ + List *pc = NULL; + List *pa = LA->next; + List *pb = LB->next; + List *LC; + LC = LA; + LC->next = NULL; + pc = LC; + free(LB); + //LB=NULL; + + while (pa != NULL && pb != NULL) + { + if (pa->data <= pb->data) + { + pc->next = pa; + + pc = pa; + pa = pa->next; + } + else + { + pc->next = pb; + + pc = pb; + pb = pb->next; + } + pc->next = NULL; + } + + if (pa == NULL) + { + pc->next = pb; + + } + else + { + pc->next = pa; + } + return LC; +} +void Print(List *head) +{ + List *p; + p = head->next; + while (p != 0) + { + printf("%d ", p->data); + p = p->next; + } + printf("\n"); +} + +int main() +{ + srand((unsigned)time(NULL)); + List *head1, *head2, *head3; + int len1, len2; + printf("随机生成线性表LA的长度:"); + len1 = rand() % 5; + head1 = Creat(len1); + + Print(head1); + + printf("随机生成线性表LB的长度:"); + len2 = rand() % 10; + head2 = Creat(len2); + Print(head2); + + + + + head3 = MergeList(head1, head2); + Print(head3); + + + return 0; + +} \ No newline at end of file diff --git "a/2017-1/Jasmine/exp01/\344\275\234\344\270\232\350\257\264\346\230\216.txt" "b/2017-1/Jasmine/exp01/\344\275\234\344\270\232\350\257\264\346\230\216.txt" new file mode 100644 index 00000000..b2e9fb9e --- /dev/null +++ "b/2017-1/Jasmine/exp01/\344\275\234\344\270\232\350\257\264\346\230\216.txt" @@ -0,0 +1,2 @@ +锘縣omework2.cpp涓轰慨鏀瑰悗缁撴灉锛屼娇鐢ㄩ殢鏈哄嚱鏁版潵鐢熸垚娴嬭瘯鏁版嵁锛屼絾鏄繖鏍风殑璇濇垜涓嶄細鍦ㄥ垱寤洪摼琛ㄧ殑鍚屾椂淇濊瘉瀹冩湁搴忥紝鍥犳鍒涘缓鐨勪袱鏉¢摼琛ㄦ槸鏃犲簭鐨勩傚洜姝ゅ綊骞剁殑閾捐〃骞堕潪浠庡皬鍒板ぇ锛岃屾槸绠鍗曞湴涓涓瀵规瘮褰掑苟 +homework.c鏄檺鍒朵簡娴嬭瘯鏁版嵁锛屽嵆宸茬煡涓ゆ潯鏈夊簭閾捐〃鐨勫綊骞躲 \ No newline at end of file diff --git a/2017-1/Jasmine/exp02/3-2-1.cpp b/2017-1/Jasmine/exp02/3-2-1.cpp new file mode 100644 index 00000000..770f11e7 --- /dev/null +++ b/2017-1/Jasmine/exp02/3-2-1.cpp @@ -0,0 +1,81 @@ +//进制数转化 + +#include +#include +#include +typedef enum +{Error,OK}Status; +//#define Status int +//#define Error 0 +//#define OK 1 +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +typedef struct{ + int *base; + int *top; + int stacksize; +}SqStack; +Status InitStack(SqStack *s) +{ + s->base=(int*)malloc(STACK_INIT_SIZE*sizeof(int)); + if(!s->base) + return Error; + else + { + s->top=s->base; + s->stacksize=STACK_INIT_SIZE; + return OK; + } +} +Status Push(SqStack *s,int e) +{ + if(s->top-s->base>=s->stacksize) + { + s->base=(int*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(int)); + if(!s->base) + return Error; + s->top=s->base+s->stacksize; + s->stacksize+=STACKINCREMENT; + } + *s->top=e; + s->top++; + return OK; +} +int StackEmpty(SqStack *s) +{ + if(s->top==s->base) + { + return 0; + } + else + return 1; +} +int Pop(SqStack *s) +{ + if(s->top==s->base) + return 0; + else + { + s->top--; + return *s->top; + } +} +int main() +{ + int n; + SqStack S; + InitStack(&S); + srand(time(NULL)); + n = rand() % 1024; + printf("将十进制数%d转化为八进制数\n", n); + while(n) + { + Push(&S,n%8); + n=n/8; + } + while(StackEmpty(&S)) + { + printf("%d",Pop(&S)); + } + return 0; +} \ No newline at end of file diff --git a/2017-1/Jasmine/exp02/3-2-2.cpp b/2017-1/Jasmine/exp02/3-2-2.cpp new file mode 100644 index 00000000..5b2a4206 --- /dev/null +++ b/2017-1/Jasmine/exp02/3-2-2.cpp @@ -0,0 +1,126 @@ +//括号配对 +#include +#include +#include +#define STACK_INIT_SIZE 20 +#define STACKINCREMENT 10 +//枚举状态定义 +typedef enum +{ + Error, OK +}Status; +//定义栈的结构体 +typedef struct { + char *base; + char *top; + int stacksize; +}SqStack; +//初始化一个空的栈a +Status InitStack(SqStack *s) +{ + s->base = (char*)malloc(STACK_INIT_SIZE * sizeof(char)); + if (!s->base) + return Error; + else + { + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; + } +} +//向栈中加入一个元素 +Status Push(SqStack *s, char e) +{ + //判断栈是否已满 + if (s->top - s->base >= s->stacksize) + { + s->base = (char*)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(char)); + if (!s->base) + return Error; + s->top = s->base + s->stacksize; + s->stacksize += STACKINCREMENT; + } + *s->top= e; + s->top++; + return OK; +} +//判断栈是否为空 +int StackEmpty(SqStack *s) +{ + if (s->top == s->base) + { + return 0; + } + else + return 1; +} +//删除栈顶元素 +Status Pop(SqStack *s) +{ + if (s->top == s->base) + return Error; + s->top--; + return OK; +} +//返回栈顶元素 +char GetTop(SqStack *s) +{ + char *e; + e = (char*)malloc(sizeof(char)); + if (s->top == s->base) + return Error; + *e = *(s->top -1); + return *e; +} +//判断匹配是否成功 +Status matching(SqStack *s,char *p) +{ + int status=1; + int len = strlen(p); + int i = 0; + while (i < len&&status) + { + switch (*p) + { + case '{': + case '(': + case '[': + Push(s, *p); + p++; + i++; + break; + case '}': + case ')': + case ']': + if (StackEmpty(s)&&(GetTop(s) == '{'||GetTop(s) == '['||GetTop(s) == '(')) + { + status = 1; + Pop(s); + i++; + } + else + { + status = 0; + i++; + } + } + } + if (!StackEmpty(s) && status == 1) + return OK; + else + return Error; +} +int main() +{ + SqStack s; + Status result; + char test[STACK_INIT_SIZE]; + scanf("%s", test); + InitStack(&s); + result = matching(&s, test); + if (result == OK) + printf("matched!\n"); + else + printf("not matched!\n"); + return 0; +} \ No newline at end of file diff --git a/2017-1/Jasmine/exp02/3-2-3.cpp b/2017-1/Jasmine/exp02/3-2-3.cpp new file mode 100644 index 00000000..9d22249c --- /dev/null +++ b/2017-1/Jasmine/exp02/3-2-3.cpp @@ -0,0 +1,107 @@ +#include +#include +#define STACK_INIT_SIZE 20 +#define STACKINCREMENT 10 +typedef enum +{ + Error, OK +}Status; +//结构体定义 +typedef struct { + char *base; + char *top; + int stacksize; +}SqStack; +//初始化一个空的栈a +Status InitStack(SqStack *s) +{ + s->base = (char*)malloc(STACK_INIT_SIZE * sizeof(char)); + if (!s->base) + return Error; + else + { + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; + } +} +//向栈中加入一个元素 +Status Push(SqStack *s, char e) +{ + //判断栈是否已满 + if (s->top - s->base >= s->stacksize) + { + s->base = (char*)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(char)); + if (!s->base) + return Error; + s->top = s->base + s->stacksize; + s->stacksize += STACKINCREMENT; + } + *s->top = e; + s->top++; + return OK; +} +//删除栈顶元素 +Status Pop(SqStack *s) +{ + if (s->top == s->base) + return Error; + s->top--; + return OK; +} +//清空栈 +Status ClearStack(SqStack *s) +{ + s->top = s->base; + return OK; +} +//打印栈中内容 +Status Print(SqStack *s) +{ + char *p; + p = s->base; + while (p != s->top) + { + printf("%c", *p); + p++; + } + return OK; +} +//行编辑程序 +Status LineEdit(SqStack *s) +{ + char str; + scanf("%c", &str); + while (str != '/') + { + while (str!='/'&&str != '\n') + { + switch (str) + { + case '#': + Pop(s); + break; + case '@': + ClearStack(s); + break; + default: + Push(s, str); + break; + } + scanf("%c", &str); + } + Print(s); + printf("\n"); + if(str!='/') + scanf("%c", &str); + } + return OK; +} + +int main() +{ + SqStack s; + InitStack(&s); + LineEdit(&s); + return 0; +} diff --git "a/2017-1/Jasmine/exp02/\344\275\234\344\270\232\350\257\264\346\230\216.txt" "b/2017-1/Jasmine/exp02/\344\275\234\344\270\232\350\257\264\346\230\216.txt" new file mode 100644 index 00000000..99d638a2 --- /dev/null +++ "b/2017-1/Jasmine/exp02/\344\275\234\344\270\232\350\257\264\346\230\216.txt" @@ -0,0 +1,19 @@ +锘跨涓涓 鏁板埗杞崲 + +闅忔満鐢熸垚涓涓皬浜1024鐨勬暟锛岄氳繃绋嬪簭鐢熸垚瀵瑰簲鐨勫叓杩涘埗鏁 + +绗簩涓 鎷彿鍖归厤 + +鎵嬪姩杈撳叆涓涓插瓧绗︿覆锛屽鈥滐紙{ }锛夆濃滐紙锛夛級[ ]鈥濈▼搴忓垽鏂緭鍏ョ殑鎷彿鏄惁鑳戒袱涓や竴缁勬寜椤哄簭鍖归厤锛岃緭鍑哄尮閰嶆垚鍔熲渕atched锛佲濇垨涓嶆垚鍔熲渘ot matched锛佲 + +绗笁涓 琛岀紪杈戠▼搴 + +閫氳繃杈撳叆涓娈典唬鐮侊紝#琛ㄧず閫鏍硷紝@琛ㄧず娓呯┖鏈锛/琛ㄧず杈撳叆缁撴潫锛岀▼搴忚緭鍑哄疄闄呮湁鏁堜唬鐮併 + +input锛 +whli##ilr#e(s#*s) + outcha@putchar(*s=#++);/ + +output: +while(*s) +putchar(*s++); \ No newline at end of file diff --git "a/2017-1/Jasmine/exp03/3-2-5\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" "b/2017-1/Jasmine/exp03/3-2-5\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" new file mode 100644 index 00000000..e8ce9953 --- /dev/null +++ "b/2017-1/Jasmine/exp03/3-2-5\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" @@ -0,0 +1,167 @@ +#include +#include +#include +#include +#define true 1 +#define false 0 +#define OPSETSIZE 8 +typedef int Status; + +unsigned char Prior[8][8] = +{ // 运算符优先级表 + // '+' '-' '*' '/' '(' ')' '#' '^' + /*'+'*/'>','>','<','<','<','>','>','<', + /*'-'*/'>','>','<','<','<','>','>','<', + /*'*'*/'>','>','>','>','<','>','>','<', + /*'/'*/'>','>','>','>','<','>','>','<', + /*'('*/'<','<','<','<','<','=',' ','<', + /*')'*/'>','>','>','>',' ','>','>','>', + /*'#'*/'<','<','<','<','<',' ','=','<', + /*'^'*/'>','>','>','>','<','>','>','>' +}; + +//StackChar类型的结点SC,储存运算符 +typedef struct StackChar +{ + char c; + struct StackChar *next; +}SC; + +//StackFloat类型的结点SF,储存运算数 +typedef struct StackFloat +{ + float f; + struct StackFloat *next; +}SF; + +SC*PushC(SC *s, char c)//SC类型运算符入栈Push,返回栈顶 +{ + SC *p = (SC*)malloc(sizeof(SC)); + p->c = c; + p->next = s; + return p; +} + +SF *PushF(SF *s, float f)//SF类型运算数入栈Push,返回p +{ + SF *p = (SF*)malloc(sizeof(SF)); + p->f = f; + p->next = s; + return p; +} + +SC *PopC(SC *s)//SC类型的指针Pop,删除栈顶运算符元素并返回其值 +{ + SC *q = s; + s = s->next; + free(q); + return s; +} + +SF* PopF(SF *s)//SF类型的指针Pop,删除栈顶运算数元素并返回其值 +{ + SF *q = s; + s = s->next; + free(q); + return s; +} + +float Operate(float a, unsigned char theta, float b)//用后缀表达式计算函数Operate +{ + switch (theta) + { + case '+': return a + b; + case '-': return a - b; + case '*': return a*b; + case '/': return a / b; + case '^': return pow(a, b); + default: return 0; + } +} + +char OPSET[OPSETSIZE] = { '+','-','*','/','(',')','#','^' }; + +Status In(char Test, char *TestOp)//判断是否为运算符 +{ + int Find = false; + for (int i = 0; i< OPSETSIZE; i++) + { + if (Test == TestOp[i]) + Find = true; + } + return Find; +} + +Status ReturnOpOrd(char op, char *TestOp)//返回是第几个运算符 +{ + for (int i = 0; i< OPSETSIZE; i++) + { + if (op == TestOp[i]) + return i; + } +} + +char precede(char Aop, char Bop) +{ + return Prior[ReturnOpOrd(Aop, OPSET)][ReturnOpOrd(Bop, OPSET)]; +} + +float EvaluateExpression(char* MyExpression) +{ + // 算术表达式求值的算符优先算法 + // 设OPTR和OPND分别为运算符栈和运算数栈,OP为运算符集合 + SC *OPTR = NULL; // 运算符栈,字符元素 + SF *OPND = NULL; // 运算数栈,实数元素 + char TempData[20]; + float Data, a, b; + char theta, *c, Dr[] = { '#','\0' }; + OPTR = PushC(OPTR, '#'); + c = strcat(MyExpression, Dr); + strcpy(TempData, "\0");//字符串拷贝函数 + while (*c != '#' || OPTR->c != '#') + { + if (!In(*c, OPSET)) + { + Dr[0] = *c; + strcat(TempData, Dr); //字符串连接函数 + c++; + if (In(*c, OPSET)) + { + Data = atof(TempData); //字符串转换函数(double) + OPND = PushF(OPND, Data); + strcpy(TempData, "\0"); + } + } + else // 不是运算符则进栈 + { + switch (precede(OPTR->c, *c)) + { + case '<': // 栈顶元素优先级低 + OPTR = PushC(OPTR, *c); + c++; + break; + case '=': // 脱括号并接收下一字符 + OPTR = PopC(OPTR); + c++; + break; + case '>': // 退栈并将运算结果入栈 + theta = OPTR->c;OPTR = PopC(OPTR); + b = OPND->f;OPND = PopF(OPND); + a = OPND->f;OPND = PopF(OPND); + OPND = PushF(OPND, Operate(a, theta, b)); + break; + } //switch + } + } //while + return OPND->f; +} //EvaluateExpression + +int main(void) +{ + char s[128]; + printf("请输入表达式:"); + gets(s); + printf("该表达式的值为:"); + printf("%s\b=%g\n", s, EvaluateExpression(s)); + return 0; +} \ No newline at end of file diff --git "a/2017-1/Jasmine/exp03/3-2-5\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" "b/2017-1/Jasmine/exp03/3-2-5\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" new file mode 100644 index 00000000..cfb0baa1 --- /dev/null +++ "b/2017-1/Jasmine/exp03/3-2-5\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" @@ -0,0 +1,165 @@ +#include +#include +#include +#include +#define true 1 +#define false 0 +#define OPSETSIZE 8 +typedef int Status; + +unsigned char Prior[8][8] = +{ // 运算符优先级表 + // '+' '-' '*' '/' '(' ')' '#' '^' + /*'+'*/'>','>','<','<','<','>','>','<', + /*'-'*/'>','>','<','<','<','>','>','<', + /*'*'*/'>','>','>','>','<','>','>','<', + /*'/'*/'>','>','>','>','<','>','>','<', + /*'('*/'<','<','<','<','<','=',' ','<', + /*')'*/'>','>','>','>',' ','>','>','>', + /*'#'*/'<','<','<','<','<',' ','=','<', + /*'^'*/'>','>','>','>','<','>','>','>' +}; + +typedef struct StackChar +{ + char c; + struct StackChar *next; +}SC; //StackChar类型的结点SC + +typedef struct StackFloat +{ + float f; + struct StackFloat *next; +}SF; //StackFloat类型的结点SF + +SC *Push(SC *s, char c) //SC类型的指针Push,返回p +{ + SC *p = (SC*)malloc(sizeof(SC)); + p->c = c; + p->next = s; + return p; +} + +SF *Push(SF *s, float f) //SF类型的指针Push,返回p +{ + SF *p = (SF*)malloc(sizeof(SF)); + p->f = f; + p->next = s; + return p; +} + +SC *Pop(SC *s) //SC类型的指针Pop +{ + SC *q = s; + s = s->next; + free(q); + return s; +} + +SF *Pop(SF *s) //SF类型的指针Pop +{ + SF *q = s; + s = s->next; + free(q); + return s; +} + +float Operate(float a, unsigned char theta, float b) //计算函数Operate +{ + switch (theta) + { + case '+': return a + b; + case '-': return a - b; + case '*': return a*b; + case '/': return a / b; + case '^': return pow(a, b); + default: return 0; + } +} + +char OPSET[OPSETSIZE] = { '+','-','*','/','(',')','#','^' }; + +Status In(char Test, char *TestOp) +{ + int Find = false; + for (int i = 0; i< OPSETSIZE; i++) + { + if (Test == TestOp[i]) + Find = true; + } + return Find; +} + +Status ReturnOpOrd(char op, char *TestOp) +{ + for (int i = 0; i< OPSETSIZE; i++) + { + if (op == TestOp[i]) + return i; + } +} + +char precede(char Aop, char Bop) +{ + return Prior[ReturnOpOrd(Aop, OPSET)][ReturnOpOrd(Bop, OPSET)]; +} + +float EvaluateExpression(char* MyExpression) +{ + // 算术表达式求值的算符优先算法 + // 设OPTR和OPND分别为运算符栈和运算数栈,OP为运算符集合 + SC *OPTR = NULL; // 运算符栈,字符元素 + SF *OPND = NULL; // 运算数栈,实数元素 + char TempData[20]; + float Data, a, b; + char theta, *c, Dr[] = { '#','\0' }; + OPTR = Push(OPTR, '#'); + c = strcat(MyExpression, Dr); + strcpy(TempData, "\0");//字符串拷贝函数 + while (*c != '#' || OPTR->c != '#') + { + if (!In(*c, OPSET)) + { + Dr[0] = *c; + strcat(TempData, Dr); //字符串连接函数 + c++; + if (In(*c, OPSET)) + { + Data = atof(TempData); //字符串转换函数(double) + OPND = Push(OPND, Data); + strcpy(TempData, "\0"); + } + } + else // 不是运算符则进栈 + { + switch (precede(OPTR->c, *c)) + { + case '<': // 栈顶元素优先级低 + OPTR = Push(OPTR, *c); + c++; + break; + case '=': // 脱括号并接收下一字符 + OPTR = Pop(OPTR); + c++; + break; + case '>': // 退栈并将运算结果入栈 + theta = OPTR->c;OPTR = Pop(OPTR); + b = OPND->f;OPND = Pop(OPND); + a = OPND->f;OPND = Pop(OPND); + OPND = Push(OPND, Operate(a, theta, b)); + break; + } //switch + } + } //while + return OPND->f; +} //EvaluateExpression + +int main(void) +{ + char s[128]; + puts("请输入表达式:"); + gets_s(s); + puts("该表达式的值为:"); + printf("%s\b=%g\n", s, EvaluateExpression(s)); + return 0; +} \ No newline at end of file diff --git a/2017-1/Jasmine/exp03/queue.cpp b/2017-1/Jasmine/exp03/queue.cpp new file mode 100644 index 00000000..3d1006e1 --- /dev/null +++ b/2017-1/Jasmine/exp03/queue.cpp @@ -0,0 +1,135 @@ +//队列操作的基本实现 + +#include +#include +#include +#define QelemType int +typedef enum +{ + Error, OK +}Status; +typedef struct QNode +{ + QelemType data; + struct QNode *next; +}QNode,*QueuePtr; +typedef struct +{ + QueuePtr front;//队头指针 + QueuePtr rear;//队尾指针 +}Linkqueue; +Status InitQueue(Linkqueue *q)//构造一个空队列 +{ + q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!q->front) + return Error; + q->front->next = NULL; + return OK; +} +Status DestroyQueue(Linkqueue *q)//销毁队列 +{ + while (q->front) + { + q->rear = q->front->next; + free(q->front); + q->front = q->rear; + } + return OK; +} +Status EnQueue(Linkqueue *q, QelemType e)//插入元素 +{ + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) + return Error; + p->data = e; + p->next = NULL; + q->rear->next = p; + q->rear = p; + return OK; +} +Status QueueEmpty(Linkqueue *q)//判断队列是否为空,是则返回OK,非则返回Error +{ + if (q->front == q->rear) + return OK; + else + return Error; +} +QelemType DeQueue(Linkqueue *q)//若队列不空,删除队头元素,并返回其值,否则返回Error +{ + if (QueueEmpty(q)) + return Error; + QueuePtr p; + p = q->front->next; + QelemType e; + e = p->data; + q->front->next=p->next; + free(p); + if (q->rear == p) + q->front = q->rear; + return e; +} +Status ClearQueue(Linkqueue *q)//将Q清为空队列 +{ + q->front = q->rear; + return OK; +} +int QueueLength(Linkqueue*q)//返回Q的元素个数 +{ + int count = 1; + QueuePtr p; + p = q->front->next; + while (p != q->rear) + { + count++; + p = p->next; + } + return count; +} +QelemType GetHead(Linkqueue*q)//若队列不空,则用e返回队头元素,否则返回Error +{ + if (QueueEmpty(q)) + return Error; + QueuePtr p; + p = q->front->next; + QelemType e; + e = p->data; + return e; +} +Status QueueTraverse(Linkqueue *q) +{ + QelemType e; + while (!QueueEmpty(q)) + { + e = DeQueue(q); + printf("%d ", e); + } + return OK; +} +int main() +{ + Linkqueue Q; + InitQueue(&Q); + srand(time(NULL)); + //随机生成队列长度 + int num = rand() % 5; + int i; + for (i = 0;i < num;i++) + { + //随机生成队列数据 + QelemType a; + a = rand() % 10; + EnQueue(&Q, a); + } + //计算并输出队列长度 + printf("The length of the queue is:"); + printf("%d\n", QueueLength(&Q)); + //打印出该队列 + printf("Print the queue:\n"); + QueueTraverse(&Q); + //销毁队列 + //DestroyQueue(&Q); + return 0; + + +} \ No newline at end of file diff --git "a/2017-1/Jasmine/exp03/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/Jasmine/exp03/\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..decbd19d Binary files /dev/null and "b/2017-1/Jasmine/exp03/\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/Jasmine/exp03/\350\277\220\350\241\214\347\273\223\346\236\234\346\210\252\345\233\276.png" "b/2017-1/Jasmine/exp03/\350\277\220\350\241\214\347\273\223\346\236\234\346\210\252\345\233\276.png" new file mode 100644 index 00000000..9328f2ac Binary files /dev/null and "b/2017-1/Jasmine/exp03/\350\277\220\350\241\214\347\273\223\346\236\234\346\210\252\345\233\276.png" differ diff --git "a/2017-1/Jasmine/exp03/\351\230\237\345\210\227\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/Jasmine/exp03/\351\230\237\345\210\227\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..decbd19d Binary files /dev/null and "b/2017-1/Jasmine/exp03/\351\230\237\345\210\227\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/Jasmine/exp04/\344\275\234\344\270\232\350\257\264\346\230\216.txt" "b/2017-1/Jasmine/exp04/\344\275\234\344\270\232\350\257\264\346\230\216.txt" new file mode 100644 index 00000000..dd123112 --- /dev/null +++ "b/2017-1/Jasmine/exp04/\344\275\234\344\270\232\350\257\264\346\230\216.txt" @@ -0,0 +1,11 @@ +锘匡豢涓轰簡鑺傜渷绌洪棿鍜屾椂闂达紝鎵鏈夌殑鎿嶄綔閮芥槸鍦ㄤ竴寮濮嬪缓绔嬬殑閾捐〃涓婂疄鐜扮殑锛屾棤闇寤虹珛鏂扮殑绌洪棿鍘诲偍瀛樺垎瑙e紑鐨勪袱涓摼琛紝鑰屾槸灏嗗師鏉ョ殑閾捐〃鍓柇鍐嶈繘琛岄噸鏂拌繛鎺ョ殑杩囩▼銆 + +鍏堥殢鏈虹敓鎴愪竴涓暱搴﹀ぇ浜庝簲灏忎簬鍗佺殑閾捐〃锛屽皢瀹冩墦鍗板嚭鏉ワ紝琛ㄥご鍌ㄥ瓨鐨勬暟鎹负閾捐〃闀垮害锛 + +鍦ㄥ皢琛ㄤ腑濂囨暟浣嶇殑鍏冪礌杩涜閲嶆柊杩炴帴锛屽垱寤轰竴涓柊鐨勮〃澶磆ead锛岀劧鍚庡皢鍋舵暟浣嶇殑鍏冪礌渚濇杩炴帴鍦╤ead涔嬪悗锛 + +閫氳繃GetLength鍑芥暟杩斿洖涓ゆ潯鏂伴摼琛ㄧ殑闀垮害锛屽苟鍌ㄥ瓨鍦ㄥご缁撶偣鐨刣ata鎴愬憳涓紱 + +鍒嗗埆鎵撳嵃涓ゆ潯鏂伴摼琛ㄧ殑闀垮害鍜屾垚鍛樺厓绱狅紱 + +绋嬪簭缁撴潫銆 \ No newline at end of file diff --git "a/2017-1/Jasmine/exp04/\345\210\206\350\247\243\347\272\277\346\200\247\350\241\250.cpp" "b/2017-1/Jasmine/exp04/\345\210\206\350\247\243\347\272\277\346\200\247\350\241\250.cpp" new file mode 100644 index 00000000..fc89d77b --- /dev/null +++ "b/2017-1/Jasmine/exp04/\345\210\206\350\247\243\347\272\277\346\200\247\350\241\250.cpp" @@ -0,0 +1,121 @@ +#include +#include +#include +typedef struct List +{ + int data; + struct List *next; +}List; + +List* CreatList(int n)//创建一个已知长度的链表,头结点的数据初始化为表长n,并返回头结点 +{ + List *head, *p, *q; + head = (List*)malloc(sizeof(List)); + head->data=n; + head->next = NULL; + srand(time(NULL)); + int a,i; + for (i = 0;i < n;i++) + { + a = rand() % 10; + p = (List*)malloc(sizeof(List)); + p->data = a; + if (head->next == NULL) + { + head->next = p; + q = p; + } + else + { + q->next = p; + q = p; + } + } + p->next = NULL; + return head; +} +void PrintList(List *s)//打印链表数据 +{ + List *p; + int num = s->data; + p = s->next; + for(num;num>0;num--) + { + printf("%3d", p->data); + p=p->next; + } + printf("\n"); +} +int GetLength(List *s)//获取并返回链表的长度 +{ + List *p; + int count = 0; + p = s->next; + while (p != NULL) + { + count++; + p = p->next; + } + return count; +} +List* SeparateList(List *s)//分解链表,将奇数位元素与偶数位元素分别存储在两个链表中 +{ + int count,count1,count2; + count = count1 = count2 = 0; + List*p,*head,*q,*r; + head = (List*)malloc(sizeof(List)); + head->next = NULL; + q = s->next; + p = s; + while (p != NULL&&q!=NULL) + { + p->next = q->next; + if(head->next==NULL) + { + head->next = q; + r = q; + p = q->next; + q = p->next; + } + else + { + r->next = q; + r = q; + p = q->next; + if (p != NULL) + q = p->next; + else + q = NULL; + } + + } + count1 = GetLength(s); + count2 = GetLength(head); + s->data = count1; + head->data = count2; + return head; +} + + +int main() +{ + int n; + srand(time(NULL)); + do + { + n = rand() % 10; + } while (n < 5); + List *list; + printf("Creat a list:"); + list = CreatList(n); + PrintList(list); + List*list2; + list2=SeparateList(list); + printf("The length of the list1 is:%d\n", list->data); + printf("Print list1:"); + PrintList(list); + printf("The length of the list2 is:%d\n", list2->data); + printf("Print list2:"); + PrintList(list2); + return 0; +} \ No newline at end of file diff --git "a/2017-1/Jasmine/exp04/\350\277\220\350\241\214\347\273\223\346\236\2341.png" "b/2017-1/Jasmine/exp04/\350\277\220\350\241\214\347\273\223\346\236\2341.png" new file mode 100644 index 00000000..97371b6a Binary files /dev/null and "b/2017-1/Jasmine/exp04/\350\277\220\350\241\214\347\273\223\346\236\2341.png" differ diff --git "a/2017-1/Jasmine/exp04/\350\277\220\350\241\214\347\273\223\346\236\2342.png" "b/2017-1/Jasmine/exp04/\350\277\220\350\241\214\347\273\223\346\236\2342.png" new file mode 100644 index 00000000..48d2d0bb Binary files /dev/null and "b/2017-1/Jasmine/exp04/\350\277\220\350\241\214\347\273\223\346\236\2342.png" differ diff --git "a/2017-1/Jasmine/exp04/\350\277\220\350\241\214\347\273\223\346\236\2343.png" "b/2017-1/Jasmine/exp04/\350\277\220\350\241\214\347\273\223\346\236\2343.png" new file mode 100644 index 00000000..c24ea2f5 Binary files /dev/null and "b/2017-1/Jasmine/exp04/\350\277\220\350\241\214\347\273\223\346\236\2343.png" differ diff --git "a/2017-1/Jasmine/exp05/\344\272\214\345\217\211\346\240\221.c" "b/2017-1/Jasmine/exp05/\344\272\214\345\217\211\346\240\221.c" new file mode 100644 index 00000000..b828cbf0 --- /dev/null +++ "b/2017-1/Jasmine/exp05/\344\272\214\345\217\211\346\240\221.c" @@ -0,0 +1,47 @@ +#include +#include +#define TelemType char +TelemType count = 0; +typedef struct BitNode +{ + TelemType data; + struct BitNode *lchild, *rchild; +}BitNode; +typedef enum +{ + error,ok +}Status; +BitNode* CreateBiTree(BitNode* T,TelemType *c) +{ + + if (c[count++] == ' ') + T = NULL; + else + { + T = (BitNode*)malloc(sizeof(BitNode)); + T->data = c[count-1]; + T->lchild=CreateBiTree(T->lchild,c); + T->rchild=CreateBiTree(T->rchild,c); + } + return T; +} +void PostOrder(BitNode* T) +{ + if (T!=NULL) + { + PostOrder(T->lchild); + PostOrder(T->rchild); + printf("%c ", T->data); + } +} +int main() +{ + BitNode* T=NULL; + char str[25] = "ABDG EH I K C F "; + TelemType *c = str; + printf("Create a BitTree:"); + T=CreateBiTree(T,c); + printf("%s\n", str); + printf("Postorder Traverse:"); + PostOrder(T); +} \ No newline at end of file diff --git "a/2017-1/Jasmine/exp05/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/Jasmine/exp05/\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..601b8495 Binary files /dev/null and "b/2017-1/Jasmine/exp05/\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/Jasmine/exp05/\350\277\220\350\241\214\347\273\223\346\236\2342.png" "b/2017-1/Jasmine/exp05/\350\277\220\350\241\214\347\273\223\346\236\2342.png" new file mode 100644 index 00000000..d71ed28e Binary files /dev/null and "b/2017-1/Jasmine/exp05/\350\277\220\350\241\214\347\273\223\346\236\2342.png" differ diff --git a/2017-1/Jasmine/exp06/Bitree leaf.c b/2017-1/Jasmine/exp06/Bitree leaf.c new file mode 100644 index 00000000..7c31c267 --- /dev/null +++ b/2017-1/Jasmine/exp06/Bitree leaf.c @@ -0,0 +1,82 @@ +#include +#include +#define TelemType char +int count = 0; +int count1 = 0; +typedef struct BitNode +{ + TelemType data; + struct BitNode *lchild, *rchild; +}BitNode; +typedef enum +{ + error, ok +}Status; +BitNode* CreateBiTree(BitNode* T, TelemType *c) +{ + + if (c[count++] == ' ') + T = NULL; + else + { + T = (BitNode*)malloc(sizeof(BitNode)); + T->data = c[count - 1]; + T->lchild = CreateBiTree(T->lchild, c); + T->rchild = CreateBiTree(T->rchild, c); + } + return T; +} +void PostOrder(BitNode* T)//计算所有节点个数 +{ + if (T != NULL) + { + PostOrder(T->lchild); + PostOrder(T->rchild); + printf("%c ", T->data); + } +} +void Point(BitNode* T) +{ + if (T != NULL) + { + count1++; + Point(T->lchild); + Point(T->rchild); + } +} +int LeafPoint(BitNode* T)//返回叶子节点个数 +{ + int m, n; + if (T == NULL) + { + return 0; + } + if (T->lchild == NULL&&T->rchild == NULL) + { + return 1; + } + else + { + m = LeafPoint(T->lchild); + n = LeafPoint(T->rchild); + } + return m + n; +} +int main() +{ + BitNode* T = NULL; + int count2, count3; + char str[25] = "ABDG EH I K C F "; + TelemType *c = str; + printf("Create a BitTree:"); + T = CreateBiTree(T, c); + printf("%s\n", str); + printf("Postorder Traverse:"); + PostOrder(T); + Point(T); + printf("\nTotal Points:%d", count1); + count2=LeafPoint(T); + printf("\nLeaf Points:%d", count2); + count3 = count1 - count2; + printf("\nNot LeafPoints:%d", count3); +} \ No newline at end of file diff --git a/2017-1/Jasmine/exp06/Bitree more.c b/2017-1/Jasmine/exp06/Bitree more.c new file mode 100644 index 00000000..55b7e32b --- /dev/null +++ b/2017-1/Jasmine/exp06/Bitree more.c @@ -0,0 +1,101 @@ +#include +#include +#define TelemType char +int count = 0; +typedef struct BitNode +{ + TelemType data; + struct BitNode *lchild, *rchild; +}BitNode; +typedef enum +{ + error, ok +}Status; +BitNode* CreateBiTree(BitNode* T, TelemType *c) +{ + + if (c[count++] == ' ') + { + T = NULL; + } + else + { + T = (BitNode*)malloc(sizeof(BitNode)); + T->data = c[count - 1]; + T->lchild = CreateBiTree(T->lchild, c); + T->rchild = CreateBiTree(T->rchild, c); + } + return T; +} +void PostOrder(BitNode* T) +{ + if (T != NULL) + { + PostOrder(T->lchild); + PostOrder(T->rchild); + printf("%c ", T->data); + } +} +int Depth(BitNode* T) +{ + int depthL, depthR, depth; + if (!T) + { + depth = 0; + } + else + { + depthL = Depth(T->lchild); + depthR = Depth(T->rchild); + depth = 1 + (depthL > depthR ? depthL : depthR); + } + return depth; +} +int a[10] = { 0 }; +int i = 0; +void Width(BitNode *T) +{ + if (T != NULL) + { + if (i == 0) + { + a[0] = 1; + i++; + if (T->lchild != NULL) a[i]++; + if (T->rchild != NULL) a[i]++; + } + else { + i++; + if (T->lchild != NULL) a[i]++; + if (T->rchild != NULL) a[i]++; + } + Width(T->lchild); + i--; + Width(T->rchild); + } +} +int MaxWidth() +{ + int max = 0,i; + for (i = 0;i < 10;i++) //找出最大宽度 + if (max < a[i]) + max = a[i]; + return max; +} + +int main() +{ + BitNode* T = NULL; + char str[25] = "ABDG EH I K C F "; + TelemType *c = str; + printf("Create a BitTree:"); + T = CreateBiTree(T, c); + printf("%s\n", str); + printf("Postorder Traverse:"); + PostOrder(T); + printf("\nThe depth of the BiTree is:"); + printf("%d", Depth(T)); + printf("\nThe width of the BiTree is:"); + Width(T); + printf("%d", MaxWidth()); +} \ No newline at end of file diff --git "a/2017-1/Jasmine/exp06/\345\217\266\345\255\220\350\277\220\350\241\214\347\273\223\346\236\2342.png" "b/2017-1/Jasmine/exp06/\345\217\266\345\255\220\350\277\220\350\241\214\347\273\223\346\236\2342.png" new file mode 100644 index 00000000..c3b9b094 Binary files /dev/null and "b/2017-1/Jasmine/exp06/\345\217\266\345\255\220\350\277\220\350\241\214\347\273\223\346\236\2342.png" differ diff --git "a/2017-1/Jasmine/exp06/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/Jasmine/exp06/\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..ef3f9d7b Binary files /dev/null and "b/2017-1/Jasmine/exp06/\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git a/2017-1/Jasmine/exp07/Picture.c b/2017-1/Jasmine/exp07/Picture.c new file mode 100644 index 00000000..c87b9135 --- /dev/null +++ b/2017-1/Jasmine/exp07/Picture.c @@ -0,0 +1,213 @@ +#include +#include + +#include"graph.h" +#include"queue.h" + +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; //无向图的邻接矩阵是对称的,为无向图顶点赋值,赋值为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操作为重复的,用一个函数来包装这个操作 + 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; + LinkQueue Q; + for (v = 0; v < G.vexnum; ++v) + { + visited[v] = false; //先初始化访问标志数组为FALSE + } + InitQueue(&Q); + EnQueue(&Q, 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)//找到最小路径输出 + { + EnQueue(&Q, w); + PrintFoot(Q, a); + flag = true; + } + if (!visited[w])//若u的邻接点没有被访问 + { + EnQueue(&Q, w); + visited[w] = true; + } + } + + if (flag) + { + break; + } + } +} + +//输出路径 +Status PrintFoot(LinkQueue Q, int start) +{ + int foot[MAX_VERTEX_NUM]; + int i; + QueuePtr p; + p = Q.rear; + 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)); + 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; +} diff --git a/2017-1/Jasmine/exp07/graph.h b/2017-1/Jasmine/exp07/graph.h new file mode 100644 index 00000000..c9093593 --- /dev/null +++ b/2017-1/Jasmine/exp07/graph.h @@ -0,0 +1,17 @@ +#ifndef H_GRAPH +#define H_GRAPH + +#define INFINITY 99999 +#define MAX_VERTEX_NUM 20 +int visited[MAX_VERTEX_NUM]; +typedef struct ArcCell //弧的定义 +{ + int adj; +}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; +typedef struct //图的定义 +{ + AdjMatrix arcs; + int vexnum, arcnum; +}Graph; + +#endif diff --git a/2017-1/Jasmine/exp07/queue.h b/2017-1/Jasmine/exp07/queue.h new file mode 100644 index 00000000..4bd1537d --- /dev/null +++ b/2017-1/Jasmine/exp07/queue.h @@ -0,0 +1,18 @@ +#ifndef H_QUEUE +#define H_QUEUE + +#define MAXQSIZE 100 +typedef struct QNode +{ + int data; + struct QNode *prious; + struct QNode *next; +}QNode, LinkList, *QueuePtr; + +typedef struct +{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +#endif \ No newline at end of file diff --git "a/2017-1/Jasmine/exp07/\346\210\252\345\233\27619.png" "b/2017-1/Jasmine/exp07/\346\210\252\345\233\27619.png" new file mode 100644 index 00000000..33e89d5c Binary files /dev/null and "b/2017-1/Jasmine/exp07/\346\210\252\345\233\27619.png" differ diff --git a/2017-1/Jasmine/exp08/BSTOutput.txt b/2017-1/Jasmine/exp08/BSTOutput.txt new file mode 100644 index 00000000..f7afee02 --- /dev/null +++ b/2017-1/Jasmine/exp08/BSTOutput.txt @@ -0,0 +1,6 @@ +8 3 1 6 4 5 7 10 14 19 22 30 +8 3 1 6 4 5 7 10 14 13 19 22 30 +10 3 1 6 4 5 7 14 13 19 22 30 +10 3 1 6 4 7 14 13 19 22 30 +10 3 1 6 4 7 14 13 19 22 20 30 +10 3 1 7 4 14 13 19 22 20 30 \ No newline at end of file diff --git "a/2017-1/Jasmine/exp08/\345\212\250\346\200\201\346\237\245\346\211\276\350\241\250.c" "b/2017-1/Jasmine/exp08/\345\212\250\346\200\201\346\237\245\346\211\276\350\241\250.c" new file mode 100644 index 00000000..48f62d29 --- /dev/null +++ "b/2017-1/Jasmine/exp08/\345\212\250\346\200\201\346\237\245\346\211\276\350\241\250.c" @@ -0,0 +1,135 @@ +#include +#include +#define ElemType int + +typedef struct NODE +{ + ElemType elem; /*数据元素字段*/ + struct NODE *lc, *rc; /*左、右指针字段*/ +}NodeType; /*二叉树结点类型*/ + +int SearchBST(NodeType *t, NodeType **p, NodeType **q, ElemType kx) + /*在二叉排序树t 上查找数据元素等于 kx 的元素,若找到,返回1,且q 指向该结点,p 指向其父结点;*/ + /*否则,返回0,且p 指向查找失败的最后一个结点*/ +{ + int flag = 0; + *q = t; + while (*q!=NULL) /*从根结点开始查找*/ + { + if (kx>(*q)->elem) /*kx 大于当前结点*q */ + { + *p = *q; + *q = (*q)->rc; + } /*将当前结点*q 的右孩子置为新根*/ + else + { + if (kx<(*q)->elem) /*kx 小于当前结点*q */ + { + *p = *q; + *q = (*q)->lc; + } /*将当前结点*q 的左子女置为新根*/ + else + { + flag = 1; + break; + } /*查找成功,返回*/ + } + }/*while*/ + return flag; +} + +int InsertBST(NodeType **t, ElemType kx)/*在二叉排序树*t 上插入数据为kx 的结点*/ +{ + NodeType *p=*t, *q, *s; + int flag = 0; + if (!SearchBST(*t, &p, &q, kx)); /*在*t 为根的子树上查找*/ + { + s = (NodeType*)malloc(sizeof(NodeType)); /*申请结点,并赋值*/ + s->elem = kx; + s->lc = NULL; + s->rc = NULL; + flag = 1; /*设置插入成功标志*/ + if (!p) + *t = s; /*向空树中插入时*/ + else + { + if (kx>p->elem) + p->rc = s; /*插入结点为p 的右子女*/ + else + p->lc = s; /*插入结点为p 的左子女*/ + } + } + return flag; +} + +int DeleteBST(NodeType **t, ElemType kx) +{ + NodeType *p = *t, *q, *s, **f; + int flag = 0; + if (SearchBST(*t, &p, &q, kx)) + { + flag = 1; /*查找成功,置删除成功标志*/ + if (p == q) + f = &(*t); /*待删结点为根结点时*/ + else /*待删结点非根结点时*/ + { + f = &(p->lc); if (kx>p->elem) f = &(p->rc); + } /*f 指向待删结点的父结点的相应指针域*/ + if (!q->rc) + *f = q->lc; /*若待删结点无右子树,以左子树替换待删结点*/ + else + { + if (!q->lc) + *f = q->rc; /*若待删结点无左子树,以右子树替换待删结点*/ + else /*既有左子树又有右子树*/ + { + p = q->rc;s = p; + while (p->lc) + { + s = p; + p = p->lc; + }/*在右子树上搜索待删结点的前驱p*/ + *f = p; + p->lc = q->lc; /*替换待删结点q,重接左子树*/ + if (s != p) + { + s->lc = p->rc; /*待删结点的右子女有左子树时,还要重接右子树*/ + p->rc = q->rc; + } + } + } + free(q); + } + return flag; +} +void PreOrder(NodeType *t)//先序遍历输出 +{ + if (t != NULL) + { + printf("%d ", t->elem); + PreOrder(t->lc); + PreOrder(t->rc); + + } +} + +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 }; + NodeType *t=NULL; + int i; + for (i = 0;i < 12;i++)//建立二叉排序树 + { + InsertBST(&t, a[i]); + } + PreOrder(t);//先序输出 + printf("\n"); + for (i = 0;i < 5;i++)//查找删除 + { + if (!DeleteBST(&t, b[i])) + InsertBST(&t, b[i]); + PreOrder(t); + printf("\n"); + } +} \ No newline at end of file diff --git "a/2017-1/Jasmine/exp08/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/Jasmine/exp08/\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..8748bab3 Binary files /dev/null and "b/2017-1/Jasmine/exp08/\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/LLF/\344\275\234\344\270\23213/ds exercise 13.c" "b/2017-1/LLF/\344\275\234\344\270\23213/ds exercise 13.c" new file mode 100644 index 00000000..cd92ff42 --- /dev/null +++ "b/2017-1/LLF/\344\275\234\344\270\23213/ds exercise 13.c" @@ -0,0 +1,143 @@ +//实现作业13题 链表分解 +//说明: +//时间上,Separate函数只遍历一次链表,时间最短 +//空间上,为实现循环链表,只重新分配头尾指针即可,空间最小 +//头指针存储链表长度 +#include +#include + +//定义链表 +typedef struct node +{ + char data; + struct node *next; +}*LinkList, Lnode; + +//创建链表 +void CreateList(LinkList *head, LinkList *tail, char ch) +{ + int n; + LinkList P; + if ((P = (LinkList)malloc(sizeof(Lnode))) == NULL) + { + exit(1); + } + P->data = ch; + P->next = NULL; + if (*head == NULL) + { + P->next = *head; + *head = P; + } + else + { + (*tail)->next = P; + } + *tail = P; + n=GetLength(*head); + (*head)->data=n; +} + +//分解链表 +void Separate(LinkList *headA, LinkList *headB, LinkList *tailB) +{ + int count = 0,countA,countB; + LinkList cA, pA; + char ch; + cA = NULL; + for (pA = *headA; pA != NULL; cA = pA, pA = pA->next) + { + ch = pA->data; + count++; + if (count % 2 == 0) + { + CreateList(headB, tailB, ch); + cA->next = pA->next; + } + } + countA = GetLength(*headA); + countB = GetLength(*headB); + (*headA)->data = countA; + (*headB)->data = countB; +} + +//获取链表长度 +int GetLength(LinkList s)//获取并返回链表的长度 +{ + LinkList p; + int count = 0; + p = s; + while (p != NULL) + { + count++; + p = p->next; + } + return count; +} + +//打印链表 +void VisitList(LinkList head) +{ + if(head!=NULL) + printf("%d ", head->data); + head = head->next; + while (head != NULL) + { + printf("%c ", head->data); + head = head->next; + } + printf("\n"); +} + +//销毁链表 +void DestroyList(LinkList *head, LinkList *tail) +{ + LinkList tempPtr; + while (*head != NULL) + { + tempPtr = *head; + *head = (*head)->next; + free(tempPtr); + } + *head = NULL; + *tail = NULL; +} + + +int main(void) +{ + LinkList headA = NULL, tailA = NULL, headB = NULL, tailB = NULL; + char ch; + printf("头指针存储链表长度\n"); + printf("以*结束输入:"); + while (1) + { + scanf_s(" %c", &ch); + if (ch == '*') + { + break; + } + else + { + CreateList(&headA, &tailA, ch); + } + } + printf("\n打印当前链表: "); + VisitList(headA); // 打印分解前的链表 + if (headA != NULL) // 链表不空的情况对其进行分解 + { + Separate(&headA, &headB, &tailB); //对链表进行分解 + } + else + { + printf("headA is empty.\n"); + } + //打印分解后的链表 + printf("奇数项链表: "); + VisitList(headA); + printf("偶数项链表: "); + VisitList(headB); + DestroyList(&headA, &tailA); //销毁链表 + DestroyList(&headB, &tailB); + return 0; +} \ No newline at end of file diff --git "a/2017-1/LLF/\345\205\210\345\272\217\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/ds homework 5.c" "b/2017-1/LLF/\345\205\210\345\272\217\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/ds homework 5.c" new file mode 100644 index 00000000..19a627ec --- /dev/null +++ "b/2017-1/LLF/\345\205\210\345\272\217\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/ds homework 5.c" @@ -0,0 +1,106 @@ +//第五次作业 实现以字符串输入先序建立二叉树、后序输出 +#include +#include + +//定义二叉树 +typedef struct BiTNode +{ + char data; + struct BiTNode *lchild, *rchild; //左右孩子指针 +}BiTNode,*BiTree; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +//读取字符 +int i = 0; +char getChar(char *s) +{ + + char c; + c = s[i]; + i++; + return c; +} + +//先序建立二叉树 +Status CreatBiTree(BiTree *root,char *a) +{ + char s; + s = getChar(a); + if (s == ' ') + { + *root = NULL; + } + else + { + *root = (BiTNode*)malloc(sizeof(BiTNode)); + if (!root) + { + return OVERFLOW; + } + (*root)->data = s; + CreatBiTree(&(*root)->lchild,a); + CreatBiTree(&(*root)->rchild,a); + } + return OK; +} + +//后序遍历二叉树 +Status PostOrderTraverse(BiTree root) +{ + if (root) + { + PostOrderTraverse(root->lchild); + PostOrderTraverse(root->rchild); + printf("%c", root->data); + } + return OK; +} + +//销毁二叉树 +Status destoryBiTree(BiTree root) +{ + if (!root) + { + return ERROR; + } + else + { + destoryBiTree(root->lchild); + destoryBiTree(root->rchild); + free(root); + } + +} + +int main() +{ + BiTree p1 = NULL; + char *ch1= "ABDG EH I K C F "; + printf("ch1 preorder: "); + puts(ch1); + printf("ch1 postorder: "); + CreatBiTree(&p1,ch1); + PostOrderTraverse(p1); + destoryBiTree(p1); + printf("\n"); + i = 0; + BiTree p2 = NULL; + char *ch2 = "BCE G DF "; + printf("ch2 preorder: "); + puts(ch2); + printf("ch2 postorder: "); + CreatBiTree(&p2, ch2); + PostOrderTraverse(p2); + destoryBiTree(p2); + + return 0; + +} + + diff --git "a/2017-1/LLF/\345\205\210\345\272\217\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/test.PNG" "b/2017-1/LLF/\345\205\210\345\272\217\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/test.PNG" new file mode 100644 index 00000000..8e1cf51d Binary files /dev/null and "b/2017-1/LLF/\345\205\210\345\272\217\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/test.PNG" differ diff --git "a/2017-1/LLF/\345\205\210\345\272\217\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/test1.PNG" "b/2017-1/LLF/\345\205\210\345\272\217\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/test1.PNG" new file mode 100644 index 00000000..5229329b Binary files /dev/null and "b/2017-1/LLF/\345\205\210\345\272\217\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/test1.PNG" differ diff --git "a/2017-1/LLF/\345\205\210\345\272\217\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/test2.PNG" "b/2017-1/LLF/\345\205\210\345\272\217\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/test2.PNG" new file mode 100644 index 00000000..859e7a11 Binary files /dev/null and "b/2017-1/LLF/\345\205\210\345\272\217\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221/test2.PNG" differ diff --git "a/2017-1/LLF/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232-\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/ds 7 graph.c" "b/2017-1/LLF/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232-\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/ds 7 graph.c" new file mode 100644 index 00000000..82b1a71f --- /dev/null +++ "b/2017-1/LLF/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232-\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/ds 7 graph.c" @@ -0,0 +1,157 @@ +//广大度优先遍历求示例无向图中两点间最短距离(用邻接矩阵) +#include +#include +#define MAXSIZE 100//最大存储 +#define MAX_VERTEX 20//图顶点数 + +typedef struct node//定义图 +{ + int data;//顶点存储数据 + struct node *next; +}node, *vexnode; + +typedef struct +{ + vexnode ad[MAXSIZE];//静态分配数组存储信息 +}Graph;//图的构成 + +//定义辅助队列数组用来存储路径信息 +struct queue +{ + int vertex;//顶点 + int pre_num;//第几层 +}queue[MAXSIZE]; + +int front, rear; + +void print(int front)//用于打印队列数组 +{ + while (front != 0) + { + int b = front; + front = queue[front].pre_num; + queue[b].pre_num = -1;//访问过后标记为-1 + } + front = 0;//front置0 + while (frontad[i] = NULL;//分别初始化结构体指针 + } + int c = 0; + node *p; + int j; + for (int i = 0; idata = j; + p->next = (*g)->ad[i]; + (*g)->ad[i] = p;//头插法构建邻接矩阵 + } + } + } +} + +//广度优先遍历查找最短路径 +void shortest_path(Graph *g, int v, int u, int *visited) +{ + node *p; + int m; + int find = 0;//find为flag,标记是否找到 + rear++; + for (int a = 0; a < MAXSIZE; a++)//初始化队列 + { + queue[a].vertex = 0; + queue[a].pre_num = 0; + } + queue[rear].vertex = v;//起点 + queue[rear].pre_num = -1;//标记该点 + visited[v] = 1;//把该点设为已经访问过 + while (front != rear && !find) + { + front++; + m = queue[front].vertex;//搜索的起点从队列依次搜索 + if (m == u)//如果找到了终点 + { + find = 1;//find标识已找到 + print(front); + return;//返回 + } + p = g->ad[m];//起点后面跟的数据 + while (p != NULL) + {//广度优先遍历 + if (visited[p->data] == 0)//如果没有访问过 + { + visited[p->data] = 1;//标识已访问 + rear++; + queue[rear].vertex = p->data; + queue[rear].pre_num = front; + } + p = p->next;//遍历下一个 + } + } +} +int main() +{ + int graph_array[][MAXSIZE] = { + + {0,1,1,1,0,0,1,0,0}, + {1,0,1,0,0,0,0,0,0}, + {1,1,0,0,0,0,0,0,0}, + {1,0,0,0,1,1,0,0,0}, + {0,0,0,1,0,1,0,0,0}, + {0,0,0,1,1,0,0,1,0}, + {1,0,0,0,0,0,0,1,1}, + {0,0,0,0,0,1,1,0,1}, + {0,0,0,0,0,0,1,1,0} + + }; + + Graph *g; + CreateGraph(&g, graph_array); + for (int i = 0; i < 9; i++) + { + + for (int j = i+1; j < 9; j++) + { + front = -1;//初始化front + rear = -1;//初始化rear + int visited[MAXSIZE] = { 0 };//初始化 visit + printf("%d<->%d: ", i + 1, j + 1);//输出 + shortest_path(g, i, j, visited);//求最短路径 + printf("\n"); + + } + printf("\n"); + } + for (int i = 0; i < MAX_VERTEX; i++) + { //释放内存 + node *p; + node *q; + p = g->ad[i]; + while (p != NULL)//当p不为空时 + { + q = p;//令q=p + p = p->next;//p指向下一个数据 + free(q);//释放q的内存 + } + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/LLF/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232-\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/test1.PNG" "b/2017-1/LLF/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232-\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/test1.PNG" new file mode 100644 index 00000000..2306d072 Binary files /dev/null and "b/2017-1/LLF/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232-\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/test1.PNG" differ diff --git "a/2017-1/LLF/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232-\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/test2.PNG" "b/2017-1/LLF/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232-\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/test2.PNG" new file mode 100644 index 00000000..db815eb7 Binary files /dev/null and "b/2017-1/LLF/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232-\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/test2.PNG" differ diff --git "a/2017-1/LLF/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232-\345\256\236\347\216\260\345\220\204\347\247\215\346\216\222\345\272\217/ds homework9 sort.c" "b/2017-1/LLF/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232-\345\256\236\347\216\260\345\220\204\347\247\215\346\216\222\345\272\217/ds homework9 sort.c" new file mode 100644 index 00000000..fbe45d1d --- /dev/null +++ "b/2017-1/LLF/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232-\345\256\236\347\216\260\345\220\204\347\247\215\346\216\222\345\272\217/ds homework9 sort.c" @@ -0,0 +1,332 @@ +//实现第九次作业 各种排序(必选+归并排序) 其中插入排序、希尔排序、冒泡排序、简单选择排序均未改变原测试数组顺序 +//在快速排序和归并排序中使用了递归,直接对原测试数组进行了排序 +#include +#include +#include + +void Print(int *t, int len,int c,int m); +void swap(int *a, int *b); +int c = 0; +int m = 0;//记录排序的比较与移动次数 + +//copy函数 +int * Copy(int s[],int len) +{ + int *r;//copy原数组 + r = (int *)malloc(len * sizeof(int)); + for (int m = 0; m < len; m++) + r[m] = s[m]; + return r; +} + +//直接插入排序 +void InsertSort(int s[], int len) +{ + int insertVal = 0; + int i = 0, j = 0; + c = 0; + m = 0; + for (i = 1; i < len; ++i) + { + insertVal = s[i]; + //若第i-1个元素大于第i个元素,则移动后插入; + //否则无需任何操作 + if (s[i - 1] > insertVal) + { + j = i - 1; + //寻找插入位置,一边寻找一边后移 + while (j >= 0 && s[j] > insertVal) + { + s[j + 1] = s[j]; + --j; + c++; + } + s[j + 1] = insertVal; + m++; + } + + } + +} + +//希尔排序 +void ShellSort(int s[], int len) +{ + int i, j, k; + c = 0; + m = 0; + int gap; //gap是分组的长度 + int temp; //因为希尔排序是在直接插入排序的基础上实现的,所以仍然需要监视哨 + for (gap = len / 2; gap>0; gap = gap / 2) + { + for (i = 0; i= 0 && s[k]>temp) + { + s[k + gap] = s[k]; + k = k - gap; + } + s[k + gap] = temp; + + } + c++; + }m++; + } + } +} + + +//冒泡排序 +void BubbleSort(int s[], int len) +{ + int i, j,t; + c = 0; + m = 0; + for (i = 0; i < len; i++) + { + for (j = i + 1; j < len; j++) + { + if (s[i] > s[j]) + { + swap(&s[i], &s[j]); + } + c++; + } + m++; + } +} + +//交换 +void swap(int *a, int *b) +{ + int temp; + + temp = *a; + *a = *b; + *b = temp; + + return; +} + +//简单选择排序 +void SelectSort(int s[], int len)//n为数组a的元素个数 +{ + c = 0; + m = 0; + //进行N-1轮选择 + for (int i = 1 - 1; i= i; j--) + { + if (s[j] < s[min]) + { + min = j; + } + m++; + } + //将第i小的数,放在第i个位置;如果刚好,就不用交换 + if (i != min) + { + swap(&s[i], &s[min]); + } + c++; + } +} + +//快速排序 +void QuickSort(int s[], int len, int low, int high) +{ + int i, j; + if (low < high) + { + i = low + 1; // 将s[low]作为基准数,因此从s[low+1]开始与基准数比较! + j = high; // s[high]是数组的最后一位 + + while (i < j) + { + if (s[i] > s[low]) // 如果比较的数组元素大于基准数,则交换位置。 + { + swap(&s[i], &s[j]); // 交换两个数 + j--; + c++; + } + else + { + i++; // 将数组向后移一位,继续与基准数比较。 + m++; + } + + } + + /* 跳出while循环后,i = j。 + * 此时数组被分割成两个部分 --> s[low+1] ~ s[i-1] < s[low] + * --> s[i+1] ~ s[high] > s[low] + * 这个时候将数组s分成两个部分,再将s[i]与s[low]进行比较,决定s[i]的位置。 + * 最后将s[i]与s[low]交换,进行两个分割部分的排序!以此类推,直到最后i = j不满足条件就退出! + */ + + if (s[i] >= s[low]) // 这里必须要取等“>=”,否则数组元素由相同的值时,会出现错误! + { + i--; + m++; + } + + swap(&s[low], &s[i]); // 交换s[i]与s[low] + + QuickSort(s, len, low, i); + QuickSort(s, len, j, high); + } +} + +//归并排序 +void merge(int s[], int low, int mid, int high) +{ + int i, k; + int *tmp = (int *)malloc((high - low + 1) * sizeof(int)); + //申请空间,使其大小为两个 + int left_low = low; + int left_high = mid; + int right_low = mid + 1; + int right_high = high; + for (k = 0; left_low <= left_high && right_low <= right_high; k++) + { // 比较两个指针所指向的元素 + if (s[left_low] <= s[right_low]) + { + tmp[k] = s[left_low++]; + m++; + } + else + { + tmp[k] = s[right_low++]; + m++; + } + c++; + } + if (left_low <= left_high) + { + + for (i = left_low; i <= left_high; i++) + { + tmp[k++] = s[i]; + m++; + } + } + if (right_low <= right_high) + { + + for (i = right_low; i <= right_high; i++) + { + tmp[k++] = s[i]; + m++; + } + } + for (i = 0; i < high - low + 1; i++) + { + s[low + i] = tmp[i]; + m++; + } + free(tmp); + return; +} + +void MergeSort(int s[], int first, int last) +{ + int mid = 0; + if (first> 1); + MergeSort(s, first, mid); + MergeSort(s, mid + 1, last); + merge(s, first, mid, last); + } + return; +} + +void Print(int *t,int len,int c,int m) +{ + + for (int j = 0; j < len; j++) + printf("%d ", t[j]); + printf("care: %d m: %d total: %d\n", c, m, c + m); + +} + +int main() +{ + int n; + srand((int)time(NULL)); /*定义这个可以产生不同的随机数*/ + n = rand() %5+10; + printf("%d\n", n); + int *t; + t = (int *)malloc(n*sizeof(int)); + for (int i = 0; i < n; i++) + { + t[i] = rand() % 200; + } + + //copy测试数组 + int *a,*b,*d,*e,*h; + a = Copy(t,n); + b = Copy(t, n); + e = Copy(t, n); + d = Copy(t, n); + h = Copy(t, n); + + printf("Example: "); + for (int j = 0; j < n; j++) + printf("%d ", t[j]); + printf("\n\n"); + + //直接插入排序 + printf("InsertSort: "); + InsertSort(a, n); + Print(a, n, c, m); + + //希尔排序 + printf("ShellSort: "); + ShellSort(b, n); + Print(b, n, c, m); + + //冒泡排序 + printf("BubbleSort: "); + BubbleSort(d, n); + Print(d, n, c, m); + + + //简单选择排序 + printf("SelectSort: "); + SelectSort(e, n); + Print(e, n, c, m); + + //快速排序 + c = 0; + m = 0; + printf("QuickSort: "); + QuickSort(h,n,0,n-1); + Print(h, n, c, m); + + //归并排序 + c = 0; + m = 0; + printf("MergeSort: "); + MergeSort(t, 0, n - 1); + Print(t, n, c, m); + + free(t); + free(a); + free(b); + free(d); + free(e); + free(h); + return 0; +} \ No newline at end of file diff --git "a/2017-1/LLF/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232-\345\256\236\347\216\260\345\220\204\347\247\215\346\216\222\345\272\217/test1.PNG" "b/2017-1/LLF/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232-\345\256\236\347\216\260\345\220\204\347\247\215\346\216\222\345\272\217/test1.PNG" new file mode 100644 index 00000000..4ed517a7 Binary files /dev/null and "b/2017-1/LLF/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232-\345\256\236\347\216\260\345\220\204\347\247\215\346\216\222\345\272\217/test1.PNG" differ diff --git "a/2017-1/LLF/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232-\345\256\236\347\216\260\345\220\204\347\247\215\346\216\222\345\272\217/test2.PNG" "b/2017-1/LLF/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232-\345\256\236\347\216\260\345\220\204\347\247\215\346\216\222\345\272\217/test2.PNG" new file mode 100644 index 00000000..17125459 Binary files /dev/null and "b/2017-1/LLF/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232-\345\256\236\347\216\260\345\220\204\347\247\215\346\216\222\345\272\217/test2.PNG" differ diff --git "a/2017-1/LLF/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232-\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" "b/2017-1/LLF/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232-\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" new file mode 100644 index 00000000..dcfcab63 --- /dev/null +++ "b/2017-1/LLF/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232-\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" @@ -0,0 +1,11 @@ +8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30, +13 +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30, +8 +10, 3, 1, 6, 4, 5, 7, 14, 13, 19, 22, 30, +5 +10, 3, 1, 6, 4, 7, 14, 13, 19, 22, 30, +20 +10, 3, 1, 6, 4, 7, 14, 13, 19, 22, 20, 30, +6 +10, 3, 1, 7, 4, 14, 13, 19, 22, 20, 30, \ No newline at end of file diff --git "a/2017-1/LLF/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232-\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/ds homework8 search.c" "b/2017-1/LLF/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232-\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/ds homework8 search.c" new file mode 100644 index 00000000..3c1cfec5 --- /dev/null +++ "b/2017-1/LLF/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232-\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/ds homework8 search.c" @@ -0,0 +1,202 @@ +//第八次作业 实现二叉排序树的查找、删除、插入操作 +#include +#include + +//定义树 +typedef struct treeNode +{ + int data; + struct treeNode *left; + struct treeNode *right; + +}treeNode; + +//查找最小元素 +treeNode* FindMin(treeNode *node) +{ + if (node == NULL) + { + return NULL; + } + if (node->left) + return FindMin(node->left);//若左子树非空,则继续向左寻找最小值 + else + return node; +} + +//查找最大元素 +treeNode* FindMax(treeNode *node) +{ + if (node == NULL) + { + return NULL; + } + if (node->right) + FindMax(node->right);//若右子树非空,继续向右寻找最大值 + else + return node; +} + +//插入数据 +treeNode * Insert(treeNode *node, int data) +{ + if (node == NULL) + { + treeNode *temp; + temp = (treeNode *)malloc(sizeof(treeNode)); + temp->data = data; + temp->left = temp->right = NULL; + return temp; + } + if (data >(node->data))//若大于则存在右子树中 + { + node->right = Insert(node->right, data); + } + else if (data < (node->data))//若小于则存在左子树中 + { + node->left = Insert(node->left, data); + } + return node; +} + +//创建树 +treeNode* createTree(treeNode *node, int *a,int n) +{ + for (int i = 0; i < n;i++) + node=Insert(node, a[i]); + return node; +} + +//删除关键字 +treeNode * Delete(treeNode *node, int data) +{ + treeNode *temp; + if (node == NULL) + { + printf("Element Not Found"); + } + else if (data < node->data) + { + node->left = Delete(node->left, data); + } + else if (data > node->data) + { + node->right = Delete(node->right, data); + } + else + { + if (node->right && node->left) + { + temp = FindMin(node->right); + node->data = temp->data; + node->right = Delete(node->right, temp->data); + } + else + { + temp = node; + if (node->left == NULL) + node = node->right; + else if (node->right == NULL) + node = node->left; + free(temp); + } + } + return node; + +} + +//查找关键字 +treeNode * Find(treeNode *node, int data) +{ + if (node == NULL) + { + //若树为空,则未查找到 + return NULL; + } + if (data > node->data) + { + //搜索右子树 + return Find(node->right, data); + } + else if (data < node->data) + { + //搜索左子树 + return Find(node->left, data); + } + else + { + //找到关键字 + return node; + } + +} + +//中序遍历 +void PrintInorder(treeNode *node) +{ + if (node == NULL) + { + return; + } + PrintInorder(node->left); + printf("%d ", node->data); + PrintInorder(node->right); +} + +//先序遍历 +void PrintPreorder(treeNode *node) +{ + if (node == NULL) + { + return; + } + printf("%d, ", node->data); + PrintPreorder(node->left); + PrintPreorder(node->right); +} + +//后序遍历 +void PrintPostorder(treeNode *node) +{ + if (node == NULL) + { + return; + } + PrintPostorder(node->left); + PrintPostorder(node->right); + printf("%d ", node->data); +} + +int main() +{ + //建立二叉树并插入测试数据 + treeNode *root = NULL; + int s[] = { 8,10,14,3,1,6,4,7,5,19,22,30 }; + int n = 12; + root=createTree(root, &s,n); + PrintPreorder(root); + printf("\n"); + //待查找关键字 + int a[] = { 13,8,5,20,6 }; + treeNode * temp; + for (int i = 0; i < 5; i++) + { + temp = Find(root, a[i]); + printf("%d\n", a[i]); + if (temp == NULL) + { + //若未查找到该元素,则插入 + Insert(root, a[i]); + PrintPreorder(root); + printf("\n"); + } + else + { + //若找到该元素,则删去 + Delete(root, a[i]); + PrintPreorder(root); + printf("\n"); + } + } + +} \ No newline at end of file diff --git "a/2017-1/LLF/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232-\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/test.PNG" "b/2017-1/LLF/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232-\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/test.PNG" new file mode 100644 index 00000000..e8c2bd42 Binary files /dev/null and "b/2017-1/LLF/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232-\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/test.PNG" differ diff --git "a/2017-1/LLF/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232-\346\261\202\346\267\261\345\272\246\345\256\275\345\272\246\345\217\266\345\255\220\347\273\223\347\202\271/ds homework6.c" "b/2017-1/LLF/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232-\346\261\202\346\267\261\345\272\246\345\256\275\345\272\246\345\217\266\345\255\220\347\273\223\347\202\271/ds homework6.c" new file mode 100644 index 00000000..4436a244 --- /dev/null +++ "b/2017-1/LLF/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232-\346\261\202\346\267\261\345\272\246\345\256\275\345\272\246\345\217\266\345\255\220\347\273\223\347\202\271/ds homework6.c" @@ -0,0 +1,228 @@ +//第六次作业 实现求二叉树的深度、宽度、叶子结点数、非叶子结点数 +#include +#include +#include +#define M 100 + +//定义二叉树 +typedef struct BiTNode +{ + char data; + struct BiTNode *lchild, *rchild; //左右孩子指针 +}BiTNode, *BiTree; + + +//定义用于求宽度的指针队列 +typedef struct Queue +{ + BiTNode *data[M]; + int f; + int r; +}Queue; + + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + + +//读取字符 +int i = 0; +char getChar(char *s) +{ + + char c; + c = s[i]; + i++; + return c; +} + + +//先序建立二叉树 +Status CreatBiTree(BiTree *root, char *a) +{ + char s; + s = getChar(a); + if (s == ' ') + { + *root = NULL; + } + else + { + *root = (BiTNode*)malloc(sizeof(BiTNode)); + if (!root) + { + return OVERFLOW; + } + (*root)->data = s; + CreatBiTree(&(*root)->lchild, a); + CreatBiTree(&(*root)->rchild, a); + } + return OK; +} + + +//后序遍历二叉树 +Status PostOrderTraverse(BiTree root) +{ + if (root) + { + PostOrderTraverse(root->lchild); + PostOrderTraverse(root->rchild); + printf("%c", root->data); + } + return OK; +} + + +//销毁二叉树 +Status destoryBiTree(BiTree root) +{ + if (!root) + { + return ERROR; + } + else + { + destoryBiTree(root->lchild); + destoryBiTree(root->rchild); + free(root); + } + +} + + +//求二叉树深度 +int TreeDepth(BiTree root) +{ + int rightdep = 0; + int leftdep = 0; + + if (root == NULL) + return -1; + + if (root->lchild != NULL) + leftdep = TreeDepth(root->lchild); + else + leftdep = -1; + + if (root->rchild != NULL) + rightdep = TreeDepth(root->rchild); + else + rightdep = -1; + + return (rightdep>leftdep) ? rightdep + 1 : leftdep + 1; +} + + + +//求二叉树宽度 +int TreeWidth(BiTree T) +{ + if (T == NULL) return 0; + int num = 0; + int max = 0; + Queue Q; + Q.r = 0; + Q.f = 0;//初始化队列 + memset(Q.data, NULL, M); + Q.data[Q.r++] = T; + + BiTNode *t = (BiTNode*)malloc(sizeof(BiTNode)); + t->data = ' '; + t->lchild = NULL; + t->rchild = NULL; + Q.data[Q.r++] = t;//构造队列 + while (Q.r != Q.f) + { + t = Q.data[Q.f++]; + if (t->data == ' ') + { + if (maxdata = ' '; + t->lchild = NULL; + t->rchild = NULL; + Q.data[Q.r++] = t; + } + + } + else + { + ++num; + if (t->lchild) Q.data[Q.r++] = t->lchild; + if (t->rchild) Q.data[Q.r++] = t->rchild; + } + } + return max; +} + +//求叶子结点数 +int LeafCount(BiTree root) +{ + if (root == NULL) + { + return 0; + } + else if ((root->lchild == NULL) && (root->rchild == NULL)) + { + return 1; + } + else + { + return LeafCount(root->lchild) + LeafCount(root->rchild); + } +} + +//求所有结点数 +int NodeCount(BiTree root) +{ + if (root == NULL) + return 0; + else + return (NodeCount(root->lchild) + NodeCount(root->rchild) + 1); +} + +int main() +{ + BiTree p1 = NULL; + char *ch1 = "ABDG EH I K C F "; + printf("ch1 preorder: "); + puts(ch1); + printf("ch1 postorder: "); + CreatBiTree(&p1, ch1); + PostOrderTraverse(p1); + printf("\nch1 depth is %d\n", TreeDepth(p1)); + printf("ch1 width is %d\n", TreeWidth(p1)); + printf("ch1 leaf node is %d\n", LeafCount(p1)); + printf("ch1 not leaf node is %d\n", NodeCount(p1) - LeafCount(p1)); + destoryBiTree(p1); + printf("\n"); + i = 0; + BiTree p2 = NULL; + char *ch2 = "BCE G DF "; + printf("ch2 preorder: "); + puts(ch2); + printf("ch2 postorder: "); + CreatBiTree(&p2, ch2); + PostOrderTraverse(p2); + printf("\nch2 depth is %d\n", TreeDepth(p2)); + printf("ch2 width is %d\n", TreeWidth(p2)); + printf("ch2 leaf node is %d\n", LeafCount(p2)); + printf("ch2 not leaf node is %d\n", NodeCount(p2) - LeafCount(p2)); + destoryBiTree(p2); + + return 0; + +} + + diff --git "a/2017-1/LLF/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232-\346\261\202\346\267\261\345\272\246\345\256\275\345\272\246\345\217\266\345\255\220\347\273\223\347\202\271/test.PNG" "b/2017-1/LLF/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232-\346\261\202\346\267\261\345\272\246\345\256\275\345\272\246\345\217\266\345\255\220\347\273\223\347\202\271/test.PNG" new file mode 100644 index 00000000..76d80c38 Binary files /dev/null and "b/2017-1/LLF/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232-\346\261\202\346\267\261\345\272\246\345\256\275\345\272\246\345\217\266\345\255\220\347\273\223\347\202\271/test.PNG" differ diff --git "a/2017-1/LLF/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232-\345\223\210\345\270\214\350\241\250/hash.c" "b/2017-1/LLF/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232-\345\223\210\345\270\214\350\241\250/hash.c" new file mode 100644 index 00000000..1755bf3b --- /dev/null +++ "b/2017-1/LLF/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232-\345\223\210\345\270\214\350\241\250/hash.c" @@ -0,0 +1,244 @@ +//Hash查找表的建立、关键字查找和重建哈希表(扩充原Hash表容量)操作实现 +//使用“开放定址法之线性探测再散列”解决Hash冲突 (随机数据插入) +#include +#include +#include +#include +#define NULLKEY 0 // 0为无记录标志 +#define N 10 // 数据元素个数 +#define SIZE 6 +#define SUCCESS 1 +#define UNSUCCESS 0 +#define DUPLICATE -1 +typedef int KeyType;// 设关键字域为整型 + + // 开放定址哈希表的存储结构 +typedef struct +{ + KeyType key;//关键字 + int val;//值 +}ElemType; // 数据元素类型 + +int hashsize[] = { 11,19,29,37 }; // 哈希表容量递增表,一个合适的素数序列 +int m = 0; // 哈希表表长,全局变量 + + //定义哈希表 +typedef struct +{ + ElemType *elem; // 数据元素存储基址,动态分配数组 + int count; // 当前数据元素个数 + int sizeindex; // hashsize[sizeindex]为当前容量 +}HashTable; + +// 构造一个空的哈希表 +int InitHashTable(HashTable *H) +{ + int i; + (*H).count = 0; // 当前元素个数为0 + (*H).sizeindex = 0; // 初始存储容量为hashsize[0] + m = hashsize[0]; + (*H).elem = (ElemType*)malloc(m * sizeof(ElemType)); + if (!(*H).elem) + exit(0); // 存储分配失败 + for (i = 0; i < m; i++) + { + (*H).elem[i].key = NULLKEY; // 未填记录的标志 + } + return 1; +} + +// 销毁哈希表H +void DestroyHashTable(HashTable *H) +{ + free((*H).elem); + (*H).elem = NULL; + (*H).count = 0; + (*H).sizeindex = 0; +} + +// 一个简单的哈希函数(m为表长,全局变量) +unsigned Hash(KeyType K) +{ + return K%m; +} + +// 开放定址法处理冲突 +void collision(int *p, int d) // 线性探测再散列 +{ + *p = (*p + d) % m; +} + +// 在开放定址哈希表H中查找关键码为K的元素,若查找成功,以p指示待查数据 +// 元素在表中位置,并返回SUCCESS;否则,以p指示插入位置,并返回UNSUCCESS +// c用以计冲突次数,其初值置零,供建表插入时参考。 +int SearchHash(HashTable H, KeyType K, int *p, int *c) +{ + *p = Hash(K); // 求得哈希地址 + while (H.elem[*p].key != NULLKEY && !(K == H.elem[*p].key)) + { + // 该位置中填有记录.并且关键字不相等 + (*c)++; + if (*ckey != NULLKEY) // 该单元有数据 + *p++ = *((*H).elem + i); + (*H).count = 0; + (*H).sizeindex++; // 增大存储容量 + m = hashsize[(*H).sizeindex]; + p = (ElemType*)realloc((*H).elem, m * sizeof(ElemType)); + if (!p) + exit(0); // 存储分配失败 + (*H).elem = p; + for (i = 0; i0 }\n", i); + } +} + +// 在开放定址哈希表H中查找关键码为K的元素,若查找成功,以p指示待查数据 +// 元素在表中位置,并返回SUCCESS;否则,返回UNSUCCESS +int Find(HashTable H, KeyType K, int *p) +{ + int c = 0; + *p = Hash(K); // 求得哈希地址 + while (H.elem[*p].key != NULLKEY && !(K == H.elem[*p].key)) + { // 该位置中填有记录.并且关键字不相等 + c++; + if (c%d }\n", p, r.key, r.val); +} + +ElemType newElemType(KeyType key, int val) +{ + ElemType temp = { key,val }; + return temp; +} + +ElemType* Inputs(int size) +{ + ElemType* a = (ElemType*)malloc(size * sizeof(ElemType)); + for (int i = 0; i < size; ++i) + { + a[i] = newElemType(rand() % 100, rand() % 100); + } + return a; +} + +int main() +{ + srand(time(0)); + ElemType* r = Inputs(SIZE); + HashTable h; + int i, j, p; + KeyType k=0; + + InitHashTable(&h); + for (i = 0; i +#include +#include +#include +#define LEN sizeof(struct LNode) + +//定义单链线性表 +typedef struct LNode +{ + int data; + struct LNode *next; +}LNode; + +//对链表中数据进行非递减排序 +LNode* SelectSort(LNode *L) +{ + LNode *p, *q, *small; + int temp; + + for (p = L->next; p->next != NULL; p = p->next) /*每次循环都找出一个最小值,将最小值交换到第一位,然后将指针向后移动一位*/ + { + small = p; + for (q = p->next; q; q = q->next) /*由前向后遍历,找出最小的节点*/ + { + if (q->data < small->data) + small = q; + } + if (small != p) + { + temp = p->data; + p->data = small->data; + small->data = temp; + } + } + return L; +} + + + +//创建单链线性表 +LNode* CreateList_L( int n)//逆位序输入n个元素的值,建立带表头结点的单链线性表L。 +{ + LNode * L; + int i; + + + if (n < 0)//若个数不合法,报错 + { + exit(0); + } + LNode * p; + L = (LNode *)malloc(LEN); + L->next = NULL;//先建立一个带头结点的单链表 + for (i = n; i > 0; --i) + { + p = (LNode *)malloc(LEN);//生成新结点; + p->data = (int)rand() % 2000;//随机生成元素值 + p->next = L->next; + L->next = p;//插入到表头 + } + + return SelectSort(L); + +}//CreateList_L + + + //已知单链线性表la和lb的元素按值非递减排列。 + //归并la和lb得到新的单链线性表lc,lc的元素也按值非递减排列。 +LNode* MergeList_L(LNode * La, LNode * Lb, LNode * Lc) +{ + struct LNode *pa, *pb, *pc; + pa = La->next; + pb = Lb->next; + pc = La; + Lc = pc;//用la的头结点作为lc的头结点 + while (pa&&pb) + { + if (pa->data <= pb->data) //将pa所指结点链接到pc所指结点之后 + { + pc->next = pa; + pc = pa; + pa = pa->next; + } + else //将pb所指结点链接到pc所指结点之后 + { + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + pc->next = pa ? pa : pb; //插入剩余段 + free(Lb); //释放lb的头结点 + return Lc; +}//MergeList_L + + //输出结点 +void Print_L(struct LNode *head) +{ + struct LNode *p; + p = head->next; + if (p != NULL) //如果不是空链表,就输出链表中所有节点 + { + do + { + printf("%d ", p->data);//输出数据 + p = p->next; //移到下一个节点 + } while (p != NULL); + } + printf("\n"); +}//Print_L + +int main(int argc, char* argv[]) +{ + srand((unsigned)time(NULL));//用时间做种 + + int n; + n = (int)rand() % 20;//随机生成个数 + + LNode * La, *Lb, *Lc;//定义链表 + + La = CreateList_L(n); + printf("La: "); + Print_L(La);//打印La + + Lb = CreateList_L(n); + printf("Lb: "); + Print_L(Lb);//打印Lb + + Lc = NULL; + + Lc=MergeList_L(La, Lb, Lc);//插入 + printf("Lc: "); + Print_L(Lc);//打印lc + + return 0; +} \ No newline at end of file diff --git "a/2017-1/LLF/\347\256\227\346\263\2253.2/ds 3.2.1.c" "b/2017-1/LLF/\347\256\227\346\263\2253.2/ds 3.2.1.c" new file mode 100644 index 00000000..3a2cb009 --- /dev/null +++ "b/2017-1/LLF/\347\256\227\346\263\2253.2/ds 3.2.1.c" @@ -0,0 +1,187 @@ +//ds 3.2.cpp +#include +#include +#include +#include + +#define STACK_INIT_SIZE 100//存储空间初始分配量 +#define STACKINCREMENT 10//存储空间分配增量 +typedef int SElemType; + +typedef struct { + SElemType *base;//在构造前和销毁后,base的值为NULL + SElemType *top;//栈顶指针 + int stacksize;//当前已分配的空间,以元素为单位 +}SqStack;//定义栈 + +typedef enum { + OK, + OVERFLOW, + ERROR +}Status;//定义返回值的枚举类型 + +typedef enum { + false, + true +} bool;//定义返回值 + + //栈的基本操作函数声明 + +Status InitStack(SqStack *S); +//构造一个空栈S + +Status DestroyStack(SqStack *S); +//销毁S + +Status ClearStack(SqStack *S); +//把S置为空栈 + +bool StackEmpty(SqStack *S); +//若S为空栈则返回TRUE,否则返回FALSE + +int StackLength(SqStack *S); +//返回S的元素个数,即栈的长度 + +Status GetTop(SqStack *S, SElemType e); +//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR + +Status Push(SqStack *S, SElemType e); +//插入元素e为新的栈顶元素 + +Status Pop(SqStack *S, SElemType *e); +//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR + +Status StackTraverse(SqStack *S, Status(*visit)(SElemType)); +//从栈底到栈顶依次对栈中每个元素调用函数visit()。一旦visit()失败,则操作失败 + +Status visit(SElemType e); +//打印栈中元素 + +Status conversion(SqStack *S, int input, int d); +//实现数制转换 + + + + +//栈的基本操作函数定义 + +Status InitStack(SqStack *S)//构造一个空栈S +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!S->base) { + return(OVERFLOW);//存储分配失败 + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; + +}//InitStack + +Status DestroyStack(SqStack *S)//销毁S +{ + free(S->base); + S->base = NULL; + S->top = NULL; + S->stacksize = 0; + return OK; +} + + +Status ClearStack(SqStack *S)//把S置为空栈 +{ + S->top = NULL; + return OK; +} + +bool StackEmpty(SqStack *S)//若S为空栈则返回TRUE,否则返回FALSE +{ + if (S->base==S->top) + return true; + else + return false; +} + +int StackLength(SqStack *S)//返回S的元素个数,即栈的长度 +{ + return *S->top + 1; +} + +Status GetTop(SqStack *S, SElemType e)//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR +{ + if (S->top == S->base)return ERROR; + e = *(S->top - 1); + return OK; +}//GetTop + +Status Push(SqStack *S, SElemType e)//插入元素e为新的栈顶元素 +{ + if (S->top - S->base >= S->stacksize) {//栈满,追加存储空间 + //暂时不用 + //S->base=(SElemaType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); + //if(!S->base){ + return(OVERFLOW);//存储分配失败 + //} + //S->top=S->base+S->stacksize; + //S->stacksize+=STACKINCREMENT; + } + *S->top++ = e; + return OK; +}//Push + +Status Pop(SqStack *S, SElemType *e)//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR +{ + if (StackEmpty(S))return ERROR; + *e = *--S->top; + return OK; +}//Pop + + + +Status StackTraverse(SqStack *S, Status(*visit)(SElemType))//从栈底到栈顶依次对栈中每个元素调用函数visit()。一旦visit()失败,则操作失败 +{ + while (S->top > S->base) + visit(*S->base++); + printf("\n"); + return OK; +}//StackTraverse + +Status visit(SElemType e) //打印栈中元素 +{ + printf("%d ", e); + return OK; +} //visit + +Status conversion(SqStack *S, int input, int d)//实现数制转换 +{ + int output; + InitStack(S); + while (input) { + Push(S, input%d); + input = input / d; + } + while (!StackEmpty(S)) + { + Pop(S, &output); + printf("%d", output); + } + + return OK; + +}//conversion + + +int main(int argc, char* argv[]) +{ + SqStack S; + int input; + int d; + + srand((unsigned)time(NULL));//用时间做种 + input = (int)rand() % 5000;//随机生成测试数据 + d = (int)rand() % 9;//随机生成转换数制 + printf("%d\n%d\n", input, d); + + conversion(&S, input, d); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/LLF/\347\256\227\346\263\2253.2/ds 3.2.2.c" "b/2017-1/LLF/\347\256\227\346\263\2253.2/ds 3.2.2.c" new file mode 100644 index 00000000..26940b27 --- /dev/null +++ "b/2017-1/LLF/\347\256\227\346\263\2253.2/ds 3.2.2.c" @@ -0,0 +1,217 @@ +//实现括号配对 +#include +#include +#include +#include + + +#define STACK_INIT_SIZE 100//存储空间初始分配量 +#define STACKINCREMENT 10//存储空间分配增量 +typedef char SElemType; + +typedef struct { + SElemType *base;//在构造前和销毁后,base的值为NULL + SElemType *top;//栈顶指针 + int stacksize;//当前已分配的空间,以元素为单位 +}SqStack;//定义栈 + +typedef enum { + OK, + OVERFLOW, + ERROR +}Status;//定义返回值的枚举类型 + +typedef enum { + false, + true +} bool;//定义返回值 + + //栈的基本操作函数声明 + +Status InitStack(SqStack *S); +//构造一个空栈S + +Status DestroyStack(SqStack *S); +//销毁S + +Status ClearStack(SqStack *S); +//把S置为空栈 + +bool StackEmpty(SqStack *S); +//若S为空栈则返回TRUE,否则返回FALSE + +int StackLength(SqStack *S); +//返回S的元素个数,即栈的长度 + +Status GetTop(SqStack *S, SElemType e); +//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR + +Status Push(SqStack *S, SElemType e); +//插入元素e为新的栈顶元素 + +Status Pop(SqStack *S, SElemType *e); +//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR + +Status StackTraverse(SqStack *S, Status(*visit)(SElemType)); +//从栈底到栈顶依次对栈中每个元素调用函数visit()。一旦visit()失败,则操作失败 + +Status visit(SElemType e); +//打印栈中元素 + +Status matching(SElemType *exp); +//实现括号的匹配 + + +#pragma once + +//栈的基本操作函数定义 + +Status InitStack(SqStack *S)//构造一个空栈S +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!S->base) { + return(OVERFLOW);//存储分配失败 + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; + +}//InitStack + +Status DestrpoyStack(SqStack *S)//销毁S +{ + free(S->base); + S->base = NULL; + S->top = NULL; + S->stacksize = 0; + return OK; +} + + +Status ClearStack(SqStack *S)//把S置为空栈 +{ + S->top = NULL; + return OK; +} + +bool StackEmpty(SqStack *S)//若S为空栈则返回TRUE,否则返回FALSE +{ + if (S->base == S->top) + return true; + else + return false; +} + +int StackLength(SqStack *S)//返回S的元素个数,即栈的长度 +{ + return *S->top + 1; +} + +Status GetTop(SqStack *S, SElemType e)//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR +{ + if (S->top == S->base)return ERROR; + e = *(S->top - 1); + return OK; +}//GetTop + +Status Push(SqStack *S, SElemType e)//插入元素e为新的栈顶元素 +{ + if (S->top - S->base >= S->stacksize) + { + return(OVERFLOW);//存储分配失败 + } + *S->top++ = e; + return OK; +}//Push + +Status Pop(SqStack *S, SElemType *e)//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR +{ + if (StackEmpty(S))return ERROR; + *e = *--S->top; + return OK; +}//Pop + + + +Status StackTraverse(SqStack *S, Status(*visit)(SElemType))//从栈底到栈顶依次对栈中每个元素调用函数visit()。一旦visit()失败,则操作失败 +{ + while (S->top > S->base) + visit(*S->base++); + printf("\n"); + return OK; +}//StackTraverse + +Status visit(SElemType e) //打印栈中元素 +{ + printf("%d ", e); + return OK; +} //visit + + +Status matching(SElemType *exp) //实现括号的匹配 +{ + SqStack S; + InitStack(&S); + int i = 0, flag = 0; + SElemType e; + while (exp[i] != '\0') + { + switch (exp[i]) + { + case '(': + Push(&S, exp[i]); + break; + case '{': + Push(&S, exp[i]); + break; + case '[': + Push(&S, exp[i]); + break; + case ')': + { + Pop(&S, &e); + if (e != '(') + flag = 1; + } + break; + case '}': + { + Pop(&S, &e); + if (e != '{') + flag = 1; + } + break; + case ']': + { + Pop(&S, &e); + if (e != '[') + flag = 1; + } + break; + default: + break; + } + if (flag) + break; + i++; + } + DestrpoyStack(&S); + if (!flag) + printf("括号匹配成功!\n"); + else + printf("括号匹配失败!\n"); + return OK; +} + +int main() +{ + + SElemType exp1[] = { "{([])}" }, exp2[] = {"({])"}; + printf("EXP1:"); + matching(exp1); + printf("EXP2:"); + matching(exp2); + + return 0; +} + \ No newline at end of file diff --git "a/2017-1/LLF/\347\256\227\346\263\2253.2/ds 3.2.3.c" "b/2017-1/LLF/\347\256\227\346\263\2253.2/ds 3.2.3.c" new file mode 100644 index 00000000..796c8ee1 --- /dev/null +++ "b/2017-1/LLF/\347\256\227\346\263\2253.2/ds 3.2.3.c" @@ -0,0 +1,175 @@ +//实现行编辑程序 +#include +#include +#include + +#define STACK_INIT_SIZE 100//存储空间初始分配量 +#define STACKINCREMENT 10//存储空间分配增量 +typedef char SElemType; + +typedef struct +{ + SElemType *base;//在构造前和销毁后,base的值为NULL + SElemType *top;//栈顶指针 + int stacksize;//当前已分配的空间,以元素为单位 +}SqStack;//定义栈 + +typedef enum { + OK, + OVERFLOW, + ERROR +}Status;//定义返回值的枚举类型 + + +typedef enum { + false, + true +} bool;//定义返回值 + + //栈的基本操作函数声明 + +Status InitStack(SqStack *S); +//构造一个空栈S + +Status DestroyStack(SqStack *S); +//销毁S + +Status ClearStack(SqStack *S); +//把S置为空栈 + +bool StackEmpty(SqStack *S); +//若S为空栈则返回TRUE,否则返回FALSE + +int StackLength(SqStack *S); +//返回S的元素个数,即栈的长度 + +Status GetTop(SqStack *S, SElemType e); +//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR + +Status Push(SqStack *S, SElemType e); +//插入元素e为新的栈顶元素 + +Status Pop(SqStack *S, SElemType *e); +//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR + +Status StackTraverse(SqStack *S); + +Status LineEdit(SqStack *s); +// 利用字符栈s,从终端接收一行并送至调用过程的数据区。 +//栈的基本操作函数定义 + +Status InitStack(SqStack *S)//构造一个空栈S +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!S->base) + { + return(OVERFLOW);//存储分配失败 + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; + +}//InitStack + + +Status DestroyStack(SqStack *S)//销毁S +{ + free(S->base); + S->base = NULL; + S->top = NULL; + S->stacksize = 0; + return OK; +} + + +Status ClearStack(SqStack *S)//把S置为空栈 +{ + S->top = S->base; //栈底栈顶相同为空栈 + return OK; +} + +bool StackEmpty(SqStack *S)//若S为空栈则返回TRUE,否则返回FALSE +{ + if (S->base == S->top) + return true; + else + return false; +} + + +Status Push(SqStack *S, SElemType e)//插入元素e为新的栈顶元素 +{ + if (S->top - S->base >= S->stacksize) {//栈满,追加存储空间 + //暂时不用 + //S->base=(SElemaType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); + //if(!S->base){ + return(OVERFLOW);//存储分配失败 + //} + //S->top=S->base+S->stacksize; + //S->stacksize+=STACKINCREMENT; + } + *(S->top)++ = e; + return OK; +}//Push + +Status Pop(SqStack *S, SElemType *e)//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR +{ + if (StackEmpty(S))return ERROR; + *e = *--(S->top); + return OK; +}//Pop + + + +Status StackTraverse(SqStack *S) +{ + char *p; + p = S->base; + while (p != S->top) + { + printf("%c", *p); + p++; + } + return OK; +}//StackTraverse + + + +// 利用字符栈s,从终端接收一行并送至调用过程的数据区。 +Status LineEdit(SqStack *S) +{ + char str,e; + scanf_s("%c", &str); + while (str != '/') + { + while (str != '/'&&str != '\n') + { + switch (str) + { + case '#': + Pop(S,&e); + break; + case '@': + ClearStack(S); + break; + default: + Push(S, str); + break; + } + scanf_s("%c", &str); + } + StackTraverse(S); + printf("\n"); + if (str != '/') + scanf_s("%c", &str); + } + return OK; +} + +int main() +{ + SqStack s; + InitStack(&s); + LineEdit(&s); + return 0; +} diff --git "a/2017-1/LLF/\347\256\227\346\263\2253.3/ds 3.3.c" "b/2017-1/LLF/\347\256\227\346\263\2253.3/ds 3.3.c" new file mode 100644 index 00000000..660fbd09 --- /dev/null +++ "b/2017-1/LLF/\347\256\227\346\263\2253.3/ds 3.3.c" @@ -0,0 +1,211 @@ +//顺序栈实现中缀表达式转后缀表达式并计算表达式的值 +//输出表达式 以及计算结果 +//可计算+-*/及含有括号的表达式 +#include +#include +#include + +#define N 50//输入字符串长度 +#define OK 1 +#define ERROR -1 +#define TRUE 1 +#define FALSE 0 +#define MAXSIZE 50 //栈容量 +typedef int Status; +typedef char ElemType; + +typedef struct { + ElemType data[MAXSIZE]; + int top;//栈顶指针 +}Stack; + +// 初始化栈 +Status InitStack(Stack *S) { + + int i; + for (i = 0; idata[i] = NULL; + S->top = -1; + return OK; +} + + +//压栈 +Status Push(Stack *S, ElemType e) { + if (MAXSIZE - 1 == S->top) + { + + return ERROR; + } + + ++(S->top); + S->data[S->top] = e; + return OK; +} + +//出栈 +Status Pop(Stack *S, ElemType *e) { + //将栈顶元素出栈,赋给e + if (-1 == S->top) + { + return ERROR; + } + *e = S->data[S->top]; + --(S->top); + return OK; +} + +Status Transform(char *mid,char *final) +{ + //中缀表达式为mid,转换成的后缀表达式传给final + //新建一个栈,来存储运算符 + char e; + Stack S; + if (OK != InitStack(&S)) + { + return ERROR; + } + //当字符串*mid未终止时,循环 + while (*mid) + { + //如果是数字,则直接输出 + if (*mid >= '0' && *mid <= '9') + { + *(final++) = *(mid++); + continue; + } + else if (*mid == '+' || *mid == '-' || *mid == '*' || *mid == '/' || *mid == '(' || *mid == ')') + { + //比较之前是否有更高优先级的符号 + if (S.top == -1 || '(' == *mid) + { + //当栈为空或遇到左括号时,直接入栈 + Push(&S, *(mid++)); + continue; + } + if (')' == *mid) + { + //遇到右括号时,栈顶元素依次出栈;直到遇到第一个左括号时结束 + Pop(&S, &e); + *(final++) = e; + while (Pop(&S, &e) && e != '(') + { + *(final++) = e; + } + mid++; + continue; + } + //接下来取出临时的栈顶元素,与当前输入的符号*mid相比较;当栈顶元素优先级大于等于输入符号的优先级时,出栈,且*mid入栈; + //否则符号入栈 + Pop(&S, &e); + if ('+' == *mid || '-' == *mid) + { + if (e == '(') + { + Push(&S, '('); + Push(&S, *(mid++)); + continue; + } + else + { + *(final++) = e; + Push(&S, *(mid++)); + continue; + } + } + else if ('*' == *mid || '/' == *mid) + { + if ('*' == e || '/' == e) + { + *(final++) = e; + Push(&S, *(mid++)); + continue; + } + else + { + Push(&S, e); + Push(&S, *(mid++)); + continue; + } + } + + } + else { + //输入的字符不合法 + return ERROR; + + } + } + //当待转换的字符已经结束时,符号栈应至少还有一个元素,将栈中的元素依次出栈 + while (S.top != -1) + { + Pop(&S, &e); + *(final++) = e; + } + //final的结束符 + *final = '\0'; +} + +//计算后缀表达式,若数字直接进栈,若是运算符则两个数字出栈,运算结果进栈,最终栈内只剩最后的结果 +int Cul(char *a,Stack s) +{ + int e1=0, e2=0,e=0; + + + for (int i = 0; i < strlen(a); i++) + { + if (a[i] >= '0'&&a[i] <= '9') + Push(&s,(int)( a[i]-'0')); + + else if (a[i] == '+') + { + Pop(&s, &e2); + Pop(&s, &e1); + e = e1 + e2 ; + Push(&s, e); + } + else if (a[i] == '-') + { + Pop(&s, &e2); + Pop(&s, &e1); + e = e1 - e2; + Push(&s, e); + } + else if (a[i] == '*') + { + Pop(&s, &e2); + Pop(&s, &e1); + e = e1 * e2; + Push(&s, e); + } + else if (a[i] == '/') + { + Pop(&s, &e2); + Pop(&s, &e1); + e = e1 / e2; + Push(&s, e); + } + } + Pop(&s, &e); + + return (int)(e); + +} + + +void main() +{ + Stack s; + InitStack(&s); + char middle[N]; + char final[N]; + int r; + printf("Please input an expression:\n"); + gets(middle); + Transform(middle,final); + printf("After:\n"); + puts(final); + r = Cul(final,s); + printf("The result is %d",r); + +} diff --git "a/2017-1/LLF/\347\256\227\346\263\2253.4.2/ds 3.4.2.c" "b/2017-1/LLF/\347\256\227\346\263\2253.4.2/ds 3.4.2.c" new file mode 100644 index 00000000..4d2eb2a3 --- /dev/null +++ "b/2017-1/LLF/\347\256\227\346\263\2253.4.2/ds 3.4.2.c" @@ -0,0 +1,169 @@ +#include +#include +typedef int QelemType; + +typedef struct QNode +{ + QelemType data; + struct QNode *next; +}QNode, *QueuePtr; + +typedef struct +{ + QueuePtr front;//队头指针 + QueuePtr rear;//队尾指针 + +}LinkQueue; + +typedef enum +{ + OK,//OK=0 + ERROR,//ERROR=1 + OVERFLOW//OVERFLOW=2 +}Status; + +typedef enum +{ + true, + false +}bool; + +Status InitQueue(LinkQueue *Q)//构造一个空队列Q +{ + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!Q->front)exit(OVERFLOW); //存储分配失败 + Q->front->next = NULL; + return OK; +} + +Status DestroyQueue(LinkQueue *Q)//销毁队列Q +{ + while (Q->front) + { + Q->rear = Q->front->next; + free(Q->front); + Q->front = Q->rear; + } + return OK; +} + +Status ClearQueue(LinkQueue *Q)//将Q清为空队列 +{ + QNode *p = Q->front; + while (p != NULL)//依次删除队列中的每一个结点,最后使队首指针为空 + { + Q->front = Q->front->next; + free(p); + p = Q->front; + } + Q->rear = NULL; + return OK; +} + +bool QueueEmpty(LinkQueue *Q)//判断队列是否为空 +{ + if (Q->front == NULL) + { + return false; + } + else return true; +} + +int QueueLength(LinkQueue *Q)//返回元素个数,即队列长度 +{ + QueuePtr p; + p = Q->front->next; + int i = 1; + while (p != Q->rear) + + { + i++; + p = p->next; + } + return i; + +} + +QelemType GetHead(LinkQueue *Q, QelemType *e) +//若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR +{ + if (Q->front == NULL) + { + exit(ERROR); + } + + return *e=Q->front->next->data; + +} + +Status EnQueue(LinkQueue *Q, QelemType e)//插入元素e为Q的新的队尾元素 +{ + QNode *p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p)exit(OVERFLOW);//存储分配失败 + p->data = e; + p->next = NULL; + Q->rear->next = p; + Q->rear = p; + return OK; +} + +Status DeQueue(LinkQueue *Q, QelemType *e) +//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK; +//否则返回ERROR +{ + QNode *p; + if (Q->front == Q->rear) + { + return ERROR; + } + p = Q->front->next; + Q->front->next = p->next; + if (Q->rear == p) + { + Q->rear = Q->front; + } + free(p); + return OK; +} + +Status QueueTraverse(LinkQueue Q) +{ + QueuePtr p; + QelemType e; + p = Q.front->next; + while (p) + { + e = p->data; + printf("%d", e); + printf(" "); + p = p->next; + } + printf("\n"); + return OK; +} + + +void main() +{ + LinkQueue q; + InitQueue(&q); + QelemType a[] = { 1,2,3,4,5,6 }; + QelemType e; + for (int i = 0; i < 6; i++) + { + EnQueue(&q, a[i]); + } + if (!QueueEmpty(&q)) printf("队列非空\n"); + printf("插入完成:"); + QueueTraverse(q); + printf("\n队列的长度: %d\n", QueueLength(&q)); + GetHead(&q, &e); + printf("队头元素:%d\n", e); + DeQueue(&q,&e); + printf("删除队头元素 %d 后 :",e); + QueueTraverse(q); + if (!ClearQueue(&q))printf("\n清空队列成功"); + if(!DestroyQueue(&q)) + printf("\n销毁队列成功"); +} diff --git a/2017-1/Micylt/hw1/MergeList.c b/2017-1/Micylt/hw1/MergeList.c new file mode 100644 index 00000000..d7dd3f1b --- /dev/null +++ b/2017-1/Micylt/hw1/MergeList.c @@ -0,0 +1,159 @@ +#include +#include +#include + +#define MAX 100 +#define OK 1 +#define OVERFLOW -2 +#define TRUE 1 +#define LEN 5 + +typedef int ElemType; + + +typedef struct LNode +{ + ElemType elem; + struct LNode* next; + +}LNode, *LinkList; + +void scan(ElemType *p) //新建输入输出函数 +{ + + scanf_s("%d", p); + +} + +void print(const ElemType n) +{ + + printf("%2d ", n); + +} + + +void CreateList_L(LinkList L, int n, void(*func)(ElemType*)) //创建单链表代码参考了ds目录下用户TheMasterOfMagic的代码 +{ + + + int i; + + LNode* p; + + L->next = NULL; + + + + for (i = 0; i < n; ++i) + { + p = (LinkList)malloc(sizeof(LNode)); + func(&p->elem); + p->next = L->next; + L->next = p; + + + } + + printf("创建完毕\n"); + printf("\n"); +} + +void MergeList(LinkList La, LinkList Lb, LinkList *Lc) //合并链表代码参考课本2.12算法修改而来 +{ + LNode *pa, *pb, *pc; + pa = La->next; pb = Lb->next; + (*Lc) = pc = La; + while (pa&&pb) + { + if (pa->elem <= pb->elem) + { + pc->next = pa; + pc = pa; + pa = pa->next; + } + else + { + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + printf("pa与pb中至少有一个为空,循环中止\n"); + printf("\n"); + pc->next = pa ? pa : pb; + free(Lb); +} + +/*遍历单链表*/ +void TraverseList_L(const LinkList L, void(*func)(ElemType)) { + + LNode *p = L->next; + + while (p) { + + func(p->elem); + + p = p->next; + + } + +} + + +int test1_iter(int k) { + + static int b = 20; + + return b -= k; + +} + +void test1(ElemType *p) +{ + + *p = test1_iter(rand() % 5 + 1); //随机产生测试数据 + +} + +int test2_iter(int k) +{ + + static int b = 20; + + return b -= k; + +} + +void test2(ElemType *p) +{ + + *p = test2_iter(rand() % 5 + 1); + +} + +int main() +{ + LinkList La; + LinkList Lb; + LinkList Lc; + srand(time(0)); + La = (LinkList)malloc(sizeof(LNode)); + printf("开始创建单链表La\n"); + CreateList_L(La, LEN + rand() % 5, test1); + Lb = (LinkList)malloc(sizeof(LNode)); + printf("开始创建单链表Lb\n"); + CreateList_L(Lb, LEN + rand() % 5, test2); + printf("\nLa内数据:"); + TraverseList_L(La, print); + printf("\n"); + printf("Lb内数据:"); + TraverseList_L(Lb, print); + printf("\n\n"); + + MergeList(La, Lb, &Lc); + printf("合并后的Lc:"); + TraverseList_L(Lc, print); + printf("\n\n"); + return 0; +} diff --git a/2017-1/Micylt/hw1/MergeList.cpp b/2017-1/Micylt/hw1/MergeList.cpp new file mode 100644 index 00000000..1e0054d8 --- /dev/null +++ b/2017-1/Micylt/hw1/MergeList.cpp @@ -0,0 +1,159 @@ +#include +#include +#include + +#define MAX 100 +#define OK 1 +#define OVERFLOW -2 +#define TRUE 1 +#define LEN 5 + +typedef int ElemType; + + +typedef struct LNode +{ + ElemType elem; + struct LNode* next; + +}LNode, *LinkList; + +void scan(ElemType *p) //新建输入输出函数 +{ + + scanf_s("%d", p); + +} + +void print(const ElemType n) +{ + + printf("%2d ", n); + +} + + +void CreateList_L(LinkList L, int n, void(*func)(ElemType*)) //创建单链表代码参考了ds目录下用户TheMasterOfMagic的代码 +{ + + + int i; + + LNode* p; + + L->next = NULL; + + + + for (i = 0; i < n; ++i) + { + p = (LinkList)malloc(sizeof(LNode)); + func(&p->elem); + p->next = L->next; + L->next = p; + + + } + + printf("创建完毕\n"); + printf("\n"); +} + +void MergeList(LinkList &La, LinkList &Lb, LinkList *Lc) //合并链表代码参考课本2.12算法修改而来 +{ + LNode *pa,*pb,*pc; + pa = La->next; pb = Lb->next; + (*Lc) = pc = La; + while (pa&&pb) + { + if (pa->elem <= pb->elem) + { + pc->next = pa; + pc = pa; + pa = pa->next; + } + else + { + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + printf("pa与pb中至少有一个为空,循环中止\n"); + printf("\n"); + pc->next = pa ? pa : pb; + free(Lb); +} + +/*遍历单链表*/ +void TraverseList_L(const LinkList L, void(*func)(ElemType)) { + + LNode *p = L->next; + + while (p) { + + func(p->elem); + + p = p->next; + + } + +} + + +int test1_iter(int k) { + + static int b = 20; + + return b -= k; + +} + +void test1(ElemType *p) +{ + + *p = test1_iter(rand() % 5 + 1); //随机产生测试数据 + +} + +int test2_iter(int k) +{ + + static int b = 20; + + return b -= k; + +} + +void test2(ElemType *p) +{ + + *p = test2_iter(rand() % 5 + 1); + +} + +int main() +{ + LinkList La; + LinkList Lb; + LinkList Lc; + srand(time(0)); + La = (LinkList)malloc(sizeof(LNode)); + printf("开始创建单链表La\n"); + CreateList_L(La, LEN + rand() % 5, test1); + Lb = (LinkList)malloc(sizeof(LNode)); + printf("开始创建单链表Lb\n"); + CreateList_L(Lb, LEN + rand() % 5, test2); + printf("\nLa内数据:"); + TraverseList_L(La, print); + printf("\n"); + printf("Lb内数据:"); + TraverseList_L(Lb, print); + printf("\n\n"); + + MergeList(La, Lb, &Lc); + printf("合并后的Lc:"); + TraverseList_L(Lc, print); + printf("\n\n"); + return 0; +} diff --git "a/2017-1/Micylt/hw1/\347\250\213\345\272\217\350\277\220\350\241\214\346\265\213\350\257\225.PNG" "b/2017-1/Micylt/hw1/\347\250\213\345\272\217\350\277\220\350\241\214\346\265\213\350\257\225.PNG" new file mode 100644 index 00000000..4db5779b Binary files /dev/null and "b/2017-1/Micylt/hw1/\347\250\213\345\272\217\350\277\220\350\241\214\346\265\213\350\257\225.PNG" differ diff --git a/2017-1/Micylt/hw2/hw2-1-a.png b/2017-1/Micylt/hw2/hw2-1-a.png new file mode 100644 index 00000000..2df2d69d Binary files /dev/null and b/2017-1/Micylt/hw2/hw2-1-a.png differ diff --git a/2017-1/Micylt/hw2/hw2-1-b.png b/2017-1/Micylt/hw2/hw2-1-b.png new file mode 100644 index 00000000..32f71461 Binary files /dev/null and b/2017-1/Micylt/hw2/hw2-1-b.png differ diff --git a/2017-1/Micylt/hw2/hw2-1.c b/2017-1/Micylt/hw2/hw2-1.c new file mode 100644 index 00000000..1b65f34f --- /dev/null +++ b/2017-1/Micylt/hw2/hw2-1.c @@ -0,0 +1,114 @@ +//进制转换程序 +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + + +typedef enum +{ + false, + true, +}bool; + +typedef enum +{ + OK, + OVERFLOW, + ERROR, +} Status; + +typedef struct +{ + int *base; + int *top; + int stacksize; +}SqStack; + + +Status InitStack(SqStack *s) +{ + printf("初始化一个栈\n"); + s->base = (int*)malloc(STACK_INIT_SIZE * sizeof(int)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; +} + +Status Push(SqStack *s, int e) +{ + if (s->top - s->base > s->stacksize) + { + return OVERFLOW; + } + printf("插入元素 %d 作为栈顶元素\n", e); + *s->top++ = e; + return OK; +} + +Status Pop(SqStack *s, int *e) +{ + if (StackEmpty(s)) + { + return ERROR; + } + *e = *--s->top; + return OK; +} + +bool StackEmpty(SqStack *s) +{ + if (s) + { + return s->base == s->top; + } + return true; +} + +Status conversion(SqStack *s, int input, int d) +{ + int e; + + if (d > 10) + { + return ERROR; + } + while (input) + { + Push(s, input % d); + input = input / d; + } + printf("转化后的数字:\n"); + while (!StackEmpty(s)) + { + Pop(s, &e); + printf("%d", e); + } + printf("\n"); + printf("转化结束\n"); +} + +int main() +{ + SqStack s; + int input; + int d; + printf("请输入要转化的数据:"); + scanf("%d", &input); + printf("请输入要转化的进制:"); + scanf("%d", &d); + if (d>10) { + printf("输入的数字不符合转换进制的要求\n"); + printf("请重新输入要转换的数\n"); + scanf("%d", &d); + } + printf("要转换的数据:%d\n", input); + printf("要转化的进制:%d\n", d); + InitStack(&s);//初始化一个栈 + conversion(&s, input, d); + return 0; +} \ No newline at end of file diff --git a/2017-1/Micylt/hw2/hw2-2-a.png b/2017-1/Micylt/hw2/hw2-2-a.png new file mode 100644 index 00000000..36e2218b Binary files /dev/null and b/2017-1/Micylt/hw2/hw2-2-a.png differ diff --git a/2017-1/Micylt/hw2/hw2-2.c b/2017-1/Micylt/hw2/hw2-2.c new file mode 100644 index 00000000..7f21f50e --- /dev/null +++ b/2017-1/Micylt/hw2/hw2-2.c @@ -0,0 +1,91 @@ +//括号匹配的检验程序 +#include +#include +#include +#define MAXLEN 50 +typedef struct stack +{ + char ch[50]; + int top; +}ST; +//栈的初始化 +ST *ST_Init() +{ + ST *st; + if (st = (ST *)malloc(sizeof(ST))) + { + st->top = 0; + return st; + } + return NULL; +} +//出栈操作 +int Pop(ST *st) +{ + if (st->top == 0) + { + printf("栈为空\n"); + return 0; + } + st->top--; + return st->ch[st->top]; +} + +//入栈操作 +void Push(ST *st, char c) +{ + if (st->top == MAXLEN) + { + printf("栈溢出\n"); + return; + } + st->ch[st->top] = c; + st->top++; +} +//符号检验函数 +void check_symbol(ST *st, char *a) +{ + int i; + Push(st, a[0]); + + for (i = 1; i< strlen(a); i++) + { + //遍历每一个元素,判断出栈还是入栈 + if ((a[i] == ']'&&st->ch[st->top - 1] == '[') || (a[i] == ')'&&st->ch[st->top - 1] == '('))//出栈的条件 + { + Pop(st); + } + else + { + Push(st, a[i]); + } + } + if (st->top == 0) + { + printf("括号是匹配的\n\n"); + } + else + + { + printf("括号不匹配\n\n"); + } +} + +void main() +{ + + while (1) + { + char s[50]; + ST *st; + st = ST_Init(); + printf("请输入一串括号||输入‘e’退出程序:\n"); + scanf("%s", s); + if (s[0] == 'e') + { + return ; + } + check_symbol(st, s); + } + +} diff --git a/2017-1/Micylt/hw2/hw2-3-a.png b/2017-1/Micylt/hw2/hw2-3-a.png new file mode 100644 index 00000000..a8e6f8f9 Binary files /dev/null and b/2017-1/Micylt/hw2/hw2-3-a.png differ diff --git a/2017-1/Micylt/hw2/hw2-3-b.png b/2017-1/Micylt/hw2/hw2-3-b.png new file mode 100644 index 00000000..694871c7 Binary files /dev/null and b/2017-1/Micylt/hw2/hw2-3-b.png differ diff --git a/2017-1/Micylt/hw2/hw2-3.c b/2017-1/Micylt/hw2/hw2-3.c new file mode 100644 index 00000000..81ae5e99 --- /dev/null +++ b/2017-1/Micylt/hw2/hw2-3.c @@ -0,0 +1,119 @@ +//行编辑程序。 +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef int SElemType; + +typedef enum { + false, + true, +}bool; +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + + +Status InitStack(SqStack *s) { //初始化栈 + printf("开始初始化一个栈\n"); + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!s->base) + return OVERFLOW; + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + printf("初始化一个栈结束\n\n"); +} + +//压栈 +Status Push(SqStack *s, char e) { + printf("插入元素 %c为栈顶元素\n", e); + if (s->top - s->base > s->stacksize) { + return OVERFLOW; + } + *s->top++ = e; + return OK; +} + +//出栈 +Status Pop(SqStack *s, char *e) { + if (StackEmpty(s)) { + return ERROR; + } + printf("清除前一个元素\n"); + *e = *--s->top; + return OK; +} +//清空栈 +bool StackEmpty(SqStack *s) { + if (s) { + return s->base == s->top; + } + return true; +} +//清除栈内元素 +Status ClearStack(SqStack *s) { + printf("清除前面的所有元素\n"); + s->top = s->base; + return OK; +} +//字符串处理 +Status LineEdit(SqStack *s,char ch) { + char str[100] = { '\0' }; + char c; + int i = 0; + while (ch != ';') { + while (ch != ';'&&ch != '\n') { + switch (ch) { + case '#':Pop(s, &c); + printf("\n"); + break; + case '@':ClearStack(s); + printf("\n"); + break; + default: + Push(s, ch); + } + ch = getchar(); + } + while (s->top != s->base) { + str[i] = *(s->base); + s->base++; + i++; + } + printf("经过整理后的字符串:\n"); + puts(str); + ClearStack(s); + if (ch != ';') { + ch = getchar(); + } + } + DestroyStack(s); + return OK; +} +Status DestroyStack(SqStack *s) { + while (!StackEmpty(s)) { + free(s->top); + s->top--; + } + s->base = NULL; + return OK; +} + +int main() { + SqStack s; + InitStack(&s); + printf("开始输入字符串(#为退格符,@为退行符):\n"); + char ch; + ch = getchar(); + LineEdit(&s,ch); + return 0; +} \ No newline at end of file diff --git a/2017-1/Micylt/hw3/hw3-2-a.png b/2017-1/Micylt/hw3/hw3-2-a.png new file mode 100644 index 00000000..5fa4809a Binary files /dev/null and b/2017-1/Micylt/hw3/hw3-2-a.png differ diff --git a/2017-1/Micylt/hw3/hw3-2-b.png b/2017-1/Micylt/hw3/hw3-2-b.png new file mode 100644 index 00000000..f1de791e Binary files /dev/null and b/2017-1/Micylt/hw3/hw3-2-b.png differ diff --git a/2017-1/Micylt/hw3/hw3-2-c.png b/2017-1/Micylt/hw3/hw3-2-c.png new file mode 100644 index 00000000..b015cfbb Binary files /dev/null and b/2017-1/Micylt/hw3/hw3-2-c.png differ diff --git a/2017-1/Micylt/hw3/hw3-2-d.png b/2017-1/Micylt/hw3/hw3-2-d.png new file mode 100644 index 00000000..3ee1ee1c Binary files /dev/null and b/2017-1/Micylt/hw3/hw3-2-d.png differ diff --git a/2017-1/Micylt/hw3/hw3-2-e.png b/2017-1/Micylt/hw3/hw3-2-e.png new file mode 100644 index 00000000..c83861f5 Binary files /dev/null and b/2017-1/Micylt/hw3/hw3-2-e.png differ diff --git a/2017-1/Micylt/hw3/hw3-2-f.png b/2017-1/Micylt/hw3/hw3-2-f.png new file mode 100644 index 00000000..1866fda7 Binary files /dev/null and b/2017-1/Micylt/hw3/hw3-2-f.png differ diff --git a/2017-1/Micylt/hw3/hw3-2-g.png b/2017-1/Micylt/hw3/hw3-2-g.png new file mode 100644 index 00000000..94e1dda9 Binary files /dev/null and b/2017-1/Micylt/hw3/hw3-2-g.png differ diff --git a/2017-1/Micylt/hw3/queen.c b/2017-1/Micylt/hw3/queen.c new file mode 100644 index 00000000..792031b4 --- /dev/null +++ b/2017-1/Micylt/hw3/queen.c @@ -0,0 +1,216 @@ +//有头尾指针的链式映像实现的单向队列的所有基本操作 +#include +#include + +typedef int QElemType; + +typedef enum +{ + false, + true, +}bool; +typedef enum +{ + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct QNode +{ + QElemType data; + struct QNode *next; +}QNode, *QueuePtr; + +typedef struct LinkQueue +{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; + + +Status InitQueue(LinkQueue q) //初始化队列 +{ + q.front = q.rear = (QueuePtr)malloc(sizeof(QNode)); + if (!q.front) + { + exit(OVERFLOW); + } + q.front->next = NULL; + printf("初始化完毕\n"); + return OK; +} +Status DestroyQueue(LinkQueue q) //销毁队列 +{ + while (q.front) + { + q.rear = q.front->next; + free(q.front); + q.front = q.rear; + } + return OK; +} +Status EnQueue(LinkQueue *q, QElemType e) //向队列插入元素 +{ + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) + { + exit(OVERFLOW); + } + p->data = e; + p->next = NULL; + q->rear->next = p; + q->rear->data = p->data; + q->rear = p; + return OK; +} +bool QueueEmpty(LinkQueue q) +{ + if (q.front == q.rear) + { + return true; + } + else + { + return false; + } +} +Status DeQueue(LinkQueue *q, QElemType *e) //删除队头元素 +{ + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (q->front == q->rear) + { + return ERROR; + } + p = q->front; + *e = p->data; + q->front = q->front->next; + if (q->rear == p) + { + q->rear = q->front; + } + free(p); + return OK; +} +Status CleanQueue(LinkQueue q) //清空队列 +{ + QueuePtr p; + QueuePtr next; + if (QueueEmpty(q)) + { + printf("将队列清空为空对列\n"); + return OK; + } + else + { + for (p = q.front; p; p = next) + { + next = p->next; + free(p); + p = next; + } + } + printf("将队列清空为空对列\n"); + return OK; +} +int QueueLength(LinkQueue q) //求队列长度 +{ + QueuePtr p; + QueuePtr next; + int length = 0; + p = q.front; + while (p != q.rear) + { + p = p->next; + length++; + } + return length; +} +Status GetHead(LinkQueue q, QElemType *e) //求队头元素 +{ + *e = q.front->data; + printf("此时队头元素为%d\n", *e); + return OK; +} +Status TraverseQueue(LinkQueue q) //遍历队列 +{ + QueuePtr p; + printf("开始遍历:\n"); + if (QueueEmpty(q)) + { + printf("此时队列里不存在元素\n"); + return OK; + } + p = q.front; + while (p != q.rear) + { + printf("%d ", p->data); + p = p->next; + } + printf("\n"); + return OK; +} + +int main() +{ + LinkQueue a; + QElemType e; + int len; + int n; + int num; + int flag = 1; + a.front = a.rear = (QueuePtr)malloc(sizeof(QNode)); + do + { + + + printf("----------队列基本操作如下:---------\n"); + printf("-----------1.初始化队列-------------\n"); + printf("-----------2.向队列中插入元素-------\n"); + printf("-----------3.求队列长度-------------\n"); + printf("-----------4.删除队头元素(空队列无效)\n"); + printf("-----------5.遍历队列---------------\n"); + printf("-----------6.退出-------------------\n"); + printf("请选择您的操作:"); + scanf("%d",&n); + switch (n) + { + case 1: + InitQueue(a); + TraverseQueue(a); + len = QueueLength(a); + printf("此时队列长度为 %d\n\n", len); + break; + case 2: + + printf("请输入要插入的元素:"); + scanf("%d", &num); + EnQueue(&a, num); + TraverseQueue(a); + len = QueueLength(a); + printf("此时队列长度为 %d\n\n", len); + break; + + case 3: + len = QueueLength(a); + printf("此时队列长度为 %d\n\n", len); + break; + + case 4: + GetHead(a, &e); + DeQueue(&a, &e); + printf("此时队列不为空,删除队头元素%d\n", e); + TraverseQueue(a); + printf("\n"); + break; + case 5: + TraverseQueue(a); + break; + default: + flag = 0; + break; + } +} while (flag==1); + return 0; +} \ No newline at end of file diff --git a/2017-1/Micylt/hw4/4-06.c b/2017-1/Micylt/hw4/4-06.c new file mode 100644 index 00000000..c92df31b --- /dev/null +++ b/2017-1/Micylt/hw4/4-06.c @@ -0,0 +1,136 @@ +//链表拆分程序 +#include +#include +#include +typedef int ElemType; + +typedef struct LNode +{ + ElemType data; + struct LNode *next; +}LNode, *LinkList; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +} Status; + + +Status CreateList(LinkList L, int n) +{ + printf("开始创建初始链表\n"); + L->next = NULL;//头结点; + int i = 0; + int num; + LinkList p; + for (i = n; i > 0; i--) + { + p = (LinkList)malloc(sizeof(LNode));//生成新结点; + num = rand() % 101; //使用随机整数作为链表数据 + p->data = num; + p->next = L->next; + L->next = p; + } + printf("链表创建完毕\n"); + return OK; +} +Status Traverse(LinkList L) //单链表遍历 +{ + LinkList Line; + Line = (LinkList)malloc(sizeof(LNode)); + if (Line->next == NULL) + { + return ERROR; + } + Line = L->next; + while (Line->next != NULL) + { + printf("%d ", Line->data); + Line = Line->next; + } + printf("%d", Line->data); + printf("\n遍历结束\n"); + return OK; +} +Status Traverse_loop(LinkList L) //循环链表的遍历 +{ + LinkList p = L; + LinkList q = L; + if (p->next == NULL) + { + return ERROR; + } + printf("链表长度为%d\n", q->data); + q = q->next; + while (q->next != p) + { + printf("%d\n", q->data); + if (q->next != NULL) + { + q = q->next; + } + } + printf("%d", q->data); + printf("\n遍历结束\n"); + return OK; +} +Status List_Distribute(LinkList list, LinkList list1, LinkList list2) // 链表拆分 +{ + LinkList p = list; + LinkList p1 = list1; + LinkList p2 = list2; + list1->data = 0; + list2->data = 0; + p = p->next; + printf("开始进行链表拆分\n"); + while (p != NULL) + { + p1->next = p; + p1 = p1->next; + list1->data++; + p = p->next; + if (p != NULL) + { + p2->next = p; + p2 = p1->next; + list2->data++; + p = p->next; + } + } + p1->next = list1; + p2->next = list2; + printf("链表拆分结束\n"); + return OK; +} + +int main() +{ + LinkList La; + LinkList L1; + LinkList L2; + int n; + La = (LinkList)malloc(sizeof(LNode)); + L1 = (LinkList)malloc(sizeof(LNode)); + L2 = (LinkList)malloc(sizeof(LNode)); + + printf("请输入生成链表长度:"); + scanf("%d", &n); + CreateList(La, n); + + printf_s("开始遍历原链表La:\n"); + Traverse(La); + printf("\n"); + + List_Distribute(La, L1, L2); + printf("\n"); + + printf("开始遍历奇数链表\n"); + Traverse_loop(L1); + printf("\n"); + + printf("开始遍历偶数链表\n"); + Traverse_loop(L2); + printf_s("\n"); +} \ No newline at end of file diff --git a/2017-1/Micylt/hw4/hw4-a.png b/2017-1/Micylt/hw4/hw4-a.png new file mode 100644 index 00000000..2827912c Binary files /dev/null and b/2017-1/Micylt/hw4/hw4-a.png differ diff --git a/2017-1/Micylt/hw4/hw4-b.png b/2017-1/Micylt/hw4/hw4-b.png new file mode 100644 index 00000000..4d89fe4e Binary files /dev/null and b/2017-1/Micylt/hw4/hw4-b.png differ diff --git a/2017-1/Micylt/hw5/hw5-1.png b/2017-1/Micylt/hw5/hw5-1.png new file mode 100644 index 00000000..922f2778 Binary files /dev/null and b/2017-1/Micylt/hw5/hw5-1.png differ diff --git a/2017-1/Micylt/hw5/tree.c b/2017-1/Micylt/hw5/tree.c new file mode 100644 index 00000000..a89a79d8 --- /dev/null +++ b/2017-1/Micylt/hw5/tree.c @@ -0,0 +1,94 @@ +//建立二叉树 +#include +#include + +typedef enum +{ + false, + true, +}bool; +typedef enum +{ + OK, + OVERFLOW, + ERROR, +} Status; + +typedef struct BiTNode +{ + char data; + struct BiTNode *lchild, *rchild; +}BiTNode, *BiTree; + +typedef struct +{ + BiTree base; + BiTree top; + int stacksize; +}SqStack; + + +int i = 0; + +Status CreateBiTree(BiTree *T, char *c) +{ + char ch; + ch = c[i]; + i++; + if (ch == '*') + { + *T = NULL; + } + else + { + if (!(*T = (BiTNode *)malloc(sizeof(BiTNode)))) + { + exit(OVERFLOW); + } + (*T)->data = ch; + CreateBiTree(&(*T)->lchild, c); + CreateBiTree(&(*T)->rchild, c); + } + return OK; +} + +Status PostOrderTraverse(BiTree T) +{ + if (T) + { + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c", T->data); + } + return OK; +} + +void print(char t[]) +{ + int i = 0; + do + { + + printf("%c", t[i++]); + } while (t[i]!='\0'); +} + +int main() +{ + + + char t2[30]; + BiTree T = NULL; + printf("请输入测试字符串:"); + gets(t2); + printf("开始建立第一个二叉树:\n"); + print(t2); + printf("\n"); + CreateBiTree(&T, t2); + printf("创建完毕第一个二叉树\n"); + printf("后序遍历第一个二叉树:\n"); + PostOrderTraverse(T); + printf("\n遍历结束\n\n"); + fflush(stdin); + return 0; +} \ No newline at end of file diff --git a/2017-1/Micylt/hw6/1.png b/2017-1/Micylt/hw6/1.png new file mode 100644 index 00000000..95e13498 Binary files /dev/null and b/2017-1/Micylt/hw6/1.png differ diff --git a/2017-1/Micylt/hw6/2.png b/2017-1/Micylt/hw6/2.png new file mode 100644 index 00000000..c953b0bf Binary files /dev/null and b/2017-1/Micylt/hw6/2.png differ diff --git a/2017-1/Micylt/hw6/3.png b/2017-1/Micylt/hw6/3.png new file mode 100644 index 00000000..4b05e042 Binary files /dev/null and b/2017-1/Micylt/hw6/3.png differ diff --git a/2017-1/Micylt/hw6/4.png b/2017-1/Micylt/hw6/4.png new file mode 100644 index 00000000..4729eca8 Binary files /dev/null and b/2017-1/Micylt/hw6/4.png differ diff --git a/2017-1/Micylt/hw6/hw6.c b/2017-1/Micylt/hw6/hw6.c new file mode 100644 index 00000000..191b7434 --- /dev/null +++ b/2017-1/Micylt/hw6/hw6.c @@ -0,0 +1,272 @@ +//说明:该文件中实现了作业2017-4-27中1和2.2的要求并且使用了上次作业中的两个测试用例外加自己写的两个测试用例进行测试 +#include +#include + +typedef enum +{ + + false, + + true, + +}bool; + +typedef enum +{ + + OK, + + OVERFLOW, + + ERROR, + +} Status; + +typedef struct BiTNode +{ + + char data; + + struct BiTNode *lchild, *rchild; + +}BiTNode, *BiTree; + + +int i = 0; + +int length = 0;//深度 + +int testlength = 0; + +int width[10] = { 0 };//宽度 + + + +Status CreateBiTree(BiTree *T, char *c) +{ + + char ch; + + ch = c[i]; + + i++; + + if (ch == '#') + { + + *T = NULL; + + } + + else + { + + if (!(*T = (BiTNode *)malloc(sizeof(BiTNode)))) + { + + exit(OVERFLOW); + + } + + (*T)->data = ch; + + CreateBiTree(&(*T)->lchild, c); + + CreateBiTree(&(*T)->rchild, c); + + } + + return OK; + +} + + + +Status PostOrderTraverse(BiTree T) +{ + + if (T) + { + + PostOrderTraverse(T->lchild); + + PostOrderTraverse(T->rchild); + + printf("%c", T->data); + + } + + return OK; + +} + + + +Status MeasureLength(BiTree T) +{ + + if (!T) + { + + if (testlength > length) + { + + length = testlength; + + } + + return OK; + + } + + else + { + + testlength++; + + MeasureLength(T->lchild); + + MeasureLength(T->rchild); + + testlength--; + + } + + if (testlength > length) + { + + length = testlength; + + } + + return OK; + +} + + + +Status MeasureWidth(BiTree T, int len) +{ + + if (!T) { + + return ERROR; + + } + + else + { + + width[len] = width[len] + 1; + + len++; + + } + + MeasureWidth(T->lchild, len); + + MeasureWidth(T->rchild, len); + + return OK; + +} + + + +Status GetWidth() +{ + + int wi = 0; + + int i; + + for (i = 1; i <= length; i++) + { + + if (wilchild == NULL&&T->rchild == NULL) + { + + *(count1) = *(count1)+1; + + } + + else + { + + *(count2) = *(count2)+1; + + } + + MeasureLeaf(T->lchild, count1, count2); + + MeasureLeaf(T->rchild, count1, count2); + + } + + return OK; + +} + + + +int main() +{ + + int count1 = 0; + + int count2 = 0; + + char c[30]; + + BiTree T = NULL; + + printf("请输入二叉树的先序序列(#表示空):"); + gets(c); + CreateBiTree(&T, c); + + MeasureLength(T); + + printf("创建完毕第一个二叉树\n二叉树的高度为%d\n", length); + + MeasureWidth(T, 1); + + GetWidth(); + + printf("后序遍历第一个二叉树:\n"); + + PostOrderTraverse(T); + + printf("\n遍历结束\n"); + + MeasureLeaf(T, &count1, &count2); + + printf("该二叉树叶子节点数目为%d,非叶子节点数目为%d\n\n", count1, count2); + + return 0; + +} \ No newline at end of file diff --git a/2017-1/Mrcui/cconte1/main1.cpp b/2017-1/Mrcui/cconte1/main1.cpp new file mode 100644 index 00000000..14f8a9a5 --- /dev/null +++ b/2017-1/Mrcui/cconte1/main1.cpp @@ -0,0 +1,102 @@ +#include +using namespace std; +typedef struct List//鍒涘缓閾捐〃缁撴瀯浣 +{ + int data; + struct List *next; +} List,*Listlink; +Listlink creatList() //鐢熸垚閾捐〃 +{ + Listlink current,previous,head; + int length,i=0; + head=NULL; + previous=NULL; + current=NULL; + cin>>length;//杈撳叆閾捐〃鐨勯暱搴 + for(i=0;inext=current; + } + cin>>current->data; + current->next=NULL; + previous=current; + } + return head; +} +Listlink mergeLinklist(Listlink la, Listlink lb)//閾捐〃鐨勫悎骞 +{ + + Listlink cHead; + cHead=NULL; + Listlink cCurrent; + cCurrent=NULL; + if(la->data < lb->data) + { + cHead=la; + la=la->next; + } + else + { + cHead=lb; + lb=lb->next; + } + cCurrent=cHead; + while((la!=NULL) && (lb!=NULL)) + { + if(la->data < lb->data) + { + cCurrent->next=la; + la=la->next; + } + else + { + cCurrent->next=lb; + lb=lb->next; + } + cCurrent=cCurrent->next; + } + if(la!=NULL) + { + cCurrent->next=la; + } + if(lb!=NULL) + { + cCurrent->next=lb; + } + return cHead; +} + +void printList(const Listlink head)//鎵撳嵃閾捐〃 +{ + const List* phead ; + phead=head; + + while ( phead!= NULL) + { + cout << phead->data<<" "; + phead = phead->next; + } +} +int main() +{ + Listlink La, Lb, Lc; + La = creatList(); + Lb = creatList(); + Lc = mergeLinklist(La, Lb); + printList(Lc); + return 0; +} + + + + + + + diff --git a/2017-1/Mrcui/cconte1/rechanged2.12.cpp b/2017-1/Mrcui/cconte1/rechanged2.12.cpp new file mode 100644 index 00000000..4660e91b --- /dev/null +++ b/2017-1/Mrcui/cconte1/rechanged2.12.cpp @@ -0,0 +1,106 @@ +#include +#include +#include + +typedef struct List//鍒涘缓閾捐〃缁撴瀯浣 +{ + int data; + struct List *next; +} List, *Listlink; + +Listlink creatList(int length, int number) //鐢熸垚閾捐〃 +{ + Listlink current, previous, head; + int i = 0; + head = NULL; + previous = NULL; + current = NULL; + srand(time(NULL)); + for (i = 0; i < length; i++)//閫氳繃寰幆鎺у埗閾捐〃鐨勫垱寤 + { + current = (Listlink) malloc(sizeof(List)); + if (head == 0) { + head = current; + } + if (previous != NULL) { + previous->next = current; + } + number = number + rand() % 4; + current->data = number; + current->next = NULL; + previous = current; + } + return head; +} + +Listlink mergeLinklist(Listlink la, Listlink lb)//閾捐〃鐨勫悎骞 +{ + + Listlink cHead; + cHead = NULL; + Listlink cCurrent; + cCurrent = NULL; + if (la->data < lb->data) { + cHead = la; + la = la->next; + } + else { + cHead = lb; + lb = lb->next; + } + cCurrent = cHead; + while ((la != NULL) && (lb != NULL)) { + if (la->data < lb->data) { + cCurrent->next = la; + la = la->next; + } + else { + cCurrent->next = lb; + lb = lb->next; + } + cCurrent = cCurrent->next; + } + if (la != NULL) { + cCurrent->next = la; + } + if (lb != NULL) { + cCurrent->next = lb; + } + return cHead; +} + +void printList(const Listlink head)//鎵撳嵃閾捐〃 +{ + const List *phead; + phead = head; + + while (phead != NULL) { + printf("%d ", phead->data); + //cout << phead->data<<" "; + phead = phead->next; + } +} + +int main() { + Listlink La, Lb, Lc; + int length; + int number = 1; + srand(time(NULL)); + length = rand() % 20; + number = number + rand() % 13; + La = creatList(length, number); + length = rand() % 16; + number = number + rand() % 10; + Lb = creatList(length, number); + printf("La:"); + printList(La); + printf("\n"); + printf("Lb:"); + printList(Lb); + printf("\n"); + Lc = mergeLinklist(La, Lb); + printf("After the merge:"); + printList(Lc); + printf("\n"); + return 0; +} \ No newline at end of file diff --git a/2017-1/Mrcui/cconte2/datacon3.cpp b/2017-1/Mrcui/cconte2/datacon3.cpp new file mode 100644 index 00000000..8d2d630e --- /dev/null +++ b/2017-1/Mrcui/cconte2/datacon3.cpp @@ -0,0 +1,150 @@ +#include +#include +#include +#include + +#define STACK_INIT_SIZE 100 //瀛樺偍绌洪棿鍒濆鍒嗛厤閲 +#define STACKINCREMENT 10 //瀛樺偍绌洪棿鍒嗛厤澧為噺 +typedef char SElemType; +typedef struct Sq_Stack { + SElemType *base; + SElemType *top; + int stacksize; + +} SqStack; +typedef enum { + OK, + ERROR, + OVERFLOW, + +} Status; + + +Status InitStack(SqStack &stack);//鏋勯犱竴涓┖鏍 +void DestroyStack(SqStack &stack);//閿姣佸凡缁忓瓨鍦ㄧ殑鏍 +Status Push(SqStack &stack, SElemType e);//鎶婁竴涓柊鐨勫厓绱犳坊鍔犲埌鏍堜腑 +SElemType Pop(SqStack &stack, SElemType &e);//鍒犻櫎鏍堥《鐨勫厓绱,骞剁敤e杩斿洖鍏跺 +bool StackEmpty(SqStack &stack);//鍒ゆ柇涓涓爤鏄惁涓虹┖ +Status GetTop(SqStack &stack);//鍥炲埌鏍堥《 +void LineEdit(SqStack *stack);//鍒╃敤瀛楀箙鏍圫,浠庣粓绔帴鍙椾竴琛,骞朵紶閫佽嚦璋冪敤杩囩▼鐨勬暟鎹尯 +void ClearStack(SqStack &stack);//閲嶇疆S涓虹┖鏍 + +Status InitStack(SqStack &stack)//鏋勯犱竴涓┖鏍 + +{ + //stack=(SqStack*)malloc(STACK_INIT_SIZE * sizeof(int)); + stack.base = (SElemType *) malloc(STACK_INIT_SIZE * sizeof(char)); + if (!stack.base) + // exit(OVERFLOW); + { + return OVERFLOW; + //exit(1); + }// 瀛樺偍鍒嗛厤澶辫触 + stack.stacksize = STACK_INIT_SIZE; + stack.top = stack.base; + return OK; +} + +void DestroyStack(SqStack &stack)//閿姣佸凡缁忓瓨鍦ㄧ殑鏍3 +{ + free(stack.base); + stack.top = stack.base; +} + +Status Push(SqStack &stack, SElemType e)//鎶婁竴涓柊鐨勫厓绱犳坊鍔犲埌鏍堜腑 +{ + if (stack.top - stack.base >= stack.stacksize) { //stack->top + stack.base = (SElemType *) realloc(stack.base, (stack.stacksize + STACKINCREMENT) * sizeof(SElemType)); + if (!stack.base) { + return OVERFLOW; + } + stack.top = stack.base + stack.stacksize; + stack.stacksize = stack.stacksize + STACKINCREMENT; + return OK; + + } + *stack.top++ = e; + return OK; +} + +SElemType Pop(SqStack &stack, SElemType &e)//鍒犻櫎鏍堥《鐨勫厓绱,骞剁敤e杩斿洖鍏跺 +{ + if (StackEmpty(stack)) { + return ERROR; + } + stack.top--; + e = *stack.top; + + return e; +} + +bool StackEmpty(SqStack &stack)// 鍒ゆ柇涓涓爤鏄惁涓虹┖鏍 +{ + + if (stack.base == stack.top) + return true; + else + return false; + +} + +Status GetTop(SqStack &stack)//鍥炲埌鏍堥《 +{ + if (StackEmpty(stack)) { + return ERROR; + } + stack.top--; + return OK; +} + +void ClearStack(SqStack &stack)//閲嶇疆S涓虹┖鏍 +{ + stack.top = stack.base; + +} + +void LineEdit()//鍒╃敤瀛楀箙鏍圫,浠庣粓绔帴鍙椾竴琛,骞朵紶閫佽嚦璋冪敤杩囩▼鐨勬暟鎹尯 + +{ + SqStack stack, Dstack; + SElemType e, ch; + InitStack(Dstack); + InitStack(stack);//鏋勯犵┖鏍圫 + ch = getchar();//浠庣粓绔帴鍙楃涓涓瓧绗 + while (ch != EOF)//EOF涓哄叏鏂囩粨鏉熺 + { + while (ch != EOF && ch != '\n') { + switch (ch) { + case '#': + Pop(stack, e);//浠呭綋鏍堥潪绌烘椂閫鏍 + break; + case '@': + ClearStack(stack);//閲嶇疆S涓虹┖鏍 + break; + default: + Push(stack, ch);//鏈夋晥瀛楃杩涙爤,鏈冭檻鏍堟弧鎯呭喌 + break; + + } + ch = getchar();//浠庣粓绔帴鏀朵笅涓涓瓧绗 + } + while (!StackEmpty(stack)) { + e = Pop(stack, e); + Push(Dstack, e); + } + while (!StackEmpty(Dstack)) { + e = Pop(Dstack, e); + printf("%c", e); + } + ClearStack(stack);//閲嶇疆S涓虹┖鏍 + if (ch != EOF) + ch = getchar(); + } + DestroyStack(stack); + +} + +int main() { + LineEdit(); + return 0; +} \ No newline at end of file diff --git a/2017-1/Mrcui/cconte2/datacons.2cpp.cpp b/2017-1/Mrcui/cconte2/datacons.2cpp.cpp new file mode 100644 index 00000000..78debfc6 --- /dev/null +++ b/2017-1/Mrcui/cconte2/datacons.2cpp.cpp @@ -0,0 +1,222 @@ +#include +#include +#include +#include + +#define STACK_INIT_SIZE 100 //瀛樺偍绌洪棿鍒濆鍒嗛厤閲 +#define STACKINCREMENT 10 //瀛樺偍绌洪棿鍒嗛厤澧為噺 +typedef char SElemType; +typedef struct Sq_Stack { + SElemType *base; + SElemType *top; + int stacksize; + +} SqStack; +typedef enum { + OK, + ERROR, + OVERFLOW, + +} Status; + + +Status InitStack(SqStack &stack);//鏋勯犱竴涓┖鏍 +void DestroyStack(SqStack &stack);//閿姣佸凡缁忓瓨鍦ㄧ殑鏍 +Status Push(SqStack &stack, SElemType e);//鎶婁竴涓柊鐨勫厓绱犳坊鍔犲埌鏍堜腑 +SElemType Pop(SqStack &stack, SElemType &e);//鍒犻櫎鏍堥《鐨勫厓绱,骞剁敤e杩斿洖鍏跺 +bool StackEmpty(SqStack &stack);//鍒ゆ柇涓涓爤鏄惁涓虹┖ +char GetTop(SqStack &stack, char e); + +void LineEdit(SqStack *stack);//鍒╃敤瀛楀箙鏍圫,浠庣粓绔帴鍙椾竴琛,骞朵紶閫佽嚦璋冪敤杩囩▼鐨勬暟鎹尯 +void ClearStack(SqStack &stack);//閲嶇疆S涓虹┖鏍 + +Status InitStack(SqStack &stack)//鏋勯犱竴涓┖鏍 + +{ + //stack=(SqStack*)malloc(STACK_INIT_SIZE * sizeof(int)); + stack.base = (SElemType *) malloc(STACK_INIT_SIZE * sizeof(char)); + if (!stack.base) + // exit(OVERFLOW); + { + return OVERFLOW; + //exit(1); + }// 瀛樺偍鍒嗛厤澶辫触 + stack.stacksize = STACK_INIT_SIZE; + stack.top = stack.base; + return OK; +} + +void DestroyStack(SqStack &stack)//閿姣佸凡缁忓瓨鍦ㄧ殑鏍3 +{ + free(stack.base); + stack.top = stack.base; +} + +Status Push(SqStack &stack, SElemType e)//鎶婁竴涓柊鐨勫厓绱犳坊鍔犲埌鏍堜腑 +{ + if (stack.top - stack.base >= stack.stacksize) { //stack->top + stack.base = (SElemType *) realloc(stack.base, (stack.stacksize + STACKINCREMENT) * sizeof(SElemType)); + if (!stack.base) { + return OVERFLOW; + } + stack.top = stack.base + stack.stacksize; + stack.stacksize = stack.stacksize + STACKINCREMENT; + return OK; + + } + *stack.top++ = e; + return OK; +} + +SElemType Pop(SqStack &stack, SElemType &e)//鍒犻櫎鏍堥《鐨勫厓绱,骞剁敤e杩斿洖鍏跺 +{ + if (StackEmpty(stack)) { + return ERROR; + } + stack.top--; + e = *stack.top; + + return e; +} + +bool StackEmpty(SqStack &stack)// 鍒ゆ柇涓涓爤鏄惁涓虹┖鏍 +{ + + if (stack.base == stack.top) + return true; + else + return false; + +} + +char GetTop(SqStack &stack, char e) { + if (StackEmpty(stack)) { + return ERROR; + } + stack.top--; + e = *stack.top; + return e; +} + +void ClearStack(SqStack &stack) { + stack.top = stack.base; + +} + +void Matching(char e[])//鎷彿鍖归厤 +{ + SqStack stack; + + int i = 0; + int status; + status = 1;// 鍒ゆ柇鎷彿鍖归厤鏄惁缁撴潫 + char z; + int length = strlen(e); + InitStack(stack); + while ((i <= length) && (status == 1) && (e[i] != '\0')) { + switch (e[i]) { + case '{': + Push(stack, e[i]); + status = 0; + i++; + break; + case '(': + Push(stack, e[i]); + status = 0; + i++; + break; + case '[': + Push(stack, e[i]); + status = 0; + i++; + break; + case ')': + GetTop(stack, z); + /*printf("top-%x\n",stack.top); + printf("base-%x\n",stack.base); + printf("top-%c\n",*stack.top); + printf("base-%c\n",*stack.base);*/ + + + if (stack.top != stack.base) { + if (z == '(') { + + Pop(stack, z); + status = 1; + i++; + + + } + } + else + status = 0; + break; + case '}': + GetTop(stack, z); + /*printf("top-%x\n",stack.top); + printf("base-%x\n",stack.base); + printf("top-%c\n",*stack.top); + printf("base-%c\n",*stack.base);*/ + + + if (stack.top != stack.base) { + if (z == '{') { + Pop(stack, z); + status = 1; + i++; + + } + } + + else + status = 0; + break; + case ']': + + GetTop(stack, z); + /* printf("top-%x\n",stack.top); + printf("base-%x\n",stack.base); + printf("top-%c\n",*stack.top); + printf("base-%c\n",*stack.base);*/ + + if (stack.base != stack.top) { + if (z == '[') { + + Pop(stack, z); + status = 1; + i++; + + } + } + + else + status = 0; + break; + default: + break; + + } + } + //printf("%d \n", status); + if (status == 1 && StackEmpty(stack)) { + printf("鎷彿鍏ㄩ兘鍖归厤"); + } + else + printf("鎷彿涓嶅畬鍏ㄥ尮閰"); + DestroyStack(stack); + } + + int main() { + char *e; + e = (char *) malloc(sizeof(char) * STACK_INIT_SIZE); + int i = 0; + int length; + gets(e); + /*while(e[i]!='\0') + { + printf("%c ",e[i]); + i++; + }*/ + Matching(e); + return 0; + } \ No newline at end of file diff --git a/2017-1/Mrcui/cconte2/datacons1.cpp b/2017-1/Mrcui/cconte2/datacons1.cpp new file mode 100644 index 00000000..efea4a89 --- /dev/null +++ b/2017-1/Mrcui/cconte2/datacons1.cpp @@ -0,0 +1,123 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 //瀛樺偍绌洪棿鍒濆鍒嗛厤閲 +#define STACKINCREMENT 10 //瀛樺偍绌洪棿鍒嗛厤澧為噺 +typedef int SElemType; +typedef struct Sq_Stack { + SElemType *base; + SElemType *top; + int stacksize; + +} SqStack; +typedef enum { + OK, + ERROR, + OVERFLOW, + +} Status; + + +Status InitStack(SqStack *stack);//鏋勯犱竴涓┖鏍 +void DestroyStack(SqStack *stack);//閿姣佸凡缁忓瓨鍦ㄧ殑鏍 +Status Push(SqStack *stack, SElemType e);//鎶婁竴涓柊鐨勫厓绱犳坊鍔犲埌鏍堜腑 +Status Pop(SqStack *stack, SElemType *e);//鍒犻櫎鏍堥《鐨勫厓绱,骞剁敤e杩斿洖鍏跺 +bool StackEmpty(SqStack *stack);//鍒ゆ柇涓涓爤鏄惁涓虹┖ +Status GetTop(SqStack stack);//杩斿洖鏍堥《 +void Conversion(SqStack *stack, SElemType input, SElemType mod);//灏嗗崄杩涘埗鏁拌浆鍖栦负鍏繘鍒 + + +Status InitStack(SqStack *stack)//鏋勯犱竴涓┖鏍 +{ + //stack=(SqStack*)malloc(STACK_INIT_SIZE * sizeof(int)); + stack->base = (SElemType *) malloc(STACK_INIT_SIZE * sizeof(int)); + if (!stack->base) + // exit(OVERFLOW); + { + return OVERFLOW; + }// 瀛樺偍鍒嗛厤澶辫触 + stack->stacksize = STACK_INIT_SIZE; + stack->top = stack->base; + return OK; +} + +void DestroyStack(SqStack *stack)//閿姣佸凡缁忓瓨鍦ㄧ殑鏍3 +{ + if (stack) { + stack = NULL; + } +} + +Status Push(SqStack *stack, SElemType e)//鎶婁竴涓柊鐨勫厓绱犳坊鍔犲埌鏍堜腑 +{ + if (stack->top - stack->base > stack->stacksize) { //stack->top + stack->base = (SElemType *) realloc(stack->base, (stack->stacksize + STACKINCREMENT) * sizeof(SElemType *)); + if (!stack->base) { + return OVERFLOW; + } + stack->top = stack->base + stack->stacksize; + stack->stacksize = stack->stacksize + STACKINCREMENT; + + } + *stack->top = e; + stack->top++; + return OK; +} + +Status Pop(SqStack *stack, SElemType *e)//鍒犻櫎鏍堥《鐨勫厓绱,骞剁敤e杩斿洖鍏跺 +{ + if (StackEmpty(stack)) { + return ERROR; + } + stack->top--; + *e = *stack->top; + + return OK; +} + +bool StackEmpty(SqStack *stack)// 鍒ゆ柇涓涓爤鏄惁涓虹┖鏍 +{ + if (stack) { + if (stack->base == stack->top) + return true; + else + return false; + } +} + +Status GetTop(SqStack *stack) {// 杩斿洖鏍堥《鍏冪礌 + if (StackEmpty(stack)) { + return ERROR; + } + stack->top--; + return OK; +} + +void Conversion(SqStack *stack, SElemType input, SElemType mod) {//灏嗗崄杩涘埗鏁拌浆鍖栦负鍏繘鍒 + SElemType e; + while (input) { + Push(stack, input % mod); + input = input / mod; + } + while (!StackEmpty(stack)) { + Pop(stack, &e); + printf("%d", e); + } + printf("\n"); + +} + +int main() { + int integer; + //SqStack * Stack; + SqStack *Stack; + srand(time(NULL)); + int Mod = 8; + integer = rand() / 3; + printf("The integer is %d\n", integer); + printf("After the transformation:"); + InitStack(Stack); + Conversion(Stack, integer, Mod); + return 0; +} \ No newline at end of file diff --git "a/2017-1/Mrcui/cconte3/\345\215\225\345\220\221\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234\345\256\236\347\216\260.cpp" "b/2017-1/Mrcui/cconte3/\345\215\225\345\220\221\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234\345\256\236\347\216\260.cpp" new file mode 100644 index 00000000..c0485343 --- /dev/null +++ "b/2017-1/Mrcui/cconte3/\345\215\225\345\220\221\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234\345\256\236\347\216\260.cpp" @@ -0,0 +1,143 @@ +#include +#include +#include + + +typedef int datatype; +typedef enum { + OK, + OVERFLOW, + ERROR +} Status; +typedef struct Queuenode { + datatype data; + struct Queuenode *next; +} Qnode; + +typedef struct link_queue { + Qnode *front; + Qnode *rear; +} Linkqueue; + +Status InitQueue(Linkqueue *q);//鍒濆鍖栭槦鍒 +Status DestoryQueue(Linkqueue *q);//閿姣佸凡缁忓瓨鍦ㄧ殑闃熷垪 +bool QueueEmpty(Linkqueue *q);//鍒ゆ柇闃熷垪涓虹┖锛屽洜涓轰笉鏄敤鏁扮粍鏉ュ瓨鍌ㄩ槦鍒楋紝鑰屾槸鐢ㄥ爢鏉ョ鐞嗛槦銆 +datatype Queuelength(Linkqueue q);//鑾峰彇闃熷垪闀垮害 +Status EnQueue(Linkqueue *q, datatype x);//鍏ラ槦 +datatype DeQueue(Linkqueue *q);//鍑洪槦 +datatype QueueFront(Linkqueue *q);//鍙栧ご鎸囬拡 + +Status InitQueue(Linkqueue *q)//鍒濆鍖栭槦鍒 +{ + q->front = q->rear = (Qnode *) malloc(sizeof(Qnode)); + if (!q->front) { + return OVERFLOW; + } + q->front = q->rear = NULL; + return OK; +} + +Status DestoryQueue(Linkqueue *q)//閿姣佸凡缁忓瓨鍦ㄧ殑闃熷垪 +{ + while (q->front) { + q->rear = q->front->next; + free(q->front); + q->front = q->rear; + } + return OK; +} + +bool QueueEmpty(Linkqueue *q)//鍒ゆ柇闃熷垪涓虹┖锛屽洜涓轰笉鏄敤鏁扮粍鏉ュ瓨鍌ㄩ槦鍒楋紝鑰屾槸鐢ㄥ爢鏉ョ鐞嗛槦銆 +{ + if ((q->front == NULL) && (q->rear == NULL)) + return true; + return false; +} + +Status ClearQueue(Linkqueue *q)//娓呯┖涓涓槦鍒 +{ + DestoryQueue(q); + InitQueue(q); + return OK; +} + +datatype Queuelength(Linkqueue q)//鑾峰彇闃熷垪闀垮害 +{ + int length = 0; + Queuenode *pqueue; + pqueue = q.front; + while (q.rear != pqueue) { + length++; + pqueue = pqueue->next; + } + return length; +} + +Status EnQueue(Linkqueue *q, datatype x)//鍏ラ槦 +{ + Qnode *p; + p = (Qnode *) malloc(sizeof(Qnode)); + + + if (!p) { + exit(OVERFLOW); + } + + p->data = x; + p->next = NULL; + + //闃熷垪涓虹┖鐨勬儏鍐 + if (QueueEmpty(q)) { + q->front = q->rear = p; + } + else { + q->rear->next = p; + q->rear = p; + } + return OK; +} + +datatype DeQueue(Linkqueue *q)//鍑洪槦 +{ + datatype x; + Qnode *p; + + + + p = q->front; + x = p->data; + q->front = p->next; + + if (q->rear == p) { + q->rear = NULL; + } + + free(p); + return x; +} + +datatype QueueFront(Linkqueue *q)//鍙栧ご鎸囬拡 +{ + if (QueueEmpty(q)) { + printf("queue is empty"); + } + return (q->front->data); +} + +int main() { + datatype i, k = 0, length, e, c; + Linkqueue q; + srand(time(NULL)); + length = rand() % 10 + 3; + for (i = 0; i < length; i++) { + k = k + 2; + e = rand() % k; + EnQueue(&q, e); + } + for (i = 0; i < length; i++) { + c = DeQueue(&q); + printf("%d ", c); + } + ClearQueue(&q); + return 0; +} \ No newline at end of file diff --git "a/2017-1/Mrcui/cconte3/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" "b/2017-1/Mrcui/cconte3/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" new file mode 100644 index 00000000..07ae4ada --- /dev/null +++ "b/2017-1/Mrcui/cconte3/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.cpp" @@ -0,0 +1,209 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 //瀛樺偍绌洪棿鍒濆鍒嗛厤閲 +#define STACKINCREMENT 10 //瀛樺偍绌洪棿鍒嗛厤澧為噺 +typedef char SElemType; + +typedef struct Sq_Stack { + SElemType *base; + SElemType *top; + int stacksize; + +} SqStack;//瀛樺偍杩愮畻鏁板拰杩愮畻绗︾殑缁撴瀯浣 +typedef enum { + OK, + ERROR, + OVERFLOW, + +} Status; +unsigned char opchartab[7][7]= + { + '>', '>', '<', '<', '<', '>', '>', + '>', '>', '<', '<', '<', '>', '>', + '>', '>', '>', '>', '<', '>', '>', + '>', '>', '>', '>', '<', '>', '>', + '<', '<', '<', '<', '<', '=', ' ', + '>', '>', '>', '>', ' ', '>', '>', + '<', '<', '<', '<', '<', ' ', '=' + + }; +char OPERATORS[]={'+','-','*','/','(',')','$'};//杩愮畻绗︽暟缁 + +Status InitStack(SqStack &stack);//鏋勯犱竴涓繍绠楁暟绌烘爤 +void DestroyStack(SqStack &stack);//閿姣佸凡缁忓瓨鍦ㄧ殑杩愮畻鏁版爤 +Status Push(SqStack &stack, SElemType e);//鎶婁竴涓柊鐨勫厓绱犳坊鍔犲埌杩愮畻鏁版爤涓 +Status Pop(SqStack &stack, SElemType &e);//鍒犻櫎杩愮畻鏁版爤椤剁殑鍏冪礌,骞剁敤e杩斿洖鍏跺 +bool StackEmpty(SqStack &stack);//鍒ゆ柇涓涓繍绠楁暟鏍堟槸鍚︿负绌 +SElemType GetTop(SqStack s); + +bool IsOperator(SElemType e);//鍒ゆ柇璇诲叆瀛楃鏄惁涓鸿繍绠楃 +SElemType Precede(SElemType a,SElemType b);//姣旇緝杩愮畻绗︾殑浼樺厛绾 +SElemType Operate(SElemType a,SElemType Operator,SElemType b);//杩愮畻 +int EvaluateExpression(); + +Status InitStack(SqStack &stack)//鏋勯犱竴涓繍绠楃绌烘爤 +{ + //stack=(SqStack*)malloc(STACK_INIT_SIZE * sizeof(int)); + stack.base = (SElemType *) malloc(STACK_INIT_SIZE * sizeof(int)); + if (!stack.base) + // exit(OVERFLOW); + { + return OVERFLOW; + }// 瀛樺偍鍒嗛厤澶辫触 + stack.stacksize = STACK_INIT_SIZE; + stack.top = stack.base; + return OK; +} +void DestroyStack(SqStack &stack)//閿姣佸凡缁忓瓨鍦ㄧ殑鏍 +{ + + /*stack = NULL;*/ + +} +Status Push(SqStack &stack, SElemType e)//鎶婁竴涓柊鐨勫厓绱犳坊鍔犲埌鏍堜腑 +{ + if (stack.top - stack.base > stack.stacksize) { //stack->top + stack.base = (SElemType *) realloc(stack.base, (stack.stacksize + STACKINCREMENT) * sizeof(SElemType *)); + if (!stack.base) { + return OVERFLOW; + } + stack.top = stack.base + stack.stacksize; + stack.stacksize = stack.stacksize + STACKINCREMENT; + + } + *stack.top = e; + stack.top++; + return OK; +} +Status Pop(SqStack &stack, SElemType &e)//鍒犻櫎杩愮畻鏁版爤椤剁殑鍏冪礌,骞剁敤e杩斿洖鍏跺 +{ + if (StackEmpty(stack)) { + return ERROR; + } + stack.top--; + e = *stack.top; + + return OK; +} +bool StackEmpty(SqStack &stack)// 鍒ゆ柇涓涓繍绠楁暟鏍堟槸鍚︿负绌烘爤 +{ + + if (stack.base == stack.top) + return true; + else + return false; + +} +SElemType GetTop(SqStack stack) {// 杩斿洖杩愮畻鏁版爤椤跺厓绱 + if (!StackEmpty(stack)) { + stack.top--; + } + + return *stack.top; +} +bool IsOperator(SElemType e)//鍒ゆ柇璇诲叆瀛楃鏄惁涓鸿繍绠楃 +{ + if(e=='+'||e=='-'||e=='*'||e=='/'||e=='('||e==')'||e=='#') + return true; + else + return false; +} +SElemType Precede(SElemType a,SElemType b)//姣旇緝杩愮畻绗︾殑浼樺厛绾 +{ + SElemType ch; + if(a=='+'||a=='-') + { + if(b=='+'||b=='-'||b==')'||b=='#') + ch='>'; + else if(b=='*'||b=='/'||b=='(') + ch='<'; + } + else if(a=='*'||a=='/') + { + if(b=='+'||b=='-'||b=='*'||b=='/'||b==')'||b=='#') + ch='>'; + else if(b=='(') + ch='<'; + } + else if(a=='(') + { + if(b=='+'||b=='-'||b=='*'||b=='/'||b=='(') + ch='<'; + else if(b==')') + ch='='; + } + else if(a==')') + { + if(b=='+'||b=='-'||b=='*'||b=='/'||b==')'||b=='#') + ch='>'; + } + else if(a=='#') + { + if(b=='+'||b=='-'||b=='*'||b=='/'||b=='(') + ch='<'; + else if(b=='#') + ch='='; + } + return ch; +} +SElemType Operate(SElemType a,SElemType Operator,SElemType b)//杩愮畻 +{ + SElemType ch; + a=a-'0';// 杞寲鎴恑nt绫 + b=b-'0';//杞寲鎴恑nt绫 + if(Operator=='+') + ch=a+b+'0';//鍦ㄨ绠楃殑鍚屾椂杞寲鎴恈har绫 + else if(Operator=='-') + ch=a-b+'0'; + else if(Operator=='*') + ch=a*b+'0'; + else if(Operator=='/') + ch=a/b+'0'; + return ch; +} + int EvaluateExpression() +{ + SqStack Operator,Oprand; + SElemType ch,a,b,theta,x; + InitStack(Operator);//鍒濆鍖栨搷浣滅鏍 + InitStack(Oprand);//鍒濆鍖栨搷浣滄暟鏍 + Push(Operator,'#'); + ch=getchar(); + while(ch!='#'||GetTop(Operator)!='#') + { + if(!IsOperator(ch)) + { + Push(Oprand,ch); + ch=getchar(); + } + else + { + switch(Precede(GetTop(Operator),ch)) + { + case '<': + Push(Operator,ch); + ch=getchar(); + break; + case '>': + Pop(Operator,theta); + Pop(Oprand,b); + Pop(Oprand,a); + Push(Oprand,Operate(a,theta,b)); + break; + case '=': + Pop(Operator,x); + ch=getchar(); + break; + } + } + } + return GetTop(Oprand)-'0'; +} +int main() +{ + printf("Please input (end of '#'):\n"); + printf("The result is %d\n",EvaluateExpression()); + return 0; +} diff --git "a/2017-1/Mrcui/cconte4/Chapter2.13th\345\256\236\347\216\260.cpp" "b/2017-1/Mrcui/cconte4/Chapter2.13th\345\256\236\347\216\260.cpp" new file mode 100644 index 00000000..bbef098d --- /dev/null +++ "b/2017-1/Mrcui/cconte4/Chapter2.13th\345\256\236\347\216\260.cpp" @@ -0,0 +1,130 @@ +#include +#include +#include + +typedef struct Link_list { + int data; + struct Link_list *next; +} Linklist, *LNode; + +LNode CreatLinklist(int length, int number);//鍒涘缓涓涓嚎鎬ч摼琛 +void PrintLinklist(const LNode list);//鎵撳嵃绾挎ч摼琛 +void PrintLoopLinklist(const LNode list);//鎵撳嵃寰幆鍗曢摼琛 +void disingrateLinklist(const LNode list, LNode la, LNode lb);//灏嗕竴涓嚎鎬ц〃鍒嗚В涓轰袱涓惊鐜崟閾捐〃 + +LNode CreatLinklist(int length, int number)//鍒涘缓涓涓嚎鎬ч摼琛 +{ + LNode current, previous, head, list; + int i = 0; + head = NULL; + list = NULL; + list = head; + previous = NULL; + current = NULL; + srand(time(NULL)); + for (i = 0; i < length; i++) { + current = (LNode) malloc(sizeof(Link_list)); + if (head == 0) { + head = current; + } + if (previous != NULL) { + previous->next = current; + } + number = number + rand() % 4; + current->data = number; + current->next = NULL; + previous = current; + } + return head; +} + +void PrintLinklist(const LNode list)//鎵撳嵃绾挎ч摼琛 +{ + LNode p; + p = list; + while (p != NULL) { + printf("%d ", p->data); + p = p->next; + } +} + +void PrintLoopLinklist(const LNode list) //鎵撳嵃寰幆鍗曢摼琛 +{ + LNode start; + start = list->next; + while (start != list) { + printf("%d ", start->data); + start = start->next; + } +} + +void disingrateLinklist(const LNode list, LNode la, LNode lb)//灏嗕竴涓嚎鎬ц〃鍒嗚В涓轰袱涓惊鐜崟閾捐〃 +{ + LNode pla, acurrent, ahead, plb, bcurrent, bhead; + int count = 1; + int alen = 0, blen = 0; + LNode p; + p = list; + acurrent = la; + ahead = la; + bcurrent = lb; + bhead = lb; + while (p != NULL) { + if (count % 2 != 0) { + pla = (LNode) malloc(sizeof(Linklist)); + pla->data = p->data; + + pla->next = acurrent; + ahead->next = pla; + acurrent = pla; + alen++; + + + } + else { + plb = (LNode) malloc(sizeof(Link_list)); + plb->data = p->data; + + plb->next = bcurrent; + bhead->next = plb; + bcurrent = plb; + blen++; + + } + p = p->next; + count++; + } + ahead->data = alen; + bhead->data = blen; + printf("\nLa is:\n"); + PrintLoopLinklist(la); + printf("\nThe length of la is:%d\n", ahead->data); + + printf("\nLb is:\n"); + PrintLoopLinklist(lb); + printf("\nThe length of lb is:%d\n", bhead->data); + +} + +int main() { + LNode List, La, Lb; + La = (LNode) malloc(sizeof(Link_list)); + La->next = La; + Lb = (LNode) malloc(sizeof(Link_list)); + Lb->next = Lb; + + int number; + srand(time(NULL)); + number = number % 5 + 3; + int length = length % 5 + 14; + List = CreatLinklist(length, number); + printf("The original list is:\n"); + PrintLinklist(List); + printf("\nThe length of original list is :%d", length); + printf("\nAfter ...\n"); + + disingrateLinklist(List, La, Lb); + return 0; + + +} diff --git a/2017-1/Mrcui/cconte5/main.cpp b/2017-1/Mrcui/cconte5/main.cpp new file mode 100644 index 00000000..23c4bd2b --- /dev/null +++ b/2017-1/Mrcui/cconte5/main.cpp @@ -0,0 +1,103 @@ +#include +#include + +typedef struct BiTNode { + + char data; + struct BiTNode *lchild;//宸﹀瀛愭寚閽 + struct BiTNode *rchild; //鍙冲瀛愭寚閽 +} BiTNode, *BiTree; + +int CreateBiTree(BiTree &Tree);//鎸夊厛搴忓簭鍒楀垱寤轰簩鍙夋爲 +int MCreateBiTree(BiTree &Tree);//鎸変腑搴忓簭鍒楀垱寤轰簩鍙夋爲 +void VisitBiTree(BiTree Tree);//杈撳嚭鏍硅妭鐐 +void PreOrderTraverse(BiTree Tree);//鍏堝簭閬嶅巻 +void InOrderTraverse(BiTree Tree);//涓簭閬嶅巻 +void PostOrderTraverse(BiTree Tree);//鍚庡簭閬嶅巻 + +int CreateBiTree(BiTree &Tree) {//鎸夊厛搴忓簭鍒楀垱寤轰簩鍙夋爲 + char data; + data = getchar(); + if (data == ' ') { //鎸夊厛搴忔搴忚緭鍏ヤ簩鍙夋爲涓粨鐐圭殑鍊硷紙涓涓瓧绗︼級锛屸 鈥欒〃绀虹┖鏍 + Tree = NULL; + } + else { + Tree = (BiTree) malloc(sizeof(BiTNode)); + Tree->data = data; //鐢熸垚鏍圭粨鐐 + CreateBiTree(Tree->lchild); //鐢熸垚宸﹀瓙鏍 + CreateBiTree(Tree->rchild); //鐢熸垚鍙冲瓙鏍 + } + return 0; +} + +int MCreateBiTree(BiTree &Tree) {//鎸変腑搴忓簭鍒楀垱寤轰簩鍙夋爲 + char data; + data = getchar(); + if (data == ' ') { //鎸夊厛搴忔搴忚緭鍏ヤ簩鍙夋爲涓粨鐐圭殑鍊硷紙涓涓瓧绗︼級锛屸 鈥欒〃绀虹┖鏍 + Tree = NULL; + } + else { + Tree = (BiTree) malloc(sizeof(BiTNode)); + CreateBiTree(Tree->lchild); //鐢熸垚宸﹀瓙鏍 + Tree->data = data; //鐢熸垚鏍圭粨鐐 + CreateBiTree(Tree->rchild); //鐢熸垚鍙冲瓙鏍 + } + return 0; +} + +void VisitBiTree(BiTree Tree) {//杈撳嚭鏍硅妭鐐 + if (Tree->data != ' ') { + printf("%c ", Tree->data); + } +} + +void PreOrderTraverse(BiTree Tree) {//鍏堝簭閬嶅巻 + if (Tree != NULL) { + + VisitBiTree(Tree); //璁块棶鏍硅妭鐐 + PreOrderTraverse(Tree->lchild); //璁块棶宸﹀瓙鏍戠粨鐐 + PreOrderTraverse(Tree->rchild);//璁块棶鍙冲瓙鏍戠粨鐐 + } +} + +void InOrderTraverse(BiTree Tree) {//涓簭閬嶅巻 + if (Tree != NULL) { + + InOrderTraverse(Tree->lchild);//璁块棶宸﹀瓙鏍戠粨鐐 + + VisitBiTree(Tree);//璁块棶鏍硅妭鐐 + + InOrderTraverse(Tree->rchild);//璁块棶鍙冲瓙鏍戠粨鐐 + } +} + +void PostOrderTraverse(BiTree Tree) {//鍚庡簭閬嶅巻 + if (Tree != NULL) { + + PostOrderTraverse(Tree->lchild); //璁块棶宸﹀瓙鏍戠粨鐐 + + PostOrderTraverse(Tree->rchild); //璁块棶鍙冲瓙鏍戠粨鐐 + + VisitBiTree(Tree); //璁块棶鏍硅妭鐐 + } +} + + +int main() { + BiTree Tree; + CreateBiTree(Tree); + printf("鍏堝簭閬嶅巻浜屽弶鏍戯細\n"); + PreOrderTraverse(Tree); + printf("\n"); + printf("涓簭閬嶅巻浜屽弶鏍戯細\n"); + InOrderTraverse(Tree); + printf("\n"); + printf("鍚庡簭閬嶅巻浜屽弶鏍戯細\n"); + PostOrderTraverse(Tree); + printf("\n"); + return 0; +} + +/*娴嬭瘯鐢ㄤ緥*/ +/*ABDG EH I K C F */ +/*ABD F CE */ \ No newline at end of file diff --git "a/2017-1/Mrcui/cconte5/\345\261\217\345\271\225\345\277\253\347\205\247 2017-05-03 \344\270\213\345\215\2105.53.05.png" "b/2017-1/Mrcui/cconte5/\345\261\217\345\271\225\345\277\253\347\205\247 2017-05-03 \344\270\213\345\215\2105.53.05.png" new file mode 100644 index 00000000..adc5bcce Binary files /dev/null and "b/2017-1/Mrcui/cconte5/\345\261\217\345\271\225\345\277\253\347\205\247 2017-05-03 \344\270\213\345\215\2105.53.05.png" differ diff --git "a/2017-1/Mrcui/cconte5/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271\344\270\252\346\225\260\344\270\216\351\235\236\345\217\266\345\255\220\350\212\202\347\202\271\344\270\252\346\225\260.cpp" "b/2017-1/Mrcui/cconte5/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271\344\270\252\346\225\260\344\270\216\351\235\236\345\217\266\345\255\220\350\212\202\347\202\271\344\270\252\346\225\260.cpp" new file mode 100644 index 00000000..9edaade4 --- /dev/null +++ "b/2017-1/Mrcui/cconte5/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\350\212\202\347\202\271\344\270\252\346\225\260\344\270\216\351\235\236\345\217\266\345\255\220\350\212\202\347\202\271\344\270\252\346\225\260.cpp" @@ -0,0 +1,201 @@ +#include +#include + +typedef struct BiTNode { + + char data; + struct BiTNode *lchild;//宸﹀瀛愭寚閽 + struct BiTNode *rchild; //鍙冲瀛愭寚閽 +} BiTNode, *BiTree; + +int CreateBiTree(BiTree &Tree);//鎸夊厛搴忓簭鍒楀垱寤轰簩鍙夋爲 +int MCreateBiTree(BiTree &Tree);//鎸変腑搴忓簭鍒楀垱寤轰簩鍙夋爲 +void VisitBiTree(BiTree Tree);//杈撳嚭鏍硅妭鐐 +void PreOrderTraverse(BiTree Tree);//鍏堝簭閬嶅巻 +void InOrderTraverse(BiTree Tree);//涓簭閬嶅巻 +void PostOrderTraverse(BiTree Tree);//鍚庡簭閬嶅巻 +int Depth(BiTree Tree);//璁$畻浜屽弶鏍戠殑楂樺害 +int Width(BiTree Tree);//璁$畻浜屽弶鏍戠殑瀹藉害 +int CountTreeLeaf(BiTree Tree);//璁$畻浜屽弶鏍戜腑鍙跺瓙鑺傜偣鐨勪釜鏁 +int CountunTreeLeaf(BiTree Tree); //璁$畻浜屽弶鏍戜腑闈炲彾瀛愯妭鐐圭殑涓暟 + +int CreateBiTree(BiTree &Tree) {//鎸夊厛搴忓簭鍒楀垱寤轰簩鍙夋爲 + char data; + data = getchar(); + if (data == ' ') { //鎸夊厛搴忔搴忚緭鍏ヤ簩鍙夋爲涓粨鐐圭殑鍊硷紙涓涓瓧绗︼級锛屸 鈥欒〃绀虹┖鏍 + Tree = NULL; + } + else { + Tree = (BiTree) malloc(sizeof(BiTNode)); + Tree->data = data; //鐢熸垚鏍圭粨鐐 + CreateBiTree(Tree->lchild); //鐢熸垚宸﹀瓙鏍 + CreateBiTree(Tree->rchild); //鐢熸垚鍙冲瓙鏍 + } + return 0; +} + +int MCreateBiTree(BiTree &Tree) {//鎸変腑搴忓簭鍒楀垱寤轰簩鍙夋爲 + char data; + data = getchar(); + if (data == ' ') { //鎸夊厛搴忔搴忚緭鍏ヤ簩鍙夋爲涓粨鐐圭殑鍊硷紙涓涓瓧绗︼級锛屸 鈥欒〃绀虹┖鏍 + Tree = NULL; + } + else { + Tree = (BiTree) malloc(sizeof(BiTNode)); + CreateBiTree(Tree->lchild); //鐢熸垚宸﹀瓙鏍 + Tree->data = data; //鐢熸垚鏍圭粨鐐 + CreateBiTree(Tree->rchild); //鐢熸垚鍙冲瓙鏍 + } + return 0; +} + +void VisitBiTree(BiTree Tree) {//杈撳嚭鏍硅妭鐐 + if (Tree->data != ' ') { + printf("%c ", Tree->data); + } +} + +void PreOrderTraverse(BiTree Tree) {//鍏堝簭閬嶅巻 + if (Tree != NULL) { + + VisitBiTree(Tree); //璁块棶鏍硅妭鐐 + PreOrderTraverse(Tree->lchild); //璁块棶宸﹀瓙鏍戠粨鐐 + PreOrderTraverse(Tree->rchild);//璁块棶鍙冲瓙鏍戠粨鐐 + } +} + +void InOrderTraverse(BiTree Tree) {//涓簭閬嶅巻 + if (Tree != NULL) { + + InOrderTraverse(Tree->lchild);//璁块棶宸﹀瓙鏍戠粨鐐 + + VisitBiTree(Tree);//璁块棶鏍硅妭鐐 + + InOrderTraverse(Tree->rchild);//璁块棶鍙冲瓙鏍戠粨鐐 + } +} + +void PostOrderTraverse(BiTree Tree) {//鍚庡簭閬嶅巻 + if (Tree != NULL) { + + PostOrderTraverse(Tree->lchild); //璁块棶宸﹀瓙鏍戠粨鐐 + + PostOrderTraverse(Tree->rchild); //璁块棶鍙冲瓙鏍戠粨鐐 + + VisitBiTree(Tree); //璁块棶鏍硅妭鐐 + } +} + + +int Depth(BiTree Tree) {//璁$畻浜屽弶鏍戦珮搴 + int depth1, depth2; + if (Tree == NULL) + return 0; + else { + depth1 = Depth(Tree->lchild); + depth2 = Depth(Tree->rchild); + if (depth1 > depth2) + return (depth1 + 1); + else + return (depth2 + 1); + } +} + + +int Width(BiTree Tree) {//璁$畻浜屽弶鏍戝搴 + BiTree queue[1000]; + int front = -1, rear = -1; + + int flag = 0, count = 0,pright;// pright鐢ㄤ簬鎸囧悜鏍戜腑灞傜殑鏈鍙宠竟鐨勭粨鐐癸紝鏍囧織flag璁板綍灞備腑缁撶偣鏁扮殑鏈澶у笺 + if (Tree != NULL) { + rear++; + queue[rear] = Tree; + flag = 1; + pright = rear; + } + while (front < pright) { + front++; + Tree = queue[front]; + if (Tree->lchild != NULL) { + rear++; + queue[rear] = Tree->lchild; + count++; + } + if (Tree->rchild != NULL) { + rear++; + queue[rear] = Tree->rchild; + count++; + } + if (front == pright)// 褰撳墠灞傚凡閬嶅巻瀹屾瘯 + + { + if (flag < count) + flag = count; + count = 0; + pright = rear; + } + } + + return flag; +} + +int CountTreeLeaf(BiTree Tree) {//璁$畻浜屽弶鏍戜腑鍙跺瓙鑺傜偣鐨勪釜鏁 + + if (Tree == NULL) //濡傛灉鑺傜偣涓虹┖锛屽垯杩斿洖0 + + return 0; + + else if ((Tree->lchild == NULL) && (Tree->rchild == NULL))//鍚﹀垯濡傛灉鑺傜偣宸﹀彸瀛╁瓙鏈変竴涓负绌猴紝鍙︿竴涓瓨鍦紝鍒欒繑鍥1 + + return 1; + + else + + return (CountTreeLeaf(Tree->lchild) + CountTreeLeaf(Tree->rchild));//鍚﹀垯杩斿洖宸﹀彸瀛愭爲鍙跺瓙鑺傜偣涔嬪拰 + +} +int CountunTreeLeaf(BiTree Tree) {//璁$畻浜屽弶鏍戜腑闈炲彾瀛愯妭鐐圭殑涓暟 + + if (Tree == NULL) //濡傛灉鑺傜偣涓虹┖锛屽垯杩斿洖0 + + return 0; + + else if ((Tree->lchild == NULL) &&(Tree->rchild == NULL))//鍚﹀垯濡傛灉鑺傜偣宸﹀彸瀛╁瓙閮戒负绌猴紝鍙︿竴涓瓨鍦紝鍒欒繑鍥0 + + return 0; + + + else + return (1+CountunTreeLeaf(Tree->lchild) + CountunTreeLeaf(Tree->rchild)); + + +} +int main() { + BiTree Tree; + int depth,width,leaf,unleaf; + CreateBiTree(Tree); + printf("鍏堝簭閬嶅巻浜屽弶鏍戯細\n"); + PreOrderTraverse(Tree); + printf("\n"); + printf("涓簭閬嶅巻浜屽弶鏍戯細\n"); + InOrderTraverse(Tree); + printf("\n"); + printf("鍚庡簭閬嶅巻浜屽弶鏍戯細\n"); + PostOrderTraverse(Tree); + printf("\n"); + depth=Depth( Tree); + printf("浜屽弶鏍戠殑楂樺害鏄:%d",depth); + width=Width(Tree); + printf("\n浜屽弶鏍戠殑瀹藉害鏄:%d",width); + leaf= CountTreeLeaf(Tree); + printf("\n浜屽弶鏍戠殑鍙跺瓙鑺傜偣涓暟鏄:%d",leaf); + unleaf=CountunTreeLeaf(Tree); + printf("\n浜屽弶鏍戠殑闈炲彾瀛愯妭鐐逛釜鏁版槸:%d",unleaf); + + return 0; +} + + +/*娴嬭瘯鐢ㄤ緥*/ +/*ABDG EH I K C F */ +/*ABD F CE */ diff --git "a/2017-1/Mrcui/cconte5/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\345\222\214\345\256\275\345\272\246.cpp" "b/2017-1/Mrcui/cconte5/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\345\222\214\345\256\275\345\272\246.cpp" new file mode 100644 index 00000000..9415b52f --- /dev/null +++ "b/2017-1/Mrcui/cconte5/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\345\222\214\345\256\275\345\272\246.cpp" @@ -0,0 +1,164 @@ +#include +#include + +typedef struct BiTNode { + + char data; + struct BiTNode *lchild;//宸﹀瀛愭寚閽 + struct BiTNode *rchild; //鍙冲瀛愭寚閽 +} BiTNode, *BiTree; + +int CreateBiTree(BiTree &Tree);//鎸夊厛搴忓簭鍒楀垱寤轰簩鍙夋爲 +int MCreateBiTree(BiTree &Tree);//鎸変腑搴忓簭鍒楀垱寤轰簩鍙夋爲 +void VisitBiTree(BiTree Tree);//杈撳嚭鏍硅妭鐐 +void PreOrderTraverse(BiTree Tree);//鍏堝簭閬嶅巻 +void InOrderTraverse(BiTree Tree);//涓簭閬嶅巻 +void PostOrderTraverse(BiTree Tree);//鍚庡簭閬嶅巻 +int Depth(BiTree Tree);//璁$畻浜屽弶鏍戠殑楂樺害 +int Width(BiTree Tree);//璁$畻浜屽弶鏍戠殑瀹藉害 + +int CreateBiTree(BiTree &Tree) {//鎸夊厛搴忓簭鍒楀垱寤轰簩鍙夋爲 + char data; + data = getchar(); + if (data == ' ') { //鎸夊厛搴忔搴忚緭鍏ヤ簩鍙夋爲涓粨鐐圭殑鍊硷紙涓涓瓧绗︼級锛屸 鈥欒〃绀虹┖鏍 + Tree = NULL; + } + else { + Tree = (BiTree) malloc(sizeof(BiTNode)); + Tree->data = data; //鐢熸垚鏍圭粨鐐 + CreateBiTree(Tree->lchild); //鐢熸垚宸﹀瓙鏍 + CreateBiTree(Tree->rchild); //鐢熸垚鍙冲瓙鏍 + } + return 0; +} + +int MCreateBiTree(BiTree &Tree) {//鎸変腑搴忓簭鍒楀垱寤轰簩鍙夋爲 + char data; + data = getchar(); + if (data == ' ') { //鎸夊厛搴忔搴忚緭鍏ヤ簩鍙夋爲涓粨鐐圭殑鍊硷紙涓涓瓧绗︼級锛屸 鈥欒〃绀虹┖鏍 + Tree = NULL; + } + else { + Tree = (BiTree) malloc(sizeof(BiTNode)); + CreateBiTree(Tree->lchild); //鐢熸垚宸﹀瓙鏍 + Tree->data = data; //鐢熸垚鏍圭粨鐐 + CreateBiTree(Tree->rchild); //鐢熸垚鍙冲瓙鏍 + } + return 0; +} + +void VisitBiTree(BiTree Tree) {//杈撳嚭鏍硅妭鐐 + if (Tree->data != ' ') { + printf("%c ", Tree->data); + } +} + +void PreOrderTraverse(BiTree Tree) {//鍏堝簭閬嶅巻 + if (Tree != NULL) { + + VisitBiTree(Tree); //璁块棶鏍硅妭鐐 + PreOrderTraverse(Tree->lchild); //璁块棶宸﹀瓙鏍戠粨鐐 + PreOrderTraverse(Tree->rchild);//璁块棶鍙冲瓙鏍戠粨鐐 + } +} + +void InOrderTraverse(BiTree Tree) {//涓簭閬嶅巻 + if (Tree != NULL) { + + InOrderTraverse(Tree->lchild);//璁块棶宸﹀瓙鏍戠粨鐐 + + VisitBiTree(Tree);//璁块棶鏍硅妭鐐 + + InOrderTraverse(Tree->rchild);//璁块棶鍙冲瓙鏍戠粨鐐 + } +} + +void PostOrderTraverse(BiTree Tree) {//鍚庡簭閬嶅巻 + if (Tree != NULL) { + + PostOrderTraverse(Tree->lchild); //璁块棶宸﹀瓙鏍戠粨鐐 + + PostOrderTraverse(Tree->rchild); //璁块棶鍙冲瓙鏍戠粨鐐 + + VisitBiTree(Tree); //璁块棶鏍硅妭鐐 + } +} + + +int Depth(BiTree Tree) {//璁$畻浜屽弶鏍戦珮搴 + int depth1, depth2; + if (Tree == NULL) + return 0; + else { + depth1 = Depth(Tree->lchild); + depth2 = Depth(Tree->rchild); + if (depth1 > depth2) + return (depth1 + 1); + else + return (depth2 + 1); + } +} + + +int Width(BiTree Tree) {//璁$畻浜屽弶鏍戝搴 + BiTree queue[1000]; + int front = -1, rear = -1; + + int flag = 0, count = 0,pright;// pright鐢ㄤ簬鎸囧悜鏍戜腑灞傜殑鏈鍙宠竟鐨勭粨鐐癸紝鏍囧織flag璁板綍灞備腑缁撶偣鏁扮殑鏈澶у笺 + if (Tree != NULL) { + rear++; + queue[rear] = Tree; + flag = 1; + pright = rear; + } + while (front < pright) { + front++; + Tree = queue[front]; + if (Tree->lchild != NULL) { + rear++; + queue[rear] = Tree->lchild; + count++; + } + if (Tree->rchild != NULL) { + rear++; + queue[rear] = Tree->rchild; + count++; + } + if (front == pright)// 褰撳墠灞傚凡閬嶅巻瀹屾瘯 + + { + if (flag < count) + flag = count; + count = 0; + pright = rear; + } + } + + return flag; +} +int main() { + BiTree Tree; + int depth,width; + CreateBiTree(Tree); + printf("鍏堝簭閬嶅巻浜屽弶鏍戯細\n"); + PreOrderTraverse(Tree); + printf("\n"); + printf("涓簭閬嶅巻浜屽弶鏍戯細\n"); + InOrderTraverse(Tree); + printf("\n"); + printf("鍚庡簭閬嶅巻浜屽弶鏍戯細\n"); + PostOrderTraverse(Tree); + printf("\n"); + depth=Depth( Tree); + printf("浜屽弶鏍戠殑楂樺害鏄:%d",depth); + width=Width(Tree); + + printf("\n浜屽弶鏍戠殑瀹藉害鏄:%d",width); + + return 0; +} + + +/*娴嬭瘯鐢ㄤ緥*/ +/*ABDG EH I K C F */ +/*ABD F CE */ diff --git "a/2017-1/Mrcui/cconte6/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\346\261\202\345\233\276\344\270\255\344\273\273\346\204\217\344\270\244\347\202\271\351\227\264\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" "b/2017-1/Mrcui/cconte6/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\346\261\202\345\233\276\344\270\255\344\273\273\346\204\217\344\270\244\347\202\271\351\227\264\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" new file mode 100644 index 00000000..0606735b --- /dev/null +++ "b/2017-1/Mrcui/cconte6/\345\271\277\345\272\246\344\274\230\345\205\210\351\201\215\345\216\206\346\261\202\345\233\276\344\270\255\344\273\273\346\204\217\344\270\244\347\202\271\351\227\264\347\232\204\346\234\200\347\237\255\350\267\257\345\276\204.cpp" @@ -0,0 +1,231 @@ +#include +#include + +#define MAX_VEX 20 +#define Queue_Size 100 +int visited[MAX_VEX];//璁块棶鏍囧織鏁扮粍visited +typedef int ElemType; +typedef int VerType; +typedef enum { + OK, + ERROR, + OVERFLOW +} Status; + +typedef struct q_node {//闃熷垪涓殑姣忎釜鑺傜偣 + ElemType data; + struct q_node *previous; + struct q_node *next; +} Qnode, *Pq_node; + +typedef struct link_queue {//闃熷垪 + Pq_node front; + Pq_node rear; +} LinkQueue; + +typedef struct ArcCell {//瀹氫箟寮 + ElemType eadj;//鐢"1"琛ㄧず鐩搁偦,"0"琛ㄧず涓嶇浉閭 +} AdjMatrix[MAX_VEX][MAX_VEX]; + +typedef struct graph {//瀹氫箟鍥 + AdjMatrix arcs; + int arcs_num;//寮ф暟 + int vers_num;//椤剁偣鏁扮洰 +} Graph; + +/*瀵逛簬闃熷垪鍩烘湰鎿嶄綔瀹炵幇*/ +Status InitQueue(LinkQueue *q);//鍒濆鍖栭槦鍒 +Status EnQueue(LinkQueue *q, ElemType e);//鍏ラ槦鍒 +Status DeQueue(LinkQueue *q, ElemType *e);//鍑洪槦鍒 +bool EmptyQueue(LinkQueue q);//鍒ゆ柇闃熷垪鏄惁涓虹┖ +/*瀵逛簬闃熷垪鍩烘湰鎿嶄綔瀹炵幇*/ + +/*瀵逛簬鍥剧殑鎿嶄綔*/ +Status Add(Graph *g, int i, int j);//涓洪偦鎺ョ煩闃佃祴鍊 +Status CreatGraph(Graph *g);//鏋勫缓涓涓浘 +VerType ToFirstAdjVex(Graph g, int num);//杩斿洖绗竴涓偦鎺ラ《鐐 +VerType ToNextAdjVex(Graph g, int i, int j);//杩斿洖涓嬩竴涓偦鎺ラ《鐐 + +void BFSTraverse(Graph g, int a, int b);//鎸夊箍搴︿紭鍏堥潪閫掑綊閬嶅巻鍥緂 +Status PrintFoot(LinkQueue q, VerType start);//杈撳嚭涓ょ偣闂寸殑鏈鐭矾寰 +/*瀵逛簬鍥剧殑鎿嶄綔*/ + + +Status InitQueue(LinkQueue *q) {//鍒濆鍖栭槦鍒 + q->front = (Pq_node) malloc(Queue_Size * sizeof(Qnode)); + q->rear = (Pq_node) malloc(Queue_Size * sizeof(Qnode)); + q->front = q->rear; + if (!(q->front)) { + return ERROR; + } + q->front->next = q->rear->next = NULL;//鍒濆鍖栭槦鍒楀緱澶村拰灏 + return OK; +} + +Status EnQueue(LinkQueue *q, ElemType e) {//鍏ラ槦鍒 + Pq_node ptr; + ptr = (Pq_node) malloc(sizeof(Qnode)); + if (!ptr) { + return ERROR; + } + ptr->data = e; + ptr->next = NULL; + ptr->previous = q->front; + q->rear->next = ptr; + q->rear = ptr; + return OK; +} + +Status DeQueue(LinkQueue *q, ElemType *e) {//鍑洪槦鍒 + if (EmptyQueue(*q)) { + return ERROR; + } + q->front = q->front->next;//闃熷垪闈炵┖鎵嶅彲浠ヨ繘琛屽嚭闃熷垪鎿嶄綔 + *e = q->front->data; + return OK; +} + +bool EmptyQueue(LinkQueue q) {//鍒ゆ柇闃熷垪鏄惁涓虹┖ + if (q.front == q.rear) { + return true; + } + + return false; + +} + +Status Add(Graph *g, int i, int j) {//涓洪偦鎺ョ煩闃佃祴鍊 + if (i >= MAX_VEX || j >= MAX_VEX) { + return ERROR; + } + g->arcs[i][j].eadj = 1; + g->arcs[j][i].eadj = 1; + + return OK; +} + +Status CreatGraph(Graph *g) {//鏋勫缓涓涓浘 + g->vers_num = 9;//鍒濆鍖栧浘鐨勫姬鏁 + g->arcs_num = 12;//鍒濆鍖栧浘鐨勮竟鏁 + int i = 0; + int j = 0; + for (i = 0; i < g->vers_num; i++) { + for (j = 0; j < g->vers_num; j++) { + g->arcs[i][j].eadj = 0;//鍒濆鍖栭偦鎺ョ煩闃,"0"琛ㄧず涓嶇浉閭 + } + } + 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; + +} + +VerType ToFirstAdjVex(Graph g, int num) {//杩斿洖绗竴涓偦鎺ラ《鐐 + int i; + for (i = 0; i < g.vers_num; i++) { + if (g.arcs[num][i].eadj == 1) { + return i; + } + } + return -1; +} + +VerType ToNextAdjVex(Graph g, int i, int j) {//杩斿洖涓嬩竴涓偦鎺ラ《鐐 + int k; + for (k = j + 1; k < g.vers_num; k++) { + if (g.arcs[i][k].eadj == 1) { + return k; + } + } + return -1; +} + +void BFSTraverse(Graph g, int a, int b) {//鎸夊箍搴︿紭鍏堥潪閫掑綊閬嶅巻鍥緂 + + int i = 0, j, k; + int e; + LinkQueue q; + bool flag; + for (i = 0; i < g.vers_num; ++i) { + visited[i] = 0; + + } + InitQueue(&q);//缃┖鐨勮緟鍔╅槦鍒梣 + EnQueue(&q, a);//璧风偣鍏冪礌a杩涘叆闃熷垪 + visited[a] = 1; + flag = false; + while (!EmptyQueue(q)) { + DeQueue(&q, &e);//闃熷ご鍏冪礌鍑洪槦骞剁疆涓篹 + + for (j = ToFirstAdjVex(g, e); j >= 0; j = ToNextAdjVex(g, e, j)) { + if (j == b) { + EnQueue(&q, j); + PrintFoot(q, a); + flag = true; + break; + } + if (!visited[j]) {//j涓篹灏氭湭璁块棶鐨勯偦鎺ラ《鐐 + EnQueue(&q, j); + visited[j] = true; + } + + } + if (flag) { + break; + } + + } + +} + +Status PrintFoot(LinkQueue q, VerType start) {//杈撳嚭涓ょ偣闂寸殑鏈鐭矾寰 + int track[MAX_VEX];//璇ユ暟缁勭敤鏉ュ瓨鍌ㄨ矾寰 + Pq_node ptr; + int i = 0, j; + + ptr = q.rear; + for (i = 0; i < MAX_VEX; i++) { + track[i] = -1; + } + track[0] = ptr->data; + ptr = ptr->previous; + for (i = 1; ptr->data != start; i++) { + track[i] = ptr->data; + ptr = ptr->previous; + } + track[i] = ptr->data; + for (j = i; j >= 0; j--)//鍊掑簭杈撳嚭璺緞 + { + if (track[j] >= 0) { + printf("%d ", track[j] + 1); + } + + } +} + +int main() { + Graph graph; + CreatGraph(&graph); + int i = 0, j = 0; + printf("鍥句腑浠绘剰涓ょ偣闂寸殑鏈鐭窛绂讳负:\n"); + for (i = 0; i < 9; i++) { + for (j = 0; j < 9; j++) { + if (j != i) { + printf("%d<->%d:", i + 1, j + 1); + BFSTraverse(graph, i, j); + printf("\n"); + } + } + } + return 0; +} diff --git a/2017-1/Mrcui/cconte7/BSTOutput.txt b/2017-1/Mrcui/cconte7/BSTOutput.txt new file mode 100644 index 00000000..a095c325 --- /dev/null +++ b/2017-1/Mrcui/cconte7/BSTOutput.txt @@ -0,0 +1,10 @@ +/Users/cclin/Library/Caches/CLion2016.2/cmake/generated/untitled16-2bd8c8f4/2bd8c8f4/Debug/untitled16 +8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30 +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30 +7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30 + +Process finished with exit code 0 + diff --git a/2017-1/Mrcui/cconte7/BSTree.c b/2017-1/Mrcui/cconte7/BSTree.c new file mode 100644 index 00000000..edaf92bc --- /dev/null +++ b/2017-1/Mrcui/cconte7/BSTree.c @@ -0,0 +1,135 @@ +#include "BSTree.h" + +Status CreatBST(BiTree &Tree, ElementType element[], int n) //鍒濆鍖栦簩鍙夋帓搴忔爲 +{ + int i = 0; + for (i = 0; i < n; i++) { + InsertBST(Tree, element[i]); + } + if (Tree) { + return ERROR; + } + return OK; +} + +bool SearchBST(BiTree Tree, BiTree f, BiTree &p, KeyType key) //浜屽弶鎺掑簭鏍戠殑鏌ユ壘 +{ + int flag = 0;//鏄惁鏌ユ壘鎴愬姛鏍囪 + if (!Tree) { + p = f; + flag = 0;//鏈煡鎵炬垚鍔 + return false; + } + else if (key == Tree->data) { + p = Tree; + flag = 1; //鏌ユ壘鎴愬姛 + return true; + } + else if (key < Tree->data) {//鍦ㄥ乏瀛愭爲缁х画鏌ユ壘 + flag = 0; + return SearchBST(Tree->lchild, Tree, p, key); + } + else if (key > Tree->data) {//鍦ㄥ彸瀛愭爲缁х画鏌ユ壘 + flag = 0; + return SearchBST(Tree->rchild, Tree, p, key); + } +} + +Status InsertBST(BiTree &Root, KeyType key) //浜屽弶鎺掑簭鏍戞湭鎵惧埌鍏抽敭瀛楃殑鎻掑叆鍜屾瀯閫犱竴妫典簩鍙夋帓搴忔爲,Tree涓轰紶寮曠敤鎸囬拡 +{ + BiTree p, s; + if (!SearchBST(Root, NULL, p, key)) //鏌ユ壘涓嶆垚鍔 + { + s = (struct BiTNode *) malloc(sizeof(BiTNode)); + s->data = key; + s->lchild = s->rchild = NULL; + if (p == NULL) //浜屽弶鎺掑簭鏍戜负绌虹殑鏃跺欙紝缁撶偣*s琚彃鍏ヤ负鏂扮殑鏍圭粨鐐 + Root = s; + else if (key < p->data) + p->lchild = s;//缁撶偣*s琚彃涓哄乏瀛╁瓙 + else + p->rchild = s;//缁撶偣*s琚彃涓哄彸瀛╁瓙 + } + return OK; +} + +void PreOrderTraverse(BiTree Tree)//鍏堝簭閬嶅巻 +{ + if (Tree != NULL) { + printf(", "); + printf("%d", Tree->data); + PreOrderTraverse(Tree->lchild); //璁块棶宸﹀瓙鏍戠粨鐐 + PreOrderTraverse(Tree->rchild);//璁块棶鍙冲瓙鏍戠粨鐐 + } +} + +void InOrderTraverse(BiTree Tree)//涓簭閬嶅巻 +{ + if (Tree != NULL) { + printf(", "); + InOrderTraverse(Tree->lchild);//璁块棶宸﹀瓙鏍戠粨鐐 + printf("%d", Tree->data); + InOrderTraverse(Tree->rchild);//璁块棶鍙冲瓙鏍戠粨鐐 + } +} + +void VisitBiTree(BiTree Tree) //杈撳嚭鏍硅妭鐐 +{ + printf("%d", Tree->data); +} + +int Delete(BiTree &Tree)//鍒犻櫎鑺傜偣 +{ + BiTree q, s; + if (!Tree->rchild) { //鍙冲瓙鏍戜负绌 + q = Tree; + Tree = Tree->lchild;//閲嶆帴瀹冪殑宸﹀瓙鏍 + free(q); + } + else if (!Tree->lchild) { //宸﹀瓙鏍戜负绌 + q = Tree; + Tree = Tree->rchild; //閲嶆帴瀹冪殑鍙冲瓙鏍 + free(q); + } + else { //宸﹀彸瀛愭爲鍧囦笉涓虹┖ + q = Tree; + s = Tree->lchild; + while (s->rchild) { //鍦ㄥ彸瀛愭爲涓婃悳绱㈠緟鍒犵粨鐐圭殑鍓嶉┍ + q = s; + s = s->rchild; + } + Tree->data = s->data; //s鎸囧悜琚垹闄ょ粨鐐圭殑鍓嶉┍ + if (q != Tree) { + q->rchild = s->lchild;//閲嶆帴*q鐨勫彸瀛愭爲 + } + else { + q->lchild = s->lchild;//閲嶆帴*q鐨勫乏瀛愭爲 + } + delete s; + } + return true; +} + +int DeleteBST(BiTree &Tree, KeyType key)//浜屽弶鎺掑簭鏍戞壘鍒板叧閿瓧鐨勫垹闄 +{ + if (!Tree) + return false; + else { + if (key == Tree->data) { + return Delete(Tree); //鎵惧埌鍏抽敭瀛椾负key鐨勬暟鎹厓绱 + } + else if (key < Tree->data) { + return DeleteBST(Tree->lchild, key);//鍏抽敭瀛楀皬浜庣粨鐐圭殑璇濓紝鍗冲湪宸﹀瓙鏍 + } + else { + return DeleteBST(Tree->rchild, key); //鍏抽敭瀛楀ぇ浜庣粨鐐圭殑璇濓紝鍗冲湪鍙冲瓙鏍 + } + } +} + +void PrintTree(BiTree Tree)//鎵撳嵃浜屽弶鎺掑簭鏍 +{ + VisitBiTree(Tree);//鎵撳嵃鏍硅妭鐐 + PreOrderTraverse(Tree->lchild);//鎵撳嵃宸﹀瓙鏍 + PreOrderTraverse(Tree->rchild);//鎵撳嵃鍙冲瓙鏍 +} diff --git a/2017-1/Mrcui/cconte7/BSTree.h b/2017-1/Mrcui/cconte7/BSTree.h new file mode 100644 index 00000000..b5d12348 --- /dev/null +++ b/2017-1/Mrcui/cconte7/BSTree.h @@ -0,0 +1,27 @@ +#include +#include + +typedef int ElementType; +typedef int KeyType; +typedef enum { + OK, + ERROR, + OVERFLOW +} Status; + +typedef struct BiTNode { + + ElementType data;//鏁版嵁鍏冪礌 + struct BiTNode *lchild;//宸﹀瀛愭寚閽 + struct BiTNode *rchild; //鍙冲瀛愭寚閽 +} BiTNode, *BiTree;//浜屽弶鏍戣妭鐐圭被鍨 + +Status CreatBST(BiTree &Tree, ElementType element[], int n);//鍒濆鍖栦簩鍙夋帓搴忔爲 +bool SearchBST(BiTree Tree, BiTree f, BiTree &p, KeyType key);//浜屽弶鎺掑簭鏍戠殑鏌ユ壘 +Status InsertBST(BiTree &Root, KeyType key); //浜屽弶鎺掑簭鏍戞湭鎵惧埌鍏抽敭瀛楃殑鎻掑叆,Tree涓轰紶寮曠敤鎸囬拡 +void PreOrderTraverse(BiTree Tree);//鍏堝簭閬嶅巻 +void InOrderTraverse(BiTree Tree);//涓簭閬嶅巻 +void VisitBiTree(BiTree Tree);//杈撳嚭鏍硅妭鐐 +int Delete(BiTree &Tree);//鍒犻櫎鑺傜偣 +int DeleteBST(BiTree &Tree, KeyType key);//浜屽弶鎺掑簭鏍戞壘鍒板叧閿瓧鐨勫垹闄 +void PrintTree(BiTree Tree);//鎵撳嵃浜屽弶鎺掑簭鏍 diff --git a/2017-1/Mrcui/cconte7/main.cpp b/2017-1/Mrcui/cconte7/main.cpp new file mode 100644 index 00000000..7be5c7c6 --- /dev/null +++ b/2017-1/Mrcui/cconte7/main.cpp @@ -0,0 +1,27 @@ +#include "BSTree.h" + +int main() { + ElementType element[12] = {8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30}; + KeyType key[5] = {13, 8, 5, 20, 6}; + int i, j, n1, n2; + n1 = sizeof(element) / sizeof(ElementType); + n2 = sizeof(key) / sizeof(KeyType); + BiTree Tree, pp; + Tree = NULL; + CreatBST(Tree, element, n1); + PrintTree(Tree); + printf("\n"); + for (j = 0; j < n2; j++) { + if (SearchBST(Tree, NULL, pp, key[j])) { + DeleteBST(Tree, key[j]); + PrintTree(Tree); + printf("\n"); + } + else { + InsertBST(Tree, key[j]); + PrintTree(Tree); + printf("\n"); + } + } + return 0; +} diff --git a/2017-1/Mrcui/cconte8/Test output.txt b/2017-1/Mrcui/cconte8/Test output.txt new file mode 100644 index 00000000..e9be1fd8 --- /dev/null +++ b/2017-1/Mrcui/cconte8/Test output.txt @@ -0,0 +1,31 @@ +/Users/cclin/Library/Caches/CLion2016.2/cmake/generated/untitled21-2bd8c90e/2bd8c90e/Debug/untitled21 +闅忔満姝f暣鏁板簭鍒: +5 8 10 5 12 12 8 +------------------------------------------ + +鐩存帴鎻掑叆鎺掑簭鍚: +5 5 8 8 10 12 12 +鎬绘瘮杈冩鏁:11,鎬荤Щ鍔ㄨ褰曟鏁:11,浜岃呮鏁颁箣鍜:22 +------------------------------------------ + +甯屽皵鎺掑簭鍚: +5 5 5 8 8 8 12 +鎬绘瘮杈冩鏁:15,鎬荤Щ鍔ㄨ褰曟鏁:4,浜岃呮鏁颁箣鍜:19 +------------------------------------------ + +鍐掓场鎺掑簭鍚: +5 5 8 8 10 12 12 +鎬绘瘮杈冩鏁:28,鎬荤Щ鍔ㄨ褰曟鏁:12,浜岃呮鏁颁箣鍜:40 +------------------------------------------ + +蹇熸帓搴忓悗: +5 5 8 8 10 12 12 +鎬绘瘮杈冩鏁:4,鎬荤Щ鍔ㄨ褰曟鏁:21,浜岃呮鏁颁箣鍜:25 +------------------------------------------ + +绠鍗曢夋嫨鎺掑簭鍚: +5 5 8 8 10 12 12 +鎬绘瘮杈冩鏁:27,鎬荤Щ鍔ㄨ褰曟鏁:4,浜岃呮鏁颁箣鍜:31 +------------------------------------------ + +Process finished with exit code 0 \ No newline at end of file diff --git a/2017-1/Mrcui/cconte8/main.c b/2017-1/Mrcui/cconte8/main.c new file mode 100644 index 00000000..9312da59 --- /dev/null +++ b/2017-1/Mrcui/cconte8/main.c @@ -0,0 +1,40 @@ +#include"sort.h" +int main() { + int array[100]; + int barry[100]; + int i = 0, m = 2, t = 1; + srand(time(NULL)); + int movecount = 0, comparecount = 0; + int length = rand() % 6 + 6; + for (; i < length; i = i + 1) { + barry[i] = array[i] = rand() % 7 + m; + barry[i + 1] = array[i + 1] = rand() % 5 + t; + m = m + 1; + t = t + 2; + } + printf("闅忔満姝f暣鏁板簭鍒:\n"); + for (i = 0; i < length; i++) { + printf("%d ", array[i]); + } + printf("\n------------------------------------------\n"); + insert_sort(array, length); + copyarray(array, barry, length); + printf("\n------------------------------------------\n"); + shell_sort(array, length); + copyarray(array, barry, length); + printf("\n------------------------------------------\n"); + bubble_sort(array, length); + copyarray(array, barry, length); + printf("\n------------------------------------------\n"); + quick_sort(array, 0, length - 1, &movecount, &comparecount); + printf("\n蹇熸帓搴忓悗:\n"); + for (i = 0; i < length; i++) { + printf("%d ", array[i]); + } + printf("\n鎬绘瘮杈冩鏁:%d,鎬荤Щ鍔ㄨ褰曟鏁:%d,浜岃呮鏁颁箣鍜:%d", movecount, comparecount, movecount + comparecount); + copyarray(array, barry, length); + printf("\n------------------------------------------\n"); + select_sort(array, length); + printf("\n------------------------------------------\n"); + return 0; +} diff --git a/2017-1/Mrcui/cconte8/sort.c b/2017-1/Mrcui/cconte8/sort.c new file mode 100644 index 00000000..e2402024 --- /dev/null +++ b/2017-1/Mrcui/cconte8/sort.c @@ -0,0 +1,149 @@ +#include"sort.h" + +void insert_sort(int a[], int n)//鐩存帴鎻掑叆鎺掑簭 +{ + int temp; + int i, j; + int acount = 0, bcount = 0, ccount; + for (i = 1; i < n; i++)//鎺у埗鍏冪礌鐨勬彃鍏 + { + temp = a[i];//璁剧疆鐩戣鍝ㄥ厓绱 + + j = i - 1; + while (bcount++ && temp < a[j]) { + a[j + 1] = a[j];//灏嗘瘮temp澶х殑鍏冪礌渚濇鍚庣Щ + acount++; + j--; + if (j < 0) { + break; + } + } + a[j + 1] = temp;//鎻掑叆鍏冪礌 + acount++; + } + printf("\n鐩存帴鎻掑叆鎺掑簭鍚:\n"); + for (i = 0; i < n; i++) { + printf("%d ", a[i]); + } + ccount = acount + bcount; + printf("\n鎬绘瘮杈冩鏁:%d,鎬荤Щ鍔ㄨ褰曟鏁:%d,浜岃呮鏁颁箣鍜:%d", bcount, acount, ccount); +} + +void shell_sort(int a[], int n)//甯屽皵鎺掑簭 +{ + int di = n / 2; + int i, j; + int temp; + int acount = 0, bcount = 0, ccount = 0; + while (di >= 1) { + for (i = di; i <= n; i++) { + temp = a[i]; + j = i - di; + while (bcount++ && temp < a[j]) { + a[i] = a[j]; + a[j] = temp; + acount++; + j -= di; + if (j <= 0) { + break; + } + } + + } + di = di / 2; + } + printf("\n甯屽皵鎺掑簭鍚:\n"); + for (i = 0; i < n; i++) { + printf("%d ", a[i]); + } + ccount = acount + bcount; + printf("\n鎬绘瘮杈冩鏁:%d,鎬荤Щ鍔ㄨ褰曟鏁:%d,浜岃呮鏁颁箣鍜:%d", bcount, acount, ccount); + +} + +void bubble_sort(int a[], int n)//璧锋场鎺掑簭 +{ + int i, j; + int temp; + int acount = 0, bcount = 0, ccount = 0; + for (i = 0; i < n; i++) { + for (j = i; j < n; j++) { + if (bcount++ && a[i] > a[j]) { + temp = a[i]; + a[i] = a[j]; + a[j] = temp; + acount = acount + 3; + } + } + } + ccount = acount + bcount; + printf("\n鍐掓场鎺掑簭鍚:\n"); + for (i = 0; i < n; i++) { + printf("%d ", a[i]); + } + printf("\n鎬绘瘮杈冩鏁:%d,鎬荤Щ鍔ㄨ褰曟鏁:%d,浜岃呮鏁颁箣鍜:%d", bcount, acount, ccount); +} + +void quick_sort(int a[], int left, int right, int *movecount, int *comcount)//蹇熸帓搴 +{ + int i, j, temp; + i = left; + j = right; + temp = a[left]; + if (left > right) + return; + while (i != j) { + while ((*comcount)++, a[j] >= temp && j > i) { + j--; + } + if (j > i) { + a[i++] = a[j]; + (*movecount)++; + } + while ((*comcount)++, a[i] <= temp && j > i) { i++; } + if (j > i) { + a[j--] = a[i]; + (*movecount)++; + } + + } + a[i] = temp; + quick_sort(a, left, i - 1, movecount, comcount); + quick_sort(a, i + 1, right, movecount, comcount); +} + +void select_sort(int a[], int n)//绠鍗曢夋嫨鎺掑簭 +{ + int min; + int acount = 0, bcount = 0, ccount = 0; + int temp; + int i, j; + for (i = 0; i < n - 1; ++i) { + min = i; + for (j = i; j < n; ++j) { + if (bcount++ && a[j] < a[min]) //浣垮緱min鎬绘槸鎸囧悜鏈灏忓厓绱 + min = j; + } + if (min != i) { + temp = a[i]; + a[i] = a[min]; + a[min] = temp; + acount++; + } + } + ccount = acount + bcount; + printf("\n绠鍗曢夋嫨鎺掑簭鍚:\n"); + for (i = 0; i < n; i++) { + printf("%d ", a[i]); + } + printf("\n鎬绘瘮杈冩鏁:%d,鎬荤Щ鍔ㄨ褰曟鏁:%d,浜岃呮鏁颁箣鍜:%d", bcount, acount, ccount); + +} + +void copyarray(int a[], int b[], int n)//鎭㈠闅忔満搴忓垪 +{ + int i; + for (i = 0; i < n; i++) { + a[i] = b[i]; + } +} diff --git a/2017-1/Mrcui/cconte8/sort.h b/2017-1/Mrcui/cconte8/sort.h new file mode 100644 index 00000000..0c2dec9a --- /dev/null +++ b/2017-1/Mrcui/cconte8/sort.h @@ -0,0 +1,10 @@ +#include +#include +#include + +void insert_sort(int a[], int n);//鐩存帴鎻掑叆鎺掑簭 +void shell_sort(int a[], int n);//甯屽皵鎺掑簭 +void bubble_sort(int a[], int n);//璧锋场鎺掑簭 +void quick_sort(int a[], int left, int right, int *movecount, int *comcount);//蹇熸帓搴 +void select_sort(int array[], int arrayCount);//绠鍗曢夋嫨鎺掑簭 +void copyarray(int a[], int b[], int n);//鎭㈠闅忔満搴忓垪 diff --git "a/2017-1/NarthilZYX/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\345\220\210\345\271\266/MergeList2-12.c" "b/2017-1/NarthilZYX/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\345\220\210\345\271\266/MergeList2-12.c" new file mode 100644 index 00000000..e240676a --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\345\220\210\345\271\266/MergeList2-12.c" @@ -0,0 +1,124 @@ +#include +#include +#include +#define ListLength 2 +typedef int ElemType; +typedef enum { + ERROR, + OK, + OVERFLOW +}Status; +typedef struct LNode{ + ElemType data; + struct LNode *next; +}LNode, *LinkList; +//链表创建函数 +LNode *createList() +{ + LNode *p1, *p2; + LNode *head = NULL; + int n = 0; + int l = ListLength; + p2 = p1 = (LNode *)malloc(sizeof(LNode)); + srand((int)time(NULL)); + p1->data = rand()%101; + printf( "%d ", p1->data ); + while(l){ + n = n + 1; + if( n == 1){ + head = p1; + } + else{ + p2->next = p1; + } + p2 = p1; + p1 = (LNode *) malloc(sizeof(LNode)); + if( l != 1){ + srand((int)time(NULL)); + p1->data = rand()%101 + 3; + printf( "%d ", p1->data ); + } + l--; + } + p2->next = NULL; + printf("\n"); + return head; +}//end of createList + +void printList(LNode *head) +{ + LNode *p; + p = head; + if( head != NULL ){ + do{ + printf("%d\n", p->data); + p = p->next; + }while( p != NULL ); + } +}//end of printList + +LinkList sort(LinkList l) +{ + LinkList p,q,small; + int temp; + for(p=l;p->next!=NULL;p=p->next) + { + small=p; + for(q=p->next;q!=NULL;q=q->next){ + if(q->datadata) + { + small=q; + } + if(small!=p) + { + temp=p->data; + p->data=small->data; + small->data=temp; + } + } + } + return l; +}//end of sort + +Status MergeList_L(LinkList La, LinkList Lb) +{ +//已知单链线性表La和Lb的元素按值非递减排列。 +//归并La和Lb得到新的单链线性表Lc,Lc的元素也按值非递减排列。 + LinkList Lc; + LinkList pa, pb, pc; + pa = La->next; + pb = Lb->next; + Lc = pc = La; +//用La的头结点作为Lc的头结点 + while (pa && pb) { + if (pa->data <= pb->data) { + pc->next = pa; + pc = pa; + pa = pa->next; + } + else { + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + pc->next = pa ? pa : pb; +//插入剩余段 + free(Lb); +//释放Lb的头结点 + Lc = sort(Lc); + printList(Lc); + return OK; +}//end of MergeList_L + +int main() +{ + LNode *A = NULL; + LNode *B = NULL; + A = createList(); + B = createList(); + A = sort(A); + B = sort(B); + MergeList_L(A, B); + return 0; +} \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\345\220\210\345\271\266/MergeList\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/NarthilZYX/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\345\220\210\345\271\266/MergeList\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..f56a8d84 Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\345\220\210\345\271\266/MergeList\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\345\233\276/Graph.c" "b/2017-1/NarthilZYX/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\345\233\276/Graph.c" new file mode 100644 index 00000000..2b4fc5d5 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\345\233\276/Graph.c" @@ -0,0 +1,120 @@ +#include +#include +#include "Graph.h" + +int FirstAdjVex(Graph G, int i) { + int k; + for (k = 0; k < G.vexnum; k++) { + if (G.arcs[i][k].adj == 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) { + return k; + } + } + return -1; +}//返回下一个邻接顶点,没有的话返回-1 +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->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); + + return OK; +}//构建已知图 +Status Add(Graph*G, int x, int y) { + //构造邻接矩阵,相邻则赋值为1 + if (x >= MAX_VERTEX_NUM || y >= MAX_VERTEX_NUM) { + return ERROR; + } + G->arcs[x][y].adj = G->arcs[y][x].adj = 1; + return OK; +}//为邻接矩阵赋值 +void BFSTraverse(Graph G, int a, int b) { + int u, v, w; + bool flag; + LinkQueue Q; //辅助队列 + for (v = 0; v < G.vexnum; ++v) { + visited[v] = False; //初始化访问标志 + } + InitQueue(&Q); // 置空的辅助队列Q + EnQueue(&Q, a); //起点入列 + visited[a] = True; + flag = false; + 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 (!visited[w]) { + EnQueue(&Q, w); + visited[w] = true; + } + } + if (flag) { + break; + } +} +}//广度优先遍历 +Status PrintFoot(LinkQueue Q, int start) { + int foot[MAX_VERTEX_NUM]; + int i; + QueuePtr p; + p = Q.rear; + for (i = 0; i < MAX_VERTEX_NUM; i++) { + foot[i] = -1; + } + foot[0] = p->data;//数据进入数组 + p = p->priou;//沿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); + } + } + return OK; +}//输出路径 + +int main() +{ + Graph G; + int i, j; + + CreateGraph(&G); + + for (i = 0; i < 9; i++) { + for (j = 0; j < 9; j++) { + if (j != i) { + printf("%d<->%d:", i + 1, j + 1); + BFSTraverse(G, i, j); + printf("\n"); + } + } + } + + return 0; +} \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\345\233\276/Graph.h" "b/2017-1/NarthilZYX/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\345\233\276/Graph.h" new file mode 100644 index 00000000..22dce6f1 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\345\233\276/Graph.h" @@ -0,0 +1,75 @@ +#define MAXQSIZE 100 +#define INFINITY INT_MAX +#define MAX_VERTEX_NUM 20 + +int visited[MAX_VERTEX_NUM]; + +typedef int VRType; +typedef int ElemType; + +typedef enum { + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum { + False, + True +}Bool; + +typedef struct ArcCell { // 弧的定义 + VRType adj; // 用1或0表示相邻否; +} AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; + +typedef struct { // 图的定义 + AdjMatrix arcs; // 弧的信息 + int vexnum, arcnum; // 顶点数,弧数 +} Graph; + +typedef struct QNode { + ElemType data; + struct QNode *priou; + struct QNode *next; +}QNode, DuLinkList, *QueuePtr; + +typedef struct { + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +//队列的基本操作// +Status InitQueue(LinkQueue *Q) { + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + Q->front->next = Q->rear->next = NULL; + return OK; +}//初始化队列 +Status EnQueue(LinkQueue *Q, ElemType e) { + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + 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) { + Q->front = Q->front->next; + *e = Q->front->data; + return OK; +}//出列 +Bool QueueEmpty(LinkQueue Q) { + if (Q.front == Q.rear) { + return True; + } + return False; +}; //判断是否为空队列 + +//图的基本操作// +int FirstAdjVex(Graph G, int i); //返回第一个邻接顶点,没有的话返回-1 +int NextAdjVex(Graph G, int i, int j); //返回下一个邻接顶点,没有的话返回-1 +Status CreateGraph(Graph *G); //构建已知图 +Status Add(Graph*G, int x, int y); //为邻接矩阵赋值 +void BFSTraverse(Graph G, int a, int b); //广度优先遍历 +Status PrintFoot(LinkQueue Q, int start); //输出路径 \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\345\233\276/\351\203\250\345\210\206\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/NarthilZYX/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\345\233\276/\351\203\250\345\210\206\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..f3a7eec8 Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232\345\233\276/\351\203\250\345\210\206\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/PostfixExpression\346\267\273\345\212\240\346\240\241\351\252\214.c" "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/PostfixExpression\346\267\273\345\212\240\346\240\241\351\252\214.c" new file mode 100644 index 00000000..021af1e3 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/PostfixExpression\346\267\273\345\212\240\346\240\241\351\252\214.c" @@ -0,0 +1,349 @@ +#include "STACK.h" + +//输入合法性校验函数 +Status isLegal(char exp[100]) +{ + int i,j; + int flag = 0; + for( i = 0; exp[i]!='\0'; i++) + { + if(!IN(exp[i])&&!IN(exp[i+1])) + { + if(exp[i+1]>=48&&exp[i+1]<=57){ + printf("This Program cannot take double-digit.\n"); + } + else{ + printf("You've input illegal oprand.\n"); + } + return ERROR; + } + else{ + return OK; + } + } +} + +//优先级判断函数 +int Precede( SElemType c, SElemType ch ) +{ + int i,j; + switch (c){ + case '+': + i = 0; + if( ch == '+' ){ + j = 0; + } + else if( ch == '-' ){ + j = 1; + } + else if( ch == '*' ){ + j = 2; + } + else if( ch == '/' ){ + j = 3; + } + else if( ch == '(' ){ + j = 4; + } + else if( ch == ')' ){ + j = 5; + } + else{ + j = 6; + } + break; + case '-': + i = 1; + if( ch == '+' ){ + j = 0; + } + else if( ch == '-' ){ + j = 1; + } + else if( ch == '*' ){ + j = 2; + } + else if( ch == '/' ){ + j = 3; + } + else if( ch == '(' ){ + j = 4; + } + else if( ch == ')' ){ + j = 5; + } + else{ + j = 6; + } + break; + case '*': + i = 2; + if( ch == '+' ){ + j = 0; + } + else if( ch == '-' ){ + j = 1; + } + else if( ch == '*' ){ + j = 2; + } + else if( ch == '/' ){ + j = 3; + } + else if( ch == '(' ){ + j = 4; + } + else if( ch == ')' ){ + j = 5; + } + else{ + j = 6; + } + break; + case '/': + i = 3; + if( ch == '+' ){ + j = 0; + } + else if( ch == '-' ){ + j = 1; + } + else if( ch == '*' ){ + j = 2; + } + else if( ch == '/' ){ + j = 3; + } + else if( ch == '(' ){ + j = 4; + } + else if( ch == ')' ){ + j = 5; + } + else{ + j = 6; + } + break; + case '(': + i = 4; + if( ch == '+' ){ + j = 0; + } + else if( ch == '-' ){ + j = 1; + } + else if( ch == '*' ){ + j = 2; + } + else if( ch == '/' ){ + j = 3; + } + else if( ch == '(' ){ + j = 4; + } + else if( ch == ')' ){ + j = 5; + } + else{ + j = 6; + } + break; + case ')': + i = 5; + if( ch == '+' ){ + j = 0; + } + else if( ch == '-' ){ + j = 1; + } + else if( ch == '*' ){ + j = 2; + } + else if( ch == '/' ){ + j = 3; + } + else if( ch == '(' ){ + j = 4; + } + else if( ch == ')' ){ + j = 5; + } + else{ + j = 6; + } + break; + case '#': + i = 6; + if( ch == '+' ){ + j = 0; + } + else if( ch == '-' ){ + j = 1; + } + else if( ch == '*' ){ + j = 2; + } + else if( ch == '/' ){ + j = 3; + } + else if( ch == '(' ){ + j = 4; + } + else if( ch == ')' ){ + j = 5; + } + else{ + j = 6; + } + break; + } + return PT[i][j]; +}//end of Precede + +//表达式求值 +Status Evaluation(Number *SN, char s[100], int length) +{ + nInitStack(SN);//数字所在的栈 + int e1,e2; + int result = 0; + int count = 1;//loop counter + nPush(SN,s[count-1]-'0'); + while(countc:false + { + if(c!='('){ + Pass(suffix, c); + } + Pop(S, &c); + } + if ( ch!= '#' ) { + Push(S, ch); + } + break; + } // switch + } + if (ch != '#') { + p++; + ch = *p; + } + else { + Pop(S, &ch);//删除'#' + Pass(suffix, ch);//'#'进入suffix + break; + } + }//while + return OK; +} // transform + +int main() +{ + SqStack S; + char suf[101] = { NULL }; + char ex[100] = {'\0'}; + char ex2[100] = {'\n'}; + int i = 0; + int flag = 0; + printf("Input an arithmetic expression and don't forget to add a '#' at the end of it.\n"); + gets(ex); + if(isLegal(ex)){ + transform( &S, suf, ex ); + } + else{ + printf("Your inputs are illegal, please reset your input here:\n"); + gets(ex2); + transform( &S, suf, ex2 ); + } + return 0; +} +//4+(3*3-6)/3# +//44+(3*3-6)/3#:error +//4%(3*3-6)/3#:error \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/STACK.h" "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/STACK.h" new file mode 100644 index 00000000..2cf0fb1a --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/STACK.h" @@ -0,0 +1,177 @@ +#include +#include +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +#define MAXNUM 100 +const char OP[7] = { '+', '-', '*', '/', '(', ')', '#' }; +const int PT[7][7] = { + {1,1,0,0,0,1,1}, + {1,1,0,0,0,1,1}, + {1,1,1,1,0,1,1}, + {1,1,1,1,0,1,1}, + {0,0,0,0,0,0,0}, + {1,1,1,1,0,0,0}, + {0,0,0,0,0,0,0} +}; +typedef char SElemType ; +typedef enum{ + ERROR, + OK, + OVERFLOW +}Status; +typedef enum{ + FALSE, + TRUE +}Bool; +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; +typedef struct { + int *base; + int *top; + int stacksize; +}Number; + +//字符栈 +//create a new stack +Status InitStack(SqStack *S) +{ + //need free + S->base = (SElemType *)malloc(STACK_INIT_SIZE * + sizeof(SElemType)); + if(!S->base) { + return OVERFLOW;//fail to allocate memory + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + + return OK; +}//end of InitStack + +Status Push(SqStack *S, SElemType e)// +{ + if(S->top - S->base >= S->stacksize) + { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if(!S->base) + { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + }//课本 + *S->top++ = e; + return OK; +}//end of Push + +Status Pop(SqStack *S, SElemType *e) +{ + if(S->top == S->base) + { + return ERROR; + } + *e = * --S->top;//待检测 + return OK; +}//end of Pop + +//判断空栈 +Status StackEmpty(SqStack *S) +{ + if(S->base == S->top) + { + return OK; + } + else{ + return ERROR; + } +}//end of StackEmpty + +//获取栈顶元素的函数 +Status GetTop(SqStack *S, SElemType *e) +{ + if(S->top == S->base){ + return ERROR; + } + *e = *(S->top-1); + return OK; +}//end of GetTop + +//数字栈 +Status nInitStack(Number *S) +{ + //need free + S->base = (int *)malloc(STACK_INIT_SIZE * + sizeof(int)); + if(!S->base) { + return OVERFLOW;//fail to allocate memory + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + + return OK; +}//end of InitStack + +Status nPush(Number *S, int e)// +{ + if(S->top - S->base >= S->stacksize) + { + S->base = (int *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(int)); + if(!S->base) + { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +}//end of Push + +Status nPop(Number *S, int *e) +{ + if(S->top == S->base) + { + return ERROR; + } + *e = * --S->top;//待检测 + return OK; +}//end of Pop + +//判断空栈 +Status nStackEmpty(Number *S) +{ + if(S->base == S->top) + { + return OK; + } + else{ + return ERROR; + } +}//end of StackEmpty + +//获取栈顶元素的函数 +Status nGetTop(Number *S, int *e) +{ + if(S->top == S->base){ + return ERROR; + } + *e = *(S->top-1); + return OK; +}//end of GetTop + +//操作符的判断函数 +Bool IN(SElemType ch) +{ + int j; + for( j = 0; j < 7; j++ ) + { + if( ch == OP[j] ){ + return TRUE; + } + } + return FALSE; +} \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\345\220\210\346\263\225\346\200\247\346\240\241\351\252\2141.png" "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\345\220\210\346\263\225\346\200\247\346\240\241\351\252\2141.png" new file mode 100644 index 00000000..a485f3b1 Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\345\220\210\346\263\225\346\200\247\346\240\241\351\252\2141.png" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\345\220\210\346\263\225\346\200\247\346\240\241\351\252\2142.png" "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\345\220\210\346\263\225\346\200\247\346\240\241\351\252\2142.png" new file mode 100644 index 00000000..8cfcd3a5 Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\345\220\210\346\263\225\346\200\247\346\240\241\351\252\2142.png" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\346\255\243\345\270\270\350\277\220\350\241\214\347\273\223\346\236\234.jpg" "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\346\255\243\345\270\270\350\277\220\350\241\214\347\273\223\346\236\234.jpg" new file mode 100644 index 00000000..910d4327 Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\346\255\243\345\270\270\350\277\220\350\241\214\347\273\223\346\236\234.jpg" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/Queue.png" "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/Queue.png" new file mode 100644 index 00000000..583376dc Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/Queue.png" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/Queue2.c" "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/Queue2.c" new file mode 100644 index 00000000..6b2a7827 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232\351\230\237\345\210\227\345\256\236\347\216\260\343\200\201\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/Queue2.c" @@ -0,0 +1,209 @@ +#include +#include +#include +#define MAXSIZE 100 +#define QSIZE 10 +typedef int QElemType; +typedef enum{ + ERROR, + OK, + OVERFLOW +}Status; +typedef struct QNode { + QElemType data; + struct QNode *next; +}QNode, * QueuePtr; +typedef struct { + QueuePtr front;//队头指针 + QueuePtr rear;//队尾指针 +}LinkQueue; + +//构造一个空队列Q +Status InitQueue(LinkQueue *Q) +{ + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if(!Q->front) + { + return OVERFLOW; + } + Q->front->next = NULL; + return OK; +} + +//销毁队列Q +Status DestroyQueue(LinkQueue *Q) +{ + while(Q->front) + { + Q->rear = Q->front->next; + free(Q->front); + Q->front = Q->rear; + } + printf("DestroyQueue called.\n"); + return OK; +} + +//将Q清空为空队列 +Status ClearQueue(LinkQueue *Q) +{ + QueuePtr i; + i=Q->front; + while ( i != Q->rear) + { + i=0; + i++; + } + Q->front = Q->rear = NULL; + printf("ClearQueue called.\n"); + return OK; +} + +//若队列Q为空队列,则返回TRUE,否则返回FALSE +Status QueueEmpty(LinkQueue *Q) +{ + if(Q->rear==Q->front) + { + return OK; + } + return ERROR; +} + +//返回Q的元素个数,即为队列的长度 +int QueueLength(LinkQueue Q) +{ + int count = 0; + QueuePtr p; + p=Q.front->next; + while(p) + { + //printf("%d",p->data); + p=p->next; + count++; + } + return count; +} + +//若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR +Status GetHead(LinkQueue Q,QElemType *e) +{ + if(Q.front==Q.rear) + { + return ERROR; + } + *e = Q.front->next->data; + return OK; +} + +//插入元素e为Q的新队尾元素 +Status EnQueue(LinkQueue *Q, QElemType e) +{ + QNode *p; + p = (QueuePtr)malloc(sizeof(QNode)); + if(!p) + { + return OVERFLOW; + } + p->data = e; + p->next = NULL; + Q->rear->next = p; + Q->rear = p; + return OK; +} + +//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR +Status DeQueue(LinkQueue *Q ) +{ + QNode *p; + QElemType e; + if(Q->front==Q->rear) + { + return ERROR; + } + p = Q->front->next; + e = p->data; + printf("%d was deleted.\n", e); + Q->front->next = p->next; + if(Q->rear==p) + { + Q->rear = Q->front; + } + free(p); + return OK; +} + +//队列遍历 +Status TraverseQueue(LinkQueue Q) +{ + QueuePtr p; + QElemType e; + p=Q.front->next; + while(p) + { + e=p->data; + printf("%d",e); + //printf("%d",p->data); + printf(" "); + p=p->next; + } + printf("\n"); + return OK; +} + +int main() +{ + LinkQueue Lq; + QElemType e; + QElemType *q = NULL; + int i;//loop counter + printf("Create a new queue:\n"); + + //test InitQueue + InitQueue(&Lq); + printf("Create random elements:\n"); + + for( i = 0; i < QSIZE; i++ ) + { + srand((int)time(NULL)); + e = rand()%100 + i; + printf("%d ",e); + //test EnQueue; + EnQueue(&Lq, e); + } + + //test QueueLength + printf("\nThe length of the queue is: %d\n", QueueLength(Lq)); + + //test QueueEmpty + if(QueueEmpty(&Lq)) + { + printf("\nThe queue is empty.\n"); + } + else + { + printf("\nThe queue is not empty.\n"); + } + + printf("Get the head of the queue:\n"); + //test GetHead + GetHead(Lq,&e); + printf("%d\n", e); + + printf("Traverse of queue here:\n"); + //test TraverseQueue + TraverseQueue(Lq); + + printf("Dequeue here:\n"); + //test DeQueue + for( i = 0; i < QSIZE; i++ ) + { + DeQueue(&Lq); + } + + //test clearQueue + ClearQueue(&Lq); + + //test DestroyQueue + DestroyQueue(&Lq); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\344\270\203\347\247\215\346\216\222\345\272\217/Sort.c" "b/2017-1/NarthilZYX/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\344\270\203\347\247\215\346\216\222\345\272\217/Sort.c" new file mode 100644 index 00000000..0326d054 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\344\270\203\347\247\215\346\216\222\345\272\217/Sort.c" @@ -0,0 +1,332 @@ +锘#include "Sort.h" +int Move = 0; +int Compare = 0; +//鍒锋柊鏁扮粍 +int *Fresh(int a[]) +{ + int *a2; + a2 = (int *)malloc(sizeof(int)*MAXSIZE); + for(int i = 0; i < MAXSIZE; i++) + { + a2[i] = a[i]; + } + return a2; +} + +//鐩存帴鎻掑叆鎺掑簭 +void InsertSort(int a[], int n) +{ + int i, j; + int compare = 0, move = 0; + for(i = 1; i < n; i++) + if(a[i] < a[i - 1]){ + int temp = a[i]; + compare++; + for(j = i - 1; j >= 0 && a[j] > temp; j--){ + a[j + 1] = a[j]; + move++; + } + a[j + 1] = temp; + } + printf("move = %d, compare = %d, total = %d\n", move, compare, move+compare); +} + +//甯屽皵鎺掑簭 +void ShellSort( int a[], int n ) +{ + int j, gap; + int compare = 0, move = 0; + for(gap = n / 2; gap > 0; gap /= 2) + for (j = gap; j < n; j++)//浠庢暟缁勭gap涓厓绱犲紑濮嬄犅 + if (a[j] < a[j - gap])//姣忎釜鍏冪礌涓庤嚜宸辩粍鍐呯殑鏁版嵁杩涜鐩存帴鎻掑叆鎺掑簭 + { + compare++; + int temp = a[j]; + int k = j - gap; + while(k >= 0 && a[k] > temp) + { + a[k + gap] = a[k]; + k -= gap; + move++; + } + a[k + gap] = temp; + } + printf("move = %d, compare = %d, total = %d\n", move, compare, move+compare); +} + +//璧锋场鎺掑簭 +void BubbleSort( int a[], int n ) +{ + int i, j; + int compare = 0, move = 0; + for(i = 0; i < n; i++) + for(j = 1; j < n - i; j++){ + if (a[j - 1] > a[j]){ + compare++; + int temp = a[j-1]; + a[j-1] = a[j]; + a[j] = temp; + } + move+=3; + } + printf("move = %d, compare = %d, total = %d\n", move, compare, move+compare); +} + +//蹇熸帓搴 +void QuickSort( int a[], int l, int r) +{ + if(l < r) + { + int temp = a[l]; + a[l] = a[(l+r)/2]; + a[(l+r)/2] = temp; + int i = l, j = r, x = a[l]; + while(i < j) + { + while(i < j && a[j] >= x)//聽浠庡彸鍚戝乏鎵剧涓涓皬浜巟鐨勬暟 + { + j--; + Compare++; + } + if(i < j){ + a[i++] = a[j]; + Move++; + } + while(i < j && a[i] < x)//聽浠庡乏鍚戝彸鎵剧涓涓ぇ浜庣瓑浜巟鐨勬暟 + { + i++; + Compare++; + } + if(i < j){ + a[j--] = a[i]; + Move++; + } + } + a[i] = x; + QuickSort(a, l, i -1);//聽閫掑綊璋冪敤 + QuickSort(a, i+ 1, r ); + } +} + +//绠鍗曢夋嫨鎺掑簭 +void SelectSort( int a[], int n ) +{ + int min; + int move = 0; + int compare = 0; + for(int i = 0; i < n - 1; i++) + { + min = i; + for(int j = i + 1; j < n ; j++) + { + if(a[j] < a[min])//浣垮緱min鎬绘槸鎸囧悜鏈灏忓厓绱 + { + compare++; + min = j; + } + } + if(min != i)//鍗砿in鏈夌Щ鍔ㄨ繃 + { + int temp = a[i]; + a[i] = a[min]; + a[min] = temp; + } + move+=3; + } + printf("move = %d, compare = %d, total = %d\n", move, compare, move+compare); +} + +//鍫嗘帓搴 +void HeapAdjust(int a[], int i, int n) +{ + int Child; + int temp; + for(;2 * i + 1 < n; i = Child) + { + //瀛愯妭鐐圭殑浣嶇疆聽=聽2聽*聽(parent(鐖剁粨鐐))聽+聽1 + Child = 2 * i + 1; + //寰楀埌瀛愮粨鐐逛腑杈冨ぇ鐨勭粨鐐 + if(Child < n - 1 && a[Child + 1] > a[Child]){ + ++Child; + Compare++; + } + //濡傛灉杈冨ぇ鐨勫瓙缁撶偣澶т簬鐖剁粨鐐归偅涔堟妸杈冨ぇ鐨勫瓙缁撶偣寰涓婄Щ鍔 + //鏇挎崲瀹冪殑鐖剁粨鐐 + if(a[i] < a[Child]) + { + temp = a[i]; + a[i] = a[Child]; + a[Child] = temp; + Move+=3; + }else{ + break; + } + } +} +//鍫嗘帓搴忕畻娉 +void HeapSort(int a[], int n) +{ + int i; + //璋冩暣搴忓垪鐨勫墠鍗婇儴鍒嗗厓绱狅紝璋冩暣瀹屼箣鍚庣涓涓厓绱 + //鏄簭鍒楃殑鏈澶у厓绱狅紝n/2-1鏄渶鍚庝竴涓潪鍙跺瓙缁撶偣 + for(i = n/2 - 1; i >= 0; --i){ + HeapAdjust(a, i, n); + } + for(i = n - 1; i > 0; --i) + { + a[i] = a[0]+a[i]; + a[0] = a[i]-a[0]; + a[i] = a[i]-a[0]; + Move+=3; + HeapAdjust(a,0,i);//閫掑綊璋冩暣 + } +} + +//褰掑苟鎺掑簭 +void MergeArray(int a[], int first, int mid, int last, int temp[]) +{ + int i = first, j = mid + 1; + int m = mid, n = last; + int k = 0; + while(i <= m && j <=n ) + { + if(a[i] <= a[j]){ + Compare++; + temp[k++] = a[i++]; + } + else{ + Compare++; + temp[k++] = a[j++]; + } + } + while(i <= m) + { + temp[k++] = a[i++]; + } + while(j <= n) + { + temp[k++] = a[j++]; + } + for(i = 0; i < k; i++) + { + a[first + i] = temp[i]; + Move++; + } +} +void MergeSort(int a[], int first, int last, int temp[]) +{ + if(first < last) + { + int mid = (first + last) / 2; + MergeSort(a, first, mid, temp);//宸﹁竟鏈夊簭 + MergeSort(a, mid + 1, last, temp);//鍙宠竟鏈夊簭 + MergeArray(a, first, mid, last, temp);//鍐嶅皢浜屼釜鏈夊簭鏁板垪鍚堝苟 + } +} + +Bool MergeSort(int a[], int n) +{ + int *p = (int *)malloc(sizeof(int)*MAXSIZE); + if(p == NULL){ + return FALSE; + } + MergeSort(a, 0, n - 1, p); + return TRUE; +} + +void printSort(int a[]) +{ + for(int i = 0; i < MAXSIZE; i++) + { + printf("%d ", a[i]); + } + printf("\n"); +} + +int main() +{ + int a[MAXSIZE], *b; + srand((int)time(NULL)); + for(int i = 0; i < MAXSIZE; i++){ + + a[i] = rand()%101 + 5*i; + } + b = (int *)malloc(sizeof(int)*MAXSIZE); + int move = 0; + int compare = 0; + + //test1 + b = Fresh(a); + printf("InsertSort:\n"); + printf("Before sorted:\n"); + printSort(b); + printf("After sorted:\n"); + InsertSort( b, MAXSIZE ); + printSort(b); + + //test2 + b = Fresh(a); + printf("\nShellSort:\n"); + printf("Before sorted:\n"); + printSort(b); + printf("After sorted:\n"); + ShellSort( b, MAXSIZE ); + printSort(b); + + //test3 + b = Fresh(a); + printf("\nBubbleSort:\n"); + printf("Before sorted:\n"); + printSort(b); + printf("After sorted:\n"); + BubbleSort( b, MAXSIZE ); + printSort(b); + + //test4 + b = Fresh(a); + printf("\nQuickSort:\n"); + printf("Before sorted:\n"); + printSort(b); + printf("After sorted:\n"); + QuickSort( b, 0, MAXSIZE-1); + printf("move = %d, compare = %d, total = %d\n", Move, Compare, Move+Compare); + printSort(b); + + //test5 + b = Fresh(a); + printf("\nSelectSort:\n"); + printf("Before sorted:\n"); + printSort(b); + printf("After sorted:\n"); + SelectSort( b, MAXSIZE ); + printSort(b); + + //test6 + b = Fresh(a); + Move = 0; + Compare = 0; + printf("\nHeapSort:\n"); + printf("Before sorted:\n"); + printSort(b); + printf("After sorted:\n"); + HeapSort( b, MAXSIZE ); + printf("move = %d, compare = %d, total = %d\n", Move, Compare, Move+Compare); + printSort(b); + + //test7 + b = Fresh(a); + Move = 0; + Compare = 0; + printf("\nMergeSort:\n"); + printf("Before sorted:\n"); + printSort(b); + printf("After sorted:\n"); + + if(MergeSort( b, MAXSIZE )){ + printf("move = %d, compare = %d, total = %d\n", Move, Compare, Move+Compare); + printSort(b); + } + + + return 0; +} \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\344\270\203\347\247\215\346\216\222\345\272\217/Sort.h" "b/2017-1/NarthilZYX/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\344\270\203\347\247\215\346\216\222\345\272\217/Sort.h" new file mode 100644 index 00000000..8d3aed2c --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\344\270\203\347\247\215\346\216\222\345\272\217/Sort.h" @@ -0,0 +1,41 @@ +#include +#include +#include + +FILE *fpo; + +#define MAXSIZE 20 +typedef enum{ + FALSE, + TRUE +}Bool; + +//刷新数组 +int *Fresh(int a[]); + +//直接插入排序 +void InsertSort( int a[], int n ); + +//希尔排序 +void ShellSort( int a[], int n ); + +//起泡排序 +void BubbleSort( int a[], int n ); + +//快速排序 +void QuickSort( int a[], int l, int r ); + +//简单选择排序 +void SelectSort( int a[], int n ); + +//堆排序 +void HeapAdjust(int a[], int i, int n); +void HeapSort( int a[], int n ); + +//归并排序 +void MergeArray( int a[], int first, int mid, int last, int temp[]); +void MergeSort(int a[], int first, int last, int temp[]); +Bool MergeSort(int a[], int n); + +//输出排序结果 +void printSort(int a[]); \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\344\270\203\347\247\215\346\216\222\345\272\217/\350\277\220\350\241\214\347\273\223\346\236\2341.png" "b/2017-1/NarthilZYX/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\344\270\203\347\247\215\346\216\222\345\272\217/\350\277\220\350\241\214\347\273\223\346\236\2341.png" new file mode 100644 index 00000000..58daef3c Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\344\270\203\347\247\215\346\216\222\345\272\217/\350\277\220\350\241\214\347\273\223\346\236\2341.png" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\344\270\203\347\247\215\346\216\222\345\272\217/\350\277\220\350\241\214\347\273\223\346\236\2342.png" "b/2017-1/NarthilZYX/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\344\270\203\347\247\215\346\216\222\345\272\217/\350\277\220\350\241\214\347\273\223\346\236\2342.png" new file mode 100644 index 00000000..c7aaaa47 Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\344\271\235\346\254\241\344\275\234\344\270\232\344\270\203\347\247\215\346\216\222\345\272\217/\350\277\220\350\241\214\347\273\223\346\236\2342.png" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\346\213\254\345\217\267\345\214\271\351\205\215/BracketPair3-2.c" "b/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\346\213\254\345\217\267\345\214\271\351\205\215/BracketPair3-2.c" new file mode 100644 index 00000000..73b058d6 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\346\213\254\345\217\267\345\214\271\351\205\215/BracketPair3-2.c" @@ -0,0 +1,164 @@ +锘#include +#include +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +#define MAXNUM 100 +typedef char SElemType ; +typedef enum{ + ERROR, + OK, + OVERFLOW +}Status; +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +//create a new stack +Status InitStack(SqStack *S) +{ + //need free + S->base = (SElemType *)malloc(STACK_INIT_SIZE * + sizeof(SElemType)); + if(!S->base) { + return OVERFLOW;//fail to allocate memory + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + + return OK; +}//end of InitStack + +Status Push(SqStack *S, SElemType e)// +{ + if(S->top - S->base >= S->stacksize) + { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if(!S->base) + { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +}//end of Push + +Status Pop(SqStack *S, SElemType e) +{ + if(S->top == S->base) + { + return ERROR; + } + e = * --S->top; + return OK; +}//end of Pop + +//鍒ゆ柇绌烘爤 +Status StackEmpty(SqStack *S) +{ + if(S->base == S->top) + { + return OK; + } + else{ + return ERROR; + } +}//end of StackEmpty + +//鑾峰彇鏍堥《鍏冪礌鐨勫嚱鏁 +Status GetTop(SqStack *S, SElemType *e) +{ + if(S->top == S->base){ + return ERROR; + } + *e = *(S->top-1); + return OK; +} + +Status matching(SqStack *S, char *c) { + int state = 1; + int i = 0; + SElemType e; + while ( c[i]!='\0' && state ) { + switch(c[i]) + { + case '(': { + Push(S, c[i]); + i++; + break; + } + case ')': { + GetTop(S,&e); + if(! StackEmpty(S) && e == '(') { + Pop(S,e); + } else { + state = 0; + } + i++; + break; + } + case '{': { + Push(S, c[i]); + i++; + break; + } + case '}': { + GetTop(S,&e); + if(! StackEmpty(S) && e == '{') { + Pop(S,e); + } else { + state = 0; + } + i++; + break; + } + case '[': { + Push(S, c[i]); + i++; + break; + } + case ']': { + GetTop(S,&e); + if(! StackEmpty(S) && e == '[') { + Pop(S,e); + } else { + state = 0; + } + i++; + break; + } + } + + } + if ( StackEmpty(S) && state) + { + return OK; + } + else + { + return ERROR; + } +} + +int main() +{ + SqStack S; + char c[MAXNUM]; + int flag=0; + InitStack(&S); + printf("Input a group of brackets as you like!\n"); + scanf("%s",c); + flag = matching(&S,c); + if(matching(&S,c)){ + printf("Matching succeed!"); + } + else{ + printf("Matching fail!"); + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\346\213\254\345\217\267\345\214\271\351\205\215/BracketsPair\350\277\220\350\241\214\347\273\223\346\236\234.jpg" "b/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\346\213\254\345\217\267\345\214\271\351\205\215/BracketsPair\350\277\220\350\241\214\347\273\223\346\236\234.jpg" new file mode 100644 index 00000000..e86bd495 Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\346\213\254\345\217\267\345\214\271\351\205\215/BracketsPair\350\277\220\350\241\214\347\273\223\346\236\234.jpg" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\346\225\260\345\210\266\350\275\254\346\215\242/Conversion3-1.c" "b/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\346\225\260\345\210\266\350\275\254\346\215\242/Conversion3-1.c" new file mode 100644 index 00000000..78061bc5 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\346\225\260\345\210\266\350\275\254\346\215\242/Conversion3-1.c" @@ -0,0 +1,90 @@ +#include +#include +#include +typedef enum { + ERROR, + OK, + OVERFLOW +}Status; + +typedef int SElemType; + +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +Status InitStack(SqStack *S) +{ + //need free + S->base = (SElemType *)malloc(STACK_INIT_SIZE * + sizeof(SElemType)); + if(!S->base) { + return OVERFLOW;//fail to allocate memory + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + + return OK; +} +Status Push(SqStack *S, SElemType e)// +{ + if(S->top - S->base >= S->stacksize) { + return OVERFLOW; + } + *S->top++ = e; + return OK; +} +Status Pop(SqStack *S, SElemType &e) +{ + if(S->top == S->base){ + return ERROR; + } + e = * --S->top; + return OK; +} +Status StackEmpty(SqStack *S) +{ + if(S->base == S->top){ + return OK; + } + else{ + return ERROR; + } +} + +Status ModConvert(int input, SqStack *S , int d) +{ + SElemType e; + e = *S->top; + if(d > 10) { + return ERROR; + } + while(input) { + Push(S, input % d); + input = input / d; + } + while(!StackEmpty(S)){ + Pop(S, e); + printf("%d", e); + } + return OK; +} + +int main() +{ + SqStack S; + InitStack(&S); + int num; + int d = 8; + srand((int)time(NULL)); + num = rand()%10001;//create random number + printf("%d\n",num); + ModConvert(num, &S, d); + system("pause"); + return 0; +} \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\346\225\260\345\210\266\350\275\254\346\215\242/Conversion\350\277\220\350\241\214\347\273\223\346\236\234.jpg" "b/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\346\225\260\345\210\266\350\275\254\346\215\242/Conversion\350\277\220\350\241\214\347\273\223\346\236\234.jpg" new file mode 100644 index 00000000..335be6a4 Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\346\225\260\345\210\266\350\275\254\346\215\242/Conversion\350\277\220\350\241\214\347\273\223\346\236\234.jpg" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\350\241\214\347\274\226\350\276\221/Editor3-3.c" "b/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\350\241\214\347\274\226\350\276\221/Editor3-3.c" new file mode 100644 index 00000000..c3a485b9 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\350\241\214\347\274\226\350\276\221/Editor3-3.c" @@ -0,0 +1,146 @@ +#include +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +#define MAXNUM 100 +typedef char SElemType ; +typedef enum{ + ERROR, + OK, + OVERFLOW +}Status; +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +//create a new stack +Status InitStack(SqStack *S) +{ + //need free + S->base = (SElemType *)malloc(STACK_INIT_SIZE * + sizeof(SElemType)); + if(!S->base) { + return OVERFLOW;//fail to allocate memory + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + + return OK; +}//end of InitStack + +Status Push(SqStack *S, SElemType e) +{ + if(S->top - S->base >= S->stacksize) + { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if(!S->base) + { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +}//end of Push + +Status Pop(SqStack *S, SElemType *e) +{ + if(S->top == S->base) + { + return ERROR; + } + *e = * --S->top; + return OK; +}//end of Pop + +//判断空栈 +Status StackEmpty(SqStack *S) +{ + if(S->base == S->top) + { + return OK; + } + else{ + return ERROR; + } +}//end of StackEmpty + +//获取栈顶元素的函数 +Status GetTop(SqStack *S, SElemType *e) +{ + if(S->top == S->base){ + return ERROR; + } + *e = *(S->top-1); + return OK; +}//end of GetTop + +//清空栈 +Status ClearStack(SqStack *S) +{ + if(S->top==S->base) + return ERROR; + S->top=S->base; + return OK; +}//end of ClearStack + +//销毁栈 +Status DestroyStack(SqStack *S) +{ + free(S->base); + S->base = NULL; + S->top = NULL; + S->stacksize = 0; + return OK; +}//end of destroy Stack + +Status LineEdit(SqStack *S) +{ + SElemType e;//栈顶元素 + SElemType *b;//指向栈底,用于输出 + char ch; + ch = getchar(); + while(ch != EOF) + { + while(ch!=EOF&&ch!='\n') + { + switch(ch){ + case '#': + Pop(S,&e); + break; + case '@': + ClearStack(S); + break; + default: + Push(S,ch); + break; + } + ch = getchar(); + } + b = S->base; + while(b!=S->top) + { + printf("%c",*b); + ++b; + } + ClearStack(S); + if( ch!=EOF ) + ch = getchar(); + } + return OK; +}//end of LineEdit + +int main() +{ + SqStack S; + InitStack(&S); + printf("Create a statement as you like!\n"); + LineEdit(&S); + return 0; +} +//whil##ilr#e(s#*s) +//outcha@putchar(*s=#++) \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\350\241\214\347\274\226\350\276\221/LineEditor\350\277\220\350\241\214\347\273\223\346\236\234.jpg" "b/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\350\241\214\347\274\226\350\276\221/LineEditor\350\277\220\350\241\214\347\273\223\346\236\234.jpg" new file mode 100644 index 00000000..b942c0bc Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\346\240\2103/\350\241\214\347\274\226\350\276\221/LineEditor\350\277\220\350\241\214\347\273\223\346\236\234.jpg" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232\346\240\221/BiTree2.h" "b/2017-1/NarthilZYX/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232\346\240\221/BiTree2.h" new file mode 100644 index 00000000..4cc66c1c --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232\346\240\221/BiTree2.h" @@ -0,0 +1,23 @@ +#include +#include + +typedef char ElemType; +typedef enum { + OK, + ERROR, + OVERFLOW +}Status; +typedef struct BiTNode { + ElemType data; + struct BiTNode *lchild, *rchild; +} BiTNode, *BiTree; + +Status PostOrderTraverse(BiTree T) { + //后序遍历二叉树的递归算法 + if (T) { + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c", T->data); + } + return OK; +} diff --git "a/2017-1/NarthilZYX/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232\346\240\221/BiTree3.c" "b/2017-1/NarthilZYX/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232\346\240\221/BiTree3.c" new file mode 100644 index 00000000..09980e5c --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232\346\240\221/BiTree3.c" @@ -0,0 +1,121 @@ +#include "BiTree.h" + +//test1 +char c[] = "ABDG EH I K C F "; +int i = 0; + +Status CreateBiTree(BiTree *T) { + char ch = c[i]; //遍历字符串 + if (ch == ' ') { + *T = NULL; + i++; + } + else { + if (!((*T) = (BiTree)malloc(sizeof(BiTNode)))) { + return OVERFLOW; + } + (*T)->data = ch; // 生成根结点 + i++; + CreateBiTree(&(*T)->lchild); // 构造左子树 + CreateBiTree(&(*T)->rchild); // 构造右子树 + } + return OK; +} + +//test2 +int Search(char ino[], char p); +Status CrtBT(BiTree *T, char pre[], char ino[], int ps, int is, int n); +void print(char *pre, char *in, BiTree T); + +int Search(char ino[], char p) { + //在ino[]中寻找字符p,找到则返回位置,否则返回-1 + int i; + for (i = 0; ino[i] != '\0'; i++) { + if (ino[i] == p) { + return i; + } + } + return -1; +} + +int Getlength(char *p) { + //获取字符串的长度 + int i; + for (i = 0; p[i] != '\0'; i++); + return i; +} + +Status CrtBT(BiTree *T, char pre[], char ino[], int ps, int is, int n) { + // 已知pre[ps...ps+n-1]为二叉树的先序序列, ino[is...is+n-1]为二叉树的中序序列 + // 本算法由此两个序列构造二叉链表 + int k; + if (n == 0) { + *T = NULL; + } + else { + k = Search(ino, pre[ps]); // 在中序序列中查询pre[ps]在ino中的第几个位置 + if (k == -1) { + *T = NULL; + } + else { + if (!((*T) = (BiTree)malloc(sizeof(BiTNode)))) { + return OVERFLOW; + } + (*T)->data = pre[ps]; + if (k == is) { + (*T)->lchild = NULL; + } + else { + CrtBT(&(*T)->lchild, pre, ino, ps + 1, is, k - is); + } + if (k == is + n - 1) { + (*T)->rchild = NULL; + } + else { + CrtBT(&(*T)->rchild, pre, ino, ps + 1 + (k - is), k + 1, n - (k - is) - 1); + } + } + } + return OK; +} + +int main(){ + BiTree Tree = NULL; + int flag; + //test1 + printf("Teat1 is as followed.\n"); + flag = CreateBiTree(&Tree); + if (!flag) { + printf("Tree creating succeeded!\n"); + printf("The original input: %s\n", c); + printf("The postorder traverse: "); + PostOrderTraverse(Tree); + printf("\n"); + } + else { + printf("Tree creating failed!\n"); + } + printf("\n"); + + //test2 + int k; + char p1[] = "ABDFCE"; + char p2[] = "DFBAEC"; + + k = Getlength(p1); + flag = CrtBT(&Tree, p1, p2, 0, 0, k); + printf("Test2 is as followed.\n"); + if (!flag) { + printf("Tree creating succeeded!\n"); + printf("Preorder input: %s\n", p1); + printf("Inorder input: %s\n", p2); + printf("The postorder traverse: "); + PostOrderTraverse(Tree); + printf("\n"); + } + else { + printf("Created tree failed!\n"); + } + + return 0; +} \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232\346\240\221/\344\277\256\346\224\271\345\220\216\347\232\204\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/NarthilZYX/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232\346\240\221/\344\277\256\346\224\271\345\220\216\347\232\204\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..111b8667 Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232\346\240\221/\344\277\256\346\224\271\345\220\216\347\232\204\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\346\237\245\346\211\276\346\240\221/Tree.h" "b/2017-1/NarthilZYX/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\346\237\245\346\211\276\346\240\221/Tree.h" new file mode 100644 index 00000000..ff0e024a --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\346\237\245\346\211\276\346\240\221/Tree.h" @@ -0,0 +1,184 @@ +#include +#include +#include +typedef int ElemType; +typedef enum{ + ERROR, + OK +}Status; +typedef enum{ + False, + True +}Bool; +typedef struct BiTNode { + ElemType data; + struct BiTNode *lchild, *rchild; +} BiTNode, *BSTTree, *AVLTree, *B3Tree; + +//------BSTTree------// +// 在给定的BST中插入结点,其数据域为element, 使之称为新的BST +Bool BSTInsert(BiTNode * &p, int element) +{ + if(NULL == p) // 空树 + { + p = (BSTTree)malloc(sizeof(BiTNode)); + p->data = element; + p->lchild = p->rchild = NULL; + return True; + } + + if(element == p->data) // BST中不能有相等的值 + { + return False; + } + if(element < p->data) // 递归 + { + return BSTInsert(p->lchild, element); + } + return BSTInsert(p->rchild, element); // 递归 +} + +// 建立BST +Status createBST(BiTNode * &T, int a[], int n) +{ + T = NULL; + int i; + for(i = 0; i < n; i++) + { + BSTInsert(T, a[i]); + } + return OK; +} + +void preOrderTraverse(BSTTree T) + //先序遍历二叉排列树 +{ + if(T) + { + printf("%d ",T->data); + preOrderTraverse(T->lchild); + preOrderTraverse(T->rchild); + } +} +Status Delete(BSTTree &p)//从二叉排序树中删除结点p,并重接它的左或右子树 +{ + BiTNode *q, *s; + if(!p->rchild)//右子树空则只需重接它的左子树 + { + q=p; + p=p->lchild; + free(q); + } + else if(!p->lchild)//左子树空只需重接它的右子树 + { + q=p; + p=p->rchild; + free(q); + } + else//左右子树均不空 + { + q=p; + s=p->lchild; + while(s->rchild)//转左,然后向右到尽头 + { + q=s; + s=s->rchild; + } + p->data=s->data;//s指向被删结点的“前驱” + if(q!=p)//以上循环至少执行了一次 + q->rchild=s->lchild; + else + q->lchild=s->lchild;//重接*q的左子树 + delete(s); + } + return OK; +} +Status searchBST(BSTTree T,ElemType key) + //若二叉排序树T中存在关键字等于key的数据元素时,则删除该数据元素,并返回TRUE;否则返回FALSE +{ + if(!T) + { + return ERROR; + } + else + { + if(key==T->data)//找到关键字等于key的数据元素 + { + return OK; + } + else if(keydata){ + return searchBST(T->lchild,key); + } + else{ + return searchBST(T->rchild,key); + } + } + return OK; +} + +Status Delete(BSTTree *p) +{ + /* 从二叉排序树中删除节点p, 并重接它的左或右子树 */ + BSTTree 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)->data = s->data; + if( q != *p ) /* 判断是否执行上述while循环 */ + { + q->rchild = s->lchild; /* 执行上述while循环,重接右子树 */ + } + else { + q->lchild = s->lchild; /* 未执行上述while循环,重接左子树 */ + } + free(s); + } + return OK; +} + +Status DeleteBST(BSTTree *T, int key) +{ + /* 若二叉排序树T中存在关键字等于key的数据元素时,则删除该数据元素节点 */ + /* 并返回TRUE;否则返回FALSE */ + if( !(*T)) { + return ERROR; /* 不存在关键字等于key的数据元素 */ + } + else + { + if( key == (*T)->data ){ + Delete(T); + } + else if( key < (*T)->data){ + return DeleteBST(&(*T)->lchild, key); + } + else { + return DeleteBST(&(*T)->rchild, key); + } + } + } + +//------AVLTree------// + +//------B3Tree------// \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\346\237\245\346\211\276\346\240\221/TreeSearch.cpp" "b/2017-1/NarthilZYX/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\346\237\245\346\211\276\346\240\221/TreeSearch.cpp" new file mode 100644 index 00000000..89411cce --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\346\237\245\346\211\276\346\240\221/TreeSearch.cpp" @@ -0,0 +1,45 @@ +#include "Tree.h" + +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 + BSTTree T; + printf("BSTtree:\n"); + if(createBST(T, a, n)){ + printf("Tree creating succeeded:)\n"); + preOrderTraverse(T); + printf("\n"); + } + else{ + printf("Tree creating faild:("); + } + + for( i = 0; i < 5; i++ ) + { + if(searchBST(T,b[i])){ + printf("FOUND %d in the BSTtree! Now delete it from the tree.\n", b[i]); + printf("After delete: "); + DeleteBST(&T,b[i]); + preOrderTraverse(T); + printf("\n\n"); + } + else{ + printf("%d NOT FOUND:( Now insert it into the tree.\n", b[i]); + if(BSTInsert(T,b[i])){ + printf("After insert: "); + preOrderTraverse(T); + printf("\n\n"); + } + else{ + printf("Insert failed:("); + } + } + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\346\237\245\346\211\276\346\240\221/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/NarthilZYX/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\346\237\245\346\211\276\346\240\221/\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..89c8e4bd Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232\346\237\245\346\211\276\346\240\221/\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\346\240\221\347\232\204\346\267\261\345\272\246\347\255\211/BiTree.h" "b/2017-1/NarthilZYX/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\346\240\221\347\232\204\346\267\261\345\272\246\347\255\211/BiTree.h" new file mode 100644 index 00000000..5ee4f3a0 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\346\240\221\347\232\204\346\267\261\345\272\246\347\255\211/BiTree.h" @@ -0,0 +1,216 @@ +#include +#include +#include +typedef char ElemType; +typedef struct BiTNode { + ElemType data; + struct BiTNode *lchild, *rchild; +} BiTNode, *BiTree, *QElemType; + +//队列基本操作 +typedef enum{ + OK, + ERROR +}Status; +typedef struct QNode { + QElemType data; + struct QNode *next; +}QNode, * QueuePtr; +typedef struct { + QueuePtr front;//队头指针 + QueuePtr rear;//队尾指针 +}LinkQueue; + +//构造一个空队列Q +Status InitQueue(LinkQueue *Q) +{ + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if(!Q->front) + { + return ERROR; + } + Q->front->next = NULL; + return OK; +} + +//若队列Q为空队列,则返回TRUE,否则返回FALSE +Status QueueEmpty(LinkQueue *Q) +{ + if(Q->rear==Q->front) + { + return OK; + } + return ERROR; +} + +//插入元素e为Q的新队尾元素 +Status EnQueue(LinkQueue *Q, QElemType e) +{ + QNode *p; + p = (QueuePtr)malloc(sizeof(QNode)); + if(!p) + { + return ERROR; + } + p->data = e; + p->next = NULL; + Q->rear->next = p; + Q->rear = p; + return OK; +} + +//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR +Status DeQueue(LinkQueue *Q, QElemType e ) +{ + QNode *p; + if(Q->front==Q->rear) + { + return ERROR; + } + p = Q->front->next; + e = p->data; + printf("%d was deleted.\n", e); + Q->front->next = p->next; + if(Q->rear==p) + { + Q->rear = Q->front; + } + free(p); + return OK; +} + +//返回Q的元素个数,即为队列的长度 +int QueueLength(LinkQueue Q) +{ + int count = 0; + QueuePtr p; + p=Q.front->next; + while(p) + { + //printf("%d",p->data); + p=p->next; + count++; + } + return count; +} + +Status PostOrderTraverse(BiTree T) { + //后序遍历二叉树的递归算法 + if (T) { + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c", T->data); + } + return OK; +} + +int Depth(BiTree T){ // 返回二叉树的深度 + int depthval; + int depthLeft, depthRight; + if(!T) { + depthval = 0; + } else { + depthLeft = Depth(T->lchild); + depthRight= Depth(T->rchild); + depthval = 1 + (depthLeft > depthRight ? depthLeft : depthRight); + } + return depthval; +} + +int Width(BiTree T) { + // 层序遍历二叉树,返回二叉树的宽度 + LinkQueue Q; + BiTNode *p = T; + int width = 1; //二叉树宽度 + int temp = 0; //上一层宽度 + int last = 0; + int now = 0; //当前宽度 + + if (!T) { + return 0; + } + InitQueue(&Q); //建立工作队列 + EnQueue(&Q, T); //将根节点入队列 + last = 1; + while (!QueueEmpty(&Q)) { + temp = last; + while (temp != 0) { + DeQueue(&Q, T); + if (p->lchild) { + EnQueue(&Q, p->lchild); + } + if (p->rchild) { + EnQueue(&Q, p->rchild); + } + temp--; + } + now = QueueLength(Q); + width = now > width ? now : width; + last = now; + } + return ++width; +} + +int Count(BiTree T) { + //返回指针T所指二叉树中所有结点个数 + int m, n; + if (!T) { + return 0; + } + if ((!T->lchild) && (!T->rchild)) { + return 1; + } + else { + m = Count(T->lchild); + n = Count(T->rchild); + return m + n + 1; + } +} + +int CountLeaf(BiTree T) { + // 返回指针T所指二叉树中所有叶子结点个数 + int m, n; + if (!T) { + return 0; + } + if ((!T->lchild) && (!T->rchild)) { + return 1; + } + else { + m = CountLeaf(T->lchild); + n = CountLeaf(T->rchild); + return m + n; + } +} + +bool Compelete(BiTree T) { + //判断是否为完全二叉树 + LinkQueue Q; + BiTNode *p = T; + InitQueue(&Q); //建立工作队列 + EnQueue(&Q, T); + + while (!QueueEmpty(&Q)) { + DeQueue(&Q, T); + if (p->lchild) { + EnQueue(&Q, p->lchild); + } + if (p->rchild) { + EnQueue(&Q, p->rchild); + } + if ((!p->lchild) && p->rchild) { + return false; + } + } + return true; +} + +bool Full(BiTree T) { + //判断是否为满树(若树的深度为k,应有2^k-1个结点) + if (pow(2.0, Depth(T)) - 1 == Count(T)) { + printf("It is a full tree\n"); + return true; + } + printf("It is not a full tree\n"); + return false; +} diff --git "a/2017-1/NarthilZYX/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\346\240\221\347\232\204\346\267\261\345\272\246\347\255\211/TreeDepth2.c" "b/2017-1/NarthilZYX/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\346\240\221\347\232\204\346\267\261\345\272\246\347\255\211/TreeDepth2.c" new file mode 100644 index 00000000..faf58596 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\346\240\221\347\232\204\346\267\261\345\272\246\347\255\211/TreeDepth2.c" @@ -0,0 +1,189 @@ +#include "BiTree.h" + +//test1 +char c[] = "ABC DE G F "; +int i = 0; + +Status CreateBiTree(BiTree *T) { + char ch = c[i]; //遍历字符串 + if (ch == ' ') { + *T = NULL; + i++; + } + else { + if (!((*T) = (BiTree)malloc(sizeof(BiTNode)))) { + return ERROR; + } + (*T)->data = ch; // 生成根结点 + i++; + CreateBiTree(&(*T)->lchild); // 构造左子树 + CreateBiTree(&(*T)->rchild); // 构造右子树 + } + return OK; +} + +//test2 +int Search(char ino[], char p); +Status CrtBT(BiTree *T, char pre[], char ino[], int ps, int is, int n); +void print(char *pre, char *in, BiTree T); + +int Search(char ino[], char p) { + //在ino[]中寻找字符p,找到则返回位置,否则返回-1 + int i; + for (i = 0; ino[i] != '\0'; i++) { + if (ino[i] == p) { + return i; + } + } + return -1; +} + +int Getlength(char *p) { + //获取字符串的长度 + int i; + for (i = 0; p[i] != '\0'; i++); + return i; +} + +Status CrtBT(BiTree *T, char pre[], char ino[], int ps, int is, int n) { + // 已知pre[ps...ps+n-1]为二叉树的先序序列, ino[is...is+n-1]为二叉树的中序序列 + // 本算法由此两个序列构造二叉链表 + int k; + if (n == 0) { + *T = NULL; + } + else { + k = Search(ino, pre[ps]); // 在中序序列中查询pre[ps]在ino中的第几个位置 + if (k == -1) { + *T = NULL; + } + else { + if (!((*T) = (BiTree)malloc(sizeof(BiTNode)))) { + return ERROR; + } + (*T)->data = pre[ps]; + if (k == is) { + (*T)->lchild = NULL; + } + else { + CrtBT(&(*T)->lchild, pre, ino, ps + 1, is, k - is); + } + if (k == is + n - 1) { + (*T)->rchild = NULL; + } + else { + CrtBT(&(*T)->rchild, pre, ino, ps + 1 + (k - is), k + 1, n - (k - is) - 1); + } + } + } + return OK; +} + + +int main(){ + BiTree Tree = NULL; + int flag; + + //test1 + printf("Teat1 is as followed.\n"); + flag = CreateBiTree(&Tree); + if (!flag) { + printf("Tree creating succeeded!\n"); + printf("The original input: %s\n", c); + printf("The postorder traverse: "); + PostOrderTraverse(Tree); + printf("\n"); + printf("The depth of the tree is: %d\n",Depth(Tree)); + printf("The width of the tree is: %d\n",Width(Tree)); + printf("The number of nodes is:%d\n", Count(Tree)); + printf("The number of leaves is:%d\n", CountLeaf(Tree)); + printf("The number of nodes is: %d\n", Count(Tree) - CountLeaf(Tree)); + if (Compelete(Tree)) { + printf("It is a compeleted tree\n"); + } + else { + printf("It is not a compeleted tree\n"); + } + Full(Tree); + } + + else { + printf("Tree creating failed!\n"); + } + printf("\n"); + + //test2 + int k; + char p1[] = "ABDFCE"; + char p2[] = "DFBAEC"; + + k = Getlength(p1); + flag = CrtBT(&Tree, p1, p2, 0, 0, k); + printf("Test2 is as followed.\n"); + if (!flag) { + printf("Tree creating succeeded!\n"); + printf("Preorder input: %s\n", p1); + printf("Inorder input: %s\n", p2); + printf("The postorder traverse: "); + PostOrderTraverse(Tree); + printf("\n"); + printf("The depth of the tree is %d\n", Depth(Tree)); + printf("The width of the tree is %d\n", Width(Tree)); + printf("The number of nodes is:%d\n", Count(Tree)); + printf("The number of leaves is:%d\n", CountLeaf(Tree)); + printf("The number of nodes is: %d\n", Count(Tree) - CountLeaf(Tree)); + if (Compelete(Tree)) { + printf("It is a compeleted tree\n"); + } + else { + printf("It is not a compeleted tree\n"); + } + Full(Tree); + } + else { + printf("Created tree failed!\n"); + } + + //第二题新增用例 + char p3[] ="ABCDEFGHK"; //构建新的先序序列 + char p4[] = "BDCAEHGKF"; //构建新的中序序列 + flag = CrtBT(&Tree, p3, p4, 0, 0, Getlength(p3)); + printf("\nTest Three is as followed\n"); + if (!flag) { + printf("Tree creating succeeded!\n"); + printf("Preorder input: %s\n", p3); + printf("Inorder input: %s\n", p4); + if (Compelete(Tree)) { + printf("It is a compeleted tree\n"); + } + else { + printf("It is not a compeleted tree\n"); + } + Full(Tree); + } + else { + printf("Created tree failed!\n"); + } + + char p5[] = "ABCDEFG"; //构建新的先序序列 + char p6[] = "CBDAFEG"; //构建新的中序序列 + flag = CrtBT(&Tree, p5, p6, 0, 0, Getlength(p5)); + printf("\nTest Four is as followed\n"); + if (!flag) { + printf("Tree creating succeeded!\n"); + printf("Preorder input: %s\n", p5); + printf("Inorder input: %s\n", p6); + if (Compelete(Tree)) { + printf("It is a compeleted tree\n"); + } + else { + printf("It is not a compeleted tree\n"); + } + Full(Tree); + } + else { + printf("Created tree failed!\n"); + } + + return 0; +} \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\346\240\221\347\232\204\346\267\261\345\272\246\347\255\211/\346\226\260\345\242\236\347\224\250\344\276\213\347\232\204\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/NarthilZYX/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\346\240\221\347\232\204\346\267\261\345\272\246\347\255\211/\346\226\260\345\242\236\347\224\250\344\276\213\347\232\204\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..27506da0 Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\346\240\221\347\232\204\346\267\261\345\272\246\347\255\211/\346\226\260\345\242\236\347\224\250\344\276\213\347\232\204\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\346\240\221\347\232\204\346\267\261\345\272\246\347\255\211/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/NarthilZYX/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\346\240\221\347\232\204\346\267\261\345\272\246\347\255\211/\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..7e147e06 Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\346\240\221\347\232\204\346\267\261\345\272\246\347\255\211/\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/NarthilZYX/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232\345\274\200\346\224\276\345\256\232\345\235\200/Hash.c" "b/2017-1/NarthilZYX/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232\345\274\200\346\224\276\345\256\232\345\235\200/Hash.c" new file mode 100644 index 00000000..4a3c2d40 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232\345\274\200\346\224\276\345\256\232\345\235\200/Hash.c" @@ -0,0 +1,176 @@ +#include "Hash.h" + +//质数判断函数 +Bool isPrime( int num ) +{ + for (int i = 2; i < num; ++i) { + if (num % i == 0) + return False; + } + return True; +} +//除数获取函数 +int getPrime( HashTable h ) +{ + int p = h.sizeindex; + if(p==1) + return 1; + while(!isPrime(p--)); + return p+1; +} + +//哈希表是否为空及是否已满的判断函数 +Bool isEmpty( HashTable h ) +{ + if(h.count == 0) + return True; + return False; +} +Bool isFull ( HashTable h ) +{ + if(h.count == h.sizeindex) + return True; + return False; +} + +//新建哈希表及数据 +HashTable createHashTable( int sizeindex ) +{ + HashTable temp; + temp.count = 0; + temp.sizeindex = sizeindex; + temp.elem = (ElemType *)malloc(sizeindex * sizeof(ElemType)); + for( int i = 0; i < sizeindex; i++) + { + temp.elem[i].key = EMPTYKEY; + temp.elem[i].val = EMPTYVALUE; + } + return temp; +} +ElemType newElemType( KeyType key, ValueType value ) +{ + ElemType temp; + temp.key = key; + temp.val = value; + return temp; +} +//重建哈希表 +HashTable rebuildHashTable( HashTable h ) +{ + int sizeindex = 2 * h.sizeindex; + HashTable temp = createHashTable( sizeindex ); + for( int i = 0; i < sizeindex; i++) + { + if(h.elem[i].key != EMPTYKEY && h.elem[i].key != NULLKEY) + InsertHT(temp, h.elem[i]); + } + free(h.elem); + return temp; +} + +Status SearchHashTable( HashTable h,KeyType k ){ + //在开放定址哈希表ha中查找关键码为k的元素,若查找成功, + //以p指示待查数据元素在表中位置,并返回OK,否则,以p + //指示其插入位置,并返回ERROR,collision用以计冲突次数,其初值置零,供建表插入时参考 + int i = 0; + int p = getPrime(h); + int pos = k % p; + int collision = 0; + while (h.elem[i].key!=NULLKEY && h.elem[pos].key!=k) + //该位置中填有记录且关键字不相等 + { + i++; //采用线性探查法找下一个地址 + collision++; + ++pos; + pos = pos % h.sizeindex; + } + if(collision) + printf("在冲突 %d 次之后,", collision); + printf("于 %d 处找到关键字 %d \n", pos, k); + return OK; +} + +//删除哈希表中关键字k +int DeleteHT(HashTable ha, ElemType elem){ + int p = getPrime(ha); + int pos = elem.key %p; + int collision = 0; + while(ha.elem[pos].key != elem.key) + { + collision++; + ++pos; + pos = pos % ha.sizeindex; + } + ha.elem[pos] = newElemType(DELKEY, DELVAL); + ha.count -= 1; + if(collision) + printf("在冲突 %d 次之后,", collision); + printf("于 %d 处找到元素{ %d ->%d }并删除 %d \n", pos, elem.key, elem.val); + return OK; +} + +//将关键字k插入到哈希表中 +void InsertHT(HashTable ha, ElemType k ){ + int i; + int p = getPrime(ha); + int pos = k.key % p; + int collision = 0; + if (ha.elem[pos].key==NULLKEY || ha.elem[pos].key==DELKEY) //x[j]可以直接放在哈希表中 + { + ha.elem[pos] = k; + ha.count+=1; + } + else //发生冲突时,采用线性探查法解决冲突 + { + i=1; //i记录x[j]发生冲突的次数 + do + { + pos = (pos+1)%p; + i++; + } while (ha.elem[pos].key!=NULLKEY && ha.elem[pos].key!=DELKEY); + ha.elem[pos]=k; + ha.count+=1; + collision++; + } + if (collision) { + printf("冲突 %d 次后,", collision); + } + printf("在 %d 位置插入了元素{ %d -> %d} \n", pos, k.key, k.val ); +} + +//输出哈希表 +void printHashTable(HashTable ha){ + for (int i = 0; i < ha.sizeindex; ++i) { + printf("{ [%d] : %d->%d }\n", i, ha.elem[i].key, ha.elem[i].val); + } +} + + +int main() +{ + srand(time(NULL)); + HashTable ha = createHashTable(10); + + ElemType* testInputs; + testInputs = (ElemType *)malloc(6 * sizeof(ElemType)); + for(int i = 0; i < 6; i++) + { + testInputs[i] = newElemType(rand() % 100, rand() % 100); + } + + for (int i = 0; i < 6; ++i) { + InsertHT(ha,testInputs[i]); + } + printHashTable(ha); + + printf("查找:\n"); + for (int i = 0; i < 6/2; ++i) { + SearchHashTable(ha, testInputs[rand()%6].key); + } + for (int i = 0; i < 6/2; ++i) { + SearchHashTable(ha,rand()%100); + } + + ha = rebuildHashTable(ha); + printHashTable(ha); +} \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232\345\274\200\346\224\276\345\256\232\345\235\200/Hash.h" "b/2017-1/NarthilZYX/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232\345\274\200\346\224\276\345\256\232\345\235\200/Hash.h" new file mode 100644 index 00000000..633f6ff5 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\345\215\201\346\254\241\344\275\234\344\270\232\345\274\200\346\224\276\345\256\232\345\235\200/Hash.h" @@ -0,0 +1,59 @@ +#include +#include +#include +typedef enum{ + ERROR, + OK +}Status; +typedef enum{ + False, + True +}Bool; +#define NULLKEY -1 //定义空关键字值 +#define DELKEY -2 //定义被删关键字值 +#define DELVAL -2 +#define EMPTYKEY 0 +#define EMPTYVALUE 0 +typedef int KeyType; +typedef int ValueType; +typedef struct _ElemType { + KeyType key; // 关键字 + ValueType val; // 值 +#ifdef CHAINED_HASH + struct _ElemType *next; +#endif +} ElemType; + +#define MaxSize 100 //定义最大哈希表长度 +int hashsize[10] = {13, 19, 29, 41, 59, 79, 107, 149, 197, 263 }; +typedef struct { + ElemType *elem;//数据元素存储基址,动态分配内存 + int count;//已被使用的数据的个数 + int sizeindex;//数据总个数 +}HashTable; + +//质数判断函数,待写 +Bool isPrime( int num ); +//质数获取函数 +int getPrime( HashTable h ); + +//哈希表是否为空及是否已满的判断函数,待写 +Bool isEmpty( HashTable h ); +Bool isFull ( HashTable h ); + +//新建哈希表及数据,待写 +HashTable newHashTable( int sizeindex ); +ElemType newElemType( KeyType key, ValueType value ); +//重建哈希表 +HashTable rebuildHashTable( HashTable h ); + +Status SearchHashTable( HashTable ha,KeyType k ); + +//删除哈希表中关键字k +int DeleteHT(HashTable ha, ElemType elem); + +//将关键字k插入到哈希表中 +void InsertHT(HashTable ha, ElemType elem); + +//输出哈希表 +void printHashTable(HashTable ha); \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\346\213\206\345\210\206/LinkDepart.c" "b/2017-1/NarthilZYX/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\346\213\206\345\210\206/LinkDepart.c" new file mode 100644 index 00000000..35478673 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\346\213\206\345\210\206/LinkDepart.c" @@ -0,0 +1,51 @@ +#include "LinkList.h" + +Status LinkDepart(LinkList L) +{ + LinkList La, Lb; + La=(LNode *)malloc(sizeof(L)); + Lb=(LNode *)malloc(sizeof(L)); + int i = 0; + LinkList p,pa,pb; + LinkList ra,rb; + p = L; + pa = La; + pb = Lb; + La->next = Lb->next = NULL; + while(p) + { + if(i%2==0){ + ra = (LNode *)malloc(sizeof(LNode)); + ra->data = p->data; + pa->next = ra; + pa = ra; + } + else{ + rb = (LNode *)malloc(sizeof(LNode)); + rb->data = p->data; + pb->next = rb; + pb = rb; + } + i++; + p = p->next; + } + pa->next = pb->next = NULL; + La = La->next; + Lb = Lb->next; + printf("LinkListA:\n"); + TraverseList(La); + printf("LinkListB:\n"); + TraverseList(Lb); + free(ra); + free(rb); + system("pause"); + return OK; +} + +int main() +{ + LNode *list = NULL; + list = createList(); + LinkDepart(list); + return 0; +} \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\346\213\206\345\210\206/LinkList.h" "b/2017-1/NarthilZYX/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\346\213\206\345\210\206/LinkList.h" new file mode 100644 index 00000000..b8477cb5 --- /dev/null +++ "b/2017-1/NarthilZYX/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\346\213\206\345\210\206/LinkList.h" @@ -0,0 +1,61 @@ +#include +#include +#include +#define ListLength 12 +typedef int ElemType; +typedef enum { + ERROR, + OK, + OVERFLOW +}Status; +typedef struct LNode{ + ElemType data; + struct LNode *next; +}LNode, *LinkList; +//链表创建函数 +LNode *createList() +{ + LNode *p1, *p2; + LNode *head = NULL; + int n = 0; + int l = ListLength; + p2 = p1 = (LNode *)malloc(sizeof(LNode)); + srand((int)time(NULL)); + p1->data = rand()%101; + printf( "%d ", p1->data ); + while(l){ + n = n + 1; + if( n == 1){ + head = p1; + } + else{ + p2->next = p1; + } + p2 = p1; + p1 = (LNode *) malloc(sizeof(LNode)); + if( l != 1){ + srand((int)time(NULL)); + p1->data = rand()%101 + 5*n; + printf( "%d ", p1->data ); + } + l--; + } + p2->next = NULL; + printf("\n"); + return head; +}//end of createList + +//链表遍历函数 +Status TraverseList(LNode *head) +{ + LNode *p; + p = head; + if( head != NULL ){ + do{ + printf("%d ", p->data); + p = p->next; + }while( p != NULL ); + } + printf("\n"); + return OK; +}//end of printList \ No newline at end of file diff --git "a/2017-1/NarthilZYX/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\346\213\206\345\210\206/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/NarthilZYX/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\346\213\206\345\210\206/\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..576d5ce6 Binary files /dev/null and "b/2017-1/NarthilZYX/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232\351\223\276\350\241\250\346\213\206\345\210\206/\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/Orange33/QQ\346\210\252\345\233\27620170321230807.png" "b/2017-1/Orange33/QQ\346\210\252\345\233\27620170321230807.png" new file mode 100644 index 00000000..079fa8e8 Binary files /dev/null and "b/2017-1/Orange33/QQ\346\210\252\345\233\27620170321230807.png" differ diff --git a/2017-1/Orange33/homework.cpp b/2017-1/Orange33/homework.cpp new file mode 100644 index 00000000..d2e01428 --- /dev/null +++ b/2017-1/Orange33/homework.cpp @@ -0,0 +1,86 @@ +锘#include +#include + +typedef int ElemType; + +typedef struct LNode{ + ElemType data; + struct LNode* next; + }LNode,*LinkList; + +void CreateList(LinkList *L,int n)/*绠楁硶2.11*/{ + /*聽閫嗕綅搴(鎻掑湪琛ㄥご)杈撳叆n涓厓绱犵殑鍊硷紝寤虹珛甯﹁〃澶寸粨鏋勭殑鍗曢摼绾挎ц〃L聽*/ + int i; + LinkList p; + printf("璇疯緭鍏b涓殑鍊间釜鏁癨n"); + scanf_s("%d",&n); + printf("璇疯緭鍏b涓墍鏈夊糪n"); + *L=(LinkList)malloc(sizeof(struct LNode)); + (*L)->next=NULL;/*寤虹珛涓涓甫澶寸粨鐐圭殑鍗曢摼琛*/ + for(i=n;i>0;--i){ + p=(LinkList)malloc(sizeof(struct LNode));/*鐢熸垚鏂扮粨鐐*/ + scanf_s("%d",&p->data); + p->next=(*L)->next;/*聽鎻掑叆鍒拌〃澶*/ + (*L)->next=p; + } +} +void CreateList2(LinkList *L,int n){ +/*姝d綅搴(鎻掑湪琛ㄥ熬)杈撳叆n涓厓绱犵殑鍊硷紝寤虹珛甯﹁〃澶寸粨鏋勭殑鍗曢摼绾挎ц〃聽*/ + int i; + LinkList p,q; + *L=(LinkList)malloc(sizeof(struct LNode));/*鐢熸垚澶寸粨鐐*/ + (*L)->next=NULL; + q=*L; + printf("璇疯緭鍏a涓殑鍊间釜鏁癨n"); + scanf_s("%d",&n); + printf("璇疯緭鍏a涓墍鏈夊糪n"); + for(i=1;i<=n;i++){ + p=(LinkList)malloc(sizeof(struct LNode)); + scanf_s("%d",&p->data); + q->next=p; + q=q->next; + } + p->next=NULL; +} +void MergeList(LinkList La,LinkList *Lb,LinkList *Lc)/*聽绠楁硶2.12聽*/{ + LinkList pa=La->next,pb=(*Lb)->next,pc; + *Lc=pc=La;/*鐢↙a鐨勫ご缁撶偣浣滀负Lc鐨勫ご缁撶偣*/ + while(pa&&pb) + if(pa->data<=pb->data){ + pc->next=pa; + pc=pa; + pa=pa->next; + } + else{ + pc->next=pb; + pc=pb; + pb=pb->next; + } + pc->next=pa?pa:pb;/*鎻掑叆鍓╀綑娈*/ + free(*Lb);/*聽閲婃斁Lb鐨勫ご缁撶偣*/ + Lb=NULL; +} +void ListTraverse(const LinkList L, void(*func)(ElemType)){ + LNode *p = L->next; + while (p){ + func(p->data); + p = p->next; + } +} +void visit(ElemType c)/*ListTraverse()璋冪敤鐨勫嚱鏁*/{ + printf("%d ",c); +} +void main() +{ + int n=0; + LinkList La,Lb,Lc; + CreateList2(&La,n); + printf("La=");/*聽杈撳嚭閾捐〃La鐨勫唴瀹*/ + ListTraverse(La,visit); + CreateList(&Lb,n); + printf("Lb=");/*聽杈撳嚭閾捐〃Lb鐨勫唴瀹孤*/ + ListTraverse(Lb,visit); + MergeList(La,&Lb,&Lc);/*褰掑苟La鍜孡b,寰楀埌鏂拌〃Lc聽*/ + printf("Lc=");/*聽杈撳嚭閾捐〃Lc鐨勫唴瀹*/ + ListTraverse(Lc,visit); +} \ No newline at end of file diff --git "a/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/LineEdit.c" "b/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/LineEdit.c" new file mode 100644 index 00000000..b0902bbf --- /dev/null +++ "b/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/LineEdit.c" @@ -0,0 +1,125 @@ +#include +#include + +#define STACK_INIT_SIZE 100//存储空间初始分配 +#define STACKINCREMENT 10//存储空间分配增量 +#define OVERFLOW -2 +#define OK 1 +#define ERROR 0 + +typedef char SElemType; + +typedef struct { + SElemType *base; //在栈构造之前和销毁之后base的值为NULL + SElemType *top;//栈顶指针 + int stacksize; //当前已分配的存储空间,以元素为单位 +}SqStack; + +//构造一个空栈 +int InitStack(SqStack *S) +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));//分配基本空间 + if(!S->base) + { + return OVERFLOW; + }//判断是否分配成功 + S->top = S->base; //指向栈顶 + S->stacksize = STACK_INIT_SIZE; //初始链表最大长度 + return 0; +} + +//插入新的栈顶数据 +int Push(SqStack *S,SElemType e) +{ + if((S->top-S->base)>=S->stacksize) + { + S->base = (SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); + if(!S->base) + { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return 0; +} + +//若栈不空 则删除栈顶数据并用e返回且返回OK 否则返回ERROR +int Pop(SqStack *S,SElemType *e) +{ + if(S->top == S->base) + { + return ERROR; + } + *e = *--S->top; + return 0; +} + +//清空栈中的元素 +int ClearStack(SqStack *S) +{ + S->top = S->base; + return OK; +} + +//销毁栈 +int DestoryStack(SqStack *S) +{ + S->top = S->base; + free(S->base); + S->top = NULL; + S->base = NULL; + return OK; +} + +void LineEdit() +{ + //利用字符栈S,从终端接收一行并传送至调用过程的数据区。 + char ch,*temp; + SqStack S; + InitStack(&S); //构造空栈S + printf("请输入一行(#:退格;@:清行)(输入Ctrl+Z的时候结束循环):\n"); + ch = getchar(); //从终端接收第一个字符 + while (ch != EOF) //EOF为全文结束符) + { + while (ch != EOF && ch != '\n') + { + switch (ch) + { + case '#': + Pop(&S, &ch); + break; // 仅当栈非空时退栈 + case '@': + ClearStack(&S); + break; // 重置S为空栈 + default : + Push(&S, ch); + break; // 有效字符进栈,未考虑栈满情形 + } + ch = getchar(); // 从终端接收下一个字符 + } + + // 将从栈底到栈顶的栈内字符传送至调用过程的数据区; + temp=S.base; + while(temp!=S.top) + { + printf("%c",*temp); //这里做控制台的输出;依情况讨论 + ++temp; + } + ClearStack(&S); // 重置S为空栈 + printf("\n"); + if (ch != EOF) + { + printf("请输入一行(#:退格;@:清行)(输入Ctrl+Z的时候结束循环):\n"); + ch = getchar(); + } + } + DestoryStack(&S); +} + +int main() +{ + LineEdit(); + return 0; +} diff --git "a/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/LineEdit.png" "b/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/LineEdit.png" new file mode 100644 index 00000000..8806578e Binary files /dev/null and "b/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/LineEdit.png" differ diff --git "a/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/test.c" "b/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/test.c" new file mode 100644 index 00000000..591cc4df --- /dev/null +++ "b/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/test.c" @@ -0,0 +1,106 @@ +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配 +#define STACKINCREMENT 10 //存储空间分配增量 +#define OVERFLOW -2 + +typedef struct SqStack{ + int *base; //栈底 + int *top; //栈顶 + int StackSize; //栈当前的存储空间 +}SqStack; + +//初始化空栈 +void InitStack(SqStack *S) +{ + S->base=(int *)malloc(STACK_INIT_SIZE*sizeof(int)); //分配基本空间 + S->StackSize=STACK_INIT_SIZE; //初始链表最大长度 + S->top=S->base; //指向栈顶 +}; +//判断栈是否为空 +int StackEmpty(SqStack S) +{ + if(S.base==S.top) + { + return 1; + } + else + { + return 0; + } +}; + +//压栈 +void push(SqStack *S,int e) +{ + if(S->top-S->base>=S->StackSize) //空间不足 + { + S->base=(int *)realloc(S->base,(S->StackSize+STACKINCREMENT)*sizeof(int)); + S->top=S->base+S->StackSize; + S->StackSize+=STACKINCREMENT; + } + *(S->top)=e; + S->top++; +}; + +//出栈 +void pop(SqStack *S,int *e) +{ + if(S->base!=S->top) + { + S->top--; + *e=*S->top; + } +}; + + +int main() +{ + + char test[100]; + char *p; + int e; + SqStack S; + + InitStack(&S); //初始化空栈 + p=test; //指针p指向字符数组ch + + printf("请输入括号:"); + gets(test); + + while(*p) + { + switch (*p) + { + case '{': + case '[': + case '(': push(&S,*p++);break;//只要是左括号就入栈 + case '}': + case ']': + case ')':pop(&S,&e); //只要是右括号就与栈中的左括号e配对 + if ((e=='{' && *p=='}') ||(e=='[' && *p==']') || (e=='(' && *p==')')) + { + p++; + } + else + { + printf("括号不匹配!"); + return OVERFLOW; + } + break; + default: + p++; + } + } + if (StackEmpty(S)) + { + printf("括号匹配成功"); + }//检查栈是否为空,若为空则代表所有括号都匹配成功 + else + { + printf("缺少右括号!"); + }//否则栈中有剩余的左括号,匹配失败 + printf("\n"); +} + diff --git "a/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/test.png" "b/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/test.png" new file mode 100644 index 00000000..316fd092 Binary files /dev/null and "b/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/test.png" differ diff --git "a/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/translate.c" "b/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/translate.c" new file mode 100644 index 00000000..f91c3e26 --- /dev/null +++ "b/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/translate.c" @@ -0,0 +1,115 @@ +#include +#include +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配 +#define STACKINCREMENT 10 //存储空间分配增量 + +#define OVERFLOW -2 + +typedef struct{ + int *base; //在栈构造之前和销毁之后base的值为NULL + int *top; //栈顶指针 + int sackbase; //当前已分配的存储空间,以元素为单位 +}SqStack; + + +//构造一个空栈 +int InitStack(SqStack *S) +{ + S->base = (int *)malloc(STACK_INIT_SIZE*sizeof(int)); //分配基本空间 + if(!S->base) + { + return(OVERFLOW); + }//判断是否分配成功 + S->top = S->base; //指向栈顶 + S->sackbase = STACK_INIT_SIZE; //初始链表最大长度 + return 0; +} + + + +//若S为空栈返回OK 否则返回ERROR +int StackEmpty(SqStack *S) +{ + if(S->top == S->base) + { + return 1; + } + else + { + return 0; + } +} + + +//插入新的栈顶数据 +int Push(SqStack *S,int e) +{ + if(S->top - S->base >= S->sackbase)//空间不足 + { + S->base = (int*)realloc(S->base,(S->sackbase+STACKINCREMENT)*sizeof(int)); + if(!S->base) + { + return(OVERFLOW); + } + S->top = S->base + S->sackbase; + S->sackbase += STACKINCREMENT; + } + *S->top ++= e; + return 0; +} + + + +//若栈不空 则删除栈顶数据并用e返回且返回OK 否则返回ERROR +int Pop(SqStack *S,int *e) +{ + if(S->top == S->base) + { + return 1; + } + *e=*--S->top;//删除S的栈顶元素,弹出原顶部,再把指针指向e + { + return 0; + } +} + + +void convertion(SqStack *S,int d,int N) +{ + //N为待转换的数,d为需转换成的数制 + InitStack(S); + + while(N) + { + Push(S,N%d); + N = N/d; + } + while(!StackEmpty(S)) + { + int e; + Pop(S,&e); + printf("%d",e); + } +} + + +void main() +{ + int N;int d; + SqStack S; + InitStack(&S); + srand((unsigned)time(NULL)); + printf("要转换的数:\n"); + N = rand()%1024; + printf("%d\n",N); + printf("输入要转换成的数制:\n"); + scanf_s("%d",&d); + printf("转换数字后的数为:\n"); + convertion(&S,d,N); + printf("\n"); + +} + diff --git "a/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/translate.png" "b/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/translate.png" new file mode 100644 index 00000000..43fb426f Binary files /dev/null and "b/2017-1/Orange33/\344\270\211\344\270\252\347\256\227\346\263\225\344\277\256\346\224\271\347\211\210/translate.png" differ diff --git "a/2017-1/Orange33/\346\213\254\345\217\267\345\214\271\351\205\215\346\243\200\351\252\214.c" "b/2017-1/Orange33/\346\213\254\345\217\267\345\214\271\351\205\215\346\243\200\351\252\214.c" new file mode 100644 index 00000000..4bbd27f0 --- /dev/null +++ "b/2017-1/Orange33/\346\213\254\345\217\267\345\214\271\351\205\215\346\243\200\351\252\214.c" @@ -0,0 +1,130 @@ +#include +#include +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配 +#define STACKINCREMENT 10 //存储空间分配增量 +#define OVERFLOW -2 + + +typedef struct +{ + char *base; //在栈构造之前和销毁之后base的值为NULL + char *top; //栈顶指针 + char StackSize; //当前已分配的存储空间,以元素为单位 +}SqStack; + + +//构造一个空栈 +int InitStack (SqStack *S) +{ + S->base = (char *)malloc(STACK_INIT_SIZE * sizeof(char)); //分配基本空间 + if(!S->base) //判断是否分配成功 + return OVERFLOW; + S->top = S->base; //指向栈顶 + S->StackSize = STACKINCREMENT; //初始链表最大长度 + return 0; +} + + +//若S为空栈返回OK 否则返回ERROR +int StackEmpty(SqStack S) +{ + if(S.base==S.top) + return 1; + else + return 0; +} + + +//若栈不空则用e返回栈顶数据并返回OK否则返回ERROR +int GetTop(SqStack *S, char *e) +{ + if(S->top == S->base) + return 1; + *e = *(S->top - 1); + return 0; +} + +//插入新的栈顶数据 +int Push(SqStack *S, int e) +{ + if(S->top - S->base >=S->StackSize) //空间不足 + { + S->base = (char *)realloc(S->base, (S->StackSize + STACKINCREMENT) * sizeof(char)); + if(!S->base) + return OVERFLOW; + S->top = S->base + S->StackSize; + S->StackSize += STACKINCREMENT; + } + S->top++; + *S->top = e; + return 0; +} + +//若栈不空 则删除栈顶数据并用e返回且返回OK 否则返回ERROR +int Pop(SqStack *S,char *e) +{ + if(StackEmpty(*S)) //空栈 + return 1; + *e = *--S->top; + return 0; +} + + +void Matching(char e[]) +{ + unsigned int i = 0; + int state = 1; + char z; + SqStack S; + + while((int)(i <= strlen(e)) && state && e[i] != '\0') //结束条件 超出数组长度或state为0或字符串结束 + { + switch(e[i]) + { + case '(': + case '{': + Push(&S,e[i]); //遇到({则进栈 + i++; + break; + case ')': + GetTop(&S,&z); + if(!StackEmpty(S) && z == '(') //遇到)则判断栈顶是不是(,是的话出栈,不是则不匹配 + { + Pop(&S,&z); + i++; + } + else + state = 0; + break; + case '}': + GetTop(&S,&z); + if(!StackEmpty(S) && z == '{')//遇到}则判断栈顶是不是{,是则出栈,不是则不匹配 + { + Pop(&S,&z); + i++; + } + else + state = 0; + break; + } + } + if(StackEmpty(S) && state) //空栈且state不为0则全部匹配 + printf("括号全部匹配\n"); + else + printf("括号不匹配\n"); +} + +int main() +{ + char test[100]; + SqStack S; + InitStack(&S); + printf("请输入括号:"); + scanf_s("%s",test); + Matching(test); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/Orange33/\346\225\260\345\210\266\350\275\254\346\215\242.c" "b/2017-1/Orange33/\346\225\260\345\210\266\350\275\254\346\215\242.c" new file mode 100644 index 00000000..1a2243c5 --- /dev/null +++ "b/2017-1/Orange33/\346\225\260\345\210\266\350\275\254\346\215\242.c" @@ -0,0 +1,103 @@ +#include +#include +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配 +#define STACKINCREMENT 10 //存储空间分配增量 + +#define OVERFLOW -2 + +typedef struct{ + int *base; //在栈构造之前和销毁之后base的值为NULL + int *top; //栈顶指针 + int sackbase; //当前已分配的存储空间,以元素为单位 +}SqStack; + + +//构造一个空栈 +int InitStack(SqStack *S) +{ + S->base = (int *)malloc(STACK_INIT_SIZE*sizeof(int)); //分配基本空间 + if(!S->base)exit(OVERFLOW); //判断是否分配成功 + S->top = S->base; //指向栈顶 + S->sackbase = STACK_INIT_SIZE; //初始链表最大长度 + return 0; +} + + + +//若S为空栈返回OK 否则返回ERROR +int StackEmpty(SqStack *S) +{ + if(S->top == S->base) + return 1; + else + return 0; +} + + +//插入新的栈顶数据 +int Push(SqStack *S,int e) +{ + if(S->top - S->base >= S->sackbase)//空间不足 + { + S->base = (int*)realloc(S->base,(S->sackbase+STACKINCREMENT)*sizeof(int)); + if(!S->base)exit(OVERFLOW); + S->top = S->base + S->sackbase; + S->sackbase += STACKINCREMENT; + } + *S->top ++= e; + return 0; +} + + + +//若栈不空 则删除栈顶数据并用e返回且返回OK 否则返回ERROR +int Pop(SqStack *S,int *e) +{ + if(S->top == S->base) + return 1; + *e=*--S->top;//删除S的栈顶元素,弹出原顶部,再把指针指向e + return 0; +} + + +void convertion(SqStack *S,int d,int N) +{ + //N为待转换的数,d为需转换成的数制 + InitStack(S); + + while(N) + { + Push(S,N%8); + N = N/d; + } + while(!StackEmpty(S)) + { + int e; + Pop(S,&e); + printf("%d",e); + } +} + + +int main() +{ + + int N;int d; + SqStack S; + InitStack(&S); + srand((unsigned)time(NULL)); + printf("要转换的数:\n"); + N = rand()%1024; + printf("%d\n",N); + printf("输入要转换成的数制:\n"); + scanf_s("%d",&d); + printf("转换数字后的数为:\n"); + convertion(&S,d,N); + printf("\n"); + + return 0; +} + diff --git "a/2017-1/Orange33/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/graph.c" "b/2017-1/Orange33/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/graph.c" new file mode 100644 index 00000000..2679f766 --- /dev/null +++ "b/2017-1/Orange33/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/graph.c" @@ -0,0 +1,224 @@ +锘#include +#include + +#define MAXQSIZE 100 //鏈澶у +#define MAX_VERTEX_NUM 20 //鏈澶ч《鐐规暟 +#define INFINITY 10000000 //鏈澶у +#define OK 1 +int visited[MAX_VERTEX_NUM];//璁块棶鏍囧織鏁扮粍 + + +typedef struct ArcCell // 寮х殑瀹氫箟 +{ + int adj; // 鐢1鎴0琛ㄧず鏄惁鐩搁偦 +} AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; + +typedef struct// 鍥剧殑瀹氫箟 +{ + AdjMatrix arcs; // 寮х殑淇℃伅 + int vexnum, arcnum; // 椤剁偣鏁帮紝寮ф暟 +} Graph; + +typedef struct QNode +{ + int data; + struct QNode *priou; + struct QNode *next; +}QNode, LinkList, *QueuePtr; + +typedef struct +{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +//闃熷垪鍩烘湰鎿嶄綔 + +int InitQueue(LinkQueue *Q)//鍒濆鍖栭槦鍒 +{ + Q->front = Q->rear = (QueuePtr)malloc(MAXQSIZE * sizeof(QNode));//鍒嗛厤闃熷垪绌洪棿 + if (!(Q->front)) + { + return 0; + } + Q->front->next = Q->rear->next = NULL; + return OK; +} + +int EnQueue(LinkQueue *Q, int e)//鍏ラ槦鍒 +{ + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) + { + return 0; + } + p->data = e; + p->next = NULL; + p->priou = Q->front; + Q->rear->next = p; + Q->rear = p; + return OK; +} + +int QueueEmpty(LinkQueue Q)//鍒ゆ柇闃熷垪鏄惁涓虹┖ +{ + if (Q.front == Q.rear) + { + return 1; + } + return 0; +}; + +int DeQueue(LinkQueue *Q, int *e)//鍑洪槦鍒 +{ + if (QueueEmpty(*Q)) //闃熷垪涓虹┖ + { + return 0; + } + Q->front = Q->front->next; + *e = Q->front->data; + return OK; +} + +//鍥剧殑鎿嶄綔 + +int FirstAdjVex(Graph G, int i)//杩斿洖绗竴涓偦鎺ラ《鐐癸紝鑻ユ棤鍒欒繑鍥-1 +{ + int k; + for (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 +{ + int k; + for (k = j + 1; k < G.vexnum; k++) + { + if (G.arcs[i][k].adj == 1) + { + return k; + } + } + return -1; +} + +int Add(Graph*G, int x, int y) ////鏋勯犻偦鎺ョ煩闃靛苟璧嬪 +{ + if (x >= MAX_VERTEX_NUM || y >= MAX_VERTEX_NUM) + { + return 0; + } + G->arcs[x][y].adj = G->arcs[y][x].adj = 1;//鐩搁偦鍒欒祴鍊间负1 + return OK; +} + +int CreateGraph(Graph *G) //鏋勫缓宸茬煡鍥 +{ + int i, j; + G->vexnum = 9; //椤剁偣 + G->arcnum = 12; //杈 + for (i = 0; i < G->vexnum; i++) + { + for (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); + return OK; +} + +void PrintFoot(LinkQueue Q, int start)//杈撳嚭椤剁偣闂存渶鐭矾寰 +{ + int i; + int foot[MAX_VERTEX_NUM]; + QueuePtr p; + p = Q.rear; + 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); + } + } +} + +void BFSTraverse(Graph G, int a, int b) //骞垮害浼樺厛閬嶅巻 +{ + LinkQueue Q; //杈呭姪闃熷垪Q + int i = 0; + int flag = 0; + + for (int j = 0; j < G.vexnum; ++j) + { + visited[j] = 0; //鍒濆鍖栬闂爣蹇 + } + InitQueue(&Q); // 鍒濆鍖栬緟鍔╅槦鍒桻 + EnQueue(&Q, a); //璧风偣鍏ラ槦鍒 + visited[a] = 1; + while (!QueueEmpty(Q)) //闃熷垪涓嶄负绌 + { + DeQueue(&Q, &i); + for (int k = FirstAdjVex(G, i); k >= 0; k = NextAdjVex(G, i, k))//鐩搁偦椤剁偣鍏ラ槦鍒 + { + if (k == b) + { + EnQueue(&Q, k); + PrintFoot(Q, a); //杈撳嚭椤剁偣闂存渶鐭矾寰 + flag = 1; + break; + } + if (!visited[k]) + { + EnQueue(&Q, k); + visited[k] = 1; + } + } + if (flag == 1) + { + break; + } + } +} + +void main() +{ + int i, j; + Graph G; + CreateGraph(&G); + printf("鍚勯《鐐归棿鏈鐭矾寰勪緷娆′负:\n\n"); + for (i = 0; i < 9; i++) + { + for (j = 0; j < 9; j++) + { + if (j != i) + { + printf("%d<->%d:", i + 1, j + 1); + BFSTraverse(G, i, j); + printf("\n"); + } + } + } +} \ No newline at end of file diff --git "a/2017-1/Orange33/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\345\233\276\347\232\204\351\241\266\347\202\271\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204.png" "b/2017-1/Orange33/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\345\233\276\347\232\204\351\241\266\347\202\271\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204.png" new file mode 100644 index 00000000..20c72058 Binary files /dev/null and "b/2017-1/Orange33/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\345\233\276\347\232\204\351\241\266\347\202\271\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204.png" differ diff --git "a/2017-1/Orange33/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/QQ\346\210\252\345\233\27620170423214246.png" "b/2017-1/Orange33/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/QQ\346\210\252\345\233\27620170423214246.png" new file mode 100644 index 00000000..fcd47e14 Binary files /dev/null and "b/2017-1/Orange33/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/QQ\346\210\252\345\233\27620170423214246.png" differ diff --git "a/2017-1/Orange33/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/calculate.c" "b/2017-1/Orange33/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/calculate.c" new file mode 100644 index 00000000..016db62c --- /dev/null +++ "b/2017-1/Orange33/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/calculate.c" @@ -0,0 +1,246 @@ +锘#include +#include + +#define STACK_INIT_SIZE 100 //瀛樺偍绌洪棿鍒濆鍒嗛厤閲 +#define STACKINCREMENT 10 //瀛樺偍绌洪棿鍒嗛厤澧為噺 + +typedef char SElemType; +#define OK 1 +#define ERROR 0 +#define OVERFLOW -2 + +typedef struct SqStack +{ + SElemType *base; + SElemType *top; + int stacksize; +} SqStack; + +int InitStack(SqStack *stack)//鏋勯犵┖鏍 +{ + + stack->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(int)); + if (!stack->base) // 鍒嗛厤瀛樺偍绌洪棿澶辫触 + { + return OVERFLOW; + } + stack->stacksize = STACK_INIT_SIZE; + stack->top = stack->base; + return OK; +} + +void DestroyStack(SqStack *stack) //姣佹爤 +{ + //stack = NULL; +} + +int StackEmpty(SqStack *stack) //鍒ゆ柇鏍堟槸鍚︿负绌 +{ + if (stack->base == stack->top) + { + return OK; + } + else + { + return ERROR; + } +} + + +int Push(SqStack *stack, SElemType e) //鍘嬫爤 +{ + if (stack->top - stack->base > stack->stacksize) + { + stack->base = (SElemType *)realloc(stack->base, + (stack->stacksize + STACKINCREMENT) * sizeof(SElemType *)); + if (!stack->base) + { + return OVERFLOW; + } + stack->top = stack->base + stack->stacksize; + stack->stacksize = stack->stacksize + STACKINCREMENT; + } + *stack->top = e; + stack->top++; + return OK; +} + + +int Pop(SqStack *stack, SElemType *e) +{ + if (StackEmpty(stack)) + { + return ERROR; + } + stack->top--; + *e = *stack->top; + return OK; +} + + +SElemType GetTop(SqStack stack) //杩斿洖鏍堥《鍏冪礌 +{ + if (!StackEmpty(&stack)) + { + stack.top--; + } + return *stack.top; +} + + +int IsOperator(SElemType e) //鍒ゆ柇璇诲叆瀛楃鏄惁涓鸿繍绠楃 +{ + if (e == '+'||e == '-'||e == '*'||e == '/'||e == '('||e == ')'||e == '#') + { + return 1; + } + else + { + return 0; + } +} + + +SElemType Precede(SElemType a, SElemType b)//姣旇緝杩愮畻绗︿紭鍏堢骇 +{ + SElemType ch; + switch (a) + { + case '+': + case '-': + { + if (b == '+' || b == '-' || b == ')' || b == '#') + { + ch = '>'; + } + else if (b == '*' || b == '/' || b == '(') + { + ch = '<'; + } + } + break; + case '*': + case '/': + { + if (b == '+' || b == '-' || b == '*' || b == '/' || b == ')' || b == '#') + { + ch = '>'; + } + else if (b == '(') + { + ch = '<'; + } + } + break; + + case '(': + { + if (b == '+' || b == '-' || b == '*' || b == '/' || b == '(') + { + ch = '<'; + } + else if (b == ')') + { + ch = '='; + } + } + break; + + case ')': + { + if (b == '+' || b == '-' || b == '*' || b == '/' || b == ')' || b == '#') + { + ch = '>'; + } + } + break; + + case '#': + { + if (b == '+' || b == '-' || b == '*' || b == '/' || b == '(') + { + ch = '<'; + } + else if (b == '#') + { + ch = '='; + } + } + break; + } + return ch; +} + + +SElemType Operate(SElemType a, SElemType Operator, SElemType b)//杩愮畻 +{ + SElemType ch; + a = a - 48; // 杞寲鎴恑nt绫 + b = b - 48; //杞寲鎴恑nt绫 + switch(Operator) + { + case '+': + ch = a + b + 48; + break; + case '-': + ch = a - b + 48; + break; + case '*': + ch = a * b + 48; + break; + case '/': + ch = a / b + 48; + break; + } + return ch; +} + + +int calculate() +{ + SqStack Operator,Oprand; + SElemType ch,a,b,theta,x; + InitStack(&Operator); //鍒濆鍖栨搷浣滅鏍 + InitStack(&Oprand); //鍒濆鍖栨搷浣滄暟鏍 + Push(&Operator,'#'); + ch=getchar(); + while(ch!='#'||GetTop(Operator)!='#') + { + if(!IsOperator(ch)) + { + Push(&Oprand,ch); + ch=getchar(); + } + else + { + switch(Precede(GetTop(Operator),ch)) + { + case '<': + Push(&Operator,ch); + ch=getchar(); + + break; + case '>': + Pop(&Operator,&theta); + Pop(&Oprand,&b); + Pop(&Oprand,&a); + Push(&Oprand,Operate(a,theta,b)); + break; + case '=': + Pop(&Operator,&x); + ch=getchar(); + break; + } + } + + } + + return GetTop(Oprand)-'0'; +} + + +void main() +{ + printf("璇疯緭鍏ョ畻鏈〃杈惧紡,杈撳叆#缁撴潫\n"); + printf("缁撴灉鏄: %d\n",calculate()); +} \ No newline at end of file diff --git "a/2017-1/Orange33/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/queue.c" "b/2017-1/Orange33/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/queue.c" new file mode 100644 index 00000000..3f9dfbbe --- /dev/null +++ "b/2017-1/Orange33/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/queue.c" @@ -0,0 +1,188 @@ +锘#include +#include +#include +#define QElemType int + +typedef struct QNode +{ + QElemType data; + struct QNode *next; +}QNode, *QueuePtr; +typedef struct +{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +Status InitQueue(LinkQueue *Q) //鏋勯犵┖闃熷垪 +{ + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!Q->front) + { + return ERROR; + } + Q->front->next = NULL; + return OK; +} +Status DestroyQueue(LinkQueue *Q) //閿姣侀槦鍒 +{ + while (Q->front) + { + + Q->rear = Q->front->next; + free(Q->front); + Q->front = Q->rear; + } + return OK; +} +Status EnQueue(LinkQueue *Q, QElemType e) // 鎻掑叆鍏冪礌e涓篞鐨勬柊鐨勯槦灏惧厓绱 +{ + QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) + { + return ERROR; + } + else + { + p->data = e; + p->next = NULL; + Q->rear->next = p; + Q->rear = p; + } + return OK; +} + +Status DeQueue(LinkQueue *Q, QElemType *e) // 鑻ラ槦鍒椾笉绌猴紝鍒欏垹闄鐨勯槦澶村厓绱 +{ + QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); + if (Q->front == Q->rear) + { + return ERROR; + } + p = Q->front->next; + + *e = p->data; + Q->front->next = p->next; + if (Q->rear == p) + { + Q->rear = Q->front; + } + free(p); + return OK; +} + +int QueueEmpty(LinkQueue*Q) //鍒ゆ柇闃熷垪鏄惁涓虹┖ +{ + if (Q->front == Q->rear) + { + return 1; + } + else + { + return 0; + } +} +Status ClearQueue(LinkQueue*Q) //娓呯┖闃熷垪 +{ + QElemType e; + if (QueueEmpty(Q)) + { + return ERROR; + } + + while (!QueueEmpty(Q)) + { + DeQueue(Q,&e); + } + return OK; +} +int QueueLength(LinkQueue Q) //姹傞槦鍒楅暱搴 +{ + int i = 0; + while (!QueueEmpty(&Q)) + { + Q.front = Q.front->next; + i++; + } + return i; +} +Status GetHead(LinkQueue Q, QElemType *e) //杩斿洖闃熷垪澶村厓绱 +{ + if (QueueEmpty(&Q) == 1) + { + return ERROR; + } + else + { + QNode *p = Q.front->next; + *e = p->data; + return OK; + } +} + +Status QueueTraverse (LinkQueue *Q )//閬嶅巻闃熷垪 +{ + QueuePtr p = Q->front->next; + if (Q->front == Q->rear) + { + printf("闃熷垪涓虹┖!\n"); + return ERROR; + } + else + { + while (p) + { + printf("%d ", p->data); + p = p->next; + } + printf("\n"); + return OK; + } +} + +int main() +{ + int n, n1; + int i; + QElemType e = 0; + LinkQueue q; + InitQueue(&q); + srand(time(0)); + n = rand() % 9 + 1; //鐢熸垚闅忔満鏁 + for (i = 0; i < n; i++) + { + n1 = rand() % 9 + 1; + EnQueue(&q, n1); + } + printf("闅忔満鐢熸垚闃熷垪q:\n"); + QueueTraverse(&q); + printf("\n"); + printf("闃熷垪闀垮害(鍔犱笂澶寸粨鐐)锛歕n"); + printf("%d", QueueLength(q)); + printf("\n闃熷垪q闃熷ご鍏冪礌涓猴細\n"); + GetHead(q, &e); + printf("%d\n", e); + printf("鍒犻櫎闃熷ご鍏冪礌锛屾柊闃熷垪涓:\n"); + DeQueue(&q, &e); + QueueTraverse(&q); + printf("\n"); + if (!QueueEmpty(&q) ) + { + printf("鐩墠闃熷垪涓嶄负绌篭n"); + } + printf("娓呯┖闃熷垪\n"); + { + ClearQueue(&q); + } + if (QueueEmpty(&q)) + { + printf("鐩墠闃熷垪涓虹┖\n"); + } +} \ No newline at end of file diff --git "a/2017-1/Orange33/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/QQ\346\210\252\345\233\27620170423205620.png" "b/2017-1/Orange33/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/QQ\346\210\252\345\233\27620170423205620.png" new file mode 100644 index 00000000..dd2cecd8 Binary files /dev/null and "b/2017-1/Orange33/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/QQ\346\210\252\345\233\27620170423205620.png" differ diff --git "a/2017-1/Orange33/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/homework.c" "b/2017-1/Orange33/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/homework.c" new file mode 100644 index 00000000..a950e280 --- /dev/null +++ "b/2017-1/Orange33/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/homework.c" @@ -0,0 +1,45 @@ +锘#include +#include + +#define TElemType char +#define ElemType BiTNode * // 鏍戠殑缁撶偣 + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild,*rchild; +}BiTNode; + +BiTNode *CreatBiTree(char z[]) +{ + static int i=-1; + BiTNode *root; + i++; + if(z[i]=='*') return NULL; + root=(BiTNode *)malloc(sizeof(BiTNode)); + root->data=z[i]; + root->lchild=CreatBiTree(z); //鏋勯犲乏瀛愭爲 + root->rchild=CreatBiTree(z); //鏋勯犲彸瀛愭爲 + return(root); +} + +void Postorder(struct BiTNode* BT) //鍚庡簭閬嶅巻 +{ + if (BT != NULL) + { + Postorder(BT->lchild); + Postorder(BT->rchild); + printf("%c,", BT->data); + } +} + +void main() +{ + char z[]="ABDG***EH**I*K**C*F**"; //娴嬭瘯鐢ㄤ緥 + BiTNode *T; + T=(BiTNode *)malloc(sizeof(BiTNode)); + T=CreatBiTree(z); + printf("娴嬭瘯鐢ㄤ緥锛欰BDG***EH**I*K**C*F** 鍚庡簭閬嶅巻鐨勯『搴忎负锛歕n"); + Postorder(T); + +} \ No newline at end of file diff --git "a/2017-1/Orange33/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTOutput.txt" "b/2017-1/Orange33/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTOutput.txt" new file mode 100644 index 00000000..afe06913 --- /dev/null +++ "b/2017-1/Orange33/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTOutput.txt" @@ -0,0 +1,6 @@ +8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30, +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30, +3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30, +3, 1, 6, 4, 7, 10, 14, 13, 19, 22, 30, +3, 1, 6, 4, 7, 10, 14, 13, 19, 22, 20, 30, +3, 1, 4, 7, 10, 14, 13, 19, 22, 20, 30, \ No newline at end of file diff --git "a/2017-1/Orange33/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTtraverse.c" "b/2017-1/Orange33/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTtraverse.c" new file mode 100644 index 00000000..f422fe3d --- /dev/null +++ "b/2017-1/Orange33/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTtraverse.c" @@ -0,0 +1,112 @@ +锘#include +#include +#include "BSTtraverse.h" + +BSTree InsertBST(BSTree T, int data) //鎻掑叆缁撶偣 +{ + BSTree t; + if (!T) + { + t = (BSTree)malloc(sizeof(BSTNode));//鍒嗛厤绌洪棿 + t->data = data; + t->lchild = NULL; + t->rchild = NULL; + return t; + } + else if (T->data < data) + { + T->rchild = InsertBST(T->rchild, data); + } + else if (T->data > data) + { + T->lchild = InsertBST(T->lchild, data); + } + return T; +} + +BSTree CreatBST(BSTree T, int array[])//寤虹珛浜屽弶鏍 +{ + for (int i = 0; i < 12; i++) + { + T = InsertBST(T, array[i]); //閫愪釜缁撶偣鎻掑叆 + } + return T; +} + +bool SearchBST(BSTree T, int key) //鏌ユ壘浜屽弶鏍戜腑鏈夋棤鐩爣缁撶偣 +{ + if (!T) //鏃犳缁撶偣锛岃繑鍥濬ALSE + { + return false; + } + else if (T->data == key) //鏈夋缁撶偣 + { + /*printf("%d\n", T->data);*/ + return true; + } + else if (T->data > key) + { + return SearchBST(T->lchild, key); + } + else if (T->data < key) + { + return SearchBST(T->rchild, key); + } + return true; +} + +void PrePrintBST(BSTree T) //鍏堝簭閬嶅巻杈撳嚭浜屽弶鏍 +{ + if (T) + { + printf("%d, ", T->data); + PrePrintBST(T->lchild); + PrePrintBST(T->rchild); + } +} + +bool DeleteBST(BSTree *T, int key) //鍒犻櫎缁撶偣 +{ + if (!*T) //濡傛灉娌℃湁姝ょ粨鐐硅繑鍥濬ALSE銆 + return false; + else if (key == (*T)->data)//鍦ㄤ互*T涓烘牴缁撶偣鐨勬爲涓紝鍒犻櫎涓巏ey鐩稿悓鐨勭粨鐐 + { + Delete(T); + return true; + } + else if (key < (*T)->data) + { + return DeleteBST(&((*T)->lchild), key); + } + else + { + return DeleteBST(&((*T)->rchild), key); + } +} + +bool Delete(BSTree *T) //鍒犻櫎*T鎸囧悜鐨勭粨鐐 +{ + BSTree L; + + if (!(*T)->lchild && !(*T)->rchild) //鏃犲乏鍙冲瀛 + *T = NULL; + + else if (!(*T)->lchild)//鍙湁鍙冲瀛 + *T = (*T)->rchild; + + else if (!(*T)->rchild)//鍙湁宸﹀瀛 + *T = (*T)->lchild; + + else//鏃㈡湁宸﹀瀛愶紝鍙堟湁鍙冲瀛 + { + L = (*T)->lchild; //鎸囧悜琚垹闄ょ粨鐐圭殑宸﹀瓙鏍 + while (L->rchild) //瀵绘壘L鐨勬渶鍙冲瀛 + { + L = L->rchild; + } + L->rchild = (*T)->rchild; //鎶*T鐨勫彸瀛愭爲鎺ュ埌宸﹀瓙鏍戞渶鍙冲瀛愮殑鍙冲瓙鏍戜笂 + + *T = (*T)->lchild; //鎶*T鐨勫乏瀛愭爲浣滀负*T鐖剁粨鐐圭殑瀛愭爲 + } + return true; +} \ No newline at end of file diff --git "a/2017-1/Orange33/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTtraverse.h" "b/2017-1/Orange33/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTtraverse.h" new file mode 100644 index 00000000..6577da73 --- /dev/null +++ "b/2017-1/Orange33/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTtraverse.h" @@ -0,0 +1,20 @@ +#pragma once +#include +#include + +typedef struct BSTNode { + int data; + struct BSTNode *lchild, *rchild; +}BSTNode, *BSTree; + +typedef enum { + false, + true +}bool; + +BSTree InsertBST(BSTree T, int data); //插入结点 +BSTree CreatBST(BSTree T, int array[]); //建立二叉树 +bool SearchBST(BSTree T, int key); //查找二叉树中有无目标结点 +void PrePrintBST(BSTree T); //先序遍历输出二叉树 +bool DeleteBST(BSTree *T, int key); //删除与key相同的结点 +bool Delete(BSTree *T); //删除*T指向的结点 diff --git "a/2017-1/Orange33/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/main.c" "b/2017-1/Orange33/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/main.c" new file mode 100644 index 00000000..6ccf308f --- /dev/null +++ "b/2017-1/Orange33/\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/main.c" @@ -0,0 +1,35 @@ +#include +#include +#include "BSTtraverse.h" + +int main() +{ + BSTree T; + T = NULL; + bool n; + int array[] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + int find[] = { 13, 8, 5, 20, 6 }; + T = CreatBST(T, array); + PrePrintBST(T); //打印当前查找表 + printf("\n"); + for (int i = 0; i < 5; i++) + { + n = SearchBST(T, find[i]); + if (!n) //未找到目标节点 + { + T = InsertBST(T, find[i]);//插入该结点 + /*printf("%d\n", find[i]);*/ + PrePrintBST(T); + printf("\n"); + } + else //找到目标节点 + { + if (DeleteBST(&T, find[i]))//删除该结点 + { + PrePrintBST(T); + } + printf("\n"); + } + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/Orange33/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/test3.c" "b/2017-1/Orange33/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/test3.c" new file mode 100644 index 00000000..4dc37758 --- /dev/null +++ "b/2017-1/Orange33/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/test3.c" @@ -0,0 +1,85 @@ +#include +#include + +#define TElemType char +#define ElemType BiTNode * // 树的结点 + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild, *rchild; +}BiTNode; + +BiTNode *CreatBiTree(char z[]) +{ + static int i = -1; + BiTNode *root; + i++; + if (z[i] == '*') + { + return NULL; + } + root = (BiTNode *)malloc(sizeof(BiTNode)); + root->data = z[i]; + root->lchild = CreatBiTree(z); //构造左子树 + root->rchild = CreatBiTree(z); //构造右子树 + return(root); +} + +int Depth(BiTNode* BT)//计算深度 +{ + int dep1, dep2; + if (!BT) + { + return 0; + } + else + { + dep1 = Depth(BT->lchild); + dep2 = Depth(BT->rchild); + if (dep1 > dep2) + { + return dep1 + 1; + } + else + { + return dep2 + 1; + } + } +} + +int Width(BiTNode* BT) //计算最大宽度 +{ + int count[50]; + int max = 0; + int i = 0; + if (!BT) + { + return 0; + } + i++; + count[i]++; + if (max < count[i]) + { + max = count[i]; + } + Width(BT->lchild); + Width(BT->rchild); + i--; //返回根结点 + return max; +} + +void main() +{ + char z[] = "ABDG***EH**I*K**C*F**"; //测试用例 + BiTNode *T; + T = (BiTNode *)malloc(sizeof(BiTNode)); + T = CreatBiTree(z); + + printf("测试用例:ABDG***EH**I*K**C*F** 的深度为:%d\n", Depth(T)); + printf("\n"); + printf("测试用例:ABDG***EH**I*K**C*F** 的最大宽度为:%d\n", Width(T)); + printf("\n"); +} + + diff --git "a/2017-1/Orange33/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/test5.c" "b/2017-1/Orange33/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/test5.c" new file mode 100644 index 00000000..fd3a6419 --- /dev/null +++ "b/2017-1/Orange33/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/test5.c" @@ -0,0 +1,73 @@ +#include +#include + +#define TElemType char +#define ElemType BiTNode * // 树的结点 + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild, *rchild; +}BiTNode; + +BiTNode *CreatBiTree(char z[]) +{ + static int i = -1; + BiTNode *root; + i++; + if (z[i] == '*') return NULL; + root = (BiTNode *)malloc(sizeof(BiTNode)); + root->data = z[i]; + root->lchild = CreatBiTree(z); //构造左子树 + root->rchild = CreatBiTree(z); //构造右子树 + return(root); +} + +int CountLeaf(BiTNode *T) // 求叶子结点个数 +{ + int m = 0, n = 0; + if (!T) + { + return 0; + } + if (!T->lchild && !T->rchild) + { + return 1; + } + else + { + m = CountLeaf(T->lchild); + n = CountLeaf(T->rchild); + return m + n; + } +} + +int Count(BiTNode *T) //求所有结点个数 +{ + int m, n; + if (!T) return 0; + if (!T->lchild && !T->rchild) + { + return 1; + } + else + { + m = Count(T->lchild); + n = Count(T->rchild); + return m + n + 1; + } +} + +void main() +{ + char z[] = "ABDG***EH**I*K**C*F**"; //测试用例 + BiTNode *T; + T = (BiTNode *)malloc(sizeof(BiTNode)); + T = CreatBiTree(z); + + printf("测试用例:ABDG***EH**I*K**C*F** 的叶子节点数为:%d\n\n",CountLeaf(T)); + printf("测试用例:ABDG***EH**I*K**C*F** 的非叶子节点数为:%d\n", Count(T)- CountLeaf(T)); + printf("\n"); +} + + diff --git "a/2017-1/Orange33/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/hw4.c" "b/2017-1/Orange33/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/hw4.c" new file mode 100644 index 00000000..1445fd67 --- /dev/null +++ "b/2017-1/Orange33/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/hw4.c" @@ -0,0 +1,109 @@ +#include +#include +#include + +typedef int ElemType; + +typedef struct LNode +{ + ElemType data; + struct LNode *next; + +}LNode, *LinkList; + +void CreateList(LinkList L, int n) //创建线性链表 +{ + int i; + LNode* p = L; + L->next = NULL; + srand(time(NULL)); + for (i = n; i >0 ; --i) + { + p = (LNode*)malloc(sizeof(LNode)); //分配空间 + p->data = rand() % 50+1; //随机生成数据 + p->next = L->next; //插入到表头 + L->next = p; + } +}; + +void TraverseList_L1(LinkList L) //遍历输出单链表 +{ + LNode *p = L->next; + while (p) + { + printf("%d ", p->data); + p = p->next; + } +} +void TraverseList_L2(LinkList L) //遍历输出循环链表 +{ + LNode *p = L->next; + while (p) + { + printf("%d ", p->data); + p = p->next; + if (p == L->next) + { + break; + } + } +} + +void change(LinkList list,LinkList *list1, LinkList *list2)//拆分原线性链表为两个循环链表 +{ + LNode*p, *p1, *p2; //用于遍历list,list1,list2的三个指向节点的指针 + LNode*rear1, *rear2; //尾指针 + int count = 0; + *list1 = (LNode*)malloc(sizeof(LNode)); + (*list1)->data = 0; + (*list1)->next = NULL; + rear1 = p1 = *list1; + *list2 = (LNode*)malloc(sizeof(LNode)); //分配存储空间 + (*list2)->data = 0; + (*list2)->next = NULL; + rear2 = p2 = *list2; + for (p = list->next; p != NULL; p = p->next) + { + count++; + if (count % 2) //控制奇数项 + { + rear1->next = p; + rear1 = p; + (*list1)->data++; //长度增加1 + printf("\n元素:%d,序号为奇数,分配给list1 ", rear1->data); + } + else + { + rear2->next = p; + rear2 = p; + (*list2)->data++; //长度增加1 + printf("\n元素:%d,序号为偶数,分配给list2 ", rear2->data); + } + } + rear1->next = p1->next; + rear2->next = p2->next; +} + +int main() +{ + int n; + LinkList L, La, Lb; + srand(time(0)); + n = rand() % 20 + 1 ;//随机生成链表元素 + L = (LinkList)malloc(sizeof(LNode)); + CreateList(L, n); //创建L + printf("L:"); //输出L + TraverseList_L1(L); + printf("\n"); + change(L, &La, &Lb); + printf("\n\n拆分结果:\n\n");//La,Lb是循环链表,通过函数实现只输出一次 + printf("序号为奇数的元素放入La\n"); + printf("La元素个数:%d\nLa:", La->data); + TraverseList_L2(La); + printf("\n序号为偶数的元素放入Lb\n"); + printf("Lb元素个数:%d\nLb:", Lb->data); + TraverseList_L2(Lb); + return 0; +} + +//查找算法时间复杂度都是O(1),所用时间最少,空间最小 \ No newline at end of file diff --git "a/2017-1/Orange33/\347\256\227\346\263\2252-12\344\277\256\346\224\271.c" "b/2017-1/Orange33/\347\256\227\346\263\2252-12\344\277\256\346\224\271.c" new file mode 100644 index 00000000..dc7979f2 --- /dev/null +++ "b/2017-1/Orange33/\347\256\227\346\263\2252-12\344\277\256\346\224\271.c" @@ -0,0 +1,90 @@ +锘#include +#include +#include +typedef int ElemType; + +typedef struct LNode{ + ElemType data; + struct LNode* next; + }LNode,*LinkList; + +void CreateList (LinkList *L,int n) +{ + LNode* p; + int data = 20; + int i; + (*L) = (LinkList)malloc(sizeof(LNode)); + (*L)->next = NULL; + srand((unsigned int)time(NULL)); + for(i=0;idata=data-=rand()%5+1; + printf("%d ",p->data); + p->next = (*L)->next; + (*L)->next=p; + } + printf("\n"); +} + +void MergeList(LinkList *La,LinkList *Lb,LinkList *Lc)/*聽绠楁硶2.12聽*/{ + LinkList pa=(*La)->next,pb=(*Lb)->next,pc; + *Lc=pc=*La;/*鐢↙a鐨勫ご缁撶偣浣滀负Lc鐨勫ご缁撶偣*/ + while(pa&&pb) + if(pa->data<=pb->data){ + pc->next=pa; + pc=pa; + pa=pa->next; + } + else{ + pc->next=pb; + pc=pb; + pb=pb->next; + } + pc->next=pa?pa:pb;/*鎻掑叆鍓╀綑娈*/ + free(*Lb);/*聽閲婃斁Lb鐨勫ご缁撶偣*/ + Lb=NULL; +} +void ListTraverse(const LinkList L, void(*func)(ElemType)){ + LNode *p = L->next; + while (p){ + func(p->data); + p = p->next; + } +} +void visit(ElemType c)/*ListTraverse()璋冪敤鐨勫嚱鏁*/{ + printf("%d ",c); +} + +void PrintList (LinkList Lc ) +{ + LNode *p; + p = Lc->next; + printf("杈撳嚭鍚堝苟閾捐〃Lc涓悇缁撶偣鏁版嵁锛歕n"); + while( p!=NULL ) + { + printf("%d ",p->data) ; + p=p->next; + } + printf("\n"); + +} + + +int main() +{ + LinkList La,Lb,Lc; + int a,b; + srand((unsigned int)time(NULL)); + a=rand()%10+5; + printf("La鐨勯暱搴︿负%d\n",a); + b=rand()%10+5; + printf("Lb鐨勯暱搴︿负%d\n",b); + printf("La涓墍鏈夊间负\n"); + CreateList( &La ,a); + printf("Lb涓墍鏈夊间负\n"); + CreateList( &Lb ,b); + MergeList( &La, &Lb ,&Lc); + PrintList( Lc); + return 0; +} \ No newline at end of file diff --git "a/2017-1/Orange33/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217.cpp" "b/2017-1/Orange33/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217.cpp" new file mode 100644 index 00000000..67c72956 --- /dev/null +++ "b/2017-1/Orange33/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217.cpp" @@ -0,0 +1,113 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100//存储空间初始分配 +#define STACKINCREMENT 10//存储空间分配增量 +#define OVERFLOW -2 +#define OK 1 +#define ERROR 0 + +typedef char SElemType; + +typedef struct { + SElemType *base; //在栈构造之前和销毁之后base的值为NULL + SElemType *top;//栈顶指针 + int stacksize; //当前已分配的存储空间,以元素为单位 +}SqStack; + +//构造一个空栈 +int InitStack(SqStack *S) +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));//分配基本空间 + if(!S->base)exit(OVERFLOW); //判断是否分配成功 + S->top = S->base; //指向栈顶 + S->stacksize = STACK_INIT_SIZE; //初始链表最大长度 + return 0; +} + +//插入新的栈顶数据 +int Push(SqStack *S,SElemType e) { + if((S->top-S->base)>=S->stacksize) { + S->base = (SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); + if(!S->base) { + exit(OVERFLOW); + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return 0; +} + +//若栈不空 则删除栈顶数据并用e返回且返回OK 否则返回ERROR +int Pop(SqStack *S,SElemType *e) { + if(S->top == S->base) return ERROR; + *e = *--S->top; + return 0; +} + +//清空栈中的元素 +int ClearStack(SqStack *S) { + S->top = S->base; + return OK; +} + +//销毁栈 +int DestoryStack(SqStack *S) { + S->top = S->base; + free(S->base); + S->top = NULL; + S->base = NULL; + return OK; +} + +void LineEdit() +{ + //利用字符栈S,从终端接收一行并传送至调用过程的数据区。 + char ch,*temp; + SqStack S; + InitStack(&S); //构造空栈S + printf("请输入一行(#:退格;@:清行)(输入Ctrl+Z的时候结束循环):\n"); + ch = getchar(); //从终端接收第一个字符 + while (ch != EOF) //EOF为全文结束符) + { + while (ch != EOF && ch != '\n') + { + switch (ch) + { + case '#': + Pop(&S, &ch); + break; // 仅当栈非空时退栈 + case '@': + ClearStack(&S); + break; // 重置S为空栈 + default : + Push(&S, ch); + break; // 有效字符进栈,未考虑栈满情形 + } + ch = getchar(); // 从终端接收下一个字符 + } + // 将从栈底到栈顶的栈内字符传送至调用过程的数据区; + temp=S.base; + while(temp!=S.top) + { + printf("%c",*temp); //这里做控制台的输出;依情况讨论 + ++temp; + } + ClearStack(&S); // 重置S为空栈 + printf("\n"); + if (ch != EOF) + { + printf("请输入一行(#:退格;@:清行)(输入Ctrl+Z的时候结束循环):\n"); + ch = getchar(); + } + } + DestoryStack(&S); +} + +int main() +{ + LineEdit(); + return 0; +} diff --git a/2017-1/PWHL/Algorithm .c b/2017-1/PWHL/Algorithm .c new file mode 100644 index 00000000..ccfef461 --- /dev/null +++ b/2017-1/PWHL/Algorithm .c @@ -0,0 +1,320 @@ +锘#include "Algorithm.h" +int main() +{ + MazeType maze;//杩峰缁撴瀯 + SqStack stack;//椤哄簭鏍堬紝瀛樺偍杩峰璺緞 + PosType start, end;//杩峰鐨勮捣鐐瑰拰缁堢偣锛 + start.x = 0; start.y = 1;//杩峰鐨勮捣鐐 + end.x = 8; end.y = 9;//杩峰鐨勭粓鐐 + InitMaze(&maze);//杩峰鍒濆鍖 + printf("杩峰褰㈢姸锛歕n"); + PrintMaze(&maze);//鎵撳嵃杩峰褰㈢姸 + if (TRUE == MazePath(&stack, maze, start, end))//鏍堜腑鏈夊彲璧扮殑璺緞,鍒欒緭鍑鸿糠瀹彲瑙 + printf("杩峰鍙В.\n"); + else + printf("杩峰涓嶅彲瑙.\n"); + return 0; +} +//鏋勯犱竴涓┖鏍圫 +Status InitStack(SqStack *S) +{ + //缁欐爤鍒嗛厤瀛樺偍绌洪棿 + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!S->base)//濡傛灉鍒嗛厤澶辫触 + { + printf("鍒嗛厤鍐呭瓨澶辫触.\n"); + exit(0); + } + S->top = S->base;//灏嗘爤椤舵寚閽堟寚鍚戞爤搴曟寚閽 + S->stacksize = STACK_INIT_SIZE;//灏嗘爤鐨勫ぇ灏忓垵濮嬪寲涓100 + return OK; +} +//鍒濆鍖栬糠瀹暟鎹 +Status InitMaze(MazeType *M) +{ + int i, j; + //鑷繁瀹氫箟杩峰褰㈢姸锛 + char mz[ROW][COLUMN] = { + '#',' ','#','#','#','#','#','#','#','#', + '#',' ',' ','#',' ',' ',' ','#',' ','#', + '#',' ',' ','#',' ',' ',' ','#',' ','#', + '#',' ',' ',' ',' ','#','#',' ',' ','#', + '#',' ','#','#','#',' ',' ',' ',' ','#', + '#',' ',' ',' ','#',' ','#',' ','#','#', + '#',' ','#',' ',' ',' ','#',' ',' ','#', + '#',' ','#','#','#',' ','#','#',' ','#', + '#','#',' ',' ',' ',' ',' ',' ',' ',' ', + '#','#','#','#','#','#','#','#','#','#' + }; + + M->maze = (char **)malloc(sizeof(char *)*ROW);//缁欒糠瀹垎閰嶅瓨鍌ㄧ┖闂 + M->footprint = (int **)malloc(sizeof(int *)*ROW);//缁欒褰曡蛋杩囩殑璺緞鍒嗛厤瀛樺偍绌洪棿 + if (!M->maze || !M->footprint) + { + printf("鐢宠绌洪棿澶辫触锛岃糠瀹棤娉曞垵濮嬪寲.\n"); + return ERROR; + exit(0); + } + for (i = 0; imaze[i] = (char *)malloc(sizeof(char)*COLUMN); + M->footprint[i] = (int *)malloc(sizeof(int)*COLUMN); + if (!M->maze[i] || !M->footprint[i]) + { + printf("鐢宠绌洪棿澶辫触锛岃糠瀹垵濮嬪寲澶辫触.\n"); + return ERROR; + exit(0); + } + } + for (i = 0; imaze[i][j] = mz[i][j];//灏嗚糠瀹玀鍒濆鍖栦负鑷繁瀹氫箟鐨勮糠瀹舰鐘 + M->footprint[i][j] = 0;//灏嗚冻杩瑰叏閮ㄨ涓0 + } + } + M->row = ROW;//琛 + M->column = COLUMN;//鍒 + return OK; +} + +//閿姣佹爤S锛孲涓嶅啀瀛樺湪 +Status DestroyStack(SqStack *S) +{ + if (!S)//S涓虹┖ + { + printf("鎸囬拡涓虹┖锛岄噴鏀惧け璐.\n"); + exit(0);//姝e父閫鍑虹▼搴 + } + free(S); + return OK; +} + +Status ClearStack(SqStack *S) +{ + //鎶婃爤S缃负绌烘爤 + if (!S)//S涓嶅瓨鍦 + return FALSE; + S->top = S->base;//鐩存帴灏嗘爤椤舵寚閽堟寚鍚戞爤搴 + return OK; +} + +Status StackEmpty(SqStack S) +{ + //鑻ユ爤S涓虹┖鏍堬紝鍒欒繑鍥濼RUE锛屽惁鍒欒繑鍥濬ALSE + if (S.top == S.base) + return TRUE; + else + return FALSE; +} + +//杩斿洖S鍏冪礌鐨勪釜鏁帮紝鍗虫爤鐨勯暱搴 +int StackLength(SqStack S) +{ + return S.stacksize; +} + +//鑻ユ爤涓嶄负绌猴紝鍒欑敤e杩斿洖S鐨勬爤椤跺厓绱狅紝骞惰繑鍥濷K锛涘惁鍒欒繑鍥濬ALSE +Status GetTop(SqStack S, SElemType *e) +{ + if (S.top == S.base) + { + printf("鏍堜负绌.\n"); + return FALSE; + } + else + { + *e = *(S.top - 1);//鑾峰緱鏍堥《鍏冪礌 + printf("鏍堥《鍏冪礌锛%c\n", *e); + return OK; + } +} + +//鎻掑叆鍏冪礌e涓烘柊鐨勬爤椤跺厓绱 +Status Push(SqStack *S, SElemType e) +{ + if (S->top - S->base >= S->stacksize) //鏍堝凡婊★紝杩藉姞瀛樺偍绌洪棿 + { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if (!S->base) + { + printf("閲嶆柊鐢宠绌洪棿澶辫触.\n"); + exit(0); + } + S->top = S->base + S->stacksize;//鏇存敼鏍堥《鎸囬拡 + S->stacksize += STACKINCREMENT;//澧炲姞鏍堢殑闀垮害 + } + *S->top++ = e;//灏唀璁句负鏍堥《鎸囬拡 + return OK; +} + +//鑻ユ爤S涓嶄负绌猴紝鍒欏垹闄鐨勬爤椤跺厓绱狅紝鐢╡杩斿洖鍏跺硷紝骞惰繑鍥濷K,鍚﹀垯杩斿洖ERROR +Status Pop(SqStack *S, SElemType *e) +{ + if (S->top == S->base) //濡傛灉鏍堜负绌 + { + printf("鏍堜负绌.\n"); + return ERROR; + } + *e = *(--S->top); + return OK; +} +Status PrintMaze(MazeType *M) +{ + int i, j; + for (i = 0; irow; i++) + { + for (j = 0; jcolumn; j++) + { + printf("%c", M->maze[i][j]);//杈撳嚭杩峰 + } + printf("\n"); + } + printf("\n"); + return OK; +} + +//杈撳嚭杩峰鐨勮矾寰 +Status PrintFoot(MazeType *M, SqStack *S) +{ + int i, j; + SElemType *p; + for (i = 0; irow; i++) + { + for (j = 0; jcolumn; j++) + { + M->footprint[i][j] = 0;//鍏堝皢璺緞鍏ㄩ儴鍒濆鍖栦负0 + } + } + p = S->base; + if (S->base == S->top) + { + printf("鏍堜负绌.\n"); + return FALSE; + } + while (p != S->top)//鏍堜笉涓虹┖ + { + M->footprint[p->seat.x][p->seat.y] = 1; + *p++; + } + for (i = 0; irow; i++) + { + for (j = 0; jcolumn; j++) + { + printf("%d", M->footprint[i][j]); + } + printf("\n"); + } + + return OK; +} + +//鑻ヨ糠瀹玬aze涓瓨鍦ㄤ粠鍏ュ彛start鍒板嚭鍙nd鐨勯氶亾锛屽垯姹傚緱涓鏉″瓨鏀惧湪鏍堜腑锛堜粠鏍堝簳 +//鍒版爤椤讹級锛屽苟杩斿洖TRUE锛涘惁鍒欒繑鍥濬ALSE +Status MazePath(SqStack *S, MazeType maze, PosType start, PosType end) +{ + + int curstep = 1;//褰撳墠姝ユ暟 + SElemType e; + PosType curpos = start;//褰撳墠浣嶇疆 + InitStack(S);//鍒濆鍖栨爤 + do { + if (TRUE == Pass(&maze, curpos))//濡傛灉鑳藉閫氳繃 + { + FootPrint(&maze, curpos);//灏嗗綋鍓嶄綅缃缃负1 + e = NewSElem(curstep, curpos, 1);//鍒涘缓鏂拌妭鐐 + Push(S, e);//灏唀鍏ユ爤 + if ((curpos.x == end.x) && (curpos.y == end.y))//鍒拌揪缁堢偣锛堝嚭鍙o級 + { + printf("杩峰璺緞:\n"); + PrintFoot(&maze, S); + return TRUE; + } + curpos = NextPos(curpos, 1); + curstep++;//褰撳墠浣嶇疆+1鑾峰彇涓嬩竴涓綅缃 + } + else {//褰撳墠浣嶇疆涓嶈兘閫氳繃 + if (!StackEmpty(*S)) + { + Pop(S, &e);//濡傛灉鏍堜笉涓虹┖锛屽皢e閫鍑烘爤 + while (e.direction == 4 && !StackEmpty(*S)) + { + MarkPrint(&maze, e.seat);//鏍囪褰撳墠浣嶇疆 + Pop(S, &e); + } + if (e.direction<4) + { + e.direction++; + Push(S, e);//鎻掑叆鏂板厓绱 + curpos = NextPos(e.seat, e.direction); + } + } + } + } while (!StackEmpty(*S)); + return FALSE; +} + +//灏嗚糠瀹殑褰撳墠浣嶇疆pos璁剧疆涓"璧拌繃"锛屽嵆footprint璇ヤ綅缃负1 +Status FootPrint(MazeType *M, PosType pos) +{ + if ((pos.x>M->row) || (pos.y>M->column))//濡傛灉浣嶇疆瓒婁綅 + return FALSE; + M->footprint[pos.x][pos.y] = 1; + return TRUE; +} + +//鍒ゆ柇褰撳墠浣嶇疆鏄惁鍙氾紝鍗充负璧拌繃鐨勯氶亾鍧 +Status Pass(MazeType *M, PosType pos) +{ + if ((pos.x>M->row) || (pos.y>M->column))//濡傛灉浣嶇疆瓒婁綅 + { + exit(0); + } + if ((0 == M->footprint[pos.x][pos.y]) && (M->maze[pos.x][pos.y] == ' ')) + return TRUE; + else + return FALSE; +} + +SElemType NewSElem(int step, PosType pos, int d) +{ + //鍒涘缓鏂扮粨鐐癸紝鐢╯tep锛宲os锛宒鍒濆鍖栬缁撶偣 + SElemType e; + e.number = step; + e.seat = pos; + e.direction = d; + return e; +} + +//鑾峰彇pos浣嶇疆d鏂瑰悜鐨勪綅缃 +PosType NextPos(PosType pos, int d) +{ + switch (d) { + case 1://涓 + pos.x++; + break; + case 2://鍗 + pos.y++; + break; + case 3://瑗 + pos.x--; + break; + case 4://鍖 + pos.y--; + break; + default: + printf("浣嶇疆缂栧彿鍑洪敊.\n"); + } + return pos; +} + +Status MarkPrint(MazeType *M, PosType pos) +{ + if (pos.x>M->row || pos.y>M->column)//鏍囪浣嶇疆瓒婁綅 + { + return ERROR; + } + M->footprint[pos.x][pos.y] = 1; //灏嗚糠瀹玀鐨勮蛋杩囩殑浣嶇疆璁句负1 + return OK; +} \ No newline at end of file diff --git a/2017-1/PWHL/Algorithm 2.12.cpp b/2017-1/PWHL/Algorithm 2.12.cpp new file mode 100644 index 00000000..71b78dd1 --- /dev/null +++ b/2017-1/PWHL/Algorithm 2.12.cpp @@ -0,0 +1,102 @@ + #include "Algorithm 2.12.h" +void CreatList(SLinkList &L, int n)//逆位序输入n个元素的值,建立带表头结点的单链线性表L +{ + int i; + int *a = (int*)malloc(sizeof(int)*n); + A(a, n); + SLinkList p; + L = (SLinkList)malloc(sizeof(component)); + L->next = NULL;//先建立一个带头结点的单链表 + for (i = n; i > 0; --i) + { + p = (SLinkList)malloc(sizeof(component));//生成新的结点 + p->data = a[n - i];//输入元素值 + p->next = L->next; + L->next = p;//插入到表头 + } + +} +void output(SLinkList b) +{ + + b = b->next; + printf("\nthe List is:\n"); + while (b != NULL) + { + printf("%d ", b->data); + b = b->next; + } + printf("\n\n"); +} +void MergeList_L(SLinkList &La, SLinkList &Lb, SLinkList &Lc) { + //已知单链线性表La和Lb的元素按值非递减排列 + //归并La和Lb得到新的单链线性表Lc,Lc的元素也按值非递减排列 + SLinkList l; + SLinkList pa = La->next; + SLinkList pb = Lb->next; + SLinkList pc = Lc = La; + while (pa&&pb) {//判断pa和pb是否为空 + + if (pa->data <= pb->data) { + pc->next = pa; pc = pa; + pa = pa->next;//将Pa赋给Pc,将Pa的地址给Pc,Pa指向下一个相邻结点 + } + else + { + pc->next = pb; pc = pb; pb = pb->next;//将Pb赋给Pc,将Pa的地址给Pc,Pb指向下一个相邻结点 + } + l = Lc->next; + while (l != NULL) + { + printf("%d ", l->data); + l = l->next; + } + printf("\n"); + } + pc->next = pa ? pa : pb;//插入剩余段 + l = Lc->next; + while (l != NULL) + { + printf("%d ", l->data); + l = l->next; + } + printf("\n"); +}//MergeList +void A(int a[], int n) +{ + int i, j, k; + for (i = 0; i < n; i++) + a[i] = rand() % 20 + 1;//产生随机数 + for (i = 0; i < n - 1; i++)//冒泡排序 + { + for (j = 0; j < n - 1 - i; j++) + { + if (a[j] < a[j + 1]) + { + k = a[j]; + a[j] = a[j + 1]; + a[j + 1] = k; + } + } + } +} +int main() +{ + srand(time(0)); + SLinkList La, Lb, Lc; + int a = x; + int b = y; + CreatList(La, a); + output(La); + printf("\n"); + CreatList(Lb, a); + output(Lb); + printf("\n"); + MergeList_L(La, Lb, Lc); + if (Lb != NULL) + { + free(Lb); + } + output(Lc); + return 0; +} \ No newline at end of file diff --git a/2017-1/PWHL/Algorithm 2.12.h b/2017-1/PWHL/Algorithm 2.12.h new file mode 100644 index 00000000..a269244a --- /dev/null +++ b/2017-1/PWHL/Algorithm 2.12.h @@ -0,0 +1,14 @@ +#include +#include +#include +#define x 5; +#define y 5; +typedef struct component +{ + int data; + struct component *next; +}component, *SLinkList; +void CreatList(SLinkList &L, int n); +void MergeList_L(SLinkList&La, SLinkList&Lb, SLinkList&Lc); +void output(SLinkList l); +void A(int a[], int n); \ No newline at end of file diff --git a/2017-1/PWHL/Algorithm.h b/2017-1/PWHL/Algorithm.h new file mode 100644 index 00000000..7331f401 --- /dev/null +++ b/2017-1/PWHL/Algorithm.h @@ -0,0 +1,98 @@ +//迷宫求解 + +#include +#include + +#define OK 1 +#define ERROR 0 +#define TRUE 1 +#define FALSE 0 +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 //栈增长 + +#define COLUMN 10 //迷宫行数 +#define ROW 10 //迷宫列数 + +typedef int Status; //函数返回状态 + +typedef struct //迷宫类型 +{ + char **maze;//迷宫数据(指向指针的指针,可以当二维数组使用) + int **footprint;//足迹数据 + int row;//行数 + int column;//列数 +}MazeType; + +typedef struct //迷宫位置坐标 +{ + int x; + int y; +}PosType; + +typedef struct +{ + int number;//通道块在路径上的"序号" + PosType seat;//通道块在迷宫中的"坐标位置" + int direction;//从此通信块走向下一通道块的"方向" +}SElemType; //栈元素类型 + +typedef struct //顺序栈结构定义 +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; +//构造一个空栈S +Status InitStack(SqStack *S); + +//初始化迷宫数据 +Status InitMaze(MazeType *M); + +//销毁栈S,S不再存在 +Status DestroyStack(SqStack *S); + +//把栈S置为空栈 +Status ClearStack(SqStack *S); + +//若栈S为空栈,则返回TRUE,否则返回FALSE +Status StackEmpty(SqStack S); + +//返回S元素的个数,即栈的长度 +int StackLength(SqStack S); + +//若栈不为空,则用e返回S的栈顶元素,并返回OK;否则返回FALSE +Status GetTop(SqStack S, SElemType *e); + +//插入元素e为新的栈顶元素 +Status Push(SqStack *S, SElemType e); + +//若栈S不为空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR +Status Pop(SqStack *S, SElemType *e); + + + +//输出迷宫 +Status PrintMaze(MazeType *M); + +//若迷宫maze中存在从入口start到出口end的通道,则求得一条存放在栈中(从栈底 +//到栈顶),并返回TRUE;否则返回FALSE +Status MazePath(SqStack *S, MazeType maze, PosType start, PosType end); + +//将迷宫的当前位置pos设置为"走过",即footprint该位置为1 +Status FootPrint(MazeType *M, PosType pos); + +//判断当前位置是否走过 +Status Pass(MazeType *M, PosType pos); + +//创建新结点,用step,pos,d初始化该结点 +SElemType NewSElem(int step, PosType pos, int d); + +//将位置pos的方向设为d +PosType NextPos(PosType pos, int d); + +//将迷宫M的pos位置,设为已走,成功返回OK;否则返回ERROR +Status MarkPrint(MazeType *M, PosType pos); + +//输出迷宫的路径 +Status PrintFoot(MazeType *M, SqStack *S); +//http://blog.csdn.net/nuaazdh/article/details/7051278(代码来源) \ No newline at end of file diff --git a/2017-1/PWHL/Homework.c b/2017-1/PWHL/Homework.c new file mode 100644 index 00000000..fcb2427f --- /dev/null +++ b/2017-1/PWHL/Homework.c @@ -0,0 +1,84 @@ +#include"Homework.h" +status CreateBiTree(BiTree *T, char *c) +{ + + ElemType ch; + ch = c[number]; + number++; + if (ch == '*') + { + *T = NULL; + } + else + { + if (!(*T = (BiTree)malloc(sizeof(BiTNode)))) + { + exit(-1); + } + (*T)->data = ch;//生成根结点 + CreateBiTree(&(*T)->lchild, c);//构造左子树 + CreateBiTree(&(*T)->rchild, c);//构造右子树 + } + return 1; +} +//后序遍历二叉树 +void postorderTraversal(BiTree t)//后续遍历 +{ + if (t == NULL) + { + printf("*"); + return; + } + postorderTraversal(t->lchild);//访问左子树 + postorderTraversal(t->rchild);//访问右子树 + printf("%c ", t->data); +} +//二叉树的深度 +int TreeDepth(BiTree T) +{ + int depth = 0; + if (T) + { + int leftdepth = TreeDepth(T->lchild);//查找左子树的的最深值 + int rightdepth = TreeDepth(T->rchild);//查找右子树的最深值 + depth = leftdepth >= rightdepth ? leftdepth + 1 : rightdepth + 1; + } + return depth; +} +int widthHouxu(BiTree t)//后续遍历 +{ + int i; + int max = 0;//用于存储最大值 + if (t == NULL) + { + return 0; + } + width[w]++; + w++; + widthHouxu(t->lchild);//访问左子树 + widthHouxu(t->rchild);//访问右子树 + w--;//当左子树和右子树均遍历了 返回上一层继续统计 + for (i = 0; i < nwidth; i++)//找到最大值进行输出 + { + + if (max < width[i]) + { + max = width[i]; + } + } + return max; +} +int main() +{ + BiTree t; + ElemType n[20]; + + scanf("%s", n); + printf("*代表空\n"); + CreateBiTree(&t, n);//产生二叉树 + printf("后序遍历的顺序为\n"); + postorderTraversal(t);//后序遍历二叉树 + printf("\n树的深度为%d \n", TreeDepth(t)); + printf("\n树的宽度为%d \n", widthHouxu(t)); + return 0; +} \ No newline at end of file diff --git a/2017-1/PWHL/Homework.h b/2017-1/PWHL/Homework.h new file mode 100644 index 00000000..28bc88a4 --- /dev/null +++ b/2017-1/PWHL/Homework.h @@ -0,0 +1,21 @@ +#include +#include +typedef int status; +typedef char ElemType; +#define nwidth 10 +int w = 1;//计算层数 +int width[nwidth] = { 0 }; +int number = 0;//用于计算结点个数 +int count[100]; + +typedef struct BiTree +{ + struct BiTree *lchild; + struct BiTree *rchild; + ElemType data; +}BiTNode, *BiTree; +status CreateBiTree(BiTree *T, char *c); +void postorderTraversal(BiTree T); +int TreeDepth(BiTree T); +int widthHouxu(BiTree T); + diff --git "a/2017-1/PWHL/QQ\345\233\276\347\211\20720170323215105.png" "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170323215105.png" new file mode 100644 index 00000000..ab05bce7 Binary files /dev/null and "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170323215105.png" differ diff --git "a/2017-1/PWHL/QQ\345\233\276\347\211\20720170328234122.png" "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170328234122.png" new file mode 100644 index 00000000..7c7ace0e Binary files /dev/null and "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170328234122.png" differ diff --git "a/2017-1/PWHL/QQ\345\233\276\347\211\20720170329185748.png" "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170329185748.png" new file mode 100644 index 00000000..8cacd29f Binary files /dev/null and "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170329185748.png" differ diff --git "a/2017-1/PWHL/QQ\345\233\276\347\211\20720170405231742.png" "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170405231742.png" new file mode 100644 index 00000000..73da30eb Binary files /dev/null and "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170405231742.png" differ diff --git "a/2017-1/PWHL/QQ\345\233\276\347\211\20720170405232049.png" "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170405232049.png" new file mode 100644 index 00000000..2d3f1eda Binary files /dev/null and "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170405232049.png" differ diff --git "a/2017-1/PWHL/QQ\345\233\276\347\211\20720170412231440.png" "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170412231440.png" new file mode 100644 index 00000000..75577911 Binary files /dev/null and "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170412231440.png" differ diff --git "a/2017-1/PWHL/QQ\345\233\276\347\211\20720170412231514.png" "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170412231514.png" new file mode 100644 index 00000000..042d7ebe Binary files /dev/null and "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170412231514.png" differ diff --git "a/2017-1/PWHL/QQ\345\233\276\347\211\20720170425223213.png" "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170425223213.png" new file mode 100644 index 00000000..57acf4da Binary files /dev/null and "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170425223213.png" differ diff --git "a/2017-1/PWHL/QQ\345\233\276\347\211\20720170501143348.png" "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170501143348.png" new file mode 100644 index 00000000..b041c797 Binary files /dev/null and "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170501143348.png" differ diff --git "a/2017-1/PWHL/QQ\345\233\276\347\211\20720170510122650.png" "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170510122650.png" new file mode 100644 index 00000000..ccd30824 Binary files /dev/null and "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170510122650.png" differ diff --git "a/2017-1/PWHL/QQ\345\233\276\347\211\20720170513134858.png" "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170513134858.png" new file mode 100644 index 00000000..ca86e209 Binary files /dev/null and "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170513134858.png" differ diff --git "a/2017-1/PWHL/QQ\345\233\276\347\211\20720170513134951.png" "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170513134951.png" new file mode 100644 index 00000000..c329995d Binary files /dev/null and "b/2017-1/PWHL/QQ\345\233\276\347\211\20720170513134951.png" differ diff --git a/2017-1/PWHL/Queue.c b/2017-1/PWHL/Queue.c new file mode 100644 index 00000000..5622c028 --- /dev/null +++ b/2017-1/PWHL/Queue.c @@ -0,0 +1,144 @@ +#include "Queue.h" +int main() +{ + LinkQueue q; + InitQueue(&q); + QElemType x[10] = { 'a','b','c','d','e','f','h','i','j','k' }; + QElemType e; + int i; + for (i = 0; i < 10; i++) + { + EnQueue(&q, x[i]); + } + printf(" 1.插入完成的队列为\n"); + QueueTraverse(q); + printf("\n 2.队列的长度为 %d\n\n", QueueLength(q)); + printf(" 3.判断队列是否为空并返回队头元素\n\n"); + if (GetHead(q, &e)) + { + printf(" 队列不为空,且栈顶元素为%c\n", e); + } + printf("\n 4.依次删除队列\n\n"); + while (!QueueEmpty(q)) + { + DeQueue(&q, &e); + printf(" 被删除的元素为%c\n", e); + printf(" 删除后的队列为\n"); + QueueTraverse(q); + } + printf("\n 5.队列的长度为 %d\n\n", QueueLength(q)); + printf(" 6.销毁队列\n"); + DestroyQueue(&q); + return 0; +} +Status InitQueue(LinkQueue *Q)//构造空队列 +{ + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + Q->front->next = NULL; + return OK; +} +Status DestroyQueue(LinkQueue *Q)//销毁队列Q +{ + while (Q->front) + { + Q->rear = Q->front->next; + free(Q->front); + Q->front = Q->rear; + } + return OK; +} +Status ClearQueue(LinkQueue *Q)//清空队列 +{ + QueuePtr p, q; + Q->rear = Q->front; + p = Q->front->next; + while (p) + { + q = p; + p = p->next; + free(q); + } + p = NULL; + Q->front = Q->rear = NULL; + return OK; +} +Status QueueEmpty(LinkQueue Q)//若队列为空,返回TRUE,否则返回FALSE +{ + if (Q.front == Q.rear) + { + return TRUE; + } + return FALSE; +} +int QueueLength(LinkQueue Q)//求队列长度 +{ + int i = 0; + while (Q.front != Q.rear) + { + i++; + Q.front = Q.front->next; + } + return i; +} +Status GetHead(LinkQueue Q, QElemType *e)//若队列不空,用e返回Q的队头元素,并返回OK ,否则返回ERROR +{ + QueuePtr p; + if (Q.front == Q.rear) + { + return 0; + } + p = Q.front->next; + *e = p->data; + return TRUE; +} +Status EnQueue(LinkQueue *Q, QElemType e)//插入元素e为Q的新的队尾元素 +{ + QueuePtr p; + if (!(p = (QueuePtr)malloc(sizeof(struct QNode)))) + { + return 0; + } + p->data = e; + p->next = NULL; + Q->rear->next = p; + Q->rear = p; + return OK; +} +Status DeQueue(LinkQueue *Q, QElemType *e)//若队列不空,删除Q的对头元素,用e返回其值,并返回OK,否则返回ERROR +{ + QueuePtr p; + if (Q->front == Q->rear) + { + return 0; + } + p = Q->front->next; + *e = p->data; + Q->front->next = p->next; + if (Q->rear == p) + { + Q->rear = Q->front; + } + free(p); + return OK; + +} +Status QueueTraverse(LinkQueue Q)// 从队头到队尾依次对队列Q中每个元素调用函数vi(). +{ + Q.front = Q.front->next; + while (visit(Q.front)) + { + Q.front = Q.front->next; + } + printf("\n"); + return OK; +} +int visit(QueuePtr p)//访问并打印每一个队列元素 +{ + if (NULL != p) + { + char x = p->data; + printf(" %c ", x); + return OK; + } + return 0; +} \ No newline at end of file diff --git a/2017-1/PWHL/Queue.h b/2017-1/PWHL/Queue.h new file mode 100644 index 00000000..f11edea7 --- /dev/null +++ b/2017-1/PWHL/Queue.h @@ -0,0 +1,29 @@ +#include +#include +#define OK 1 +#define ERROR 0 +#define FALSE 0 +#define TRUE 1 +typedef char QElemType; +typedef int Status; +typedef struct QNode +{ + QElemType data; + struct QNode *next; +}QNode, *QueuePtr; +typedef struct Queue +{ + QueuePtr front;//对头指针 + QueuePtr rear;//队尾指针 +}LinkQueue; + +Status InitQueue(LinkQueue *Q);//构造空队列 +Status DestroyQueue(LinkQueue *Q);//销毁队列Q +Status ClearQueue(LinkQueue *Q);//清空队列 +Status QueueEmpty(LinkQueue Q);//若队列为空,返回TRUE,否则返回FALSE +int QueueLength(LinkQueue Q);//求队列长度 +Status GetHead(LinkQueue Q, QElemType *e);//若队列不空,用e返回Q的队头元素,并返回OK ,否则返回ERROR +Status EnQueue(LinkQueue *Q, QElemType e);//插入元素e为Q的新的队尾元素 +Status DeQueue(LinkQueue *Q, QElemType *e);//若队列不空,删除Q的对头元素,用e返回其值,并返回OK,否则返回ERROR +Status QueueTraverse(LinkQueue Q);// 从队头到队尾依次对队列Q中每个元素调用函数vi(). +int visit(Queueptr);//访问并打印每一个队列元素 diff --git a/2017-1/PWHL/S-homework.c b/2017-1/PWHL/S-homework.c new file mode 100644 index 00000000..f741ccf9 --- /dev/null +++ b/2017-1/PWHL/S-homework.c @@ -0,0 +1,45 @@ +#include "S-homework.h" +status CreateBiTree(BiTree *T, char *c) +{ + + ElemType ch; + ch = c[count]; + count++; + if (ch == '*') + { + *T = NULL; + } + else + { + if (!(*T = (BiTree)malloc(sizeof(BiTNode)))) + { + exit(-1); + } + (*T)->data = ch;//生成根结点 + CreateBiTree(&(*T)->lchild, c);//构造左子树 + CreateBiTree(&(*T)->rchild, c);//构造右子树 + } + return 1; +} +void postorderTraversal(BiTree T) +{ + if (T==NULL) + { + printf("*"); + return; + } + + postorderTraversal(T->lchild); + postorderTraversal(T->rchild); + printf("%c ", T->data); +} +int main() +{ + BiTree t; + ElemType c[20]; + scanf("%s", c); + CreateBiTree(&t, c); + printf("后续遍历的顺序为\n"); + postorderTraversal(t); + return 0; +} \ No newline at end of file diff --git a/2017-1/PWHL/S-homework.h b/2017-1/PWHL/S-homework.h new file mode 100644 index 00000000..11f0b166 --- /dev/null +++ b/2017-1/PWHL/S-homework.h @@ -0,0 +1,13 @@ +#include +#include +typedef int status; +typedef char ElemType; +int count=0;//用于计算结点个数 +typedef struct BiTree +{ + struct BiTree *lchild; + struct BiTree *rchild; + ElemType data; +}BiTNode, *BiTree; +status CreateBiTree(BiTree *T, char *c); +void postorderTraversal(BiTree T); diff --git a/2017-1/PWHL/expresion.c b/2017-1/PWHL/expresion.c new file mode 100644 index 00000000..c34d543d --- /dev/null +++ b/2017-1/PWHL/expresion.c @@ -0,0 +1,349 @@ + +#include "expresion.h" + +//构造一个空栈 +PSeqStack createEmptyStack_seq() +{ + PSeqStack pastack; + pastack = (SeqStack*)malloc(sizeof(SeqStack)); + + if (pastack == NULL) + { + printf("空间不够!!\n"); + } + else + { + pastack->t = -1; + } + return pastack;//返回空栈顶 +} + + +//清空栈 +int isEmptyStack_seq(PSeqStack pastack) +{ + return pastack->t == -1; + +} + + +//入栈 +void push_seq(PSeqStack pastack, DataType x) +{ + if (pastack->t >= MAXNUM - 1) + + printf("\n"); + else + { + pastack->t = pastack->t + 1; + pastack->s[pastack->t] = x; + } +} + +//出栈 +void pop_seq(PSeqStack pastack) +{ + if (pastack->t == -1) + { + printf("\n"); + } + else + { + pastack->t = pastack->t - 1; + } +} + +//返回栈顶元素的值 +DataType top_seq(PSeqStack pastack) +{ + return pastack->s[pastack->t]; +} + + + +/*将中缀表达式转换为后缀表达式,顺利转换返回true,若转换过程中发现中缀表达式非法则返回false*/ +int infixtoSuffix(const char* infix, char* suffix) +{ + int state_int = FALSE; /*state_int记录状态,等于true表示刚读入的是数字字符,等于false表示刚读入的是运算符, + 设置这个变量是为了在每输出一个整数后输出一个空格,以免连续输出的两个整数混在一起。*/ + char c, c2; + PSeqStack ps = createEmptyStack_seq(); /*构造一个运算符栈*/ + int i, j = 0; + if (infix[0] == '\0') + { + return FALSE; /*不允许出现空表达式*/ + } + for (i = 0; infix[i] != '\0'; i++) + { + c = infix[i]; + + switch (c) + { + case ' ': + case '\t': + case '\n': + if (state_int == TRUE) + { + suffix[j++] = ' ';/*状态从true转换为false时输出一个空格*/ + } + state_int = FALSE; + break; /*遇到空格或制表符忽略*/ + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + state_int = TRUE; + suffix[j++] = c; /*遇到数字输出*/ + break; + case '(': + if (state_int == TRUE) + { + suffix[j++] = ' ';/*状态从true转换为false时输出一个空格*/ + } + state_int = FALSE; + + push_seq(ps, c); /*遇到左括号,入栈*/ + break; + case ')': + if (state_int == TRUE) + { + suffix[j++] = ' ';/*状态从true转换为false时输出一个空格*/ + } + state_int = FALSE; + c2 = ')'; + while (!isEmptyStack_seq(ps)) + { + c2 = top_seq(ps);/*取栈顶*/ + pop_seq(ps); /*出栈*/ + if (c2 == '(') + { + break; + } + suffix[j++] = c2; + } + if (c2 != '(') + { + free(ps); + suffix[j++] = '\0'; + return FALSE; + } + break; + case '+': + case '-': + if (state_int == TRUE) + { + suffix[j++] = ' '; + } + state_int = FALSE; + while (!isEmptyStack_seq(ps)) + { + c2 = top_seq(ps); + if (c2 == '+' || c2 == '-' || c2 == '*' || c2 == '/') + { + pop_seq(ps); + suffix[j++] = c2; + } + else if (c2 == '(') + { + break; + } + } + push_seq(ps, c); + break; + case '*': + case '/': + if (state_int == TRUE) + { + suffix[j++] = ' '; + } + state_int = FALSE; + while (!isEmptyStack_seq(ps)) + { + c2 = top_seq(ps); + if (c2 == '*' || c2 == '/') + { + pop_seq(ps); + suffix[j++] = c2; + } + else if (c2 == '+' || c2 == '-' || c2 == '(') + { + break; + } + } + push_seq(ps, c); + break; + default: + free(ps); + suffix[j++] = '\0'; + return FALSE; + } + } + if (state_int == TRUE) + { + suffix[j++] = ' '; + } + while (!isEmptyStack_seq(ps)) + { + c2 = top_seq(ps); + pop_seq(ps); + if (c2 == '(') + { + free(ps); + suffix[j++] = '\0'; + return FALSE; + } + suffix[j++] = c2; + } + free(ps); + suffix[j++] = '\0'; + return TRUE; +} + + +//计算后缀 +int calculateSuffix(const char * suffix, int * presult) +{ + + int state_int = FALSE; + PSeqStack ps = createEmptyStack_seq(); + int num = 0, num1, num2; + int i; + char c; + for (i = 0; suffix[i] != '\0'; i++) + { + c = suffix[i]; + switch (c) + { + case '0': + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': + case '9': + if (state_int == TRUE) + { + num = num * 10 + c - '0'; + } + else + { + num = c - '0'; + } + state_int = TRUE; + break; + case ' ': + case'\t': + case '\n': + if (state_int == TRUE) + { + push_seq(ps, num); + state_int = FALSE; + } + break; + case '+': + case '-': + case '*': + case '/': + if (state_int == TRUE) + { + push_seq(ps, num); + state_int = FALSE; + } + if (isEmptyStack_seq(ps)) + { + free(ps); + return FALSE; + } + num2 = top_seq(ps); + pop_seq(ps); + if (isEmptyStack_seq(ps)) + { + free(ps); + return FALSE; + } + num1 = top_seq(ps); + pop_seq(ps); + if (c == '+') + { + push_seq(ps, num1 + num2); + } + if (c == '-') + { + push_seq(ps, num1 - num2); + } + if (c == '*') + { + push_seq(ps, num1 * num2); + } + if (c == '/') + { + push_seq(ps, num1 / num2); + } + break; + default: + free(ps); + return FALSE; + } + } + *presult = top_seq(ps); + pop_seq(ps); + if (!isEmptyStack_seq(ps)) + { + free(ps); + return FALSE; + } + free(ps); + return TRUE; +} +void getline(char* line, int limit) +{ + char c; + int i = 0; + while (i +#include +#define TRUE 1 +#define FALSE 0 +#define MAXNUM 100 +typedef int DataType; +typedef struct +{ + DataType s[MAXNUM]; + int t; +}SeqStack, *PSeqStack; + +PSeqStack createEmptyStack_seq(); +int isEmptyStack_seq(PSeqStack pastack); +void push_seq(PSeqStack pastack, DataType x); +void pop_seq(PSeqStack pastack); +DataType top_seq(PSeqStack pastack); +int infixtoSuffix(const char* infix, char* suffix); +int calculateSuffix(const char * suffix, int * presult); +void getline(char* line, int limit); diff --git a/2017-1/PWHL/homework-13.c b/2017-1/PWHL/homework-13.c new file mode 100644 index 00000000..cac0c2a3 --- /dev/null +++ b/2017-1/PWHL/homework-13.c @@ -0,0 +1,122 @@ +#include "homework-13.h" +int main() +{ + LNode *L; + LNode *p = InitList(&L);//制造要初始化的线性链表 + PrintList(p); + LNode *p1; + LNode *p2; + SplitList(p, &p1, &p2); + printf("拆分后的第一个链表\n"); + PrinttCircle_List(p1); + printf("拆分后的第二个链表\n"); + PrinttCircle_List(p2); + Destroy(&p1); + Destroy(&p2); + return 0; +} + + +//初始化链表 +LNode *InitList(LNode *L) +{ + int i, j; + printf("链表长度为:\n"); + scanf_s("%d", &i); + L = (LNode*)malloc(sizeof(LNode)); + L->data = 1; + L->next = NULL;//创建空链表 + LNode *p; + p = L; + for (j = 2; j <= i; j++) + { + LNode *l; + l = (LNode *)malloc(sizeof(LNode)); + l->data = j;//给结点数据域赋值 + p->next = l; + p = l; + } + p->next = NULL; + return L; +} +//拆分链表 +int SplitList(LNode *L, LNode **L1, LNode **L2) +{ + LNode *p, *p1, *p2, *q1, *q2, *temp; + p = L;//将P指向要拆分的链表L + p1 = (LNode *)malloc(sizeof(LNode)); + p1->next = NULL; + p2 = (LNode *)malloc(sizeof(LNode)); + p2->next = NULL; + q1 = p1; + q2 = p2; + while (NULL != p) + { + temp = p; + p = p->next; + if (temp->data % 2 == 0)//将偶数存入链表q1 + { + temp->next = q1->next; + q1->next = temp; + q1 = temp; + } + //将奇数存入链表q2 + else + { + temp->next = q2->next; + q2->next = temp; + q2 = temp; + } + } + p1->data = (q1->data) / 2; + p2->data = (q2->data + 1) / 2; + q1->next = p1; + q2->next = p2; + *L1 = p1; + *L2 = p2; + return 0; +} +//输出链表 +int PrintList(LNode *L) +{ + do + { + printf("%d ", L->data); + L = L->next; + } while (L != NULL); + printf("\n"); + return OK; +} +//输出循环链表 +int PrinttCircle_List(LNode *L) +{ + LNode *p; + printf("链表长度 %d \n", L->data); + p = L; + L = L->next; + printf("表中元素为:\n"); + do + { + printf("%d ", L->data); + L = L->next; + } while (L != p); + printf("\n"); + return OK; +} +//销毁链表 +int Destroy(LNode **L) +{ + LNode *p; + int i, j; + j = (*L)->data + 1; + for (i = 0; i < j; i++) + { + p = (*L); + (*L) = (*L)->next; + free(p); + } + return OK; +} + + + diff --git a/2017-1/PWHL/homework-13.h b/2017-1/PWHL/homework-13.h new file mode 100644 index 00000000..22810577 --- /dev/null +++ b/2017-1/PWHL/homework-13.h @@ -0,0 +1,24 @@ +#include +#include +#define TRUE 1 +#define FALSE 0 +#define OK 1 +#define ERROR 0 +typedef struct LNode +{ + int data; + struct LNode *next; +}LNode; +//初始化链表 +LNode *InitList(LNode *L); +//拆分链表 +int SplitList(LNode *L, LNode **L1, LNode **L2); +//输出单链表 +int PrintList(LNode *L); +//输出循环链表 +int PrinttCircle_List(LNode *L); +//销毁链表 +int Destroy(LNode **L); + +//空间复杂度为O(1) +//时间复杂度为O(n),将链表拆分为2时只需要对链表循环一次 \ No newline at end of file diff --git a/2017-1/PWHL/hw-13.c b/2017-1/PWHL/hw-13.c new file mode 100644 index 00000000..b51d914a --- /dev/null +++ b/2017-1/PWHL/hw-13.c @@ -0,0 +1,124 @@ +#include "hw-13.h" +int main() +{ + LNode *L; + LNode *p = InitList(&L);//制造要初始化的线性链表 + PrintList(p); + LNode *p1; + LNode *p2; + SplitList(p, &p1, &p2); + printf("拆分后的第一个链表\n"); + PrinttCircle_List(p1); + printf("拆分后的第二个链表\n"); + PrinttCircle_List(p2); + Destroy(&p1); + Destroy(&p2); + return 0; +} + + +//初始化链表 +LNode *InitList(LNode *L) +{ + int i, j; + printf("链表长度为:\n"); + scanf_s("%d", &i); + L = (LNode*)malloc(sizeof(LNode)); + L->data = 1; + L->next = NULL;//创建空链表 + LNode *p; + p = L; + for (j = 2; j <= i; j++) + { + LNode *l; + l = (LNode *)malloc(sizeof(LNode)); + l->data = j;//给结点数据域赋值 + p->next = l; + p = l; + } + p->next = NULL; + return L; +} +//拆分链表 +int SplitList(LNode *L, LNode **L1, LNode **L2) +{ + LNode *p, *p1, *p2, *q1, *q2, *temp; + p = L;//将P指向要拆分的链表L + p1 = (LNode *)malloc(sizeof(LNode)); + p1->next = NULL; + p2 = (LNode *)malloc(sizeof(LNode)); + p2->next = NULL; + q1 = p1; + q2 = p2; + int i = 0; + while (NULL != p) + { + i++; + temp = p; + p = p->next; + if (i% 2 == 0)//将偶数存入链表q1 + { + temp->next = q1->next; + q1->next = temp; + q1 = temp; + } + //将奇数存入链表q2 + else + { + temp->next = q2->next; + q2->next = temp; + q2 = temp; + } + } + p1->data = (q1->data) / 2; + p2->data = (q2->data + 1) / 2; + q1->next = p1; + q2->next = p2; + *L1 = p1; + *L2 = p2; + return 0; +} +//输出链表 +int PrintList(LNode *L) +{ + do + { + printf("%d ", L->data); + L = L->next; + } while (L != NULL); + printf("\n"); + return OK; +} +//输出循环链表 +int PrinttCircle_List(LNode *L) +{ + LNode *p; + printf("链表长度 %d \n", L->data); + p = L; + L = L->next; + printf("表中元素为:\n"); + do + { + printf("%d ", L->data); + L = L->next; + } while (L != p); + printf("\n"); + return OK; +} +//销毁链表 +int Destroy(LNode **L) +{ + LNode *p; + int i, j; + j = (*L)->data + 1; + for (i = 0; i < j; i++) + { + p = (*L); + (*L) = (*L)->next; + free(p); + } + return OK; +} + + + diff --git a/2017-1/PWHL/hw-13.h b/2017-1/PWHL/hw-13.h new file mode 100644 index 00000000..22810577 --- /dev/null +++ b/2017-1/PWHL/hw-13.h @@ -0,0 +1,24 @@ +#include +#include +#define TRUE 1 +#define FALSE 0 +#define OK 1 +#define ERROR 0 +typedef struct LNode +{ + int data; + struct LNode *next; +}LNode; +//初始化链表 +LNode *InitList(LNode *L); +//拆分链表 +int SplitList(LNode *L, LNode **L1, LNode **L2); +//输出单链表 +int PrintList(LNode *L); +//输出循环链表 +int PrinttCircle_List(LNode *L); +//销毁链表 +int Destroy(LNode **L); + +//空间复杂度为O(1) +//时间复杂度为O(n),将链表拆分为2时只需要对链表循环一次 \ No newline at end of file diff --git a/2017-1/PWHL/hw.c b/2017-1/PWHL/hw.c new file mode 100644 index 00000000..4f7ec1db --- /dev/null +++ b/2017-1/PWHL/hw.c @@ -0,0 +1,94 @@ +#include"hw.h" +status CreateBiTree(BiTree *T, char *c) +{ + + ElemType ch; + ch = c[number]; + number++; + if (ch == '*') + { + *T = NULL; + } + else + { + if (!(*T = (BiTree)malloc(sizeof(BiTNode)))) + { + exit(-1); + } + (*T)->data = ch;//生成根结点 + CreateBiTree(&(*T)->lchild, c);//构造左子树 + CreateBiTree(&(*T)->rchild, c);//构造右子树 + } + return 1; +} +//后序遍历二叉树 +void postorderTraversal(BiTree t)//后续遍历 +{ + if (t == NULL) + { + printf("*"); + return; + } + postorderTraversal(t->lchild);//访问左子树 + postorderTraversal(t->rchild);//访问右子树 + printf("%c ", t->data); +} +//二叉树的深度 +int TreeDepth(BiTree T) +{ + int depth = 0; + if (T) + { + int leftdepth = TreeDepth(T->lchild);//查找左子树的的最深值 + int rightdepth = TreeDepth(T->rchild);//查找右子树的最深值 + depth = leftdepth >= rightdepth ? leftdepth + 1 : rightdepth + 1; + } + return depth; +} +int widthHouxu(BiTree t)//后续遍历 +{ + int i; + int max = 0;//用于存储最大值 + if (t == NULL) + { + return 0; + } + width[w]++; + w++; + widthHouxu(t->lchild);//访问左子树 + widthHouxu(t->rchild);//访问右子树 + w--;//当左子树和右子树均遍历了 返回上一层继续统计 + for (i = 0; i < nwidth; i++)//找到最大值进行输出 + { + + if (max < width[i]) + { + max = width[i]; + } + } + return max; +} +int main() +{ + BiTree t; + ElemType n[20]; + int k=0; + printf("用例1:输入1为abc**d**ef***,输入2为abcd**ef***g**h*g**\n"); + + scanf("%d",&k); + if (k == 1) + { + strcpy(n, "abc**d**ef***"); + } + else if (k == 2) + { + strcpy(n, "abcd**ef***g**h*g**"); + } + printf("*代表空\n"); + CreateBiTree(&t, n);//产生二叉树 + printf("后序遍历的顺序为\n"); + postorderTraversal(t);//后序遍历二叉树 + printf("\n树的深度为%d \n", TreeDepth(t)); + printf("\n树的宽度为%d \n", widthHouxu(t)); + return 0; +} \ No newline at end of file diff --git a/2017-1/PWHL/hw.h b/2017-1/PWHL/hw.h new file mode 100644 index 00000000..28bc88a4 --- /dev/null +++ b/2017-1/PWHL/hw.h @@ -0,0 +1,21 @@ +#include +#include +typedef int status; +typedef char ElemType; +#define nwidth 10 +int w = 1;//计算层数 +int width[nwidth] = { 0 }; +int number = 0;//用于计算结点个数 +int count[100]; + +typedef struct BiTree +{ + struct BiTree *lchild; + struct BiTree *rchild; + ElemType data; +}BiTNode, *BiTree; +status CreateBiTree(BiTree *T, char *c); +void postorderTraversal(BiTree T); +int TreeDepth(BiTree T); +int widthHouxu(BiTree T); + diff --git "a/2017-1/PWHL/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTOutput.txt" "b/2017-1/PWHL/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTOutput.txt" new file mode 100644 index 00000000..e9730024 --- /dev/null +++ "b/2017-1/PWHL/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTOutput.txt" @@ -0,0 +1,6 @@ +8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30 +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30 +7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30 diff --git "a/2017-1/PWHL/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/hello.c" "b/2017-1/PWHL/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/hello.c" new file mode 100644 index 00000000..485639ba --- /dev/null +++ "b/2017-1/PWHL/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/hello.c" @@ -0,0 +1,157 @@ +#include "hello.h" +extern int k; +//查找结点 +Status SearchBST(BiTree T, ElemType d, BiTree f, BiTree *p) +{ + if (!T) + { // 空树,查找不成功 + (*p) = f; //f的作用是初始化 p的吗 + return FALSE; + } + else if (d == T->data) + { + (*p) = T; + return TRUE; // 查找成功 + } + else if (d < T->data) //如果该数较小的话 + { // 在左子树中继续查找 + return SearchBST(T->lchild, d, T, p); + } + else + { // 在右子树中继续查找 + return SearchBST(T->rchild, d, T, p); + } +} +//插入元素 +Status InsertBST(BiTree *T, ElemType e) +{ + // 当二叉排序树中不存在关键字等于 e.key 的 + // 数据元素时,插入元素值为 e 的结点,并返 + // 回 TRUE; 否则,不进行插入并返回FALSE + BiTree *p = (BiTree *)malloc(sizeof(BiTree)); + BiTree s; + if (!SearchBST((*T), e, NULL, p)) + { + _CRT_JIT_INTRINSIC s = (BiTNode*)malloc(sizeof(BiTNode)); + s->data = e; + s->lchild = s->rchild = NULL; + if (!(*p)) + { + (*T) = s; // 插入 s 为新的根结点 + } + else if (e < (*p)->data) + { + (*p)->lchild = s; // 插入 *s 为 *p 的左孩子 + } + else + { + (*p)->rchild = s; // 插入 *s 为 *p 的右孩子 + } + return TRUE; + } + else { + return FALSE; + } +} +//删除结点 +Status DeleteBST(BiTree *T, ElemType d) +{ + if (!(*T)) + { // 不存在关键字等于kval的数据元素 + return FALSE; + } + else + { + if (d == (*T)->data) + { // 找到关键字等于key的数据元素 + Delete(T); + return TRUE; + } + else if (d < (*T)->data) + { // 继续在左子树中进行查找 + return DeleteBST(&((*T)->lchild), d); + } + else + { // 继续在右子树中进行查找 + return DeleteBST(&((*T)->rchild), d); + } + } +} +int Delete(BiTree *p) +{ + // 从二叉排序树中删除结点 p, + // 并重接它的左子树或右子树 + BiTree q; + BiTree s; + if (!(*p)->rchild) + { // 右子树为空树则只需重接它的左子树 + q = (*p); + (*p) = (*p)->lchild; + free(q); + } + else if (!(*p)->lchild) + { // 左子树为空树则只需重接它的右子树 + q = (*p); + (*p) = (*p)->rchild; + free(q); + } + else + { // 左右子树均不空 + q = (*p); + s = (*p)->lchild; + while (s->rchild) { // s 指向被删结点的前驱 + q = s; + s = s->rchild; + } + (*p)->data = s->data; + if (q != (*p)) + { + q->rchild = s->lchild; // 重接*q的右子树 + } + else + { + q->lchild = s->lchild; // 重接*q的左子树 + } + free(s); + } + return 1; +} +//先序遍历 +Status PreOrderTraverse(BiTree T) +{ + if (T) + { + out[k] = T->data;//每次输出结点的值 + k++; + PreOrderTraverse(T->lchild); + PreOrderTraverse(T->rchild); + } + else + return 1; +} +//用于打印特定形式的输出 +Status print(BiTree T, FILE *fp) +{ + char x[4] = ", "; + int i; + for (i = 0; i < num; i++) + { + out[i] = -1; + } + PreOrderTraverse(T); + for (i = 0;; i++) + { + if (out[i] == -1) + { + break; + } + if (i != 0) + { + //printf(", "); + fputs(x, fp); + } + //printf("%d", out[i]); + fprintf(fp, "%d", out[i]); + } + return 0; +} diff --git "a/2017-1/PWHL/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/hello.h" "b/2017-1/PWHL/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/hello.h" new file mode 100644 index 00000000..37621c32 --- /dev/null +++ "b/2017-1/PWHL/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/hello.h" @@ -0,0 +1,25 @@ +#include +#include +#include +#define num_length 20 +#define s_length 10 +#define num 25 +typedef int ElemType; +typedef int Status; +typedef enum { FALSE, TRUE } bool; +typedef struct BiTNode +{ + ElemType data;//数据域 + struct BiTNode *lchild, *rchild; +} BiTNode, *BiTree; +//查找结点 +Status SearchBST(BiTree T, ElemType d, BiTree f, BiTree *p); +Status InsertBST(BiTree *T, ElemType e); +Status DeleteBST(BiTree *T, ElemType d); +Status PreOrderTraverse(BiTree T); +Status print(BiTree T, FILE *fp); +int Delete(BiTree *p); +int out[num];//用于存储结点数据域的数组 +//用于给数组计数 + + diff --git "a/2017-1/PWHL/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/hello1.c" "b/2017-1/PWHL/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/hello1.c" new file mode 100644 index 00000000..b9a071df --- /dev/null +++ "b/2017-1/PWHL/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/hello1.c" @@ -0,0 +1,33 @@ +#include "hello.h" +int k = 0; +int main() +{ + FILE *fp; + char z[4] = "\n"; + fp = fopen("BSTOutput.txt", "a"); + int number[num_length] = { 8, 10,14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + int search[s_length] = { 13, 8, 5, 20, 6 }; + BiTree T = NULL; + int i; + for (i = 0; i < 12; i++) + { + InsertBST(&T, number[i]); + T->data; + } + print(T, fp); + fputs(z, fp); + //printf("\n"); + for (i = 0; i < 5; i++) + {//如果插入失败 就进行删除操作 + k = 0;//初始化计数的数字 + if (!InsertBST(&T, search[i])) + { + DeleteBST(&T, search[i]); + } + print(T, fp); + //printf("\n"); + fputs(z, fp); + } + fclose(fp); + return 0; +} diff --git "a/2017-1/PWHL/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\346\234\200\347\237\255\350\267\235\347\246\273/QQ\345\233\276\347\211\20720170517173634.png" "b/2017-1/PWHL/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\346\234\200\347\237\255\350\267\235\347\246\273/QQ\345\233\276\347\211\20720170517173634.png" new file mode 100644 index 00000000..b1566a7d Binary files /dev/null and "b/2017-1/PWHL/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\346\234\200\347\237\255\350\267\235\347\246\273/QQ\345\233\276\347\211\20720170517173634.png" differ diff --git "a/2017-1/PWHL/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\346\234\200\347\237\255\350\267\235\347\246\273/hw.c" "b/2017-1/PWHL/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\346\234\200\347\237\255\350\267\235\347\246\273/hw.c" new file mode 100644 index 00000000..b3f5f5ab --- /dev/null +++ "b/2017-1/PWHL/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\346\234\200\347\237\255\350\267\235\347\246\273/hw.c" @@ -0,0 +1,135 @@ +锘#include "hw.h" +void print(int front)//鎵撳嵃闃熷垪鏁扮粍 +{ + int k = front; + int j; + do + { + j = k; + k = queue[k].c_num;//鎵句笂涓灞傞《鐐 + queue[j].c_num = -1;//灏嗚闂繃鐨勯《鐐规爣娉ㄤ负-1 + } while (k != 0); + k = 0; + while (k < Max) + { + if (queue[k].c_num == -1) + { + printf("%d ", queue[k].vertex + 1); + } + k++; + } +} +//鍒濆鍖栭偦鎺ョ煩闃 +void CreateGraph(Graph **g, int array[][Max], int k)//浣跨敤**g鏂逛究鏀瑰彉g鐨勫唴瀛 +{ + (*g) = (Graph *)malloc(sizeof(Graph));//鍒嗛厤鍐呭瓨 + int i, j; + for (i = 0; i < k; i++) + { + (*g)->a[i] = NULL;//鍒濆鍖栫粨鏋勪綋鎸囬拡 + } + Node *p; + for (i = 0; i < k; i++) + { + for (j = 0; j < k; j++) + { + if (array[i][j] != 0)//濡傛灉鏄氳矾 + { + p = (Node*)malloc(sizeof(Node)); + p->num = j; + p->next = (*g)->a[i]; + (*g)->a[i] = p; + } + } + } +} +//姹傛渶鐭矾寰 +void shortest_path(Graph *g, int i, int j, int *visit) +{ + Node *p; + int m; + int find = 0; + rear++; + int n; + //鍒濆鍖栭槦鍒 + for (n = 0; n < Max; n++) + { + queue[n].vertex = 0; + queue[n].c_num = 0; + } + queue[rear].vertex = i;//璧风偣 + queue[rear].c_num = -1;//鏍囪璇ョ偣 + visit[i] = 1;//鎶婅鐐硅缃负宸茬粡璁块棶杩 + while (front != rear && !find) + { + front++; + m = queue[front].vertex;//浠庨槦鍒椾腑渚濇鎼滅储璧风偣 + if (m == j)//濡傛灉鎵惧埌缁堢偣 + { + find = 1; + print(front); + return; + } + p = g->a[m];//璧风偣鍚庨潰鐨勬暟鎹 + while (p != NULL) + { + //骞垮害浼樺厛閬嶅巻 + if (visit[p->num] == 0) + { + visit[p->num] = 1; + rear++; + queue[rear].vertex = p->num; + queue[rear].c_num = front; + } + p = p->next; + } + } +} +int main() +{ + int Array[][Max] = { + { 1,1,1 ,1,0,0, 1,0,0 }, + { 1,1,1 ,0,0,0, 0,0,0 }, + { 1,1,1, 0,0,0, 0,0,0 }, + { 1,0,0, 1,1,1, 0,0,0 }, + { 0,0,0, 1,1,1, 0,0,0 }, + { 0,0,0, 1,1,1, 0,1,0 }, + { 1,0,0, 0,0,0, 1,1,1 }, + { 0,0,0 ,0,0,1, 1,1,1 }, + { 0,0,0 ,0,0,0, 1,1,1 } + }; + Graph *g; + CreateGraph(&g, Array, Maxnum); + int i, j; + for (i = 0; i < 9; i++) + { + printf("\n"); + for (j = 0; j < 9; j++) + { + if (i == j) + { + + } + else + { + front = rear = -1; + int visit[Max] = { 0 }; + printf("%d<->%d ", i + 1, j + 1); + shortest_path(g, i, j, visit); + printf("\n"); + } + } + } + for (i = 0; i < Maxnum; i++) + { + Node *p, *q; + p = g->a[i]; + while (p != NULL) + { + q = p; + p = p->next; + free(q); + } + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/PWHL/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\346\234\200\347\237\255\350\267\235\347\246\273/hw.h" "b/2017-1/PWHL/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\346\234\200\347\237\255\350\267\235\347\246\273/hw.h" new file mode 100644 index 00000000..6f2d9c85 --- /dev/null +++ "b/2017-1/PWHL/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\346\234\200\347\237\255\350\267\235\347\246\273/hw.h" @@ -0,0 +1,27 @@ +//参考jackcily的作业 +#include +#include + +#define Max 100 //矩阵最大容量 +#define Maxnum 10 //已知图点数 + +//图的邻接矩阵存储 +typedef struct Node +{ + int num;//存储顶点 + struct Node *next; +}Node,*vertexnode; +typedef struct +{ + vertexnode a[Max]; +}Graph; +//设置队列用于存储通过路径顶点的信息 +struct queue +{ + int vertex;//顶点 + int c_num;//层数 +}queue[Max]; +int front, rear;//定义全局变量用来计数遍历过的顶点 +void print(int front);//打印队列数组 +void CreateGraph(Graph **g, int array[][Max], int k);//初始化邻接矩阵 +void shortest_path(Graph *g, int i, int j, int *visit);//求最短路径 \ No newline at end of file diff --git "a/2017-1/PWHL/\347\256\227\346\263\2252.12.c" "b/2017-1/PWHL/\347\256\227\346\263\2252.12.c" new file mode 100644 index 00000000..b5bf072f --- /dev/null +++ "b/2017-1/PWHL/\347\256\227\346\263\2252.12.c" @@ -0,0 +1,118 @@ +#include "算法2.12.h" +void CreatList(Linklist *L, int n)//逆位序输入n个元素的值,建立带表头结点的单链线性表L +{ + int i; + Linklist p; + int *a = (int *)malloc(sizeof(int)*n); + A(a, n); + (*L) = (Linklist)malloc(sizeof(LNode)); + (*L)->next = NULL;//先建立一个带头结点的单链表 + for (i = n; i > 0; i--) + { + p = (Linklist )malloc(sizeof(LNode));//生成新的结点 + p->data = a[n - i]; + p->next = (*L)->next; + (*L)->next = p;//插入到表头 + } + free(a); +} +void output(Linklist s) +{ + s = s->next; + printf("\nthe List is:\n"); + while (s != NULL) + { + printf("%d ", s->data); + s = s->next; + } + printf("\n\n"); +} +void MergeList_L(Linklist La, Linklist Lb, Linklist *Lc) +{ + //已知单链线性表La和Lb的元素按值非递减排列 + //归并La和Lb得到新的单链线性表Lc,Lc的元素也按值非递减排列 + Linklist l,pa,pb,pc; + pa = La->next; + pb = Lb->next; + (*Lc) = pc = La; + while (pa&&pb) //判断pa和pb是否为空 + { + if (pa->data <= pb->data) + { + pc->next = pa; + pc = pa; + pa = pa->next;//将Pa赋给Pc,将Pa的地址给Pc,Pa指向下一个相邻结点 + pc->next = NULL; + } + else + { + pc->next = pb; + pc = pb; + pb = pb->next;//将Pb赋给Pc,将Pa的地址给Pc,Pb指向下一个相邻结点 + pc->next = NULL; + + } + l = (*Lc)->next; + while (l != NULL) + { + printf("%d ", l->data); + l = l->next; + } + printf("\n"); + } + pc->next = pa ? pa : pb; + l = (*Lc)->next; + while (l != NULL) + { + printf("%d ", l->data); + l = l->next; + } +}//MergeList + + +void A(int a[], int n) +{ + int i, j, k; + for (i = 0; i < n; i++) + a[i] = rand() % 20 + 1;//产生随机数 + for (i = 0; i < n - 1; i++)//冒泡排序,将随机数排序 + { + for (j = 0; j < n - 1 - i; j++) + { + if (a[j] < a[j + 1]) + { + k = a[j]; + a[j] = a[j + 1]; + a[j + 1] = k; + } + } + } +} + +void destroyList(Linklist s) +{ + Linklist l; + while (s != NULL) + { + l = s->next; + free(s); + s = l; + } +} +int main() +{ + srand(time(0)); + Linklist La, Lb, Lc; + int a = x; + int b = y; + CreatList(&La, a); + output(La); + printf("\n"); + CreatList(&Lb, a); + output(Lb); + printf("\n"); + MergeList_L(La, Lb, &Lc); + output(Lc); + destroyList(Lb); + return 0; +} \ No newline at end of file diff --git "a/2017-1/PWHL/\347\256\227\346\263\2252.12.h" "b/2017-1/PWHL/\347\256\227\346\263\2252.12.h" new file mode 100644 index 00000000..c3273bc5 --- /dev/null +++ "b/2017-1/PWHL/\347\256\227\346\263\2252.12.h" @@ -0,0 +1,15 @@ +#include +#include +#include +#define x 5; +#define y 5; +typedef struct LNode +{ + int data; + struct LNode *next; +}LNode, *Linklist; +void CreatList(Linklist *L, int n); +void MergeList_L(Linklist La, Linklist Lb, Linklist *Lc); +void output(Linklist s); +void A(int a[], int n); +void destroyList(Linklist); diff --git "a/2017-1/PWHL/\350\277\267\345\256\253\346\261\202\350\247\243.cpp" "b/2017-1/PWHL/\350\277\267\345\256\253\346\261\202\350\247\243.cpp" new file mode 100644 index 00000000..d64c0adc --- /dev/null +++ "b/2017-1/PWHL/\350\277\267\345\256\253\346\261\202\350\247\243.cpp" @@ -0,0 +1,343 @@ +#include "迷宫求解.h" +int main() +{ + MazeType maze;//迷宫结构 + SqStack stack;//顺序栈,存储迷宫路径 + PosType start, end;//迷宫的起点和终点; + start.x = 0; start.y = 1;//迷宫的起点 + end.x = 8; end.y = 9;//迷宫的终点 + InitMaze(&maze);//迷宫初始化 + printf("迷宫形状:\n"); + PrintMaze(&maze);//打印迷宫形状 + if (TRUE == MazePath(&stack, maze, start, end)) + printf("迷宫可解.\n"); + else + printf("迷宫不可解.\n"); + return 0; +} + +Status InitStack(SqStack *S) +{ + //构造一个空栈S + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!S->base)//分配失败 + { + printf("分配内存失败.\n"); + exit(0); + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status InitMaze(MazeType *M) +{ + //初始化迷宫数据 + int i, j; + char mz[ROW][COLUMN] = { + '#',' ','#','#','#','#','#','#','#','#', + '#',' ',' ','#',' ',' ',' ','#',' ','#', + '#',' ',' ','#',' ',' ',' ','#',' ','#', + '#',' ',' ',' ',' ','#','#',' ',' ','#', + '#',' ','#','#','#',' ',' ',' ',' ','#', + '#',' ',' ',' ','#',' ','#',' ','#','#', + '#',' ','#',' ',' ',' ','#',' ',' ','#', + '#',' ','#','#','#',' ','#','#',' ','#', + '#','#',' ',' ',' ',' ',' ',' ',' ',' ', + '#','#','#','#','#','#','#','#','#','#' + }; + + M->maze = (char **)malloc(sizeof(char *)*ROW); + M->footprint = (int **)malloc(sizeof(int *)*ROW); + if (!M->maze || !M->footprint) + { + printf("申请空间失败,迷宫无法初始化.\n"); + return ERROR; + exit(0); + } + for (i = 0; imaze[i] = (char *)malloc(sizeof(char)*COLUMN); + M->footprint[i] = (int *)malloc(sizeof(int)*COLUMN); + if (!M->maze[i] || !M->footprint[i]) + { + printf("申请空间失败,迷宫初始化失败.\n"); + return ERROR; + exit(0); + } + } + for (i = 0; imaze[i][j] = mz[i][j]; + M->footprint[i][j] = 0; + } + } + M->row = ROW;//行 + M->column = COLUMN;//列 + return OK; +} + +Status DestroyStack(SqStack *S) +{ + //销毁栈S,S不再存在 + if (!S)//S为空 + { + printf("指针为空,释放失败.\n"); + exit(0); + } + free(S); + return OK; +} + +Status ClearStack(SqStack *S) +{ + //把栈S置为空栈 + if (!S)//S不存在 + return FALSE; + S->top = S->base;//直接将栈顶指针指向栈底 + return OK; +} + +Status StackEmpty(SqStack S) +{ + //若栈S为空栈,则返回TRUE,否则返回FALSE + if (S.top == S.base) + return TRUE; + else + return FALSE; +} + +int StackLength(SqStack S) +{ + //返回S元素的个数,即栈的长度 + return S.stacksize; +} + +Status GetTop(SqStack S, SElemType *e) +{ + //若栈不为空,则用e返回S的栈顶元素,并返回OK;否则返回FALSE + if (S.top == S.base) + { + printf("栈为空.\n"); + return FALSE; + } + else + { + *e = *(S.top - 1); + printf("栈顶元素:%c\n", *e); + return OK; + } +} + +Status Push(SqStack *S, SElemType e) +{ + //插入元素e为新的栈顶元素 + if (S->top - S->base >= S->stacksize) //栈已满,追加存储空间 + { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if (!S->base) + { + printf("重新申请空间失败.\n"); + exit(0); + } + S->top = S->base + S->stacksize;//更改栈顶指针 + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +} + +Status Pop(SqStack *S, SElemType *e) +{ + //若栈S不为空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR + if (S->top == S->base) //栈为空 + { + printf("栈为空.\n"); + return ERROR; + } + *e = *(--S->top); + return OK; +} + +Status StackTraverse(const SqStack *S) +{ + //从栈底到栈顶依次对每个元素进行访问 + SElemType *p = S->base; + if (S->base == S->top) + { + printf("栈为空.\n"); + return FALSE; + } + printf("栈中元素:"); + while (p != S->top) + { + printf("x=%d,y=%d\n", p->seat.x, p->seat.y); + *p++; + } + printf("\n"); + return OK; +} + +Status PrintMaze(MazeType *M) +{ + //输出迷宫 + int i, j; + for (i = 0; irow; i++) + { + for (j = 0; jcolumn; j++) + { + printf("%c", M->maze[i][j]); + } + printf("\n"); + } + printf("\n"); + return OK; +} + +Status PrintFoot(MazeType *M, SqStack *S) +{ + //输出迷宫的路径 + int i, j; + SElemType *p; + for (i = 0; irow; i++) + { + for (j = 0; jcolumn; j++) + { + M->footprint[i][j] = 0; + } + } + p = S->base; + if (S->base == S->top) + { + printf("栈为空.\n"); + return FALSE; + } + while (p != S->top) + { + M->footprint[p->seat.x][p->seat.y] = 1; + *p++; + } + for (i = 0; irow; i++) + { + for (j = 0; jcolumn; j++) + { + printf("%d", M->footprint[i][j]); + } + printf("\n"); + } + + return OK; +} + +Status MazePath(SqStack *S, MazeType maze, PosType start, PosType end) +{ + //若迷宫maze中存在从入口start到出口end的通道,则求得一条存放在栈中(从栈底 + //到栈顶),并返回TRUE;否则返回FALSE + int curstep = 1;//当前步数 + SElemType e; + PosType curpos = start;//当前位置 + InitStack(S);//初始化栈 + do { + if (TRUE == Pass(&maze, curpos)) + { + FootPrint(&maze, curpos); + e = NewSElem(curstep, curpos, 1); + Push(S, e); + if ((curpos.x == end.x) && (curpos.y == end.y)) + {//到达终点(出口) + printf("迷宫路径:\n"); + PrintFoot(&maze, S); + return TRUE; + } + curpos = NextPos(curpos, 1); + curstep++; + } + else {//当前位置不能通过 + if (!StackEmpty(*S)) + { + Pop(S, &e); + while (e.direction == 4 && !StackEmpty(*S)) + { + MarkPrint(&maze, e.seat); + Pop(S, &e); + } + if (e.direction<4) + { + e.direction++; + Push(S, e); + curpos = NextPos(e.seat, e.direction); + } + } + } + } while (!StackEmpty(*S)); + return FALSE; +} + +Status FootPrint(MazeType *M, PosType pos) +{ + //将迷宫的当前位置pos设置为"走过",即footprint该位置为1 + if ((pos.x>M->row) || (pos.y>M->column)) + return FALSE; + M->footprint[pos.x][pos.y] = 1; + return TRUE; +} + +Status Pass(MazeType *M, PosType pos) +{ + //判断当前位置是否可通,即为走过的通道块 + if ((M->rowcolumnfootprint[pos.x][pos.y]) && (M->maze[pos.x][pos.y] == ' ')) + return TRUE; + else + return FALSE; +} + +SElemType NewSElem(int step, PosType pos, int d) +{ + //创建新结点,用step,pos,d初始化该结点 + SElemType e; + e.number = step; + e.seat = pos; + e.direction = d; + return e; +} + +PosType NextPos(PosType pos, int d) +{ + //获取pos位置d方向的位置 + switch (d) { + case 1://东 + pos.x++; + break; + case 2://南 + pos.y++; + break; + case 3://西 + pos.x--; + break; + case 4://北 + pos.y--; + break; + default: + printf("位置编号出错.\n"); + } + return pos; +} + +Status MarkPrint(MazeType *M, PosType pos) +{ + //将迷宫M的pos位置,设为已走,成功返回OK;否则返回ERROR + if (pos.x>M->row || pos.y>M->column) + { + printf("所要标记位置越位.\n"); + return ERROR; + } + M->footprint[pos.x][pos.y] = 1; + return OK; +} \ No newline at end of file diff --git "a/2017-1/PWHL/\350\277\267\345\256\253\346\261\202\350\247\243.h" "b/2017-1/PWHL/\350\277\267\345\256\253\346\261\202\350\247\243.h" new file mode 100644 index 00000000..ee817b45 --- /dev/null +++ "b/2017-1/PWHL/\350\277\267\345\256\253\346\261\202\350\247\243.h" @@ -0,0 +1,80 @@ +#include +#include + +#define OK 1 +#define ERROR 0 +#define TRUE 1 +#define FALSE 0 +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 //栈增长 + +#define COLUMN 10 //迷宫行数 +#define ROW 10 //迷宫列数 + +typedef int Status; //函数返回状态 + +typedef struct //迷宫类型 +{ + char **maze;//迷宫数据 //(指向指针的指针,可以当二维数组使用) + int **footprint;//足迹数据 + int row;//行数 + int column;//列数 +}MazeType; + +typedef struct //迷宫位置坐标 +{ + int x; + int y; +}PosType; + +typedef struct +{ + int number;//通道块在路劲上的"序号" + PosType seat;//通道块在迷宫中的"坐标位置" + int direction;//从此通信块走向下一通道块的"方向" +}SElemType; //栈元素类型 + +typedef struct //顺序栈结构定义 +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack(SqStack *S); +//构造一个空栈S +Status InitMaze(MazeType *M); +//初始化迷宫数据 +Status DestroyStack(SqStack *S); +//销毁栈S,S不再存在 +Status ClearStack(SqStack *S); +//把栈S置为空栈 +Status StackEmpty(SqStack S); +//若栈S为空栈,则返回TRUE,否则返回FALSE +int StackLength(SqStack S); +//返回S元素的个数,即栈的长度 +Status GetTop(SqStack S, SElemType *e); +//若栈不为空,则用e返回S的栈顶元素,并返回OK;否则返回FALSE +Status Push(SqStack *S, SElemType e); +//插入元素e为新的栈顶元素 +Status Pop(SqStack *S, SElemType *e); +//若栈S不为空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR +Status StackTraverse(const SqStack *S); +//从栈底到栈顶依次对每个元素进行访问 +Status PrintMaze(MazeType *M); +//输出迷宫 +Status MazePath(SqStack *S, MazeType maze, PosType start, PosType end); +//若迷宫maze中存在从入口start到出口end的通道,则求得一条存放在栈中(从栈底 +//到栈顶),并返回TRUE;否则返回FALSE +Status FootPrint(MazeType *M, PosType pos); +//将迷宫的当前位置pos设置为"走过",即footprint该位置为1 +Status Pass(MazeType *M, PosType pos); +//判断当前位置是否走过 +SElemType NewSElem(int step, PosType pos, int d); +//创建新结点,用step,pos,d初始化该结点 +PosType NextPos(PosType pos, int d); +//将位置pos的方向设为d +Status MarkPrint(MazeType *M, PosType pos); +//将迷宫M的pos位置,设为已走,成功返回OK;否则返回ERROR +Status PrintFoot(MazeType *M, SqStack *S); +//输出迷宫的路径 diff --git a/2017-1/README.md b/2017-1/README.md old mode 100644 new mode 100755 diff --git "a/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/Tree.c" "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/Tree.c" new file mode 100644 index 00000000..aefb8d3e --- /dev/null +++ "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/Tree.c" @@ -0,0 +1,136 @@ +#include +#include +#include "Tree.h" + +//==========方法一,以字符串的形式“根 左子树 右子树”定义一棵二叉树=============// +char c[] = "ABC DFE G H "; +int i = 0; + +Status CreateBiTree(BiTree *T) { + char ch = c[i]; //遍历字符串 + + if (ch == ' ') { + *T = NULL; + i++; + } + else { + if (!((*T) = (BiTree)malloc(sizeof(BiTNode)))) { + return OVERFLOW; + } + (*T)->data = ch; // 生成根结点 + i++; + CreateBiTree(&(*T)->lchild); // 构造左子树 + CreateBiTree(&(*T)->rchild); // 构造右子树 + } + return OK; +} + +//==========方法二,由二叉树的先序和中序序列建树=============// +int Search(char ino[], char p) { + //在ino[]中寻找字符p,找到则返回位置,否则返回-1 + int i; + for (i = 0; ino[i] != '\0'; i++) { + if (ino[i] == p) { + return i; + } + } + return -1; +} + +int Getlength(char *p) { + //获取字符串的长度 + int i; + for (i = 0; p[i] != '\0'; i++); + return i; +} + +Status CrtBT(BiTree *T, char pre[], char ino[], int ps, int is, int n) { + // 已知pre[ps...ps+n-1]为二叉树的先序序列, ino[is...is+n-1]为二叉树的中序序列 + // 本算法由此两个序列构造二叉链表 + int k; + if (n == 0) { + *T = NULL; + } + else { + k = Search(ino, pre[ps]); // 在中序序列中查询pre[ps]在ino中的第几个位置 + if (k == -1) { + *T = NULL; + } + else { + if (!((*T) = (BiTree)malloc(sizeof(BiTNode)))) { + return OVERFLOW; + } + (*T)->data = pre[ps]; + if (k == is) { + (*T)->lchild = NULL; + } + else { + CrtBT(&(*T)->lchild, pre, ino, ps + 1, is, k - is); + } + if (k == is + n - 1) { + (*T)->rchild = NULL; + } + else { + CrtBT(&(*T)->rchild, pre, ino, ps + 1 + (k - is), k + 1, n - (k - is) - 1); + } + } + } + return OK; +} + +void print(char *pre, char *in, BiTree T) { + //将方法二的一系列输出合为一个函数 + printf("Created tree succeeded!\n"); + printf("Preorder input: %s\n", pre); + printf("Inorder input: %s\n", in); + printf("The postorder traverse: "); + PostOrderTraverse(T); + printf("\n"); +} + +//==========二叉树的基本操作=============// +Status PostOrderTraverse(BiTree T) { + //后序遍历二叉树的递归算法 + if (T) { + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c", T->data); + } + return OK; +} + +int main(){ + BiTree Tree = NULL; + int flag; + + //==========方法一=============// + flag = CreateBiTree(&Tree); + printf("---------------Test one---------------\n"); + if (!flag) { + printf("Created tree succeeded!\n"); + printf("The original input: %s\n", c); + printf("The postorder traverse: "); + PostOrderTraverse(Tree); + printf("\n"); + } + else { + printf("Created tree failed!\n"); + } + + //==========方法二=============// + int k; + char p1[] = "ABDFCE"; + char p2[] = "DFBAEC"; + + k = Getlength(p1); + flag = CrtBT(&Tree, p1, p2, 0, 0, k); + printf("\n---------------Test two---------------\n"); + if (!flag) { + print(p1, p2, Tree); + } + else { + printf("Created tree failed!\n"); + } + + return 0; +} \ No newline at end of file diff --git "a/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/Tree.h" "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/Tree.h" new file mode 100644 index 00000000..7f88051a --- /dev/null +++ "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/Tree.h" @@ -0,0 +1,24 @@ + +typedef char ElemType; + +typedef struct BiTNode { //二叉树结构定义 + ElemType data; + struct BiTNode *lchild, *rchild; +} BiTNode, *BiTree; + +typedef enum { + OK, // OK = 0 + ERROR, //ERROR = 1 + OVERFLOW //OVERFLOW = 2 +}Status;//函数返回状态 + +//==========二叉树的基本操作=============// +Status PostOrderTraverse(BiTree T); + +//==========方法一,以字符串的形式“根 左子树 右子树”定义一棵二叉树=============// +Status CreateBiTree(BiTree *T); + +//==========方法二,由二叉树的先序和中序序列建树=============// +int Search(char ino[], char p); +Status CrtBT(BiTree *T, char pre[], char ino[], int ps, int is, int n); +void print(char *pre, char *in, BiTree T); diff --git "a/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..1dbd8188 Binary files /dev/null and "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\346\267\261\345\272\246\343\200\201\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\346\225\260\343\200\201\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/tree.c" "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\346\267\261\345\272\246\343\200\201\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\346\225\260\343\200\201\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/tree.c" new file mode 100644 index 00000000..07414c57 --- /dev/null +++ "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\346\267\261\345\272\246\343\200\201\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\346\225\260\343\200\201\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/tree.c" @@ -0,0 +1,404 @@ +#include +#include +#include +#include +#include "tree.h" + +//==========队列的基本操作=============// +Status InitQueue(LinkQueue *Q) { + //构造空队列 + Q->front = Q->rear = (QueuePtr)malloc(MAXQSIZE * sizeof(QNode)); + if (!(Q->front)) { + return ERROR; + } + Q->front->next = NULL; + Q->rear->next = NULL; + return OK; +}; + +bool QueueEmpty(LinkQueue Q) { + //判断是否为空队列 + if (Q.front == Q.rear) { + return true; + } + return false; +}; + +Status EnQueue(LinkQueue *Q, QElemType e) { + //插入新的队尾元素 + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) { + return ERROR; + } + p->data = e; + p->next = NULL; + Q->rear->next = p; + Q->rear = p; + return OK; +}; + +Status DeQueue(LinkQueue *Q, QElemType *e) { + //删除队头元素,并用e返回其值 + QueuePtr p = NULL; + if (QueueEmpty(*Q)) { + printf("错误!空队列!\n"); + return ERROR; + } + p = Q->front->next; + *e = p->data; + Q->front->next = p->next; + if (Q->rear == p) { + Q->rear = Q->front; + } + free(p); + return OK; +}; + +int QueueLength(LinkQueue Q) { + //返回队列的元素个数 + QueuePtr p; + int i = 0; + p = Q.front->next; + if (QueueEmpty(Q)) { + return 0; + } + while (p) { + p = p->next; + i++; + } + return i; +}; + +//==========二叉树的基本操作=============// +Status PostOrderTraverse(BiTree T) { + //后序遍历二叉树的递归算法 + if (T) { + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c", T->data); + } + return OK; +} + +int Depth(BiTree T) { + // 返回二叉树的深度 + int depthval, depthLeft, depthRight; + if (!T) { + depthval = 0; + } + else { + depthLeft = Depth(T->lchild); + depthRight = Depth(T->rchild); + depthval = 1 + (depthLeft > depthRight ? depthLeft : depthRight); + } + return depthval; +} + +int Width(BiTree T) { + // 层序遍历二叉树,返回二叉树的宽度 + LinkQueue Q; + BiTNode *p = T; + int width = 1; //二叉树宽度 + int temp = 0; //上一层宽度 + int last = 0; + int now = 0; //当前宽度 + + if (!T) { + return 0; + } + InitQueue(&Q); //建立工作队列 + EnQueue(&Q, T); //将根节点入队列 + last = 1; + while (!QueueEmpty(Q)) { + temp = last; + while (temp != 0) { + DeQueue(&Q, &p); + if (p->lchild) { + EnQueue(&Q, p->lchild); + } + if (p->rchild) { + EnQueue(&Q, p->rchild); + } + temp--; + } + now = QueueLength(Q); + width = now > width ? now : width; + last = now; + } + return width; +} + +bool Compelete(BiTree T) { + //判断是否为完全二叉树 + LinkQueue Q; + BiTNode *p = T; + InitQueue(&Q); //建立工作队列 + EnQueue(&Q, T); + + while (!QueueEmpty(Q)) { + DeQueue(&Q, &p); + if (p->lchild) { + EnQueue(&Q, p->lchild); + } + if (p->rchild) { + EnQueue(&Q, p->rchild); + } + if ((!p->lchild) && p->rchild) { + return false; + } + } + + int depth; //左孩子都存在时,进一步判断 + depth = Depth(T); + if ((Count(T) - CountLeaf(T)) == pow(2, depth - 1)) { + return true; //除了最后一层,其余层的结点都达到最大值 + } + else { + return false; + } + return true; +} + +int Count(BiTree T) { + //返回指针T所指二叉树中所有结点个数 + int m, n; + if (!T) { + return 0; + } + if ((!T->lchild) && (!T->rchild)) { + return 1; + } + else { + m = Count(T->lchild); + n = Count(T->rchild); + return m + n + 1; + } +} + +int CountLeaf(BiTree T) { + // 返回指针T所指二叉树中所有叶子结点个数 + int m, n; + if (!T) { + return 0; + } + if ((!T->lchild) && (!T->rchild)) { + return 1; + } + else { + m = CountLeaf(T->lchild); + n = CountLeaf(T->rchild); + return m + n; + } +} + +bool Full(BiTree T) { + //判断是否为满树(若树的深度为k,应有2^k-1个结点) + if (pow(2, Depth(T)) - 1 == Count(T)) { + printf("It is a full tree\n"); + return true; + } + printf("It is not a full tree\n"); + return false; +} + +Status CountNode(BiTree T) { + int total, leaf; + if (T) { + total = Count(T); + leaf = CountLeaf(T); + printf("The number of node is:%d\n", total); + printf("The number of leaves is:%d\n", leaf); + printf("The number of node(except leaves) is:%d - %d = %d\n", total, leaf, total - leaf); + return OK; + } + return ERROR; +} + +//==========方法一,以字符串的形式“根 左子树 右子树”定义一棵二叉树=============// +char c[] = "ABC DE G F "; +int i = 0; +Status CreateBiTree(BiTree *T) { + char ch = c[i]; //遍历字符串 + + if (ch == ' ') { + *T = NULL; + i++; + } + else { + if (!((*T) = (BiTree)malloc(sizeof(BiTNode)))) { + return OVERFLOW; + } + (*T)->data = ch; // 生成根结点 + i++; + CreateBiTree(&(*T)->lchild); // 构造左子树 + CreateBiTree(&(*T)->rchild); // 构造右子树 + } + return OK; +} + +//==========方法二,由二叉树的先序和中序序列建树=============// +int Search(char ino[], char p) { + //在ino[]中寻找字符p,找到则返回位置,否则返回-1 + int i; + for (i = 0; ino[i] != '\0'; i++) { + if (ino[i] == p) { + return i; + } + } + return -1; +} + +int Getlength(char *p) { + //获取字符串的长度 + int i; + for (i = 0; p[i] != '\0'; i++); + return i; +} + +Status CrtBT(BiTree *T, char pre[], char ino[], int ps, int is, int n) { + // 已知pre[ps...ps+n-1]为二叉树的先序序列, ino[is...is+n-1]为二叉树的中序序列 + // 本算法由此两个序列构造二叉链表 + int k; + if (n == 0) { + *T = NULL; + } + else { + k = Search(ino, pre[ps]); // 在中序序列中查询pre[ps]在ino中的第几个位置 + if (k == -1) { + *T = NULL; + } + else { + if (!((*T) = (BiTree)malloc(sizeof(BiTNode)))) { + return OVERFLOW; + } + (*T)->data = pre[ps]; + if (k == is) { + (*T)->lchild = NULL; + } + else { + CrtBT(&(*T)->lchild, pre, ino, ps + 1, is, k - is); + } + if (k == is + n - 1) { + (*T)->rchild = NULL; + } + else { + CrtBT(&(*T)->rchild, pre, ino, ps + 1 + (k - is), k + 1, n - (k - is) - 1); + } + } + } + return OK; +} + +void print(char *pre, char *in, BiTree T) { + //将方法二的一系列输出合为一个函数 + printf("Created tree succeeded!\n"); + printf("Preorder input: %s\n", pre); + printf("Inorder input: %s\n", in); + printf("The postorder traverse: "); + PostOrderTraverse(T); + printf("\n"); + printf("The depth of the tree is %d\n", Depth(T)); + printf("The width of the tree is %d\n", Width(T)); +} + +int main() { + BiTree Tree = NULL; + int flag; + int length; + + //==========方法一=============// + flag = CreateBiTree(&Tree); + printf("---------------Test One---------------\n"); + if (!flag) { + printf("Created tree succeeded!\n"); + printf("The original input: %s\n", c); + printf("The postorder traverse: "); + PostOrderTraverse(Tree); + printf("\n"); + printf("The depth of the tree is %d\n", Depth(Tree)); + printf("The width of the tree is %d\n", Width(Tree)); + if (Compelete(Tree)) { + printf("It is a compeleted tree\n"); + } + else{ + printf("It is not a compeleted tree\n"); + } + Full(Tree); + CountNode(Tree); + } + else { + printf("Created tree failed!\n"); + } + + //==========方法二=============// + char p1[10] = "ABDFCE"; //先序序列 + char p2[10] = "DFBAEC"; //中序序列 + + length = Getlength(p1); + flag = CrtBT(&Tree, p1, p2, 0, 0, length); + printf("\n---------------Test Two---------------\n"); + if (!flag) { + print(p1, p2, Tree); + if (Compelete(Tree)) { + printf("It is a compeleted tree\n"); + } + else { + printf("It is not a compeleted tree\n"); + } + Full(Tree); + CountNode(Tree); + } + else { + printf("Created tree failed!\n"); + } + + //==========新增用例一=============// + strcpy(p1, "ABCDEFGHK"); //构建新的先序序列 + strcpy(p2, "BDCAEHGKF"); //构建新的中序序列 + length = Getlength(p1); + flag = CrtBT(&Tree, p1, p2, 0, 0, length); + printf("\n---------------Test Three---------------\n"); + if (!flag) { + print(p1, p2, Tree); + if (Compelete(Tree)) { + printf("It is a compeleted tree\n"); + } + else { + printf("It is not a compeleted tree\n"); + } + Full(Tree); + CountNode(Tree); + } + else { + printf("Created tree failed!\n"); + } + + //==========新增用例二=============// + strcpy(p1, "ABCDEFG"); //构建新的先序序列 + strcpy(p2, "CBDAFEG"); //构建新的中序序列 + + //==========新增用例(非完全树)=============// + //strcpy(p1, "ABDEFGC"); //构建新的先序序列 + //strcpy(p2, "DBFEGAC"); //构建新的中序序列 + + length = Getlength(p1); + flag = CrtBT(&Tree, p1, p2, 0, 0, length); + printf("\n---------------Test Four---------------\n"); + if (!flag) { + print(p1, p2, Tree); + if (Compelete(Tree)) { + printf("It is a compeleted tree\n"); + } + else { + printf("It is not a compeleted tree\n"); + } + Full(Tree); + CountNode(Tree); + } + else { + printf("Created tree failed!\n"); + } + + return 0; +} \ No newline at end of file diff --git "a/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\346\267\261\345\272\246\343\200\201\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\346\225\260\343\200\201\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/tree.h" "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\346\267\261\345\272\246\343\200\201\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\346\225\260\343\200\201\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/tree.h" new file mode 100644 index 00000000..59f588c1 --- /dev/null +++ "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\346\267\261\345\272\246\343\200\201\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\346\225\260\343\200\201\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/tree.h" @@ -0,0 +1,55 @@ +#define MAXQSIZE 100 //队列大小定义 + +typedef char ElemType; + +typedef struct BiTNode { //二叉树结构定义 + ElemType data; + struct BiTNode *lchild, *rchild; +} BiTNode, *BiTree, *QElemType; + +typedef enum { + OK, // OK = 0 + ERROR //ERROR = 1 +}Status;//函数返回状态 + +typedef enum { + false, + true +}bool;//布尔返回状态 + +typedef struct QNode { //队列结构定义 + QElemType data; + struct QNode *next; +}QNode, *QueuePtr; + +typedef struct { + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +//==========队列的基本操作=============// +Status InitQueue(LinkQueue *Q); +bool QueueEmpty(LinkQueue Q); +Status EnQueue(LinkQueue *Q, QElemType e); +Status DeQueue(LinkQueue *Q, QElemType *e); + +//==========二叉树的基本操作=============// +Status PostOrderTraverse(BiTree T); +int Depth(BiTree T); +int Width(BiTree T); +bool Compelete(BiTree T); +int Count(BiTree T); +int CountLeaf(BiTree T); +bool Full(BiTree T); +Status CountNode(BiTree T); + +//==========方法一,以字符串的形式“根 左子树 右子树”定义一棵二叉树=============// +Status CreateBiTree(BiTree *T); + +//==========方法二,由二叉树的先序和中序序列建树=============// +int Search(char ino[], char p); +int Getlength(char *p); +Status CrtBT(BiTree *T, char pre[], char ino[], int ps, int is, int n); +void print(char *pre, char *in, BiTree T); + +#pragma once \ No newline at end of file diff --git "a/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\346\267\261\345\272\246\343\200\201\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\346\225\260\343\200\201\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\345\233\233\346\243\265\346\240\221\347\232\204\347\244\272\346\204\217\345\233\276.jpg" "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\346\267\261\345\272\246\343\200\201\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\346\225\260\343\200\201\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\345\233\233\346\243\265\346\240\221\347\232\204\347\244\272\346\204\217\345\233\276.jpg" new file mode 100644 index 00000000..7d59b3c0 Binary files /dev/null and "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\346\267\261\345\272\246\343\200\201\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\346\225\260\343\200\201\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\345\233\233\346\243\265\346\240\221\347\232\204\347\244\272\346\204\217\345\233\276.jpg" differ diff --git "a/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\346\267\261\345\272\246\343\200\201\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\346\225\260\343\200\201\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\346\267\261\345\272\246\343\200\201\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\346\225\260\343\200\201\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..d13e8b3f Binary files /dev/null and "b/2017-1/SQ/\344\272\214\345\217\211\346\240\221/\346\267\261\345\272\246\343\200\201\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\346\225\260\343\200\201\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/SQ/\345\223\210\345\270\214\350\241\250/Hash.c" "b/2017-1/SQ/\345\223\210\345\270\214\350\241\250/Hash.c" new file mode 100644 index 00000000..93f56d06 --- /dev/null +++ "b/2017-1/SQ/\345\223\210\345\270\214\350\241\250/Hash.c" @@ -0,0 +1,163 @@ +#include "Hash.h" + +Status CreateHash(HashTable *H, int size) { + // 构建Hash表 + ElemType temp; + int i; + + (*H).sizeindex = size; + (*H).count = 0; + (*H).elem = (ElemType *)malloc(sizeof(ElemType) * size); + + if (!((*H).elem)) { + return ERROR; + } + + for (i = 0; i < size; i++) { + (*H).elem[i].key = NULLKEY; + (*H).elem[i].val = NONEVALUE; + } + + printf("==========Creating HashTable==========\n"); + printf("HashTable size:%d\n", (*H).sizeindex); + for (i = 0; H->count < size / 2; i++) { + temp.key = (int)rand() % 501; + temp.val = (int)rand() % 501; + InsertHash(H, temp); + } + printf("Crating Over\n\n"); + + return OK; +} + +bool Empty(HashTable H) { + // 判断Hash表是否为空,即没有元素 + if (H.count == 0) { + return TRUE; + } + + return FALSE; +} + +bool Prime(int n) { + // 判断一个数是否为素数 + if (n == 2) { + return TRUE; + } + + for (int i = 2; i <= sqrt(n); i++) { + if (n % i == 0) { + return FALSE; + } + } + + return TRUE; +} + +int SearchHash(HashTable H, KeyType K, int *p, int *c) { + // 在开放定址哈希表H中查找关键值为K的记录 + *p = K % H.sizeindex; // 求得哈希地址 + *c = 0; // c用于记录冲突次数,初值置0 + while (H.elem[*p].key != NULLKEY && !EQ(K, H.elem[*p].key)) { + if ((*p) < H.sizeindex) { + printf("Collide with %d->%d\n", H.elem[*p].key, H.elem[*p].val); + (*p)++; + (*c)++; + } + else + break; + } + if (EQ(K, H.elem[*p].key)) { // 查找成功,p返回待查数据元素位置 + return SUCCESS; + } + else { + return FAILED; // 查找不成功 + } +} + +Status InsertHash(HashTable *H, ElemType e) { + // 查找不成功时插入数据元素e到开放地址哈希表H中,并返回OK + // 若冲突次数过大或找到末尾,则重建哈希表 + int c, p; + if (SearchHash(*H, e.key, &p, &c) == SUCCESS) { // 表中已有与 e 有相同关键字的元素 + return DUPLICATE; + } + else{ + if (p == (*H).sizeindex || c > (*H).sizeindex / 2) { + RecreateHashTable(H); + } + (*H).elem[p] = e; + ++(*H).count; + printf("Collision times:%d\n", c); + printf("Insert:%d->%d\n", H->elem[p].key, H->elem[p].val); + printf("--------------------------\n"); + return OK; + } +} + +bool EQ(KeyType a, KeyType b) { + // 判断关键值a是否等于b + if (a == b) { + return TRUE; + } + + return FALSE; +} + +Status RecreateHashTable(HashTable *H) { + // 重建Hash表,即扩充(一个麻烦的做法) + int i, n; + HashTable temp; // 临时Hash表 + ElemType e; + + if (Empty(*H)) { + return ERROR; + } + + e.key = NULLKEY; + e.val = NONEVALUE; + n = 2 * (*H).sizeindex; + + while (!Prime(n)) { + n++; + }; + + temp.sizeindex = n; + temp.count = (*H).count; + temp.elem = (ElemType *)malloc(sizeof(ElemType)*n); + for (i = 0; i < n; i++) { + temp.elem[i].key = NULLKEY; + temp.elem[i].val = NONEVALUE; + } + for (i = 0; i < (*H).sizeindex; i++) { + temp.elem[i].key = (*H).elem[i].key; + temp.elem[i].val = (*H).elem[i].val; + } + + (*H).sizeindex = temp.sizeindex; + (*H).count = temp.count; + (*H).elem = (ElemType *)malloc(sizeof(ElemType)*n); + for (i = 0; i < n; i++) { + (*H).elem[i].key = temp.elem[i].key; + (*H).elem[i].val = temp.elem[i].val; + } + + free(temp.elem); + + return OK; +} + +Status Print(HashTable H) { + // 遍历输出Hash表 + if (Empty(H)) { + return ERROR; + } + + printf("==========Print HashTable==========\n"); + for (int i = 0; i < H.sizeindex; i++) + printf("[%d] : %d->%d\n", i, H.elem[i].key, H.elem[i].val); + printf("Print Over\n"); + printf("\n"); + + return OK; +} \ No newline at end of file diff --git "a/2017-1/SQ/\345\223\210\345\270\214\350\241\250/Hash.h" "b/2017-1/SQ/\345\223\210\345\270\214\350\241\250/Hash.h" new file mode 100644 index 00000000..4166012f --- /dev/null +++ "b/2017-1/SQ/\345\223\210\345\270\214\350\241\250/Hash.h" @@ -0,0 +1,45 @@ +#include +#include +#include + +#define SUCCESS 1 +#define FAILED 0 +#define DUPLICATE -1 // 重复 +#define NULLKEY -1 // 空关键字 +#define NONEVALUE 0 // 空值 + +typedef int KeyType; +typedef int ValueType; + +typedef enum { // 函数返回值 + OK, + ERROR +}Status; + +typedef enum { // 布尔返回值 + FALSE, + TRUE +}bool; + +typedef struct _ElemType { + KeyType key; // 关键字 + ValueType val; // 值 +#ifdef CHAINED_HASH + struct _ElemType *next; +#endif +} ElemType; + +typedef struct { + ElemType *elem; // 储存哈希表元素 + int count; // 元素个数 + int sizeindex; // 容量 +} HashTable; + +Status CreateHash(HashTable *H, int size); // 构建Hash表 +bool Empty(HashTable H); // 判断Hash表是否为空 +bool Prime(int n); // 判断是否为素数 +int SearchHash(HashTable H, KeyType K, int *p, int *c); // 在Hash表中查找关键字为K的元素 +Status InsertHash(HashTable *H, ElemType e); // 往Hash表中插入新的元素 +bool EQ(KeyType a, KeyType b); // 判断关键字是否相等 +Status RecreateHashTable(HashTable *H); // 重建Hash表 +Status Print(HashTable H); // 输出Hash表 \ No newline at end of file diff --git "a/2017-1/SQ/\345\223\210\345\270\214\350\241\250/Hashmain.c" "b/2017-1/SQ/\345\223\210\345\270\214\350\241\250/Hashmain.c" new file mode 100644 index 00000000..83fc0111 --- /dev/null +++ "b/2017-1/SQ/\345\223\210\345\270\214\350\241\250/Hashmain.c" @@ -0,0 +1,43 @@ +#include "Hash.h" + +int main() +{ + int size, i; + int p, c; + HashTable H; + ElemType e; + + srand(time(NULL)); // 随机种子 + + do { + size = (int)rand() % 11 + 20; // 构建哈希表,大小在20~30之间 + } while (!Prime(size)); + + CreateHash(&H, size); + Print(H); + + p = c = 0; + printf("==========Searching==========\n"); + for (i = 0; i < H.sizeindex; i++) { + e.key = (int)rand() % 501; + printf("Search : %d\n", e.key); + if (SearchHash(H, e.key, &p, &c) == SUCCESS) { + printf("Find %d\n", e.key); + printf("The Element Is : %d->%d\n", H.elem[p].key, H.elem[p].val); + printf("--------------------------\n"); + } + else { + printf("Didn't Find %d\n", e.key); + printf("--------------------------\n"); + } + } + printf("Search Over\n\n"); + + printf("==========Rebuild HashTable==========\n"); + if (!RecreateHashTable(&H)) { + printf("Rebuild Over\n"); + Print(H); + } + + return 0; +} \ No newline at end of file diff --git "a/2017-1/SQ/\345\233\276\347\232\204\345\271\277\345\272\246\344\274\230\345\205\210\347\256\227\346\263\225/Graph.c" "b/2017-1/SQ/\345\233\276\347\232\204\345\271\277\345\272\246\344\274\230\345\205\210\347\256\227\346\263\225/Graph.c" new file mode 100644 index 00000000..51f66e4e --- /dev/null +++ "b/2017-1/SQ/\345\233\276\347\232\204\345\271\277\345\272\246\344\274\230\345\205\210\347\256\227\346\263\225/Graph.c" @@ -0,0 +1,156 @@ +#include +#include +#include "Graph.h" + +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)) { + return ERROR; + } + Q->front = Q->front->next; + *e = Q->front->data; + return OK; +}//出列 +bool QueueEmpty(LinkQueue Q) { + if (Q.front == Q.rear) { + return true; + } + return false; +}; //判断是否为空队列 + +int FirstAdjVex(Graph G, int i) { + int k; + for (k = 0; k < G.vexnum; k++) { + if (G.arcs[i][k].adj == 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) { + return k; + } + } + return -1; +}//返回下一个邻接顶点,没有的话返回-1 +Status CreateGraph(Graph *G) { + int i, j, k; + G->vexnum = 9; //顶点数 + G->arcnum = 12; //边数 + + for (i = 0; i < G->vexnum; i++) { + for (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); + + return OK; +}//构建已知图 +Status Add(Graph*G, int x, int y) { + //构造邻接矩阵,相邻则赋值为1 + if (x >= MAX_VERTEX_NUM || y >= MAX_VERTEX_NUM) { + return ERROR; + } + G->arcs[x][y].adj = G->arcs[y][x].adj = 1; + return OK; +}//为邻接矩阵赋值 +void BFSTraverse(Graph G, int a, int b) { + int u, v, w; + bool flag; + LinkQueue Q; //辅助队列 + for (v = 0; v < G.vexnum; ++v) { + visited[v] = FALSE; //初始化访问标志 + } + InitQueue(&Q); // 置空的辅助队列Q + EnQueue(&Q, a); //起点入列 + visited[a] = TRUE; + flag = false; + 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 (!visited[w]) { + EnQueue(&Q, w); + visited[w] = true; + } + } + if (flag) { + break; + } + } +}//广度优先遍历 +Status PrintFoot(LinkQueue Q, int start) { + int foot[MAX_VERTEX_NUM]; + int i; + QueuePtr p; + p = Q.rear; + 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; + int i, j; + + CreateGraph(&G); + + for (i = 0; i < 9; i++) { + for (j = 0; j < 9; j++) { + if (j != i) { + printf("%d<->%d:", i + 1, j + 1); + BFSTraverse(G, i, j); + printf("\n"); + } + } + } + + return 0; +} \ No newline at end of file diff --git "a/2017-1/SQ/\345\233\276\347\232\204\345\271\277\345\272\246\344\274\230\345\205\210\347\256\227\346\263\225/Graph.h" "b/2017-1/SQ/\345\233\276\347\232\204\345\271\277\345\272\246\344\274\230\345\205\210\347\256\227\346\263\225/Graph.h" new file mode 100644 index 00000000..dfb0afef --- /dev/null +++ "b/2017-1/SQ/\345\233\276\347\232\204\345\271\277\345\272\246\344\274\230\345\205\210\347\256\227\346\263\225/Graph.h" @@ -0,0 +1,56 @@ +#define MAXQSIZE 100 +#define FALSE 0 +#define TRUE 1 + +#define INFINITY INT_MAX +#define MAX_VERTEX_NUM 20 + +int visited[MAX_VERTEX_NUM]; + +typedef int VRType; +typedef int ElemType; + +typedef enum { + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum { + false, + true +}bool; + +typedef struct ArcCell { // 弧的定义 + VRType adj; // 用1或0表示相邻否; +} AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; + +typedef struct { // 图的定义 + AdjMatrix arcs; // 弧的信息 + int vexnum, arcnum; // 顶点数,弧数 +} Graph; + +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); //初始化队列 +Status EnQueue(LinkQueue *Q, ElemType e); //入列 +Status DeQueue(LinkQueue *Q, ElemType *e); //出列 +bool QueueEmpty(LinkQueue Q); //判断是否为空队列 + +//==========图的基本操作=============// +int FirstAdjVex(Graph G, int i); //返回第一个邻接顶点,没有的话返回-1 +int NextAdjVex(Graph G, int i, int j); //返回下一个邻接顶点,没有的话返回-1 +Status CreateGraph(Graph *G); //构建已知图 +Status Add(Graph*G, int x, int y); //为邻接矩阵赋值 +void BFSTraverse(Graph G, int a, int b); //广度优先遍历 +Status PrintFoot(LinkQueue Q, int start); //输出路径 \ No newline at end of file diff --git "a/2017-1/SQ/\345\233\276\347\232\204\345\271\277\345\272\246\344\274\230\345\205\210\347\256\227\346\263\225/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/SQ/\345\233\276\347\232\204\345\271\277\345\272\246\344\274\230\345\205\210\347\256\227\346\263\225/\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..28f4d8e0 Binary files /dev/null and "b/2017-1/SQ/\345\233\276\347\232\204\345\271\277\345\272\246\344\274\230\345\205\210\347\256\227\346\263\225/\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/SQ/\346\216\222\345\272\217/Sort.c" "b/2017-1/SQ/\346\216\222\345\272\217/Sort.c" new file mode 100644 index 00000000..a3a7463f --- /dev/null +++ "b/2017-1/SQ/\346\216\222\345\272\217/Sort.c" @@ -0,0 +1,365 @@ +#include "Sort.h" + +/* 对记录数组r做直接插入排序,length为数组的长度 */ +Status InsSort(RecordType **r, int length, int *compare, int *move) { + int i, j; + RecordType temp; + if (Empty(r)) { + return ERROR; + } + for (i = 1; i < length; i++) { + temp = (*r)[i]; /* 将待插入记录存放到临时变量中 */ + (*move)++; + j = i - 1; /* 最近一次排序结束的边界位置 */ + while (++(*compare) && temp.key < (*r)[j].key) { /* 寻找插入位置 */ + (*r)[j + 1] = (*r)[j]; /* 从小到大排序,大元素右移 */ + (*move)++; + j = j - 1; /* 待插入元素与已排序区下一个(左移一位)元素继续进行比较 */ + } + (*r)[j + 1] = temp; /* 将待插入记录插入到已排序的序列中 */ + (*move)++; + } + return OK; +} + +/* 对记录数组r做一趟希尔插入排序,length为数组的长度, delta 为增量 */ +Status ShellInsert(RecordType **r, int length, int delta, int *compare, int *move) { + int i, j; + RecordType temp; + if (Empty(r)) { + return ERROR; + } + for (i = delta; i < length; i++) { /* 1+delta为第一个子序列的第二个元素的下标 */ + if ((*r)[i].key < (*r)[i - delta].key && ++(*compare)) { + temp = (*r)[i]; /* 备份r[i] (不做监视哨) */ + (*move)++; + for (j = i - delta; (j >= i % delta) && temp.key < (*r)[j].key; j -= delta) { + (*r)[j + delta] = (*r)[j]; + (*move)++; + } + (*r)[j + delta] = temp; + (*move)++; + } + } + return OK; +} + +/* 对记录数组r做希尔排序, length为数组的长度*/ +Status ShellSort(RecordType **r, int length, int *compare, int *move) { + int d; + d = length / 2; + if (Empty(r)) { + return ERROR; + } + while (d >= 1) { + ShellInsert(r, length, d, compare, move); + d = d / 2; + } + return OK; +} + +/* 对记录数组r做冒泡排序,length为数组的长度 */ +Status BubbleSort(RecordType **r, int length, int *compare, int *move) { + int i, j, n; + RecordType temp; + bool change; + n = length; + change = TRUE; + if (Empty(r)) { + return ERROR; + } + for (i = 0; i < n && change; ++i) { + change = FALSE; + for (j = 0; j < n - i - 1; ++j) { + if ((*r)[j].key >(*r)[j + 1].key && ++(*compare)) { + temp = (*r)[j]; + (*r)[j] = (*r)[j + 1]; + (*r)[j + 1] = temp; + *move += 3; + change = TRUE; + } + } + if (!change) { + break; + } + } + return OK; +} + +/* 对记录数组r[low...high]用快速排序算法进行排序 */ +Status QKSort(RecordType *r, int low, int high, int *compare, int *move) { + int pivot; + if (low < high) { + pivot = QKPass(r, low, high, compare, move); + QKSort(r, low, pivot - 1, compare, move); + QKSort(r, pivot + 1, high, compare, move); + } + return OK; +} + +/* 对记录数组r 中的r[left]至r[right]部分进行一趟排序,并得到基准的位置,使得排序后的结果满足其之后(前)的记录的关键字均不小于(大于)于基准记录 */ +int QKPass(RecordType **r, int left, int right, int *compare, int *move) { + int low, high; + RecordType temp; + temp = (*r)[left]; /* 选择基准记录*/ + (*move)++; + low = left; + high = right; + while (low < high) { + while (low < high && (*r)[high].key >= temp.key && ++(*compare)) { /* high从右到左找小于x.key的记录 */ + high--; + } + (*r)[low] = (*r)[high]; /* 找到小于x.key的记录,则进行交换 */ + (*move)++; + while (low < high && (*r)[low].key <= temp.key && ++(*compare)) { /* low从左到右找不小于x.key的记录 */ + low++; + } + (*r)[high] = (*r)[low]; /* 找到不小于x.key的记录,则交换*/ + (*move)++; + } + (*r)[low] = temp; /* 将基准记录保存到low=high的位置 */ + *move++; + return low; /*返回基准记录的位置*/ +} + +/* 对记录数组r做简单选择排序,length为数组的长度 */ +Status SelectSort(RecordType **r, int length, int *compare, int *move) { + int i, j, k, n; + RecordType temp; + n = length; + for (i = 0; i < n; ++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 += 3; + } + } + return OK; +} + +/* 已知r[s..m]中记录的关键字除了r[s].key之外均满足堆的定义,本函数调整r[s]的关键字,使r[s..m]成为一个大顶堆(对其中记录的关键字而言) */ +Status HeapAdjust(RecordType **r, int s, int m, int *compare, int *move) { + RecordType rc; + int j; + if (Empty(r)) { + return ERROR; + } + for (; 2 * s + 1 < m; s = j) { + j = 2 * s + 1; + if (j < m - 1 && (*r)[j].key < (*r)[j + 1].key && ++(*compare)) { + ++j; + } + if ((*r)[s].key >= (*r)[j].key && ++(*compare)) { + break; + } + rc = (*r)[s]; + (*r)[s] = (*r)[j]; + + (*r)[j] = rc; + (*move) += 3; + } + return OK; +} + +/* 对记录数组r进行堆排序,length为数组的长度 */ +Status HeapSort(RecordType **r, int length, int *compare, int *move) { + int i; + RecordType temp; + if (Empty(r)) { + return ERROR; + } + for (i = length / 2; i >= 0; --i) { + HeapAdjust(r, i, length, compare, move); + } + for (i = length - 1; i > 0; --i) { + temp = (*r)[0]; + (*r)[0] = (*r)[i]; + (*r)[i] = temp; + (*move) += 3; + HeapAdjust(r, 0, i, compare, move); + } +} + +/* 将有序的r[i..m]和r[m+1..n]归并为有序的r1[i..n]中 */ +Status Merge(RecordType **r, RecordType **r1, int i, int m, int n, int *compare, int *move) { + int j, k; + int low, mid, high; + low = i, mid = m, high = n; + if (Empty(r)) { + return ERROR; + } + for (j = mid + 1, k = low; low <= mid && j <= high; ++k) { // 将r中记录由小到大地并入r1 + if ((*r)[low].key <= (*r)[j].key && ++(*compare)) { + (*r1)[k] = (*r)[low++]; + (*move)++; + } + else { + (*r1)[k] = (*r)[j++]; + (*move)++; + } + } + while (low <= mid) { // 将剩余的r[i..m]复制到r1 + (*r1)[k++] = (*r)[low++]; + (*move)++; + } + while (j <= high) { // 将剩余的r[j..n]复制到r1 + (*r1)[k++] = (*r)[j++]; + (*move)++; + } + return OK; +} + +/* 将有序的r[s..t]归并排序为r1[s..t] */ +Status MSort(RecordType **r, RecordType **r1, int s, int t, int *compare, int *move) { + int low, mid, high; + low = s, high = t; + RecordType *temp; + if (Empty(r)) { + return ERROR; + } + if (low == high) { + (*r1)[low] = (*r)[low]; + (*move)++; + } + else { + mid = (low + high) / 2; // 将r[low..high]平分为r1[low..mid]和r1[mid+1,high] + temp = (RecordType *)malloc(sizeof(RecordType) * (high + 1)); // 辅助空间, 测试发现大小要为high+1,否则溢出 + MSort(r, &temp, low, mid, compare, move); // 递归地将r[low..mid]归并为有序的temp[low,mid] + MSort(r, &temp, mid + 1, high, compare, move); // 递归地将r[mid+1..high]归并为有序的temp[mid+1,high] + Merge(&temp, r1, low, mid, high, compare, move); // 将temp[low..mid]和temp[mid+1,high]归并到r1[low,high] + free(temp); + } + return OK; +} + +/* 对记录数组r做归并排序,length为数组的长度 */ +Status MergeSort(RecordType **r, int length, int *compare, int *move) { + if (!MSort(r, r, 0, length - 1, compare, move)) { + return OK; + } + return ERROR; +} + +/* 判断记录数组r是否为空 */ +bool Empty(RecordType **r) { + if (!r || !(*r)) { + return TRUE; + } + return FALSE; +} + +/* 为记录数组r开辟空间,并用随机数填充数组,返回数组大小 */ +int CreateTest(RecordType **r) { + int n; + n = (int)rand() % 9 + 10; // 生成数组大小,范围在10~18之间 + *r = (RecordType *)malloc(sizeof(RecordType) * n); + if (Empty(r)) { + return 0; + } + for (int i = 0; i < n; i++) { + (*r)[i].key = (int)rand() % 100 + 1; // 产生1~100的随机数 + } + return n; +} + +/* 输出记录数组r中的元素 */ +Status PrintCase(RecordType **r, int n) { + if (Empty(r)) { + return ERROR; + } + for (int i = 0; i < n; i++) { + printf("%4d", (*r)[i]); + } + printf("\n"); + return OK; +} + +/* 输出记录数组r经过排序的总比较次数、总移动次数以及两者之和 */ +Status PrintNum(int *compare, int *move) { + // 输出比较次数、交换次数、总次数 + if (*compare == 0 || *move == 0) { + return ERROR; + } + printf("Compare times:%d\n", *compare); + printf("Move times:%d\n", *move); + printf("Total times:%d\n", *compare + *move); + return OK; +} + +/* 输出记录数组r中的元素,以及经过排序的总比较次数、总移动次数、两者之和 */ +Status Print(RecordType **r, int n, int *compare, int *move) { + if (PrintCase(r, n) && PrintNum(compare, move)) { + printf("\n"); + return OK; + } + return ERROR; +} + +/* 将记录数组p还原为原始的记录数组r,并将总比较次数、总移动次数清零 */ +Status Fresh(RecordType **r, int n, RecordType **p, int *compare, int *move) { + if (Empty(r)) { + return ERROR; + } + *p = (RecordType *)malloc(sizeof(RecordType) * n); + *compare = *move = 0; + for (int i = 0; i < n; i++) { + (*p)[i] = (*r)[i]; + } + return OK; +} + +/* 对记录数组r使用各种排序方法,并将结果输出 */ +Status Sort(RecordType **r, int n, int *compare, int *move) { + RecordType *temp; // 暂存数组 + + printf("==================== InsertSort ====================\n"); + Fresh(r, n, &temp, compare, move); + printf("Original order:"); PrintCase(&temp, n); + InsSort(&temp, n, compare, move); + printf("Sorted order:"); Print(&temp, n, compare, move); + + printf("==================== ShellSort ====================\n"); + free(temp); Fresh(r, n, &temp, compare, move); + printf("Original order:"); PrintCase(&temp, n); + ShellSort(&temp, n, compare, move); + printf("Sorted order:"); Print(&temp, n, compare, move); + + printf("==================== BubbleSort ====================\n"); + free(temp); Fresh(r, n, &temp, compare, move); + printf("Original order:"); PrintCase(&temp, n); + BubbleSort(&temp, n, compare, move); + printf("Sorted order:"); Print(&temp, n, compare, move); + + printf("==================== QuickSort ====================\n"); + free(temp); Fresh(r, n, &temp, compare, move); + printf("Original order:"); PrintCase(&temp, n); + QKSort(&temp, 0, n - 1, compare, move); + printf("Sorted order:"); Print(&temp, n, compare, move); + + printf("==================== SelectSort ====================\n"); + free(temp); Fresh(r, n, &temp, compare, move); + printf("Original order:"); PrintCase(&temp, n); + SelectSort(&temp, n, compare, move); + printf("Sorted order:"); Print(&temp, n, compare, move); + + printf("==================== HeapSort ====================\n"); + free(temp); Fresh(r, n, &temp, compare, move); + printf("Original order:"); PrintCase(&temp, n); + HeapSort(&temp, n, compare, move); + printf("Sorted order:"); Print(&temp, n, compare, move); + + printf("==================== MergeSort ====================\n"); + RecordType *r_temp; + r_temp = (RecordType *)malloc(sizeof(RecordType) * n); + free(temp); Fresh(r, n, &temp, compare, move); + printf("Original order:"); PrintCase(&temp, n); + MergeSort(&temp, n, compare, move); + printf("Sorted order:"); Print(&temp, n, compare, move); +} \ No newline at end of file diff --git "a/2017-1/SQ/\346\216\222\345\272\217/Sort.h" "b/2017-1/SQ/\346\216\222\345\272\217/Sort.h" new file mode 100644 index 00000000..0321f1dc --- /dev/null +++ "b/2017-1/SQ/\346\216\222\345\272\217/Sort.h" @@ -0,0 +1,56 @@ +#include +#include +#include + +typedef int KeyType; // 关键字类型 + +typedef enum { // 函数返回值 + ERROR, + OK +}Status; + +typedef enum { // 定义布尔函数值 + FALSE, + TRUE +}bool; + +typedef struct RecordType { // 记录结构 + KeyType key; +} RecordType; + +//==================直接插入排序==================// +Status InsSort(RecordType **r, int length, int *compare, int *move); + +//==================希尔排序==================// +Status ShellInsert(RecordType **r, int length, int delta, int *compare, int *move); +Status ShellSort(RecordType **r, int length, int *compare, int *move); + +//==================冒泡排序==================// +Status BubbleSort(RecordType **r, int length, int *compare, int *move); + +//==================快速排序==================// +Status QKSort(RecordType *r, int low, int high, int *compare, int *move); +int QKPass(RecordType **r, int left, int right, int *compare, int *move); + +//==================简单选择排序==================// +Status SelectSort(RecordType **r, int length, int *compare, int *move); + +//==================堆排序==================// +Status HeapAdjust(RecordType **r, int s, int m, int *compare, int *move); +Status HeapSort(RecordType **r, int length, int *compare, int *move); + +//==================归并排序==================// +Status Merge(RecordType **r, RecordType **r1, int i, int m, int n, int *compare, int *move); +Status MSort(RecordType **r, RecordType **r1, int s, int t, int *compare, int *move); +Status MergeSort(RecordType **r, int length, int *compare, int *move); + +//==================对数组的操作==================// +bool Empty(RecordType **r); +int CreateTest(RecordType **r); +Status Fresh(RecordType **r, int n, RecordType **p, int *compare, int *move); +Status Sort(RecordType **r, int n, int *compare, int *move); + +//==================输出函数==================// +Status PrintCase(RecordType **r, int n); +Status PrintNum(int *compare, int *move); +Status Print(RecordType **r, int n, int *compare, int *move); \ No newline at end of file diff --git "a/2017-1/SQ/\346\216\222\345\272\217/main.c" "b/2017-1/SQ/\346\216\222\345\272\217/main.c" new file mode 100644 index 00000000..34faf4fb --- /dev/null +++ "b/2017-1/SQ/\346\216\222\345\272\217/main.c" @@ -0,0 +1,18 @@ +#include "Sort.h" + +int main() +{ + int compare, move, length; + RecordType *test; // 待开辟的数组 + + srand(time(NULL)); // 随机种子 + + if (!(length = CreateTest(&test))) { + return 1; // 开辟空间失败 + } + + compare = move = 0; // 初始化比较次数、移动次数 + Sort(&test, length, &compare, &move); // 调用各种排序方法 + + return 0; +} \ No newline at end of file diff --git "a/2017-1/SQ/\346\216\222\345\272\217/\350\277\220\350\241\214\347\244\272\344\276\213.png" "b/2017-1/SQ/\346\216\222\345\272\217/\350\277\220\350\241\214\347\244\272\344\276\213.png" new file mode 100644 index 00000000..9a3e3878 Binary files /dev/null and "b/2017-1/SQ/\346\216\222\345\272\217/\350\277\220\350\241\214\347\244\272\344\276\213.png" differ diff --git "a/2017-1/SQ/\346\237\245\346\211\276\350\241\250/AVLOutput.txt" "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/AVLOutput.txt" new file mode 100644 index 00000000..4e92b068 --- /dev/null +++ "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/AVLOutput.txt" @@ -0,0 +1,6 @@ +8, 4, 3, 1, 6, 5, 7, 14, 10, 22, 19, 30 +8, 4, 3, 1, 6, 5, 7, 14, 10, 13, 22, 19, 30 +7, 4, 3, 1, 6, 5, 14, 10, 13, 22, 19, 30 +7, 4, 3, 1, 6, 14, 10, 13, 22, 19, 30 +7, 4, 3, 1, 6, 14, 10, 13, 22, 19, 20, 30 +14, 7, 3, 1, 4, 10, 13, 22, 19, 20, 30 diff --git "a/2017-1/SQ/\346\237\245\346\211\276\350\241\250/AVLTree.c" "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/AVLTree.c" new file mode 100644 index 00000000..3a5c4c96 --- /dev/null +++ "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/AVLTree.c" @@ -0,0 +1,336 @@ +#include "AVLTree.h" + +Status CreateAVLT(AVLTree *T, ElemType d[], int n) { + // 构造二叉平衡树 + int i; + bool taller = false; + for (i = 0; i < n; i++) { + InsertAVLT(T, d[i], &taller); + } + if (!T) { + return OK; + } + return ERROR; +} + +Status SearchAVLT(AVLTree *T, KeyType kval, AVLTree f, AVLTree *p) { + // 在根指针 T 所指二叉树中递归地查找其关键字等于 kval 的数据元素 + // 若查找成功,则返回指针 p 指向该数据元素的结点,并返回函数值为 true + // 否则表明查找不成功,返回指针 p 指向查找路径上访问的最后一个结点,并返回函数值为false + // 指针 f 指向当前访问的结点的双亲,其初始调用值为NULL + if (!*T) { // 空树,查找不成功 + *p = f; + return ERROR; + } + else if (EQ(kval, (*T)->key)) { + *p = *T; + return OK; // 查找成功 + } + else if (LT(kval, (*T)->key)) { // 在左子树中继续查找 + return SearchAVLT(&(*T)->lchild, kval, *T, p); + } + else { // 在右子树中继续查找 + return SearchAVLT(&(*T)->rchild, kval, *T, p); + } +} + +bool EQ(KeyType a, KeyType b) { + // 若a与b相等,返回true,否则返回false + if (a == b) { + return true; + } + return false; +} + +bool LT(KeyType a, KeyType b) { + // 若a小于b,返回true,否则返回false + if (a < b) { + return true; + } + return false; +} + +Status InsertAVLT(AVLTree *T, ElemType e, bool *taller) { + // 若再平衡的二叉排序树T中不存在和e相等的结点,则插入一个数据元素为e的新结点,并返回成功 + // 若因插入而使二叉排序树失去平衡,则作平衡旋转处理,布尔变量taller反应T长高与否 + if (!(*T)) { + *T = (AVLTree)malloc(sizeof(AVLTNode)); + (*T)->key = e; + (*T)->lchild = (*T)->rchild = NULL; + (*T)->bf = EH; + *taller = true; + } + else { + if (EQ(e, (*T)->key)) { + *taller = false; + return ERROR; + } + if (LT(e, (*T)->key)) { + if (InsertAVLT(&(*T)->lchild, e, taller)) { + return ERROR; + } + if (*taller) { + switch ((*T)->bf) { + case LH: + LeftBalance(T); + *taller = false; + break; + case EH: + (*T)->bf = LH; + *taller = true; + break; + case RH: + (*T)->bf = EH; + *taller = false; + break; + } + } + } + else { + if (InsertAVLT(&(*T)->rchild, e, taller)) { + return ERROR; + } + if (*taller) { + switch ((*T)->bf) { + case LH: + (*T)->bf = EH; + *taller = false; + break; + case EH: + (*T)->bf = RH; + *taller = true; + break; + case RH: + RightBalance(T); + *taller = false; + break; + } + } + } + } + return OK; +} + +Status L_Rotate(AVLTree *T) { + // 对以指针 *T 为根的二叉排序树作左旋处理,处理之后 T 指向新的根节点 + AVLTree rc; + rc = (*T)->rchild; + (*T)->rchild = rc->lchild; + rc->lchild = (*T); + (*T) = rc; + return OK; +} + +Status R_Rotate(AVLTree *T) { + // 对以指针 *T 为根的二叉排序树作右旋处理,处理之后 T 指向新的根节点 + AVLTree lc; + lc = (*T)->lchild; + (*T)->lchild = lc->rchild; + lc->rchild = (*T); + (*T) = lc; + return OK; +} + +Status LeftBalance(AVLTree *T) { + // 对以指针T所指结点为根的二叉树作左平衡旋转处理,算法结束时T指向新的根节点 + AVLTree lc, rc; + lc = (*T)->lchild; + rc = NULL; + if (lc) { + switch (lc->bf) { + case LH: + (*T)->bf = lc->bf = EH; + R_Rotate(T); + break; + case RH: + rc = lc->rchild; + switch (rc->bf) { + case LH: + (*T)->bf = RH; + lc->bf = EH; + break; + case EH: + (*T)->bf = lc->bf = EH; + break; + case RH: + (*T)->bf = EH; + lc->bf = LH; + break; + } + rc->bf = EH; + L_Rotate(&(*T)->lchild); + R_Rotate(T); + break; + } + return OK; + } + return ERROR; +} + +Status RightBalance(AVLTree *T) { + // 对以指针T所指结点为根的二叉树作右平衡旋转处理,算法结束时T指向新的根节点 + AVLTree lc, rc; + rc = (*T)->rchild; + lc = NULL; + if (rc) { + switch (rc->bf) { + case RH: + (*T)->bf = rc->bf = EH; + L_Rotate(T); + break; + case LH: + lc = rc->lchild; + switch (lc->bf) { + case LH: + (*T)->bf = EH; + rc->bf = RH; + break; + case EH: + (*T)->bf = rc->bf = EH; + break; + case RH: + (*T)->bf = LH; + rc->bf = EH; + break; + } + lc->bf = EH; + R_Rotate(&(*T)->rchild); + L_Rotate(T); + break; + } + return OK; + } + return ERROR; +} + +Status PreOrderTraverse(AVLTree T, FILE *p, Status(*Visit)(ElemType)) { + // 先序遍历二叉树,对每个数据元素调用函数Visit + if (T) { + fprintf(p, ", "); + Visit(T->key, p); + PreOrderTraverse(T->lchild, p, Visit); + PreOrderTraverse(T->rchild, p, Visit); + return OK; + } + return ERROR; +} + +Status Visit(ElemType e, FILE *p) { + // 输出元素 + fprintf(p, "%d", e); + return OK; +} + +Status PrintTree(AVLTree T, FILE *p) { + // 打印输出整棵二叉树 + Visit(T->key, p); + PreOrderTraverse(T->lchild, p, Visit); + PreOrderTraverse(T->rchild, p, Visit); + fprintf(p, "\n"); +} + +//===============以下参考代码(删除操作)=============// +// 参数说明:*T为待进行调整的子树的根结点 +// bfChild为*T在删除结点前的平衡因子 +Status DelLeftCase(AVLTree *T, int bfChild) +{ + // 当bf为-1或1变为0,或者孩子为空时说明子树高降低 + if ((!(*T)->lchild) || (EH != bfChild && EH == (*T)->lchild->bf)) { + switch ((*T)->bf) { // 左子树树高降低 + case EH: + (*T)->bf = RH; + break; + case LH: + (*T)->bf = EH; + break; + case RH: // 原本右子树比较高 + RightBalance(T); + break; + } + return OK; + } + else { + return ERROR; + } +} + +Status DelRightCase(AVLTree *T, int bfChild) +{ + // 当bf为LH或RH变为EH,或者孩子为空时说明子树高降低 + if ((!(*T)->rchild) || (EH != bfChild && EH == (*T)->rchild->bf)) { + switch ((*T)->bf) { // 右子树树高降低 + case EH: + (*T)->bf = LH; + break; + case RH: + (*T)->bf = EH; + break; + case LH: //原本左子树比较高 + LeftBalance(T); + break; + } + return OK; + } + else { + return ERROR; + } +} + +AVLTree DeleteNode(AVLTree *T, KeyType key) +{ + // 递归找到元素值为key的元素,删除该元素,调整当前子树的平衡 + // 跳出递归时,依次利用递归调整子树的平衡直至根节点 + int bfChild = 0; + if (*T) { + if (LT(key, (*T)->key)) { // 当前元素大于待删除元素 + bfChild = (*T)->lchild->bf; + (*T)->lchild = DeleteNode(&(*T)->lchild, key); // 继续在左子树中进行查找 + DelLeftCase(T, bfChild); + } + else if (LT((*T)->key, key)) { // 当前元素小于待删除元素 + bfChild = (*T)->rchild->bf; + (*T)->rchild = DeleteNode(&(*T)->rchild, key); // 继续在右子树中进行查找 + DelRightCase(T, bfChild); + } + else // 当前节点就是要删除的节点 + { + if ((*T)->lchild) // *T不是叶子结点并且具有直接前驱 + { + AVLTree farRight, temp; + // AVLTree farRight = GofarRight((*T)->lchild); + // 根据http://www.cs.usfca.edu/~galles/visualization/RedBlack.html + // 人工输入数据,看动态图的演示推测出farRight的指向 + temp = (*T)->lchild->rchild; + while (temp) { + farRight = temp; + temp = temp->rchild; + } + (*T)->key = farRight->key; // 交换元素值 + // 可以确定,删除的节点为当前节点的左子树的某一个节点 + (*T)->lchild = DeleteNode(&(*T)->lchild, farRight->key); // 删除元素值为key的节点 + DelLeftCase(T, bfChild); // 左子树平衡调整 + } + else if ((*T)->rchild) // *T不是叶子结点并且具有直接后驱 + { + AVLTree farLeft, temp; + // AVLTree farLeft = GofarLeft((*T)->rchild); + // 根据http://www.cs.usfca.edu/~galles/visualization/RedBlack.html + // 人工输入数据,看动态图的演示推测出farLeft的指向 + temp = (*T)->rchild->lchild; + while(temp){ + farLeft = temp; + temp = temp->lchild; + } + (*T)->key = farLeft->key; // 交换元素值 + (*T)->rchild = DeleteNode(&(*T)->rchild, farLeft->key); // 删除元素值为key的节点 + DelRightCase(T, bfChild); // 右子树平衡调整 + } + else // *T是叶子结点 + { + free(*T); + *T = NULL; + } + } + } + return (*T); // 包含了返回NULL与正常的当前节点 +} \ No newline at end of file diff --git "a/2017-1/SQ/\346\237\245\346\211\276\350\241\250/AVLTree.h" "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/AVLTree.h" new file mode 100644 index 00000000..f81eeae4 --- /dev/null +++ "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/AVLTree.h" @@ -0,0 +1,42 @@ +#include +#include + +#define LH +1 // 左高 +#define EH 0 // 等高 +#define RH -1 // 右高 + +typedef int KeyType; // 关键字类型 +typedef int ElemType; + +typedef enum { // 函数返回值 + OK, + ERROR +}Status; + +typedef enum { // 定义布尔函数值 + false, + true +}bool; + +typedef struct AVLTNode { // 结点结构 + int bf; //平衡因子 + KeyType key; + struct AVLTNode *lchild, *rchild; // 左右孩子指针 +} AVLTNode, *AVLTree; + +Status CreateAVLT(AVLTree *T, ElemType d[], int n);// 构造一棵二叉平衡树 +Status SearchAVLT(AVLTree *T, KeyType kval, AVLTree f, AVLTree *p);// 在树中寻找关键字为kval的元素 +bool EQ(KeyType a, KeyType b);// 相等判断 +bool LT(KeyType a, KeyType b);// 小于判断 +Status InsertAVLT(AVLTree *T, ElemType e, bool *taller);// 向树中插入元素 +Status L_Rotate(AVLTree *T); // 左旋 +Status R_Rotate(AVLTree *T); // 右旋 +Status LeftBalance(AVLTree *T); // 左平衡旋转处理 +Status RightBalance(AVLTree *T); // 右平衡旋转处理 +Status PreOrderTraverse(AVLTree T, FILE *p, Status(*Visit)(ElemType)); // 先序遍历 +Status Visit(ElemType e, FILE *p); // 输出树中的元素 +Status PrintTree(AVLTree T, FILE *p); // 输出整棵树 + +AVLTree DeleteNode(AVLTree *T, KeyType key); // 删除树中关键字为kval的元素 +Status DelLeftCase(AVLTree *T, int bfChild); // 删除左子树结点 +Status DelRightCase(AVLTree *T, int bfChild); // 删除右子树结点 \ No newline at end of file diff --git "a/2017-1/SQ/\346\237\245\346\211\276\350\241\250/BSTOutput.txt" "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/BSTOutput.txt" new file mode 100644 index 00000000..e9730024 --- /dev/null +++ "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/BSTOutput.txt" @@ -0,0 +1,6 @@ +8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30 +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30 +7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30 diff --git "a/2017-1/SQ/\346\237\245\346\211\276\350\241\250/BSTree.c" "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/BSTree.c" new file mode 100644 index 00000000..912aca36 --- /dev/null +++ "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/BSTree.c" @@ -0,0 +1,152 @@ +#include "BSTree.h" + +Status CreateBST(BSTree *T, ElemType d[], int n) { + //构造二叉排序树 + int i; + for (i = 0; i < n; i++) { + InsertBST(T, d[i]); + } + if (!T) { + return OK; + } + return ERROR; +} + +Status InsertBST(BSTree *T, ElemType e) { + // 当二叉排序树中不存在关键字等于 e 的数据元素时,插入元素值为 e 的结点,并返回 true + // 否则,不进行插入并返回false + BSTree p, s; + if (SearchBST(T, e, NULL, &p)) { + s = (BSTree)malloc(sizeof(BSTNode)); // 为新结点分配空间 + s->key = e; + s->lchild = s->rchild = NULL; + if (!p) { + *T = s; // 插入 s 为新的根结点 + } + else if (LT(e, p->key)) { + p->lchild = s; // 插入 *s 为 *p 的左孩子 + } + else { + p->rchild = s; // 插入 *s 为 *p 的右孩子 + } + return OK; + } + else { + return ERROR; + } +} + +Status SearchBST(BSTree *T, KeyType kval, BSTree f, BSTree *p) { + // 在根指针 T 所指二叉排序树中递归地查找其关键字等于 kval 的数据元素 + // 若查找成功,则返回指针 p 指向该数据元素的结点,并返回函数值为 true + // 否则表明查找不成功,返回指针 p 指向查找路径上访问的最后一个结点,并返回函数值为false + // 指针 f 指向当前访问的结点的双亲,其初始调用值为NULL + if (!*T) { // 空树,查找不成功 + *p = f; + return ERROR; + } + else if (EQ(kval,(*T)->key)) { + *p = *T; + return OK; // 查找成功 + } + else if (LT(kval, (*T)->key)) { // 在左子树中继续查找 + return SearchBST(&(*T)->lchild, kval, *T, p); + } + else { // 在右子树中继续查找 + return SearchBST(&(*T)->rchild, kval, *T, p); + } +} + +bool EQ(KeyType a, KeyType b) { + // 若a与b相等,返回true,否则返回false + if (a == b) { + return true; + } + return false; +} + +bool LT(KeyType a, KeyType b) { + // 若a小于b,返回true,否则返回false + if (a < b) { + return true; + } + return false; +} + +Status DeleteBST(BSTree *T, KeyType kval) { + // 若二叉排序树 T 中存在其关键字等于 kval 的数据元素,则删除该数据元素结点, + // 并返回函数值 OK,否则返回函数值 ERROR + if (!(*T)) { // 不存在关键字等于kval的数据元素 + return ERROR; + } + else { + if (EQ(kval, (*T)->key)) { // 找到关键字等于key的数据元素 + Delete(T); + return OK; + } + else if (LT(kval, (*T)->key)) { // 继续在左子树中进行查找 + return DeleteBST(&(*T)->lchild, kval); + } + else { // 继续在右子树中进行查找 + return DeleteBST(&(*T)->rchild, kval); + } + } +} + +Status Delete(BSTree *p) { + // 从二叉排序树中删除结点 p,并重接它的左子树或右子树 + BSTree q, s; + if (!(*p)->rchild) { // 右子树为空树则只需重接它的左子树 + q = *p; + *p = (*p)->lchild; + free(q); + } + else if (!(*p)->lchild) { // 左子树为空树则只需重接它的右子树 + q = *p; + *p = (*p)->rchild; + free(q); + } + else { // 左右子树均不空 + q = *p; + s = (*p)->lchild; + while (s->rchild) { // s 指向被删结点的前驱 + q = s; + s = s->rchild; + } + (*p)->key = s->key; + if (q != *p) { + q->rchild = s->lchild; // 重接*q的右子树 + } + else { + q->lchild = s->lchild; // 重接*q的左子树 + } + free(s); + } + return OK; +} + +Status PreOrderTraverse(BSTree T, FILE *p, Status(*Visit)(ElemType)) { + //先序遍历二叉树,对每个数据元素调用函数Visit + if (T) { + fprintf(p,", "); + Visit(T->key,p); + PreOrderTraverse(T->lchild, p,Visit); + PreOrderTraverse(T->rchild, p,Visit); + return OK; + } + return ERROR; +} + +Status Visit(ElemType e,FILE *p) { + // 输出元素 + fprintf(p,"%d", e); + return OK; +} + +Status PrintTree(BSTree T,FILE *p) { + // 打印输出整棵二叉树 + Visit(T->key,p); + PreOrderTraverse(T->lchild, p,Visit); + PreOrderTraverse(T->rchild, p,Visit); + fprintf(p,"\n"); +} \ No newline at end of file diff --git "a/2017-1/SQ/\346\237\245\346\211\276\350\241\250/BSTree.h" "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/BSTree.h" new file mode 100644 index 00000000..1564f556 --- /dev/null +++ "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/BSTree.h" @@ -0,0 +1,31 @@ +#include +#include + +typedef int KeyType; // 关键字类型 +typedef int ElemType; + +typedef enum { // 函数返回值 + OK, + ERROR +}Status; + +typedef enum { // 定义布尔函数值 + false, + true +}bool; + +typedef struct BSTNode { // 结点结构 + KeyType key; + struct BSTNode *lchild, *rchild; // 左右孩子指针 +} BSTNode, *BSTree; + +Status CreateBST(BSTree *T, ElemType d[], int n);// 构造一棵二叉排序树 +Status SearchBST(BSTree T, KeyType kval, BSTree f, BSTree *p);// 在树中寻找关键字为kval的元素 +Status InsertBST(BSTree *T, ElemType e);// 向树中插入元素 +bool EQ(KeyType a, KeyType b);// 相等判断 +bool LT(KeyType a, KeyType b);// 小于判断 +Status DeleteBST(BSTree *T, KeyType kval);// 删除树中关键字为kval的元素 +Status Delete(BSTree *p);// 删除的具体操作 +Status PreOrderTraverse(BSTree T, Status(*Visit)(ElemType));// 先序遍历 +Status Visit(ElemType e);// 输出树中的元素 +Status PrintTree(BSTree T);// 输出整棵树 \ No newline at end of file diff --git "a/2017-1/SQ/\346\237\245\346\211\276\350\241\250/Test.c" "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/Test.c" new file mode 100644 index 00000000..9ffbf76b --- /dev/null +++ "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/Test.c" @@ -0,0 +1,33 @@ +#include "BSTree.h" + +int main(){ + ElemType test1[] = { 8,10,14,3,1,6,4,7,5,19,22,30 }; + KeyType Key[] = { 13,8,5,20,6 }; + int elem_number, search_number; + int i; + + elem_number = sizeof(test1) / sizeof(ElemType); + search_number = sizeof(Key) / sizeof(KeyType); + + BSTree Tree = NULL; + BSTree temp = NULL; + FILE *p; + + p = fopen("D:\\BaiduYunDownload\\BSTOutput.txt", "w+"); + + CreateBST(&Tree, test1, elem_number); + PrintTree(Tree,p); + + for (i = 0; i < search_number; i++) { + if (!SearchBST(&Tree, Key[i], NULL, &temp)) { + DeleteBST(&Tree, Key[i]); + PrintTree(Tree,p); + } + else { + InsertBST(&Tree, Key[i]); + PrintTree(Tree,p); + } + } + + return 0; +} \ No newline at end of file diff --git "a/2017-1/SQ/\346\237\245\346\211\276\350\241\250/Test2.c" "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/Test2.c" new file mode 100644 index 00000000..69566ced --- /dev/null +++ "b/2017-1/SQ/\346\237\245\346\211\276\350\241\250/Test2.c" @@ -0,0 +1,35 @@ +#include "AVLTree.h" + +int main() { + ElemType test1[] = { 8,10,14,3,1,6,4,7,5,19,22,30 }; // 原始数据 + KeyType Key[] = { 13,8,5,20,6 }; // 待查找数据 + int elem_number, search_number; + int i; // 计数用 + bool taller = false; + + elem_number = sizeof(test1) / sizeof(ElemType); // 计算平衡树中将会有的元素个数 + search_number = sizeof(Key) / sizeof(KeyType); // 计算待查找元素的个数 + + AVLTree Tree = NULL; + AVLTree temp = NULL; + FILE *p; + + p = fopen("D:\\BaiduYunDownload\\AVLOutput.txt", "w+"); + + CreateAVLT(&Tree, test1, elem_number); // 构建一棵平衡树 + PrintTree(Tree, p); // 打印 + + for (i = 0; i < search_number; i++) { + // 依次查找,若找到值为key[i]的结点,删除并打印 + // 若没找到,则将该元素插入平衡树 + if (!SearchAVLT(&Tree, Key[i], NULL, &temp)) { + DeleteNode(&Tree, Key[i]); + PrintTree(Tree, p); + } + else { + InsertAVLT(&Tree, Key[i], &taller); + PrintTree(Tree, p); + } + } + return 0; +} diff --git "a/2017-1/SQ/\347\256\227\346\263\2252.12/2.12.h" "b/2017-1/SQ/\347\256\227\346\263\2252.12/2.12.h" new file mode 100644 index 00000000..627bd5bb --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2252.12/2.12.h" @@ -0,0 +1,17 @@ +#define OK 0 +#define ERROR 1 +#define OVERFLOW 2 + +typedef int ElemType; + +typedef struct LNode { + ElemType data; + struct LNode *next; +}LNode, *LinkList; + + +void CreateList_L(LinkList L); + +void MergeList_L(LinkList La, LinkList Lb, LinkList *Lc); + +void PrintList(const LinkList L); diff --git "a/2017-1/SQ/\347\256\227\346\263\2252.12/2.12\346\211\213\345\212\250\350\276\223\345\205\245\346\225\260\346\215\256 \350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/SQ/\347\256\227\346\263\2252.12/2.12\346\211\213\345\212\250\350\276\223\345\205\245\346\225\260\346\215\256 \350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..0cb1f154 Binary files /dev/null and "b/2017-1/SQ/\347\256\227\346\263\2252.12/2.12\346\211\213\345\212\250\350\276\223\345\205\245\346\225\260\346\215\256 \350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/SQ/\347\256\227\346\263\2252.12/2.12\346\211\213\345\212\250\350\276\223\345\205\245\346\225\260\346\215\256.c" "b/2017-1/SQ/\347\256\227\346\263\2252.12/2.12\346\211\213\345\212\250\350\276\223\345\205\245\346\225\260\346\215\256.c" new file mode 100644 index 00000000..86e2d9ff --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2252.12/2.12\346\211\213\345\212\250\350\276\223\345\205\245\346\225\260\346\215\256.c" @@ -0,0 +1,76 @@ +#include +#include + +#include "2.12.h" + +void CreateList_L(LinkList L) { //逆序输入,否则输出为逆序 + int i, n; + LinkList p; + scanf("%d", &n); + printf("输入链表元素: "); + L->next = NULL; + for (i = n; i > 0; --i) { + p = (LinkList)malloc(sizeof(LNode)); + scanf("%d", &p->data); + p->next = L->next; + L->next = p; + } + printf("当前链表构造完成\n\n"); +} + +void MergeList_L(LinkList La, LinkList Lb, LinkList *Lc) { //归并两个链表为新链表LC + LinkList pa, pb, pc; + pa = La->next; + pb = Lb->next; + (*Lc) = pc = La; + while (pa && pb) { + if (pa->data <= pb->data) { + pc->next = pa; + pc = pa; + pa = pa->next; + } + else { + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + pc->next = pa ? pa : pb; + free(Lb); +} + +void PrintList(const LinkList L) { //遍历并输出元素 + LinkList p; + p = L->next; + while (p) + { + printf("%2d ", p->data); + p = p->next; + } + printf("\n"); +} + +int main() +{ + LinkList La, Lb, Lc; + La = (LinkList)malloc(sizeof(LNode)); + Lb = (LinkList)malloc(sizeof(LNode)); + Lc = (LinkList)malloc(sizeof(LNode)); + + printf("输入链表1的大小: "); + CreateList_L(La); + + printf("输入链表2的大小: "); + CreateList_L(Lb); + + printf("链表一: "); + PrintList(La); + + printf("链表二: "); + PrintList(Lb); + + printf("归并两个链表:"); + MergeList_L(La, Lb, &Lc); + PrintList(Lc); + +} \ No newline at end of file diff --git "a/2017-1/SQ/\347\256\227\346\263\2252.12/2.12\351\232\217\346\234\272\351\223\276\350\241\250 \350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/SQ/\347\256\227\346\263\2252.12/2.12\351\232\217\346\234\272\351\223\276\350\241\250 \350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..48c3b048 Binary files /dev/null and "b/2017-1/SQ/\347\256\227\346\263\2252.12/2.12\351\232\217\346\234\272\351\223\276\350\241\250 \350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/SQ/\347\256\227\346\263\2252.12/2.12\351\232\217\346\234\272\351\223\276\350\241\250.c" "b/2017-1/SQ/\347\256\227\346\263\2252.12/2.12\351\232\217\346\234\272\351\223\276\350\241\250.c" new file mode 100644 index 00000000..0a52fbd2 --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2252.12/2.12\351\232\217\346\234\272\351\223\276\350\241\250.c" @@ -0,0 +1,91 @@ +#include +#include +#include + +#include "2.12.h" + +void CreateList_L(LinkList L) { //构建长度为n的随机链表 + int i, n, temp1, temp2; + LinkList p; + scanf("%d", &n); + L->next = NULL; + for (i = n; i > 0; --i) { + p = (LinkList)malloc(sizeof(LNode)); + + if (i == n){ + p->data = (int)rand() % 1024; + temp1 = p->data; + } + else + { + temp2 = (int)rand() % 1024; + while (temp2 > temp1) { + temp2 = (int)rand() % 1024; + } + temp1 = temp2; + p->data = temp2; + } + p->next = L->next; + L->next = p; + } + printf("当前链表构造完成\n\n"); +} + +void MergeList_L(LinkList La, LinkList Lb, LinkList *Lc) { //归并两个链表为新链表LC + LinkList pa, pb, pc; + pa = La->next; + pb = Lb->next; + *Lc = pc = La; + while (pa && pb) { + if (pa->data <= pb->data) { + pc->next = pa; + pc = pa; + pa = pa->next; + } + else { + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + pc->next = pa ? pa : pb; + free(Lb); +} + +void PrintList(const LinkList L) { //遍历并输出元素 + LinkList p; + p = L->next; + while (p) + { + printf("%2d ", p->data); + p = p->next; + } + printf("\n"); +} + +int main() +{ + LinkList La, Lb, Lc; + La = (LinkList)malloc(sizeof(LNode)); + Lb = (LinkList)malloc(sizeof(LNode)); + Lc = (LinkList)malloc(sizeof(LNode)); + + srand(time(NULL)); + + printf("输入链表1的大小: "); + CreateList_L(La); + + printf("输入链表2的大小: "); + CreateList_L(Lb); + + printf("链表一: "); + PrintList(La); + + printf("链表二: "); + PrintList(Lb); + + printf("归并两个链表:"); + MergeList_L(La, Lb, &Lc); + PrintList(Lc); + +} \ No newline at end of file diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.1.c" "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.1.c" new file mode 100644 index 00000000..dc6c9d3a --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.1.c" @@ -0,0 +1,86 @@ +//小于十进制转换 +#include +#include +#include +#include "3.2.1.h" + +Status InitStack(SqStack *S) { //构造空栈 + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!(S->base)) { + return OVERFLOW; + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +}; +Status DestoryStack(SqStack *S) { //销毁栈 + SqStack *p; + if (StackEmpty(*S)) { + return ERROR; + } + free(S); + S->base = NULL; + S->top = NULL; + S->stacksize = 0; + return OK; +}; +Status StackEmpty(SqStack S) { //判断是否为空栈 + if (S.top == S.base) { + return true; + } + return false; +}; +Status Push(SqStack *S, SElemType e) { //压栈(插入元素e为新的栈顶元素) + if ((S->top) - (S->base) >= (S->stacksize)) { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if (!(S->base)) { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *(S->top)++ = e; + return OK; +}; +Status Pop(SqStack *S, SElemType *e) { //出栈(删除栈顶元素,并用e返回其值) + if (StackEmpty(*S)) { + return ERROR; + } + *e = *--(S->top); + return OK; +}; + +Status conversation(int d) { + SqStack S; + SElemType N, e; + if (d > 10) { + printf("ERROR! Please input a number (<=10)\n"); + return ERROR; + } + if (InitStack(&S)) { //构造空栈 + printf("Initialize stack failed.\n"); + return ERROR; + }; + N = (int)rand() % 1024; //产生随机数 + printf("\n整数: %d\n", N); + while (N) { + Push(&S, N % d); + N = N / d; + } + printf("对应%d进制数: ",d); + while (!StackEmpty(S)) { + Pop(&S, &e); + printf("%d", e); + } + printf("\n"); + DestoryStack(&S); +} + +int main() { + srand(time(NULL)); + + conversation(8); + conversation(2); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.1.h" "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.1.h" new file mode 100644 index 00000000..f045c48d --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.1.h" @@ -0,0 +1,31 @@ +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef int SElemType; + +typedef enum { + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum { + false, + true +}bool; + +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack(SqStack *S); +Status DestoryStack(SqStack *S); +Status ClearStack(SqStack *S); +bool StackEmpty(SqStack S); +int StackLength(SqStack S); +Status GetTop(SqStack S, SElemType *e); +Status Push(SqStack *S, SElemType e); +Status Pop(SqStack *S, SElemType *e); +Status StackTraverse(SqStack S, Status(*visit)()); diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.1\351\232\217\346\234\272\346\225\260\350\277\220\350\241\214\347\273\223\346\236\234\347\244\272\344\276\213.png" "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.1\351\232\217\346\234\272\346\225\260\350\277\220\350\241\214\347\273\223\346\236\234\347\244\272\344\276\213.png" new file mode 100644 index 00000000..f7462a2c Binary files /dev/null and "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.1\351\232\217\346\234\272\346\225\260\350\277\220\350\241\214\347\273\223\346\236\234\347\244\272\344\276\213.png" differ diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.2.c" "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.2.c" new file mode 100644 index 00000000..82287657 --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.2.c" @@ -0,0 +1,172 @@ +//括号匹配检验 +#include +#include +#include "3.2.2and3.2.3.h" + +Status InitStack(SqStack *S) { //构造空栈 + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!(S->base)) { + return OVERFLOW; + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +}; +Status DestoryStack(SqStack *S) { //销毁栈 + SqStack *p; + if (StackEmpty(*S)) { + return ERROR; + } + free(S);//S->base + S->base = NULL; + S->top = NULL; + S->stacksize = 0; + return OK; +}; +Status ClearStack(SqStack *S) { //清空栈 + if (StackEmpty(*S)) { + return ERROR; + } + S->top = S->base; + return OK; +}; +bool StackEmpty(SqStack S) { //判断是否为空栈 + if (S.top == S.base) { + return true; + } + return false; +}; +int StackLength(SqStack S) { //返回栈的长度 + if (StackEmpty(S)) { + return ERROR; + } + return S.stacksize; +}; +Status GetTop(SqStack S, SElemType *e) { //用e返回栈顶元素 + if (StackEmpty(S)) { + return ERROR; + } + *e = *(S.top - 1); + return OK; +}; +Status Push(SqStack *S, SElemType e) { //压栈(插入元素e为新的栈顶元素) + if ((S->top) - (S->base) >= (S->stacksize)) { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if (!(S->base)) { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *(S->top)++ = e; + return OK; +}; +Status Pop(SqStack *S, SElemType *e) { //出栈(删除栈顶元素,并用e返回其值) + if (StackEmpty(*S)) { + return ERROR; + } + *e = *--(S->top); + return OK; +}; +Status StackTraverse(SqStack S, Status(*visit)()) { //遍历栈的元素,并对每个元素调用visit函数 + if (StackEmpty(S)) { + return ERROR; + } + while (S.top > S.base){ + visit(*S.base++); + } + printf("\n"); + return OK; +}; +Status visit(SElemType e) { //输出元素 + printf("%c", e); + return OK; +}; + +void Matching(SqStack *S, SElemType *p) { + //遇到左括号则入栈,遇到右括号寻找与其匹配的左括号,找到则左括号出栈,否则不匹配 + int state = 1; + int i = 0; + SElemType c; + while (state && p[i] != '\0') { + switch (p[i]) { + case'{': { + Push(S, p[i]); + i++; + break; + } + case'}': { + GetTop(*S, &c); + if (!StackEmpty(*S) && c == '{') { + Pop(S, &c); + i++; + } + else { + state = 0; + } + break; + } + case'[': { + Push(S, p[i]); + i++; + break; + } + case']': { + GetTop(*S, &c); + if (!StackEmpty(*S) && c == '[') { + Pop(S, &c); + i++; + } + else { + state = 0; + } + break; + } + case'(': { + Push(S, p[i]); + i++; + break; + } + case')': { + GetTop(*S, &c); + if (!StackEmpty(*S) && c == '(') { + Pop(S, &c); + i++; + } + else { + state = 0; + } + break; + } + } + } + if (StackEmpty(*S) && state) { //若栈为空且括号都匹配完,匹配成功,否则匹配失败 + printf("Matched!\n"); + } + else { + printf("Not Matched!\n"); + } + printf("\n"); +} + +int main() { + SqStack S; + if (InitStack(&S)) { //构造空栈 + printf("Initialize stack failed.\n"); + } + else { + printf("测试(正确)用例一: {{{[[[((()))]]]}}}\n"); + Matching(&S, "{{{[[[((()))]]]}}}"); + + printf("测试(正确)用例二: {[()]}\n"); + Matching(&S, "{[()]}"); + + printf("测试(错误)用例一: {{{[[[((()))]]]}}}}\n"); + Matching(&S, "{{{[[[((()))]]]}}}}"); + + printf("测试(错误)用例二: {)]]]}}}}\n"); + Matching(&S, "{)]]]}}}}"); + }; + + return 0; +} \ No newline at end of file diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.2and3.2.3.h" "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.2and3.2.3.h" new file mode 100644 index 00000000..86ef0cbb --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.2and3.2.3.h" @@ -0,0 +1,32 @@ +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef char SElemType; + +typedef enum { + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum { + false, + true +}bool; + +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + + +Status InitStack(SqStack *S); +Status DestoryStack(SqStack *S); +Status ClearStack(SqStack *S); +bool StackEmpty(SqStack S); +int StackLength(SqStack S); +Status GetTop(SqStack S, SElemType *e); +Status Push(SqStack *S, SElemType e); +Status Pop(SqStack *S, SElemType *e); +Status StackTraverse(SqStack S, Status(*visit)()); diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.2\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.2\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..fb56514c Binary files /dev/null and "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.2\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.3.c" "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.3.c" new file mode 100644 index 00000000..c8dbeace --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.3.c" @@ -0,0 +1,127 @@ +//行编辑程序 +#include +#include +#include "3.2.2and3.2.3.h" + +Status InitStack(SqStack *S) { //构造空栈 + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!(S->base)) { + return OVERFLOW; + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +}; +Status DestoryStack(SqStack *S) { //销毁栈 + SqStack *p; + if (StackEmpty(*S)) { + return ERROR; + } + free(S);//S->base + S->base = NULL; + S->top = NULL; + S->stacksize = 0; + return OK; +}; +Status ClearStack(SqStack *S) { //清空栈 + if (StackEmpty(*S)) { + return ERROR; + } + S->top = S->base; + return OK; +}; +bool StackEmpty(SqStack S) { //判断是否为空栈 + if (S.top == S.base) { + return true; + } + return false; +}; +int StackLength(SqStack S) { //返回栈的长度 + if (StackEmpty(S)) { + return ERROR; + } + return S.stacksize; +}; +Status GetTop(SqStack S, SElemType *e) { //用e返回栈顶元素 + if (StackEmpty(S)) { + return ERROR; + } + *e = *(S.top - 1); + return OK; +}; +Status Push(SqStack *S, SElemType e) { //压栈(插入元素e为新的栈顶元素) + if ((S->top) - (S->base) >= (S->stacksize)) { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if (!(S->base)) { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *(S->top)++ = e; + return OK; +}; +Status Pop(SqStack *S, SElemType *e) { //出栈(删除栈顶元素,并用e返回其值) + if (StackEmpty(*S)) { + return ERROR; + } + *e = *--(S->top); + return OK; +}; +Status StackTraverse(SqStack S, Status(*visit)()) { //遍历栈的元素,并对每个元素调用visit函数 + if (StackEmpty(S)) { + return ERROR; + } + while (S.top > S.base) { + visit(*S.base++); + } + printf("\n"); + return OK; +}; +Status visit(SElemType e) { //输出元素 + printf("%c", e); + return OK; +}; + +Status LineEdit(char *p) { + SqStack S; + int i = 0; + char c; + if (InitStack(&S)) { //构造空栈 + printf("Initialize stack failed.\n"); + return ERROR; + }; + while (p[i] != '\0') { + switch (p[i]) { + case'#': + Pop(&S, &c); //仅当栈非空时出栈 + break; + case'@': + ClearStack(&S); //重置为空栈 + break; + default: + Push(&S,p[i]); //有效字符进栈 + break; + } + i++; + } + printf("输出结果:"); + StackTraverse(S, visit); + printf("\n"); + ClearStack(&S); + DestoryStack(&S); + return OK; +}; + +int main() { + char string1[] = "whli##ilr#e(s#*s)"; + char string2[] = "outcha@putchar(*s=#++)"; + + printf("测试用例一:whli##ilr#e(s#*s)\n"); + LineEdit(string1); + + printf("测试用例二:outcha@putchar(*s=#++)\n"); + LineEdit(string2); + + return 0; +} diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.3\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.3\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..19804cfa Binary files /dev/null and "b/2017-1/SQ/\347\256\227\346\263\2253.2.1&3.2.2&3.2.3/3.2.3\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5+.c" "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5+.c" new file mode 100644 index 00000000..b9e41329 --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5+.c" @@ -0,0 +1,408 @@ +//计算有限个数的运算符和操作数的数学表达式 +#include +#include +#include +#include "3.2.5+.h" + +Status InitOptrStack(OptrStack *S) { //构造空的运算符栈 + S->base = (ElemType_Optr *)malloc(STACK_INIT_SIZE * sizeof(ElemType_Optr)); + if (!(S->base)) { + printf("Initiate stack failed.\n"); + return ERROR; + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +}; +Status GetOptrTop(OptrStack S, ElemType_Optr *e) { //用e返回栈顶元素(运算符) + if (OptrStackEmpty(S)) { + printf("Error, the stack is empty\n"); + return ERROR; + } + *e = *(S.top - 1); + return OK; +}; +Status PushOptr(OptrStack *S, ElemType_Optr e) { //压栈(插入元素e为新的栈顶元素) + if ((S->top) - (S->base) >= (S->stacksize)) { + S->base = (ElemType_Optr *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(ElemType_Optr)); + if (!(S->base)) { + printf("The increment of the stack failed.\n"); + return ERROR; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +}; +Status PopOptr(OptrStack *S, ElemType_Optr *e) { //出栈(删除栈顶元素,并用e返回其值) + if (OptrStackEmpty(*S)) { + printf("Error, the stack is empty\n"); + return ERROR; + } + *e = *(--S->top); + return OK; +}; +bool OptrStackEmpty(OptrStack S) { //判断是否为空栈 + if (S.top == S.base) { + return true; + } + return false; +}; + +Status InitOpndStack(OpndStack *S) { //构造空的操作数栈 + S->base = (ElemType_Opnd *)malloc(STACK_INIT_SIZE * sizeof(ElemType_Opnd)); + if (!(S->base)) { + printf("Initiate stack failed.\n"); + return ERROR; + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +}; +Status GetOpndTop(OpndStack S, ElemType_Opnd *e) { //用e返回栈顶元素(操作数) + if (OpndStackEmpty(S)) { + printf("Error, the stack is empty\n"); + return ERROR; + } + *e = *(S.top - 1); + return OK; +}; +Status PushOpnd(OpndStack *S, ElemType_Opnd e) { //压栈(插入元素e为新的栈顶元素) + if ((S->top) - (S->base) >= (S->stacksize)) { + S->base = (ElemType_Opnd *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(ElemType_Opnd)); + if (!(S->base)) { + printf("The increment of the stack failed.\n"); + return ERROR; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +}; +Status PopOpnd(OpndStack *S, ElemType_Opnd *e) { //出栈(删除栈顶元素,并用e返回其值) + if (OpndStackEmpty(*S)) { + printf("Error, the stack is empty\n"); + return ERROR; + } + *e = *(--S->top); + return OK; +}; +bool OpndStackEmpty(OpndStack S) { //判断是否为空栈 + if (S.top == S.base) { + return true; + } + return false; +}; + +Status Standard(char *exp) { //化为算术表达式,删除多余的空格 + char *q, *p; + p = exp; + while (*p != '\0') { //遍历字符串 + if (*p == ' ') { //如果是空格,删除 + q = p; + do { + *q = *(q + 1); + q++; + } while (*q != '\0'); + } + p++; + } + *p = '\0'; + return OK; +} +bool Precede(ElemType_Optr a, ElemType_Optr b) { //判断运算符的优先级 + int i, j; + char pre[][7] = { + // + - * / ( ) # + { '>','>','<','<','<','>','>' }, // + + { '>','>','<','<','<','>','>' }, // - + { '>','>','>','>','<','>','>' }, // * + { '>','>','>','>','<','>','>' }, // / + { '<','<','<','<','<','=','0' }, // ( + { '>','>','>','>','0','>','>' }, // ) + { '<','<','<','<','<','0','=' } // # + }; + + switch (a) { + case '+': + i = 0; + break; + case '-': + i = 1; + break; + case '*': + i = 2; + break; + case '/': + i = 3; + break; + case '(': + i = 4; + break; + case ')': + i = 5; + break; + case '#': + i = 6; + break; + } + switch (b) { + case '+': + j = 0; + break; + case '-': + j = 1; + break; + case '*': + j = 2; + break; + case '/': + j = 3; + break; + case '(': + j = 4; + break; + case ')': + j = 5; + break; + case '#': + j = 6; + break; + } + + if (pre[i][j] == '>') { + return true; + } + else { + return false; + } +}; +bool IN(char c) { //判断是否为运算符 + switch (c) { + case '+': + case '-': + case '*': + case '/': + case '(': + case ')': + case '#': + return true; + break; + default: + return false; + break; + } +}; +ElemType_Opnd Operate(ElemType_Opnd a, ElemType_Optr op, ElemType_Opnd b) { //进行运算,运算符op只能为+、-、*、/ + ElemType_Opnd result = 0; + switch (op) { + case'+': + result = a + b; + break; + case'-': + result = a - b; + break; + case'*': + result = a * b; + break; + case'/': { + if (b == 0) { + printf("Error, the divisor can't be 0\n"); + exit(0); + } + result = a / b; + break; + } + default: + break; + } + printf("%d %c %d = %d \n", a, op, b, result); + return result; +}; + +Status Evaluate(char suffix[][5]) { //计算过程 + OpndStack OPND; + int i, num; //i为计数器,num暂存操作数 + int temp1, temp2, result; //用来计算的两个操作数及运算符 + i = temp1 = temp2 = result = 0; + + if (!InitOpndStack(&OPND)) { + printf("ERROR! Initate stack failed.\n"); + return ERROR; + } + while (suffix[i][0]!= '#') { + if (!IN(suffix[i][0])) { + num = atoi(suffix[i]); + PushOpnd(&OPND, num); + } + else { + if (!OpndStackEmpty(OPND)) { + PopOpnd(&OPND, &temp1); + PopOpnd(&OPND, &temp2); + result = Operate(temp2, suffix[i][0], temp1); + PushOpnd(&OPND, result); + } + } + i++; + } + if (result == 0 && temp1 == 0 && temp2 == 0) { + printf("The expression is wrong!\n"); + return ERROR; + } + else { + printf("The result of the expression is : %d\n\n", result); + return OK; + } +}; +Status Pass(char suffix[50][5], char *c) { //构造后缀式 + static int j; + if (*c != '\0') { + strcpy(suffix[j], c); + j++; + suffix[j][0] = '#'; + } + return OK; +}; +Status Transform(char suffix[50][5], char exp[]) { //求后缀式,suffix储存后缀式,exp输入的原表达式 + OptrStack S; //暂存运算符的栈 + char *p; + char temp[5] = { '\0' }; //暂存操作数 + int i = 0; + char ch, c; + if (!InitOptrStack(&S)) { + printf("ERROR! Initate stack failed.\n"); + return ERROR; + }; + PushOptr(&S, '#'); + p = exp; + ch = *p; + while ((!OptrStackEmpty(S)) && (*p != '\0')) { + if (!IN(ch)) { + temp[i] = ch; + i++; + } + else { + if (i != 0) { + temp[i] = '\0'; + Pass(suffix, temp); //操作数进入后缀表达式 + i = 0; + } + switch (ch) { + case '(': + PushOptr(&S, ch); + break; + case ')': + PopOptr(&S, &c); + while (c != '(') { + temp[0] = c; + temp[1] = '\0'; + Pass(suffix, temp); //运算符进入后缀表达式 + PopOptr(&S, &c); + } + break; + default: + // Precede函数是判定运算符的栈顶运算符与读入的运算符之间优先关系的函数 + while ((GetOptrTop(S,&c)) && (Precede(c, ch))) { + temp[0] = c; + temp[1] = '\0'; + Pass(suffix, temp); //运算符进入后缀表达式 + PopOptr(&S, &c); + } + if (ch != '#') { + PushOptr(&S, ch); + } + break; + } // switch + } + + if (ch != '#') { + p++; + ch = *p; + } + else { + PopOptr(&S, &c); + temp[0] = c; + temp[1] = '\0'; + Pass(suffix, temp); + } + } // while + if (i != 0) { + temp[i] = '\0'; + Pass(suffix, temp); + } + while (!OptrStackEmpty(S)) { + PopOptr(&S, &c); + if (c != '#') { + temp[0] = c; + temp[1] = '\0'; + Pass(suffix, temp); + } + else { + break; + } + } +} // transform + +int main() { + char exp[50]; //存储表达式 + char suffix[50][5] = { '\0' }; //存后缀式,操作数+运算符的数量和<=100 + int i, result = 0; //i计数,result存储表达式结果 + + //printf("Please input an expression: "); + //scanf("%s",exp); + + //Standard(exp); //标准化 + //Transform(suffix, exp); //求后缀表达式 + + //if (exp[0] == '#') { + // printf("Error, the expression was wrong\n"); + //} + //else { + //printf("The Original expression is: %s\n", exp); + // printf("The suffix type of the expression:"); + // for (i = 0; suffix[i][0] != '#'; i++) { + // printf("%s", suffix[i]); + // } + // printf("\nThe excuation of the expression:\n"); + // Evaluate(suffix); //计算后缀式 + //} + + //for (int i = 0; i < 50; i++) { //清空suffix + // for (int j = 0; j < 5; j++) { + // suffix[i][j] = '\0'; + // } + //} + + char exp1[50] = { '9','+','1','1','-','(','2','*','2',')','\0' }; + char exp2[50] = { '3','*','1','0','-','(','5','+','6','/','2',')','\0' }; + + printf("The Original expression is: %s\n", exp1); + Standard(exp1); + Transform(suffix, exp1); + printf("The suffix type of the expression:"); + for (i = 0; suffix[i][0] != '#'; i++) { + printf("%s", suffix[i]); + } + printf("\nThe excuation of the expression:\n"); + Evaluate(suffix); //计算后缀式 + + for (int i = 0; i < 50; i++) { //清空suffix + for (int j = 0; j < 5; j++) { + suffix[i][j] = '\0'; + } + } + + printf("The Original expression is: %s\n", exp2); + Standard(exp2); + Transform(suffix, exp2); + printf("The suffix type of the expression:"); + for (i = 0; suffix[i][0] != '#'; i++) { + printf("%s", suffix[i]); + } + printf("\nThe excuation of the expression:\n"); + Evaluate(suffix); //计算后缀式 + + return 0; +} \ No newline at end of file diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5+.h" "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5+.h" new file mode 100644 index 00000000..0b0e8cac --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5+.h" @@ -0,0 +1,44 @@ +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef char ElemType_Optr; +typedef int ElemType_Opnd; +typedef enum { + ERROR, + OK +}Status; +typedef enum { + false, + true +}bool; +typedef struct { + ElemType_Opnd *base; + ElemType_Opnd *top; + int stacksize; +}OpndStack; +typedef struct { + ElemType_Optr *base; + ElemType_Optr *top; + int stacksize; +}OptrStack; + +Status InitOptrStack(OptrStack *S); +Status GetOptrTop(OptrStack S, ElemType_Optr *e); +Status PushOptr(OptrStack *S, ElemType_Optr e); +Status PopOptr(OptrStack *S, ElemType_Optr *e); +bool OptrStackEmpty(OptrStack S); + +Status InitOpndStack(OpndStack *S); +Status GetOpndTop(OpndStack S, ElemType_Opnd *e); +Status PushOpnd(OpndStack *S, ElemType_Opnd e); +Status PopOpnd(OpndStack *S, ElemType_Opnd *e); +bool OpndStackEmpty(OpndStack S); + +Status Standard(char *exp); +bool Precede(ElemType_Optr a, ElemType_Optr b); +bool IN(char c); +ElemType_Opnd Operate(ElemType_Opnd a, ElemType_Optr op, ElemType_Opnd b); + +Status Evaluate(char suffix[][5]); +Status Pass(char suffix[50][5], char *c); +Status Transform(char suffix[50][5], char exp[]); \ No newline at end of file diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5+\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5+\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..56ca3832 Binary files /dev/null and "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5+\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5.c" "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5.c" new file mode 100644 index 00000000..ccdfc0c3 --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5.c" @@ -0,0 +1,299 @@ +//实现操作数为十以内的表达式求值,输入输出都为整数 +#include +#include +#include "3.2.5.h" + +Status InitStack(SqStack *S) { //构造空栈 + S->base = (ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType)); + if (!(S->base)) { + return OVERFLOW; + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +}; +bool StackEmpty(SqStack S) { //判断是否为空栈 + if (S.top == S.base) { + return true; + } + return false; +}; +Status GetTop(SqStack S, ElemType *e) { //用e返回栈顶元素 + if (StackEmpty(S)) { + printf("Error, the stack is empty\n"); + return ERROR; + } + *e = *(S.top - 1); + return OK; +}; +Status Push(SqStack *S, ElemType e) { //压栈(插入元素e为新的栈顶元素) + if ((S->top) - (S->base) >= (S->stacksize)) { + S->base = (ElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(ElemType)); + if (!(S->base)) { + printf("The increment of the stack failed.\n"); + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +}; +Status Pop(SqStack *S, ElemType *e) { //出栈(删除栈顶元素,并用e返回其值) + if (StackEmpty(*S)) { + printf("Error, the stack is empty\n"); + return ERROR; + } + *e = *--S->top; + return OK; +}; + +bool Precede(char a,char b){ //判断运算符的优先级 + int i, j; + char pre[][7] = { + // + - * / ( ) # + { '>','>','<','<','<','>','>' }, // + + { '>','>','<','<','<','>','>' }, // - + { '>','>','>','>','<','>','>' }, // * + { '>','>','>','>','<','>','>' }, // / + { '<','<','<','<','<','=','0' }, // ( + { '>','>','>','>','0','>','>' }, // ) + { '<','<','<','<','<','0','=' } // # + }; + + switch (a) { + case '+': + i = 0; + break; + case '-': + i = 1; + break; + case '*': + i = 2; + break; + case '/': + i = 3; + break; + case '(': + i = 4; + break; + case ')': + i = 5; + break; + case '#': + i = 6; + break; + } + switch (b) { + case '+': + j = 0; + break; + case '-': + j = 1; + break; + case '*': + j = 2; + break; + case '/': + j = 3; + break; + case '(': + j = 4; + break; + case ')': + j = 5; + break; + case '#': + j = 6; + break; + } + + if (pre[i][j] == '>') { + return true; + } + else { + return false; + } +}; +bool IN(char c) { + //判断是否为运算符 + switch (c) { + case '+': + case '-': + case '*': + case '/': + case '(': + case ')': + case '#': + return true; + break; + default: + return false; + break; + } +}; +Status Pass(char *suffix, char c) { //构造后缀式 + int i = 0; + while (suffix[i] != '\0') { + i++; + } + suffix[i] = c; + suffix[i + 1] = '\0'; +}; +Status Evaluate(char *suffix) { //将后缀式进行计算 + SqStack OPND; + char *p = suffix; + char c, ch; + int temp1, temp2, result; + ch = *p; + temp1 = temp2 = result = 0; + if (InitStack(&OPND)) { + printf("ERROR! Initate stack failed.\n"); + exit(0); + } + while (ch != '\0') { + if (!IN(ch)) { + Push(&OPND, ch); + } + else { + if (!StackEmpty(OPND)){ + Pop(&OPND, &c); + temp1 = c - '0'; + if (!StackEmpty(OPND)) { + Pop(&OPND, &c); + temp2 = c - '0'; + result = Operate(temp2, ch, temp1); + c = result + '0'; + Push(&OPND, c); + } + } + } + p++; + ch = *p; + } + if (result == 0 && temp1 == 0 && temp2 == 0) { + printf("The expression was wrong!\n"); + return ERROR; + } + else { + printf("The result of the expression is:%d\n\n", result); + return OK; + } +}; +Status Transform(char *suffix, char exp[]) { //求后缀式,suffix储存后缀式,exp输入的原表达式 + SqStack S; //暂存运算符的栈 + char *p; + char ch, c; + if (InitStack(&S)) { + printf("Initiate stack failed.\n"); + return ERROR; + }; + Push(&S, '#'); + p = exp; + ch = *p; + while ((!StackEmpty(S)) && (*p != '\0')) { + if (!IN(ch)) { + Pass(suffix, ch); + } + else { + switch (ch) { + case '(': + Push(&S, ch); + break; + case ')': + Pop(&S, &c); + while (c != '(') { + Pass(suffix, c); + Pop(&S, &c); + } + break; + default: + // Precede函数是判定运算符的栈顶运算符与读入的运算符之间优先关系的函数 + while ((!GetTop(S, &c)) && (Precede(c, ch))) { + Pass(suffix, c); + Pop(&S, &c); + } + if (ch != '#') { + Push(&S, ch); + } + break; + } // switch + } + + if (ch != '#') { + p++; + ch = *p; + } + else { + Pop(&S, &c); + Pass(suffix, c); + } + } // while + while (!StackEmpty(S)) { + Pop(&S, &c); + if (c != '#') { + Pass(suffix, c); + } + else { + break; + } + } +} // transform +int Operate(int a, char op, int b) { //进行运算,op只能为+、-、*、/ + int result; + switch (op){ + case'+': + result = a + b; + break; + case'-': + result = a - b; + break; + case'*': + result = a * b; + break; + case'/': { + if (b == 0) { + printf("Error, the divisor can't be 0\n"); + exit(0); + } + result = a / b; + } + break; + } + printf("%d %c %d = %d \n", a, op, b, result); + return result; +}; + +int main() { + //char exp[50];//存原表达式 + char exp1[]= { '3','+','9','-','(','2','*','2',')','\0' }; + char exp2[] = { '5','*','3','-','(','2','+','6',')','\0' }; + char suffix[50] = { '\0' }; //存后缀式 + + /*printf("Please input an expression:"); + scanf("%s", exp); + printf("The Original expression is: %s\n", exp); + Transform(suffix, exp); + printf("The suffix type of the expression:%s\n", suffix); + printf("The excuation of the expression:\n"); + Evaluate(suffix);*/ + + + printf("The Original expression is: %s\n", exp1); + Transform(suffix, exp1); + printf("The suffix type of the expression:%s\n", suffix); + printf("The excuation of the expression:\n"); + Evaluate(suffix); + + for (int i = 0; i < 50; i++) { + suffix[i] = '\0'; + } + + printf("The Original expression is: %s\n", exp2); + Transform(suffix, exp2); + printf("The suffix type of the expression:%s\n", suffix); + printf("The excuation of the expression:\n"); + Evaluate(suffix); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5.h" "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5.h" new file mode 100644 index 00000000..98497f95 --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5.h" @@ -0,0 +1,32 @@ +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef char ElemType; +typedef struct { + ElemType *base; + ElemType *top; + int stacksize; +}SqStack; +typedef enum { + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum { + false, + true +}bool; + +Status InitStack(SqStack *S); +bool StackEmpty(SqStack S); +Status GetTop(SqStack S, ElemType *e); +Status Push(SqStack *S, ElemType e); +Status Pop(SqStack *S, ElemType *e); + +bool Precede(char a, char b); +bool IN(char c); +Status Pass(char *suffix, char c); +Status Evaluate(char *suffix); +Status Transform(char *suffix, char exp[]); +int Operate(int a, char op, int b); \ No newline at end of file diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..07f61b57 Binary files /dev/null and "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.2.5(\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274)/3.2.5\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.4(\351\230\237\345\210\227)/3.4.c" "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.4(\351\230\237\345\210\227)/3.4.c" new file mode 100644 index 00000000..ecf42da7 --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.4(\351\230\237\345\210\227)/3.4.c" @@ -0,0 +1,130 @@ +#include +#include +#include +#include "3.4.h" + +Status InitQueue(LinkQueue *Q) { + Q->front = Q->rear = (QueuePtr)malloc(MAXQSIZE * sizeof(QNode)); + if (!(Q->front)) { + return OVERFLOW; + } + Q->front->next = NULL; + Q->rear->next = NULL; + printf("\n队列初始化完成\n"); + return OK; +}; //构造空队列 +Status DestoryQueue(LinkQueue *Q) { + while (Q->front) { + Q->rear = Q->front->next; + free(Q->front); + Q->front = Q->rear; + } + printf("队列销毁成功\n\n"); + return OK; +}; //销毁队列 +Status ClearQueue(LinkQueue *Q) { + Q->rear = Q->front = NULL; + printf("\n队列清空完成\n"); + return OK; +}; //清空队列 +bool QueueEmpty(LinkQueue Q) { + if (Q.front == Q.rear) { + return true; + } + return false; +}; //判断是否为空队列 +Status GetHead(LinkQueue Q, QElemType *e) { //需更改 + if (QueueEmpty(Q)) { + printf("错误!空队列!\n"); + return ERROR; + } + *e = Q.front->next->data; +}; //返回队头元素 +Status EnQueue(LinkQueue *Q, QElemType e) { + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) { + return OVERFLOW; + } + p->data = e; + p->next = NULL; + Q->rear->next = p; + Q->rear = p; + return OK; +}; //插入新的队尾元素 +Status DeQueue(LinkQueue *Q, QElemType *e) { + QueuePtr p = NULL; + if (QueueEmpty(*Q)) { + printf("错误!空队列!\n"); + return ERROR; + } + p = Q->front->next; + *e = p->data; + Q->front->next = p->next; + if (Q->rear == p) { + Q->rear = Q->front; + } + free(p); + return OK; +}; //删除队头元素,并用e返回其值 +Status QueueTraverse(LinkQueue Q) { + QueuePtr p; + int i = 1; + p = Q.front->next; + if (QueueEmpty(Q)) { + printf("错误!空队列!\n"); + return ERROR; + } + while (p) { + printf("%d ", p->data); + p = p->next; + i++; + } + printf("\n队列输出完毕\n"); + return OK; +}; //遍历队列中的元素,并将其输出 +Status CreateQueue(int n){ + LinkQueue Q; + QElemType e; + int i, temp; + if (InitQueue(&Q)) { + printf("Initialize stack failed.\n"); + return ERROR; + }; + for (i = 0; i < n; i++) { + temp = rand() % 1024; + EnQueue(&Q, temp); + } + + printf("\n该队列的元素为:"); + QueueTraverse(Q); + + GetHead(Q, &e); + printf("\n队列中第一个元素为:%d\n", e); + printf("\n"); + + for (i = 0; i < n; i++) { + DeQueue(&Q, &e); + printf("删除第%d个的元素:%d", i + 1, e); + printf("\n该队列的元素为:"); + QueueTraverse(Q); + printf("\n"); + } + + ClearQueue(&Q); + DestoryQueue(&Q); + + return OK; +} +int main(){ + LinkQueue Q; + int n; + srand(time(NULL)); + n = rand() % 20 + 1; + printf("随机产生队列的大小,其大小在1~20之间\n"); + printf("\n随机队列的大小: %d",n); + + CreateQueue(n); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.4(\351\230\237\345\210\227)/3.4.h" "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.4(\351\230\237\345\210\227)/3.4.h" new file mode 100644 index 00000000..ff02eac3 --- /dev/null +++ "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.4(\351\230\237\345\210\227)/3.4.h" @@ -0,0 +1,29 @@ +#define MAXQSIZE 100 + +typedef int QElemType; +typedef enum { + OK, + ERROR, + OVERFLOW +}Status; +typedef enum { + false, + true +}bool; +typedef struct QNode { + QElemType data; + struct QNode *next; +}QNode, *QueuePtr; +typedef struct{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; +Status InitQueue(LinkQueue *Q); //构造空队列 +Status DestoryQueue(LinkQueue *Q); //销毁队列 +Status ClearQueue(LinkQueue *Q); //清空队列 +bool QueueEmpty(LinkQueue Q); //判断是否为空队列 +Status GetHead(LinkQueue Q, QElemType *e); //返回队头元素 +Status EnQueue(LinkQueue *Q, QElemType e); //插入新的队尾元素 +Status DeQueue(LinkQueue *Q, QElemType *e); //删除队头元素,并用e返回其值 +Status QueueTraverse(LinkQueue Q); //遍历队列中的元素,并将其输出 + diff --git "a/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.4(\351\230\237\345\210\227)/3.4\350\277\220\350\241\214\347\273\223\346\236\234\347\244\272\344\276\213.png" "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.4(\351\230\237\345\210\227)/3.4\350\277\220\350\241\214\347\273\223\346\236\234\347\244\272\344\276\213.png" new file mode 100644 index 00000000..b0ddb505 Binary files /dev/null and "b/2017-1/SQ/\347\256\227\346\263\2253.2.5&3.4/\347\256\227\346\263\225 3.4(\351\230\237\345\210\227)/3.4\350\277\220\350\241\214\347\273\223\346\236\234\347\244\272\344\276\213.png" differ diff --git "a/2017-1/SQ/\351\223\276\350\241\250\346\213\206\345\210\206/S.c" "b/2017-1/SQ/\351\223\276\350\241\250\346\213\206\345\210\206/S.c" new file mode 100644 index 00000000..b0f5fd62 --- /dev/null +++ "b/2017-1/SQ/\351\223\276\350\241\250\346\213\206\345\210\206/S.c" @@ -0,0 +1,163 @@ +#include +#include +#include +#include "S.h" + +ElemType GetElem(LinkList *L, int i) { //返回第i个元素的值 + LinkList *p; + int j; + p = L; + for (j = 0; j != i; j++) { + p = p->next; + } + return p->data; +} +LinkList* CreateList(LinkList *L, int n) { //构建大小为n的随机数单链表,返回头指针 + LinkList *list, *p, *q ; + int i; + list= (LinkList *)malloc(sizeof(LinkList)); + list->data = n; + list->next = NULL; + for (i = 0; i < n; i++) { + p = (LinkList *)malloc(sizeof(LinkList)); + if (!p) { + exit(0); + } + p->data = (int)rand() % 100; + if (list->next == NULL) { + list ->next= p; + q = p; + } + else { + q->next = p; + q = p; + } + } + if (list != NULL) { + q->next = NULL; + } + L = list; + return list; +} +LinkList* ListInsert(LinkList *L, ElemType e) { //在链表末尾添加元素e + LinkList *p, *q; + q = (LinkList *)malloc(sizeof(LinkList)); + p = L; + while (p->next != NULL) { + p = p->next; + } + q->data = e; + q->next = NULL; + p->next = q; + return L; +} +LinkList* Connect(LinkList *L) { //首尾相连,构成循环链表 + LinkList *p; + p = L; + while (p->next != NULL) { + p = p->next; + } + p->next = L; + return L; +} +Status Traverse(LinkList *L) { + LinkList *p; + if (ListEmpty(L)) { + return ERROR; + } + p = L->next; + while (p && p != L) { + printf("%4d", p->data); + p = p->next; + } + printf("\n"); + return OK; +} +Status DestroyList(LinkList *L) { + LinkList *p; + if (ListEmpty(L)) { + return ERROR; + } + while (L) { + p = L; + L = L->next; + free(p); + } + printf("\nThe Original List has been destoryed.\n"); + return OK; +} +bool ListEmpty(LinkList *L) { + if (L->next == NULL) { + return true; + } + return false; +} +int InputSize() { //输入原始链表的大小 + int n = 0; + do { + if (n == 0) { + printf("Please input a positive integer: "); + } + scanf("%d", &n); + if (n <= 0) { + printf("Error! The size of the list can't be less than zero\n"); + printf("Please input a positive integer again:"); + } + } while (n <= 0); + return n; +} + + +int main() { + LinkList *L, *La, *Lb; + int i, n, temp; + srand(time(NULL)); + n = InputSize(); + L = CreateList(&L, n); + if (!ListEmpty(L)) { + printf("\nThe numbers of List are:"); + Traverse(L); + } + else{ + exit(0); + } + La = (LinkList *)malloc(sizeof(LinkList)); + Lb = (LinkList *)malloc(sizeof(LinkList)); + if (n % 2 == 0) { + La->data = Lb->data= n / 2; + La->next = Lb->next = NULL; + for (i = 1; i <= n; i++) { + temp = GetElem(L, i); + if (i % 2 != 0) { + La = ListInsert(La, temp); + } + else { + Lb = ListInsert(Lb, temp); + } + } + } + else { + La->data = (n + 1) / 2; + Lb->data = (n - 1) / 2; + La->next = Lb->next = NULL; + for (i = 1; i <= n; i++) { + temp = GetElem(L, i); + if (i % 2 != 0) { + La = ListInsert(La, temp); + } + else { + Lb = ListInsert(Lb, temp); + } + } + } + printf("\nThe size of List 1 is: %d", Lb->data); + printf("\nThe List 1 is:"); + Lb = Connect(Lb); + Traverse(Lb); + printf("\nThe size of List 2 is: %d", La->data); + printf("\nThe List 2 is:"); + La = Connect(La); + Traverse(La); + DestroyList(L); + return 0; +} \ No newline at end of file diff --git "a/2017-1/SQ/\351\223\276\350\241\250\346\213\206\345\210\206/S.h" "b/2017-1/SQ/\351\223\276\350\241\250\346\213\206\345\210\206/S.h" new file mode 100644 index 00000000..769e66ef --- /dev/null +++ "b/2017-1/SQ/\351\223\276\350\241\250\346\213\206\345\210\206/S.h" @@ -0,0 +1,27 @@ + + +typedef int ElemType; + +typedef struct LNode { + ElemType data; + struct LNode *next; +}LinkList; + +typedef enum { + ERROR, + OK +}Status; + +typedef enum { + false, + true +}bool; + +ElemType GetElem(LinkList *L, int i); +LinkList* CreateList(LinkList *L, int n); +LinkList* ListInsert(LinkList *L, ElemType e); +LinkList* Connect(LinkList *L); +Status Traverse(LinkList *L); +Status DestroyList(LinkList *L); +bool ListEmpty(LinkList *L); +int InputSize(); \ No newline at end of file diff --git "a/2017-1/SQ/\351\223\276\350\241\250\346\213\206\345\210\206/\350\257\264\346\230\216.txt" "b/2017-1/SQ/\351\223\276\350\241\250\346\213\206\345\210\206/\350\257\264\346\230\216.txt" new file mode 100644 index 00000000..e48c20fe --- /dev/null +++ "b/2017-1/SQ/\351\223\276\350\241\250\346\213\206\345\210\206/\350\257\264\346\230\216.txt" @@ -0,0 +1,6 @@ +题目 +已知线性链表第一个链结点指针为list,请写一算法,将该链表分解为两个带有头结点的循环链表,并将两个循环链表的长度分别存放在各自头结点的数据域中。其中,线性表中序号为奇数的元素分解到第一个循环链表中,序号为偶数的元素分解到第二个循环链表中。(要求用最少的时间和最少的空间) + +说明 +①单链表拆解为两个循环链表时只进行了一次循环,因此时间复杂度T(n)=O(n) +②因为是新建的两个循环链表都是直接插入,所以空间复杂度S(n)=O(1) \ No newline at end of file diff --git a/2017-1/Sismark/2-12.cpp b/2017-1/Sismark/2-12.cpp new file mode 100644 index 00000000..099f235a --- /dev/null +++ b/2017-1/Sismark/2-12.cpp @@ -0,0 +1,103 @@ +#include +#include + +typedef struct Node +{ + int data;// 存放数据 + struct Node *pNext;//结构体指针,指向下一个与当前节点相同的节点 +}NODE,*LinkList; + + +LinkList CreateList_L(int len){ + int base = 10; +int i;//循环变量,用一个循环来每次创建一个节点,并把每个节点连在一起; +LinkList pHead=(LinkList)malloc(sizeof(NODE));//分配一个头节点 +printf("LinkList pHead=%X\n",pHead); +LinkList pTail=pHead;//定义一个尾指针pTail,并指向头节点 +printf("LinkList pTail=%X\n",pTail); +pHead->pNext=NULL; +printf("pHead->pNext=NULL\n"); +for(i=0;idata); + base += rand()%3+2; + p->data = base; + printf("p->data=%d\n",p->data); + pTail->pNext=p; + printf("pTail->pNext=%X\n",pTail->pNext); + p->pNext=NULL; + printf("LinkList p->pNext=%X\n",p->pNext); + pTail=p;//使pTail指向链表最后一个元素 + printf("LinkList pTail=%X\n",pTail); + } +return pHead; +} + +LinkList MergeList_L(const LinkList La,const LinkList Lb){ + LinkList pa=La->pNext,pb=Lb->pNext,L,pl; + L=(LinkList)malloc(sizeof(Node)); + printf("LinkList L=%X\n",L); + pl=L; + printf("LinkList pl=%X\n",pl); + while(pa&&pb){ + NODE*p=(LinkList)malloc(sizeof(Node)); + printf("LinkList p=%X\n",p); + if(pa->data<=pb->data) { + p->data=pa->data; printf("p->data=%d\n",p->data); pa=pa->pNext; + printf("LinkList pa=%X\n",pa); + } + + else { + p->data=pb->data; printf("p->data=%d\n",p->data); pb=pb->pNext; + printf("LinkList pb=%X\n",pb); + } + p->pNext=NULL; + printf("LinkList p->pNext=%X\n",p->pNext); + pl->pNext=p; + printf("LinkList pl->pNext=%X\n",pl->pNext); + pl=pl->pNext; + printf("LinkList pl=%X\n",pl); + } + NODE*P=pa?pa:pb; + for(;P;P=P->pNext){ + NODE*p=(LinkList)malloc(sizeof(Node)); + printf("LinkList p=%X\n",p); + p->data=P->data; + printf("p->data=%d\n",p->data); + p->pNext=NULL; + printf("LinkList p->pNext=%X\n",p->pNext); + pl->pNext=p; + printf("LinkList pl->pNext=%X\n",pl->pNext); + pl=pl->pNext; + printf("LinkList pl=%X\n",pl); + } + return L; +} + +void TravelList_L(const LinkList L){ + Node * p = L->pNext; + for(;p;p=p->pNext) + printf("%d ",p->data); +} +int main(){ + LinkList La,Lb; + printf( "开始创建La\n" ); + La = CreateList_L(rand()%5+3); + TravelList_L(La); + printf( "\n开始创建Lb\n" ); + Lb = CreateList_L(rand()%5+3); + TravelList_L(Lb); + LinkList Lc; + printf( "\n开始创建Lc\n" ); + Lc = MergeList_L(La,Lb); + TravelList_L(Lc); + + printf("\nLa="); + TravelList_L(La); + printf("\nLb="); + TravelList_L(Lb); + printf("\nLc="); + TravelList_L(Lc); +} diff --git a/2017-1/Sismark/3-4.c b/2017-1/Sismark/3-4.c new file mode 100644 index 00000000..e6a205f1 --- /dev/null +++ b/2017-1/Sismark/3-4.c @@ -0,0 +1,171 @@ +#include +#include +#include + +#define SIZE 100 +#define CREAT 10 +#define OVERFLOW -1 +#define OK 0 +#define ERROR -1 +#define FALSE 0 +#define TRUE 1 + +typedef int Status; +typedef char SElemType; + +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack(SqStack *pS) { + //构造空栈 + (*pS).base = (SElemType *)malloc(SIZE * sizeof(SElemType)); + if (!(*pS).base) exit(OVERFLOW);//存储分配失败 + (*pS).top = (*pS).base; + (*pS).stacksize = SIZE; + return OK; +} + +Status Push(SqStack *pS, SElemType e) { + //插入元素e为新的栈顶元素 + if ((*pS).top - (*pS).base >= (*pS).stacksize) { + (*pS).base = (SElemType *)realloc((*pS).base, ((*pS).stacksize + CREAT) * sizeof(SElemType)); + if (!(*pS).base) exit(OVERFLOW); + (*pS).top = (*pS).base + (*pS).stacksize; + (*pS).stacksize += CREAT; + } + *(*pS).top++ = e; + return OK; +} + +Status Pop(SqStack *pS, SElemType *e) { + if ((*pS).top == (*pS).base) return ERROR; + *e = *--(*pS).top; + return *e; + return OK; +} + +Status GetTop(SqStack S, SElemType *e) { + if (S.top == S.base) return ERROR; + e = *(S.top - 1); + return e; + return OK; +} + +Status StackEmpty(SqStack *pS) { + if ((*pS).base == (*pS).top) return TRUE; + else return FALSE; +} + +Status IN(SElemType e) { + if (e != '+'&&e != '-'&&e != '*'&& e != '/'&&e != '#'&&e != '('&&e != ')') return TRUE; + else return FALSE; +} + +Status Precede(SElemType ch, SElemType e) { + switch (e) { + case'+': { + if (ch == '#' || ch == '(') return '>'; + else return '<'; + break; + } + case'-': { + if ( ch == '#'||ch=='(') return '>'; + else return '<'; + break; + } + case'*': { + return '>'; + break; + } + case'/': { + return '>'; + break; + } + case'#': { + if (ch == '#') return '='; + else return '<'; + break; + } + case'(': return '>'; break; + case')': return '!!'; break; + } + return OK; +} + +Status Count() { + SqStack S1; + SqStack S2; + SElemType e; + int E=0; + InitStack(&S1);//造栈 + InitStack(&S2); + Push(&S1, '#'); + int i = 0, j = 0; + char ch[100] = "1*2+(3-8/4)*6#"; + printf("表达式:%s\n", ch); + char suf[100] = " "; + char ah[100] = " "; + while (ch[i] != '#') { + if (IN(ch[i]) && ch[i] != '#') + { + suf[j] = ch[i]; + j++; + i++; + } + else { + switch (Precede(GetTop(S1,&e), ch[i])) { + + case'>': + { Push(&S1, ch[i]); i++; }; break; + + case'<': + { Pop(&S1, &e); suf[j] = e; j++; }; break; + + case'=': break; + + case'!!': { + while (e != '(') { + Pop(&S1, &e); if (e != '(') { suf[j] = e; j++; } + }; + i++; + } + } + } + + } + while (!StackEmpty(&S1)) { + Pop(&S1, &e); + suf[j] = e; j++; + }; + printf("后缀式:"); + for (int k = 0; k < j; k++) printf("%c", suf[k]); + printf("\n"); + //start + i = 0; + while (suf[i] != '#') { + if (IN(suf[i])) { Push(&S2, suf[i] - '0'); i++; } + if (!IN(suf[i])) { + int a1, a2,a3; + Pop(&S2, &E); a1=E ; + Pop(&S2, &E); a2=E; + printf("a2=%d ope=%c a1=%d\n",a1,suf[i],a2); + if (suf[i] == '+') a3 = a2+a1; + if (suf[i] == '-') a3 = a2-a1; + if (suf[i] == '*') a3 = a2*a1; + if (suf[i] == '/') a3 = a2/a1; + printf("a3=%d\n",a3); + Push(&S2, a3); + i++; + } + }//end while + printf("\n表达式结果:%d", Pop(&S2, &E)); + return 0; +} +int main() { + Count(); + return 0; +} +//a*b + (c - d / e)*f# \ No newline at end of file diff --git a/2017-1/Sismark/BST.c b/2017-1/Sismark/BST.c new file mode 100644 index 00000000..b284424f --- /dev/null +++ b/2017-1/Sismark/BST.c @@ -0,0 +1,162 @@ +#include +#include + +//#define DEBUG 0 +//#define debug_print if(DEBUG)printf +typedef enum bool{ + false, + true +} bool; + +typedef struct BiTNode { + int data; + struct BiTNode* left; + struct BiTNode* right; +}BiTNode, *BiTree; + +int LT(int a, int b); +int EQ(int a, int b); +void Delete(BiTree *p); +int DeleteBST(BiTree *T, int kval); + +bool SearchBST(BiTree T, int data) { + if (!T) { + return false; + } + if (T->data == data) { + return true; + } + if (data < T->data) { + return SearchBST(T->left, data); + } + if (data > T->data) { + return SearchBST(T->right, data); + } + return false; +} +int LT(int a,int b) { + if (a < b) return true; + else return false; +} +int EQ(int a, int b) { + if (a == b) return true; + else return false; +} +BiTree newNode(int data) { + BiTree temp = (BiTree)malloc(sizeof(BiTNode)); + temp->data = data; + temp->left = temp->right = NULL; + return temp; +} +void _InsertBST(BiTree *T, int e) { + BiTNode* temp = newNode(e); + + if (*T == NULL) { + *T = temp; + } + else { + if (e > (*T)->data) { + (*T)->right = temp; + } + else if (e < (*T)->data) { + (*T)->left = temp; + } + } +} +void InsertBST(BiTree *T, int e) { + if (*T == NULL) { + _InsertBST(T, e); + return; + } + if(e > (*T)->data && (*T)->right){ + InsertBST(&(*T)->right, e); + } + else if (e < (*T)->data && (*T)->left) { + InsertBST(&(*T)->left, e); + } + else { + _InsertBST(T, e); + } +} +void Delete(BiTree *p) { + // 从二叉排序树中删除结点 p, + // 并重接它的左子树或右子树 + BiTree q, s; + if (!(*p)->right) { // 右子树为空树则只需重接它的左子树 + q = *p; + *p = (*p)->left; + q = NULL; + free(q); + } + else if (!(*p)->left) { // 左子树为空树则只需重接它的右子树 + q = *p; + *p = (*p)->right; + q = NULL; + free(q); + } + else { // 左右子树均不空 + q = *p; + s = (*p)->left; + while (s->right) { // s 指向被删结点的前驱 + q = s; + s = s->right; + } + (*p)->data = s->data; + if (q != *p) { + q->right = s->left; // 重接*q的右子树 + } + else { + q->left = s->left; // 重接*q的左子树 + } + s = NULL; + free(s); + } +} +int DeleteBST(BiTree *T, int kval) { + // 若二叉排序树 T 中存在其关键字等于 kval 的 + // 数据元素,则删除该数据元素结点,并返回 + // 函数值 true,否则返回函数值 false + if (!(*T)) { // 不存在关键字等于kval的数据元素 + return false; + } + else { + if (kval == (*T)->data) { // 找到关键字等于key的数据元素 + Delete(&(*T)); + return true; + } + else if (kval < (*T)->data) { // 继续在左子树中进行查找 + return DeleteBST(&(*T)->left, kval); + } + else { // 继续在右子树中进行查找 + return DeleteBST(&(*T)->right, kval); + } + } +} +void PreOrderTraverse(BiTree T) { + printf("%d, ",T->data); + if (T->left) PreOrderTraverse(T->left); + if (T->right) PreOrderTraverse(T->right); +} +int main() { + int i = 0, j = 0; + int a[] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + int b[] = { 13, 8, 5, 20, 6 }; + BiTree T = NULL; + while (i<12) { + InsertBST(&T, a[i]); + i++; + } + PreOrderTraverse(T); + printf("\n"); + while (j < 5) { + if (SearchBST(T, b[j])) { + DeleteBST(&T, b[j]); + } + else { + InsertBST(&T, b[j]); + } + PreOrderTraverse(T); + printf("\n"); + j++; + } +} \ No newline at end of file diff --git a/2017-1/Sismark/Queue.c b/2017-1/Sismark/Queue.c new file mode 100644 index 00000000..c7ac4247 --- /dev/null +++ b/2017-1/Sismark/Queue.c @@ -0,0 +1,82 @@ +#include +#include +#include + +#define SIZE 100 +#define CREAT 10 +#define OVERFLOW -1 +#define OK 0 +#define ERROR -1 +#define FALSE 0 +#define TRUE 1 + +typedef int Status; +typedef char QElemType; + +typedef struct QNode { + QElemType data; + struct QNode *next; +}QNode,*Queueptr; + +typedef struct { + Queueptr front; + Queueptr rear; +}LinkQueue; + +Status InitQueue(LinkQueue *pQ) { + //构造一个空队列Q + (*pQ).front = (*pQ).rear= (*pQ).rear = (Queueptr)malloc(sizeof(QNode)); + if (!(*pQ).front) exit(OVERFLOW); + (*pQ).front->next = NULL; + return OK; +} + +Status EnQueue(LinkQueue *pQ,QElemType e) { + //插入元素e为Q的新的队尾元素 + Queueptr p; + p = (Queueptr)malloc(sizeof(QNode)); + if (!p) exit(OVERFLOW); + p->data = e; p->next = NULL; + (*pQ).rear->next = p; + (*pQ).rear = p; + return OK; +} + +Status DeQueue(LinkQueue *pQ, QElemType *e) { + Queueptr p; + if ((*pQ).front == (*pQ).rear) return ERROR; + p = (*pQ).front->next; + *e = p->data; + (*pQ).front->next = p->next; + if ((*pQ).rear == p) (*pQ).rear = (*pQ).front; + free(p); + return *e; + return OK; +} + +Status QueueEmpty(LinkQueue Q) { + if (Q.front == Q.rear) return TRUE; + else return FALSE; +} + +void queue() { + LinkQueue Q; + QElemType e; + InitQueue(&Q); + char a[100]="sajnxskajnknc"; + int i = 0; + while (a[i]!='\0') + { + EnQueue(&Q,a[i]); + i++; + } + printf("\n出队列:"); + while (!QueueEmpty(Q)) { + DeQueue(&Q,&e); + printf("%c",e); + } +} +int main() { + queue(); + return 0; +} \ No newline at end of file diff --git a/2017-1/Sismark/alg 3-1.c b/2017-1/Sismark/alg 3-1.c new file mode 100644 index 00000000..6e6d55e9 --- /dev/null +++ b/2017-1/Sismark/alg 3-1.c @@ -0,0 +1,77 @@ +#include +#include +#include + +#define OK 1 +#define ERROR 0 +#define TRUE 1 +#define FALSE 0 +#define OVERFLOW -1 +#define SIZE 100 +#define CREAT 10 + +typedef int SElemType ; +typedef int Status ; +/*1.声明顺序栈*/ +typedef struct { + SElemType *base;/*栈底*/ + SElemType *top;/*栈顶*/ + int stacksize;/*栈的大小*/ +}SqStack; +/*2.构造一个空栈*/ +Status InitStack(SqStack *ps){ + (*ps).base = (SElemType * )malloc(SIZE * sizeof(SElemType));/*分配存储*/ + if(!(*ps).base) exit (-1); + (*ps).top = (*ps).base ;/*栈顶指向栈底*/ + (*ps).stacksize = SIZE; + return OK; +} + +Status push(SqStack *pS,SElemType e){ + if((*pS).top -( *pS).base >= (*pS).stacksize ){ + (*pS).base =(SElemType * )realloc((*pS).base,((*pS).stacksize + CREAT)*sizeof(SElemType)); + if(!(*pS).base ) exit (-1); + (*pS).top = (*pS).base + (*pS).stacksize ; + (*pS).stacksize +=CREAT; + } + *(*pS).top ++=e; + printf("%d\n", e);/*逆序输出八进制数*/ + return OK; +} + +Status Pop(SqStack *pS,SElemType *pe){ + if((*pS).top == (*pS).base) return ERROR; + *pe =*--(*pS).top ; + return OK; + return *pe; +} + +Status StackEmpty(SqStack *pS){ + if((*pS).base == (*pS).top ) return TRUE; + else return FALSE; +} + +void conversion(){ + SqStack S; + InitStack(&S); + int N,e; + srand(time(0)); + N = rand() % 10000; + printf("%d",N); + //scanf("%d",&N); + while(N){ + push(&S,N%8); + N=N/8; + } + printf("转换成八进制数:"); + while(!StackEmpty(&S)){ + Pop(&S,&e); + printf("%d",e); + } +} + +int main() { + conversion(); + printf("\n"); + return OK; +} \ No newline at end of file diff --git a/2017-1/Sismark/alg 3-1.cpp b/2017-1/Sismark/alg 3-1.cpp new file mode 100644 index 00000000..5817db55 --- /dev/null +++ b/2017-1/Sismark/alg 3-1.cpp @@ -0,0 +1,74 @@ +#include +#include +#include + +#define OK 1 +#define ERROR 0 +#define TRUE 1 +#define FALSE 0 +#define OVERFLOW -1 +#define SIZE 100 +#define CREAT 10 + +typedef int SElemType ; +typedef int Status ; +/*1.声明顺序栈*/ +typedef struct { + SElemType *base;/*栈底*/ + SElemType *top;/*栈顶*/ + int stacksize;/*栈的大小*/ +}SqStack; +/*2.构造一个空栈*/ +Status InitStack(SqStack &s){ + s.base = (SElemType * )malloc(SIZE * sizeof(SElemType));/*分配存储*/ + if(!s.base) exit (-1); + s.top =s.base ;/*栈顶指向栈底*/ + s.stacksize = SIZE; + return OK; +} + +Status push(SqStack &S,SElemType e){ + if(S.top -S.base >=S.stacksize ){ + S.base =(SElemType * )realloc(S.base,(S.stacksize + CREAT)*sizeof(SElemType)); + if(!S.base ) exit (-1); + S.top = S.base + S.stacksize ; + S.stacksize +=CREAT; + } + *S.top ++=e; + printf("%d\n",e);/*逆序输出八进制数*/ + return OK; +} + +Status Pop(SqStack &S,SElemType &e){ + if(S.top == S.base) return ERROR; + e=*--S.top ; + return OK; + return e; +} + +Status StackEmpty(SqStack S){ + if(S.base == S.top ) return TRUE; + else return FALSE; +} + +void conversion(){ + SqStack S; + InitStack(S); + int N=1348,e; + scanf("%d",&N); + while(N){ + push(S,N%8); + N=N/8; + } + printf("转换成八进制数:"); + while(!StackEmpty(S)){ + Pop(S,e); + printf("%d",e); + } +} + +int main(){ + conversion(); + printf("\n"); + return OK; +} \ No newline at end of file diff --git a/2017-1/Sismark/alg3-2.c b/2017-1/Sismark/alg3-2.c new file mode 100644 index 00000000..b34ad2ee --- /dev/null +++ b/2017-1/Sismark/alg3-2.c @@ -0,0 +1,85 @@ +#include +#include + +typedef int Status; +typedef char SElemType; + +#define SIZE 100 +#define CREAT 10 + +#define OVERFLOW -1 +#define OK 1 +#define FALSE 0 +#define TRUE 1 +#define ERROR 0 + +typedef struct { + SElemType *base;//栈底指针 + SElemType *top;//栈顶指针 + int stacksize; +} SqStack; + +Status InitStack(SqStack * pS){ + (*pS).base = (SElemType * )malloc(SIZE*sizeof(SElemType)); + if(!(*pS).base ) exit (OVERFLOW); //存储分配失败 + (*pS).top = (*pS).base ; + (*pS).stacksize =SIZE; + return OK; +} +//造空栈 +Status Pop(SqStack *pS , SElemType *pe){ + if((*pS).top == (*pS).base ) return ERROR; + (*pe) = *--(*pS).top ; + return *pe; + return OK; +} +//出栈 +Status Push(SqStack *pS , SElemType e){ + if((*pS).top - (*pS).base >= (*pS).stacksize ){ + (*pS).base =(SElemType *)realloc((*pS).base , ((*pS).stacksize + CREAT)*sizeof(SElemType)); + if(!(*pS).base ) exit (OVERFLOW); + (*pS).top = (*pS).base + (*pS).stacksize ; + (*pS).stacksize += CREAT; + } + *(*pS).top ++ = e; + return OK; +} +//入栈 +Status StackEmpty(SqStack *pS){ + if((*pS).base == (*pS).top ) return TRUE; + else return FALSE; +} +//判断 +void ParMat(){ + SElemType e; + SqStack S; + InitStack(&S);//造栈 + int i=0,flag=0; + char pa[100]="{([][])]"; + printf("%s\n",pa); + //scanf("%s",pa); + Push(&S,pa[i]); + e=pa[i]; + while(pa[i+1]!='\0'){ + i++; + if(pa[i]==']'){ + Pop(&S,&e); + if(e !='[') { flag++; break; } + } + if(pa[i]=='}'){ + Pop(&S,&e); + if(e!='{') { flag++; break; } + } + if(pa[i]==')'){ + Pop(&S,&e); + if(e!='(') { flag++; break; } + } + if(pa[i]=='['||pa[i]=='{'||pa[i]=='(') { Push(&S,pa[i]); } + } + if(StackEmpty(&S)&& flag==0) printf("匹配成功\n"); + else printf("匹配失败\n"); +} +int main(){ + ParMat(); + return 0; +} \ No newline at end of file diff --git a/2017-1/Sismark/alg3-2.cpp b/2017-1/Sismark/alg3-2.cpp new file mode 100644 index 00000000..29eb7ad2 --- /dev/null +++ b/2017-1/Sismark/alg3-2.cpp @@ -0,0 +1,85 @@ +#include +#include + +typedef int Status; +typedef char SElemType; + +#define SIZE 100 +#define CREAT 10 + +#define OVERFLOW -1 +#define OK 1 +#define FALSE 0 +#define TRUE 1 +#define ERROR 0 + +typedef struct { + SElemType *base;//栈底指针 + SElemType *top;//栈顶指针 + int stacksize; +} SqStack; + +Status InitStack(SqStack &S){ + S.base = (SElemType * )malloc(SIZE*sizeof(SElemType)); + if(!S.base ) exit (OVERFLOW); //存储分配失败 + S.top =S.base ; + S.stacksize =SIZE; + return OK; +} +//造空栈 +Status Pop(SqStack &S , SElemType &e){ + if(S.top == S.base ) return ERROR; + e = *--S.top ; + return e; + return OK; +} +//出栈 +Status Push(SqStack &S , SElemType e){ + if(S.top -S.base >=S.stacksize ){ + S.base =(SElemType *)realloc(S.base , (S.stacksize + CREAT)*sizeof(SElemType)); + if(!S.base ) exit (OVERFLOW); + S.top = S.base + S.stacksize ; + S.stacksize += CREAT; + } + *S.top ++ = e; + return OK; +} +//入栈 +Status StackEmpty(SqStack S){ + if(S.base == S.top ) return TRUE; + else return FALSE; +} +//判断 +void ParMat(){ + SElemType e; + SqStack S; + InitStack(S);//造栈 + int i=0; + char pa[100]="[([][])]"; + printf("%s\n",pa); + //scanf("%s",pa); + Push(S,pa[i]); + e=pa[i]; + while(pa[i+1]!='\0'){ + i++; + if(pa[i]==']'){ + Pop(S,e); + if(e !='[') { printf("匹配失败\n"); break; } + } + if(pa[i]=='}'){ + Pop(S,e); + if(e!='{') { printf("匹配失败\n"); break; } + } + if(pa[i]==')'){ + Pop(S,e); + if(e!='(') { printf("匹配失败\n"); break; } + } + if(pa[i]=='['||pa[i]=='{'||pa[i]=='(') { Push(S,pa[i]); } + } + if(StackEmpty(S) ) printf("匹配成功\n"); + else printf("匹配失败\n"); +} +int main(){ + ParMat(); + return 0; +} \ No newline at end of file diff --git a/2017-1/Sismark/alg3-3.c b/2017-1/Sismark/alg3-3.c new file mode 100644 index 00000000..1f083100 --- /dev/null +++ b/2017-1/Sismark/alg3-3.c @@ -0,0 +1,100 @@ +#include +#include +#include + +typedef int Status; +typedef char SElemType; + +#define SIZE 100 +#define CREAT 10 + +#define OVERFLOW -1 +#define OK 1 +#define FALSE 0 +#define TRUE 1 +#define ERROR 0 + +typedef struct { + SElemType *base;//栈底指针 + SElemType *top;//栈顶指针 + int stacksize; +} SqStack; + +Status InitStack(SqStack *pS){ + (*pS).base = (SElemType * )malloc(SIZE*sizeof(SElemType)); + if(!(*pS).base ) exit (OVERFLOW); //存储分配失败 + (*pS).top = (*pS).base ; + (*pS).stacksize =SIZE; + return OK; +} + +Status Pop(SqStack *pS , SElemType *pe){ + if((*pS).top == (*pS).base ) return ERROR; + (*pe) = *--(*pS).top ; + return *pe; + return OK; +} + +Status Push(SqStack *pS , SElemType e){ + if((*pS).top - (*pS).base >= (*pS).stacksize ){ + (*pS).base =(SElemType *)realloc((*pS).base , ((*pS).stacksize + CREAT)*sizeof(SElemType)); + if(!(*pS).base ) exit (OVERFLOW); + (*pS).top = (*pS).base + (*pS).stacksize ; + (*pS).stacksize += CREAT; + } + *(*pS).top ++ = e; + return OK; +} + +Status DestroyStack(SqStack *pS){ + (*pS).top =NULL; + (*pS).stacksize =0; + free((*pS).base ); + return OK; +} + +Status StackEmpty(SqStack *pS){ +if((*pS).base == (*pS).top ) return TRUE; +else return FALSE; +} +void Lineedit(){ + int i=0; + char ch[200]="whli##ilr#e(s#*s)\noutchar@\nputchar(*s=#++)\0"; + SqStack S; + SElemType e=ch[i]; + InitStack(&S); //构造空栈 、、、、、、、、、、、、、、、、、、、、、、、完成 + while(ch[i]!='\0'){ + e=ch[i]; + switch(ch[i]){ + case '#': + Pop(&S, &e); break;//仅当栈非空时退栈 + case '@': { + if(StackEmpty(&S)) break; + while(e!='\n'){Pop(&S, &e);}; + if(StackEmpty(&S)||e=='\n') { Push(&S,e); break; } break; + }//重置为空栈 + default: + Push(&S,ch[i]); break;//有效字符进栈 + }; + i++; + //将从栈底到栈顶的栈内字符传送至调用过程的数据区 + //ClearStack(S);//重置S为空栈 + }; + char pa[100]; + int j=0; + while(!StackEmpty(&S)){ + pa[j]= Pop(&S, &e); + j++; + }; + do{ + j--; + printf("%c",pa[j]); + }while(j>=0); + DestroyStack(&S); +} +int main(){ + Lineedit(); + return OK; +} + + diff --git a/2017-1/Sismark/alg3-3.cpp b/2017-1/Sismark/alg3-3.cpp new file mode 100644 index 00000000..4d948f12 --- /dev/null +++ b/2017-1/Sismark/alg3-3.cpp @@ -0,0 +1,111 @@ +#include +#include +#include + +typedef int Status; +typedef char SElemType; + +#define SIZE 100 +#define CREAT 10 + +#define OVERFLOW -1 +#define OK 1 +#define FALSE 0 +#define TRUE 1 +#define ERROR 0 + +typedef struct { + SElemType *base;//栈底指针 + SElemType *top;//栈顶指针 + int stacksize; +} SqStack; + +Status InitStack(SqStack &S){ + S.base = (SElemType * )malloc(SIZE*sizeof(SElemType)); + if(!S.base ) exit (OVERFLOW); //存储分配失败 + S.top =S.base ; + S.stacksize =SIZE; + return OK; +} + +Status Pop(SqStack &S , SElemType &e){ + if(S.top == S.base ) return ERROR; + e = *--S.top ; + return e; + return OK; +} + +Status ClearStack(SqStack &S){ + if(S.base ==S.top ) return ERROR; + while(S.base !=S.top ){ *S.top=*--S.top ; } + return *S.top ; + return OK; +} + +Status Push(SqStack &S , SElemType e){ + if(S.top -S.base >=S.stacksize ){ + S.base =(SElemType *)realloc(S.base , (S.stacksize + CREAT)*sizeof(SElemType)); + if(!S.base ) exit (OVERFLOW); + S.top = S.base + S.stacksize ; + S.stacksize += CREAT; + } + *S.top ++ = e; + return OK; +} + +Status DestroyStack(SqStack &S){ + S.top =NULL; + S.stacksize =0; + free(S.base ); + return OK; +} + +Status StackEmpty(SqStack &S){ +if(S.base ==S.top ) return TRUE; +else return FALSE; +} +void Lineedit(){ + int i=0; + char ch[200]="@11#2\n2@#3"; + SqStack S; + SElemType e=ch[i]; + InitStack(S); //构造空栈 、、、、、、、、、、、、、、、、、、、、、、、完成 + while(ch[i]!='\0'){ + e=ch[i]; + switch(ch[i]){ + case '#': + Pop(S,e); break;//仅当栈非空时退栈、、、、、、、、、、、、、、、、、、完成 + case '@': { + if(StackEmpty(S)) break; + while(e!='\n'){Pop(S,e);}; + if(StackEmpty(S)||e=='\n') { Push(S,e); break; } break; + }//重置为空栈、、、、、、、、、、、、、、、、、、、、完成 + default: + Push(S,ch[i]); break;//有效字符进栈、、、、、、、、、、、、、、、、、、、、完成 + } + i++; + //将从栈底到栈顶的栈内字符传送至调用过程的数据区 + //ClearStack(S);//重置S为空栈 + } + char pa[100]; + int j=0; + while(!StackEmpty(S)){ + pa[j]= Pop(S,e); + j++; + //printf("%c",e); + } + do{ + j--; + printf("%c",pa[j]); + }while(j>=0); + DestroyStack(S); +} +int main(){ + Lineedit(); + //printf("123321"); + return OK; +} +//whli##ilr#e(s#*s)\noutchar@\nputchar(*s=#++)\0 +//@11#2\n2@#3 +//abcd\nputchar@whli##ilr#e(s#*s)\noutchar@\nputchar(*s=#++)\0 + diff --git a/2017-1/Sismark/alg6.cpp b/2017-1/Sismark/alg6.cpp new file mode 100644 index 00000000..093b5108 --- /dev/null +++ b/2017-1/Sismark/alg6.cpp @@ -0,0 +1,103 @@ +#include +#include + +#define isLeaf(node) ((node)->left==NULL&&(node)->right==NULL) + +int i = 0;//二叉树第i层 +int a[10] = { 0 };//最多十层 每层初始化为0宽度 +int max = 0;//最大宽度 + +typedef char ElemType; +typedef int Status; + +typedef struct Node { + ElemType data; + struct Node* left; + struct Node* right; + struct Node* parent; +}Node, *Tree; + +Node* _newNode() { + Node* p = (Node*)malloc(sizeof(Node)); + p->data = 0; + p->left = p->parent = p->right = NULL; + return p; +} + +void _DeleteAllLeave(Tree *pt) { + if (isLeaf((*pt)->left)) { + free((*pt)->left); + (*pt)->left = NULL; + } + if (isLeaf((*pt)->right)) { + free((*pt)->right); + (*pt)->right = NULL; + } + if ((*pt)->left) _DeleteAllLeave(&(*pt)->left); + if ((*pt)->right) _DeleteAllLeave(&(*pt)->right); +} + +Tree newBinaryTree(char *str) { + if (*str == '\0') return NULL; + Tree t = _newNode(); + t->data = *str; + Node*current = t;//current表示当前节点 + for (char *ch = str + 1; *ch != '\0'; ch++) { + Node* p = _newNode(); + p->data = *ch; + if (current->left == NULL) { + current->left = p; + p->parent = current; + if (*ch != '#') current = p; + } + else { + if (current->right == NULL) { + current->right = p; + p->parent = current; + if (*ch != '#') current = p; + } + else { + current = current->parent; + ch -= 1; + } + } + } + _DeleteAllLeave(&t); + return t; +} +void Traverse(Tree t) { + printf("%c", t->data); + if (t->left) Traverse(t->left); + if (t->right) Traverse(t->right); +} +int Depth(Tree t) { + if (t == NULL) return 0; + int depth1, depth2; + depth1 = Depth(t->left) + 1; + depth2 = Depth(t->right) + 1; + if (depth1 > depth2) return depth1; + else return depth2; +} + +int Width(Tree t) { + if (t == NULL) return 0; + a[i]++;//第i层宽度加一 + if (max < a[i]) max = a[i]; + i++;//层数加一 + Width(t->left); + Width(t->right); + i--;//返回t所在的层 + return max; +} +int main() { + int m = rand() % 2; + Tree t; + //char str[] = "ABC##DE#G##F###"; + char str[] = "ABDG###EH##I#K##C#F##"; + t = newBinaryTree(str); + printf("遍历二叉树:"); + Traverse(t); + printf("\ndepth=%d\n", Depth(t)); + printf("width=%d\n", Width(t)); + return 0; +} diff --git a/2017-1/Sismark/alg7.c b/2017-1/Sismark/alg7.c new file mode 100644 index 00000000..17fb0a24 --- /dev/null +++ b/2017-1/Sismark/alg7.c @@ -0,0 +1,214 @@ +锘#include +#include + +#define INFINITY 65535//鏈澶у 鏃犵┓澶 +#define MAX 20//鏈澶ч《鐐逛釜鏁 +#define OVERFLOW -1 + +#define DEBUG 0 +#define debug_print if(DEBUG)printf + + +typedef int VRType; +typedef int InfoType; +typedef int VertexType; +typedef int Status; + +enum { OK, ERROR }; +enum{ FALSE =0, TRUE =1 }; +typedef enum { DG, DN, UDG, UDN } GraphKind; +typedef struct ArcCell { + VRType adj;//琛ㄧず鐩搁偦鍚 鏃犳潈鍥剧敤1鎴0 + InfoType *info;//璇ュ姬鐩稿叧淇℃伅鐨勬寚閽 +}ArcCell, AdjMatrix[MAX][MAX]; +typedef struct { + VertexType vexs[MAX];//椤剁偣鍚戦噺 + AdjMatrix arcs;//閭绘帴鐭╅樀 + int vexnum, arcnum;//褰撳墠椤剁偣鏁 寮ф暟 + GraphKind kind;//鍥剧殑绉嶇被 +}MGraph; +int LocateVex(MGraph G, int u) +{//杩斿洖椤剁偣u鐨勪綅缃 + int i; + //printf("u=%d ",u); + if (u < G.vexnum) { + for (i = 0; i < G.vexnum; i++) { + //printf("vexs[%d] = %d\n", i, G.vexs[i]); + if (G.vexs[i] == u) { + //printf("浣嶇疆=%d\n", i); + return i; + } + } + } + else { + //printf("閿欒浣嶇疆%d\n",u); + return -1; + } +} +void Build(MGraph *G, int v1, int v2) { + int i = LocateVex(*G, v1);//杩斿洖v1鐨勪綅缃负i + int j = LocateVex(*G, v2);//杩斿洖v2鐨勪綅缃负j + G->arcs[i][j].adj = 1;//寮鐨勬潈鍊 + G->arcs[j][i].adj = G->arcs[i][j].adj;//鐭╅樀瀵圭О + //printf(" ",v1,v2); + //printf("G->arcs[%d][%d].adj=%d\n",i,j,G->arcs[i][j].adj); +} +int CreateUDN(MGraph *G) { + //int IncInfo; + int v1=0, v2=0; + G->vexnum = 9;//椤剁偣鏁 + G->arcnum = 12;//寮ф暟 + for (int i = 0; i < G->vexnum; ++i) G->vexs[i] = i; + for (int i = 0; i < G->vexnum; ++i) { + for (int j = 0; j < G->vexnum; ++j) { + G->arcs[i][j].adj = -1; + G->arcs[i][j].info = NULL; + } + } + Build(G, 0, 1); Build(G, 0, 2); Build(G, 0, 3); Build(G, 0, 6); + Build(G, 1, 2); Build(G, 3, 4); Build(G, 3, 5); Build(G, 4, 5); + Build(G, 5, 7); Build(G, 6, 7); Build(G, 6, 8); Build(G, 7, 8); + return OK; +} +//闃熷垪 +typedef int QElemType; + +typedef struct QNode { + QElemType data; + struct QNode *next; + struct QNode *priou; +}QNode, *Queueptr; + +typedef struct { + Queueptr front; + Queueptr rear; + Queueptr lastOut; +}LinkQueue; + +Status InitQueue(LinkQueue *Q) {//鏋勯犵┖闃熷垪Q + debug_print(__FUNCTION__" \n"); + Q->lastOut = NULL; + Q->front = Q->rear = NULL; + return OK; +} + +int QueueEmpty(LinkQueue Q) { + if (Q.front == NULL && Q.rear == NULL) return TRUE; + else return FALSE; +} +void showQueue(LinkQueue *Q) { + QNode* p = Q->front; + if (p == NULL)printf("(empty)"); + while (p) { + printf("%d ",p->data); + p = p->next; + } + printf("\n"); +} +Status DeQueue(LinkQueue *Q, QElemType *e) {//涓嶅垹闄ら槦澶村厓绱犵敤e杩斿洖鍊 + *e = Q->front->data; + debug_print(__FUNCTION__" %d\n", *e); + if (QueueEmpty(*Q)) { + printf("Error : %s\n", __LINE__); + exit(-1); + } + Q->lastOut = Q->front; + Q->front = Q->front->next; + if (Q->front == NULL)Q->rear = NULL; + //if (Q->front != Q->rear) { + // QNode* temp = Q->front; + // Q->front = Q->front->next; + // free(temp); + //} + //else { + // free(Q->front); + // Q->front = Q->rear; + //} + + if(DEBUG)showQueue(Q); + return OK; +} + +Status EnQueue(LinkQueue *Q, QElemType e) {//鎻掑叆e涓洪槦灏惧厓绱 + debug_print(__FUNCTION__" %d\n",e); + Queueptr p; + p = (Queueptr)malloc(sizeof(QNode)); + if (!p) exit(OVERFLOW); + p->data = e; + p->next = NULL; + p->priou = Q->lastOut; + + if (!QueueEmpty(*Q)) { + Q->rear->next = p; + Q->rear = p; + } + else { + Q->front = p; + Q->rear = p; + } + + if(DEBUG)showQueue(Q); + return OK; +} +int FirstAdjvex(MGraph G, int v) +{//杩斿洖v鐨勭涓涓偦鎺ラ《鐐 + for (int i = 0; i < G.vexnum; i++) { + if (G.arcs[v][i].adj > 0&&v!=i) return i; + } + return -1; +} + +int NextAdjVex(MGraph G, int v, int w) {//杩斿洖V鐨勭浉瀵逛簬W鐨勪笅涓涓偦鎺ラ《鐐孤犅 + for (int i =w+1; i < G.vexnum; i++) { + if (G.arcs[v][i].adj > 0) return i; + } + return -1; +} + +void showList(QNode* p) { + if (p) { + showList(p->priou); + printf("%d ", p->data); + } +} +void BFSTraverse(MGraph G,int start,int end) {//骞垮害浼樺厛閬嶅巻鍥 + debug_print(__FUNCTION__"\n"); + int visited[MAX]; + int v; + for ( v = 0; v < G.vexnum; v++) visited[v] = FALSE; + LinkQueue Q; + InitQueue(&Q); + v = start; + if (!visited[v]) {//椤剁偣v灏氭湭璁块棶 + visited[v] = TRUE; + EnQueue(&Q, v);//v鍏ラ槦鍒 + while (!QueueEmpty(Q)) { + DeQueue(&Q, &v);//闃熷ご鍏冪礌鍑洪槦鍒楀苟璧嬪间负u + //debug_print("寮濮嬭闂%d鐨勯偦鑺傜偣\n",v); + for (int w = FirstAdjvex(G, v); w != -1; w = NextAdjVex(G,v, w)) { + //debug_print("褰撳墠閭昏妭鐐规槸%d\n", w); + if (!visited[w]) { + visited[w] = TRUE; + EnQueue(&Q, w);//鐩搁偦椤剁偣鍏ラ槦鍒 + if (w == end)break; + } + }//end for + if (Q.rear->data == end)break; + }//end while + } + showList(Q.rear); +} +int main() { + MGraph G; + CreateUDN(&G); + for (int i = 0; i < G.vexnum; i++) { + for (int j = 0; j < G.vexnum; j++) { + if (i != j) { + printf("%d<->%d:", i, j); + BFSTraverse(G, i, j); + printf("\n"); + } + } + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/Sismark/\344\272\214\345\217\211\346\240\221.c" "b/2017-1/Sismark/\344\272\214\345\217\211\346\240\221.c" new file mode 100644 index 00000000..c4565a43 --- /dev/null +++ "b/2017-1/Sismark/\344\272\214\345\217\211\346\240\221.c" @@ -0,0 +1,73 @@ +锘#include +#include + +#define NEWBIT (BiTNode*)malloc(sizeof(BiTNode)) +#define OVERFLOW -1 +#define OK 1 +#define FALSE 0 +#define TRUE 1 +#define ERROR 0 + +typedef char TElemType; +typedef int Status; + +Status size = 0; +typedef struct BiTNode { + TElemType data; + struct BiTNode *lchild;//宸﹀瀛愭寚閽 + struct BiTNode *rchild;//鍙冲瀛愭寚閽 +}BiTNode, *BiTree; + +Status CreateBiTree(BiTree *pT) { + //閫掑綊鍏堝簭寤虹珛浜屽弶鏍 + char data; + char ch[] = "ABDG###EH##I#K##C#F##"; + data = ch[size]; + size++; + if (data == '#') { + *pT = NULL; + } + else { + *pT = NEWBIT; + (*pT)->data = data;//鐢熸垚鏍硅妭鐐 + CreateBiTree(&(*pT)->lchild);//鏋勯犲乏瀛愭爲 + CreateBiTree(&(*pT)->rchild);//鏋勯犲彸瀛愭爲 + } + return OK; +} +void preOrderTraverse(BiTree T) { + if (T) { + printf("%c", T->data);//杈撳嚭鏍硅妭鐐 + preOrderTraverse(T->lchild);//閬嶅巻宸﹀瓙鏍 + preOrderTraverse(T->rchild);//閬嶅巻鍙冲瓙鏍 + } +} +void inOrderTraverse(BiTree T) { + if (T) { + inOrderTraverse(T->lchild); + printf("%c",T->data); + inOrderTraverse(T->rchild); + } +} +void postOrderTraverse(BiTree T) { + if (T) { + postOrderTraverse(T->lchild); + postOrderTraverse(T->rchild); + printf("%c", T->data); + } +} +int main() { + BiTree T; + T = NULL; + CreateBiTree(&T); + printf("鍏堝簭閬嶅巻:"); + preOrderTraverse(T); + printf("\n"); + printf("涓簭閬嶅巻:"); + inOrderTraverse(T); + printf("\n"); + printf("鍚庡簭閬嶅巻:"); + postOrderTraverse(T); + printf("\n"); + return 0; +} \ No newline at end of file diff --git "a/2017-1/Sismark/\344\272\214\345\217\211\346\240\221.cpp" "b/2017-1/Sismark/\344\272\214\345\217\211\346\240\221.cpp" new file mode 100644 index 00000000..23b68f7f --- /dev/null +++ "b/2017-1/Sismark/\344\272\214\345\217\211\346\240\221.cpp" @@ -0,0 +1,75 @@ +#include +#include + +#define NEWBIT (BiTNode*)malloc(sizeof(BiTNode)) +#define OVERFLOW -1 +#define OK 1 +#define FALSE 0 +#define TRUE 1 +#define ERROR 0 + +typedef char TElemType; +typedef int Status; + +typedef struct BiTNode { + TElemType data; + struct BiTNode *lchild;//左孩子指针 + struct BiTNode *rchild;//右孩子指针 +}BiTNode, *BiTree; + +Status CreateBiTree(BiTree *pT,int size) { + //递归先序建立二叉树 + char data; + char ch[] = "ABC##DE#G##F###"; + data = ch[size]; + size++; + if (data == '#') { + *pT = NULL; + }//AB#D##CE### + else { + *pT = NEWBIT; + (*pT)->data = data;//生成根节点 + CreateBiTree(&(*pT)->lchild,size);//构造左子树 + CreateBiTree(&(*pT)->rchild,size);//构造右子树 + } + return OK; +} +void preOrderTraverse(BiTree T) { + //先序遍历 + if (!T) printf("#"); + if (T) { + printf("%c", T->data);//输出根节点 + preOrderTraverse(T->lchild);//遍历左子树 + preOrderTraverse(T->rchild);//遍历右子树 + } +} +void inOrderTraverse(BiTree T) { + if (!T) printf("#"); + if (T) { + preOrderTraverse(T->lchild); + printf("%c",T->data); + preOrderTraverse(T->rchild); + } +} +void destroyBiTree(BiTree *pT) { + if (*pT) { + destroyBiTree(&(*pT)->lchild); + destroyBiTree(&(*pT)->rchild); + delete *pT; + *pT = NULL; + } +} +int main() { + BiTree T; + int size = 0; + T = NULL; + CreateBiTree(&T,size); + printf("先序遍历:"); + preOrderTraverse(T); + printf("\n"); + printf("中序遍历:"); + inOrderTraverse(T); + printf("\n"); + return 0; +} +//ABC##DE#G##F### \ No newline at end of file diff --git "a/2017-1/Sismark/\351\223\276\350\241\250\346\213\206\345\210\206.cpp" "b/2017-1/Sismark/\351\223\276\350\241\250\346\213\206\345\210\206.cpp" new file mode 100644 index 00000000..c0b5a384 --- /dev/null +++ "b/2017-1/Sismark/\351\223\276\350\241\250\346\213\206\345\210\206.cpp" @@ -0,0 +1,106 @@ +#include +#include +#include + +#define OK 0 +#define ERROR 1 +#define NEWNODE (LinkList)malloc(sizeof(LNode)) +typedef int ElemType; +typedef int Status; + +typedef struct LNode {//结点 + ElemType data; + struct LNode *next; +}LNode, *LinkList; + +Status CreateList_L(LinkList *pL, int n) { + int i; + *pL = NEWNODE; + (*pL)->next = NULL; + (*pL)->data = 0; + LinkList temp; + LinkList p; + p = *pL; + for (i = 0; i < n; i++) { + temp = NEWNODE; + temp->data = 0; + temp->next = NULL; + + temp->data = rand()%101; + + p->next = temp; + p = p->next; + + } + return OK; +} +Status SplitLink(LinkList *pL, LinkList *pL1, LinkList *pL2) { + int num = 0; + LinkList p; + LinkList p1; + LinkList p2; + *pL1= NEWNODE; + *pL2 = NEWNODE; + printf("建造两个链表\n"); + (*pL1)->next = (*pL2)->next = NULL; + (*pL1)->data = (*pL2)->data = 0; + printf("pL1=0x%p\n", pL1); + printf("pL2=0x%p\n", pL2); + p = (*pL)->next; + p1 = *pL1; + p2 = *pL2; + printf("开始拆分\n"); + while (p) { + printf("p=0x%p\n",p); + //创建新节点 + LinkList temp = NEWNODE; + temp->next = NULL; + temp->data = 0; + //给节点赋值 + temp->data = p->data; + //把节点归并到新链表 + p1->next = temp; + p1 = p1->next; + p = p->next; + printf("p1->data=%d\n", p1->data); + if (!p) break; + + temp = NEWNODE; + temp->next = NULL; + temp->data = 0; + //给节点赋值 + temp->data = p->data; + //把节点归并到新链表 + p2->next = temp; + p2 = p2->next; + p = p->next; + printf("p2->data=%d\n",p2->data); + } + return OK; +} +Status Traverse(LinkList *pL) { + *pL = (*pL)->next; + while (*pL) { + printf("%d ", (*pL)->data); + *pL = (*pL)->next; + } + printf("\n"); + return 0; +} +int main() { + srand(time(NULL)); + int n = rand()%10; + printf("n=%d\n",n); + LinkList L; + LinkList L1; + LinkList L2; + CreateList_L(&L, n); + SplitLink(&L, &L1, &L2); + printf("L="); + Traverse(&L); + printf("L1="); + Traverse(&L1); + printf("L2="); + Traverse(&L2); + return 0; +} \ No newline at end of file diff --git a/2017-1/Sv/2.12/2.12.c b/2017-1/Sv/2.12/2.12.c new file mode 100644 index 00000000..3f11f8a3 --- /dev/null +++ b/2017-1/Sv/2.12/2.12.c @@ -0,0 +1,76 @@ +#include +#include +#include "2.12.h" +//算法2.12:有序链表合并 +void MergeList_L(LinkList La, LinkList Lb, LinkList Lc) { + LinkList pa = La->next, pb = Lb->next; + LinkList pc = La = Lc;//La头节点作为Lc头节点 + while (pa&&pb) { + if (pa->data <= pb->data) { + pc->next = pa; + pc = pa; + pa = pa->next; + }//pa所指结点链接至pc所指节点之后 + else { + pc->next = pb; + pc = pb; + pb = pb->next; + }//pb所指结点连接至pc所指节点之后 + }//pa,pb其中一个为假(空值)时,停止循环 + pc->next = pa ? pa : pb;//插入剩余段 + free(Lb);//释放Lb头节点 +}//Mergel_L +//新建单链表 +void CreateList_L(LinkList L, int n) { + //L = (LinkList)malloc(sizeof(LNode)); + //srand(time(0)); + L->next = NULL; + int i; LinkList p; + printf("列表元素:\n"); + for (i = n; i > 0; --i) { + p = (LinkList)malloc(sizeof(LNode));//生成新结点 + p->data = rand() % 10 + 1; + printf("%d ", p->data); + //scanf_s("%d", &p->data);//输入元素值 + p->next = L->next; L->next = p;//插入到表头 + } + printf("\n"); +}//CreatList_L +//遍历输出: +void PRINT(LinkList L) +{ + LinkList p = L->next; + while (p) { + printf("%d ", p->data); + p = p->next; + } + printf("\n"); +} +void order(LinkList L, int n) { + int *p = (int *)malloc(n*sizeof(int)); + LinkList l = (LinkList)malloc(sizeof(LNode)); + l = L; + for (int i = 0; i < n; i++) { + p[i] = l->data; + l = l->next; + } + for (int i= 0; i< n; i++) + { + for (int j= 0; i+ j< n- 1; j++) + { + if (p[j]>p[j+ 1]) + { + int temp = p[j]; + p[j] = p[j + 1]; + p[j + 1] = temp; + } + } + } + l = L; + for (int i = 0; i < n; i++) { + l->data=p[i]; + l = l->next; + } +} + + diff --git a/2017-1/Sv/2.12/2.12.h b/2017-1/Sv/2.12/2.12.h new file mode 100644 index 00000000..ef5bcb49 --- /dev/null +++ b/2017-1/Sv/2.12/2.12.h @@ -0,0 +1,13 @@ +#include +#include +#include +typedef int ElemType; +typedef struct LNode { + ElemType data; + struct LNode* next; +}LNode, *LinkList; + +void MergeList_L(LinkList, LinkList, LinkList); +void CreateList_L(LinkList, int); +void PRINT(LinkList L); +void order(LinkList,int); diff --git a/2017-1/Sv/2.12/main.c b/2017-1/Sv/2.12/main.c new file mode 100644 index 00000000..176ea942 --- /dev/null +++ b/2017-1/Sv/2.12/main.c @@ -0,0 +1,26 @@ +#include "2.12.h" +int main() { + int n1, n2; + printf("列表长度:\n"); + srand(time(0)); + n1 = rand() % 10 + 1; + n2 = rand() % 10 + 1; + printf("%d %d\n", n1, n2); + LinkList l1,l2,l3; + l1 = (LinkList)malloc(sizeof(LNode)); + l2 = (LinkList)malloc(sizeof(LNode)); + l3 = (LinkList)malloc(sizeof(LNode)); + //l1->next = NULL; l2->next = NULL; + CreateList_L(l1, n1); + CreateList_L(l2, n2); + order(l1, n1); + order(l2, n2); + printf("链表1:\n"); + PRINT(l1); + printf("链表2:\n"); + PRINT(l2); + + MergeList_L(l1,l2,l3); + printf("\n合并链表:\n"); + PRINT(l3); +} \ No newline at end of file diff --git "a/2017-1/Sv/2.12/\347\273\223\346\236\234.png" "b/2017-1/Sv/2.12/\347\273\223\346\236\234.png" new file mode 100644 index 00000000..5a86648b Binary files /dev/null and "b/2017-1/Sv/2.12/\347\273\223\346\236\234.png" differ diff --git "a/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/Tree.c" "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/Tree.c" new file mode 100644 index 00000000..b3fe0d6d --- /dev/null +++ "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/Tree.c" @@ -0,0 +1,28 @@ +锘#include"Tree.h" +BiTNode* CreateBiTree(BiTNode *T,char *p){ + static char *ch=(char *)malloc(sizeof(char)); + ch=p; + //scanf_s("%c",&ch); + //getchar(); + if(*ch == '0') { + T = NULL; + } + else { + T = (BiTNode*)malloc(sizeof(BiTNode)); + if (!T){ + printf("OVERFLOW"); + exit(1); + } + T->data = *ch; // 鐢熸垚鏍圭粨鐐 + T->lchild= CreateBiTree(T->lchild,++ch); // 鏋勯犲乏瀛愭爲 + T->rchild= CreateBiTree(T->rchild,++ch); //` 鏋勯犲彸瀛愭爲 + } + return T; +} +void Preorder(BiTNode *T) { // 鍏堝簭閬嶅巻浜屽弶鏍 + if(T) { + Preorder(T->lchild); // 閬嶅巻宸﹀瓙鏍 + Preorder(T->rchild); // 閬嶅巻鍙冲瓙鏍 + printf("%c ",T->data); + } +} \ No newline at end of file diff --git "a/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/Tree.h" "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/Tree.h" new file mode 100644 index 00000000..2c5f1468 --- /dev/null +++ "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/Tree.h" @@ -0,0 +1,14 @@ +#include +#include +#define TElemType char +typedef struct BiTNode { // 结点结构 + TElemType data; + struct BiTNode *lchild, *rchild; // 左右孩子指针 +} BiTNode, *BiTree; +enum Status{ + OVERFLOW, + OK, + FALSE +}; +BiTNode* CreateBiTree(BiTNode *T,char *); +void Preorder(BiTNode *T); \ No newline at end of file diff --git "a/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/source.c" "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/source.c" new file mode 100644 index 00000000..6f81acd5 --- /dev/null +++ "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/source.c" @@ -0,0 +1,13 @@ +锘#include"Tree.h" +int main(){ + char *p=(char*)malloc(100*sizeof(char)); + //scanf("%s",p); + //鐢0浠f浛绌烘爲缁撶偣 + //p = "ABDG000EH00I0K00C0F00"; + p = "ABD0F000CE000"; + printf("娴嬭瘯瀛楃涓:\n%s\n", p); + BiTNode *T=NULL; + printf("鍚庡簭搴忓垪锛歕n"); + T=CreateBiTree(T,p); + Preorder(T); +} \ No newline at end of file diff --git "a/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/\346\265\213\350\257\225\346\210\252\345\233\276.png" "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/\346\265\213\350\257\225\346\210\252\345\233\276.png" new file mode 100644 index 00000000..5ba30a63 Binary files /dev/null and "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/\346\265\213\350\257\225\346\210\252\345\233\276.png" differ diff --git "a/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/\346\265\213\350\257\225\346\210\252\345\233\2762.png" "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/\346\265\213\350\257\225\346\210\252\345\233\2762.png" new file mode 100644 index 00000000..31cc3bbf Binary files /dev/null and "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213\345\217\212\351\201\215\345\216\206/\346\265\213\350\257\225\346\210\252\345\233\2762.png" differ diff --git "a/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/Tree.c" "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/Tree.c" new file mode 100644 index 00000000..df7d7fcb --- /dev/null +++ "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/Tree.c" @@ -0,0 +1,163 @@ +#include"Tree.h" + +BiTNode* CreateBiTree(BiTNode *T,char *p){ + static char *ch; + ch=p; + //scanf_s("%c",&ch); + //getchar(); + if(*ch == '0') { + T = NULL; + } + else { + T = (BiTNode*)malloc(sizeof(BiTNode)); + if (!T){ + printf("OVERFLOW"); + exit(1); + } + T->data = *ch; // 鐢熸垚鏍圭粨鐐 + T->lchild= CreateBiTree(T->lchild,++ch); // 鏋勯犲乏瀛愭爲 + T->rchild= CreateBiTree(T->rchild,++ch); //` 鏋勯犲彸瀛愭爲 + } + return T; +} +void Preorder(BiTNode *T) +{ + if (T) { + Preorder(T->lchild); // 閬嶅巻宸﹀瓙鏍 + Preorder(T->rchild); // 閬嶅巻鍙冲瓙鏍 + printf("%c ", T->data); + } +} +int Depth(BiTNode *T){ // 杩斿洖浜屽弶鏍戠殑娣卞害 + int depthval; + if(!T) { + depthval = 0; + } else { + int depthLeft = Depth(T->lchild); + int depthRight= Depth(T->rchild); + depthval = 1 + (depthLeft > depthRight ? depthLeft : depthRight); + } + return depthval; +} +Status InitQueue(LinkQueue *Q) { + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!Q->front)exit(0); + Q->front->Next = NULL; + return OK; +} +Status QueueEmpty(LinkQueue Q) { + if (Q.front == Q.rear)return TRUE; + else return FALSE; +} +Status EnQueue(LinkQueue *Q, QElemType e) { + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p)exit(OVERFLOW); + p->data = e; + p->Next = NULL; + Q->rear->Next = p; + Q->rear = p; + return OK; +} +Status DeQueue(LinkQueue *Q, QElemType *e) { + if (Q->front == Q->rear)return ERROR; + QueuePtr p; + p = Q->front->Next; + *e = p->data; + Q->front->Next = p->Next; + if (Q->rear == p)Q->rear = Q->front; + free(p); + return OK; +} +int LevelOrderTraverseTree(BiTNode *T){ + LinkQueue Q; + BiTNode *p=(BiTNode*)malloc(sizeof(BiTNode)); + InitQueue(&Q); //寤虹珛宸ヤ綔闃熷垪 + if(T==NULL){ + return 0; + } + int width=1;//鎵姹傚搴︽渶澶у硷紙杩斿洖鍊硷級 + int LastWidth=1;//涓婁竴灞傚搴︼紙杩欎竴娆¤寰幆閬嶅巻鐨勬鏁帮級 + int ThisWidth=0;//褰撳墠閬嶅巻灞傛暟鐨勫搴 + EnQueue(&Q,*T); + while(QueueEmpty(Q)==FALSE) { + while(LastWidth!=0){ + DeQueue(&Q, p); + LastWidth--; + //visit(p); + if(p->lchild) { + EnQueue(&Q, *p->lchild); + ThisWidth++; + } + if(p->rchild) { + EnQueue(&Q, *p->rchild); + ThisWidth++; + } + } + width=(ThisWidth>width)?ThisWidth:width; + LastWidth=ThisWidth; + ThisWidth=0; + } + return width; +} +Status CheckComplete(BiTNode *T){ + + int temp = 1; + int flag=0; + int depth=Depth(T); + printf("娣卞害锛%d\n",depth); + LinkQueue Q; + BiTNode *p=(BiTNode*)malloc(sizeof(BiTNode)); + InitQueue(&Q); //寤虹珛宸ヤ綔闃熷垪 + EnQueue(&Q,*T); + int LastWidth=1;//涓婁竴灞傚搴︼紙杩欎竴娆¤寰幆閬嶅巻鐨勬鏁帮級 + int ThisWidth=0;//褰撳墠閬嶅巻灞傛暟鐨勫搴 + + while(QueueEmpty(Q)==FALSE) { + static int d=2; + flag=2; + while(LastWidth!=0){ + // flag=0; + DeQueue(&Q, p); + //visit(p); + LastWidth--; + if(p->lchild) { + EnQueue(&Q, *p->lchild); + ThisWidth++; + } + if(p->rchild) { + EnQueue(&Q, *p->rchild); + ThisWidth++; + } + { + if (p->lchild == NULL&&p->rchild != NULL) { + return FALSE;//鍙湁鍙冲瀛愭病鏈夊乏瀛╁瓙锛屽垯鏄潪瀹屽叏浜屽弶鏍 + } + else if (p->lchild != NULL&&p->rchild != NULL) { + flag = 2;//宸﹀彸瀛╁瓙鍧囧瓨鍦 + } + else if (p->rchild == NULL&&flag == 2) { + flag = 1;//绔嬩釜flag锛屽鏋滄帴涓嬫潵閮芥槸绌猴紝鍒欏畬鍏ㄤ簩鍙夋爲 + } + else if (p->rchild == NULL&&p->lchild == NULL&&flag == 1) { + flag = 1; + } + } + if((p->rchild!=NULL||p->lchild!=NULL)&&flag==1){ + flag=0;//绔嬩笅flag鍚庯紝涔嬪悗涓嶄负绌猴紝鍒欓潪瀹屽叏浜屽弶鏍 + } + if(flag!=0&&LastWidth==0&&d==depth){ + return OK;// + } + } + if(flag==0){ + return FALSE; + } + LastWidth=ThisWidth; + temp=LastWidth; + ThisWidth=0; + d++; + // flag=0; + } + return FALSE; +} \ No newline at end of file diff --git "a/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/Tree.h" "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/Tree.h" new file mode 100644 index 00000000..dae9b883 --- /dev/null +++ "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/Tree.h" @@ -0,0 +1,36 @@ +#include +#include +#define TElemType char +typedef enum _Status{ + FALSE, + OK, + OVERFLOW, + TRUE, + ERROR, +}Status; + +typedef struct BiTNode { + TElemType data; + struct BiTNode *lchild, *rchild; +} BiTNode, *BiTree; + +typedef BiTNode QElemType; + +typedef struct QNode { + QElemType data; + struct QNode *Next; +}QNode, *QueuePtr; +typedef struct { + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +Status InitQueue(LinkQueue *Q); +Status QueueEmpty(LinkQueue Q); +Status EnQueue(LinkQueue *Q, QElemType e); +Status DeQueue(LinkQueue *Q, QElemType *e); +BiTNode* CreateBiTree(BiTNode *T,char *); +void Preorder(BiTNode *T); +int Depth(BiTree T); +int LevelOrderTraverseTree(BiTNode *T); +Status CheckComplete(BiTNode *T); diff --git "a/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/main.c" "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/main.c" new file mode 100644 index 00000000..732cc857 --- /dev/null +++ "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/main.c" @@ -0,0 +1,26 @@ +#include"Tree.h" +typedef BiTNode QElemType; +int main(){ + char *p=(char*)malloc(100*sizeof(char)); + //scanf("%s",p); + //鐢0浠f浛绌烘爲缁撶偣 + //p = "ABDG000EH00I0K00C0F00"; + // p = "ABD0F000CE000"; + // p = "ABDG00Q00EH00I00CQ00F00"; + //p="ABD00E00CF00G00"; + p="ABD00E00C0F00"; + printf("鍏堝簭娴嬭瘯瀛楃涓:\n%s\n", p); + BiTNode *T=NULL; + // printf("鍚庡簭搴忓垪锛歕n"); + T=CreateBiTree(T,p); + //Preorder(T); + //printf("楂樺害锛%d\n",Depth(T)); + printf("瀹藉害鏈澶у硷細%d\n",LevelOrderTraverseTree(T)); + if(CheckComplete(T)==FALSE){ + printf("闈炲畬鍏ㄤ簩鍙夋爲"); + } + else{ + printf("瀹屽叏浜屽弶鏍慭n"); + } + +} \ No newline at end of file diff --git "a/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\346\265\213\350\257\2251.png" "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\346\265\213\350\257\2251.png" new file mode 100644 index 00000000..255e80e7 Binary files /dev/null and "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\346\265\213\350\257\2251.png" differ diff --git "a/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\346\265\213\350\257\2252.png" "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\346\265\213\350\257\2252.png" new file mode 100644 index 00000000..b11a1f75 Binary files /dev/null and "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\346\265\213\350\257\2252.png" differ diff --git "a/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\346\265\213\350\257\2253.png" "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\346\265\213\350\257\2253.png" new file mode 100644 index 00000000..c2bc04e3 Binary files /dev/null and "b/2017-1/Sv/\344\272\214\345\217\211\346\240\221\351\253\230\345\272\246\346\267\261\345\272\246\350\256\241\347\256\227\357\274\214\345\210\244\346\226\255\346\230\257\345\220\246\345\256\214\345\205\250\344\272\214\345\217\211\346\240\221/\346\265\213\350\257\2253.png" differ diff --git "a/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/Sort.c" "b/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/Sort.c" new file mode 100644 index 00000000..1b751f23 --- /dev/null +++ "b/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/Sort.c" @@ -0,0 +1,137 @@ +#include"Sort.h" +void Print(RecordType r[], int length) +{ + for (int i = 1; i <= length; i++) + { + printf("%d ", r[i]); + } + printf("\n"); +} +void InsSort(RecordType r[], int length,int *CpNumber,int *MovNumber) +{ + *CpNumber = 0;//比较次数 + *MovNumber = 0;//移动次数 + int i, j; + for ( i = 2; i <=length; i++) { + r[0] = r[i]; /* 将待插入记录存放到临时变量中 */ + (*MovNumber)++; + j = i - 1; /* 最近一次排序结束的边界位置 */ + while ((++(*CpNumber))&&(r[0] < r[j])) { /* 寻找插入位置 */ + r[j + 1] = r[j]; /* 从小到大排序,大元素右移 */ + (*MovNumber)++; + j = j - 1; /* 待插入元素与已排序区下一个(左移一位)元素继续进行比较 */ + } + r[j + 1] = r[0]; /* 将待插入记录插入到已排序的序列中 */ + (*MovNumber)++; + } + Print(r, length); +} +void ShellInsert(RecordType r[], int length, int delta,int *CpNumber, int *MovNumber) { + int i,j; + *CpNumber = 0; + *MovNumber = 0; + for (i = 1 + delta; i <= length; i++) { /* 1+delta为第一个子序列的第二个元素的下标 */ + if ((++(*CpNumber))&&(r[i] < r[i - delta])) { + r[0] = r[i]; /* 备份r[i] (不做监视哨) */ + for (j = i - delta; j > 0 &&(++*(CpNumber))&& r[0] < r[j] ; j -= delta) { + r[j + delta] = r[j]; + (*MovNumber)++; + } + r[j + delta] = r[0]; + } + } +} + +/* 对记录数组r做希尔排序, length为数组的长度*/ +void ShellSort(RecordType r[], int length,int *CpNumber, int *MovNumber) { + int d = length / 2; + while (d >= 1) { + ShellInsert(r, length, d,CpNumber,MovNumber); + d = d / 2; + } + Print(r, length); +} + +void Copy(RecordType a[], RecordType b[], int c) +{ + for (int i = 0; i <= c; i++) + { + a[i] = b[i]; + } +} + +void BubbleSort(RecordType r[], int length,int *c,int *m) { + *c = 0; *m = 0; + int n = length,i,j,x; + int change = 1;//TRUE + for (i = 1; i <= n - 1 && change; ++i) { + change = 0;//FALSE + for (j = 1; j <= n - i; ++j) { + if ((++(*c))&&r[j] > r[j + 1]) { + x = r[j]; + r[j] = r[j + 1]; + r[j + 1] = x; + (*m)++; + change = 1; + } + } + } + Print(r, length); +} + +int QKPass(RecordType r[], int left, int right, int *c, int *m) { + int x = r[left]; /* 选择基准记录*/ + int low = left; + int high = right; + while (low < high) { + while (low < high &&(++(*c))&& r[high] >= x) { /* high从右到左找小于x.key的记录 */ + high--; + } + r[low] = r[high]; /* 找到小于x.key的记录,则进行交换 */ + ++(*m); + while (low < high && (++(*c)) && r[low] < x) { /* low从左到右找不小于x.key的记录 */ + low++; + } + r[high] = r[low]; /* 找到不小于x.key的记录,则交换*/ + ++(*m); + } + + r[low] = x; /* 将基准记录保存到low=high的位置 */ + ++(*m); + return low; /*返回基准记录的位置*/ +} + +void QKSort(RecordType r[], int low, int high,int *c,int *m) { + if (low < high) { + int pivot = QKPass(r, low, high,c,m); + QKSort(r, low, pivot - 1,c,m); + QKSort(r, pivot + 1, high,c,m); + } +} + +void SelectSort(RecordType r[], int length, int *c, int *m) +{ + *c = 0; *m = 0; + int n = length; + int i,k,j; + for (i = 1; i <= n-1; ++i) { + k = i; + for (j = i + 1; j <= n; ++j) { + if ((++(*c))&&r[j] < r[k]) { + k = j; + } + } + + if (k != i) { + int x = r[i]; + r[i] = r[k]; + r[k] = x; + (*m)++; + } + } + Print(r, 10); +} + + + + diff --git "a/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/Sort.h" "b/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/Sort.h" new file mode 100644 index 00000000..cb494218 --- /dev/null +++ "b/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/Sort.h" @@ -0,0 +1,12 @@ +#include +#include +#include +typedef int RecordType; +void Print(RecordType r[], int length); +void InsSort(RecordType r[], int length,int *a,int *b); +void ShellInsert(RecordType r[], int length, int delta,int *,int *); +void ShellSort(RecordType r[], int length,int *,int *); +void Copy(RecordType a[], RecordType b[], int c); +void BubbleSort(RecordType r[], int length, int *, int *); +void QKSort(RecordType r[], int low, int high,int *,int *); +void SelectSort(RecordType r[], int length, int *, int *); diff --git "a/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/text1.png" "b/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/text1.png" new file mode 100644 index 00000000..5f059913 Binary files /dev/null and "b/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/text1.png" differ diff --git "a/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/text2.png" "b/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/text2.png" new file mode 100644 index 00000000..82939907 Binary files /dev/null and "b/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/text2.png" differ diff --git "a/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/\346\272\220.c" "b/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/\346\272\220.c" new file mode 100644 index 00000000..ef68cfb5 --- /dev/null +++ "b/2017-1/Sv/\344\272\224\347\247\215\346\216\222\345\272\217/\346\272\220.c" @@ -0,0 +1,47 @@ +#include"Sort.h" +int main() +{ + RecordType r[11]; + RecordType temp[11]; + srand(time(0)); + temp[0] = 0; + for (int i = 1; i < 11; i++) + { + temp[i] = rand() % 100; + } + int a, b; + int *CpNumber, *MovNumber; + CpNumber = &a; MovNumber = &b; + printf("序列:\n"); + Print(temp, 10); + Copy(r, temp,10); + printf("\n插入排序:\n"); + InsSort(r, 10,CpNumber,MovNumber); + printf("比较次数:%d,移动次数:%d\n\n", (*CpNumber), (*MovNumber)); + Copy(r, temp, 10); + printf("希尔排序:\n"); + ShellSort(r, 10, CpNumber, MovNumber); + printf("比较次数:%d,移动次数:%d\n\n", (*CpNumber), (*MovNumber)); + Copy(r, temp, 10); + printf("冒泡排序:\n"); + BubbleSort(r, 10, CpNumber, MovNumber); + printf("比较次数:%d,移动次数:%d\n\n", (*CpNumber), (*MovNumber)); + Copy(r, temp, 10); + *CpNumber = 0; *MovNumber = 0; + printf("快速排序:\n"); + QKSort(r, 1, 10, CpNumber, MovNumber); + Print(r, 10); + printf("比较次数:%d,移动次数:%d\n\n", (*CpNumber), (*MovNumber)); + Copy(r, temp, 10); + printf("简单选择排序:\n"); + SelectSort(r, 10, CpNumber, MovNumber); + printf("比较次数:%d,移动次数:%d\n\n", (*CpNumber), (*MovNumber)); + + + + + + + + +} \ No newline at end of file diff --git "a/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/OHash.c" "b/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/OHash.c" new file mode 100644 index 00000000..dd3547d9 --- /dev/null +++ "b/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/OHash.c" @@ -0,0 +1,102 @@ +#include"OHash.h" +bool isSushu(int a) +{ + for (int i = 2; i < sqrt(a)+2; i++) + + { + if (a%i == 0)return false; + } + return true; +} +int hashsize(int size) +{ + int p = size; + if (p == 1)return 1; + while (!isSushu(p--));//从size开始往下的遇到的第一个素数,此处借鉴钟总 + return p + 1; +} +int Hash(KeyType K, int size) +{ + int p = hashsize(size); + return K%p; +} +bool EQ(int a, int b) { return(a == b); } +void collision(int *p, int *c){ (*p)++ ; (*c)++; } + +HashTable NewHashTable(int size) +{ + HashTable temp; + temp.count = 0; + temp.sizeindex = size; + temp.elem = (ElemType*)malloc(size * sizeof(ElemType)); + for (int i = 0; i < size; i++) + { + temp.elem[i].key = -1; + temp.elem[i].val = 0; + } + return temp; +} +void RecreateHashTable(HashTable *H) +{ + HashTable temp = NewHashTable(H->sizeindex * 2); + int a = H->sizeindex; + for (int i = 0; i elem[i]; + } + H->sizeindex *= 2; + H->elem= (ElemType*)malloc(H->sizeindex * sizeof(ElemType)); + for (int i = 0; i < H->sizeindex; i++) + { + H->elem[i].key = -1; H->elem[i].val = 0; + } + for (int i = 0; i < a; i++) + { + int p = Hash(temp.elem[i].key, H->sizeindex); + H->elem[p] = temp.elem[i]; + } +} +Status SearchHash(HashTable H, KeyType K, int *p, int *c) +{ + *p = Hash(K,H.sizeindex); // 求得哈希地址 + while (H.elem[*p].key != NULLKEY && !EQ(K, H.elem[*p].key)) { + collision(p, c); //冲突,求得下一探查地址 p + //printf("***"); + } + + if (EQ(K, H.elem[*p].key)) { // 查找成功,p返回待查数据元素位置 + return SUCCESS; + } + else { + return FAILED; // 查找不成功 + } +} +Status InsertHash(HashTable &H, ElemType e) +{ + // 查找不成功时插入数据元素e到开放地址哈希表H中,并返回OK + // 若冲突次数过大,则重建哈希表 + int a = 0,b=0; + int *c ;//用于记录冲突次数 + c = &a; + int *p ; + p = &b; + if (SearchHash(H, e.key, p, c) == SUCCESS) { // 表中已有与 e 有相同关键字的元素 + if ((*c) != 0) + { + printf("\n哈希冲突!次数:%d,关键字值:%d->%d\n", *c, e.key, e.val); + } + return DUPLICATE; + } + + else if (*c < hashsize(H.sizeindex) / 2) { + // 冲突次数 c 未达到上限,(阀值可调,此处仅为示例) + H.elem[*p] = e; + ++H.count; + return OK; + } + else { + // 重建哈希表,极端情况下可能哈希表“已满”。 + // 所以通常的重建过程都是先增大新哈希表的容量 + RecreateHashTable(&H); + } +} \ No newline at end of file diff --git "a/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/OHash.h" "b/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/OHash.h" new file mode 100644 index 00000000..215e8a2c --- /dev/null +++ "b/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/OHash.h" @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#define NULLKEY -1 +//----------------------------------- +typedef int KeyType; +typedef int ValueType; +typedef struct _ElemType { + KeyType key; // 关键字 + ValueType val; // 值 +#ifdef CHAINED_HASH + struct _ElemType *next; +#endif +} ElemType; +typedef struct _bool{ true,false }bool; +typedef struct { + ElemType *elem;//地址 + int count; // 当前数据元素个数 + int sizeindex; // hashsize[sizeindex]为当前容量 +} HashTable; +typedef enum _status +{ + SUCCESS, FAILED, DUPLICATE,OK +}Status; +//----------------------------------- +bool EQ(int, int); +bool isSushu(int a);//判断是否为素数 +int hashsize(int size); +void RecreateHashTable(HashTable *H); +Status SearchHash(HashTable H, KeyType K, int *p, int *c);//哈希表中查找关键值为K的记录,c记录次数,p返回地址 +int Hash(KeyType,int size); +Status InsertHash(HashTable &H, ElemType e); +HashTable NewHashTable(int size); + + + + + + diff --git "a/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/text1.png" "b/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/text1.png" new file mode 100644 index 00000000..cbc8b725 Binary files /dev/null and "b/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/text1.png" differ diff --git "a/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/text2.png" "b/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/text2.png" new file mode 100644 index 00000000..245d1958 Binary files /dev/null and "b/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/text2.png" differ diff --git "a/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/\346\272\220.c" "b/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/\346\272\220.c" new file mode 100644 index 00000000..192932e3 --- /dev/null +++ "b/2017-1/Sv/\345\223\210\345\270\214\350\241\250\345\273\272\347\253\213\346\237\245\346\211\276\357\274\210\347\272\277\346\200\247\350\247\243\345\206\263\345\206\262\347\252\201\357\274\211/\346\272\220.c" @@ -0,0 +1,54 @@ +#include"OHash.h" + +int main() +{ + int SIZE = 30; + HashTable HA = NewHashTable(SIZE); + srand(time(0)); + printf("插入20个元素:\n"); + ElemType array[20]; + for (int i = 0; i < 20; i++) + { + ElemType e; + e.key = rand() % 1000 + 1; + e.val = rand() % 1000 + 1; + array[i] = e; + InsertHash(HA, e); + } + + for (int i = 0; i < HA.sizeindex; i++) + { + printf("{ [%d] : %d -> %d }\t", i, HA.elem[i].key, HA.elem[i].val); + } + for (int i = 0; i < 3; i++) + { + ElemType temp = array[rand() % 20]; + int p,c; + if (SearchHash(HA, temp.key, &p, &c) == SUCCESS) + { + printf("查找关键字为%d,地址:%d\n", temp.key, p); + } + } + for (int i = 0; i < 3; i++) + { + ElemType temp; + temp.key = rand() % 1000 + 1; + temp.val = rand() % 1000 + 1; + int p, c; + if (SearchHash(HA, temp.key, &p, &c) == SUCCESS) + { + printf("查找关键字为%d,地址:%d\n", temp.key, p); + } + else + { + printf("查找关键字为%d,查找失败!\n", temp.key); + } + } + printf("\n重建哈希表:\n"); + RecreateHashTable(&HA); + for (int i = 0; i < HA.sizeindex; i++) + { + printf("{ [%d] : %d -> %d }\t", i, HA.elem[i].key, HA.elem[i].val); + } + +} \ No newline at end of file diff --git "a/2017-1/Sv/\345\234\250\347\272\27713\351\242\230\345\245\207\345\201\266\345\210\206\347\246\273/List.c" "b/2017-1/Sv/\345\234\250\347\272\27713\351\242\230\345\245\207\345\201\266\345\210\206\347\246\273/List.c" new file mode 100644 index 00000000..3c948762 --- /dev/null +++ "b/2017-1/Sv/\345\234\250\347\272\27713\351\242\230\345\245\207\345\201\266\345\210\206\347\246\273/List.c" @@ -0,0 +1,54 @@ +#include"List.h" +Status CreateList(LinkList L,int n) { + //L = (LinkList)malloc(sizeof(LNode)); + LinkList p; + L->next = NULL; + L->data = n; + int i,temp; + for (i = n; i > 0; i--) { + p = (LinkList)malloc(sizeof(LNode)); + temp = rand() % 100 + 1; + p->data = temp; + p->next = L->next; L->next = p; + } + return OK; +} +Status SperateList(LinkList L, LinkList L1, LinkList L2, int n) { + LinkList p = L->next; + LinkList p1 = L1->next = (LinkList)malloc(sizeof(LNode)); + LinkList p2 = L2->next = (LinkList)malloc(sizeof(LNode)); + int n1=0, n2=0; + for (int i = 0; i < n; i++) { + switch ((i+1) % 2) { + case 1: + p1->next = (LinkList)malloc(sizeof(LNode)); + p1->data = p->data; + p1 = p1->next; + n1++; + break; + case 0: + p2->next = (LinkList)malloc(sizeof(LNode)); + p2->data = p->data; + p2 = p2->next; + n2++; + break; + } + p = p->next; + } + L1->data = n1; + L2->data = n2; + p1 = L1; + p2 = L2; + return OK; +} +void PRINT(LinkList L) + +{ + LinkList p = L->next; + int n = L->data; + for (int i = 0; i < n;i++){ + printf("%d ", p->data); + p = p->next; + } + printf("\n"); +} \ No newline at end of file diff --git "a/2017-1/Sv/\345\234\250\347\272\27713\351\242\230\345\245\207\345\201\266\345\210\206\347\246\273/List.h" "b/2017-1/Sv/\345\234\250\347\272\27713\351\242\230\345\245\207\345\201\266\345\210\206\347\246\273/List.h" new file mode 100644 index 00000000..e684dd12 --- /dev/null +++ "b/2017-1/Sv/\345\234\250\347\272\27713\351\242\230\345\245\207\345\201\266\345\210\206\347\246\273/List.h" @@ -0,0 +1,18 @@ +#include +#include +#include +typedef int ElemType; +typedef struct LNode { + ElemType data; + struct LNode *next; +}LNode,*LinkList; + +typedef enum { + OK, + ERROR, + OVERFLOW +}Status; +Status CreateList(LinkList , int ); +Status SperateList(LinkList, LinkList, LinkList, int); +void PRINT(LinkList L); + diff --git "a/2017-1/Sv/\345\234\250\347\272\27713\351\242\230\345\245\207\345\201\266\345\210\206\347\246\273/text.png" "b/2017-1/Sv/\345\234\250\347\272\27713\351\242\230\345\245\207\345\201\266\345\210\206\347\246\273/text.png" new file mode 100644 index 00000000..225f46a0 Binary files /dev/null and "b/2017-1/Sv/\345\234\250\347\272\27713\351\242\230\345\245\207\345\201\266\345\210\206\347\246\273/text.png" differ diff --git "a/2017-1/Sv/\345\234\250\347\272\27713\351\242\230\345\245\207\345\201\266\345\210\206\347\246\273/\346\272\220.c" "b/2017-1/Sv/\345\234\250\347\272\27713\351\242\230\345\245\207\345\201\266\345\210\206\347\246\273/\346\272\220.c" new file mode 100644 index 00000000..91fb90c0 --- /dev/null +++ "b/2017-1/Sv/\345\234\250\347\272\27713\351\242\230\345\245\207\345\201\266\345\210\206\347\246\273/\346\272\220.c" @@ -0,0 +1,18 @@ +#include"List.h" +int main() { + srand(time(0)); + int n = rand() % 10 + 1; + LinkList L = (LinkList)malloc(sizeof(LNode)); + LinkList L1 = (LinkList)malloc(sizeof(LNode)); + LinkList L2 = (LinkList)malloc(sizeof(LNode)); + CreateList(L, n); + printf("随机创建:\n"); + PRINT(L); + printf("\n按序列奇偶分离:\n"); + SperateList(L, L1, L2, n); + printf("奇列:\n"); + PRINT(L1); + printf("偶列:\n"); + PRINT(L2); + //时间复杂度O(n),奇偶分离创建列表于一个函数 +} \ No newline at end of file diff --git "a/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/graph.c" "b/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/graph.c" new file mode 100644 index 00000000..398626a1 --- /dev/null +++ "b/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/graph.c" @@ -0,0 +1,124 @@ +#include "graph.h" + + +//闃熷垪鍩烘湰鎿嶄綔鈥斺斺斺斺斺斺斺斺斺斺斺 +void InitQueue(LinkQueue *Q) { + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + Q->front->Next = Q->rear->Next= NULL; + Q->front->Priou=NULL; +} +void EnQueue(LinkQueue *Q, QElemType e) { + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + p->data = e; + p->Next = NULL; + p->Priou = Q->front; + Q->rear->Next = p; + Q->rear = p; +} +void DeQueue(LinkQueue* Q, QElemType* e) { + Q->front = Q->front->Next; + *e = Q->front->data; +} +Status QueueEmpty(LinkQueue Q) { + if (Q.front == Q.rear)return TRUE; + else return FALSE; +} +Status DestroyQueue(LinkQueue *Q){ + while (Q->front) { + Q->rear = Q->front->Next; + free(Q->front); + Q->front = Q->rear; + } + return OK; +} +//鈥斺斺斺斺斺斺斺斺斺斺斺斺斺斺斺斺斺 +void CreatGraph(MGraph *G,AdjMatrix A){ + //閽堝鏈鍒涘缓鍥 + + for(int i=0;i<9;i++){ + for(int j=0;j<9;j++){ + G->arcs[i][j]=A[i][j]; + } + } + G->vexnum=9; + G->arcnum=12; + for(int i=0;i<9;i++){ + G->vexs[i]=0;//鍏ㄩ儴璁剧疆涓轰负璁块棶 + } +} +int FirstAdjVex(MGraph G,QElemType e){ + for(int i=0;iNext; +// while(p->Priou!=NULL){ +// p=p->Priou; +// } +// p=p->Next; +// Q.front=p;//渚夸簬閿姣侀槦鍒楋紱 + int temp[10]; + int count=0; + QueuePtr p=Q.rear; + for(int i=0;i<10;i++){ + temp[i]=0; + } + while (p->Priou!=NULL) { + temp[count++]=p->data; + p=p->Priou; + } + printf("%d->%d:",a,b); + for(int i=count-1;i>=0;i--){ + printf("%d ",temp[i]+1); + } + printf("\n"); + QueuePtr q=Q.front->Next; + while(q->Priou!=NULL){ + q=q->Priou; + } + q=q->Next; + Q.front=q;//渚夸簬閿姣侀槦鍒楋紱 + DestroyQueue(&Q); + } + diff --git "a/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/graph.h" "b/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/graph.h" new file mode 100644 index 00000000..a7e96a6f --- /dev/null +++ "b/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/graph.h" @@ -0,0 +1,41 @@ +// 鍥剧殑鏁扮粍瀛樺偍琛ㄧず鈥斺斺斺斺斺斺斺 +#include +#include + +#define MAX_VERTEX_NUM 9 +typedef int VRType; +typedef int QElemType; + +typedef enum { + FALSE, + OK, + OVERFLOW, + TRUE, + ERROR, +}Status; + +typedef VRType AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; +typedef struct { + int vexs[9]; + AdjMatrix arcs; + int vexnum,arcnum;//鍥剧殑椤剁偣鍜屽姬鏁 +}MGraph; + +//闃熷垪鍩烘湰鎿嶄綔鈥斺斺斺斺斺 +typedef struct QNode { + QElemType data; + struct QNode *Next,*Priou; +}QNode, *QueuePtr; +typedef struct { + QueuePtr front; + QueuePtr rear; +}LinkQueue; +void InitQueue(LinkQueue *Q); +void EnQueue(LinkQueue *Q, QElemType e); +void DeQueue(LinkQueue *Q, QElemType *e) ; +Status QueueEmpty(LinkQueue Q); +Status DestroyQueue(LinkQueue *Q); +//鍥剧殑鎿嶄綔鈥斺斺斺斺斺 +void CreatGraph(MGraph *,AdjMatrix ); +int FirstAdjVex(MGraph,QElemType); +void BFSTraverse(MGraph G,int a,int b); diff --git "a/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/main.c" "b/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/main.c" new file mode 100644 index 00000000..59a3aa4f --- /dev/null +++ "b/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/main.c" @@ -0,0 +1,22 @@ +#include"graph.h" +int main(){ + AdjMatrix A= { + 1,1,1,1,0,0,1,0,0 , + 1,1,1,0,0,0,0,0,0 , + 1,1,1,0,0,0,0,0,0 , + 1,0,0,1,1,1,0,0,0 , + 0,0,0,1,1,1,0,0,0 , + 0,0,0,1,1,1,0,1,0 , + 1,0,0,0,0,0,1,1,1 , + 0,0,0,0,0,1,1,1,1 , + 0,0,0,0,0,0,1,1,1 + }; + MGraph G; + CreatGraph(&G, A); + for(int i=1;i<10;i++){ + for(int j=1;j<10;j++){ + if(i!=j)BFSTraverse(G, i, j); + } + } + //BFSTraverse(G, 9, 5); +} \ No newline at end of file diff --git "a/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/\346\265\213\350\257\225(\344\270\212).png" "b/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/\346\265\213\350\257\225(\344\270\212).png" new file mode 100644 index 00000000..c25a49fc Binary files /dev/null and "b/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/\346\265\213\350\257\225(\344\270\212).png" differ diff --git "a/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/\346\265\213\350\257\225(\344\270\213).png" "b/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/\346\265\213\350\257\225(\344\270\213).png" new file mode 100644 index 00000000..c377c8f9 Binary files /dev/null and "b/2017-1/Sv/\345\271\277\345\272\246\351\201\215\345\216\206\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/\346\265\213\350\257\225(\344\270\213).png" differ diff --git "a/2017-1/Sv/\346\225\260\346\215\256\347\273\223\346\236\204\350\277\267\345\256\253\346\261\202\350\247\243/main.c" "b/2017-1/Sv/\346\225\260\346\215\256\347\273\223\346\236\204\350\277\267\345\256\253\346\261\202\350\247\243/main.c" new file mode 100644 index 00000000..387cb6f0 --- /dev/null +++ "b/2017-1/Sv/\346\225\260\346\215\256\347\273\223\346\236\204\350\277\267\345\256\253\346\261\202\350\247\243/main.c" @@ -0,0 +1,50 @@ +// +// main.c +// 杩峰姹傝В +// +// Created by qwe on 17/3/27. +// Copyright 漏 2017骞 qwe. All rights reserved. +// + +#include + +#include"maze.h" +int main() +{ + MazeType maze={ + 1,1,1,1,1,1,1,1,1,1, + 1,0,0,1,0,0,0,1,0,1, + 1,0,0,1,0,0,0,1,0,1, + 1,0,0,0,0,1,1,0,0,1, + 1,0,1,1,1,0,0,0,0,1, + 1,0,0,0,1,0,0,0,0,1, + 1,0,1,0,0,0,1,0,0,1, + 1,0,1,1,1,0,1,1,0,1, + 1,1,0,0,0,0,0,0,0,1, + 1,1,1,1,1,1,1,1,1,1 + }; + printf("褰撳墠杩峰锛0浣嶅彲閫氳繃锛1涓哄锛夛細\n"); + for(int i=0;i<10;i++){ + for(int j=0;j<10;j++){ + printf("%d ",maze[i][j]); + } + printf("\n"); + } + printf("杩峰鍏ュ彛浣嶇疆锛氾紙1锛1锛塡n"); + printf("杩峰鍑哄彛浣嶇疆锛氾紙8锛8锛塡n"); + PosType start; + start.x=1; + start.y=1; + PosType end; + end.x=8; + end .y=8; +//妫楠屾槸鍚︽敼鍙樿糠瀹細 +MazePath(maze,start,end); +// for(int i=0;i<10;i++){ +// for(int j=0;j<10;j++){ +// printf("%d ",maze[i][j]); +// } +// printf("\n"); +// } + printf("锛堟敞锛3涓哄嚭杩峰璺緞锛塡n"); +} diff --git "a/2017-1/Sv/\346\225\260\346\215\256\347\273\223\346\236\204\350\277\267\345\256\253\346\261\202\350\247\243/maze.c" "b/2017-1/Sv/\346\225\260\346\215\256\347\273\223\346\236\204\350\277\267\345\256\253\346\261\202\350\247\243/maze.c" new file mode 100644 index 00000000..4a8eff0b --- /dev/null +++ "b/2017-1/Sv/\346\225\260\346\215\256\347\273\223\346\236\204\350\277\267\345\256\253\346\261\202\350\247\243/maze.c" @@ -0,0 +1,111 @@ +#include "maze.h" +Status InitStack(SqStack *S){ + S->base = (SElemType *) malloc (STACK_INIT_SIZE*sizeof(SElemType)); + if (!S->base) { exit(OVERFLOW); } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status StackEmpty(SqStack S){ + if(S.base == S.top){return TRUE; } + else { return FALSE; } +} + +Status Pop(SqStack *S,SElemType *e){ + if(S->top == S->base){ return ERROR;} +*e = *(--S->top); + return OK; +} + +Status Push(SqStack *S,SElemType e){ + if (S->top-S->base>=S->stacksize){ + S->base = (SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); + if(!S->base){exit(OVERFLOW);} + S->top = S->base + S->stacksize; + S ->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +}//Push +Status MazePath(MazeType maze,PosType start,PosType end){ + SqStack S; + InitStack(&S); + SElemType e; + MazeType footprint; + for(int i=0;i<10;i++){ + for(int j=0;j<10;j++){ + footprint[i][j]=0; + } + } + PosType curpos = start;//褰撳墠浣嶇疆涓哄叆鍙d綅缃 + int curstep = 1;//鎺㈢储绗竴姝 + do{ + if(Pass(curpos,maze,footprint)){//褰撳墠浣嶇疆鍙氳繃 + FootPrint(curpos, footprint); + e.ord=curstep;e.seat=curpos;e.di=1; + Push(&S,e); + if((curpos.x==end.x)&&(curpos.y==end.y)){ + PRINT(maze,&S) ; + return (TRUE); + } + curpos=NextPos(curpos, 1,maze); + curstep++; + } + else{//褰撳墠浣嶇疆涓嶈兘閫氳繃 + if(!StackEmpty(S)){ + Pop(&S,&e); + while(e.di==4&&!StackEmpty(S)){ + MarkPrint(e.seat,footprint);Pop(&S,&e);//鐣欎笅涓嶈兘閫氳繃鐨勬爣璁板苟閫鍥炰竴姝 + } + if(e.di<4){ + e.di++; + //e=NewElem(e.ord, e.seat, e.di); + Push(&S,e); + curpos=NextPos(e.seat,e.di,maze); + } + } + } + }while(!StackEmpty(S)); + return FALSE; +} +int Pass(PosType curpos,MazeType maze,MazeType footprint){ + if(maze[curpos.x][curpos.y]==0&&footprint[curpos.x][curpos.y]==0){return 1;} + else {return 0;} +} +void FootPrint(PosType curpos,MazeType footprint){ + footprint[curpos.x][curpos.y]=1; +} +PosType NextPos(PosType a,int b,MazeType maze){ + if(b==1&&maze[a.x][a.y+1]==0){a.y++;} + else if (b==2&&maze[a.x+1][a.y]==0){a.x++;} + else if(b==3&&maze[a.x][a.y-1]==0){a.y--;} + else if(b==4&&maze[a.x-1][a.y]==0){a.x--;} + return a; +} +void MarkPrint(PosType seat,MazeType footprint){ + footprint[seat.x][seat.y]=1; +} +SElemType NewElem(int curstep,PosType b,int di){ + SElemType e; + e.ord=curstep; + e.seat = b; + e.di = di; + return e; +} +void PRINT(MazeType maze,SqStack* S){ + SElemType *p; + p=S->base; + while(p!=S->top){ + maze[p->seat.x][p->seat.y]=3; + p++; + } + for(int i=0;i<10;i++){ + for(int j=0;j<10;j++){ + printf("%d ",maze[i][j]); + } + printf("\n"); + } +} + + diff --git "a/2017-1/Sv/\346\225\260\346\215\256\347\273\223\346\236\204\350\277\267\345\256\253\346\261\202\350\247\243/maze.h" "b/2017-1/Sv/\346\225\260\346\215\256\347\273\223\346\236\204\350\277\267\345\256\253\346\261\202\350\247\243/maze.h" new file mode 100644 index 00000000..79132be4 --- /dev/null +++ "b/2017-1/Sv/\346\225\260\346\215\256\347\273\223\346\236\204\350\277\267\345\256\253\346\261\202\350\247\243/maze.h" @@ -0,0 +1,54 @@ +#include +#include +//#define SElemType int +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +typedef struct _PosType{ + int x; + int y; +}PosType; + +typedef struct _SElemType { + int ord; + PosType seat; + int di; +}SElemType; + +typedef struct _SqStack { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +typedef enum{ + FALSE, + OK, + OVERFLOW, + TRUE, + ERROR +}Status; + +typedef int MazeType[10][10]; + // int (*array)[10]; +// maze={1,1,1,1,1,1,1,1,1,1, +// 1,0,0,1,0,0,0,1,0,1, +// 1,0,0,1,0,0,0,1,0,1, +// 1,0,0,0,0,1,1,0,0,1, +// 1,0,1,1,1,0,0,0,0,1, +// 1,0,0,0,1,0,0,0,0,1, +// 1,0,1,0,0,0,1,0,0,1, +// 1,0,1,1,1,0,1,1,0,1, +// 1,1,0,0,0,0,0,0,0,1, +// 1,1,1,1,1,1,1,1,1,1} + +Status InitStack(SqStack *S); +Status StackEmpty(SqStack S); +Status Pop(SqStack *S,SElemType *e); +Status Push(SqStack *S,SElemType e); +Status MazePath(MazeType maze,PosType start,PosType end); +SElemType NewElem(int ,PosType,int); +int Pass(PosType curpos,MazeType maze,MazeType); +void FootPrint(PosType curpos,MazeType); +PosType NextPos(PosType a,int b,MazeType maze); +void MarkPrint(PosType,MazeType); +void PRINT(MazeType,SqStack*); diff --git "a/2017-1/Sv/\346\225\260\346\215\256\347\273\223\346\236\204\350\277\267\345\256\253\346\261\202\350\247\243/\345\261\217\345\271\225\345\277\253\347\205\247 2017-03-29 \344\270\213\345\215\2102.05.05.png" "b/2017-1/Sv/\346\225\260\346\215\256\347\273\223\346\236\204\350\277\267\345\256\253\346\261\202\350\247\243/\345\261\217\345\271\225\345\277\253\347\205\247 2017-03-29 \344\270\213\345\215\2102.05.05.png" new file mode 100644 index 00000000..89b08425 Binary files /dev/null and "b/2017-1/Sv/\346\225\260\346\215\256\347\273\223\346\236\204\350\277\267\345\256\253\346\261\202\350\247\243/\345\261\217\345\271\225\345\277\253\347\205\247 2017-03-29 \344\270\213\345\215\2102.05.05.png" differ diff --git "a/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3.3.h" "b/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3.3.h" new file mode 100644 index 00000000..a670250a --- /dev/null +++ "b/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3.3.h" @@ -0,0 +1,47 @@ +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +const char OP[7] = { '+','-','*','/','(',')','#' }; +const int PrecedenceTable[7][7] = { + {1,1,0,0,0,1,1}, + {1,1,0,0,0,1,1}, + {1,1,1,1,0,1,1}, + {1,1,1,1,0,1,1}, + {0,0,0,0,0,0,0}, + {0,0,0,0,0,0,0} +}; +typedef char SElemType; +typedef struct _SqStack { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; +typedef struct _COUNT { + int *base; + int *top; + int stacksize; +}COUNT; +typedef enum { + FALSE, + OK, + OVERFLOW, + TRUE, + ERROR, +}Status; +Status InitStack(SqStack *S); +Status Pop(SqStack *S, SElemType *e); +Status Push(SqStack *S, SElemType e); +Status StackEmpty(SqStack S); +Status GetTop(SqStack S, SElemType *e); +void transform(char *, char *);//中缀式转化为后缀式 +int IN(char); +void Pass(char *, char); +int Precede(char, char); +int count(char *suffix); +int operate(char, int, int); +Status nInitStack(COUNT *S); +Status nStackEmpty(COUNT S); +Status nPop(COUNT *S, int *e); +Status nPush(COUNT *S, int e); +Status nGetTop(COUNT S, int *e); \ No newline at end of file diff --git "a/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/main.c" "b/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/main.c" new file mode 100644 index 00000000..369026bb --- /dev/null +++ "b/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/main.c" @@ -0,0 +1,14 @@ + +#include"3.3.h" +int main() { + char a[100] = {"4+(3*3-6)/3#"/*"(8+4)*3/2-1#"*/}; + printf("中缀表达式为:\n"); + puts(a); + char b[100] = { '\0' }; + transform(b,a); + printf("\n后缀表达式为:\n"); + puts(b); + int c = count(b); + printf("\n结果为:\n"); + printf("%d", c); +} \ No newline at end of file diff --git "a/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\346\265\213\350\257\2251.png" "b/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\346\265\213\350\257\2251.png" new file mode 100644 index 00000000..302ae462 Binary files /dev/null and "b/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\346\265\213\350\257\2251.png" differ diff --git "a/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\346\265\213\350\257\2252.png" "b/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\346\265\213\350\257\2252.png" new file mode 100644 index 00000000..234df792 Binary files /dev/null and "b/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\346\265\213\350\257\2252.png" differ diff --git "a/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" "b/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" new file mode 100644 index 00000000..84594d6c --- /dev/null +++ "b/2017-1/Sv/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" @@ -0,0 +1,202 @@ +#include "3.3.h" + +Status InitStack(SqStack *S) { + S->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); + if (!S->base) { exit(OVERFLOW); } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status StackEmpty(SqStack S) { + if (S.base == S.top) { return TRUE; } + else { return FALSE; } +} + +Status Pop(SqStack *S, SElemType *e) { + if (S->top == S->base) { return ERROR; } + *e = *(--S->top); + return OK; +} + +Status Push(SqStack *S, SElemType e) { + if (S->top - S->base >= S->stacksize) { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT)*sizeof(SElemType)); + if (!S->base) { exit(OVERFLOW); } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +}//Push + +Status GetTop(SqStack S, SElemType *e) { + //若栈不空,则用e返回S的栈顶元素,并返回OK,否则返回ERROR + if (S.top == S.base) { return ERROR; } + *e = *(S.top - 1); + return OK; +} + +Status nInitStack(COUNT *S) { + S->base = (int *)malloc(STACK_INIT_SIZE*sizeof(int)); + if (!S->base) { exit(OVERFLOW); } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status nStackEmpty(COUNT S) { + if (S.base == S.top) { return TRUE; } + else { return FALSE; } +} + +Status nPop(COUNT *S, int *e) { + if (S->top == S->base) { return ERROR; } + *e = *(--S->top); + return OK; +} + +Status nPush(COUNT *S, int e) { + if (S->top - S->base >= S->stacksize) { + S->base = (int *)realloc(S->base, (S->stacksize + STACKINCREMENT)*sizeof(int)); + if (!S->base) { exit(OVERFLOW); } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +}//Push + +Status nGetTop(COUNT S, int *e) { + //若栈不空,则用e返回S的栈顶元素,并返回OK,否则返回ERROR + if (S.top == S.base) { return ERROR; } + *e = *(S.top - 1); + return OK; +} + +int IN(char ch) { + int i = 0,flag=0; + for (i = 0; i < 7; i++) { + if (ch == OP[i]) { flag = 1; break; } + } + return flag; +} + +void Pass(char *suffix, char ch) { + static int i = 0; + suffix[i] = ch; + i++; +} + +int Precede(char c, char ch) { + int i, j; + switch (c) { + case'+':i = 0; break; + case'-':i = 1; break; + case'*':i = 2; break; + case'/':i = 3; break; + case'(':i = 4; break; + case')':i = 5; break; + case'#':i = 6; break; + } + switch (ch) { + case'+':j = 0; break; + case'-':j = 1; break; + case'*':j = 2; break; + case'/':j = 3; break; + case'(':j = 4; break; + case')':j = 5; break; + case'#':j = 6; break; + } + return PrecedenceTable[i][j]; +} + +int operate(char ch, int t1, int t2) { + switch (ch) { + case'+':return t2 + t1; break; + case'-':return t2 - t1; break; + case'*':return t2*t1; break; + case '/':return t2 / t1; break; + } + +} + +void transform(char *suffix, char *exp) { + SqStack S; + SElemType c='#' ; + InitStack(&S); + Push(&S, c); + char *p;p = exp; + char ch = *p; + while (StackEmpty(S)==FALSE) { + if (!IN(ch)) { + Pass(suffix,ch); + } + else { + switch (ch) { + case'(': + Push(&S, ch); + break; + case')': + Pop(&S, &c); + while (c != '(') { + Pass(suffix, c); + Pop(&S,&c); + } + break; + default: + // Precede函数是判定运算符的栈顶运算符与读入的运算符之间优先关系的函数 + while (GetTop(S, &c) == OK && (Precede(c, ch))) { + Pass(suffix, c); + Pop(&S, &c); + } + if (ch != '#') { + Push(&S, ch); + } + break; + }//switch + }//else + if (ch != '#') { + p++; + ch = *p; + } + else { + Pop(&S, &ch); + Pass(suffix, ch); + } + }//while +}//transform + +int count(char *suffix) { + //typedef int SElemType; + SqStack(OPTR);//操作符 + COUNT(OPND);//操作数 + InitStack(&OPTR); + nInitStack(&OPND); + char *p; p = suffix; + char ch = *p; + char c; + while (ch != '#') { + if (!IN(ch)) { + nPush(&OPND, ch - '0'); + } + else { + Push(&OPTR, ch); + if ((OPND.top - 1) && (OPND.top - 2)) { + //如果栈顶和其下面的栈有数,则执行算数操作 + int t1, t2; + //GetTop(OPND, &t1); + nPop(&OPND, &t1); + nPop(&OPND, &t2); + int t3 = operate(ch, t1, t2); + nPush(&OPND, t3); + Pop(&OPTR, &c); + } + } + p++; + ch = *p; + }//while + int sum; + nPop(&OPND,&sum); + return sum; +} \ No newline at end of file diff --git "a/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/Queue.c" "b/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/Queue.c" new file mode 100644 index 00000000..61273128 --- /dev/null +++ "b/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/Queue.c" @@ -0,0 +1,82 @@ +#include"Queue.h" +Status InitQueue(LinkQueue *Q) { + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!Q->front)exit(OVERFLOW); + Q->front->Next = NULL; + return OK; +} +Status DestroyQueue(LinkQueue *Q) { + while (Q->front) { + Q->rear = Q->front->Next; + free(Q->front); + Q->front = Q->rear; + } + return OK; +} +Status ClearQueue(LinkQueue *Q) { + QNode *p, *next; + p = Q->front; + while (p!=Q->rear) { + next = p->Next; + free(p); + p = next; + } + + free(p); + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + //Q->front->Next = NULL; + return OK; +} +Status QueueEmpty(LinkQueue Q) { + if (Q.front == Q.rear)return TRUE; + else return FALSE; +} +int QueueLength(LinkQueue Q) { + QNode *p; + p = Q.front; + int k = 1; + while (p != Q.rear) { + p = p->Next; + k++; + } + return k-1; +} +Status GetHead(LinkQueue Q, QElemType *e) { + if (QueueEmpty(Q) == FALSE) { + *e = Q.front->Next->data; + return OK; + } + else { + return ERROR; + } +} +Status EnQueue(LinkQueue *Q, QElemType e) { + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p)exit(OVERFLOW); + p->data = e; + p->Next = NULL; + Q->rear->Next = p; + Q->rear = p; + return OK; +} +Status DeQueue(LinkQueue *Q, QElemType *e) { + if (Q->front == Q->rear)return ERROR; + QueuePtr p; + p = Q->front->Next; + *e = p->data; + Q->front->Next = p->Next; + if (Q->rear == p)Q->rear = Q->front; + free(p); + return OK; +} +Status QueueTraverse(LinkQueue Q) { + QNode *p; + p = Q.front->Next; + while (p != Q.rear) { + printf("%d ", p->data); + p = p->Next; + } + printf("%d ", p->data); + return OK; +} \ No newline at end of file diff --git "a/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/Queue.h" "b/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/Queue.h" new file mode 100644 index 00000000..b8f20822 --- /dev/null +++ "b/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/Queue.h" @@ -0,0 +1,29 @@ +#pragma once +#include +#include +#include +typedef int QElemType; +typedef enum { + FALSE, + OK, + OVERFLOW, + TRUE, + ERROR, +}Status; +typedef struct QNode { + QElemType data; + struct QNode *Next; +}QNode, *QueuePtr; +typedef struct { + QueuePtr front; + QueuePtr rear; +}LinkQueue; +Status InitQueue(LinkQueue *Q); +Status DestroyQueue(LinkQueue *Q); +Status ClearQueue(LinkQueue *Q); +Status QueueEmpty(LinkQueue Q); +int QueueLength(LinkQueue Q); +Status GetHead(LinkQueue Q, QElemType *e); +Status EnQueue(LinkQueue *Q, QElemType e); +Status DeQueue(LinkQueue *Q, QElemType *e); +Status QueueTraverse(LinkQueue Q); \ No newline at end of file diff --git "a/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/main.c" "b/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/main.c" new file mode 100644 index 00000000..baafcab2 --- /dev/null +++ "b/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/main.c" @@ -0,0 +1,38 @@ +#include"Queue.h" +int main() { + srand(time(0)); + int i,a,n,temp; + LinkQueue Q; + n = rand() % 10 + 5; + InitQueue(&Q); + printf("随机%d个数字进队列:\n",n); + for (i = 0; i < n; i++) { + a = rand() % 100 + 1; + printf("%d ", a); + EnQueue(&Q, a); + } + printf("\n遍历当前队列:\n"); + QueueTraverse(Q); + n = rand() % 4+1; + printf("\n删除前%d个队伍元素(出列):\n",n); + for (i = 0; i < n; i++) { + DeQueue(&Q, &temp); + printf("%d ", temp); + } + printf("\n遍历当前队列:\n"); + QueueTraverse(Q); + printf("\n当前队列长度:\n%d\n", QueueLength(Q)); + printf("清空队列\n"); + ClearQueue(&Q); + n = rand() % 10 + 1; + printf("重新随机%d个数字进队列:\n", n); + for (i = 0; i < n; i++) { + a = rand() % 100 + 1; + printf("%d ", a); + EnQueue(&Q, a); + } + printf("\n遍历当前队列:\n"); + QueueTraverse(Q); + printf("\n销毁队列\n"); + DestroyQueue(&Q); +} \ No newline at end of file diff --git "a/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/\346\265\213\350\257\2251.png" "b/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/\346\265\213\350\257\2251.png" new file mode 100644 index 00000000..e7394d37 Binary files /dev/null and "b/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/\346\265\213\350\257\2251.png" differ diff --git "a/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/\346\265\213\350\257\2252.png" "b/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/\346\265\213\350\257\2252.png" new file mode 100644 index 00000000..faeb70d9 Binary files /dev/null and "b/2017-1/Sv/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/\346\265\213\350\257\2252.png" differ diff --git a/2017-1/Wzy-CC/2-12/2-12.cpp b/2017-1/Wzy-CC/2-12/2-12.cpp new file mode 100644 index 00000000..3428d8df --- /dev/null +++ b/2017-1/Wzy-CC/2-12/2-12.cpp @@ -0,0 +1,153 @@ +//#include //头文件 +//typedef struct LNode //结构体LNode表示头节点 +//{ +// int data; +// struct LNode *next; +//}LNode, *ListList; //并且加了指针表示 +//void CreateList_L(LinkList&L, int n)//创建一个线性表(函数) +//{ +// L = (LinkList)malloc(sizeof(LNode)); +// L—— > next = NULL; +// for (i = n; i > 0; ——i) +// { +// p = (Listlist)malloc(sizeof(LNode)); +// scanf(&p—— > data); +// p->next = L->next; +// L->next = p; +// } +//} +//void MergeList_L(LinkList&La, LinkList&Lb, LinkList&Lc)//合并线性表函数 +//{ +// pa = La->next; +// pb = Lb->next; +// Lc = pc = La; +// while (pa&&pb) +// { +// if (pa->data <= pb->data) +// { +// pc->next = pa; +// pc = pa; +// pa = pa->next; +// } +// else +// { +// pc->next = pb; pc = pb; pb = pb->next; +// } +// } +// pc->next = pa ? pa : pb; +// free(Lb); +//} +//int main +#include +#include +#include +//#include "List.h" +typedef int ElemType; +//将ElemType设为int? +typedef struct LNode { + ElemType data; + struct LNode *next; +}LNode, *LinkList; +void creatList(LinkList &L, int n); +//创建函数中有两个参数:某线性表,长度 +void Traverse(LinkList &L); +void MergeList(LinkList &La, LinkList &Lb, LinkList &Lc); +void creatList(LinkList &L, int n) { + printf("\nCreatList:\n"); + printf("开始创建长度为%d的单链表\n", n); + int i; + int number = 20; + LinkList p = NULL; + L->next = NULL; + for (i = n; i>0; i--) { + p = (LinkList)malloc(sizeof(LNode)); + number = number - (rand() % n + 1); + p->data = number; + p->next = L->next; + L->next = p; + } + printf("链表创建完毕\n"); +} + +void Traverse(LinkList &L) { + LinkList a = L; + a = a->next; + while (a) { + printf("%d", a->data); + printf(" "); + a = a->next; + } + printf("\n"); + printf("遍历完成"); +} + +void MergeList(LinkList &La, LinkList &Lb, LinkList &Lc) { + printf("MergeList\n"); + printf("开始归并链表La和Lb\n"); + LinkList pa, pb, pc; + + pa = pb = pc = NULL; + pa = La->next; + pb = Lb->next; + Lc = pc = La; + + while (pa&&pb) { + printf("pa与pb都不为空链表,循环继续执行\n"); + printf("pa->data(%d)%spb->data(%d),所以\n", pa->data, pa->data <= pb->data ? "<=" : ">", pb->data); + if (pa->data <= pb->data) { + printf("pc->next=pa(%x),pc=pa(%x),pa=pa->next(%x)\n", pa, pa, pa->next); + pc->next = pa; + pc = pa; + pa = pa->next; + } + else { + printf("pc->next=pa(%x),pc=pa(%x),pb=pb->next(%x)\n", pb, pb, pa->next); + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + printf("pa与pb中至少有一个为空,循环终止\n"); + pc->next = pa ? pa : pb; + free(Lb); + printf("释放链表Lb\n"); + printf("归并链表结束\n"); +} +#include +#include +#include +#define length 5 + +int main() { + LinkList La; + LinkList Lb; + LinkList Lc; + //三个LinkList类型的线性表la,lb,lc + printf("函数开始执行\n\n"); + srand(time(0)); + //种子 + La = (LinkList)malloc(sizeof(LNode)); + printf("开始创建链表La\n"); + creatList(La, length + rand() % 5); + //创建la + printf("对链表La进行遍历"); + printf("\nLa:"); + Traverse(La); + //遍历La + printf("\n\n"); + + Lb = (LinkList)malloc(sizeof(LNode)); + printf("开始创建链表Lb\n"); + creatList(Lb, length + rand() % 5); + printf("对链表Lb进行遍历"); + printf("\nLb:"); + Traverse(Lb); + printf("\n\n"); + + printf("\n"); + MergeList(La, Lb, Lc); + printf("合并后的数组Lc为:"); + Traverse(Lc); + printf("\n"); + return 0; +}//主函数 \ No newline at end of file diff --git a/2017-1/Wzy-CC/3-1/3-1.cpp b/2017-1/Wzy-CC/3-1/3-1.cpp new file mode 100644 index 00000000..7eb360c9 --- /dev/null +++ b/2017-1/Wzy-CC/3-1/3-1.cpp @@ -0,0 +1,100 @@ +//算法3.1 +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef int SElemType; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef struct _SqStack +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack;//结构体:栈 + //typedef struct _SqStack *Sq; + +Status InitStack(SqStack* pS) +{ + (pS)->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!(pS)->base) + { + exit(OVERFLOW); + }//储存分配失败 + (pS)->top = (pS)->base; + (pS)->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status GetTop(SqStack S, SElemType *e) +{ + if (S.top == S.base) + return ERROR; + e = S.top - 1; + return OK; +} + +Status Push(SqStack* S, SElemType e) +{ + if (S->top - S->base >= S->stacksize)//栈满,追加储存空间 + { + S->base = (SElemType*)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if (!S->base)exit(OVERFLOW);//储存分配失败 + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +}//push + +Status Pop(SqStack *S, SElemType &e) +{ + //若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR + if (S->top == S->base) + { + return ERROR; + } + e = *(--S->top); + return OK; +} + +bool StackEmpty(SqStack S) +{ + if (S.top == S.base) + return true; + else + return false; +}//为空elsereturn false;//不为空} + +void conversion() +{ + SqStack S; + SElemType e; + //输入十进制数,输出八进制数 + InitStack(&S); //初始化 + int N; + scanf("%d", &N); + while (N) + { + Push(&S, N % 8); + N = N / 8; + } + while (!StackEmpty(S)) + { + Pop(&S, e); + printf("%d", e); + } +} // conversion + +int main() +{ + conversion(); + return OK; +} \ No newline at end of file diff --git a/2017-1/Wzy-CC/3-2-2/3.2.2.cpp b/2017-1/Wzy-CC/3-2-2/3.2.2.cpp new file mode 100644 index 00000000..1a5b7964 --- /dev/null +++ b/2017-1/Wzy-CC/3-2-2/3.2.2.cpp @@ -0,0 +1,162 @@ +//算法3.2.2 +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +typedef char SElemType; + +typedef struct { + SElemType *elem; + int length; + int listsize; +} SqList; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef struct +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack(SqStack *S) +{ + S->base = (SElemType*)malloc(100 * sizeof(SElemType)); + if (!S->base)exit(OVERFLOW);//储存分配失败 + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status GetTop(SqStack S, SElemType *e) +{ + if (S.top == S.base) + return ERROR; + e = S.top - 1; + return OK; +} + +Status Push(SqStack *S, SElemType e) +{ + if (S->top - S->base >= S->stacksize)//栈满,追加储存空间 + { + S->base = (SElemType*)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if (!S->base)exit(OVERFLOW);//储存分配失败 + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +}//push + +Status Pop(SqStack *S, SElemType *e) +{ + //若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK,否则返回ERROR + if (S->top == S->base) + return ERROR; + e = --S->top; + return OK; +} + +bool StackEmpty(SqStack S) +{ + if (S.top == S.base) + return true; + else + return false; +}//为空elsereturn false;//不为空} + +Status MatchCheck(SqList exp) +/* 顺序表exp表示表达式; */ +/* 若exp中的括号配对,则返回TRUE,否则返回FALSE */ +{ + SqStack s; + char e; + //逐个读入括号 + for (int i = 0; i +#include + +#include "3-4QUENE.h" + +Status InitQueue(LinkQueue q){ + q.front = q.rear = (QueuePtr)malloc(sizeof(QNode)); + if(!q.front){ + exit(OVERFLOW); + } + q.front->next = NULL; + printf("初始化完毕\n"); + return OK; +} +//初始化队列的函数实现 +Status DestroyQueue(LinkQueue q){ + while(q.front){ + q.rear = q.front->next; + free(q.front); + q.front = q.rear; + } + return OK; +} +//销毁队列的函数实现 +Status EnQueue(LinkQueue *q,QElemType e){ + QueuePtr p; + p=(QueuePtr)malloc(sizeof(QNode)); + if(!p){ + exit(OVERFLOW); + } + p->data = e; + p->next = NULL; + q->rear->next = p; + q->rear->data = p->data; + q->rear = p; + return OK; +} +//插入元素的函数实现 +bool QueueEmpty(LinkQueue q){ + if(q.front == q.rear){ + return true; + } + else{ + return false; + } +} +//判断的函数实现 +Status DeQueue(LinkQueue *q,QElemType *e){ + QueuePtr p; + p=(QueuePtr)malloc(sizeof(QNode)); + if(q->front == q->rear){ + return ERROR; + } + p = q->front; + *e = p->data; + q->front = q->front->next; + if(q->rear == p){ + q->rear = q->front; + } + free(p); + return OK; +} +//删除队列头元素的函数实现 +Status CleanQueue(LinkQueue q){ + QueuePtr p; + QueuePtr next; + if(QueueEmpty(q)){ + printf("将队列清空为空对列\n"); + return OK; + } + else{ + for(p = q.front; p; p = next){ + next = p->next; + free(p); + p = next; + } + } + printf("将队列清空为空对列\n"); + return OK; +} +//清空队列的函数实现 +int QueueLength(LinkQueue q){ + QueuePtr p; + QueuePtr next; + int length = 0; + p = q.front; + while(p != q.rear){ + p = p->next; + length++; + } + return length; +} +//计算队列长度的函数实现 +Status GetHead(LinkQueue q,QElemType *e){ + *e = q.front->data; + printf("此时队头元素为%d\n",*e); + return OK; +} + +Status TraverseQueue(LinkQueue q){ + QueuePtr p; + printf("开始遍历:\n"); + if(QueueEmpty(q)){ + printf("此时队列里不存在元素\n"); + return OK; + } + p = q.front; + while(p != q.rear){ + printf("%d ",p->data); + p = p->next; + } + printf("\n"); + return OK; +} +//遍历队列的函数实现 \ No newline at end of file diff --git a/2017-1/Wzy-CC/3-4/3-4QUENE.h b/2017-1/Wzy-CC/3-4/3-4QUENE.h new file mode 100644 index 00000000..7b765203 --- /dev/null +++ b/2017-1/Wzy-CC/3-4/3-4QUENE.h @@ -0,0 +1,36 @@ +#include +#include +//头文件 +typedef int QElemType; +//定义一个新类型 +typedef enum{ + false, + true, +}bool; + +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; + +typedef struct QNode{ + QElemType data; + struct QNode *next; +}QNode,*QueuePtr; +//结构体:队列 +typedef struct LinkQueue{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; +//链队列 +Status InitQueue(LinkQueue q); +Status DestroyQueue(LinkQueue q); +Status EnQueue(LinkQueue *q,QElemType e); +bool QueueEmpty(LinkQueue q); +Status DeQueue(LinkQueue *q,QElemType *e); +Status CleanQueue(LinkQueue q); +int QueueLength(LinkQueue q); +Status GetHead(LinkQueue q,QElemType *e); +Status TraverseQueue(LinkQueue q); +//所有的函数声明 \ No newline at end of file diff --git a/2017-1/Wzy-CC/3-4/main.c b/2017-1/Wzy-CC/3-4/main.c new file mode 100644 index 00000000..2b16d625 --- /dev/null +++ b/2017-1/Wzy-CC/3-4/main.c @@ -0,0 +1,46 @@ +#include +#include + +#include "3-4QUENE.h" + +int main(){ + LinkQueue a; + QElemType e; + int len; + + a.front = a.rear = (QueuePtr)malloc(sizeof(QNode)); + InitQueue(a); + TraverseQueue(a); + len=QueueLength(a); + printf("此时队列长度为 %d\n\n", len); + + printf("将数字3加入队列中\n"); + EnQueue(&a, 3); + TraverseQueue(a); + len=QueueLength(a); + printf("此时队列长度为 %d\n\n", len); + + printf("将数字18加入队列中\n"); + EnQueue(&a, 18); + TraverseQueue(a); + len=QueueLength(a); + printf("此时队列长度为 %d\n\n", len); + + GetHead(a, &e); + + DeQueue(&a, &e); + printf("此时队列不为空,删除队头元素%d\n",e); + TraverseQueue(a); + printf("\n"); + + GetHead(a, &e); + + DeQueue(&a, &e); + printf("此时队列不为空,删除队头元素%d\n",e); + TraverseQueue(a); + printf("\n"); + + CleanQueue(a); + return 0; +} +//主函数 \ No newline at end of file diff --git a/2017-1/Wzy-CC/Btree-Traver/Source.cpp b/2017-1/Wzy-CC/Btree-Traver/Source.cpp new file mode 100644 index 00000000..dbf6023b --- /dev/null +++ b/2017-1/Wzy-CC/Btree-Traver/Source.cpp @@ -0,0 +1,286 @@ +锘#include +#include +//娉ㄩ噴 +#define MAXSIZE 20 +//浜屽弶鏍戠粨鐐圭殑缁撴瀯浣撹〃绀哄舰寮 +typedef struct BitNode +{ + char data; + struct BitNode* left, *right; +}BitTree; + +//鏍堢殑缁撴瀯浣撹〃绀哄舰寮 +typedef struct stackelem +{ + BitTree* a[MAXSIZE]; + int top; +}Stack; + +//闃熷垪鐨勭粨鏋勪綋鐨勮〃绀哄舰寮 +typedef struct queueelem +{ + BitTree* b[MAXSIZE]; + int front, rear; +}Queue; + +//鍒涘缓浜屽弶鏍戯紝鍒╃敤閫掑綊鐨勬柟娉 +//鎸夊墠搴忔搴忚緭鍏ャ 濡 A # #(#琛ㄧず绌烘爲) +BitTree* Create() +{ + char ch; + scanf("%c", &ch); + getchar(); //鍚冩帀绌烘牸绗 + if (ch == '#') + { + return NULL; + } + else + { + BitTree* btree = (BitTree*)malloc(sizeof(BitTree)); + if (NULL == btree) + { + return NULL; + } + btree->data = ch; + btree->left = Create(); + btree->right = Create(); + return btree; + } +} + +//鍓嶅簭閬嶅巻锛岄掑綊鐨勬柟娉 +void Preorder(BitTree* bt) +{ + if (NULL != bt) + { + printf("%c ", bt->data); + Preorder(bt->left); + Preorder(bt->right); + } +} + +//鍓嶅簭閬嶅巻鐨勯潪閫掑綊瀹炵幇 +/* +鎬濇兂锛氬埄鐢ㄦ爤鏉ュ疄鐜帮紱鏍圭粨鐐硅繘鏍堬紝涔嬪悗鏍堥潪绌猴紝寮瑰嚭锛屾帴鐫鏍硅妭鐐圭殑鍙崇粨鐐硅繘鏍堬紝涔嬪悗锛屽乏鑺傜偣杩涙爤锛涙帴鐫锛屽脊鍑烘爤椤跺厓绱狅紙杈撳嚭锛, +姝ょ粨鐐圭殑鍙崇粨鐐硅繘鏍堬紝涔嬪悗宸﹁妭鐐硅繘鏍堬紝寮瑰嚭鏍堥《鍏冪礌锛堣緭鍑猴級...涓鐩磋繖鏍蜂笅鍘伙紝鐩村埌鏍堜负绌恒 +*/ +void Preorder2(BitTree* bt) +{ + BitTree* p; + Stack st; + st.top = -1; + if (NULL == bt) + { + return; + } + else + { + st.top++; + st.a[st.top] = bt; + while (st.top != -1) + { + p = st.a[st.top]; + st.top--; + printf("%c ", p->data); + if (p->right != NULL) + { + st.top++; + st.a[st.top] = p->right; + } + if (p->left != NULL) + { + st.top++; + st.a[st.top] = p->left; + } + } + } +} + +//涓簭閬嶅巻锛岄掑綊瀹炵幇 +void Inorder(BitTree* bt) +{ + if (NULL != bt) + { + Inorder(bt->left); + printf("%c ", bt->data); + Inorder(bt->right); + } +} + +//涓簭閬嶅巻锛岄潪閫掑綊瀹炵幇 +/* +鎬濇兂锛氬埄鐢ㄦ爤銆備粠鏍硅妭鐐瑰紑濮嬶紝寰幆锛屽彧瑕佹湁宸﹀瓙鑺傜偣鍒欒繘鏍堬紝鐩村埌宸﹀瓙鑺傜偣涓虹┖銆傛帴鐫寮瑰嚭鏍堥《杈撳嚭锛屽垽鏂缁撶偣鏄惁鏈夊彸瀛愯妭鐐癸紝 +鑻ユ湁鍒欒繘鏍堬紝鑻ユ病鏈夌户缁脊鏍堛傛湁鍙冲瓙鑺傜偣鐨勬儏鍐碉紝鍒ゆ柇璇ヨ妭鐐规槸鍚︽湁宸﹀瓙鑺傜偣锛屾湁鍒欒繘鏍堬紝鐩村埌宸﹀瓙鑺傜偣涓虹┖锛涜嫢璇ュ彸瀛愯妭鐐规病鏈 +宸﹀瓙鑺傜偣锛屽垯寮规爤锛涘垽鏂脊鍑虹殑鑺傜偣锛屾槸鍚︽湁鍙冲瓙鑺傜偣锛岃嫢鏈夊垯杩涙爤锛屾病鏈夌户缁脊鏍堬紱鎺ョ潃鍙堣鍒ゆ柇鍒氳繘鏍堢殑杩欎釜鑺傜偣锛屾槸鍚︽湁宸﹀瓙鑺傜偣锛 +鏈夊垯杩涙爤锛屾病鏈夊垯缁х画寮规爤銆傞噸澶嶄笅鍘.... +鏍堢┖锛屾槸鍒ゅ畾鏉′欢銆 +*/ +void Inorder2(BitTree* bt) +{ + BitTree* p, *q; + Stack st; + st.top = -1; + if (NULL == bt) + { + return; + } + else + { + while (bt != NULL) + { + st.top++; + st.a[st.top] = bt; + bt = bt->left; + } + while (st.top != -1) + { + p = st.a[st.top]; + st.top--; + printf("%c ", p->data); + while (p->right != NULL) + { + st.top++; + st.a[st.top] = p->right; + q = p->right; + while (q->left != NULL) + { + st.top++; + st.a[st.top] = q->left; + q = q->left; + } + break; + } + } + } +} + +//鍚庡簭閬嶅巻锛岄掑綊瀹炵幇 +void Postorder(BitTree* bt) +{ + if (bt != NULL) + { + Postorder(bt->left); + Postorder(bt->right); + printf("%c ", bt->data); + } +} + +//鍚庡簭閬嶅巻锛岄潪閫掑綊瀹炵幇 +/* +绠楁硶鎬濇兂锛氬埄鐢ㄦ爤鏉ュ疄鐜般備粠鏍圭粨鐐瑰紑濮嬶紝鍙宸﹀瓙鑺傜偣闈炵┖锛屽垯杩涙爤锛岀洿鍒板乏瀛愯妭鐐逛负绌轰负姝€傚彇鍑烘爤椤跺厓绱狅紙鍙槸鍙栵紝骞堕潪寮规爤锛夛紝鍒ゆ柇 +1:鍙栧嚭鐨勬爤椤跺厓绱犳槸鍚︽湁鍙冲瓙鑺傜偣锛屾垨鑰呭彸瀛愯妭鐐规槸鍚﹁璁块棶杩囷紝鑻ユ弧瓒虫潯浠讹紙鏃犲彸瀛愯妭鐐癸紝鎴栬呭彸瀛愯妭鐐硅璁块棶杩囷級锛屽垯杈撳嚭璇ョ粨鐐癸紝 +鍚屾椂寮规爤锛屽苟涓旇褰曚笅璇ヨ闂殑鑺傜偣銆 +2:鍙栧嚭鐨勬爤椤跺厓绱狅紝鑻ユ湁鍙冲瓙鑺傜偣锛屼笖鏈璁块棶杩囷紝鍒欐寚閽堢户缁Щ鍔ㄥ埌鍙冲瓙鑺傜偣锛岄噸澶嶄竴寮濮嬫槸鍚﹀張宸﹀瓙鑺傜偣鐨勫垽鏂 +*/ +void Postorder2(BitTree* bt) +{ + Stack st; + st.top = -1; + BitTree* t; + do + { + while (bt != NULL) + { + st.top++; + st.a[st.top] = bt; + bt = bt->left; + } + t = NULL; + while (st.top != -1) + { + bt = st.a[st.top]; + if (bt->right == t) //t:琛ㄧず涓簄ull锛屾垨鑰呭彸瀛愯妭鐐硅璁块棶杩囦簡銆 + { + printf("%c ", bt->data); + st.top--; + t = bt; //t璁板綍涓嬪垰鍒氳闂殑鑺傜偣 + } + else + { + bt = bt->right; + break; + } + } + } while (st.top != -1); +} + +//姹備簩鍙夋爲鐨勯珮搴︼紝閫掑綊瀹炵幇 +int Height(BitTree* bt) +{ + int depth1, depth2; + if (NULL == bt) + { + return 0; + } + else + { + depth1 = Height(bt->left); + depth2 = Height(bt->right); + if (depth1>depth2) + { + return (depth1 + 1); + } + else + { + return (depth2 + 1); + } + } +} + +//灞傛閬嶅巻浜屽弶鏍,鐢ㄩ槦鍒楁潵瀹炵幇 +void TraversalOfLevel(BitTree* bt) +{ + Queue q; + q.front = q.rear = 0; + if (bt != NULL) + { + printf("%c ", bt->data); + } + q.b[q.front] = bt; + q.rear = q.rear + 1; + while (q.frontleft != NULL) + { + printf("%c ", bt->left->data); + q.b[q.rear] = bt->left; + q.rear = q.rear + 1; + } + if (bt->right != NULL) + { + printf("%c ", bt->right->data); + q.b[q.rear] = bt->right; + q.rear = q.rear + 1; + } + } +} + +int main() +{ + BitTree* btr = Create(); + printf("鍓嶅簭閬嶅巻锛氶掑綊鍜岄潪閫掑綊瀹炵幇锛歕n"); + Preorder(btr); + printf("\n"); + Preorder2(btr); + printf("\n"); + printf("涓簭閬嶅巻锛氶掑綊鍜岄潪閫掑綊瀹炵幇锛歕n"); + Inorder(btr); + printf("\n"); + Inorder2(btr); + printf("\n"); + exit(1);// + printf("鍚庡簭閬嶅巻锛氶掑綊鍜岄潪閫掑綊瀹炵幇锛歕n"); + Postorder(btr); + printf("\n"); + Postorder2(btr); + printf("\n"); + printf("浜屽弶鏍戠殑楂樺害锛歕n"); + int Hgt = Height(btr); + printf("%d \n", Hgt); + printf("灞傛閬嶅巻浜屽弶鏍:\n"); + TraversalOfLevel(btr); + printf("\n"); + return 0; +} diff --git a/2017-1/Wzy-CC/queue/queue.c b/2017-1/Wzy-CC/queue/queue.c new file mode 100644 index 00000000..f0d88151 --- /dev/null +++ b/2017-1/Wzy-CC/queue/queue.c @@ -0,0 +1,209 @@ +#include "queue.h" + + + +/*初始化队列*/ + +Status InitQueue(LinkQueue *q) { + + q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); + + if (!q->front) { + + exit(OVERFLOW); + + } + + q->front->next = NULL; + + q->rear->next = NULL; + + return OK; + +} + + + +/*添加一个元素,用e传递其值*/ + +Status EnQueue(LinkQueue *q, QElemType e) { + + QueuePtr p; + + p = (QueuePtr)malloc(sizeof(QNode)); + + if (!p) { + + return OVERFLOW; + + } + + p->data = e; + + p->next = NULL; + + q->rear->next = p; + + q->rear = p; + + return OK; + +} + + + +/*检查队列是否为空*/ + +bool QueueEmpty(LinkQueue q) { + + if (q.front == q.rear) { + + return true; + + } + + else { + + return false; + + } + +} + + + +/*删除队头元素, 用e返回其值*/ + +Status DeQueue(LinkQueue *q, QElemType *e) { + + if (QueueEmpty(*q)) { + + return ERROR; + + } + + QueuePtr p; + + p = q->front->next;//q->front为头节点,q->front->next为队头元素 + + e = p->data; + + q->front->next = p->next; + + if (q->rear == p)//如果删除的是队列的最后一个元素,需要修改尾指针 + + q->rear = q->front; + + return OK; + +} + + + +/*遍历队列*/ + +Status TraverseQueue(LinkQueue q) { + + QueuePtr p; + + if (QueueEmpty(q)) { + + printf("队列为空\n"); + + return OK; + + } + + p = q.front->next; + + while (p != q.rear) { + + printf("%d ", p->data); + + p = p->next; + + } + + printf("%d", p->data); + + printf("\n"); + + return OK; + +} + + + +/*销毁队列*/ + +Status DestroyQueue(LinkQueue *q) { + + while (q->front) { + + q->rear = q->front->next; + + free(q->front); + + q->front = q->rear; + + } + + return OK; + +} + + + +/*将队列q清空为空队列*/ + +Status ClearQueue(LinkQueue *q) { + + if (!QueueEmpty(*q)) { + + q->rear = q->front; + + return OK; + + } + + return false; + +} + + + +/*返回队列的长度*/ + +int QueueLength(LinkQueue *q) { + + int l; + + QueuePtr lq; + + for (l = 0, lq = q->front->next; lq != q->rear; l++) { + + lq = lq->next; + + } + + return l + 1; + +} + + + +/*返回队列的队头元素*/ + +Status GetHead(LinkQueue *q, QElemType *e) { + + if (!QueueEmpty(*q)) { + + *e = q->front->next->data; + + return OK; + + } + + return false; + +} \ No newline at end of file diff --git a/2017-1/Wzy-CC/queue/queue.h b/2017-1/Wzy-CC/queue/queue.h new file mode 100644 index 00000000..fa226081 --- /dev/null +++ b/2017-1/Wzy-CC/queue/queue.h @@ -0,0 +1,83 @@ +#define QUEUE_H + +#include + +#include + +#include + +typedef int QElemType; + +typedef enum { + + false, + + true, + +}bool; + +typedef enum { + + OK, + + ERROR, + + OVERFLOW + +} Status; + +typedef struct QNode { + + QElemType data; + + struct QNode *next; + +}QNode, *QueuePtr; + +typedef struct _LinkQueue { + + QueuePtr front; + + QueuePtr rear; + +}LinkQueue; + + + +/*初始化队列*/ + +Status InitQueue(LinkQueue *q); + +/*添加一个结点,用e传递其值*/ + +Status EnQueue(LinkQueue *q, QElemType e); + +/*检查队列是否为空*/ + +bool QueueEmpty(LinkQueue q); + +/*删除一个结点, 用e返回其值*/ + +Status DeQueue(LinkQueue *q, QElemType *e); + +/*遍历队列*/ + +Status TraverseQueue(LinkQueue q); + +/*销毁队列*/ + +Status DestroyQueue(LinkQueue *q); + +/*将队列q清空为空队列*/ + +Status ClearQueue(LinkQueue *q); + +/*返回队列的长度*/ + +int QueueLength(LinkQueue *q); + +/*返回队列的队头元素*/ + +Status GetHead(LinkQueue *q, QElemType *e); + +#endif \ No newline at end of file diff --git a/2017-1/Xhl_blabla/13/13.c b/2017-1/Xhl_blabla/13/13.c new file mode 100644 index 00000000..246e2373 --- /dev/null +++ b/2017-1/Xhl_blabla/13/13.c @@ -0,0 +1,123 @@ +#include "13.h" +int main() +{ + Lnode *p = ListCreat();//制造要初始化的线性链表 使用了尾插法进行插入 + print_list(p); + Lnode *p1; + Lnode *p2; + ApartList(p, &p1, &p2);//将链表进行拆分 只需要进行一次拆分 节省了循环的时间 + printf("拆分后的第一个链表\n"); + print_circle_list(p1);//输出第一个循环的数组 + printf("拆分后的第二个链表\n"); + print_circle_list(p2);//输出第二个循环的数组 + destory(&p1);//销毁第一个循环数组 + destory(&p2);//销毁第二个循环的数组 + return 0; +} +//初始化一个链表 尾插法 +Lnode * ListCreat() +{ + int i; + int num; + printf("请输入初始的线性链表的长度\n"); + scanf_s("%d", &num); + Lnode *L; + L = (Lnode *)malloc(sizeof(Lnode)); //申请头结点空间 + L->data = 1; + L->next = NULL; //初始化一个空链表 + Lnode *r; + r = L; //r始终指向终端结点,开始时指向头结点 + for (i = 2; i <= num; i++) + { + Lnode *p; + p = (Lnode *)malloc(sizeof(Lnode)); + p->data = i; //结点数据域赋值 + r->next = p; + r = p; + } + r->next = NULL; + return L; +} +//将线性链表整个拆开 拆成两个循环链表 +int ApartList(Lnode *t,Lnode **px, Lnode **py) +{//尾插法 + Lnode *p1; + Lnode *p2; + Lnode *temp1; + Lnode *temp2; + Lnode *temp; + Lnode *p; + p = t; + p1 = (Lnode *)malloc(sizeof(Lnode)); //申请头结点空间 + p1->next = NULL; + p2 = (Lnode *)malloc(sizeof(Lnode)); //申请头结点空间 + temp1 = p1; + temp2 = p2; + p2->next = NULL; + while (NULL != p) + { + temp = p; + p = p->next; + if (temp->data % 2 != 0)//如果是奇数 + { + temp->next= temp1->next;//temp1移动用于最后首尾相连 + temp1->next = temp; + temp1 = temp; + } + else + { + temp->next = temp2->next;//temp2移动 + temp2->next = temp; + temp2 = temp; + } + }//当拆分完成的时候 + p1->data = (temp1->data + 1) / 2; + p2->data = temp2->data / 2; + temp1->next = p1; + temp2->next = p2; + *px = p1; + *py = p2; + return 0; +} +//打印初始单链表 +int print_list(Lnode *p) +{ + printf("初始的链表为:\n"); + do + { + printf("%d ", p->data); + p = p->next; + }while (p!= NULL); + printf("\n"); + return 1; +} +//打印循环单链表 +int print_circle_list(Lnode *p) +{ + Lnode *t; + printf("链表长度为 %d \n", p->data); + t = p; + p = p->next; + printf("元素为: "); + do + { + printf("%d ", p->data); + p = p->next; + } while (p != t); + printf("\n\n"); + return 1; +} +//销毁链表 +int destory(Lnode **p) +{ + Lnode *t; + int i; + int k = (*p)->data+1; + for (i = 0; i < k; i++) + { + t = *p; + (*p) = (*p)->next; + free(t); + } + return 1; +} \ No newline at end of file diff --git a/2017-1/Xhl_blabla/13/13.h b/2017-1/Xhl_blabla/13/13.h new file mode 100644 index 00000000..bae9acd6 --- /dev/null +++ b/2017-1/Xhl_blabla/13/13.h @@ -0,0 +1,12 @@ +#include //头插法 没有指针的移动 +typedef struct Lnode +{ + int data; + struct Lnode* next; +}Lnode; +//初始化一个链表 尾插法 +Lnode * ListCreat(); +int ApartList(Lnode *p, Lnode **px, Lnode **py); +int print_list(Lnode *p); +int print_circle_list(p1); +int destory(Lnode **p); \ No newline at end of file diff --git a/2017-1/Xhl_blabla/13/13.png b/2017-1/Xhl_blabla/13/13.png new file mode 100644 index 00000000..883f94ad Binary files /dev/null and b/2017-1/Xhl_blabla/13/13.png differ diff --git "a/2017-1/Xhl_blabla/QQ\345\233\276\347\211\20720170322095438.png" "b/2017-1/Xhl_blabla/QQ\345\233\276\347\211\20720170322095438.png" new file mode 100755 index 00000000..31e073f0 Binary files /dev/null and "b/2017-1/Xhl_blabla/QQ\345\233\276\347\211\20720170322095438.png" differ diff --git "a/2017-1/Xhl_blabla/avl \346\240\221\346\223\215\344\275\234/AVLOutput.txt" "b/2017-1/Xhl_blabla/avl \346\240\221\346\223\215\344\275\234/AVLOutput.txt" new file mode 100644 index 00000000..52978783 --- /dev/null +++ "b/2017-1/Xhl_blabla/avl \346\240\221\346\223\215\344\275\234/AVLOutput.txt" @@ -0,0 +1,6 @@ +8, 4, 3, 1, 6, 5, 7, 14, 10, 22, 19, 30 +8, 4, 3, 1, 6, 5, 7, 14, 10, 13, 22, 19, 30 +10, 4, 3, 1, 6, 5, 7, 14, 13, 22, 19, 30 +10, 4, 3, 1, 6, 7, 14, 13, 22, 19, 30 +10, 4, 3, 1, 6, 7, 19, 14, 13, 22, 20, 30 +10, 4, 3, 1, 7, 19, 14, 13, 22, 20, 30 diff --git "a/2017-1/Xhl_blabla/avl \346\240\221\346\223\215\344\275\234/c++.c" "b/2017-1/Xhl_blabla/avl \346\240\221\346\223\215\344\275\234/c++.c" new file mode 100644 index 00000000..06359349 --- /dev/null +++ "b/2017-1/Xhl_blabla/avl \346\240\221\346\223\215\344\275\234/c++.c" @@ -0,0 +1,361 @@ +#include "c.h" +//对节点p进行右旋处理 +void R_Rotate(BSTree *p) +{ + BSTree lc; + lc = (*p)->lchild; + (*p)->lchild = lc->rchild; + lc->rchild = (*p); + (*p) = lc; +} +//左旋处理 传入的p可以改变上一个节点的指向 +void L_Rotate(BSTree *p) +{ + BSTree rc; + rc = (*p)->rchild; + (*p)->rchild = rc->lchild; + rc->lchild = (*p); + (*p) = rc; +} +void RightBalance(BSTree *T) +{ + BSTree lc; + BSTree rd; + lc = (*T)->rchild; + switch (lc->bf) + { + case RH: + (*T)->bf = lc->bf = EH; + L_Rotate(T); + break; + case LH: + rd = lc->lchild; + switch (rd->bf) + { + case RH: + (*T)->bf = LH;//rh + lc->bf = EH; + break; + case EH: + (*T)->bf = lc->bf = EH; + break; + case LH: + (*T)->bf = EH; + lc->bf = RH;//LH + break; + } + rd->bf = EH; + R_Rotate(&((*T)->rchild)); + L_Rotate(T); + break;// + } +} +void LeftBalance(BSTree *T) +{ + BSTree lc; + BSTree rd; + lc = (*T)->lchild; + switch (lc->bf) + {//因为传入的情况是只有左子树高的情况 所以子节点 左边高 要么右边高 + case LH: + (*T)->bf = lc->bf = EH; + R_Rotate(T); + break; + case RH: + rd = lc->rchild; + switch (rd->bf) + { + case LH: + (*T)->bf = RH; + lc->bf = EH; + break; + case EH: + (*T)->bf = lc->bf = EH; + break; + case RH: + (*T)->bf = EH; + lc->bf = LH; + break; + } + rd->bf = EH; + L_Rotate(&((*T)->lchild)); + R_Rotate(T); + break; + } +} +status InsertAVL(BSTree *T, ElemType e, int *taller) +{//进行插入并检查是否失去平衡 TALLER 反应 t 是否长高 + if (!(*T))//如果没有节点 直接插入根节点 + { + (*T) = (BSTree)malloc(sizeof(BSTNode));//插入根节点 + (*T)->data = e; + (*T)->lchild = (*T)->rchild = NULL; + (*T)->bf = EH;//平衡因子 初始值 都是0 那么后面怎么进行改变 + (*taller) = 1; + } + else//如果有已有根节点 + { + if (e == (*T)->data)//如果已有该节点 进行返回 + { + (*taller) = 0; + return 0; + } + if (e < (*T)->data)//如果小于根节点 继续在左子树进行查找 + { + if (!InsertAVL(&((*T)->lchild), e, taller))//未插入返回0 + { + return 0; + } + if ((*taller)!=0)//如果插入成功则检查平衡因子 + { + switch ((*T)->bf) + { + case LH: + LeftBalance(T);//左平衡旋转处理 + (*taller) = 0; + break; + case EH: + (*T)->bf = LH; + (*taller) = 1; + break; + case RH: + (*T)->bf = EH; + (*taller) = 0; + break; + } + } + } + else + { + if (!InsertAVL(&((*T)->rchild), e, taller))//未插入返回0 + { + return 0; + } + if ((*taller)!=0) + { + switch ((*T)->bf) + { + case LH: + (*T)->bf = EH; + (*taller) = 0; + break; + case EH: + (*T)->bf = RH; + (*taller) = 1; + break; + case RH: + RightBalance(T); + (*taller) = 0; + break; + } + } + } + + } + return 1; +} +//判断是不是平衡树 +int IsBalanced(BSTree pRoot, int* pDepth) +{ + if (pRoot == NULL) + { + *pDepth = 0; + return 1; + } + int left, right; + if (IsBalanced(pRoot->lchild, &left) && IsBalanced(pRoot->rchild, &right)) + { + int diff = left - right; + if (diff >= -1 && diff <= 1) { + *pDepth = 1 + (left>right ? left : right); + return 1; + } + } + return 0; +} +int Balanced(BSTree pRoot) +{ + int depth = 0; + return IsBalanced(pRoot, &depth); +} +//end 判断是不是平衡树 +//删除 +int DeleteAVL(BSTree *T, int key, int *shorter) +{ + if (!(*T)) + {//No such key + (*shorter) = 0; + return 0; + } + else//如果树不空 + { + if (key == (*T)->data) //找到了需要删除的结点 + { + //如果该结点的lchild 和 + //rchild 至少有一个为NULL + //则删除 + BSTree q = (*T); + if (!((*T)->lchild)) + {//如果该结点的lchild 为NULL + q = (*T); + (*T) = (*T)->rchild; + free(q); + (*shorter) = 1; + return 1; + } + else if (!((*T)->rchild)) + {//如果该结点的rchild 为 NULL + q = (*T); + (*T) = (*T)->lchild; + free(q); + (*shorter) = 1; + return 1; + } + else + { + //删除一个左右孩子都不为空的结点 + //使该结点的右子树的最左孩子代替 + BSTree s = (*T)->rchild; + while (s->lchild) + { + s = s->lchild;//查找t的最左结点 ,用它的数值来代替根节点 并删除最右子树 + } + (*T)->data = s->data; + key = s->data; + } + } + if (key < (*T)->data) //删除了等号 + {//这里与InsertAVL不同 + if (!DeleteAVL(&((*T)->lchild), key, shorter)) + { + return 0; + } + if ((*shorter)) + { + switch ((*T)->bf) + { + case LH: + (*T)->bf = EH; + (*shorter) = 1; + break; + case EH: + (*T)->bf = RH; + (*shorter) = 0; + break; + case RH: + RightBalance(T); + if ((*T)->rchild->bf == EH) + { + (*shorter) = 0; + } + else + (*shorter) = 1; + break; + } + } + } + else + { + if (!DeleteAVL(&((*T)->rchild), key, shorter)) + { + return 0; + } + if ((*shorter)) + { + switch ((*T)->bf) + { + case LH: + LeftBalance(T); + if ((*T)->lchild->bf == EH) + { + (*shorter) = 0; + } + else + (*shorter) = 1; + break; + case EH: + (*T)->bf = LH; + (*shorter) = 0; + break; + case RH: + (*T)->bf = EH; + (*shorter) = 1; + break; + } + } + } + + } + return 1; +}//Delete + //先序遍历 +status InOrder(BSTree T) +{ + if (T) + { + out[k] = T->data;//每次输出结点的值 + k++; + InOrder(T->lchild); + InOrder(T->rchild); + } + else + return 1; +} +//用于打印特定形式的输出 +status print(BSTree T, FILE *fp) +{ + char x[4] = ", "; + int i; + for (i = 0; i < num; i++) + { + out[i] = -1; + } + InOrder(T); + for (i = 0;; i++) + { + if (out[i] == -1) + { + break; + } + if (i != 0) + { + //printf(", "); + fputs(x, fp); + } + //printf("%d", out[i]); + fprintf(fp, "%d", out[i]); + } + return 0; +} + int main() + { + FILE *fp; + char z[4] = "\n"; + fp = fopen("AVLOutput.txt", "a"); + int number[num_length] = { 8, 10,14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + int search[s_length] = { 13, 8, 5, 20, 6 }; + int i; + BSTree T = NULL; + int taller;//反映了树是否长高 + for (i = 0; i < 12; i++) + { + InsertAVL(&T, number[i], &taller); + } + //InOrder(T,out,k); + print(T, fp); + fputs(z, fp); + for (i = 0; i < s_length; i++) + {//如果插入失败 就进行删除操作 + k = 0;//初始化计数数字 + if (!InsertAVL(&T, search[i], &taller)) + { + DeleteAVL(&T, search[i], &taller); + } + print(T, fp); + fputs(z, fp); + //用深度判断是否是平衡树 + /* int result =Balanced(T); + printf("result = %s\n", result == 0 ? "false" : "true");*/ + } + return 0; + } diff --git "a/2017-1/Xhl_blabla/avl \346\240\221\346\223\215\344\275\234/c.h" "b/2017-1/Xhl_blabla/avl \346\240\221\346\223\215\344\275\234/c.h" new file mode 100644 index 00000000..4ec15e8a --- /dev/null +++ "b/2017-1/Xhl_blabla/avl \346\240\221\346\223\215\344\275\234/c.h" @@ -0,0 +1,29 @@ +#include +#include +#include +#define num_length 20 +# define LH 1//左高 +#define EH 0 +#define RH -1 +#define s_length 5 +#define num 25 +int out[num];//用于存储结点数据域的数组 +int k = 0;//用于给数组计数 +typedef int ElemType; +typedef int status; +typedef struct BSTNode +{ + ElemType data; + int bf;//节点平衡因子 + struct BSTNode *lchild; + struct BSTNode *rchild; +}BSTNode, *BSTree; +void R_Rotate(BSTree *p); +void LeftBalance(BSTree *T); +status InsertAVL(BSTree *T, ElemType e, int * taller); +void RightBalance(BSTree *T); +int IsBalanced(BSTree pRoot, int* pDepth); +int Balanced(BSTree pRoot); +int DeleteAVL(BSTree *T, int key, int *shorter); +status InOrder(BSTree T); +status print(BSTree T, FILE *fp); \ No newline at end of file diff --git a/2017-1/Xhl_blabla/erchashu-2/1.png b/2017-1/Xhl_blabla/erchashu-2/1.png new file mode 100644 index 00000000..692e3b73 Binary files /dev/null and b/2017-1/Xhl_blabla/erchashu-2/1.png differ diff --git a/2017-1/Xhl_blabla/erchashu-2/bitree.h b/2017-1/Xhl_blabla/erchashu-2/bitree.h new file mode 100644 index 00000000..0a511adb --- /dev/null +++ b/2017-1/Xhl_blabla/erchashu-2/bitree.h @@ -0,0 +1,15 @@ +#include +#include +typedef char ElemType; +typedef int status; +#define num 30 +int number = 0;//定义一个全局变量 用于赋值时的计数 + //定义二叉树 +typedef struct BiTree +{ + ElemType date; + struct BiTree *lChild; + struct BiTree * rChild; +} BitNode, *BiTree; +status CreatBiTree(BiTree *t, char *x);//先序创立二叉树 +void VisitHouxu(BiTree t);//后续遍历 \ No newline at end of file diff --git a/2017-1/Xhl_blabla/erchashu-2/psh.c b/2017-1/Xhl_blabla/erchashu-2/psh.c new file mode 100644 index 00000000..4a31d86b --- /dev/null +++ b/2017-1/Xhl_blabla/erchashu-2/psh.c @@ -0,0 +1,56 @@ +#include"bitree.h" +status CreatBiTree(BiTree *t, char *x) +{ + ElemType ch, temp; + ch = x[number]; + number++; + if (ch == '*') + { + *t = NULL; + } + else + { + *t = (BiTree)malloc(sizeof(BitNode));//分配空间进行存储 + if (!(*t))//如果分配失败 + { + exit(-1); + } + (*t)->date = ch;//生成根节点 + CreatBiTree(&(*t)->lChild, x);//构造左子树 + CreatBiTree(&(*t)->rChild, x);//构造右子树 + } + return 1; +} +void VisitHouxu(BiTree t)//后续遍历 +{ + if (t == NULL) + { + /*printf("*");*/ + return; + } + VisitHouxu(t->lChild);//访问左子树 + VisitHouxu(t->rChild);//访问右子树 + printf("%c ", t->date); +} +//后序遍历二叉树 +int main() +{ + BiTree t; + ElemType n[num]; + int k; + printf("请选择要进行转化的字符串,输入1为abd*f***ce***,输入2为abdg***eh**i*k**c*f** *代表为空\n"); + scanf("%d", &k); + if (k == 1) + { + strcpy(n, "abd*f***ce***"); + } + else + { + strcpy(n, "abdg***eh**i*k**c*f**"); + } + printf("*代表空\n"); + CreatBiTree(&t, n);//产生二叉树 + printf("后序遍历的顺序为\n"); + VisitHouxu(t);//后序遍历二叉树 + return 0; +} diff --git a/2017-1/Xhl_blabla/erchashu-3/1.png b/2017-1/Xhl_blabla/erchashu-3/1.png new file mode 100644 index 00000000..692e3b73 Binary files /dev/null and b/2017-1/Xhl_blabla/erchashu-3/1.png differ diff --git a/2017-1/Xhl_blabla/erchashu-3/bitree.h b/2017-1/Xhl_blabla/erchashu-3/bitree.h new file mode 100644 index 00000000..0a511adb --- /dev/null +++ b/2017-1/Xhl_blabla/erchashu-3/bitree.h @@ -0,0 +1,15 @@ +#include +#include +typedef char ElemType; +typedef int status; +#define num 30 +int number = 0;//定义一个全局变量 用于赋值时的计数 + //定义二叉树 +typedef struct BiTree +{ + ElemType date; + struct BiTree *lChild; + struct BiTree * rChild; +} BitNode, *BiTree; +status CreatBiTree(BiTree *t, char *x);//先序创立二叉树 +void VisitHouxu(BiTree t);//后续遍历 \ No newline at end of file diff --git a/2017-1/Xhl_blabla/erchashu-3/psh.c b/2017-1/Xhl_blabla/erchashu-3/psh.c new file mode 100644 index 00000000..4a31d86b --- /dev/null +++ b/2017-1/Xhl_blabla/erchashu-3/psh.c @@ -0,0 +1,56 @@ +#include"bitree.h" +status CreatBiTree(BiTree *t, char *x) +{ + ElemType ch, temp; + ch = x[number]; + number++; + if (ch == '*') + { + *t = NULL; + } + else + { + *t = (BiTree)malloc(sizeof(BitNode));//分配空间进行存储 + if (!(*t))//如果分配失败 + { + exit(-1); + } + (*t)->date = ch;//生成根节点 + CreatBiTree(&(*t)->lChild, x);//构造左子树 + CreatBiTree(&(*t)->rChild, x);//构造右子树 + } + return 1; +} +void VisitHouxu(BiTree t)//后续遍历 +{ + if (t == NULL) + { + /*printf("*");*/ + return; + } + VisitHouxu(t->lChild);//访问左子树 + VisitHouxu(t->rChild);//访问右子树 + printf("%c ", t->date); +} +//后序遍历二叉树 +int main() +{ + BiTree t; + ElemType n[num]; + int k; + printf("请选择要进行转化的字符串,输入1为abd*f***ce***,输入2为abdg***eh**i*k**c*f** *代表为空\n"); + scanf("%d", &k); + if (k == 1) + { + strcpy(n, "abd*f***ce***"); + } + else + { + strcpy(n, "abdg***eh**i*k**c*f**"); + } + printf("*代表空\n"); + CreatBiTree(&t, n);//产生二叉树 + printf("后序遍历的顺序为\n"); + VisitHouxu(t);//后序遍历二叉树 + return 0; +} diff --git a/2017-1/Xhl_blabla/erchashu/1.png b/2017-1/Xhl_blabla/erchashu/1.png new file mode 100644 index 00000000..692e3b73 Binary files /dev/null and b/2017-1/Xhl_blabla/erchashu/1.png differ diff --git a/2017-1/Xhl_blabla/erchashu/bitree.h b/2017-1/Xhl_blabla/erchashu/bitree.h new file mode 100644 index 00000000..9afd8962 --- /dev/null +++ b/2017-1/Xhl_blabla/erchashu/bitree.h @@ -0,0 +1,15 @@ +#include +#include +typedef char ElemType; +typedef int status; +#define num 20 +int number = 0;//定义一个全局变量 用于赋值时的计数 + //定义二叉树 +typedef struct BiTree +{ + ElemType date; + struct BiTree *lChild; + struct BiTree * rChild; +} BitNode, *BiTree; +status CreatBiTree(BiTree *t, char *x);//先序创立二叉树 +void VisitHouxu(BiTree t);//后续遍历 \ No newline at end of file diff --git a/2017-1/Xhl_blabla/erchashu/psh.c b/2017-1/Xhl_blabla/erchashu/psh.c new file mode 100644 index 00000000..1e9b1332 --- /dev/null +++ b/2017-1/Xhl_blabla/erchashu/psh.c @@ -0,0 +1,56 @@ +#include"bitree.h" +status CreatBiTree(BiTree *t, char *x) +{ + ElemType ch, temp; + ch = x[number]; + number++; + if (ch == '*') + { + *t = NULL; + } + else + { + *t = (BiTree)malloc(sizeof(BitNode));//分配空间进行存储 + if (!(*t))//如果分配失败 + { + exit(-1); + } + (*t)->date = ch;//生成根节点 + CreatBiTree(&(*t)->lChild, x);//构造左子树 + CreatBiTree(&(*t)->rChild, x);//构造右子树 + } + return 1; +} +void VisitHouxu(BiTree t)//后续遍历 +{ + if (t == NULL) + { + printf("*"); + return; + } + VisitHouxu(t->lChild);//访问左子树 + VisitHouxu(t->rChild);//访问右子树 + printf("%c ", t->date); +} +//后序遍历二叉树 +int main() +{ + BiTree t; + ElemType n[num]; + int k; + printf("请选择要进行转化的字符串,输入1为abd*f***ce***,输入2为abdg**eh***k**c*f** *代表为空\n"); + scanf("%d", &k); + if (k == 1) + { + strcpy(n, "abd*f***ce***"); + } + else + { + strcpy(n, "abdg**eh***k**c*f**"); + } + printf("*代表空\n"); + CreatBiTree(&t, n);//产生二叉树 + printf("后序遍历的顺序为\n"); + VisitHouxu(t);//后序遍历二叉树 + return 0; +} diff --git a/2017-1/Xhl_blabla/hello.h b/2017-1/Xhl_blabla/hello.h new file mode 100755 index 00000000..c9eac1d5 --- /dev/null +++ b/2017-1/Xhl_blabla/hello.h @@ -0,0 +1,21 @@ +#include +#include +#include +#define LIST_NUM_A 4; +#define LIST_NUM_B 4; +typedef struct LNode +{ + int data; + struct LNode *next; + +}LNode, *LinkList; +void CreatList(LinkList *L, int n); +void MergeList(LinkList la, LinkList lb, LinkList *c); +void output(LinkList l); +void assign_value(int a[], int n); +void destoryList(LinkList); + + + + + diff --git a/2017-1/Xhl_blabla/sweet.cpp b/2017-1/Xhl_blabla/sweet.cpp new file mode 100755 index 00000000..68cf2b8b --- /dev/null +++ b/2017-1/Xhl_blabla/sweet.cpp @@ -0,0 +1,133 @@ +#include "hello.h" +int main() +{ + srand(time(0)); + LinkList la, lb, lc; + int a = LIST_NUM_A; + int b = LIST_NUM_B;//la的长度和lb的长度均为宏常量 + CreatList(&la, a); + output(la); + CreatList(&lb, b); + output(lb); + MergeList(la, lb, &lc); + output(lc); + destoryList(lb); + return 0; +} + +//逆序输入n个元素的值,建立单链表 +void CreatList(LinkList *L, int n) +{ + + int i; + int *a = (int*)malloc(sizeof(int)*n); + assign_value(a, n); + LinkList p; + (*L) = (LinkList)malloc(sizeof(LNode));//头节点 + (*L)->next = NULL; + for (i = n; i > 0; --i) + { + p = (LinkList)malloc(sizeof(LNode));//生成新节点 + p->data = a[n - i]; //输入元素值 + p->next = (*L)->next; + (*L)->next = p; //插入到表头 + } + free(a); +} + +//已知单链表线性表la和lb的元素按值非递减排列 +//合并后的链lc 仍然按值非递减排列 +void MergeList(LinkList la, LinkList lb, LinkList *lc) +{ + LinkList pa, pb, pc, l = NULL, lc_copy; + pa = la->next; + pb = lb->next; + (*lc) = pc = la; + while (pa&&pb) + { + if (pa->data <= pb->data) + { + pc->next = pa; + pc = pa; + pa = pa->next; + pc->next = NULL; + + } + else + { + pc->next = pb; + pc = pb; + pb = pb->next; + pc->next = NULL; + } + + lc_copy = (*lc)->next; + + while (lc_copy != NULL) + { + printf("%d ", lc_copy->data); + lc_copy = lc_copy->next; + } + printf("\n"); + + //厉害了 我的算法; + } + pc->next = pa ? pa : pb; + lc_copy = (*lc)->next; + while (lc_copy != NULL) + { + printf("%d ", lc_copy->data); + lc_copy = lc_copy->next; + } + printf("\n"); +} +//产生随机数,并进行排序 +void assign_value(int a[], int n) +{ + int i, j, temp; + //产生随机数 + for (i = 0; i < n; i++) + { + a[i] = rand() % 20 + 1; + } + // 冒泡法对随机数排序 + for (i = 0; i < n - 1; i++) + { + for (j = 0; j < n - 1 - i; j++) + { + if (a[j] < a[j + 1]) + { + temp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = temp; + + } + } + } +} +//输出链表 +void output(LinkList l) +{ + l = l->next; + printf("\nthe List is:\n"); + + while (l != NULL) + { + printf("%d ", l->data); + l = l->next; + } + printf("\n\n"); +} +//对链表执行销毁 +void destoryList(LinkList l) +{ + LinkList p; + + while (NULL != l) + { + p = l->next; + free(l); + l = p; + } + +} \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221/1.png" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221/1.png" new file mode 100644 index 00000000..d2463723 Binary files /dev/null and "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221/1.png" differ diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221/bitree.h" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221/bitree.h" new file mode 100644 index 00000000..3acb5bf6 --- /dev/null +++ "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221/bitree.h" @@ -0,0 +1,15 @@ +#include +#include +typedef char ElemType; +typedef int status; +#define num 20 +int number = 0;//定义一个全局变量 用于赋值时的计数 +//定义二叉树 +typedef struct BiTree +{ + ElemType date; + struct BiTree *lChild; + struct BiTree * rChild; +} BitNode, *BiTree; +status CreatBiTree(BiTree *t, char *x);//先序创立二叉树 +void VisitHouxu(BiTree t);//后续遍历 \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221/psh.c" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221/psh.c" new file mode 100644 index 00000000..35b9e60a --- /dev/null +++ "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221/psh.c" @@ -0,0 +1,46 @@ +#include"bitree.h" +status CreatBiTree(BiTree *t,char *x) +{ + ElemType ch, temp; + ch = x[number]; + number++; + if (ch == '*') + { + *t = NULL; + } + else + { + *t = (BiTree)malloc(sizeof(BitNode));//分配空间进行存储 + if (!(*t))//如果分配失败 + { + exit(-1); + } + (*t)->date = ch;//生成根节点 + CreatBiTree(&(*t)->lChild,x);//构造左子树 + CreatBiTree(&(*t)->rChild,x);//构造右子树 + } + return 1; +} +void VisitHouxu(BiTree t)//后续遍历 +{ + if (t == NULL) + { + return; + } + VisitHouxu(t->lChild);//访问左子树 + VisitHouxu(t->rChild);//访问右子树 + printf("%c ", t->date); +} +//后序遍历二叉树 +int main() +{ + BiTree t; + ElemType n[num]; + printf("请输入要进行转化的字符串 *代表为空\n"); + scanf("%s", n); + printf("*代表空\n"); + CreatBiTree(&t ,n);//产生二叉树 + printf("后序遍历的顺序为\n"); + VisitHouxu(t);//后序遍历二叉树 + return 0; +} diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\345\217\266\345\255\220\350\212\202\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\350\212\202\347\202\2712/1.png" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\345\217\266\345\255\220\350\212\202\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\350\212\202\347\202\2712/1.png" new file mode 100644 index 00000000..f96115fe Binary files /dev/null and "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\345\217\266\345\255\220\350\212\202\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\350\212\202\347\202\2712/1.png" differ diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\345\217\266\345\255\220\350\212\202\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\350\212\202\347\202\2712/bitree.h" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\345\217\266\345\255\220\350\212\202\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\350\212\202\347\202\2712/bitree.h" new file mode 100644 index 00000000..71b31a55 --- /dev/null +++ "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\345\217\266\345\255\220\350\212\202\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\350\212\202\347\202\2712/bitree.h" @@ -0,0 +1,16 @@ +#include +#include +typedef char ElemType; +typedef int status; +#define num 30 +int number = 0;//定义一个全局变量 用于赋值时的计数 + //定义二叉树 +typedef struct BiTree +{ + ElemType date; + struct BiTree *lChild; + struct BiTree * rChild; +} BitNode, *BiTree; +status CreatBiTree(BiTree *t, char *x);//先序创立二叉树 +int GetCount(BiTree T);//求二叉树的所有结点 +int Leafcount(BiTree T, int *m);//求二叉树叶子结点个数 \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\345\217\266\345\255\220\350\212\202\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\350\212\202\347\202\2712/psh.c" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\345\217\266\345\255\220\350\212\202\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\350\212\202\347\202\2712/psh.c" new file mode 100644 index 00000000..7869107e --- /dev/null +++ "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\345\217\266\345\255\220\350\212\202\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\350\212\202\347\202\2712/psh.c" @@ -0,0 +1,67 @@ +#include"bitree.h" +status CreatBiTree(BiTree *t, char *x) +{ + ElemType ch, temp; + ch = x[number]; + number++; + if (ch == '*') + { + *t = NULL; + } + else + { + *t = (BiTree)malloc(sizeof(BitNode));//分配空间进行存储 + if (!(*t))//如果分配失败 + { + exit(-1); + } + (*t)->date = ch;//生成根节点 + CreatBiTree(&(*t)->lChild, x);//构造左子树 + CreatBiTree(&(*t)->rChild, x);//构造右子树 + } + return 1; +} +//求二叉树叶子结点个数 +int Leafcount(BiTree T,int *m) +{ + if (T) + { + if (T->lChild == NULL &&T->rChild == NULL) + (*m)++; + Leafcount(T->lChild, m); + Leafcount(T->rChild, m); + } + return *m; +} +//求二叉树的所有结点 +int GetCount(BiTree T) +{ + if (T == NULL) + { + return 0; + } + return 1 + GetCount(T->lChild) + GetCount(T->rChild);//返回子数的结点 +} +int main() +{ + BiTree t; + ElemType n[num]; + int m = 0;//进行叶子结点的统计 + int k; + printf("请选择要进行转化的字符串,输入1为abd*f***ce***,输入2为abdg***eh**i*k**c*f** *代表为空\n"); + scanf("%d", &k); + if (k == 1) + { + strcpy(n, "abd*f***ce***"); + } + else + { + strcpy(n, "abdg***eh**i*k**c*f**"); + } + printf("*代表空\n"); + CreatBiTree(&t, n);//产生二叉树 + Leafcount(t, &m);//叶子结点 + printf("\n叶子结点的个数为%d",m); + printf("\n非叶子结点的个数为%d", GetCount(t)-m); + return 0; +} diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\222\214\345\256\275\345\272\246/3.png" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\222\214\345\256\275\345\272\246/3.png" new file mode 100644 index 00000000..3a7a1db6 Binary files /dev/null and "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\222\214\345\256\275\345\272\246/3.png" differ diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\222\214\345\256\275\345\272\246/bitree.h" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\222\214\345\256\275\345\272\246/bitree.h" new file mode 100644 index 00000000..a162fc2c --- /dev/null +++ "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\222\214\345\256\275\345\272\246/bitree.h" @@ -0,0 +1,17 @@ +#include +#include +typedef char ElemType; +typedef int status; +#define num 20 +#define width_num 10 +int number = 0;//定义一个全局变量 用于赋值时的计数 + //定义二叉树 +typedef struct BiTree +{ + ElemType date; + struct BiTree *lChild; + struct BiTree * rChild; +} BitNode, *BiTree; +int TreeDeep(BiTree T);//查找数的深度 +status CreatBiTree(BiTree *t, char *x);//先序创立二叉树 +void VisitHouxu(BiTree t);//后续遍历 \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\222\214\345\256\275\345\272\246/psh.c" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\222\214\345\256\275\345\272\246/psh.c" new file mode 100644 index 00000000..ce116c86 --- /dev/null +++ "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\222\214\345\256\275\345\272\246/psh.c" @@ -0,0 +1,95 @@ +#include"bitree.h" +status CreatBiTree(BiTree *t, char *x) +{ + ElemType ch, temp; + ch = x[number]; + number++; + if (ch == '*') + { + *t = NULL; + } + else + { + *t = (BiTree)malloc(sizeof(BitNode));//分配空间进行存储 + if (!(*t))//如果分配失败 + { + exit(-1); + } + (*t)->date = ch;//生成根节点 + CreatBiTree(&(*t)->lChild, x);//构造左子树 + CreatBiTree(&(*t)->rChild, x);//构造右子树 + } + return 1; +} +//后序遍历二叉树 +void VisitHouxu(BiTree t)//后续遍历 +{ + if (t == NULL) + { + printf("*"); + return; + } + VisitHouxu(t->lChild);//访问左子树 + VisitHouxu(t->rChild);//访问右子树 + printf("%c ", t->date); +} +//二叉树的深度 +int TreeDeep(BiTree T) +{ + int deep = 0; + if (T) + { + int leftdeep = TreeDeep(T->lChild);//查找左子树的的最深值 + int rightdeep = TreeDeep(T->rChild);//查找右子树的最深值 + deep = leftdeep >= rightdeep ? leftdeep + 1 : rightdeep + 1; + } + return deep; +} +//后序遍历二叉树 +int floor = 1;//用于计数层数 +int width[width_num] = {0};//用于存储每层的结点个数 +int widthHouxu(BiTree t)//后续遍历 +{ + int i ; + int max = 0;//用于存储最大值 + if (t == NULL) + { + return 0; + } + width[floor]++; + floor++; + widthHouxu(t->lChild);//访问左子树 + widthHouxu(t->rChild);//访问右子树 + floor--;//当左子树和右子树均遍历了 返回上一层继续统计 + for (i = 0; i < width_num; i++)//找到最大值进行输出 + { + if (max < width[i]) + { + max = width[i]; + } + } + return max; +} +int main() +{ + BiTree t; + ElemType n[num]; + int k; + printf("请选择要进行转化的字符串,输入1为abd*f***ce***,输入2为abdg**eh***k**c*f** *代表为空\n"); + scanf("%d", &k); + if (k == 1) + { + strcpy(n, "abd*f***ce***"); + } + else + { + strcpy(n, "abdg**eh***k**c*f**"); + } + printf("*代表空\n"); + CreatBiTree(&t, n);//产生二叉树 + printf("后序遍历的顺序为\n"); + VisitHouxu(t);//后序遍历二叉树 + printf ("\n树的深度为%d \n",TreeDeep(t)); + printf("\n树的宽度为%d \n", widthHouxu(t)); + return 0; +} diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\256\275\345\272\246 2/3.png" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\256\275\345\272\246 2/3.png" new file mode 100644 index 00000000..3a7a1db6 Binary files /dev/null and "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\256\275\345\272\246 2/3.png" differ diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\256\275\345\272\246 2/bitree.h" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\256\275\345\272\246 2/bitree.h" new file mode 100644 index 00000000..1a193a76 --- /dev/null +++ "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\256\275\345\272\246 2/bitree.h" @@ -0,0 +1,17 @@ +#include +#include +typedef char ElemType; +typedef int status; +#define num 30 +#define width_num 10 +int number = 0;//定义一个全局变量 用于赋值时的计数 + //定义二叉树 +typedef struct BiTree +{ + ElemType date; + struct BiTree *lChild; + struct BiTree * rChild; +} BitNode, *BiTree; +int TreeDeep(BiTree T);//查找数的深度 +status CreatBiTree(BiTree *t, char *x);//先序创立二叉树 +void VisitHouxu(BiTree t);//后续遍历 \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\256\275\345\272\246 2/psh.c" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\256\275\345\272\246 2/psh.c" new file mode 100644 index 00000000..af6404a9 --- /dev/null +++ "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\345\256\275\345\272\246 2/psh.c" @@ -0,0 +1,95 @@ +#include"bitree.h" +status CreatBiTree(BiTree *t, char *x) +{ + ElemType ch, temp; + ch = x[number]; + number++; + if (ch == '*') + { + *t = NULL; + } + else + { + *t = (BiTree)malloc(sizeof(BitNode));//分配空间进行存储 + if (!(*t))//如果分配失败 + { + exit(-1); + } + (*t)->date = ch;//生成根节点 + CreatBiTree(&(*t)->lChild, x);//构造左子树 + CreatBiTree(&(*t)->rChild, x);//构造右子树 + } + return 1; +} +//后序遍历二叉树 +void VisitHouxu(BiTree t)//后续遍历 +{ + if (t == NULL) + { + printf("*"); + return; + } + VisitHouxu(t->lChild);//访问左子树 + VisitHouxu(t->rChild);//访问右子树 + printf("%c ", t->date); +} +//二叉树的深度 +int TreeDeep(BiTree T) +{ + int deep = 0; + if (T) + { + int leftdeep = TreeDeep(T->lChild);//查找左子树的的最深值 + int rightdeep = TreeDeep(T->rChild);//查找右子树的最深值 + deep = leftdeep >= rightdeep ? leftdeep + 1 : rightdeep + 1; + } + return deep; +} +//后序遍历二叉树 +int floor = 1;//用于计数层数 +int width[width_num] = { 0 };//用于存储每层的结点个数 +int widthHouxu(BiTree t)//后续遍历 +{ + int i; + int max = 0;//用于存储最大值 + if (t == NULL) + { + return 0; + } + width[floor]++; + floor++; + widthHouxu(t->lChild);//访问左子树 + widthHouxu(t->rChild);//访问右子树 + floor--;//当左子树和右子树均遍历了 返回上一层继续统计 + for (i = 0; i < width_num; i++)//找到最大值进行输出 + { + if (max < width[i]) + { + max = width[i]; + } + } + return max; +} +int main() +{ + BiTree t; + ElemType n[num]; + int k; + printf("请选择要进行转化的字符串,输入1为abd*f***ce***,输入2为abdg***eh**i*k**c*f** *代表为空\n"); + scanf("%d", &k); + if (k == 1) + { + strcpy(n, "abd*f***ce***"); + } + else + { + strcpy(n, "abdg***eh**i*k**c*f**"); + } + printf("*代表空\n"); + CreatBiTree(&t, n);//产生二叉树 + printf("后序遍历的顺序为\n"); + VisitHouxu(t);//后序遍历二叉树 + printf("\n树的深度为%d \n", TreeDeep(t)); + printf("\n树的宽度为%d \n", widthHouxu(t)); + return 0; +} diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/1.png" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/1.png" new file mode 100644 index 00000000..f96115fe Binary files /dev/null and "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/1.png" differ diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/bitree.h" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/bitree.h" new file mode 100644 index 00000000..842a1be3 --- /dev/null +++ "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/bitree.h" @@ -0,0 +1,16 @@ +#include +#include +typedef char ElemType; +typedef int status; +#define num 20 +int number = 0;//定义一个全局变量 用于赋值时的计数 + //定义二叉树 +typedef struct BiTree +{ + ElemType date; + struct BiTree *lChild; + struct BiTree * rChild; +} BitNode, *BiTree; +status CreatBiTree(BiTree *t, char *x);//先序创立二叉树 +int GetCount(BiTree T);//求二叉树的所有结点 +int Leafcount(BiTree T, int *m);//求二叉树叶子结点个数 \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/psh.c" "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/psh.c" new file mode 100644 index 00000000..979668f9 --- /dev/null +++ "b/2017-1/Xhl_blabla/\344\272\214\345\217\211\346\240\221\347\232\204\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/psh.c" @@ -0,0 +1,67 @@ +#include"bitree.h" +status CreatBiTree(BiTree *t, char *x) +{ + ElemType ch, temp; + ch = x[number]; + number++; + if (ch == '*') + { + *t = NULL; + } + else + { + *t = (BiTree)malloc(sizeof(BitNode));//分配空间进行存储 + if (!(*t))//如果分配失败 + { + exit(-1); + } + (*t)->date = ch;//生成根节点 + CreatBiTree(&(*t)->lChild, x);//构造左子树 + CreatBiTree(&(*t)->rChild, x);//构造右子树 + } + return 1; +} +//求二叉树叶子结点个数 +int Leafcount(BiTree T,int *m) +{ + if (T) + { + if (T->lChild == NULL &&T->rChild == NULL) + (*m)++; + Leafcount(T->lChild, m); + Leafcount(T->rChild, m); + } + return *m; +} +//求二叉树的所有结点 +int GetCount(BiTree T) +{ + if (T == NULL) + { + return 0; + } + return 1 + GetCount(T->lChild) + GetCount(T->rChild);//返回子数的结点 +} +int main() +{ + BiTree t; + ElemType n[num]; + int m = 0;//进行叶子结点的统计 + int k; + printf("请选择要进行转化的字符串,输入1为abd*f***ce***,输入2为abdg**eh***k**c*f** *代表为空\n"); + scanf("%d", &k); + if (k == 1) + { + strcpy(n, "abd*f***ce***"); + } + else + { + strcpy(n, "abdg**eh***k**c*f**"); + } + printf("*代表空\n"); + CreatBiTree(&t, n);//产生二叉树 + Leafcount(t, &m);//叶子结点 + printf("\n叶子结点的个数为%d",m); + printf("\n非叶子结点的个数为%d", GetCount(t)-m); + return 0; +} diff --git "a/2017-1/Xhl_blabla/\345\220\216\347\274\200\345\274\217/1.c" "b/2017-1/Xhl_blabla/\345\220\216\347\274\200\345\274\217/1.c" new file mode 100644 index 00000000..bcedb013 --- /dev/null +++ "b/2017-1/Xhl_blabla/\345\220\216\347\274\200\345\274\217/1.c" @@ -0,0 +1,318 @@ +#include "1.h" +int main() +{ + char x[SUANSHUSHI_NUM]; + int t; + scanf("%s", x); + stack mystack; + initstack(&mystack); + rank_list(&mystack, x); + printf("\n 排序完成后 \n"); + Traverse_Stack(mystack);//第一次进行排序之后 + printf("\n*********************************************************\n"); + t = cal_list(mystack); + printf("公式的计算结果是 %d", t); + free(mystack.base); + return 0; +} +//初始化栈 +status initstack(stack* s) +{ + s->base = (ElemType*)malloc(sizeof(ElemType)); + if (NULL == s->top) + { + printf("Memory allocation failure"); + exit(-1); + } + s->top = s->base; + int k = DATE_NUM; + s->top->date = (char*)malloc(sizeof(char) * k); + s->top->date = "#\0";//怎么不能指向空 + s->base = s->top;//防止出现野指针 + return 1; +} +//检验栈是否为空 是返回0 否返回1 +status isStackempty(stack s) +{ + if (s.base == s.top) + { + return 0; + } + return 1; +} +//进行压栈 +status push(stack *s, char *e) +{ + ElemType *p = (ElemType*)malloc(sizeof(ElemType)); + if (NULL == p) + { + printf("Memory allocation failure"); + exit(-1); + } + int k = DATE_NUM; + p->date = (char*)malloc(sizeof(char) *k); + strcpy(p->date, e); + p->pNext = s->top; //让p的指针域指向上一个节点 + s->top = p; //让pTop指针指向栈顶元素 + return 1; +} +//用于判断是数字还是界限符还是运算符 是数字就返回0 +status judgenumber(char x) +{ + int i; + switch (x) + { + case '+': + case'-': + case'*': + case'/': + i = 1;//运算符返回 1 + break; + case '(': + i = 3; + break; + case')': + i = 4; //界限符返回-1 + break; + case'\0': + i = -10;//过界返回-10 + break; + case '#': + i = 6;//如果是到了栈底 + break; + default: + i = 0; //数字返回0 + break; + } + return i; +} +//用于字符串的拆分,为压栈做准备 +char** conversion(char x[]) +{ + int i = 0, j = 0, k = 0; + int q = WEIDU_NUM; + int p = DATE_NUM; + char **y = (char**)malloc(sizeof(char*)*q); + for (i = 0; i < q; i++) + { + y[i] = (char*)malloc(sizeof(char)*p); + } + //动态分配二维数组进行存储 + i = 0; + do + {//其实此段可以进行化简 只要进行字符串的化简 + j = 0; + if (judgenumber(x[i]) == 0) + { + while (judgenumber(x[i]) == 0 && x[i] != '\0')//当输入的字符是数字 进行存储 + { + y[k][j] = x[i]; + i++; + j++; + } + y[k][j] = '\0'; + } + else + { + y[k][j] = x[i]; + y[k][1] = '\0'; + i++; + } + k++; + } while (x[i] != '\0'); + y[k][0] = '\0';//结尾元素怎么设计 + i = 0; + printf("将输入的字符串进行拆分 \n"); + while (y[i][0] != '\0') + { + printf("%s ", y[i]); + i++; + } + return y; +} +//用于后缀式的排序 排数字和运算符 +status rank_list(stack *s, char x[]) +{ + stack print_out_stack; + initstack(&print_out_stack);//用于存储打印输出的栈 + int i = 0;//j的作用是 只用于入栈时候的循环 作用范围很小 i的作用是记录头尾的数字 + char**p = conversion(x);//拆分成独立的元素 + char *temp;//临时存储值 + int q = WEIDU_NUM;//用于p的释放 + while (p[i][0] != '\0') + { + if (judgenumber(p[i][0]) == 0)//如果 + { + push(&print_out_stack, p[i]);//直接打印 + printf("\n遇见数字直接打印 %s \n ", p[i]); + } + else if (judgenumber(p[i][0]) == 1)//如果是运算符 + { + if (0==isStackempty(*s) || judge_pre(p[i][0]) >= judge_pre(s->top->date[0])|| s->top->date[0]=='(')//如果栈为空或元素的优先级大于等于栈顶元素 进行入栈 + { + printf("\n优先级较高 或栈顶为括号 入栈 %s\n", p[i]); + push(s, p[i]); + } + else//将所有需要弹出的数字进行打印 并将新的运算符进行压栈 + { + //弹出栈顶元素 直到遇见左括号或者遇见优先级更低的元素 或栈空 当栈顶元素优先级大或相等 出栈 + while (0 != isStackempty(*s) && judgenumber(s->top->date[0]) != 3 && judge_pre(s->top->date[0]) >= judge_pre(p[i][0]))//左括号和右括号的数字不同 + { + + Pop_Stack(s, &temp); + push(&print_out_stack, temp); + printf("\n%s优先级低于%s 将 %s 出栈打印\n", p[i],temp,temp); + } + push(s, p[i]);//并入栈 + } + } + else //如果遇见括号 左括号直接进栈 右括号进行打印输出直到遇见左括号 + { + if (judgenumber(p[i][0]) == 3) + { + printf("\n将左括号入栈\n"); + push(s, p[i]); + } + else//如果是右括号 进行弹出并进行一系列出栈操作 + { + printf("\n遇见右括号 将左括号以前的操作符进行打印\n"); + while (judgenumber(s->top->date[0]) != 3) + { + Pop_Stack(s, &temp); + push(&print_out_stack, temp); + printf("\n打印 %s\n", temp); + } + Pop_Stack(s, &temp);//弹出左括号 + printf("\n弹出左括号\n"); + } + } + i++; + } + //对拆分构造的元素占用内存进行释放 + while (isStackempty(*s)) + { + Pop_Stack(s, &temp); + push(&print_out_stack, temp); + printf("\n打印 %s\n", temp); + } + //释放截取字符时的内存 + for (i = 0; i < q; i++)//释放临时的二级指针 + { + free(p[i]); + } + free(p); + while (isStackempty(print_out_stack)) + { + Pop_Stack(&print_out_stack, &temp); + push(s, temp); + } + free(print_out_stack.base); + return 1; +} +//判断运算符的优先级 +status judge_pre(char x) +{ + int i; + switch (x) + { + case '+': + case'-': + i = 0; + break; + case'*': + case'/': + i = 1;//运算符返回 1 + break; + default: + i = 2; + break; + } + return i; +} +//弹出栈顶 +status Pop_Stack(stack *S, char ** val) +{ + ElemType* p = S->top; + (*val) = (char*)malloc(sizeof(char) * 5); + (*val) = S->top->date; + S->top = S->top->pNext; //使pTop指针指向栈顶元素; + free(p); //释放p指针所指向的那个节点的内存; + p = NULL; //要养成好的习惯; + return 1; +} +void Traverse_Stack(stack s) +{ + ElemType *p = s.top; + printf("\n栈中的元素是:\n"); + while (p != s.base) + { + printf("%s ", p->date); + p = p->pNext; + } + printf("\n"); +} +//用于计算后缀式 +status cal_list(stack s)//传入的数据 +{ + stack mystack;//用于计算的栈 + initstack(&mystack); + char * temp;//用于临时存储的栈顶元素 + while (s.top != s.base) + { + if(judgenumber(s.top->date[0]) == 0)//如果是数字直接进行压栈 + { + Pop_Stack(&s, &temp); + push(&mystack, temp); + } + else + {//如果遇到运算符进行计算 + char *v;//用于 + int a = (int)atof(mystack.top->date);//转化栈顶元素 + int b = (int)atof(mystack.top->pNext->date);//转化第二个元素 + int c = cal(a, b, s.top->date[0]); + printf("\n取出栈中的 %d %d 进行%c的计算", a, b,s.top->date[0]); + printf(" %d %c %d = %d\n", b, s.top->date[0], a, c); + Pop_Stack(&mystack, &v);//计算完成后对数据进行相应的出栈 + Pop_Stack(&mystack, &v); + Pop_Stack(&s, &v); + itoa(c, v, 10); + push(&mystack, v);//将计算结果进行入栈 + free(v);//对临时分配的量进行释放 + } + } + free(temp); + int c = (int)atof(mystack.top->date); + free(mystack.top); + return c; +} +int cal(int a, int b, char x)//进行计算 +{ + switch (x) + { + case '+': + return a + b; + break; + case '-': + return b- a; + break; + case '*': + return a*b; + break; + case '/': + return b / a; + break; + } +} +//销毁栈 +status destory(stack *s) +{ + ElemType *p; + while (s->top != s->base) + { + p = s->top; + s->top = s->top->pNext; + free(p); + } + free(s->base); + return 1; +} diff --git "a/2017-1/Xhl_blabla/\345\220\216\347\274\200\345\274\217/1.h" "b/2017-1/Xhl_blabla/\345\220\216\347\274\200\345\274\217/1.h" new file mode 100644 index 00000000..f74dd7a6 --- /dev/null +++ "b/2017-1/Xhl_blabla/\345\220\216\347\274\200\345\274\217/1.h" @@ -0,0 +1,36 @@ +//必须得把顺序栈改成链式栈吗 +//只能处理有一个括号的运算式 而且是数字要求全是 int 整数 +//只能进行大于零的计算 因为没有设数据存数字位 +#include +#include +#include +#define SUANSHUSHI_NUM 20//输入的式子所在的数组的长度 +#define DATE_NUM 10 //栈的字符串元素的长度 +#define STACR_INIT_SIZE 100 +#define STACR_ADD_SIZE 10 +#define WEIDU_NUM 20; +typedef struct ElemType +{ + char *date; + struct ElemType *pNext; +}ElemType; +typedef int status;//整数的别名 +typedef struct +{ + ElemType *base; + ElemType *top; +}stack;//用于存储运算符和操作数的栈 +status initstack(stack *s);//初始化栈 +status destory(stack *s);//销毁栈 +status isStackempty(stack s);//检验栈是否为空 是返回0 否返回1 +status judgenumber(char x);//用于判断是数字还是操作符 运算符返回 1 界限符返回-1 过界返回-10 数字返回0 +status rank_list(stack*, char x[]);//用于后缀式的排序 +status cal_list(stack);//用于后缀式初次排序 排数字和运算符 +char** conversion(char x[]);//用于字符串的拆分 +status judge_pre(char);//对字符的优先级进行比较 优先级越大数字越大 +status push(stack *s, char *e); +status initstack(stack* s);//创建一个空栈,里面没有任何有效数据; +status Pop_Stack(stack *S, char ** val); +void Traverse_Stack(stack s);//打印栈 +int cal(int a, int b, char x);//计算 + diff --git "a/2017-1/Xhl_blabla/\345\220\216\347\274\200\345\274\217/1.png" "b/2017-1/Xhl_blabla/\345\220\216\347\274\200\345\274\217/1.png" new file mode 100644 index 00000000..c1bdea4c Binary files /dev/null and "b/2017-1/Xhl_blabla/\345\220\216\347\274\200\345\274\217/1.png" differ diff --git "a/2017-1/Xhl_blabla/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Source.c" "b/2017-1/Xhl_blabla/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Source.c" new file mode 100644 index 00000000..90368b45 --- /dev/null +++ "b/2017-1/Xhl_blabla/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Source.c" @@ -0,0 +1,487 @@ +#include "Source.h" +int main() +{ + char x[SUANSHUSHI_NUM]; + int t; + scanf("%s", x); + stack mystack; + initstack(&mystack); + rank_list(&mystack, x); + printf("\n 初次排序完成后 (只进行括号里的字符排序 和括号外的数字和字符排序 ) "); + Traverse_Stack(mystack);//第一次进行排序之后 + rank_list_more(&mystack);//后缀式排序完成 + Traverse_Stack(mystack); + printf("\n第二步 对后缀式进行计算\n"); + t=cal_list(mystack); + printf("公式的计算结果是 %d", t); + destory(&mystack); + return 0; +} +//初始化栈 +status initstack(stack* s) +{ + s->base = (ElemType*)malloc(sizeof(ElemType)); + if (NULL == s->top) + { + printf("Memory allocation failure"); + exit(-1); + } + s->top = s->base; + int k = DATE_NUM; + s->top->date = (char*)malloc(sizeof(char) * k); + s->top->date = "#\0";//怎么不能指向空 + s->base=s->top =NULL;//防止出现野指针 + return 1; +} +//检验栈是否为空 是返回0 否返回1 +status isStackempty(stack s) +{ + if (s.base == s.top) + { + return 0; + } + return 1; +} +//进行压栈 +status push(stack *s, char *e) +{ + ElemType *p = (ElemType*)malloc(sizeof(ElemType)); + if (NULL == p) + { + printf("Memory allocation failure"); + exit(-1); + } + int k = DATE_NUM; + p->date = (char*)malloc(sizeof(char) *k); + strcpy(p->date, e); + p->pNext = s->top; //让p的指针域指向上一个节点 + s->top = p; //让pTop指针指向栈顶元素 + return 1; +} +//用于判断是数字还是界限符还是运算符 是数字就返回0 +status judgenumber(char x) +{ + int i; + switch (x) + { + case '+': + case'-': + case'*': + case'/': + i = 1;//运算符返回 1 + break; + case '(': + case')': + i = -1; //界限符返回-1 + break; + case'\0': + i = -10;//过界返回-10 + break; + default: + i = 0; //数字返回0 + break; + } + return i; +} +//用于字符串的拆分,为压栈做准备 +char** conversion(char x[]) +{ + int i = 0, j = 0, k = 0; + int q = WEIDU_NUM; + int p = DATE_NUM; + char **y = (char**)malloc(sizeof(char*)*q); + for (i = 0; i < q; i++) + { + y[i] = (char*)malloc(sizeof(char)*p); + } + //动态分配二维数组进行存储 + i = 0; + do + {//其实此段可以进行化简 只要进行字符串的化简 + j = 0; + if (judgenumber(x[i]) == 0) + { + while (judgenumber(x[i]) ==0&& x[i] != '\0')//当输入的字符是数字 进行存储 + { + y[k][j] = x[i]; + i++; + j++; + } + y[k][j] = '\0'; + } + else + { + y[k][j] = x[i]; + y[k][1] = '\0'; + i++; + } + k++; + } while (x[i] != '\0'); + y[k][0] = '\0';//结尾元素怎么设计 + i = 0; + printf("将输入的字符串进行拆分 \n"); + while (y[i][0] != '\0') + { + printf("%s ", y[i]); + i++; + } + return y; +} +//用于后缀式的初次排序 排数字和运算符 +status rank_list(stack *s, char x[]) +{ + int i = 0, j;//j的作用是 只用于入栈时候的循环 作用范围很小 i的作用是记录头尾的数字 + int begin = 0, end = 0;//压栈数组的始末 + int flag;//辅助判断 + char**p = conversion(x);//拆分成独立的元素 + int q = WEIDU_NUM;//用于p的释放 + p[0]; + char temp[DATE_NUM];//临时存储值 + int a, b; + while (p[i][0] != '\0') + { + if (judgenumber(p[i][0]) == -1)//如果是括号 进行内部排序 再入栈 一次就彻底排好 以后看作一个整体 + {//确定开始结尾 进行压栈 + push(s, p[i]);//将括号入栈 + i++; + begin = i; + while (judgenumber(p[i][0]) != -1)//当不是括号时循环 + { + i++; + } + end = i; + //冒泡法对数组进行排序 + for (a = begin; a < end-1 ; a++) + { + for (b = begin; b < end - 1 - a+begin; b++) + { + int k = 0; + if (judge_pre(p[b][0]) < judge_pre(p[b + 1][0]))//对优先级比较排序 + { + strcpy(temp, p[b]); + strcpy(p[b], p[b + 1]); + strcpy(p[b + 1], temp); + } + } + } + int de; + printf("\n 将括号里的字符进行排序 \n"); + for ( de= begin; de< end;de++) + { + printf("%s ", p[de]); + } + for (j = begin; j < end; j++) + { + push(s, p[j]);//对元素进行入栈 + } + push(s, p[i]);//将括号入栈 + i++; + begin = i; + } + else //如果不是括号直接进行排序 压栈 + {//在这里面只进行数字与运算符的排序 + flag = 0; + while (flag == 0 || judgenumber(p[i][0]) == 0)//是数字或者第一次进入 + { + if (judgenumber(p[i][0]) != 0)//如果不是数字 + { + flag = 1; + if (judgenumber(p[i + 1][0]) == 0)//交换数值 + { + strcpy(temp, p[i]); + strcpy(p[i], p[i + 1]); + strcpy(p[i + 1], temp); + end = i + 1; + i += 2; + //可以进行压栈了 + for (j = begin; j <= end; j++) + { + push(s, p[j]);//对元素进行入栈 + } + begin = i; + i--; + } + else + { + push(s, p[i]); + begin = i+1;//对begin进行赋 + } + //压完栈就可以跳出来了 + } + else//如果是数字直接入栈 + { + push(s, p[i]); + begin++;//压栈之后对值做出更新 + } + i++;//进行下一轮 + //什么作用 + for (a = begin; a < end - 1; a++) + { + for (b = begin; b < end - 1 - a + begin; b++) + { + int k = 0; + if (judge_pre(p[b][0]) < judge_pre(p[b + 1][0]))//对优先级比较排序 + { + strcpy(temp, p[b]); + strcpy(p[b], p[b + 1]); + strcpy(p[b + 1], temp); + } + } + } + } + } + } + for (i = 0; i < q; i++)//释放临时的二级指针 + { + free(p[i]); + } + free(p); + return 1; +} +//判断优先级 二次判断后缀式的时候使用 +status judge_pre2(char x) +{ + int i; + switch (x) + { + case '+': + case'-': + i = 0; + break; + case'*': + case'/': + case '(': + case')': + i = 1;//运算符返回 1 + break; + default: + i = 1;//字母的优先级 + break; + } + return i; +} +//对字符的优先级进行 优先级越大数值越高 用于初次排序 +status judge_pre(char x) +{ + int i; + switch (x) + { + case '+': + case'-': + i = 0; + break; + case'*': + case'/': + case '(': + case')': + i = 1;//运算符返回 1 + break; + default: + i = 2;//字母的优先级 + break; + } + return i; +} +//弹出栈顶 +status Pop_Stack(stack *S, char ** val) +{ + ElemType* p = S->top; + (*val) = (char*)malloc(sizeof(char) * 5); + (*val) = S->top->date; + S->top = S->top->pNext; //使pTop指针指向栈顶元素; + free(p); //释放p指针所指向的那个节点的内存; + p = NULL; //要养成好的习惯; + return 1; +} +void Traverse_Stack(stack s) +{ + ElemType *p = s.top; + printf("\n栈中的元素是:\n"); + while (p != s.base) + { + printf("%s ", p->date); + p = p->pNext; + } + printf("\n"); +} +//这一次只用排运算符 +status rank_list_more(stack *s) +{ + //这一次只用排运算符 + int i = 0, j, k = 0; + int q = WEIDU_NUM; + int p = DATE_NUM; + char **y = (char**)malloc(sizeof(char*)*q);//临时存储要排序的元素 + char**x = (char**)malloc(sizeof(char*)*q);//存储从括号里拿出来的元素 + for (i = 0; i < q; i++) + { + y[i] = (char*)malloc(sizeof(char)*p); + x[i] = (char*)malloc(sizeof(char)*p); + } + //动态分配二维数组进行存储 + ElemType *temp; + temp = s->top; + i = 0; + j = 0; + while (temp != s->base) + { + if (judgenumber(temp->date[0]) != -1)//如果不是括号 + { + strcpy(y[i], temp->date); + i++; + temp = temp->pNext; + } + else + { + strcpy(y[i], temp->date);//将括号存进去 + i++; + temp = temp->pNext;//存括号里的其余的部分 + while (judgenumber(temp->date[0]) != -1) + { + strcpy(x[j], temp->date); + j++; + temp = temp->pNext; + } + strcpy(x[j], temp->date); + temp = temp->pNext; + } + } + int end = i; + int x_num = j + 1; + //对运算符进行排序 + char *tem = (char*)malloc(sizeof(char)*p); + for (i = 0; i judge_pre2(y[j + 1][0]))//对优先级比较排序 + { + strcpy(tem, y[j]); + strcpy(y[j], y[j + 1]); + strcpy(y[j + 1], tem); + } + } + }//插入括号中取出的元素 + free(tem);//释放临时分配的内存 + printf("\n二次排序求后缀式时 未插入括号前\n"); + for (i = 0; i < end; i++) + { + printf("%s ", y[i]); + } + for (i = 0; i < end; i++) + { + if (judgenumber(y[i][0]) == -1) + { + for (j = 0; j < x_num; j++) + { + for (k = end - 1; k > i + j; k--) + { + strcpy(y[k + 1], y[k]); + } + strcpy(y[i + j + 1], x[j]); + end++; + } + break;//插入括号后就跳出来 + } + } + //然后再入栈 + printf("\n二次排序求后缀式时 插入括号后\n"); + for (i = 0; i < end; i++) + { + printf("%s ", y[i]); + } + i = end - 1; + temp = s->top; + while (temp != s->base)//将排好序的数组入栈 + { + + strcpy(temp->date, y[i]); + temp = temp->pNext; + i--; + } + for (i = 0; i < q; i++) + { + free(x[i]); + free(y[i]); + } + free(x); + free(y); + return 1; +} +//用于计算后缀式 +status cal_list(stack s) +{ + stack mystack; + initstack(&mystack); + ElemType *temp; + temp = s.top; + while (temp != s.base) + { + if (judgenumber(temp->date[0]) != -1)//如果不是括号就进行压栈 + { + push(&mystack, temp->date); + //判断是否可以进行运算 + if ( judgenumber(mystack.top->date[0]) == 1)//是运算符 + { + if (judgenumber(mystack.top->pNext->date[0]) == 0)//数字 + { + if (judgenumber(mystack.top->pNext->pNext->date[0]) == 0)//进行数字转换并计算并压栈 + { + char *v=(char*)malloc(sizeof(char)*DATE_NUM);//用到 + int a =(int) atof(mystack.top->pNext->date); + + int b = (int)atof(mystack.top->pNext->pNext->date); + + int c = cal(a, b, mystack.top->date[0]); + printf("\n取出栈中的 %d %d 进行%c的计算", a, b, mystack.top->date[0]); + printf(" %d %c %d = %d\n", a, mystack.top->date[0], b, c); + Pop_Stack(&mystack, &v); + //printf("弹出%s \n", val); + Pop_Stack(&mystack, &v); + //printf("弹出%s \n", val); + Pop_Stack(&mystack, &v); + itoa(c, v,10); + push(&mystack, v); + } + } + } + temp = temp->pNext; + } + else + { + temp = temp->pNext; + } + } + return (int)atof(mystack.top->date); +} +int cal(int a, int b, char x)//进行计算 +{ + switch (x) + { + case '+': + return a + b; + break; + case '-': + return b - a; + break; + case '*': + return a*b; + break; + case '/': + return b/a; + break; + } +} +//销毁栈 +status destory(stack *s) +{ + ElemType *p; + while (s->top != s->base) + { + p = s->top; + s->top = s->top->pNext; + free(p); + } + free(s->base); + return 1; +} diff --git "a/2017-1/Xhl_blabla/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Source.h" "b/2017-1/Xhl_blabla/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Source.h" new file mode 100644 index 00000000..9d733088 --- /dev/null +++ "b/2017-1/Xhl_blabla/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Source.h" @@ -0,0 +1,38 @@ +//必须得把顺序栈改成链式栈吗 +//只能处理有一个括号的运算式 而且是数字要求全是 int 整数 +//只能进行大于零的计算 因为没有设数据存数字位 +#include +#include +#include +#define SUANSHUSHI_NUM 20//输入的式子所在的数组的长度 +#define DATE_NUM 10 //栈的字符串元素的长度 +#define STACR_INIT_SIZE 100 +#define STACR_ADD_SIZE 10 +#define WEIDU_NUM 20; +typedef struct ElemType +{ + char *date; + struct ElemType *pNext; +}ElemType; +typedef int status;//整数的别名 +typedef struct +{ + ElemType *base; + ElemType *top; +}stack;//用于存储运算符和操作数的栈 +status initstack(stack *s);//初始化栈 +status destory(stack *s);//销毁栈 +status isStackempty(stack s);//检验栈是否为空 是返回0 否返回1 +status judgenumber(char x);//用于判断是数字还是操作符 运算符返回 1 界限符返回-1 过界返回-10 数字返回0 +status rank_list(stack*, char x[]);//用于后缀式的排序 +status cal_list(stack);//用于后缀式初次排序 排数字和运算符 +char** conversion(char x[]);//用于字符串的拆分 +//char** rank_num(char **);//对传进来的二维数组进行排序 +status judge_pre(char);//对字符的优先级进行比较 优先级越大数字越大 +status judge_pre2(char);//此时运算符和数字的运算符优先级一样大 +status push(stack *s, char *e); +status initstack(stack* s);//创建一个空栈,里面没有任何有效数据; +status Pop_Stack(stack *S, char ** val); +void Traverse_Stack(stack s);//打印栈 +status rank_list_more(stack *s);//后缀式的二次排序 只排运算符 +int cal(int a, int b, char x);//计算 diff --git "a/2017-1/Xhl_blabla/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\345\220\216\347\274\200\345\274\217.png" "b/2017-1/Xhl_blabla/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\345\220\216\347\274\200\345\274\217.png" new file mode 100644 index 00000000..3551494d Binary files /dev/null and "b/2017-1/Xhl_blabla/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\345\220\216\347\274\200\345\274\217.png" differ diff --git "a/2017-1/Xhl_blabla/\345\223\210\345\270\214\350\241\250/QQ\345\233\276\347\211\20720170614151924.png" "b/2017-1/Xhl_blabla/\345\223\210\345\270\214\350\241\250/QQ\345\233\276\347\211\20720170614151924.png" new file mode 100644 index 00000000..7ad7f8b2 Binary files /dev/null and "b/2017-1/Xhl_blabla/\345\223\210\345\270\214\350\241\250/QQ\345\233\276\347\211\20720170614151924.png" differ diff --git "a/2017-1/Xhl_blabla/\345\223\210\345\270\214\350\241\250/ded.c" "b/2017-1/Xhl_blabla/\345\223\210\345\270\214\350\241\250/ded.c" new file mode 100644 index 00000000..a2cefde0 --- /dev/null +++ "b/2017-1/Xhl_blabla/\345\223\210\345\270\214\350\241\250/ded.c" @@ -0,0 +1,215 @@ +#include "ded.h" +//制造数组 +status make(int *q,int l) +{ + int temp; + int length; + int i; + length = l; + for (i = 0; i elem = (ElemType*)malloc(m * sizeof(ElemType)); + if (!(*H).elem) + exit(0);//return 0; // 存储分配失败 + for (i = 0; i < (*H).sizeindex; i++) + { + (*H).elem[i].key =-1; // 未填记录的标志 + (*H).elem[i].val = 0; + } + hashsize_count++; + return 1; +} +//进行插入操作 +status InsertHash(HashTable *h, KeyType e, ValueType v) +{ + int c = 0;//用于计数冲突次数 + int p;//用于返回插入的位置 + if (SearchHash(*h, e, v, &p, &c)) + { + return DUPLICATE;//如果已有该元素 返回 + } + else if (c < h->sizeindex / 2)//冲突次数达到则进行扩表 c的值可调 + { + (*h).elem[p].val = v; + (*h).elem[p].key = e; + ++(*h).count; + return SUCCESS;//插入成功 + } + else + {//如果哈希表过于小 + printf("哈希表分配过小 重建表\n"); + RecreateHashTable(h); + return UNSUCCESS; + } +} +status SearchHash(HashTable h, KeyType e, ValueType v, int* p, int *c) +{//如果查找成功 p为所在的下标 否则p为待插入的位置 + *p = hash_(e, h.sizeindex);//返回下标 + while (h.elem[*p].key != -1 && v != h.elem[*p].val) + { + (*c)++; + if ((*c) < h.sizeindex) + {//如果冲突次数还能继续处理 + collision(p, *c,e,h.sizeindex); + } + else + { + break; + } + } + if (v == h.elem[*p].val) + return SUCCESS; + else + return UNSUCCESS; +} +int hash_(KeyType e,int l) +{//除留余数法构造简单的哈希表 + int i = e%l; + return i; +} +void collision(int *p,int c, KeyType k,int l) +{ + *p = (k + c) % l;//线性探索再散列:di = 1,2,3,...,m-1 +} +//打印哈希表 +void TraverHash(HashTable h) +{ + for (int i = 0; i < h.sizeindex; i++) + { + printf("{[%d] : ",i); + printf("%d->%d} ",h.elem[i].key, h.elem[i].val); + } + printf("\n"); +} +//重建哈希表 +status RecreateHashTable(HashTable *H) +{ + int i, count = (*H).sizeindex;//H中原有记录个数 + ElemType *p, *elem = (ElemType *)malloc(count * sizeof(ElemType));//动态生成存放哈希表H原有数据的空间 + p = elem; + for (i = 0; i < (*H).sizeindex; ++i) + {//将原有的所有记录,保存到elem中 + //*p++ = (*H).elem[i];//将记录依次存入elem + p[i].key = (*H).elem[i].key; + p[i].val = (*H).elem[i].val; + } + (*H).count = 0;//将原有记录数置为0,为下面调用InserHash做准备 + (*H).sizeindex = hashsize[hashsize_count]; + (*H).elem = (ElemType *)realloc((*H).elem, (*H).sizeindex * sizeof(ElemType));//以新的存储容量重新生成空哈希表H + for (i = 0; i <(*H).sizeindex; ++i) + {//初始化新的哈希表 + (*H).elem[i].key = -1;//未填记录 + (*H).elem[i].val = 0; + } + for (i=0;i +#include +#include +#include +#define init_num 9; +#define SUCCESS 1 +#define UNSUCCESS 0 +#define DUPLICATE -1 +#define init_num 9 +#define search_num 6 +typedef int status; +typedef int KeyType; +typedef int ValueType; +typedef struct _ElemType { + KeyType key; // 关键字 + ValueType val; // 值 +#ifdef CHAINED_HASH + struct _ElemType *next; +#endif +} ElemType; +//难道我要用单链表存储吗 +typedef struct +{ + ElemType *elem; + int count;//当前元素个数 + int sizeindex;//哈希表表长 +}HashTable; +static int m;//内部链接静态变量,m为哈希表表长 +static int hashsize[] = { 11,19,29,37,49 };//内部链接静态变量,哈希表容量(表长m)递增表,一个合适的素数序列。 +int hashsize_count = 0; +int hash_(KeyType e, int l);//进行寻址 +void collision(int *p, int c, KeyType k, int l);//下一个搜寻的位置确定 +status RecreateHashTable(HashTable *H);//重建哈希表 +status InsertHash(HashTable *H, KeyType e, ValueType v);//进行插入操作 +status SearchHash(HashTable H, KeyType e, ValueType v, int* p, int *c);//进行查找操作 +void TraverHash(HashTable H); +status InitHashTable(HashTable *H); +void travel_(int *q, int l); \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\345\271\277\345\272\246\344\274\230\345\205\210\346\263\225\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/QQ\345\233\276\347\211\20720170513161332.png" "b/2017-1/Xhl_blabla/\345\271\277\345\272\246\344\274\230\345\205\210\346\263\225\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/QQ\345\233\276\347\211\20720170513161332.png" new file mode 100644 index 00000000..add5cf25 Binary files /dev/null and "b/2017-1/Xhl_blabla/\345\271\277\345\272\246\344\274\230\345\205\210\346\263\225\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/QQ\345\233\276\347\211\20720170513161332.png" differ diff --git "a/2017-1/Xhl_blabla/\345\271\277\345\272\246\344\274\230\345\205\210\346\263\225\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/psh.c" "b/2017-1/Xhl_blabla/\345\271\277\345\272\246\344\274\230\345\205\210\346\263\225\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/psh.c" new file mode 100644 index 00000000..a7d8b616 --- /dev/null +++ "b/2017-1/Xhl_blabla/\345\271\277\345\272\246\344\274\230\345\205\210\346\263\225\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/psh.c" @@ -0,0 +1,136 @@ +锘#include "psh.h" +void print(int front) +{ + int k = front, j; + do + { + j = k; + k = queue[k].pre_num;//寰幆瀵绘壘涓婁竴涓偣 + queue[j].pre_num = -1;//璁块棶杩囧悗鏍囪 + } while (k != 0); + k = 0; + while (kad[i] = NULL;//鍒濆鍖栫粨鏋勪綋鎸囬拡 + } + int c = 0; + node *p; + int i; + int j; + for (i = 0; inumber = j; + p->next = (*g)->ad[i]; + (*g)->ad[i] = p;//澶存彃娉曟瀯寤洪偦鎺ョ煩闃 + } + } + } +} +void shortest_path(Graph *g, int v, int u, int *visited) +{ + node *p; + int m; + int find = 0; + rear++; + int nn; + for (nn = 0; nn < MAXSIZE; nn++)//鍒濆鍖栭槦鍒 + { + queue[nn].vertex = 0; + queue[nn].pre_num = 0; + } + queue[rear].vertex = v;//璧风偣 + queue[rear].pre_num = -1;//鏍囪璇ョ偣 + visited[v] = 1;//鎶婅鐐硅涓哄凡缁忚闂繃 + while (front != rear && !find) + { + front++; + m = queue[front].vertex;//鎼滅储鐨勮捣鐐逛粠闃熷垪渚濇鎼滅储 + if (m == u)//濡傛灉鎵惧埌浜嗙粓鐐 + { + find = 1; + print(front); + return; + } + p = g->ad[m];//璧风偣鍚庨潰璺熺殑鏁版嵁 + while (p != NULL) + {//骞垮害浼樺厛閬嶅巻 + if (visited[p->number] == 0)//濡傛灉娌℃湁璁块棶杩 + { + visited[p->number] = 1; + rear++; + queue[rear].vertex = p->number; + queue[rear].pre_num = front; + } + p = p->next; + } + } +} +int main() +{//鐢ㄤ簩缁存暟缁勫瓨鍌ㄧ敤渚嬮偦鎺ョ煩闃电殑淇℃伅 + int graph_array[][MAXSIZE] = { + { 1,1,1 ,1,0,0, 1,0,0 }, + { 1,1,1 ,0,0,0, 0,0,0 }, + { 1,1,1, 0,0,0, 0,0,0 }, + { 1,0,0, 1,1,1, 0,0,0 }, + { 0,0,0, 1,1,1, 0,0,0 }, + { 0,0,0, 1,1,1, 0,1,0 }, + { 1,0,0, 0,0,0, 1,1,1 }, + { 0,0,0 ,0,0,1, 1,1,1 }, + { 0,0,0 ,0,0,0, 1,1,1 } + }; + Graph *g; + CreateGraph(&g, graph_array, MAXNUM);//鍒濆鍖栦复鎺ョ煩闃 + int i, j; + for (i = 0; i < 9; i++) + { + printf("\n"); + for (j = 0; j < 9; j++) + { + if (i == j) + { + } + else + { + front = -1, rear = -1; + int visited[MAXSIZE] = { 0 };//鍒濆鍖 visit + printf("%d<->%d ", i + 1, j + 1); + shortest_path(g, i, j, visited); + printf("\n"); + } + } + } + //閲婃斁鍫嗕笂鐨勫唴瀛 + for (i = 0; i < MAXNUM; i++) + { + node *p; + node *q; + p = g->ad[i]; + while (p != NULL) + { + q = p; + p = p->next; + free(q); + } + } + return 0; +} + + + diff --git "a/2017-1/Xhl_blabla/\345\271\277\345\272\246\344\274\230\345\205\210\346\263\225\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/psh.h" "b/2017-1/Xhl_blabla/\345\271\277\345\272\246\344\274\230\345\205\210\346\263\225\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/psh.h" new file mode 100644 index 00000000..e1234c9d --- /dev/null +++ "b/2017-1/Xhl_blabla/\345\271\277\345\272\246\344\274\230\345\205\210\346\263\225\346\261\202\346\234\200\347\237\255\350\267\257\345\276\204/psh.h" @@ -0,0 +1,22 @@ +#include +#include +#define MAXSIZE 100//最大存储 +#define MAXNUM 10//已知图点数 +typedef struct node +{ + int number;//存储顶点 + struct node *next;//指针 +}node, *vertexnode;//基本图元素 +typedef struct +{ + vertexnode ad[MAXSIZE];//静态分配数组存储信息 +}Graph;//图的构成 +struct queue//设置队列,用于存储可通路径顶点的信息 +{ + int vertex;//顶点 + int pre_num;//第几层 +}queue[MAXSIZE]; +int front, rear; +void print(int front);//用于打印队列数组 +void CreateGraph(Graph **g, int array[][MAXSIZE], int k);//头插法初始化邻接矩阵 +void shortest_path(Graph *g, int v, int u, int *visited);//求最短路径 \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\346\216\222\345\272\217/QQ\345\233\276\347\211\20720170603100252.png" "b/2017-1/Xhl_blabla/\346\216\222\345\272\217/QQ\345\233\276\347\211\20720170603100252.png" new file mode 100644 index 00000000..6f70ff49 Binary files /dev/null and "b/2017-1/Xhl_blabla/\346\216\222\345\272\217/QQ\345\233\276\347\211\20720170603100252.png" differ diff --git "a/2017-1/Xhl_blabla/\346\216\222\345\272\217/sw.c" "b/2017-1/Xhl_blabla/\346\216\222\345\272\217/sw.c" new file mode 100644 index 00000000..e71b267b --- /dev/null +++ "b/2017-1/Xhl_blabla/\346\216\222\345\272\217/sw.c" @@ -0,0 +1,242 @@ +#include "sw.h" +//复制数组 +void copy(int *a, int *b,int length) +{ + int i; + for (i = 0; i < length; i++) + { + a[i] = b[i]; + } +} +//打印数组 + void print_(int * q,int length,int num1,int num2) + { + int i; + if (num1 != 0 && num2 != 0) + { + printf("\n总比较次数 %d\n", num1); + printf("总移动记录次数 %d\n", num2); + printf("二者次数之和为 %d\n", num1 + num2); + printf("\n打印排序完成的数组\n"); + } + for (i = 0; i < length; i++) + { + printf("%d ", q[i]); + } + printf("\n"); + } + //制造数组 +int make(int *q) +{ + int temp; + int length; + int i; + do + { + temp = rand() % 20 + 1; + } while (temp <= 0); + length = temp; + for (i = 0; i q[i - 1])//如果后加加 因为num1 初值为0 第一个排序会跳过 + { + temp = q[i]; + for (j = i - 1; (++num1) && temp > q[j] && j >=0; j--) + { + q[j+1] = q[j];//从后面开始挪 很好 + num2++; + } + q[j + 1] = temp; + } + } + printf("\n"); + printf("直接插入排序\n\n"); + print_(q, length, num1, num2); + printf("\n\n"); +} +//希尔排序 +void ShellInsert(int *q, int dk, int *num1, int *num2,int length) +{//对顺序表进行一趟希尔 位置增量变成 dk 而不是1 + //q[0] 只是暂存单元 不是哨兵 当j<=0 时 插入位置已经找到 + int i, j; + int temp; + for (i = dk; i < length; i++) + { + if ((++(*num1)) && q[i] >q[i - dk]) + { + temp =q[i]; + (*num1)++; + for (j = i - dk; (++(*num1))&&temp > q[j]&&j>=0; j -= dk) + { + q[j + dk] = q[j]; + (*num2)++; + } + q[j + dk] = temp; + } + } +} +void ShellSort(int *q,int length) +{//按增量对顺序表进行希尔排序 + int num1 = 0, num2 = 0; + int k; + int t = (length - 1) / 2; + for (t = length / 2; t>0; t = t / 2) + { + ShellInsert(q, t, &num1, &num2,length); + } + printf("\n"); + printf("希尔排序\n\n"); + print_(q, length, num1, num2); + printf("\n\n"); +} +//end希尔排序 +//快速排序 +int Partition(int *q, int length, int low, int high,int *num1,int *num2) +{ + int p;//枢纽值 + p = q[low]; + while (low < high) + { + while (++(*num1)&&low < high&&q[high] <= p) + { + --high; + (*num2)++; + } + q[low] = q[high]; + while (++(*num1) &&low < high&&q[low] >= p) + { + ++low; + (*num2)++; + } + q[high] = q[low]; + } + q[low] = p; + return low;//相当于根节点 +} +void QSort(int *q, int length, int low, int high,int *num1,int *num2) +{ + int p; + if (low < high) + { + p = Partition(q, length, low, high,num1,num2);//一分为二 对左右分别进行排序 + QSort(q, length, low, p - 1,num1,num2);//对低子表进行排序 + QSort(q, length, p + 1, high, num1, num2);//对高子表进行排序 + } +} +void QuickSort(int*q, int length) +{ + int num1=0, num2=0; + QSort(q, length, 0, length-1,&num1,&num2); + printf("\n"); + printf("快速排序\n\n"); + print_(q, length, num1, num2); + printf("\n\n"); +} +//end快速排序 +int change(int *q, int length, int low, int high) +{ + int p;// + p = q[low];//枢轴值 + while (low < high) + { + while (low < high&&q[high] <= p) + { + high--; + } + q[low] = q[high]; + while (low < high&&q[low] >= p) + { + low++; + } + q[high] = q[low]; + } + q[low] =p; +} +//简单选择排序 +void SelectSort(int *q, int length) +{ + int i, j; + int num1 = 0, num2 = 0; + int temp; + for (i = 0; i +#include +#include +#define ListNum 25 +void copy(int *a, int *b, int length); +void print_(int * q, int length, int num1, int num2); +int make(int *q); +void Sort(int *q, int length); +void InsertSort(int *q, int length); +void ShellInsert(int *q, int dk, int *num1, int *num2, int length); +void ShellSort(int *q, int length); +int Partition(int *q, int length, int low, int high, int *num1, int *num2); +void QSort(int *q, int length, int low, int high, int *num1, int *num2); +void QuickSort(int*q, int length); +int change(int *q, int length, int low, int high); +void SelectSort(int *q, int length); \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" "b/2017-1/Xhl_blabla/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" new file mode 100644 index 00000000..e9730024 --- /dev/null +++ "b/2017-1/Xhl_blabla/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" @@ -0,0 +1,6 @@ +8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30 +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30 +7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30 diff --git "a/2017-1/Xhl_blabla/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/s.c" "b/2017-1/Xhl_blabla/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/s.c" new file mode 100644 index 00000000..742d2a41 --- /dev/null +++ "b/2017-1/Xhl_blabla/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/s.c" @@ -0,0 +1,192 @@ +#include "s.h" +//查找结点 +Status SearchBST(BiTree T, ElemType kval, BiTree f, BiTree *p) +{ + if (!T) + { // 空树,查找不成功 + (*p) = f; //f的作用是初始化 p的吗 + return FALSE; + } + else if (kval == T->data) + { + (*p) = T; + return TRUE; // 查找成功 + } + else if (kval < T->data) //如果该数较小的话 + { // 在左子树中继续查找 + return SearchBST(T->lchild, kval, T, p); + } + else + { // 在右子树中继续查找 + return SearchBST(T->rchild, kval, T, p); + } +} +//插入元素 +Status InsertBST(BiTree *T, ElemType e) +{ + // 当二叉排序树中不存在关键字等于 e.key 的 + // 数据元素时,插入元素值为 e 的结点,并返 + // 回 TRUE; 否则,不进行插入并返回FALSE + BiTree *p = (BiTree *)malloc(sizeof(BiTree)); + BiTree s; + if (!SearchBST((*T), e, NULL, p)) + { + _CRT_JIT_INTRINSIC s = (BiTNode*)malloc(sizeof(BiTNode)); + s->data = e; + s->lchild = s->rchild = NULL; + if (!(*p)) + { + (*T) = s; // 插入 s 为新的根结点 + } + else if (e < (*p)->data) + { + (*p)->lchild = s; // 插入 *s 为 *p 的左孩子 + } + else + { + (*p)->rchild = s; // 插入 *s 为 *p 的右孩子 + } + return TRUE; + } + else { + return FALSE; + } +} +//删除结点 +Status DeleteBST(BiTree *T, ElemType kval) +{ + + if (!(*T)) + { // 不存在关键字等于kval的数据元素 + return FALSE; + } + else + { + if (kval == (*T)->data) + { // 找到关键字等于key的数据元素 + Delete(T); + return TRUE; + } + else if (kval < (*T)->data) + { // 继续在左子树中进行查找 + return DeleteBST(&((*T)->lchild), kval); + } + else + { // 继续在右子树中进行查找 + return DeleteBST(&((*T)->rchild), kval); + } + } +} + +int Delete(BiTree *p) +{ + // 从二叉排序树中删除结点 p, + // 并重接它的左子树或右子树 + BiTree q; + BiTree s; + + if (!(*p)->rchild) + { // 右子树为空树则只需重接它的左子树 + q = (*p); + (*p) = (*p)->lchild; + free(q); + } + else if (!(*p)->lchild) + { // 左子树为空树则只需重接它的右子树 + q = (*p); + (*p) = (*p)->rchild; + free(q); + } + else + { // 左右子树均不空 + q = (*p); + s = (*p)->lchild; + while (s->rchild) { // s 指向被删结点的前驱 + q = s; + s = s->rchild; + } + (*p)->data = s->data; + if (q != (*p)) + { + q->rchild = s->lchild; // 重接*q的右子树 + } + else + { + q->lchild = s->lchild; // 重接*q的左子树 + } + free(s); + } + return 1; +} + +//先序遍历 +Status InOrder(BiTree T) +{ + if (T) + { + out[k]=T->data ;//每次输出结点的值 + k++; + InOrder(T->lchild); + InOrder(T->rchild); + } + else + return 1; +} +//用于打印特定形式的输出 +Status print(BiTree T, FILE *fp) +{ + char x[4] = ", "; + int i; + for (i = 0; i < num; i++) + { + out[i] = -1; + } + InOrder(T); + for (i = 0;; i++) + { + if (out[i] == -1) + { + break; + } + if (i != 0) + { + //printf(", "); + fputs(x, fp); + } + //printf("%d", out[i]); + fprintf(fp, "%d", out[i]); + } + return 0; +} +int main() +{ + FILE *fp; + char z[4] = "\n"; + fp = fopen("BSTOutput.txt", "a"); + int number[num_length] = { 8, 10,14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + int search[s_length] = { 13, 8, 5, 20, 6 }; + BiTree T = NULL; + int i; + for (i = 0; i < 12; i++) + { + InsertBST(&T, number[i]); + T->data; + } + print(T , fp); + fputs(z, fp); + //printf("\n"); + for (i = 0; i < 5; i++) + {//如果插入失败 就进行删除操作 + k = 0;//初始化计数的数字 + if (!InsertBST(&T, search[i])) + { + DeleteBST(&T, search[i]); + } + print(T, fp); + //printf("\n"); + fputs(z, fp); + } + + fclose(fp); + return 0; +} \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/s.h" "b/2017-1/Xhl_blabla/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/s.h" new file mode 100644 index 00000000..167f4aed --- /dev/null +++ "b/2017-1/Xhl_blabla/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/s.h" @@ -0,0 +1,23 @@ +#include +#include +#include +#define num_length 20 +#define s_length 10 +#define num 25 +typedef int ElemType; +typedef int Status; +typedef enum { FALSE, TRUE } bool;//自定义布尔 返回状态 +typedef struct BiTNode +{ + ElemType data;//数据域 + struct BiTNode *lchild, *rchild; +} BiTNode, *BiTree; +//查找结点 +Status SearchBST(BiTree T, ElemType kval, BiTree f, BiTree *p); +Status InsertBST(BiTree *T, ElemType e); +Status DeleteBST(BiTree *T, ElemType kval); +int Delete(BiTree *p); +int out[num];//用于存储结点数据域的数组 +int k=0;//用于给数组计数 +Status InOrder(BiTree T); +Status print(BiTree T, FILE *fp); \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\347\254\25413\351\242\230/13.c" "b/2017-1/Xhl_blabla/\347\254\25413\351\242\230/13.c" new file mode 100644 index 00000000..246e2373 --- /dev/null +++ "b/2017-1/Xhl_blabla/\347\254\25413\351\242\230/13.c" @@ -0,0 +1,123 @@ +#include "13.h" +int main() +{ + Lnode *p = ListCreat();//制造要初始化的线性链表 使用了尾插法进行插入 + print_list(p); + Lnode *p1; + Lnode *p2; + ApartList(p, &p1, &p2);//将链表进行拆分 只需要进行一次拆分 节省了循环的时间 + printf("拆分后的第一个链表\n"); + print_circle_list(p1);//输出第一个循环的数组 + printf("拆分后的第二个链表\n"); + print_circle_list(p2);//输出第二个循环的数组 + destory(&p1);//销毁第一个循环数组 + destory(&p2);//销毁第二个循环的数组 + return 0; +} +//初始化一个链表 尾插法 +Lnode * ListCreat() +{ + int i; + int num; + printf("请输入初始的线性链表的长度\n"); + scanf_s("%d", &num); + Lnode *L; + L = (Lnode *)malloc(sizeof(Lnode)); //申请头结点空间 + L->data = 1; + L->next = NULL; //初始化一个空链表 + Lnode *r; + r = L; //r始终指向终端结点,开始时指向头结点 + for (i = 2; i <= num; i++) + { + Lnode *p; + p = (Lnode *)malloc(sizeof(Lnode)); + p->data = i; //结点数据域赋值 + r->next = p; + r = p; + } + r->next = NULL; + return L; +} +//将线性链表整个拆开 拆成两个循环链表 +int ApartList(Lnode *t,Lnode **px, Lnode **py) +{//尾插法 + Lnode *p1; + Lnode *p2; + Lnode *temp1; + Lnode *temp2; + Lnode *temp; + Lnode *p; + p = t; + p1 = (Lnode *)malloc(sizeof(Lnode)); //申请头结点空间 + p1->next = NULL; + p2 = (Lnode *)malloc(sizeof(Lnode)); //申请头结点空间 + temp1 = p1; + temp2 = p2; + p2->next = NULL; + while (NULL != p) + { + temp = p; + p = p->next; + if (temp->data % 2 != 0)//如果是奇数 + { + temp->next= temp1->next;//temp1移动用于最后首尾相连 + temp1->next = temp; + temp1 = temp; + } + else + { + temp->next = temp2->next;//temp2移动 + temp2->next = temp; + temp2 = temp; + } + }//当拆分完成的时候 + p1->data = (temp1->data + 1) / 2; + p2->data = temp2->data / 2; + temp1->next = p1; + temp2->next = p2; + *px = p1; + *py = p2; + return 0; +} +//打印初始单链表 +int print_list(Lnode *p) +{ + printf("初始的链表为:\n"); + do + { + printf("%d ", p->data); + p = p->next; + }while (p!= NULL); + printf("\n"); + return 1; +} +//打印循环单链表 +int print_circle_list(Lnode *p) +{ + Lnode *t; + printf("链表长度为 %d \n", p->data); + t = p; + p = p->next; + printf("元素为: "); + do + { + printf("%d ", p->data); + p = p->next; + } while (p != t); + printf("\n\n"); + return 1; +} +//销毁链表 +int destory(Lnode **p) +{ + Lnode *t; + int i; + int k = (*p)->data+1; + for (i = 0; i < k; i++) + { + t = *p; + (*p) = (*p)->next; + free(t); + } + return 1; +} \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\347\254\25413\351\242\230/13.h" "b/2017-1/Xhl_blabla/\347\254\25413\351\242\230/13.h" new file mode 100644 index 00000000..bae9acd6 --- /dev/null +++ "b/2017-1/Xhl_blabla/\347\254\25413\351\242\230/13.h" @@ -0,0 +1,12 @@ +#include //头插法 没有指针的移动 +typedef struct Lnode +{ + int data; + struct Lnode* next; +}Lnode; +//初始化一个链表 尾插法 +Lnode * ListCreat(); +int ApartList(Lnode *p, Lnode **px, Lnode **py); +int print_list(Lnode *p); +int print_circle_list(p1); +int destory(Lnode **p); \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\347\254\25413\351\242\230/13.png" "b/2017-1/Xhl_blabla/\347\254\25413\351\242\230/13.png" new file mode 100644 index 00000000..883f94ad Binary files /dev/null and "b/2017-1/Xhl_blabla/\347\254\25413\351\242\230/13.png" differ diff --git "a/2017-1/Xhl_blabla/\350\277\267\345\256\253\346\261\202\350\247\243/migong.c" "b/2017-1/Xhl_blabla/\350\277\267\345\256\253\346\261\202\350\247\243/migong.c" new file mode 100644 index 00000000..756cd215 --- /dev/null +++ "b/2017-1/Xhl_blabla/\350\277\267\345\256\253\346\261\202\350\247\243/migong.c" @@ -0,0 +1,362 @@ +#include "migong.h" +int main() +{ + //初始化迷宫 + srand((unsigned)time(NULL)); //用时间做种,每次产生随机数不一样 + int i, j; + Initmingong();//初始化迷宫 + Printmigong();//打印迷宫 + position end;//迷宫的出口 + end.x = MazeScale - 2; + end.y = MazeScale - 2; + int flag = 0;//用于判断迷宫是否有解 + //迷宫主体 + Mystack path;//记录路径的栈 + Initstack(&path);//初始化路径 ,初始化栈长? + position curp;//正在走的通道块坐标 + curp.x = 1; //先将其初始化为入口 + curp.y = 1; + int curstep = 1;//探索的步数 + do + { + if (Pass(curp))//当前位置可以通过,即未曾走到过 + { + Print_curp(curp); + Footprint(curp, curstep);//留下足迹 + SElement e; + //初始化e + e.di = 1;//设定探索方向 + e.ord = curstep; + e.seat.x = curp.x; + e.seat.y = curp.y; + Push(&path, e);//将路径入栈 + printf("将(%d %d)入栈\n\n", curp.x, curp.y); + if (curp.x == end.x&&curp.y == end.y)//如果到达终点 就退出 + { + flag = 1; + break; + } + //找下一个被试块 + curp = Nextposition(curp, 1);//找到前一个被试块东面的路径块作为被试块,优先试最东边的块 + Print_direct(e,curp); + curstep++;//探步数增加 + } + //用于当某点不能通过时,进行对某点的出栈并且尝试下一方向 + else //当前被试路径不能通过 + { + Print_uncurp(curp); + if (isStackempty(&path)) + { + SElement e; + Pop(&path, &e);//退回上一步 + Print_direct_result(e); + curstep--; + //将这一段所有周围被测试过的路从栈中清除 + while (e.di == 4 && isStackempty(&path)) + { + Markprint(e.seat); + printf("四个方向均不可走 弹出(%d %d) ", e.seat.x, e.seat.y); + Pop(&path, &e); + printf("退回 (%d %d)\n\n", e.seat.x, e.seat.y); + curstep--; + } + //如果当前栈顶还有未被测试的路径就测试剩余的周围路径 + if (e.di < 4) + { + curp = Nextposition(e.seat, e.di + 1); + e.di++; + curstep++; + Print_direct( e,curp); + Push(&path, e); + } + } + } + } while (isStackempty(&path)); + printf("\n"); + Print_result(flag,path); + clear(&path); + return 0; +} +//打印可以通过的步子 +void Print_curp(position a) +{ + printf("(%d %d) 可以通过\n",a.x,a.y); +} +//打印不能通过的步子 +void Print_uncurp(position a) +{ + printf("(%d %d) 不能通过\n", a.x, a.y); +} +//初始化迷宫的墙和路 +void Initmingong() +{ + printf("请选择初始化迷宫的方式:\n \n1 随机初始化 \n2 用矩阵初始化 \n请输入"); + int x; + int i, j; + scanf("%d", &x); + if (x == 1) + { + for (i = 0; i < MazeScale; i++) + { + for (j = 0; j < MazeScale; j++) + { + Maze[i][j] = rand() % 2;//0为可走通路 ,1为墙元素 + } + } + //初始化墙壁 + for (i = 0; i < MazeScale; i++) + { + Maze[0][i] = 1; + Maze[MazeScale - 1][i] = 1; + } + for (i = 0; i < MazeScale; i++) + { + Maze[i][0] = 1; + Maze[i][MazeScale - 1] = 1; + } + } + else if(x==2) + { + int num[6][6] = { {1,1,1, 1,1,1},{1,0,0, 0,1,1},{1,0,0,1,0,1},{1,0,0, 0,1,1},{1,1,0, 0,0,1},{1,1,1, 1,1,1} }; + int y; + for (i = 0; i < MazeScale; i++) + { + for (j = 0; j < MazeScale; j++) + { + Maze[i][j] = num[i][j];//0为可走通路 ,1为墙元素 + } + } + } + //设(1,1)为入口,(MazeScale-2,MazeScale-2)为出口 + Maze[1][1] = 0; + Maze[MazeScale - 2][MazeScale - 2] = 0; +} +//打印刚被初始化的迷宫 +void Printmigong() +{ + //打印出迷宫 + char x; + int i, j; + printf("图形版\n"); + for (i = 0; i < MazeScale; i++) + { + printf("|"); + for (j = 0; j < MazeScale; j++) + { + x = Maze[i][j] == 0 ? ' ' : '#'; + printf("%c", x); + } + printf("|\n"); + } + printf("-----------------\n"); + printf("数字版(0是路 1是墙)\n"); + for (i = 0; i < MazeScale; i++) + { + printf("|"); + for (j = 0; j < MazeScale; j++) + { + printf("%d ", Maze[i][j]); + } + printf("|\n"); + } +} +//打印出运行的结果; +void Print_result(int flag, Mystack path) +{ +if (flag == 1) + { + printf("迷宫有解\n #为墙 @为所走路径\n"); + int i, j; + char x; + Maze[1][1] = 2; + for (i =0; i< MazeScale; i++) + { + printf("|"); + for (j =0; j< MazeScale; j++) + { + if (Maze[i][j] <= 0) + { + x = ' '; + } + else if (Maze[i][j] == 1) + { + x = '#'; + } + else + { + x = '@'; + } + printf("%c", x); + } + printf("|\n"); + } + } + else + { + printf("迷宫无解\n"); + } +} +//创建一个栈如果创建成功则返回1,否则就返回0 +int Initstack(Mystack * s) +{ + s->base = (SElement*)malloc(STACK_INIT_SIZE * sizeof(SElement));//为栈分配初始空间 + if (!s->base) + return 0; + s->top = s->base;//设定为空栈 + s->stacksize = STACK_INIT_SIZE; + return 1; +} +//检验当前位置是否可以通过 +int Pass(position p) +{ + if (Maze[p.x][p.y] == 0) + { + return 1; + } + else + { + return 0; + } +} +//留下足迹 +void Footprint(position p, int step) +{ + Maze[p.x][p.y] = step; +} +//插入元素为e的元素,成功返回1 失败返回0 +int Push(Mystack* s, SElement e) +{ + if (s->top - s->base >= STACK_INIT_SIZE)//如果栈满了,进行再扩大 + { + s->base = (SElement*)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(SElement)); + if (NULL != s->base) + { + return 0; + } + s->top = s->base + s->stacksize; + s->stacksize += STACKINCREMENT; + } + *(s->top) = e; + s->top++; + return 1; +} +//按顺时针方向从东开始寻找矩阵当中某一个位置的临近位置 +position Nextposition(position now, int direction) +{ + position next; + int x = now.x; + int y = now.y; + switch (direction) + { + case 1: + //东 + { + next.x = x; + next.y = y + 1; + break; + } + //南 + case 2: { + next.x = x + 1; + next.y = y; + break; + } + //西 + case 3: { + next.x = x; + next.y = y - 1; + break; + } + //北 + case 4: + { + next.x = x - 1; + next.y = y; + break; + } + default:break; + } + return next; +} +//打印要走的方向 +void Print_direct(SElement e,position a) +{ + switch (e.di) + { + case 1: + printf("测试向东走 (%d %d)\n",a.x,a.y); + break; + case 2: + printf("测试向南走 (%d %d)\n", a.x, a.y); + break; + case 3: + printf("测试向西走 (%d %d)\n", a.x, a.y); + break; + case 4: + printf("测试向北走 (%d %d)\n", a.x, a.y); + break; + } +} +//打印测试方向失败的结果 +void Print_direct_result(SElement e) +{ + switch (e.di) + { + case 1: + printf("测试向东走失败 "); + printf("退回(%d %d)\n\n", e.seat.x, e.seat.y); + break; + case 2: + printf("测试向南走失败 "); + printf("退回(%d %d)\n\n", e.seat.x, e.seat.y); + break; + case 3: + printf("测试向西走失败 "); + printf("退回(%d %d)\n\n", e.seat.x, e.seat.y); + break; + case 4: + printf("测试向北走失败 "); + printf("退回(%d %d)\n\n", e.seat.x, e.seat.y); + break; + } +} +//判断栈是否为空,如果是空的就返回0,否则就返回1 +int isStackempty(Mystack *s) +{ + //判断栈是否为空,如果是空的就返回0,否则就返回1 + if (s->top == s->base) + { + return 0; + } + else + { + return 1; + } +} +//弹出栈顶元素赋值给e弹出成功返回1,弹出失败返回0 +int Pop(Mystack* s, SElement* e) +{ + if (isStackempty(s)) + { + *e = *(s->top - 1); + s->top--; + return 1; + } + else + { + return 0; + } +} +//路径不可走的话就留下-1的标记 +void Markprint(position p) +{ + Maze[p.x][p.y] = -1; +} +void clear(Mystack *pS) +{ + if (NULL == pS) //如果 + { + return; + } + free(pS->base); +} \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\350\277\267\345\256\253\346\261\202\350\247\243/migong.h" "b/2017-1/Xhl_blabla/\350\277\267\345\256\253\346\261\202\350\247\243/migong.h" new file mode 100644 index 00000000..bf3f815d --- /dev/null +++ "b/2017-1/Xhl_blabla/\350\277\267\345\256\253\346\261\202\350\247\243/migong.h" @@ -0,0 +1,44 @@ +#include +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +#define MazeScale 6 +//点的位置坐标 +typedef struct position +{ + int x; + int y; +}position; +//每走一步进行记录 +typedef struct SElement +{ + int ord;//通道在路径块上的序号 + position seat;//通道块在路径上的坐标 + int di;//从此通道块走向下一块的“方向“ + +}SElement; +typedef struct Mystack +{ + SElement *base; + SElement *top; + int stacksize; +}Mystack; +int Initstack(Mystack *); +int Pass(position); +void Footprint(position, int); +int Push(Mystack*, SElement); +position Nextposition(position, int); +int isStackempty(Mystack*); +int Pop(Mystack *, SElement*); +position Nextposition(position, int); +void Markprint(position p); +int Maze[MazeScale][MazeScale]; +void clear(Mystack*); +void Initmingong(); +void Printmigong(); +void Print_result(int, Mystack); +void Print_direct(SElement e,position);//打印将要走的方向 +void Print_direct_result(SElement e);//打印走的方向的结果 +void Print_curp(position);//打印走的步子 +void Print_uncurp(position);//打印走的步子的结果 \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\350\277\267\345\256\253\346\261\202\350\247\243/migong.png" "b/2017-1/Xhl_blabla/\350\277\267\345\256\253\346\261\202\350\247\243/migong.png" new file mode 100644 index 00000000..cbbfc55c Binary files /dev/null and "b/2017-1/Xhl_blabla/\350\277\267\345\256\253\346\261\202\350\247\243/migong.png" differ diff --git "a/2017-1/Xhl_blabla/\351\223\276\345\274\217\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/3.4.2.c" "b/2017-1/Xhl_blabla/\351\223\276\345\274\217\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/3.4.2.c" new file mode 100644 index 00000000..c89c2655 --- /dev/null +++ "b/2017-1/Xhl_blabla/\351\223\276\345\274\217\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/3.4.2.c" @@ -0,0 +1,152 @@ +#include "3.4.2.h" +int main() +{ + Linkqueue q; + Init(&q); + QElmtype x[NUM] = {'a','b','c','d','e','f','h','i','j','k'}; + QElmtype e; + int i; + for (i = 0; i < NUM; i++) + { + Enqueue(&q, x[i]); + } + printf(" 1.插入完成的队列为\n"); + Queuetraverse(q); + printf("\n 2.队列的长度为 %d\n\n", Queuelength(q)); + printf(" 3.判断队列是否为空并返回队头元素\n\n"); + if (Gethead(q, &e)) + { + printf(" 队列不为空,且栈顶元素为%c\n", e); + } + printf("\n 4.依次删除队列\n\n"); + while (!Queueempty(q)) + { + Dequeue(&q, &e); + printf(" 被删除的元素为%c\n", e); + printf(" 删除后的队列为\n"); + Queuetraverse(q); + } + printf("\n 5.队列的长度为 %d\n\n", Queuelength(q)); + printf(" 6.销毁队列\n"); + Destory(&q); + return 0; +} +//初始化构造一个空队列 成功返回1 否则返回0 +status Init(Linkqueue *q) +{//第一个头元素是不存数据的 + q->rear = (Queueptr)malloc(sizeof(Qnode)); + if (!q->rear) + { + return 0; + } + q->front = q->rear; + q->rear->next = NULL; + return 1; +} +status Destory(Linkqueue *q)//销毁队列q q不在存在 +{ + while (q->front) + { + q->rear = q->front->next; + free(q->front); + q->front = q->rear; + } + return 1; +} +status Clearqueue(Linkqueue *q)//将队列清空 +{ + Queueptr temp = q->front->next; + Queueptr th; + while (temp) + { + th = temp; + temp = temp->next; + free(th); + }//只保留队头队尾 + temp = NULL; + q->front = q->rear = NULL;//头尾指针均为空 但是再用需要初始化 因为已经完全释放了 + return 1; +} +//若队列为空 返回1 否则返回0 +status Queueempty(Linkqueue q) +{ + if (q.front == q.rear) + { + return 1; + } + return 0; +} +//返回队列的长度 不算队头的长度 +status Queuelength(Linkqueue q) +{ + q.front = q.front->next; + int num = 0; + while (q.front) + { + num++; + q.front = q.front->next; + } + return num; +} +//若队列不空 返回e 1 否则返回0 +status Gethead(Linkqueue q, QElmtype *e) +{ + if (q.front == q.rear) + { + return 0; + } + *e = q.front->next->data; + return 1; +} +//插入元素e进q 失败返回0 否则返回1 +status Enqueue(Linkqueue *q, QElmtype e) +{ + Queueptr p = (Queueptr)malloc(sizeof(Qnode)); + if (!p) + { + return 0; + } + p->data = e; + p->next = NULL; + q->rear->next = p;//注意单向链表的方向 + q->rear = p; + return 1; +} +//若队列不空 删除队头元素 并用e返回其值 返回1 否则返回0 +status Dequeue(Linkqueue *q, QElmtype *e) +{ + if (q->front == q->rear) + { + return 0; + } + Queueptr p = q->front->next; + *e = p->data; + q->front->next = p->next; + if (q->rear == p) + { + q->rear = q->front; + } + free(p); + return 1; +} +//从队头到队尾依次调用函数visit() ,一旦visit()失败则操作失败 +status Queuetraverse(Linkqueue q) +{ + q.front = q.front->next; + while (visit(q.front)) + { + q.front = q.front->next; + } + printf("\n"); + return 1; +} +int visit(Queueptr p) +{ + if (NULL != p) + { + char x = p->data; + printf(" %c ", x); + return 1; + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\351\223\276\345\274\217\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/3.4.2.h" "b/2017-1/Xhl_blabla/\351\223\276\345\274\217\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/3.4.2.h" new file mode 100644 index 00000000..ec63349c --- /dev/null +++ "b/2017-1/Xhl_blabla/\351\223\276\345\274\217\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/3.4.2.h" @@ -0,0 +1,27 @@ +//单项队列的基本操作 +//队尾插入 队头删除 +#include +#include +#define NUM 10 +typedef char QElmtype; +typedef int status; +typedef struct Qnode +{ + QElmtype data; + struct Qnode *next; +}Qnode, *Queueptr; +typedef struct +{ + Queueptr front;//队头指针 + Queueptr rear;//队尾指针 +}Linkqueue; +status Init(Linkqueue *q);//初始化构造一个空队列 +status Destory(Linkqueue *q);//销毁队列q q不在存在 +status Clearqueue(Linkqueue *q);//将队列清空 +status Queueempty(Linkqueue q);//若队列为空 返回1 否则返回0 +status Queuelength(Linkqueue q);//返回队列的长度 +status Gethead(Linkqueue q, QElmtype *e);//若队列不空 返回e 1 否则返回0 +status Enqueue(Linkqueue *q, QElmtype e);//插入元素e进q +status Dequeue(Linkqueue *q, QElmtype *e);//若队列不空 删除队头元素 并用e返回其值 返回1 否则返回0 +status Queuetraverse(Linkqueue q);//从队头到队尾依次调用函数visit() ,一旦visit()失败则操作失败 +int visit(Queueptr);//访问并打印每一个队列元素 \ No newline at end of file diff --git "a/2017-1/Xhl_blabla/\351\223\276\345\274\217\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/3.4.2.png" "b/2017-1/Xhl_blabla/\351\223\276\345\274\217\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/3.4.2.png" new file mode 100644 index 00000000..aea13f30 Binary files /dev/null and "b/2017-1/Xhl_blabla/\351\223\276\345\274\217\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234/3.4.2.png" differ diff --git "a/2017-1/byswsz/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.cpp" "b/2017-1/byswsz/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.cpp" new file mode 100644 index 00000000..b68e7393 --- /dev/null +++ "b/2017-1/byswsz/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.cpp" @@ -0,0 +1,143 @@ +#include +#include +#include +#define LIST_numa 4; +#define LIST_numb 4;//对其存储空间进行限定为4 +typedef struct LNode +{ + int data; + struct LNode *next; + +}LNode, *LinkList;//线性表的单链表储存结构。 +//对下面函数进行声明 +void CreatList(LinkList &L, int n); +void MergeList(LinkList&la, LinkList&lb, LinkList&c); +void output(LinkList l); +void assign_value(int a[], int n); + + + +//输入主函数, +int main() +{ + srand(time(0)); + LinkList la, lb, lc; + int a =LIST_numa; + int b = LIST_numb ;//la的长度和lb的长度均为宏常量,在前面已经被限定为四 + CreatList(la, a); + output(la); + CreatList(lb, b); + output(lb); + MergeList(la, lb, lc); + if (lb != NULL) + { + free(lb); + } + + output(lc); + return 0; + +} +//逆序输入n个元素的值,建立代表头节点的单链表 +void CreatList(LinkList &L, int n) +{ + + int i; + int *a = (int*)malloc(sizeof(int)*n); + assign_value(a, n); + LinkList p; + L = (LinkList)malloc(sizeof(LNode)); + L->next = NULL;//先建立一个带头结点的单链表 + for (i = n; i > 0; --i) + { + p = (LinkList)malloc(sizeof(LNode));//生成新节点 + p->data = a[n - i]; //输入元素值 + p->next = L->next; L->next = p; //插入到表头 + } + +} +//已知单链表线性表la和lb的元素按值非递减排列 +//归并La和Lb得到新的单链线性表Lc,Lc的元素也按值非递减排列 +void MergeList(LinkList&la, LinkList&lb, LinkList&lc) +{ + LinkList pa, pb, pc, l = NULL, lc_copy; + pa = la->next; + pb = lb->next; + lc = pc = la; + while (pa&&pb) + { + if (pa->data <= pb->data) + { + pc->next = pa; + pc = pa; + pa = pa->next; + pc->next = NULL; + + }//对其La的进行归并当第一次循环中其中一个为空的时候说明归并一个完毕 + else + { + pc->next = pb; + pc = pb; + pb = pb->next; + pc->next = NULL; + } + + lc_copy = lc->next; + + while (lc_copy != NULL) + { + printf("%d ", lc_copy->data); + lc_copy = lc_copy->next; + } + printf("\n");//第二个归并完 + + + } + pc->next = pa ? pa : pb; + lc_copy = lc->next; + while (lc_copy != NULL) + { + printf("%d ", lc_copy->data); + lc_copy = lc_copy->next; + } + printf("\n"); +} +//产生随机数,并进行排序 +void assign_value(int a[], int n) +{ + int i, j, temp; + //产生随机数 + for (i = 0; i < n; i++) + { + a[i] = rand() % 20 + 1; + } + // 冒泡排序法对随机数排序 + for (i = 0; i < n - 1; i++) + { + for (j = 0; j < n - 1 - i; j++) + { + if (a[j] < a[j + 1]) + { + temp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = temp; + + } + } + } + + +} +//输出链表 +void output(LinkList l) +{ + l = l->next; + printf("\nthe List is:\n"); + + while (l != NULL) + { + printf("%d ", l->data); + l= l->next; + } + printf("\n\n"); +} \ No newline at end of file diff --git "a/2017-1/byswsz/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\2101\357\274\211.cpp" "b/2017-1/byswsz/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\2101\357\274\211.cpp" new file mode 100644 index 00000000..dd59b4b7 --- /dev/null +++ "b/2017-1/byswsz/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\2101\357\274\211.cpp" @@ -0,0 +1,113 @@ +#include +#include +#include +#define STACK_INIT_SIZE 100 //存储空间初始分配量 +#define STACKINCREMENT 10 //存储空间分配增量 + +typedef int SElemType; +//对顺序栈进行定义 +typedef struct Sq_Stack +{ + SElemType *base; + SElemType *top; + int stacksize; + +}SqStack; +typedef enum +{ OK, + ERROR, + OVERFLOW, + +}Status; + + +//Status InitStack(SqStack*S);//构造一个空栈 +////void DestroyStack(SqStack*S);//销毁已经存在的栈 +//Status Push(SqStack*S,SElemType e);//把一个新的元素添加到栈中 +//Status Pop(SqStack*S,SElemType *e);//删除栈顶的元素,并用e返回其值 +//bool StackEmpty(SqStack*S);//判断一个栈是否为空 +////Status GetTop(SqStack S); +//void Conversion(SqStack*S,SElemType input,SElemType mod); + + +//构造一个空栈S +Status InitStack (SqStack* pS) //构造一个空栈 +{ + pS->base=(SElemType *)malloc(STACK_INIT_SIZE * + sizeof(SElemType)); + + if(! pS->base) + { + return OVERFLOW; + } + + pS->top =(*pS).base; + pS->stacksize = STACK_INIT_SIZE; + + return OK; +} + +Status Push(SqStack *S,SElemType e) +{ + if(S->top-S->base>=S->stacksize) + { + S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); + if(!S->base) + return OVERFLOW; + S->top=S->base+S->stacksize; + S->stacksize+=STACKINCREMENT; + } + *S->top++=e; + return OK; + +} +int StackEmpty(SqStack *S) +{ + if (S) + return S->base==S->top; + return 1; +} + +Status Pop(SqStack *S,SElemType *e) +{ + if(StackEmpty(S)) + { + return ERROR; + } + *e=*--S->top; + return OK; + +} + +void conversion (SqStack *S,int n,int d) +{ + SElemType e; + while (n) + { + Push(S, n % d); + n = n / d; + } + while (!StackEmpty(S)) + { + Pop(S, &e); + printf ( "%d", e ); + } + + printf("\n"); +} // conversion +int main() +{ + int n; + int d; + SqStack S; + srand(time(0)); + + n=rand()%1024; + printf("待转换数据 :%d\n",n); + d=rand()%10+1; + printf("转换进制 :%d\n",d); + + InitStack(&S); + conversion(&S,n,d); + return 0; +} \ No newline at end of file diff --git "a/2017-1/byswsz/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\2102\357\274\211.cpp" "b/2017-1/byswsz/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\2102\357\274\211.cpp" new file mode 100644 index 00000000..bd972265 --- /dev/null +++ "b/2017-1/byswsz/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232\357\274\2102\357\274\211.cpp" @@ -0,0 +1,156 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配量 +#define STACKINCREMENT 10 //存储空间分配增量 +#define DEBUG 1 + +typedef enum +{ ERROR, + OVERFLOW, + OK} +Status; + +typedef char SElemType; +//对顺序栈进行定义 +typedef struct +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack (SqStack* pS);//构造一个空栈 +Status Push (SqStack *S , SElemType e); +int StackEmpty (SqStack *S); +Status Pop (SqStack S , int *e); +void Match(SqStack *S , char *p); + + +//构造一个空栈 + +Status InitStack (SqStack* pS) +{ + pS->base=(SElemType *)malloc(STACK_INIT_SIZE * + sizeof(SElemType)); + + if(! pS->base) + { + return OVERFLOW; + } + + pS->top =(*pS).base; + pS->stacksize = STACK_INIT_SIZE; + + return OK; +} +//把一个新的元素添加到栈中 +Status Push(SqStack *S,SElemType e) +{ + if(S->top-S->base>=S->stacksize) + { + S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); + if(!S->base) + return OVERFLOW; + S->top=S->base+S->stacksize; + S->stacksize+=STACKINCREMENT; + } + *S->top++=e; + return OK; + +} + +int StackEmpty(SqStack *S) +{ + if (S) + return S->base==S->top; + return 1; +} +//删除栈顶的元素,并用e返回其值 +Status Pop(SqStack *S,int *e) +{ + if(StackEmpty(S)) + { + return ERROR; + } + *e=*--S->top; + return OK; +} +//自定义函数,对括号进行判定。 +void Match(SqStack *S , char *p) +{ + int i=0; + int bracket; + InitStack(S); //构造一个空栈 + while(p[i] != '\0') + { + DEBUG && printf("当前括号 '%c' ", p[i]); + switch(p[i]) + { + case '(': + DEBUG && printf(" '%c' 入栈 \n", p[i]); + Push(S,p[i]); //入栈时添加一个新的元素 + break; + + case ')': + DEBUG && printf(" 出栈 "); + Pop(S,&bracket);//出栈时删除栈顶元素 + printf("结果是 '%c' \n",bracket); + if (bracket != (p[i] == ')' ? '(' : p[i] == ']' ? '[' : '{')) + { + DEBUG && printf("匹配失败\n"); + return; + }//出栈的元素不与其对应 + + break; + case '[': + DEBUG && printf(" '%c' 入栈 \n", p[i]); + Push(S,p[i]); + break; + + case ']': + DEBUG && printf(" 出栈 "); + Pop(S,&bracket); + printf("结果是 '%c' \n",bracket); + + if (bracket != (p[i] == ')' ? '(' : p[i] == ']' ? '[' : '{')) + { + DEBUG && printf("匹配失败\n"); + return; + } + + break; + default: + break; + } + i++;//循环执行,当配完时 + } //while 循环结束。 + StackEmpty(S);//判断是否循环完 + DEBUG && printf("匹配成功 \n\n");//若debug成功则OK啦啦啦啦啦啦啦啦 + + +} + + +int main() +{ + char str1[10]="([()])"; + char str2[10]="[([][])]"; + char str3[10]="[((]])"; + SqStack S; + + printf("开始匹配第一组 :"); + puts(str1); + Match(&S,str1); + + printf("开始匹配第二组 :"); + puts(str2); + Match(&S,str2); + + printf("开始匹配第三组 :"); + puts(str3); + Match(&S,str3); + return 0; +}//最后想说一句mmp,让我去哭一会儿呜呜呜呜呜呜 + diff --git "a/2017-1/cxhomework/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/list.c" "b/2017-1/cxhomework/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/list.c" new file mode 100644 index 00000000..1124a7c5 --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/list.c" @@ -0,0 +1,96 @@ +//#include +////头文件,标准输入输出 +//#include +////内存分配函数malloc的头文件 +//typedef struct LNode +//{ +// int data;//数据 +// struct LNode *next;//指向下一个节点的指针 +// +//}LNode, *Linklist; +//Linklist CreatList(LNode *L, int n);//创建新的线性表的函数 +//Linklist MergeList(LNode *l1, LNode *L2, LNode *l3);//实现两个链表相结合形成第三个链表 +//void output(LNode *ll, int n); +#include "Source.h"; + + +int main() +{ + Linklist la = NULL, lb = NULL, lc = NULL;//定义三个Linklist变量 + //srand((unsigned)time(NULL)); + int a, b; + a = rand() % 101; + /*scanf("%d", &a);*/ + la = CreatList(la, a);//调用函数创建第一个线性表 + b= rand() % 101; + /*scanf("%d", &b);*/ + lb = CreatList(lb, b); + lc = MergeList(la, lb, lc);//调用函数实现两个线性表相连 + //lc = lc->next; + output(lc, a + b); + return 0; + +} + +Linklist CreatList(LNode *L, int n) +{ + LNode * p = NULL; + L = (Linklist)malloc(sizeof(LNode)); + L->next = NULL; + for (int i = n; i > 0; i--) + { + /* + p->data = rand() % 101;*/ + p = (LNode *)malloc(sizeof(LNode)); + /* scanf("%d", &p->data);*/ + p->data = rand() % 101; + p->next = L->next; + L->next = p; + } + return L; +} + + +Linklist MergeList(LNode *l1, LNode *l2, LNode *l3) +{ + LNode * la = NULL; + LNode * lb = NULL; + LNode * lc = NULL; + la = l1->next; + lb = l2->next; + l3 = lc = l1; + while (la&&lb)//当la和lb都不为空时进行操作 + { + if (la->data <= lb->data) + { + lc->next = la; + lc = la; + la = la->next; + } + else + { + lc->next = lb; + lc = lb; + lb = lb->next; + } + } + lc->next = (la ? la : lb); + + if (l2 != NULL) + free(l2);//将第二个线性表占用的内存释放 + + return l3; +} + +void output(LNode *ll, int n) +{ + int i; + LNode *p = ll->next; + for (i = n; i > 0; i--) + + { + printf("%d", p->data); + p = p->next; + } + +} \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/list.h" "b/2017-1/cxhomework/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/list.h" new file mode 100644 index 00000000..2d1f720d --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/list.h" @@ -0,0 +1,16 @@ +#include +//头文件,标准输入输出 +#include +//内存分配函数malloc的头文件 +//#include//生成随机数需要的头文件 +#includesrand(time(NULL)); + +typedef struct LNode +{ + int data;//数据 + struct LNode *next;//指向下一个节点的指针 + +}LNode, *Linklist; +Linklist CreatList(LNode *L, int n);//创建新的线性表的函数 +Linklist MergeList(LNode *l1, LNode *L2, LNode *l3);//实现两个链表相结合形成第三个链表 +void output(LNode *ll, int n); diff --git "a/2017-1/cxhomework/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/picrture.c" "b/2017-1/cxhomework/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/picrture.c" new file mode 100644 index 00000000..aa1e910a --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/picrture.c" @@ -0,0 +1,146 @@ +#include +#include +#include "picrture.h" + +int FirstAdjVex(Graph G, int i) { + int k; + for (k = 0; k < G.vexnum; k++) { + if (G.arcs[i][k].adj == 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) { + return k; + + } + + } + return -1; + +}//返回下一个邻接顶点,没有的话返回-1 +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->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); + + return OK; + +}//构建已知图 +Status Add(Graph*G, int x, int y) { + //构造邻接矩阵,相邻则赋值为1 + if (x >= MAX_VERTEX_NUM || y >= MAX_VERTEX_NUM) { + return ERROR; + + } + G->arcs[x][y].adj = G->arcs[y][x].adj = 1; + return OK; + +}//为邻接矩阵赋值 +void BFSTraverse(Graph G, int a, int b) { + int u, v, w; + int flag; + LinkQueue Q; //辅助队列 + for (v = 0; v < G.vexnum; ++v) { + visited[v] = False; //初始化访问标志 + + } + InitQueue(&Q); // 置空的辅助队列Q + EnQueue(&Q, a); //起点入列 + visited[a] = True; + flag = 0; + 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 = 1; + break; + + } + if (!visited[w]) { + EnQueue(&Q, w); + visited[w] = 1; + + } + + } + if (flag) { + break; + + } + + } + +}//广度优先遍历 +Status PrintFoot(LinkQueue Q, int start) { + int foot[MAX_VERTEX_NUM]; + int i; + QueuePtr p; + p = Q.rear; + for (i = 0; i < MAX_VERTEX_NUM; i++) { + foot[i] = -1; + + } + foot[0] = p->data;//数据进入数组 + p = p->priou;//沿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); + + } + + } + return OK; + +}//输出路径 + +int main() + { + Graph G; + int i, j; + + CreateGraph(&G); + + for (i = 0; i < 9; i++) { + for (j = 0; j < 9; j++) { + if (j != i) { + printf("%d<->%d:", i + 1, j + 1); + BFSTraverse(G, i, j); + printf("\n"); + + } + + } + + } + + return 0; + } \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/picrture.h" "b/2017-1/cxhomework/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/picrture.h" new file mode 100644 index 00000000..4f4b1014 --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232/picrture.h" @@ -0,0 +1,85 @@ +#define MAXQSIZE 100 +#define INFINITY INT_MAX +#define MAX_VERTEX_NUM 20 + +int visited[MAX_VERTEX_NUM]; + +typedef int VRType; +typedef int ElemType; + +typedef enum { + OK, + ERROR, + OVERFLOW + +}Status; + +typedef enum { + False, + True + +}Bool; + +typedef struct ArcCell { // 弧的定义 + VRType adj; // 用1或0表示相邻否; + +} AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; + + typedef struct { // 图的定义 + AdjMatrix arcs; // 弧的信息 + int vexnum, arcnum; // 顶点数,弧数 + + } Graph; + + typedef struct QNode { + ElemType data; + struct QNode *priou; + struct QNode *next; + + }QNode, DuLinkList, *QueuePtr; + + typedef struct { + QueuePtr front; + QueuePtr rear; + + }LinkQueue; + + //队列的基本操作// + Status InitQueue(LinkQueue *Q) { + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + Q->front->next = Q->rear->next = NULL; + return OK; + }//初始化队列 + Status EnQueue(LinkQueue *Q, ElemType e) { + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + 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) { + Q->front = Q->front->next; + *e = Q->front->data; + return OK; + + }//出列 + Bool QueueEmpty(LinkQueue Q) { + if (Q.front == Q.rear) { + return True; + + } + return False; + + }; //判断是否为空队列 + + //图的基本操作// + int FirstAdjVex(Graph G, int i); //返回第一个邻接顶点,没有的话返回-1 + int NextAdjVex(Graph G, int i, int j); //返回下一个邻接顶点,没有的话返回-1 + Status CreateGraph(Graph *G); //构建已知图 + Status Add(Graph*G, int x, int y); //为邻接矩阵赋值 + void BFSTraverse(Graph G, int a, int b); //广度优先遍历 + Status PrintFoot(LinkQueue Q, int start); //输出路径 \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/Source.h" "b/2017-1/cxhomework/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/Source.h" new file mode 100644 index 00000000..f47a8b9d --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/Source.h" @@ -0,0 +1,188 @@ +#include +#include +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +#define MAXNUM 100 +const char OP[7] = { '+', '-', '*', '/', '(', ')', '#' }; +const int PT[7][7] = { + {1,1,0,0,0,1,1}, + {1,1,0,0,0,1,1}, + {1,1,1,1,0,1,1}, + {1,1,1,1,0,1,1}, + {0,0,0,0,0,0,0}, + {1,1,1,1,0,0,0}, + {0,0,0,0,0,0,0} +}; +typedef char SElemType; +typedef enum { + ERROR, + OK, + OVERFLOW + +}Status; +typedef enum { + FALSE, + TRUE + +}Bool; +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; + +}SqStack; +typedef struct { + int *base; + int *top; + int stacksize; + +}Number; + +//字符栈 +//create a new stack +Status InitStack(SqStack *S) + { + //need free + S->base = (SElemType *)malloc(STACK_INIT_SIZE * + sizeof(SElemType)); + if (!S->base) { + return OVERFLOW;//fail to allocate memory + + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + + return OK; + }//end of InitStack + +Status Push(SqStack *S, SElemType e)// + { + if (S->top - S->base >= S->stacksize) + { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if (!S->base) + { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + }//课本 + *S->top++ = e; + return OK; + }//end of Push + +Status Pop(SqStack *S, SElemType *e) + { + if (S->top == S->base) + { + return ERROR; + } + *e = *--S->top;//待检测 + return OK; + }//end of Pop + +//判断空栈 +Status StackEmpty(SqStack *S) + { + if (S->base == S->top) + { + return OK; + } + else { + return ERROR; + + } + }//end of StackEmpty + +//获取栈顶元素的函数 +Status GetTop(SqStack *S, SElemType *e) + { + if (S->top == S->base) { + return ERROR; + + } + *e = *(S->top - 1); + return OK; + }//end of GetTop + +//数字栈 +Status nInitStack(Number *S) + { + //need free + S->base = (int *)malloc(STACK_INIT_SIZE * + sizeof(int)); + if (!S->base) { + return OVERFLOW;//fail to allocate memory + + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + + return OK; + }//end of InitStack + +Status nPush(Number *S, int e)// + { + if (S->top - S->base >= S->stacksize) + { + S->base = (int *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(int)); + if (!S->base) + { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; + }//end of Push + +Status nPop(Number *S, int *e) + { + if (S->top == S->base) + { + return ERROR; + } + *e = *--S->top;//待检测 + return OK; + }//end of Pop + +//判断空栈 +Status nStackEmpty(Number *S) + { + if (S->base == S->top) + { + return OK; + } + else { + return ERROR; + + } + }//end of StackEmpty + +//获取栈顶元素的函数 +Status nGetTop(Number *S, int *e) + { + if (S->top == S->base) { + return ERROR; + + } + *e = *(S->top - 1); + return OK; + }//end of GetTop + +//操作符的判断函数 +Bool IN(SElemType ch) + { + int j; + for (j = 0; j < 7; j++) + { + if (ch == OP[j]) { + return TRUE; + + } + } + return FALSE; + } \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/struct.c" "b/2017-1/cxhomework/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/struct.c" new file mode 100644 index 00000000..8072125f --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/struct.c" @@ -0,0 +1,375 @@ +#include "source.h" + +//优先级判断函数 +int Precede(SElemType c, SElemType ch) + { + int i, j; + switch (c) { + case '+': + i = 0; + if (ch == '+') { + j = 0; + + } + else if (ch == '-') { + j = 1; + + } + else if (ch == '*') { + j = 2; + + } + else if (ch == '/') { + j = 3; + + } + else if (ch == '(') { + j = 4; + + } + else if (ch == ')') { + j = 5; + + } + else { + j = 6; + + } + break; + case '-': + i = 1; + if (ch == '+') { + j = 0; + + } + else if (ch == '-') { + j = 1; + + } + else if (ch == '*') { + j = 2; + + } + else if (ch == '/') { + j = 3; + + } + else if (ch == '(') { + j = 4; + + } + else if (ch == ')') { + j = 5; + + } + else { + j = 6; + + } + break; + case '*': + i = 2; + if (ch == '+') { + j = 0; + + } + else if (ch == '-') { + j = 1; + + } + else if (ch == '*') { + j = 2; + + } + else if (ch == '/') { + j = 3; + + } + else if (ch == '(') { + j = 4; + + } + else if (ch == ')') { + j = 5; + + } + else { + j = 6; + + } + break; + case '/': + i = 3; + if (ch == '+') { + j = 0; + + } + else if (ch == '-') { + j = 1; + + } + else if (ch == '*') { + j = 2; + + } + else if (ch == '/') { + j = 3; + + } + else if (ch == '(') { + j = 4; + + } + else if (ch == ')') { + j = 5; + + } + else { + j = 6; + + } + break; + case '(': + i = 4; + if (ch == '+') { + j = 0; + + } + else if (ch == '-') { + j = 1; + + } + else if (ch == '*') { + j = 2; + + } + else if (ch == '/') { + j = 3; + + } + else if (ch == '(') { + j = 4; + + } + else if (ch == ')') { + j = 5; + + } + else { + j = 6; + + } + break; + case ')': + i = 5; + if (ch == '+') { + j = 0; + + } + else if (ch == '-') { + j = 1; + + } + else if (ch == '*') { + j = 2; + + } + else if (ch == '/') { + j = 3; + + } + else if (ch == '(') { + j = 4; + + } + else if (ch == ')') { + j = 5; + + } + else { + j = 6; + + } + break; + case '#': + i = 6; + if (ch == '+') { + j = 0; + + } + else if (ch == '-') { + j = 1; + + } + else if (ch == '*') { + j = 2; + + } + else if (ch == '/') { + j = 3; + + } + else if (ch == '(') { + j = 4; + + } + else if (ch == ')') { + j = 5; + + } + else { + j = 6; + + } + break; + + } + return PT[i][j]; + }//end of Precede + +//表达式求值 +Status Evaluation(Number *SN, char s[100], int length) + { + nInitStack(SN);//数字所在的栈 + int e1, e2; + int result = 0; + int count = 1;//loop counter + nPush(SN, s[count - 1] - '0'); + while (countc:false + { + if (c != '(') { + Pass(suffix, c); + + } + Pop(S, &c); + } + if (ch != '#') { + Push(S, ch); + + } + break; + + } // switch + + } + if (ch != '#') { + p++; + ch = *p; + + } + else { + Pop(S, &ch);//删除'#' + Pass(suffix, ch);//'#'进入suffix + break; + + } + + }//while + return OK; + +} // transform + +int main() + { + SqStack S; + char suf[101] = { NULL }; + char ex[100] = { NULL }; + gets(ex); + transform(&S, suf, ex); + return 0; + } \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/misshome.c" "b/2017-1/cxhomework/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/misshome.c" new file mode 100644 index 00000000..0991eabe --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/misshome.c" @@ -0,0 +1,135 @@ +//#include +//#include +//#include +//struct stack_node +//{ +// int x; //路径坐标x +// int y; //路径坐标y +// struct stack_node *next; +//}; +//typedef struct stack_node stack_list; +//typedef stack_list * link; +#include "misshome.h"; + +link path = NULL; //路径栈指针 +link push(link stack, int x, int y) //栈数据的存入 +{ + link new_node; + new_node = (link)malloc(sizeof(stack_list)); + if (!new_node) + { + printf("内存分配失败! \n"); + return NULL; + } + new_node->x = x; + new_node->y = y; + new_node->next = stack; + stack = new_node; + return stack; +} +link pop(link stack, int *x, int *y) //栈数据的取出 +{ + link top; + if (stack != NULL) + { + top = stack; + stack = stack->next; + *x = stack->x; + *y = stack->y; + free(top); + return stack; + } + else + *x = -1; +} + +void migong(int maze[8][11]) +{ + int i, j; + i = 1; + for (j = 1; j<11; j++) + maze[i][j] = 1; + j = 1; + for (i = 1; i<8; i++) + maze[i][j] = 1; + i = 7; + for (j = 1; j<11; j++) + maze[i][j] = 1; + j = 10; + for (i = 1; i<8; i++) + maze[i][j] = 1; + srand(time(NULL)); + for (i = 2; i<7; i++) + for (j = 2; j<10; j++) + maze[i][j] = rand() % 2; + printf("迷宫\n"); + for (i = 1; i<8; i++) + { + for (j = 1; j<11; j++) + printf("%3d", maze[i][j]); + printf("\n"); + } +} +void main() +{ + int maze[8][11]; + int endx, endy; + int startx, starty; + int x, y, i, j; + migong(maze); + printf("输入开始点坐标(1,1开始): "); + scanf("%d %d", &startx, &starty); + while (maze[startx][starty] != 0) + { + printf("重新输入: "); + scanf("%d %d", &startx, &starty); + } + printf("输入终点坐标: "); + scanf("%d %d", &endx, &endy); + while (maze[endx][endy] != 0) + { + printf("重新输入: "); + scanf("%d %d", &endx, &endy); + } + x = startx; y = starty; + while (x != endx || y != endy) + { + maze[x][y] = 2; + if (maze[x - 1][y] <= 0) //上 + { + x = x - 1; + path = push(path, x, y); + } + else + if (maze[x + 1][y] <= 0) //下 + { + x = x + 1; + path = push(path, x, y); + } + else + if (maze[x][y - 1] <= 0) //左 + { + y = y - 1; + path = push(path, x, y); + } + else + if (maze[x][y + 1] <= 0) //右 + { + y = y + 1; + path = push(path, x, y); + } + else + { + maze[x][y] = 3; //退栈 + path = pop(path, &x, &y); + } + } + maze[x][y] = 2; //标示最后一点 + printf("迷宫路径如下图所示: \n"); + for (i = 1; i<8; i++) + { + for (j = 1; j<11; j++) + printf("%3d", maze[i][j]); + printf("\n"); + } +} \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/misshome.h" "b/2017-1/cxhomework/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/misshome.h" new file mode 100644 index 00000000..dec75e7d --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/misshome.h" @@ -0,0 +1,11 @@ +#include +#include +#include +struct stack_node +{ + int x; //路径坐标x + int y; //路径坐标y + struct stack_node *next; +}; +typedef struct stack_node stack_list; +typedef stack_list * link; \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/Source.c" "b/2017-1/cxhomework/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/Source.c" new file mode 100644 index 00000000..65639f92 --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/Source.c" @@ -0,0 +1,46 @@ +#include"Source.h" + status CreatBiTree(BiTree *t, char *x) + { + ElemType ch, temp; + ch = x[number]; + number++; + if (ch == '*') + { + *t = NULL; + } + else + { + *t = (BiTree)malloc(sizeof(BitNode));//分配空间进行存储 + if (!(*t))//如果分配失败 + { + exit(-1); + } + (*t)->date = ch;//生成根节点 + CreatBiTree(&(*t)->lChild, x);//构造左子树 + CreatBiTree(&(*t)->rChild, x);//构造右子树 + } + return 1; + } +void VisitHouxu(BiTree t)//后续遍历 + { + if (t == NULL) + { + return; + } + VisitHouxu(t->lChild);//访问左子树 + VisitHouxu(t->rChild);//访问右子树 + printf("%c ", t->date); + } +//后序遍历二叉树 +int main() + { + BiTree t; + ElemType n[num]; + printf("请输入要进行转化的字符串 *代表为空\n"); + scanf("%s", n); + printf("*代表空\n"); + CreatBiTree(&t, n);//产生二叉树 + printf("后序遍历的顺序为\n"); + VisitHouxu(t);//后序遍历二叉树 + return 0; + } \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/Source.h" "b/2017-1/cxhomework/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/Source.h" new file mode 100644 index 00000000..a87ad302 --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/Source.h" @@ -0,0 +1,15 @@ +#include +#include +typedef char ElemType; +typedef int status; +#define num 20 +int number = 0;//定义一个全局变量 用于赋值时的计数 +//定义二叉树 +typedef struct BiTree + { + ElemType date; + struct BiTree *lChild; + struct BiTree * rChild; + } BitNode, *BiTree; +status CreatBiTree(BiTree *t, char *x);//先序创立二叉树 +void VisitHouxu(BiTree t);//后续遍历 \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\347\254\254\344\270\200\347\247\215\345\212\236\346\263\225\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221.jpg" "b/2017-1/cxhomework/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\347\254\254\344\270\200\347\247\215\345\212\236\346\263\225\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221.jpg" new file mode 100644 index 00000000..42a27c9c Binary files /dev/null and "b/2017-1/cxhomework/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\347\254\254\344\270\200\347\247\215\345\212\236\346\263\225\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221.jpg" differ diff --git "a/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/leaves.c" "b/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/leaves.c" new file mode 100644 index 00000000..885e0a45 --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/leaves.c" @@ -0,0 +1,67 @@ +#include"leaves.h" + status CreatBiTree(BiTree *t, char *x) + { + ElemType ch, temp; + ch = x[number]; + number++; + if (ch == '*') + { + *t = NULL; + } + else + { + *t = (BiTree)malloc(sizeof(BitNode));//分配空间进行存储 + if (!(*t))//如果分配失败 + { + exit(-1); + } + (*t)->date = ch;//生成根节点 + CreatBiTree(&(*t)->lChild, x);//构造左子树 + CreatBiTree(&(*t)->rChild, x);//构造右子树 + } + return 1; + } +//求二叉树叶子结点个数 +int Leafcount(BiTree T, int *m) + { + if (T) + { + if (T->lChild == NULL &&T->rChild == NULL) + (*m)++; + Leafcount(T->lChild, m); + Leafcount(T->rChild, m); + } + return *m; + } +//求二叉树的所有结点 +int GetCount(BiTree T) + { + if (T == NULL) + { + return 0; + } + return 1 + GetCount(T->lChild) + GetCount(T->rChild);//返回子数的结点 + } +int main() + { + BiTree t; + ElemType n[num]; + int m = 0;//进行叶子结点的统计 + int k; + printf("请选择要进行转化的字符串,输入1为abd*f***ce***,输入2为abdg**eh***k**c*f** *代表为空\n"); + scanf("%d", &k); + if (k == 1) + { + strcpy(n, "abd*f***ce***"); + } + else + { + strcpy(n, "abdg**eh***k**c*f**"); + } + printf("*代表空\n"); + CreatBiTree(&t, n);//产生二叉树 + Leafcount(t, &m);//叶子结点 + printf("\n叶子结点的个数为%d", m); + printf("\n非叶子结点的个数为%d", GetCount(t) - m); + return 0; + } \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/leaves.h" "b/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/leaves.h" new file mode 100644 index 00000000..fdb6068a --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/leaves.h" @@ -0,0 +1,16 @@ +#include +#include +typedef char ElemType; +typedef int status; +#define num 20 + int number = 0;//定义一个全局变量 用于赋值时的计数 + //定义二叉树 +typedef struct BiTree + { + ElemType date; + struct BiTree *lChild; + struct BiTree * rChild; + } BitNode, *BiTree; +status CreatBiTree(BiTree *t, char *x);//先序创立二叉树 +int GetCount(BiTree T);//求二叉树的所有结点 +int Leafcount(BiTree T, int *m);//求二叉树叶子结点个数 \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/twotree.c" "b/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/twotree.c" new file mode 100644 index 00000000..23220ca4 --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/twotree.c" @@ -0,0 +1,95 @@ +#include"twotree.h" + status CreatBiTree(BiTree *t, char *x) + { + ElemType ch, temp; + ch = x[number]; + number++; + if (ch == '*') + { + *t = NULL; + } + else + { + *t = (BiTree)malloc(sizeof(BitNode));//分配空间进行存储 + if (!(*t))//如果分配失败 + { + exit(-1); + } + (*t)->date = ch;//生成根节点 + CreatBiTree(&(*t)->lChild, x);//构造左子树 + CreatBiTree(&(*t)->rChild, x);//构造右子树 + } + return 1; + } +//后序遍历二叉树 +void VisitHouxu(BiTree t)//后续遍历 + { + if (t == NULL) + { + printf("*"); + return; + } + VisitHouxu(t->lChild);//访问左子树 + VisitHouxu(t->rChild);//访问右子树 + printf("%c ", t->date); + } +//二叉树的深度 +int TreeDeep(BiTree T) + { + int deep = 0; + if (T) + { + int leftdeep = TreeDeep(T->lChild);//查找左子树的的最深值 + int rightdeep = TreeDeep(T->rChild);//查找右子树的最深值 + deep = leftdeep >= rightdeep ? leftdeep + 1 : rightdeep + 1; + } + return deep; + } +//后序遍历二叉树 +int floor = 1;//用于计数层数 +int width[width_num] = { 0 };//用于存储每层的结点个数 +int widthHouxu(BiTree t)//后续遍历 + { + int i; + int max = 0;//用于存储最大值 + if (t == NULL) + { + return 0; + } + width[floor]++; + floor++; + widthHouxu(t->lChild);//访问左子树 + widthHouxu(t->rChild);//访问右子树 + floor--;//当左子树和右子树均遍历了 返回上一层继续统计 + for (i = 0; i < width_num; i++)//找到最大值进行输出 + { + if (max < width[i]) + { + max = width[i]; + } + } + return max; + } +int main() + { + BiTree t; + ElemType n[num]; + int k; + printf("请选择要进行转化的字符串,输入1为abd*f***ce***,输入2为abdg**eh***k**c*f** *代表为空\n"); + scanf("%d", &k); + if (k == 1) + { + strcpy(n, "abd*f***ce***"); + } + else + { + strcpy(n, "abdg**eh***k**c*f**"); + } + printf("*代表空\n"); + CreatBiTree(&t, n);//产生二叉树 + printf("后序遍历的顺序为\n"); + VisitHouxu(t);//后序遍历二叉树 + printf("\n树的深度为%d \n", TreeDeep(t)); + printf("\n树的宽度为%d \n", widthHouxu(t)); + return 0; + } \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/twotree.h" "b/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/twotree.h" new file mode 100644 index 00000000..584a1cf6 --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/twotree.h" @@ -0,0 +1,17 @@ +#include +#include +typedef char ElemType; +typedef int status; +#define num 20 +#define width_num 10 + int number = 0;//定义一个全局变量 用于赋值时的计数 + //定义二叉树 + typedef struct BiTree + { + ElemType date; + struct BiTree *lChild; + struct BiTree * rChild; + } BitNode, *BiTree; +int TreeDeep(BiTree T);//查找数的深度 +status CreatBiTree(BiTree *t, char *x);//先序创立二叉树 +void VisitHouxu(BiTree t);//后续遍历 \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\344\271\213\344\272\214\345\217\211\346\240\221\347\232\204\351\253\230\345\272\246\345\222\214\346\234\200\345\244\247\346\267\261\345\272\246\347\232\204\347\256\227\346\263\225.jpg" "b/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\344\271\213\344\272\214\345\217\211\346\240\221\347\232\204\351\253\230\345\272\246\345\222\214\346\234\200\345\244\247\346\267\261\345\272\246\347\232\204\347\256\227\346\263\225.jpg" new file mode 100644 index 00000000..4d10d99f Binary files /dev/null and "b/2017-1/cxhomework/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\344\271\213\344\272\214\345\217\211\346\240\221\347\232\204\351\253\230\345\272\246\345\222\214\346\234\200\345\244\247\346\267\261\345\272\246\347\232\204\347\256\227\346\263\225.jpg" differ diff --git "a/2017-1/cxhomework/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/Queen.c" "b/2017-1/cxhomework/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/Queen.c" new file mode 100644 index 00000000..5b43f723 --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/Queen.c" @@ -0,0 +1,152 @@ +#include "Queen.h" +int main() + { + Linkqueue q; + Init(&q); + QElmtype x[NUM] = { 'a','b','c','d','e','f','h','i','j','k' }; + QElmtype e; + int i; + for (i = 0; i < NUM; i++) + { + Enqueue(&q, x[i]); + } + printf(" 1.插入完成的队列为\n"); + Queuetraverse(q); + printf("\n 2.队列的长度为 %d\n\n", Queuelength(q)); + printf(" 3.判断队列是否为空并返回队头元素\n\n"); + if (Gethead(q, &e)) + { + printf(" 队列不为空,且栈顶元素为%c\n", e); + } + printf("\n 4.依次删除队列\n\n"); + while (!Queueempty(q)) + { + Dequeue(&q, &e); + printf(" 被删除的元素为%c\n", e); + printf(" 删除后的队列为\n"); + Queuetraverse(q); + } + printf("\n 5.队列的长度为 %d\n\n", Queuelength(q)); + printf(" 6.销毁队列\n"); + Destory(&q); + return 0; + } +//初始化构造一个空队列 成功返回1 否则返回0 +status Init(Linkqueue *q) + {//第一个头元素是不存数据的 + q->rear = (Queueptr)malloc(sizeof(Qnode)); + if (!q->rear) + { + return 0; + } + q->front = q->rear; + q->rear->next = NULL; + return 1; + } +status Destory(Linkqueue *q)//销毁队列q q不在存在 + { + while (q->front) + { + q->rear = q->front->next; + free(q->front); + q->front = q->rear; + } + return 1; + } +status Clearqueue(Linkqueue *q)//将队列清空 + { + Queueptr temp = q->front->next; + Queueptr th; + while (temp) + { + th = temp; + temp = temp->next; + free(th); + }//只保留队头队尾 + temp = NULL; + q->front = q->rear = NULL;//头尾指针均为空 但是再用需要初始化 因为已经完全释放了 + return 1; + } +//若队列为空 返回1 否则返回0 +status Queueempty(Linkqueue q) + { + if (q.front == q.rear) + { + return 1; + } + return 0; + } +//返回队列的长度 不算队头的长度 +status Queuelength(Linkqueue q) + { + q.front = q.front->next; + int num = 0; + while (q.front) + { + num++; + q.front = q.front->next; + } + return num; + } +//若队列不空 返回e 1 否则返回0 +status Gethead(Linkqueue q, QElmtype *e) + { + if (q.front == q.rear) + { + return 0; + } + *e = q.front->next->data; + return 1; + } +//插入元素e进q 失败返回0 否则返回1 +status Enqueue(Linkqueue *q, QElmtype e) + { + Queueptr p = (Queueptr)malloc(sizeof(Qnode)); + if (!p) + { + return 0; + } + p->data = e; + p->next = NULL; + q->rear->next = p;//注意单向链表的方向 + q->rear = p; + return 1; + } +//若队列不空 删除队头元素 并用e返回其值 返回1 否则返回0 +status Dequeue(Linkqueue *q, QElmtype *e) + { + if (q->front == q->rear) + { + return 0; + } + Queueptr p = q->front->next; + *e = p->data; + q->front->next = p->next; + if (q->rear == p) + { + q->rear = q->front; + } + free(p); + return 1; + } +//从队头到队尾依次调用函数visit() ,一旦visit()失败则操作失败 +status Queuetraverse(Linkqueue q) + { + q.front = q.front->next; + while (visit(q.front)) + { + q.front = q.front->next; + } + printf("\n"); + return 1; + } +int visit(Queueptr p) +{ + if (NULL != p) + { + char x = p->data; + printf(" %c ", x); + return 1; + } + return 0; + } \ No newline at end of file diff --git "a/2017-1/cxhomework/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/Queen.h" "b/2017-1/cxhomework/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/Queen.h" new file mode 100644 index 00000000..714617e2 --- /dev/null +++ "b/2017-1/cxhomework/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/Queen.h" @@ -0,0 +1,25 @@ +#include +#include +#define NUM 10 +typedef char QElmtype; +typedef int status; +typedef struct Qnode + { + QElmtype data; + struct Qnode *next; + }Qnode, *Queueptr; +typedef struct + { + Queueptr front;//队头指针 + Queueptr rear;//队尾指针 + }Linkqueue; +status Init(Linkqueue *q);//初始化构造一个空队列 +status Destory(Linkqueue *q);//销毁队列q q不在存在 +status Clearqueue(Linkqueue *q);//将队列清空 +status Queueempty(Linkqueue q);//若队列为空 返回1 否则返回0 +status Queuelength(Linkqueue q);//返回队列的长度 +status Gethead(Linkqueue q, QElmtype *e);//若队列不空 返回e 1 否则返回0 +status Enqueue(Linkqueue *q, QElmtype e);//插入元素e进q +status Dequeue(Linkqueue *q, QElmtype *e);//若队列不空 删除队头元素 并用e返回其值 返回1 否则返回0 +status Queuetraverse(Linkqueue q);//从队头到队尾依次调用函数visit() ,一旦visit()失败则操作失败 +int visit(Queueptr);//访问并打印每一个队列元素 \ No newline at end of file diff --git a/2017-1/ds b/2017-1/ds new file mode 160000 index 00000000..f3a045f7 --- /dev/null +++ b/2017-1/ds @@ -0,0 +1 @@ +Subproject commit f3a045f79d257c515ca974cfd95e4ecd1ab90101 diff --git "a/2017-1/hh-bo/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\347\256\227\346\263\2252.12.c" "b/2017-1/hh-bo/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\347\256\227\346\263\2252.12.c" new file mode 100644 index 00000000..424052e3 --- /dev/null +++ "b/2017-1/hh-bo/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\347\256\227\346\263\2252.12.c" @@ -0,0 +1,102 @@ +#include +#include +typedef struct list +{ + int data; + struct list *next; +}List; +List *Creat(int m[],int n) +{ + List *head, *next, *present; + int a; + head = (List*)malloc(sizeof(List)); + head->next = NULL; + present = head; + next = NULL; + for (int i = 0;i < n;i++) + { + a=m[i]; + next = (List*)malloc(sizeof(List)); + next->data = a; + present->next = next; + present = next; + } + present->next = NULL; + return head; +} +List* MergeList(List *LA, List *LB) +{ + List *pc = NULL; + List *pa = LA->next; + List *pb = LB->next; + List *LC; + LC = LA; + LC->next = NULL; + pc = LC; + free(LB); + //LB=NULL; + + while (pa != NULL && pb != NULL) + { + if (pa->data <= pb->data) + { + pc->next = pa; + + pc = pa; + pa = pa->next; + } + else + { + pc->next = pb; + + pc = pb; + pb = pb->next; + } + pc->next = NULL; + } + + if (pa == NULL) + { + pc->next = pb; + + } + else + { + pc->next = pa; + } + return LC; +} +void Print(List *head) +{ + List *p; + p = head->next; + while (p != 0) + { + printf("%d ", p->data); + p = p->next; + } + printf("\n"); +} +int main() +{ + List *head1, *head2, *head3; + int a[4] = { 3,5,8,11 }; + int b[7] = { 2,6,8,9,11,15,20 }; + printf("线性表LA:"); + head1 = Creat(a,4); + Print(head1); + + printf("线性表LB:"); + head2 = Creat(b,7); + Print(head2); + + + + + head3=MergeList(head1, head2); + Print(head3); + + + return 0; + +} diff --git "a/2017-1/hh-bo/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\347\256\227\346\263\2252.12\347\250\213\345\272\217\346\210\252\345\233\276.png" "b/2017-1/hh-bo/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\347\256\227\346\263\2252.12\347\250\213\345\272\217\346\210\252\345\233\276.png" new file mode 100644 index 00000000..c88c4ad4 Binary files /dev/null and "b/2017-1/hh-bo/\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232/\347\256\227\346\263\2252.12\347\250\213\345\272\217\346\210\252\345\233\276.png" differ diff --git "a/2017-1/hh-bo/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217.c" "b/2017-1/hh-bo/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217.c" new file mode 100644 index 00000000..c70e119c --- /dev/null +++ "b/2017-1/hh-bo/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217.c" @@ -0,0 +1,494 @@ +// +// main.c +// houzhui +// +// Created by Huhongbo on 2017/4/10. +// Copyright 漏 2017骞 Huhongbo. All rights reserved. +// + +#include +#include +#include + +#define STACK_INIT_SIZE 100 + +typedef char SElemType; +typedef double SElem; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef struct _SqStack +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +typedef struct _SqStack1 +{ + SElem *base; + SElem *top; + int stacksize; +}SqStack1; + +typedef enum +{ + true, + false +}bool; + +Status Pop(SqStack*s, SElemType*e); +bool StackEmpty(SqStack*s); +Status InitStack(SqStack*s); +Status Push(SqStack*s, SElemType e); +Status GetTop(SqStack *s, SElemType *e); + + +Status Pop1(SqStack1*s, SElem*e); +bool StackEmpty1(SqStack1*s); +Status InitStack1(SqStack1*s); +Status Push1(SqStack1*s, SElem e); + + +int Level(char character); +bool IN(char ch); +Status Pass(char suffix[50], char ch); +bool Precede(SElemType*c, char ch); + +void transform(char suffix[100], char exp[100], SqStack*s); + +void EvaluateExpression(SqStack *s2, SqStack1*s3, char suffix[100]); + +//瀛樺偍瀛楃鍨嬫暟鎹殑鏍堬紱 +Status Pop(SqStack*s, SElemType*e) +{ + if (s->top == s->base) + { + return ERROR; + } + else + { + *e = *--s->top; + return OK; + } + +} +bool StackEmpty(SqStack*s) +{ + if (s->base == s->top) + { + return true; + } + else + { + return false; + } +} + +Status InitStack(SqStack*s) +{ + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; +} +Status Push(SqStack*s, SElemType e) +{ + if (s->top - s->base > s->stacksize) + { + s->base = (SElemType*)realloc(s->base, (s->stacksize + 2 * STACK_INIT_SIZE) * sizeof(SElemType)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base + s->stacksize; + s->stacksize += 2 * STACK_INIT_SIZE; + } + *s->top = e; + s->top++; + return OK; + +} + +Status GetTop(SqStack *s, SElemType *e) +{ + if (s->top == s->base) + { + return ERROR; + } + *e = *(s->top - 1); + return OK; +} + +Status DistoryStack(SqStack *s) +{ + while (StackEmpty(s) == false) + { + s->top = NULL; + s->top--; + } + s->base = NULL; + free(s->base); + free(s->top); + return OK; +} + + +//瀛樺偍鏁村瀷鏁版嵁鐨勬爤锛 +Status Pop1(SqStack1*s, SElem*e) +{ + if (s->top == s->base) + { + return ERROR; + } + else + { + *e = *--s->top; + return OK; + } + +} +bool StackEmpty1(SqStack1*s) +{ + if (s->base == s->top) + { + return true; + } + else + { + return false; + } +} + +Status InitStack1(SqStack1*s) +{ + s->base = (SElem*)malloc(STACK_INIT_SIZE * sizeof(SElem)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; +} +Status Push1(SqStack1*s, SElem e) +{ + if (s->top - s->base > s->stacksize) + { + s->base = (SElem*)realloc(s->base, (s->stacksize + 2 * STACK_INIT_SIZE) * sizeof(SElem)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base + s->stacksize; + s->stacksize += 2 * STACK_INIT_SIZE; + } + *s->top = e; + s->top++; + return OK; + +} + +Status DistoryStack1(SqStack1 *s) +{ + while (StackEmpty(s) == false) + { + s->top = NULL; + s->top--; + } + s->base = NULL; + free(s->base); + free(s->top); + return OK; +} + + + +//鍒ゆ柇杩愮畻绗︾殑浼樺厛绾э紱 +int Level(char character) +{ + int num; + switch (character) + { + case'(': + num = 9; + break; + case')': + num = 1; + break; + case'*': + num = 3; + break; + case'/': + num = 4; + break; + case'+': + num = 5; + break; + case'-': + num = 6; + break; + case'#': + num = 8; + break; + default: + num = 0; + break; + } + return num; +} + +bool Precede(SElemType*c, char ch) +{ + + if (Level(*c) != 0 && Level(ch) != 0) + { + if (Level(*c) - Level(ch)>1) + { + return false; + } + else + { + return true; + } + } + else + { + return false; + } +} + + +//鍒ゆ柇鏁版嵁鏄惁涓0-9鐨勬搷浣滄暟锛 +bool IN(char ch) +{ + if (ch >= '0'&&ch <= '9') + { + return false; + } + else + { + return true; + } +} + +//鍚戝悗缂寮忔暟缁勪腑濉厖鏁版嵁锛 +Status Pass(char suffix[50], char ch) +{ + static int i = 0; + if (i <= 50) + { + suffix[i] = ch; + suffix[i + 1] = '\0'; + i++; + return OK; + } + else + { + return ERROR; + } +} + +//寤虹珛鍚庣紑寮忥紱 +void transform(char suffix[100], char exp[100], SqStack*s) +{ + Push(s, '#'); + char*p = exp; + char ch; + ch = *p; + int i = 0; + SElemType c; + while (StackEmpty(s) == false) + { + if (IN(ch) == false)//鍒ゆ柇鏄惁涓烘搷浣滄暟锛 + { + Pass(suffix, ch); + } + else + { + switch (ch) + { + case'(': + Push(s, ch); + break; + case ')': + Pop(s, &c); + while (c != '(') + { + Pass(suffix, c); + Pop(s, &c); + while (suffix[i] != '\0') + { + printf("%c", suffix[i]); + i++; + } + printf("\n"); + i = 0; + } + break; + default: + GetTop(s, &c); + while (Precede(&c, ch) == true && ch != '\0') + { + Pass(suffix, c); + Pop(s, &c); + GetTop(s, &c); + while (suffix[i] != 0) + { + printf("%c", suffix[i]); + i++; + } + printf("\n"); + i = 0; + } + if (ch != '\0') + { + Push(s, ch); + while (suffix[i] != 0) + { + printf("%c", suffix[i]); + i++; + } + i = 0; + printf("\n"); + } + break; + } + + } + if (ch != '\0') + { + p++; + ch = *p; + } + else + { + while (*s->top != '#') + { + Pop(s, &ch); + if (ch != '#') + { + Pass(suffix, ch); + while (suffix[i] != 0) + { + printf("%c", suffix[i]); + i++; + } + i = 0; + printf("\n"); + } + } + } + } +} + + +//鍒╃敤鍚庣紑寮忔暟鎹強鏁版嵁鏍堝拰绗﹀彿鏍堣繘琛0-9鏁版嵁鐨勭畝鍗+-*/杩愮畻锛 +void EvaluateExpression(SqStack *s2, SqStack1*s3, char suffix[100]) +{ + //char a[5] = {'*','/','+','-','#' }; + double x = 0; + double y = 0; + char z; + int i = 0; + int j = 0; + int q = 0; + char*p = suffix; + char ch; + Push(s2, '#'); + while (StackEmpty(s2) == false) + { + ch = *p; + if (IN(ch) == false)//鍒ゆ柇鏄惁涓烘搷浣滄暟锛 + { + Push1(s3, ch - '0'); + i++; + } + else + { + Push(s2, ch); + j++; + } + if (i >= 2 && j>0) + { + Pop1(s3, &x); + Pop1(s3, &y); + Pop(s2, &z); + q = Level(z); + switch (q) + { + case 5: + x = y + x; + Push1(s3, x); + break; + case 6: + x = y - x; + Push1(s3, x); + break; + case 3: + x = (y) * (x); + Push1(s3, x); + break; + case 4: + x = y / x; + Push1(s3, x); + break; + default: + break; + } + i--; + j--; + } + p++; + if (*p == '\0') + { + break; + } + } +} +int main() +{ + int i = 0, j = 0; + SqStack s1; + SqStack s2; + SqStack1 s3; + + InitStack(&s1); + InitStack(&s2); + InitStack1(&s3); + + char suffix[50]; + char exp[50]; + printf("璇疯緭鍏0-9浠ュ唴鏁扮殑绠鍗曡繍绠楀紡\n"); + gets(exp); + while (exp[i] != '\0') + { + if (exp[i] == '/'&&exp[i + 1] == '0') + { + printf("ERROR\n"); + printf("璇烽噸鏂拌緭鍏n"); + gets(exp); + + } + i++; + } + printf("\n"); + /*for (i = 0; i < 50; i++) + { + exp[i] = '#'; + }*/ + printf("鍚庣紑寮忓舰鎴愯繃绋:\n"); + transform(suffix, exp, &s1); + EvaluateExpression(&s2, &s3, suffix); + printf("缁撴灉:"); + printf("%2lf", *s3.base); + + return 0; +} diff --git "a/2017-1/hh-bo/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\347\250\213\345\272\217\346\210\252\345\233\276.png" "b/2017-1/hh-bo/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\347\250\213\345\272\217\346\210\252\345\233\276.png" new file mode 100644 index 00000000..dbd8571f Binary files /dev/null and "b/2017-1/hh-bo/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\345\220\216\347\274\200\350\241\250\350\276\276\345\274\217\347\250\213\345\272\217\346\210\252\345\233\276.png" differ diff --git "a/2017-1/hh-bo/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234.cpp" "b/2017-1/hh-bo/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234.cpp" new file mode 100644 index 00000000..34300f1a --- /dev/null +++ "b/2017-1/hh-bo/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234.cpp" @@ -0,0 +1,135 @@ +//队列操作的基本实现 + +#include +#include +#include +#define QelemType int +typedef enum +{ + Error, OK +}Status; +typedef struct QNode +{ + QelemType data; + struct QNode *next; +}QNode,*QueuePtr; +typedef struct +{ + QueuePtr front;//队头指针 + QueuePtr rear;//队尾指针 +}Linkqueue; +Status InitQueue(Linkqueue *q)//构造一个空队列 +{ + q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!q->front) + return Error; + q->front->next = NULL; + return OK; +} +Status DestroyQueue(Linkqueue *q)//销毁队列 +{ + while (q->front) + { + q->rear = q->front->next; + free(q->front); + q->front = q->rear; + } + return OK; +} +Status EnQueue(Linkqueue *q, QelemType e)//插入元素 +{ + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) + return Error; + p->data = e; + p->next = NULL; + q->rear->next = p; + q->rear = p; + return OK; +} +Status QueueEmpty(Linkqueue *q)//判断队列是否为空,是则返回OK,非则返回Error +{ + if (q->front == q->rear) + return OK; + else + return Error; +} +QelemType DeQueue(Linkqueue *q)//若队列不空,删除队头元素,并返回其值,否则返回Error +{ + if (QueueEmpty(q)) + return Error; + QueuePtr p; + p = q->front->next; + QelemType e; + e = p->data; + q->front->next=p->next; + free(p); + if (q->rear == p) + q->front = q->rear; + return e; +} +Status ClearQueue(Linkqueue *q)//将Q清为空队列 +{ + q->front = q->rear; + return OK; +} +int QueueLength(Linkqueue*q)//返回Q的元素个数 +{ + int count = 1; + QueuePtr p; + p = q->front->next; + while (p != q->rear) + { + count++; + p = p->next; + } + return count; +} +QelemType GetHead(Linkqueue*q)//若队列不空,则用e返回队头元素,否则返回Error +{ + if (QueueEmpty(q)) + return Error; + QueuePtr p; + p = q->front->next; + QelemType e; + e = p->data; + return e; +} +Status QueueTraverse(Linkqueue *q) +{ + QelemType e; + while (!QueueEmpty(q)) + { + e = DeQueue(q); + printf("%d ", e); + } + return OK; +} +int main() +{ + Linkqueue Q; + InitQueue(&Q); + srand(time(NULL)); + //随机生成队列长度 + int num = rand() % 5; + int i; + for (i = 0;i < num;i++) + { + //随机生成队列数据 + QelemType a; + a = rand() % 10; + EnQueue(&Q, a); + } + //计算并输出队列长度 + printf("The length of the queue is:"); + printf("%d\n", QueueLength(&Q)); + //打印出该队列 + printf("Print the queue:\n"); + QueueTraverse(&Q); + //销毁队列 + //DestroyQueue(&Q);因为在打印的过程中调用了DeQueue函数,清空了链表,无法正确调用销毁函数 + return 0; + + +} \ No newline at end of file diff --git "a/2017-1/hh-bo/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234\347\250\213\345\272\217\346\210\252\345\233\276.png" "b/2017-1/hh-bo/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234\347\250\213\345\272\217\346\210\252\345\233\276.png" new file mode 100644 index 00000000..1ee82219 Binary files /dev/null and "b/2017-1/hh-bo/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\351\230\237\345\210\227\345\237\272\346\234\254\346\223\215\344\275\234\347\250\213\345\272\217\346\210\252\345\233\276.png" differ diff --git "a/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\213\254\345\217\267\345\214\271\351\205\215.cpp" "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\213\254\345\217\267\345\214\271\351\205\215.cpp" new file mode 100644 index 00000000..20b39381 --- /dev/null +++ "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\213\254\345\217\267\345\214\271\351\205\215.cpp" @@ -0,0 +1,126 @@ + +#include +#include +#include +#define STACK_INIT_SIZE 20 +#define STACKINCREMENT 10 + +typedef enum +{ + Error, OK +}Status; + +typedef struct { + char *base; + char *top; + int stacksize; +}SqStack; + +Status InitStack(SqStack *s) +{ + s->base = (char*)malloc(STACK_INIT_SIZE * sizeof(char)); + if (!s->base) + return Error; + else + { + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; + } +} + +Status Push(SqStack *s, char e) +{ + + if (s->top - s->base >= s->stacksize) + { + s->base = (char*)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(char)); + if (!s->base) + return Error; + s->top = s->base + s->stacksize; + s->stacksize += STACKINCREMENT; + } + *s->top= e; + s->top++; + return OK; +} + +int StackEmpty(SqStack *s) +{ + if (s->top == s->base) + { + return 0; + } + else + return 1; +} + +Status Pop(SqStack *s) +{ + if (s->top == s->base) + return Error; + s->top--; + return OK; +} + +char GetTop(SqStack *s) +{ + char *e; + e = (char*)malloc(sizeof(char)); + if (s->top == s->base) + return Error; + *e = *(s->top -1); + return *e; +} + +Status matching(SqStack *s,char *p) +{ + int status=1; + int len = strlen(p); + int i = 0; + while (i < len&&status) + { + switch (*p) + { + case '{': + case '(': + case '[': + Push(s, *p); + p++; + i++; + break; + case '}': + case ')': + case ']': + if (StackEmpty(s)&&(GetTop(s) == '{'||GetTop(s) == '['||GetTop(s) == '(')) + { + status = 1; + Pop(s); + i++; + } + else + { + status = 0; + i++; + } + } + } + if (!StackEmpty(s) && status == 1) + return OK; + else + return Error; +} +int main() +{ + SqStack s; + Status result; + char test[STACK_INIT_SIZE]; + scanf("%s", test); + InitStack(&s); + result = matching(&s, test); + if (result == OK) + printf("matched!\n"); + else + printf("not matched!\n"); + return 0; +} diff --git "a/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\213\254\345\217\267\345\214\271\351\205\215\347\250\213\345\272\217\346\210\252\345\233\2761.png" "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\213\254\345\217\267\345\214\271\351\205\215\347\250\213\345\272\217\346\210\252\345\233\2761.png" new file mode 100644 index 00000000..f08147d2 Binary files /dev/null and "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\213\254\345\217\267\345\214\271\351\205\215\347\250\213\345\272\217\346\210\252\345\233\2761.png" differ diff --git "a/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\213\254\345\217\267\345\214\271\351\205\215\347\250\213\345\272\217\346\210\252\345\233\2762.png" "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\213\254\345\217\267\345\214\271\351\205\215\347\250\213\345\272\217\346\210\252\345\233\2762.png" new file mode 100644 index 00000000..36418a18 Binary files /dev/null and "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\213\254\345\217\267\345\214\271\351\205\215\347\250\213\345\272\217\346\210\252\345\233\2762.png" differ diff --git "a/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\225\260\345\210\266\350\275\254\346\215\242.cpp" "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\225\260\345\210\266\350\275\254\346\215\242.cpp" new file mode 100644 index 00000000..df641c97 --- /dev/null +++ "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\225\260\345\210\266\350\275\254\346\215\242.cpp" @@ -0,0 +1,81 @@ + + +#include +#include +#include +typedef enum +{Error,OK}Status; +//#define Status int +//#define Error 0 +//#define OK 1 +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +typedef struct{ + int *base; + int *top; + int stacksize; +}SqStack; +Status InitStack(SqStack *s) +{ + s->base=(int*)malloc(STACK_INIT_SIZE*sizeof(int)); + if(!s->base) + return Error; + else + { + s->top=s->base; + s->stacksize=STACK_INIT_SIZE; + return OK; + } +} +Status Push(SqStack *s,int e) +{ + if(s->top-s->base>=s->stacksize) + { + s->base=(int*)realloc(s->base,(s->stacksize+STACKINCREMENT)*sizeof(int)); + if(!s->base) + return Error; + s->top=s->base+s->stacksize; + s->stacksize+=STACKINCREMENT; + } + *s->top=e; + s->top++; + return OK; +} +int StackEmpty(SqStack *s) +{ + if(s->top==s->base) + { + return 0; + } + else + return 1; +} +int Pop(SqStack *s) +{ + if(s->top==s->base) + return 0; + else + { + s->top--; + return *s->top; + } +} +int main() +{ + int n; + SqStack S; + InitStack(&S); + srand(time(NULL)); + n = rand() % 1024; + printf("zhuan huan\n", n); + while(n) + { + Push(&S,n%8); + n=n/8; + } + while(StackEmpty(&S)) + { + printf("%d",Pop(&S)); + } + return 0; +} diff --git "a/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\225\260\345\210\266\350\275\254\346\215\242\347\250\213\345\272\217\346\210\252\345\233\276.png" "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\225\260\345\210\266\350\275\254\346\215\242\347\250\213\345\272\217\346\210\252\345\233\276.png" new file mode 100644 index 00000000..d738bf4e Binary files /dev/null and "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\225\260\345\210\266\350\275\254\346\215\242\347\250\213\345\272\217\346\210\252\345\233\276.png" differ diff --git "a/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217.cpp" "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217.cpp" new file mode 100644 index 00000000..7c82462c --- /dev/null +++ "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217.cpp" @@ -0,0 +1,107 @@ +#include +#include +#define STACK_INIT_SIZE 20 +#define STACKINCREMENT 10 +typedef enum +{ + Error, OK +}Status; +//结构体定义 +typedef struct { + char *base; + char *top; + int stacksize; +}SqStack; +//初始化一个空的栈a +Status InitStack(SqStack *s) +{ + s->base = (char*)malloc(STACK_INIT_SIZE * sizeof(char)); + if (!s->base) + return Error; + else + { + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; + } +} +//向栈中加入一个元素 +Status Push(SqStack *s, char e) +{ + //判断栈是否已满 + if (s->top - s->base >= s->stacksize) + { + s->base = (char*)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(char)); + if (!s->base) + return Error; + s->top = s->base + s->stacksize; + s->stacksize += STACKINCREMENT; + } + *s->top = e; + s->top++; + return OK; +} +//删除栈顶元素 +Status Pop(SqStack *s) +{ + if (s->top == s->base) + return Error; + s->top--; + return OK; +} +//清空栈 +Status ClearStack(SqStack *s) +{ + s->top = s->base; + return OK; +} +//打印栈中内容 +Status Print(SqStack *s) +{ + char *p; + p = s->base; + while (p != s->top) + { + printf("%c", *p); + p++; + } + return OK; +} +//行编辑程序 +Status LineEdit(SqStack *s) +{ + char str; + scanf("%c", &str); + while (str != '/') + { + while (str!='/'&&str != '\n') + { + switch (str) + { + case '#': + Pop(s); + break; + case '@': + ClearStack(s); + break; + default: + Push(s, str); + break; + } + scanf("%c", &str); + } + Print(s); + printf("\n"); + if(str!='/') + scanf("%c", &str); + } + return OK; +} + +int main() +{ + SqStack s; + InitStack(&s); + LineEdit(&s); + return 0; +} diff --git "a/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217\346\210\252\345\233\276.png" "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217\346\210\252\345\233\276.png" new file mode 100644 index 00000000..2164fad6 Binary files /dev/null and "b/2017-1/hh-bo/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217\346\210\252\345\233\276.png" differ diff --git "a/2017-1/hh-bo/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221.c" "b/2017-1/hh-bo/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221.c" new file mode 100644 index 00000000..e3d89280 --- /dev/null +++ "b/2017-1/hh-bo/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221.c" @@ -0,0 +1,58 @@ +// +// main.c +// binary tree +// +// Created by Huhongbo on 2017/4/25. +// Copyright 漏 2017骞 Huhongbo. All rights reserved. +// + +#include +#include + +#define TelemType char + +typedef struct BitNode//缁撴瀯瀹氫箟 +{ + TelemType data; + struct BitNode *lchild, *rchild; +}BitNode; + +typedef enum +{ + error,ok +}Status; + +BitNode* CreateBiTree(BitNode* T)//寤虹珛浜屽弶鏍 +{ + TelemType c; + scanf("%c", &c); + if (c == ' ') + T = NULL; + else + { + T = (BitNode*)malloc(sizeof(BitNode)); + T->data = c; + T->lchild=CreateBiTree(T->lchild); + T->rchild=CreateBiTree(T->rchild); + } + return T; +} + +void PostOrder(BitNode* T) +{ + if (T!=NULL) + { + PostOrder(T->lchild); + PostOrder(T->rchild); + printf("%c ", T->data); + } +} + +int main()//涓诲嚱鏁 +{ + BitNode* T=NULL; + printf("Create a BitTree:\n"); + T=CreateBiTree(T); + printf("Postorder Traverse:\n"); + PostOrder(T); +} diff --git "a/2017-1/hh-bo/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221\347\250\213\345\272\217\346\210\252\345\233\276.png" "b/2017-1/hh-bo/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221\347\250\213\345\272\217\346\210\252\345\233\276.png" new file mode 100644 index 00000000..5b5f37b2 Binary files /dev/null and "b/2017-1/hh-bo/\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232/\345\273\272\347\253\213\344\272\214\345\217\211\346\240\221\347\250\213\345\272\217\346\210\252\345\233\276.png" differ diff --git "a/2017-1/hh-bo/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232(2)/main.c" "b/2017-1/hh-bo/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232(2)/main.c" new file mode 100644 index 00000000..21a5affc --- /dev/null +++ "b/2017-1/hh-bo/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232(2)/main.c" @@ -0,0 +1,143 @@ +// +// main.c +// 6 +// +// Created by Huhongbo on 2017/5/10. +// Copyright 漏 2017骞 Huhongbo. All rights reserved. +// + +#include +#include +#define TelemType char +int count = 0; +int count1 = 0; +typedef struct BitNode +{ + TelemType data; + struct BitNode *lchild, *rchild; +}BitNode; +typedef enum +{ + error, ok +}Status; +BitNode* CreateBiTree(BitNode* T, TelemType *c) +{ + + if (c[count++] == ' ') + T = NULL; + else + { + T = (BitNode*)malloc(sizeof(BitNode)); + T->data = c[count - 1]; + T->lchild = CreateBiTree(T->lchild, c); + T->rchild = CreateBiTree(T->rchild, c); + } + return T; +} +void PostOrder(BitNode* T)//璁$畻鎵鏈夎妭鐐逛釜鏁 +{ + if (T != NULL) + { + PostOrder(T->lchild); + PostOrder(T->rchild); + printf("%c ", T->data); + } +} +void Point(BitNode* T) +{ + if (T != NULL) + { + count1++; + Point(T->lchild); + Point(T->rchild); + } +} +int LeafPoint(BitNode* T)//杩斿洖鍙跺瓙鑺傜偣涓暟 +{ + int m, n; + if (T == NULL) + { + return 0; + } + if (T->lchild == NULL&&T->rchild == NULL) + { + return 1; + } + else + { + m = LeafPoint(T->lchild); + n = LeafPoint(T->rchild); + } + return m + n; +} +int Depth(BitNode* T) +{ + int depthL, depthR, depth; + if (!T) + { + depth = 0; + } + else + { + depthL = Depth(T->lchild); + depthR = Depth(T->rchild); + depth = 1 + (depthL > depthR ? depthL : depthR); + } + return depth; +} +int a[10] = { 0 }; +int i = 0; +void Width(BitNode *T) +{ + if (T != NULL) + { + if (i == 0) + { + a[0] = 1; + i++; + if (T->lchild != NULL) a[i]++; + if (T->rchild != NULL) a[i]++; + } + else { + i++; + if (T->lchild != NULL) a[i]++; + if (T->rchild != NULL) a[i]++; + } + Width(T->lchild); + i--; + Width(T->rchild); + } +} +int MaxWidth() +{ + int max = 0,i; + for (i = 0;i < 10;i++) + if (max < a[i]) + max = a[i]; + return max; +} + + +int main() +{ + BitNode* T = NULL; + int count2, count3; + char str[25] = "ABDG EH I K C F "; + TelemType *c = str; + printf("鍒涘缓浜屽弶鏍:"); + T = CreateBiTree(T, c); + printf("%s\n", str); + printf("鍚庡簭閬嶅巻:"); + PostOrder(T); + Point(T); + printf("\n缁撶偣鏁:%d", count1); + count2=LeafPoint(T); + printf("\n鍙跺瓙缁撶偣鏁:%d", count2); + count3 = count1 - count2; + printf("\n闈炲彾瀛愯妭缁撶偣鏁:%d", count3); + printf("\n浜屽弶鏍戠殑娣卞害:"); + printf("%d", Depth(T)); + printf("\n浜屽弶鏍戠殑瀹藉害:"); + Width(T); + printf("%d\n", MaxWidth()); +} diff --git "a/2017-1/hh-bo/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232(2)/\347\250\213\345\272\217\346\210\252\345\233\276.png" "b/2017-1/hh-bo/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232(2)/\347\250\213\345\272\217\346\210\252\345\233\276.png" new file mode 100644 index 00000000..6aa27252 Binary files /dev/null and "b/2017-1/hh-bo/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232(2)/\347\250\213\345\272\217\346\210\252\345\233\276.png" differ diff --git "a/2017-1/hh-bo/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/main.c" "b/2017-1/hh-bo/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/main.c" new file mode 100644 index 00000000..760fd3e7 --- /dev/null +++ "b/2017-1/hh-bo/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/main.c" @@ -0,0 +1,145 @@ +// +// main.c +// Binary tree2 +// +// Created by Huhongbo on 2017/5/2. +// Copyright 漏 2017骞 Huhongbo. All rights reserved. +// + +#include +#include + +typedef enum status +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef char TElemType; + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild,*rchild; +}BiTNode,*BiTree; + +int i = 0; +Status CreateBiTree(BiTree *T,TElemType * p) //浜屽弶鏍戠殑鏋勫缓 +{ + + TElemType ch; + ch = p[i]; + i++; + + if(ch == '*') + { + *T = NULL; + } + else + { + if(!(*T = (BiTree)malloc(sizeof(BiTNode)))) + { + return OVERFLOW; + } + (*T)->data = ch; + CreateBiTree(&(*T)->lchild,p); + CreateBiTree(&(*T)->rchild,p); + } + return OK; +} + +void PostOrderTraverseTree(BiTree T) //浜屽弶鏍戠殑杈撳嚭 +{ + if(T) + { + PostOrderTraverseTree(T->lchild); + PostOrderTraverseTree(T->rchild); + printf("%c",T->data); + } +} + +int Depth(BiTree T)// 浜屽弶鏍戠殑娣卞害 +{ + int depthval,depthLeft,depthRight; + if(!T) + { + depthval = 0; + } + else + { + depthLeft = Depth(T->lchild); + depthRight= Depth(T->rchild); + depthval = 1 + (depthLeft > depthRight ? depthLeft : depthRight); + } + return depthval; +} + +int count[50];//瀛樻斁鍚勫眰缁撶偣鏁帮紱 +int max = 0; //鏈澶у搴︼紱 +int j=0;// + +int Width(BiTree T)//鐢ㄩ掑綊姹傛渶澶у搴 +{ + if(T == NULL) + return 0; + i++; + count[i]++; + if(max < count[i]) + max = count[i]; + Width(T -> lchild); + Width(T -> rchild); + i--; + return max; +} + +int CountLeaf(BiTree T) // 杩斿洖鎸囬拡T鎵鎸囦簩鍙夋爲涓墍鏈夊彾瀛愮粨鐐逛釜鏁 +{ + int m=0,n=0; + if(!T) return 0; + if(!T->lchild && !T->rchild) { + return 1; + } else { + m = CountLeaf(T->lchild); + n = CountLeaf(T->rchild); + return m+n; + } +} + +int Count (BiTree T) //杩斿洖鎸囬拡T鎵鎸囦簩鍙夋爲涓墍鏈夌粨鐐逛釜鏁 +{ + int m,n; + if (!T) return 0; + if (!T->lchild && !T->rchild) { + return 1; + } else { + m = Count(T->lchild); + n = Count(T->rchild); + return m + n + 1; + } +} + +//涓诲嚱鏁扮殑瀹炵幇 +int main() +{ + BiTree T = NULL; + + TElemType str1[30] = "ABDG***EH**I*K**C*F**"; + TElemType str2[30] = "ABD*F***CE***"; + + printf("娴嬭瘯鐢ㄤ緥1:ABDG***EH**I*K**C*F**\n"); + CreateBiTree(&T,str1); + printf("姝や簩鍙夋爲楂樺害涓%d,鏈澶у搴︿负%d\n",Depth(T),Width(T)); + printf("姝や簩鍙夋爲缁撶偣涓%d,鍙跺瓙缁撶偣涓%d",Count(T),CountLeaf(T)); + + printf("\n\n"); + + i=0; + j=0; + printf("娴嬭瘯鐢ㄤ緥2:ABD*F***CE***\n"); + CreateBiTree(&T,str2); + printf("姝や簩鍙夋爲楂樺害涓%d,鏈澶у搴︿负%d\n",Depth(T),Width(T)); + printf("姝や簩鍙夋爲缁撶偣涓%d,鍙跺瓙缁撶偣涓%d",Count(T),CountLeaf(T)); + printf("\n"); + return 0; +} diff --git "a/2017-1/hh-bo/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\347\250\213\345\272\217\346\210\252\345\233\276.png" "b/2017-1/hh-bo/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\347\250\213\345\272\217\346\210\252\345\233\276.png" new file mode 100644 index 00000000..ca770d79 Binary files /dev/null and "b/2017-1/hh-bo/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232\347\250\213\345\272\217\346\210\252\345\233\276.png" differ diff --git "a/2017-1/hh-bo/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\351\223\276\350\241\250\345\210\206\350\247\243.c" "b/2017-1/hh-bo/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\351\223\276\350\241\250\345\210\206\350\247\243.c" new file mode 100644 index 00000000..a2ee9e03 --- /dev/null +++ "b/2017-1/hh-bo/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\351\223\276\350\241\250\345\210\206\350\247\243.c" @@ -0,0 +1,150 @@ +// +// main.c +// 鍒嗚В閾捐〃 +// +// Created by Huhongbo on 2017/4/10. +// Copyright 漏 2017骞 Huhongbo. All rights reserved. +// + +//璇存槑锛 +//鏃堕棿澶嶆潅搴︼細 +//Sequence鍑芥暟鍦ㄥ皢list绾挎ч摼琛ㄥ垎瑙f垚涓や釜寰幆閾捐〃鏃讹紝鍙亶鍘嗕竴娆$嚎鎬ц〃銆傚叾鏃堕棿澶嶆潅搴︾害涓篛(n)(n涓簂ist閾捐〃鐨勯暱搴)锛屾鏃讹紝鏃堕棿澶嶆潅搴︽渶灏忋 +//绌洪棿澶嶆潅搴︼細 +//鍦ㄥ垱寤轰袱涓惊鐜摼琛(list1,list2)鏃讹紝鍙垎鍒噸鏂板姩鎬佸垎閰嶄簡涓涓ご缁撶偣銆傚垱寤轰袱涓惊鐜摼琛ㄧ殑杩囩▼锛宭ist1,list2鍒濆ご缁撶偣澶栧叾浠栫粨鐐癸紝娌℃湁閲嶆柊鍒嗛厤绌洪棿锛岀┖闂村鏉傚害鏈灏忋 + +#include +#include +#include + +typedef int ElemType; + +typedef struct LNode +{ + ElemType data; + struct LNode *next; +}LNode, *LinkList; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +//杈撳嚭鍗曢摼琛紱 +Status output(LinkList L); + +//杈撳嚭寰幆鍗曢摼琛ㄧ殑涓涓懆鏈燂紱 +Status output1(LinkList L); + +//寤虹珛涓涓祴璇曠嚎鎬ч摼琛紙绗竴涓摼缁撶偣涓烘寚閽坙ist鐨勯摼琛級 +void CreateList_L(LinkList L, int n); + +//灏嗙嚎鎬у崟閾捐〃鍒嗚В鍒板鍋跺簭鍒楀惊鐜摼琛紱 +void ParitySequence(LinkList list, LinkList list1, LinkList list2); + +Status output(LinkList L) +{ + LinkList p = L; + if (L->next == NULL) + { + return ERROR; + } + LinkList Line; + Line = L; + while (Line->next != NULL) + { + printf("%d", Line->data); + printf("\n"); + if (Line->next != NULL) + { + Line = Line->next; + } + } + printf("%d", Line->data); + return OK; +} + +Status output1(LinkList L) +{ + LinkList p = L; + if (L->next == NULL) + { + return ERROR; + } + LinkList Line; + Line = L; + while (Line->next !=p) + { + printf("%d", Line->data); + printf("\n"); + if (Line->next != NULL) + { + Line = Line->next; + } + } + printf("%d", Line->data); + return OK; +} +//寤虹珛涓涓祴璇曠嚎鎬ч摼琛紙绗竴涓摼缁撶偣涓烘寚閽坙ist鐨勯摼琛級 +void CreateList_L(LinkList L, int n) +{ + LinkList L1 = L; + L1->data = (int)rand() % 1024; + LinkList p; + int i; + for (i = 0; i < n-1; i++) + { + p = (LinkList)malloc(sizeof(LNode)); //鐢熸垚鏂扮粨鐐癸紱 + p->data = (int)rand() % 1024; + L1->next= p; + L1 = L1->next; + p->next=NULL; + } +} + + +void Sequence(LinkList list,LinkList list1,LinkList list2) +{ + + + LNode*p1 = list1; + LNode*p2 = list2; + LNode*p = list; + list1->data = 0; + list2->data = 0; + while (p!=NULL) + { + p1->next = p; + p1 = p1->next; + list1->data++; + p = p->next; + if (p!= NULL) + { + p2->next = p; + p2 = p2->next; + list2->data++; + p = p->next; + } + } + p1->next = list1; + p2->next = list2; +} +int main() +{ + srand(time(0)); + int n; + LinkList list = (LinkList)malloc(sizeof(LNode)); + LinkList list1 = (LinkList)malloc(sizeof(LNode));//濂囧簭鍙烽摼琛ㄥご缁撶偣锛 + LinkList list2 = (LinkList)malloc(sizeof(LNode));//鍋跺簭鍙烽摼琛ㄥご缁撶偣锛 + n = rand() % 9 + 3;//纭繚閾捐〃list澶у皬澶т簬绛変簬2锛 + printf("闅忔満鐢熸垚绾挎ч摼琛:\n"); + CreateList_L(list, n); + output(list); + Sequence(list, list1, list2); + printf("\n濂囧簭鍒楃殑寰幆閾捐〃锛歕n"); + output1(list1); + printf("\n鍋跺簭鍒楃殑寰幆閾捐〃锛歕n"); + output1(list2); + +} diff --git "a/2017-1/hh-bo/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\351\223\276\350\241\250\345\210\206\350\247\243\347\250\213\345\272\217\346\210\252\345\233\276.png" "b/2017-1/hh-bo/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\351\223\276\350\241\250\345\210\206\350\247\243\347\250\213\345\272\217\346\210\252\345\233\276.png" new file mode 100644 index 00000000..d932fef7 Binary files /dev/null and "b/2017-1/hh-bo/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/\351\223\276\350\241\250\345\210\206\350\247\243\347\250\213\345\272\217\346\210\252\345\233\276.png" differ diff --git a/2017-1/li-yc/Graph/1.PNG b/2017-1/li-yc/Graph/1.PNG new file mode 100644 index 00000000..60d54342 Binary files /dev/null and b/2017-1/li-yc/Graph/1.PNG differ diff --git a/2017-1/li-yc/Graph/2.PNG b/2017-1/li-yc/Graph/2.PNG new file mode 100644 index 00000000..70818a25 Binary files /dev/null and b/2017-1/li-yc/Graph/2.PNG differ diff --git a/2017-1/li-yc/Graph/3.PNG b/2017-1/li-yc/Graph/3.PNG new file mode 100644 index 00000000..501897da Binary files /dev/null and b/2017-1/li-yc/Graph/3.PNG differ diff --git a/2017-1/li-yc/Graph/4.PNG b/2017-1/li-yc/Graph/4.PNG new file mode 100644 index 00000000..11196fbb Binary files /dev/null and b/2017-1/li-yc/Graph/4.PNG differ diff --git a/2017-1/li-yc/Graph/Graph.c b/2017-1/li-yc/Graph/Graph.c new file mode 100644 index 00000000..33d8b269 --- /dev/null +++ b/2017-1/li-yc/Graph/Graph.c @@ -0,0 +1,146 @@ +#include "Graph.h" + +Graph CreatGraph(int size) { + Graph temp; + int i; + int j; + temp.size = size; + temp.matrix = (bool**)malloc(size * sizeof(bool*)); + for (i = 0; i < size; i++) { + temp.matrix[i] = (bool*)malloc(size * sizeof(bool)); + for (j = 0; j < size; j++) { + temp.matrix[i][j] = 0; + } + } + return temp; +} + +Status InitTestGraph(Graph* G) { + G->matrix[0][1] = 1; + G->matrix[1][0] = 1; + G->matrix[0][2] = 1; + G->matrix[2][0] = 1; + G->matrix[0][3] = 1; + G->matrix[3][0] = 1; + G->matrix[0][6] = 1; + G->matrix[6][0] = 1; + G->matrix[1][2] = 1; + G->matrix[2][1] = 1; + G->matrix[3][4] = 1; + G->matrix[4][3] = 1; + G->matrix[3][5] = 1; + G->matrix[5][3] = 1; + G->matrix[4][5] = 1; + G->matrix[5][4] = 1; + G->matrix[5][7] = 1; + G->matrix[7][5] = 1; + G->matrix[6][7] = 1; + G->matrix[7][6] = 1; + G->matrix[6][8] = 1; + G->matrix[8][6] = 1; + G->matrix[7][8] = 1; + G->matrix[8][7] = 1; + + return OK; +} + +Status TraverseGraph(Graph a) { + int size = a.size; + int i; + int j; + for (i = 0; i <= size; i++) { + printf("%2d", i); + } + printf("\n"); + for (j = 0; j < size; j++) { + printf("%2d", j + 1); + for (i = 0; i < size; i++) { + printf("%2d", a.matrix[j][i]); + } + printf("\n"); + } + return OK; +} + +int FirstAdjVex(Graph p, int i) { + int a; + for (a = 0; a < p.size; a++) { + if (p.matrix[i][a] == true) { + return a; + } + } + return -1; +} + +int NextVex(Graph g, int i, int j) { + int a; + for (a = j + 1; a < g.size; a++) { + if (g.matrix[i][a] == true) { + return a; + } + } + return -1; +} + +Status TraversePath(LinkQueue Q, int start) { + int a[MAX] = { -1 }; + QueuePtr p; + int i = 1; + + p = Q.rear; + a[0] = p->data + 1; + p = p->prious; + + while (p->data != start) { + a[i] = p->data + 1; + p = p->prious; + i++; + } + a[i] = start + 1; + for (; i >= 0; i--) { + if (a[i] >= 0) { + printf("%d ", a[i]); + } + } + return OK; +} + +Status BFSTraverse(Graph *g, int start, int end) { + LinkQueue a; + QElemType e; + bool* visited = (bool*)malloc(g->size * sizeof(bool)); + int i; + int w; + int flag = 1; + + a.front = a.rear = (QueuePtr)malloc(sizeof(QNode)); + InitQueue(a); + + for (i = 0; i < g->size; i++) { + visited[i] = false; + } + visited[start] = true; + EnQueue(&a, start); + + while (!QueueEmpty(a)) { + DeQueue(&a, &e); + for (w = FirstAdjVex(*g, e); w != -1; w = NextVex(*g, e, w)) { + if (w == end) { + EnQueue(&a, w); + flag = 0; + break; + } + if (!visited[w]) { + EnQueue(&a, w); + visited[w] = true; + } + } + if (flag == 0) { + TraversePath(a, start); + printf("\n"); + break; + } + } + return OK; +} + diff --git a/2017-1/li-yc/Graph/Graph.h b/2017-1/li-yc/Graph/Graph.h new file mode 100644 index 00000000..775a67c8 --- /dev/null +++ b/2017-1/li-yc/Graph/Graph.h @@ -0,0 +1,17 @@ +#include +#include +#include "Queen.h" + +#define MAX 12 + +typedef struct Graph{ + bool** matrix; + int size; +}Graph; +Graph CreatGraph(int size);//创建图 +Status InitTestGraph(Graph* G);//初始化图 +Status TraverseGraph(Graph a);//遍历图 +Status BFSTraverse(Graph* g, int start ,int end);//广度优先搜索 +int FirstAdjVex(Graph p, int i);//初始点 +int NextVex(Graph g,int i,int j);//下一个点 +Status TraversePath(LinkQueue Q, int start);//打印路径 \ No newline at end of file diff --git a/2017-1/li-yc/Graph/Queen.c b/2017-1/li-yc/Graph/Queen.c new file mode 100644 index 00000000..5b84636c --- /dev/null +++ b/2017-1/li-yc/Graph/Queen.c @@ -0,0 +1,58 @@ +#include "Queen.h" + +Status InitQueue(LinkQueue q) { + q.front = q.rear = (QueuePtr)malloc(sizeof(QNode)); + if (!q.front) { + exit(OVERFLOW); + } + q.front->next = q.rear->next = NULL; + return OK; +} + +Status EnQueue(LinkQueue *q, QElemType e) { + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) { + return OVERFLOW; + } + p->data = e; + p->next = NULL; + p->prious = q->front; + q->rear->next = p; + q->rear = p; + return OK; +} + +bool QueueEmpty(LinkQueue q) { + if (q.front == q.rear) { + return true; + } + else { + return false; + } +} + +Status DeQueue(LinkQueue *q, QElemType *e) { + if (QueueEmpty(*q)) { + return ERROR; + } + q->front = q->front->next; + *e = q->front->data; + return OK; +} + +Status TraverseQueue(LinkQueue q) { + QueuePtr p; + printf("开始遍历:\n"); + if (QueueEmpty(q)) { + printf("此时队列里不存在元素\n"); + return OK; + } + p = q.front; + while (p != q.rear) { + printf("%d ", p->data); + p = p->next; + } + printf("\n"); + return OK; +} diff --git a/2017-1/li-yc/Graph/Queen.h b/2017-1/li-yc/Graph/Queen.h new file mode 100644 index 00000000..747f44b8 --- /dev/null +++ b/2017-1/li-yc/Graph/Queen.h @@ -0,0 +1,30 @@ +#include +#include +#include + +typedef int QElemType; +typedef enum { + false, + true, +}bool; +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct QNode { + QElemType data; + struct QNode *next; + struct QNode *prious; +}QNode, *QueuePtr; +typedef struct LinkQueue { + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +/*关于队列的相关操作*/ +Status InitQueue(LinkQueue q);//初始化队列 +Status EnQueue(LinkQueue *q,QElemType e);//加入一个结点 +bool QueueEmpty(LinkQueue q);//检查队列是否为空 +Status DeQueue(LinkQueue *q,QElemType *e);//删除一个结点 +Status TraverseQueue(LinkQueue q);//遍历队列 \ No newline at end of file diff --git a/2017-1/li-yc/Graph/main.c b/2017-1/li-yc/Graph/main.c new file mode 100644 index 00000000..5c45201b --- /dev/null +++ b/2017-1/li-yc/Graph/main.c @@ -0,0 +1,23 @@ +#include "Graph.h" + +int main() { + Graph a = CreatGraph(9); + int x; + int y; + + InitTestGraph(&a); + //TraverseGraph(a); + + printf("开始遍历每个点到另外一个点的最短路径\n"); + for (x = 1; x <= 9; x++) { + for (y = 1; y <= 9; y++) { + if (x != y) { + printf("<%d --> %d>", x, y); + BFSTraverse(&a, x - 1, y - 1); + printf("\n"); + } + } + } + printf("遍历结束\n"); + return 0; +} \ No newline at end of file diff --git a/2017-1/li-yc/List.cpp b/2017-1/li-yc/List.cpp new file mode 100644 index 00000000..72812d34 --- /dev/null +++ b/2017-1/li-yc/List.cpp @@ -0,0 +1,76 @@ +#include +#include +#include + +#include"List.h" + +void creatList(LinkList &L,int n){ + printf("\nCreatList:\n"); + printf("开始创建长度为%d的单链表\n",n); + + int i; + int number=20; + LinkList p=NULL; + + L->next=NULL; + printf("L->next=NULL\n"); + + for(i=n;i>0;i--){ + printf("创建第%d个节点\n",i); + p=(LinkList)malloc(sizeof(LNode)); + printf("使用malloc函数为指针p分配了空间,p=%x\n",p); + number=number-(rand()%n+1); + p->data=number; + printf("p->data=%d\n",p->data); + p->next=L->next; + printf("p->next=L->next(%x)\n",p); + L->next=p; + } + printf("链表创建完毕\n"); +} + +void Traverse(LinkList &L){ + LinkList a=L; + a=a->next; + while(a){ + printf("%d",a->data); + printf(" "); + a=a->next; + } + printf("\n"); + printf("遍历完成"); +} + +void MergeList(LinkList &La,LinkList &Lb,LinkList &Lc){ + printf("MergeList\n"); + printf("开始归并链表La和Lb\n"); + LinkList pa, pb, pc; + + pa=pb=pc=NULL; + pa=La->next; + pb=Lb->next; + Lc=pc=La; + printf("pa=La->next(%x),pb=Lb->next(%x),Lc=pc=La(%x)\n",La->next,Lb->next,La); + + while(pa&&pb){ + printf("pa与pb都不为空链表,循环继续执行\n"); + printf("pa->data(%d)%spb->data(%d),所以\n",pa->data,pa->data<=pb->data ? "<=" : ">",pb->data); + if(pa->data<=pb->data){ + printf("pc->next=pa(%x),pc=pa(%x),pa=pa->next(%x)\n",pa,pa,pa->next); + pc->next=pa; + pc=pa; + pa=pa->next; + } + else{ + printf("pc->next=pa(%x),pc=pa(%x),pb=pb->next(%x)\n",pb,pb,pa->next); + pc->next=pb; + pc=pb; + pb=pb->next; + } + } + printf("pa与pb中至少有一个为空,循环终止\n"); + pc->next=pa ? pa : pb; + free(Lb); + printf("释放链表Lb\n"); + printf("归并链表结束\n"); +} \ No newline at end of file diff --git a/2017-1/li-yc/List.h b/2017-1/li-yc/List.h new file mode 100644 index 00000000..6344b932 --- /dev/null +++ b/2017-1/li-yc/List.h @@ -0,0 +1,12 @@ +#include + +typedef int ElemType; + +typedef struct LNode{ + ElemType data; + struct LNode *next; +}LNode,*LinkList; + +void creatList(LinkList &L,int n); +void Traverse(LinkList &L); +void MergeList(LinkList &La,LinkList &Lb,LinkList &Lc); \ No newline at end of file diff --git a/2017-1/li-yc/main.cpp b/2017-1/li-yc/main.cpp new file mode 100644 index 00000000..ada2be21 --- /dev/null +++ b/2017-1/li-yc/main.cpp @@ -0,0 +1,39 @@ +#include +#include +#include + +#include "List.h" + +#define length 5 + +int main(){ + LinkList La; + LinkList Lb; + LinkList Lc; + + printf("函数开始执行\n\n"); + srand(time(0)); + + La=(LinkList)malloc(sizeof(LNode)); + printf("开始创建链表La\n"); + creatList(La,length+rand()%5); + printf("对链表La进行遍历"); + printf("\nLa:"); + Traverse(La); + printf("\n\n"); + + Lb=(LinkList)malloc(sizeof(LNode)); + printf("开始创建链表Lb\n"); + creatList(Lb,length+rand()%5); + printf("对链表Lb进行遍历"); + printf("\nLb:"); + Traverse(Lb); + printf("\n\n"); + + printf("\n"); + MergeList(La,Lb,Lc); + printf("合并后的数组Lc为:"); + Traverse(Lc); + printf("\n"); + return 0; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/BiTree.h" "b/2017-1/li-yc/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/BiTree.h" new file mode 100644 index 00000000..a5e8f925 --- /dev/null +++ "b/2017-1/li-yc/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/BiTree.h" @@ -0,0 +1,24 @@ +#include +#include + +typedef enum{ + false, + true, +}bool; +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct BiTNode{ + char data; + struct BiTNode *lchild,*rchild; +}BiTNode,*BiTree; +typedef struct { + BiTree base; + BiTree top; + int stacksize; +}SqStack; + +Status CreateBiTree( BiTree T, char *c); +Status InOrderTraverse( BiTree T, Status (*Print)(char e)); \ No newline at end of file diff --git "a/2017-1/li-yc/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/main.c" "b/2017-1/li-yc/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/main.c" new file mode 100644 index 00000000..6209bea2 --- /dev/null +++ "b/2017-1/li-yc/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/main.c" @@ -0,0 +1,54 @@ +#include +#include + +#include "BiTree.h" + +int i = 0; + +Status CreateBiTree(BiTree *T,char *c){ + char ch; + ch = c[i]; + i++; + if(ch == '#'){ + *T = NULL; + } + else{ + if(!(*T = (BiTNode * )malloc(sizeof(BiTNode)))){ + exit(OVERFLOW); + } + (*T)->data = ch; + CreateBiTree( &(*T)->lchild,c); + CreateBiTree( &(*T)->rchild,c); + } + return OK; +} + +Status PostOrderTraverse(BiTree T){ + if(T){ + PostOrderTraverse( T->lchild); + PostOrderTraverse( T->rchild); + printf( "%c", T->data); + } + return OK; +} + +int main(){ + char c[30]="ABDG###EH##I#K##C#F##"; + char c1[30]="ABD#F###CE###"; + BiTree T=NULL; + printf("开始创建第一个二叉数 : ABDG###EH##I#K##C#F##\n"); + CreateBiTree( &T, c); + printf("创建完毕第一个二叉树\n"); + printf("后序遍历第一个二叉树:\n"); + PostOrderTraverse(T); + printf("\n遍历结束\n\n"); + + i=0; + printf("开始创建第二个二叉数 : ABD#F###CE###\n"); + CreateBiTree( &T, c); + printf("创建完毕第二个二叉树\n"); + printf("后序遍历第二个二叉树:\n"); + PostOrderTraverse(T); + printf("\n遍历结束\n\n"); + return 0; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213.PNG" "b/2017-1/li-yc/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213.PNG" new file mode 100644 index 00000000..4cdd3794 Binary files /dev/null and "b/2017-1/li-yc/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213.PNG" differ diff --git "a/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/List.c" "b/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/List.c" new file mode 100644 index 00000000..1c74ecd3 --- /dev/null +++ "b/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/List.c" @@ -0,0 +1,77 @@ +#include +#include + +#include "List.h" + +Status CreateList(LinkList L, int n){ + printf("开始创建初始链表\n"); + L->next = NULL;//头结点; + int i = 0; + LinkList p; + for (i = n; i > 0; i--){ + p = (LinkList)malloc(sizeof(LNode));//生成新结点; + p->data = i; + p->next = L->next; + L->next = p; + } + printf("链表创建完毕\n"); + return OK; +} +Status Traverse(LinkList L){ + LinkList Line; + Line = (LinkList)malloc(sizeof(LNode)); + if(Line->next == NULL){ + return ERROR; + } + Line = L->next; + while (Line->next != NULL){ + printf("%d ", Line->data); + Line = Line->next; + } + printf("%d", Line->data); + printf("\n遍历结束\n"); + return OK; +} +Status Traverse_loop(LinkList L){ + LinkList p = L; + LinkList q = L; + if(p->next == NULL){ + return ERROR; + } + printf("链表长度为%d\n", q->data); + q = q->next; + while(q->next != p){ + printf("%d\n", q->data); + if(q->next != NULL){ + q = q->next; + } + } + printf("%d", q->data); + printf("\n遍历结束\n"); + return OK; +} +Status List_Distribute(LinkList list,LinkList list1,LinkList list2){ + LinkList p = list; + LinkList p1 = list1; + LinkList p2 = list2; + list1->data = 0; + list2->data = 0; + p = p->next; + printf("开始进行链表分配\n"); + while(p != NULL){ + p1->next = p; + p1 = p1->next; + list1->data++; + p = p->next; + if(p != NULL){ + p2->next = p; + p2 = p1->next; + list2->data++; + p = p->next; + } + } + p1->next = list1; + p2->next = list2; + printf("链表分配结束\n"); + return OK; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/List.h" "b/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/List.h" new file mode 100644 index 00000000..7d1b273e --- /dev/null +++ "b/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/List.h" @@ -0,0 +1,20 @@ +#include +#include + +typedef int ElemType; +typedef struct LNode{ + ElemType data; + struct LNode *next; +}LNode, *LinkList; +typedef enum { + OK, + ERROR, + OVERFLOW +} Status; + +Status CreateList(LinkList L, int n); +Status Traverse(LinkList L); +Status Traverse_loop(LinkList L); +Status List_Distribute(LinkList list,LinkList list1,LinkList list2); +/*创建了三个链表,然后在List_Distribute中创立了三个指向结构体的指针,然后一个个分配,达到了时间利用的最少性,指针用完也释放 +也最少利用空间了*/ \ No newline at end of file diff --git "a/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/main.c" "b/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/main.c" new file mode 100644 index 00000000..c968c020 --- /dev/null +++ "b/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/main.c" @@ -0,0 +1,33 @@ +#include +#include + +# include "List.h" + +int main(){ + LinkList La; + LinkList L1; + LinkList L2; + + La = (LinkList)malloc(sizeof(LNode)); + L1 = (LinkList)malloc(sizeof(LNode)); + L2 = (LinkList)malloc(sizeof(LNode)); + + printf("生成链表长度\n%d\n",20); + + CreateList(La, 20); + + printf_s("开始遍历链表La:\n"); + Traverse(La); + printf("\n"); + + List_Distribute(La, L1, L2); + printf("\n"); + + printf("开始遍历奇数链表\n"); + Traverse_loop(L1); + printf("\n"); + + printf("开始遍历偶数链表\n"); + Traverse_loop(L2); + printf_s("\n"); +} \ No newline at end of file diff --git "a/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/\346\210\252\345\233\2761.png" "b/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/\346\210\252\345\233\2761.png" new file mode 100644 index 00000000..005e8c2c Binary files /dev/null and "b/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/\346\210\252\345\233\2761.png" differ diff --git "a/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/\346\210\252\345\233\2762.png" "b/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/\346\210\252\345\233\2762.png" new file mode 100644 index 00000000..ae6eca86 Binary files /dev/null and "b/2017-1/li-yc/\344\275\234\344\270\232\347\254\254\345\215\201\344\270\211\351\242\230/\346\210\252\345\233\2762.png" differ diff --git "a/2017-1/li-yc/\345\256\236\351\252\2142-12/List.c" "b/2017-1/li-yc/\345\256\236\351\252\2142-12/List.c" new file mode 100644 index 00000000..8c89cefc --- /dev/null +++ "b/2017-1/li-yc/\345\256\236\351\252\2142-12/List.c" @@ -0,0 +1,71 @@ +#include +#include +#include + +#include"List.h" + +void creatList(LinkList L,int n){ + printf("\nCreatList:\n"); + printf("开始创建长度为%d的单链表\n",n); + + int i; + int number=20; + LinkList p; + p=NULL; + + L->next=NULL; + + for(i=n;i>0;i--){ + p=(LinkList)malloc(sizeof(LNode)); + number=number-(rand()%n+1); + p->data=number; + p->next=L->next; + L->next=p; + } + printf("链表创建完毕\n"); +} + +void Traverse(LinkList L){ + LinkList a=L; + a=a->next; + while(a){ + printf("%d",a->data); + printf(" "); + a=a->next; + } + printf("\n"); + printf("遍历完成"); +} + +void MergeList(LinkList La,LinkList Lb,LinkList Lc){ + printf("MergeList\n"); + printf("开始归并链表La和Lb\n"); + LinkList pa, pb, pc; + + pa=pb=pc=NULL; + pa=La->next; + pb=Lb->next; + Lc=pc=La; + + while(pa&&pb){ + printf("pa与pb都不为空链表,循环继续执行\n"); + printf("pa->data(%d)%spb->data(%d),所以\n",pa->data,pa->data<=pb->data ? "<=" : ">",pb->data); + if(pa->data<=pb->data){ + printf("pc->next=pa(%x),pc=pa(%x),pa=pa->next(%x)\n",pa,pa,pa->next); + pc->next=pa; + pc=pa; + pa=pa->next; + } + else{ + printf("pc->next=pa(%x),pc=pa(%x),pb=pb->next(%x)\n",pb,pb,pa->next); + pc->next=pb; + pc=pb; + pb=pb->next; + } + } + printf("pa与pb中至少有一个为空,循环终止\n"); + pc->next=pa ? pa : pb; + free(Lb); + printf("释放链表Lb\n"); + printf("归并链表结束\n"); +} \ No newline at end of file diff --git "a/2017-1/li-yc/\345\256\236\351\252\2142-12/List.h" "b/2017-1/li-yc/\345\256\236\351\252\2142-12/List.h" new file mode 100644 index 00000000..6e23492e --- /dev/null +++ "b/2017-1/li-yc/\345\256\236\351\252\2142-12/List.h" @@ -0,0 +1,12 @@ +#include + +typedef int ElemType; + +typedef struct LNode{ + ElemType data; + struct LNode *next; +}LNode,*LinkList; + +void creatList(LinkList L,int n); +void Traverse(LinkList L); +void MergeList(LinkList La,LinkList Lb,LinkList Lc); \ No newline at end of file diff --git "a/2017-1/li-yc/\345\256\236\351\252\2142-12/main.c" "b/2017-1/li-yc/\345\256\236\351\252\2142-12/main.c" new file mode 100644 index 00000000..ada2be21 --- /dev/null +++ "b/2017-1/li-yc/\345\256\236\351\252\2142-12/main.c" @@ -0,0 +1,39 @@ +#include +#include +#include + +#include "List.h" + +#define length 5 + +int main(){ + LinkList La; + LinkList Lb; + LinkList Lc; + + printf("函数开始执行\n\n"); + srand(time(0)); + + La=(LinkList)malloc(sizeof(LNode)); + printf("开始创建链表La\n"); + creatList(La,length+rand()%5); + printf("对链表La进行遍历"); + printf("\nLa:"); + Traverse(La); + printf("\n\n"); + + Lb=(LinkList)malloc(sizeof(LNode)); + printf("开始创建链表Lb\n"); + creatList(Lb,length+rand()%5); + printf("对链表Lb进行遍历"); + printf("\nLb:"); + Traverse(Lb); + printf("\n\n"); + + printf("\n"); + MergeList(La,Lb,Lc); + printf("合并后的数组Lc为:"); + Traverse(Lc); + printf("\n"); + return 0; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\345\256\236\351\252\2142-12/\346\210\252\345\261\217\344\270\200.png" "b/2017-1/li-yc/\345\256\236\351\252\2142-12/\346\210\252\345\261\217\344\270\200.png" new file mode 100644 index 00000000..6ab564eb Binary files /dev/null and "b/2017-1/li-yc/\345\256\236\351\252\2142-12/\346\210\252\345\261\217\344\270\200.png" differ diff --git "a/2017-1/li-yc/\345\256\236\351\252\2142-12/\346\210\252\345\261\217\344\272\214.png" "b/2017-1/li-yc/\345\256\236\351\252\2142-12/\346\210\252\345\261\217\344\272\214.png" new file mode 100644 index 00000000..f37e06af Binary files /dev/null and "b/2017-1/li-yc/\345\256\236\351\252\2142-12/\346\210\252\345\261\217\344\272\214.png" differ diff --git "a/2017-1/li-yc/\345\256\236\351\252\2143-1/3-2-1.PNG" "b/2017-1/li-yc/\345\256\236\351\252\2143-1/3-2-1.PNG" new file mode 100644 index 00000000..5d1a9491 Binary files /dev/null and "b/2017-1/li-yc/\345\256\236\351\252\2143-1/3-2-1.PNG" differ diff --git "a/2017-1/li-yc/\345\256\236\351\252\2143-1/3-2-1.c" "b/2017-1/li-yc/\345\256\236\351\252\2143-1/3-2-1.c" new file mode 100644 index 00000000..db876c46 --- /dev/null +++ "b/2017-1/li-yc/\345\256\236\351\252\2143-1/3-2-1.c" @@ -0,0 +1,58 @@ +#include +#include +#include + +#include "3-2-1.h" + +Status InitStack(SqStack *s){ + printf("初始化一个栈\n"); + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if(!s->base) + return OVERFLOW; + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + printf("初始化一个栈完毕\n"); +} + +Status Push(SqStack *s,SElemType e){ + if(s->top - s->base > s->stacksize){ + return OVERFLOW; + } + printf("插入元素 %d 作为栈顶元素\n",e); + *s->top++ = e; + return OK; +} + +Status Pop(SqStack *s,SElemType *e){ + if(StackEmpty(s)){ + return ERROR; + } + *e = *--s->top; + return OK; +} + +bool StackEmpty(SqStack *s){ + if(s) { + return s->base == s->top; + } + return true; +} + +Status conversion(SqStack *s,int input,int d ){ + SElemType e; + + if(d > 10){ + return ERROR; + } + while(input){ + Push(s, input % d); + input = input / d; + } + printf("转化后的数字:\n"); + while(!StackEmpty(s)){ + Pop(s, &e); + printf("%d", e); + } + printf("\n"); + printf("转化结束\n"); +} \ No newline at end of file diff --git "a/2017-1/li-yc/\345\256\236\351\252\2143-1/3-2-1.h" "b/2017-1/li-yc/\345\256\236\351\252\2143-1/3-2-1.h" new file mode 100644 index 00000000..13b388e8 --- /dev/null +++ "b/2017-1/li-yc/\345\256\236\351\252\2143-1/3-2-1.h" @@ -0,0 +1,28 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef int SElemType; +typedef enum{ + false, + true, +}bool; +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack(SqStack *s); +Status Push(SqStack *s,SElemType e); +Status Pop(SqStack *s,SElemType *e); +Status conversion(SqStack *s,int input,int d ); +bool StackEmpty(SqStack *s); \ No newline at end of file diff --git "a/2017-1/li-yc/\345\256\236\351\252\2143-1/main(3-2-1).c" "b/2017-1/li-yc/\345\256\236\351\252\2143-1/main(3-2-1).c" new file mode 100644 index 00000000..d5f0e140 --- /dev/null +++ "b/2017-1/li-yc/\345\256\236\351\252\2143-1/main(3-2-1).c" @@ -0,0 +1,24 @@ +#include +#include +#include + +#include "3-2-1.h" + +int main(){ + SqStack s; + int input; + int d; + srand(time(0)); + input=rand(); + d=rand()%10; + if(d>10){ + printf("输入的数字不符合转换进制的要求\n"); + printf("请重新输入要转换的数\n"); + scanf_s("%d",&d); + } + printf("要转换的数据:%d\n",input); + printf("要转化的进制:%d\n",d); + InitStack(&s);//初始化一个栈 + conversion(&s, input, d); + return 0; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\345\256\236\351\252\2143-2/3-2-2.c" "b/2017-1/li-yc/\345\256\236\351\252\2143-2/3-2-2.c" new file mode 100644 index 00000000..08e07a2f --- /dev/null +++ "b/2017-1/li-yc/\345\256\236\351\252\2143-2/3-2-2.c" @@ -0,0 +1,119 @@ +#include +#include +#include +#include + +#include "3-2-2.h" + +Status InitStack(SqStack *s){ + printf("初始化一个栈\n"); + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if(!s->base) + return OVERFLOW; + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + printf("初始化一个栈完毕\n\n"); +} + +Status Push(SqStack *s,char e){ + printf("插入新的字符 %c 到栈中\n",e); + if(s->top - s->base > s->stacksize){ + return OVERFLOW; + } + *s->top++ = e; + return OK; +} + +Status Pop(SqStack *s,char *e){ + if(isStackEmpty(s)){ + return ERROR; + } + printf("将 %c 作出栈处理\n",*e); + *e = *--s->top; + return OK; +} + +bool isStackEmpty(SqStack *s){ + if(s->base==s->top){ + return true; + } + else{ + return false; + } +} +Status GetTop(SqStack s,char *e){ + if(s.top==s.base){ + return ERROR; + } + printf("栈顶字符为 %c\n",*e); + *e=*(s.top-1); + return OK; +} +Status matching(char *exp){ + SqStack s; + InitStack(&s); + int i=0; + int state=1; + int length=0; + length=strlen(exp); + while(i +#include +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef char SElemType; +typedef enum{ + false, + true, +}bool; +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack(SqStack *s); +Status Push(SqStack *s,SElemType e); +Status Pop(SqStack *s,SElemType *e); +bool isStackEmpty(SqStack *s); +Status GetTop(SqStack s,char *e); +Status matching(char *exp); +void Traverse(char *exp); \ No newline at end of file diff --git "a/2017-1/li-yc/\345\256\236\351\252\2143-2/3-2-3.png" "b/2017-1/li-yc/\345\256\236\351\252\2143-2/3-2-3.png" new file mode 100644 index 00000000..0f442c0e Binary files /dev/null and "b/2017-1/li-yc/\345\256\236\351\252\2143-2/3-2-3.png" differ diff --git "a/2017-1/li-yc/\345\256\236\351\252\2143-2/main(3-2-2).c" "b/2017-1/li-yc/\345\256\236\351\252\2143-2/main(3-2-2).c" new file mode 100644 index 00000000..fe360fc4 --- /dev/null +++ "b/2017-1/li-yc/\345\256\236\351\252\2143-2/main(3-2-2).c" @@ -0,0 +1,19 @@ +#include +#include +#include +#include + +#include "3-2-2.h" + +int main(){ + char test[10]={'\0'}; + char ch[6]={'(',')','{','}','[',']'}; + srand(time(0)); + for(int i=0;i<6;i++){ + test[i]=ch[rand()%6]; + } + printf("the character string is :\n"); + Traverse(test); + matching(test); + return 0; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\345\256\236\351\252\2143-3/3-2-3.c" "b/2017-1/li-yc/\345\256\236\351\252\2143-3/3-2-3.c" new file mode 100644 index 00000000..72203b9f --- /dev/null +++ "b/2017-1/li-yc/\345\256\236\351\252\2143-3/3-2-3.c" @@ -0,0 +1,88 @@ +#include +#include + +#include "3-2-3.h" + +Status InitStack(SqStack *s){ + printf("开始初始化一个栈\n"); + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if(!s->base) + return OVERFLOW; + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + printf("初始化一个栈结束\n\n"); +} + +Status Push(SqStack *s,char e){ + printf("插入元素 %c为栈顶元素\n",e); + if(s->top - s->base > s->stacksize){ + return OVERFLOW; + } + *s->top++ = e; + return OK; +} + +Status Pop(SqStack *s,char *e){ + if(StackEmpty(s)){ + return ERROR; + } + printf("清除前一个元素\n"); + *e = *--s->top; + return OK; +} + +bool StackEmpty(SqStack *s){ + if(s) { + return s->base == s->top; + } + return true; +} +Status ClearStack(SqStack *s){ + printf("清除当行前面的所有元素\n"); + s->top=s->base; + return OK; +} +Status LineEdit(SqStack *s){ + char str[100]={'\0'}; + char c; + char ch; + int i=0; + printf("开始输入字符串:\n"); + ch=getchar(); + while(ch!= ';'){ + while(ch!=';'&&ch!='\n'){ + switch(ch){ + case '#':Pop(s,&c); + printf("\n"); + break; + case '@':ClearStack(s); + printf("\n"); + break; + default: + Push(s,ch); + } + ch=getchar(); + } + while(s->top!=s->base){ + str[i]=*(s->base); + s->base++; + i++; + } + printf("经过整理后的字符串:\n"); + puts(str); + ClearStack(s); + if(ch!=';'){ + ch=getchar(); + } + } + DestroyStack(s); + return OK; +} +Status DestroyStack(SqStack *s){ + while(!StackEmpty(s)){ + free(s->top); + s->top--; + } + s->base=NULL; + return OK; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\345\256\236\351\252\2143-3/3-2-3.h" "b/2017-1/li-yc/\345\256\236\351\252\2143-3/3-2-3.h" new file mode 100644 index 00000000..f9cbef18 --- /dev/null +++ "b/2017-1/li-yc/\345\256\236\351\252\2143-3/3-2-3.h" @@ -0,0 +1,29 @@ +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef int SElemType; +typedef enum{ + false, + true, +}bool; +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack(SqStack *s); +Status Push(SqStack *s,SElemType e); +Status Pop(SqStack *s,SElemType *e); +bool StackEmpty(SqStack *s); +Status DestroyStack(SqStack *s); +Status ClearStack(SqStack *s); +Status LineEdit(SqStack *s); \ No newline at end of file diff --git "a/2017-1/li-yc/\345\256\236\351\252\2143-3/3-3(1).PNG" "b/2017-1/li-yc/\345\256\236\351\252\2143-3/3-3(1).PNG" new file mode 100644 index 00000000..026a7a5e Binary files /dev/null and "b/2017-1/li-yc/\345\256\236\351\252\2143-3/3-3(1).PNG" differ diff --git "a/2017-1/li-yc/\345\256\236\351\252\2143-3/3-3(2).PNG" "b/2017-1/li-yc/\345\256\236\351\252\2143-3/3-3(2).PNG" new file mode 100644 index 00000000..cf500d29 Binary files /dev/null and "b/2017-1/li-yc/\345\256\236\351\252\2143-3/3-3(2).PNG" differ diff --git "a/2017-1/li-yc/\345\256\236\351\252\2143-3/main(3-2-3).c" "b/2017-1/li-yc/\345\256\236\351\252\2143-3/main(3-2-3).c" new file mode 100644 index 00000000..0744f1fa --- /dev/null +++ "b/2017-1/li-yc/\345\256\236\351\252\2143-3/main(3-2-3).c" @@ -0,0 +1,11 @@ +#include +#include + +#include "3-2-3.h" + +int main(){ + SqStack s; + InitStack(&s); + LineEdit(&s); + return 0; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" "b/2017-1/li-yc/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" new file mode 100644 index 00000000..35bb8552 --- /dev/null +++ "b/2017-1/li-yc/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" @@ -0,0 +1,11 @@ +8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30 + +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30 + +7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30 + +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30 + +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30 + +7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30 diff --git "a/2017-1/li-yc/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BiTree.c" "b/2017-1/li-yc/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BiTree.c" new file mode 100644 index 00000000..e13780f0 --- /dev/null +++ "b/2017-1/li-yc/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BiTree.c" @@ -0,0 +1,118 @@ +#include "BiTree.h" + +int max = 0; + +bool EQ(KeyType kval, KeyType data){ + if(kval == data){ + return true; + } + else{ + return false; + } +} + +bool LT(KeyType kval, KeyType data){ + if(kval != data && kval < data){ + return true; + } + else{ + return false; + } +} + +Status preOrderTraverse(BiTree T){ + if(T){ + printf("%d", T->data); + if(T->data != max){ + printf(", "); + } + preOrderTraverse( T->lchild); + preOrderTraverse( T->rchild); + return OK; + } + else{ + return ERROR; + } +} + +bool SearchBST(BiTree T, KeyType kval, BiTree f, BiTree *p){ + if(!T){ + *p = f; + return false; + }else if(EQ(kval, T->data)){ + *p = T; + return true; + }else if(LT(kval, T->data)){ + return SearchBST( T->lchild, kval, T, p); + }else{ + return SearchBST( T->rchild, kval, T, p); + } +} + +bool InsertBST(BiTree *T, KeyType e, BiTree p){ + BiTree s = NULL; + if(max < e){ + max = e; + } + if(!(s = (BiTNode * )malloc(sizeof(BiTNode)))){ + exit(OVERFLOW); + } + if(!(p = (BiTNode * )malloc(sizeof(BiTNode)))){ + exit(OVERFLOW); + } + if(!SearchBST(*T, e, NULL, &p)){ + s->data = e; + s->lchild = s->rchild = NULL; + if(!p){ + *T = s; + }else if(LT(e, p->data)){ + p->lchild = s; + }else{ + p->rchild = s; + } + return true; + }else{ + return false; + } +} + +Status Delete(BiTree *p){ + BiTree q = NULL; + BiTree s = NULL; + if(!(*p)->rchild){ + q = *p; + (*p) = (*p)->lchild; + }else if(!(*p)->lchild){ + q = *p; + (*p) = (*p)->rchild; + }else{ + q = *p; + s = (*p)->lchild; + while(s->rchild){ + q = s; + s = s->rchild; + } + (*p)->data = s->data; + if(q != (*p)){ + q->rchild = s->lchild; + }else{ + q->rchild = s->lchild; + } + } + return OK; +} + +bool DeleteBST(BiTree *T, KeyType kval){ + if(!T){ + return false; + }else{ + if(EQ(kval, ( *T )->data)){ + Delete(T); + return true; + }else if(LT(kval, ( *T )->data)){ + return DeleteBST(&(*T)->lchild, kval); + }else{ + return DeleteBST(&(*T)->rchild, kval); + } + } +} \ No newline at end of file diff --git "a/2017-1/li-yc/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BiTree.h" "b/2017-1/li-yc/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BiTree.h" new file mode 100644 index 00000000..c0da105a --- /dev/null +++ "b/2017-1/li-yc/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BiTree.h" @@ -0,0 +1,25 @@ +#include +#include + +typedef int KeyType; +typedef enum{ + false, + true, +}bool; +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct BiTNode{ + int data; + struct BiTNode *lchild,*rchild; +}BiTNode,*BiTree; + +bool EQ(KeyType kval, KeyType data); +bool LT(KeyType kval, KeyType data); +Status preOrderTraverse(BiTree T); +bool SearchBST(BiTree T, KeyType kval, BiTree f, BiTree *p); +bool InsertBST(BiTree *T, KeyType e, BiTree p); +Status Delete(BiTree *p); +bool DeleteBST(BiTree *T, KeyType kval); \ No newline at end of file diff --git "a/2017-1/li-yc/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/main.c" "b/2017-1/li-yc/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/main.c" new file mode 100644 index 00000000..cd9f3ccc --- /dev/null +++ "b/2017-1/li-yc/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/main.c" @@ -0,0 +1,30 @@ +#include "BiTree.h" + +int main(){ + char c[30] = {8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30}; + char c1[10] = {13, 8, 5, 20, 6}; + int i; + bool flag = 0; + BiTree T = NULL; + BiTree p = NULL; + + for(i=0; i<12; i++){ + InsertBST(&T, c[i], p); + } + + preOrderTraverse(T); + printf("\n\n"); + + for(i=0; i<5; i++){ + flag = SearchBST(T, c1[i], NULL, &p); + if(flag == true){ + DeleteBST(&T, c1[i]); + } + else{ + InsertBST(&T, c1[i], p); + } + preOrderTraverse(T); + printf("\n\n"); + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(1).PNG" "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(1).PNG" new file mode 100644 index 00000000..7b7db173 Binary files /dev/null and "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(1).PNG" differ diff --git "a/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(2).PNG" "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(2).PNG" new file mode 100644 index 00000000..d858c641 Binary files /dev/null and "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(2).PNG" differ diff --git "a/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(3).PNG" "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(3).PNG" new file mode 100644 index 00000000..2f311cc3 Binary files /dev/null and "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(3).PNG" differ diff --git "a/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(4).PNG" "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(4).PNG" new file mode 100644 index 00000000..d194f674 Binary files /dev/null and "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(4).PNG" differ diff --git "a/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(5).PNG" "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(5).PNG" new file mode 100644 index 00000000..8c026ad4 Binary files /dev/null and "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(5).PNG" differ diff --git "a/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(6).PNG" "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(6).PNG" new file mode 100644 index 00000000..bd7eb35e Binary files /dev/null and "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(6).PNG" differ diff --git "a/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(7).PNG" "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(7).PNG" new file mode 100644 index 00000000..aa849a2f Binary files /dev/null and "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4(7).PNG" differ diff --git "a/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4.c" "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4.c" new file mode 100644 index 00000000..10fc47a7 --- /dev/null +++ "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4.c" @@ -0,0 +1,199 @@ +#include +#include +#include + +#include "3-4.h" + +Status InitStack(SqStack *s){ + printf("初始化一个栈\n"); + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if(!s->base) + return OVERFLOW; + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + printf("初始化一个栈完毕\n\n"); +} +Status Push(SqStack *s,char e){ + if(s->top - s->base > s->stacksize){ + return OVERFLOW; + } + *s->top++ = e; + return OK; +} +Status Pop(SqStack *s,char *e){ + if(StackEmpty(s)){ + return ERROR; + } + *e = *--s->top; + return OK; +} +bool StackEmpty(SqStack *s){ + if(s->base==s->top){ + return true; + } + else{ + return false; + } +} +Status GetTop(SqStack s,char *e){ + if(s.top==s.base){ + return ERROR; + } + *e=*(s.top-1); + printf("栈顶字符为 %c\n",*e); + return OK; +} +void Traverse(SqStack s){ + printf("开始遍历:\n"); + while(!StackEmpty(&s)){ + printf("%c\n",*(s.top-1)); + --s.top; + } +} +bool IN(char ch){ + if(ch<='100'&&ch>='0'){ + return false; + } + else{ + return true; + } +} +char Precede(char exist,char read){ + switch(exist){ + case '+': + if(read=='+'||read=='-'||read==')'||read=='#'){ + return '>'; + } + else{ + return '<'; + } + break; + case '-': + if(read=='+'||read=='-'||read==')'||read=='#'){ + return '>'; + } + else{ + return '<'; + } + break; + case '*': + if(read=='('){ + return '<'; + } + else{ + return '>'; + } + break; + case '/': + if(read=='('){ + return '<'; + } + else{ + return '>'; + } + break; + case '(': + if(read==')'){ + return '='; + } + else{ + return '<'; + } + break; + case ')': + return '>'; + break; + case '#': + if(read=='#'){ + return '='; + } + else{ + return '<'; + } + break; + } + return OK; +} +int EvaluateExpression(char *exp){ + SqStack OPTR; + SqStack OPND; + char e; + char a,b,theta; + char x; + int len; + int i=0; + len=strlen(exp); + exp[len]='#'; + + InitStack(&OPTR); + InitStack(&OPND); + Push(&OPTR,'#'); + GetTop(OPTR,&e); + while(exp[i]!='#'||e!='#'){ + if(!IN(exp[i])){ + printf("插入 %c 到操作数栈中\n",exp[i]); + Push(&OPND,exp[i]); + i++; + } + else{ + switch(Precede(e,exp[i])){ + case '<': + printf("插入 %c 到运算符栈中\n",exp[i]); + Push(&OPTR,exp[i]); + i++; + break; + case '=': + Pop(&OPTR,&x); + printf("将 %c 作出栈处理\n",x); + i++; + break; + case '>': + printf("\n进行合并前进行遍历:\n"); + printf("------------------------------------\n"); + Traverse(OPTR); + Traverse(OPND); + printf("------------------------------------\n\n"); + Pop(&OPTR,&theta); + if(*(OPTR.top-1)=='('){ + Pop(&OPTR,&x); + exp[i]='#'; + } + printf("将 %c 作出栈处理\n",theta); + Pop(&OPND,&b); + printf("将 %c 作出栈处理\n",b); + Pop(&OPND,&a); + printf("将 %c 作出栈处理\n",a); + Push(&OPND,Operate(a,theta,b)); + printf("\n进行合并后进行遍历:\n"); + printf("------------------------------------\n"); + Traverse(OPTR); + Traverse(OPND); + printf("------------------------------------\n\n"); + if(OPTR.base==OPTR.top){ + exp[i]='#'; + } + break; + } + } + GetTop(OPTR,&e); + } + GetTop(OPND,&e); + printf("运算结果为%d\n",e-'0'); + return e-'0'; +} +char Operate(char a,char thete,char b){ + switch(thete){ + case '+': + return (a-'0')+(b-'0')+'0'; + break; + case '-': + return (a-'0')-(b-'0')+'0'; + break; + case '*': + return (a-'0')*(b-'0')+'0'; + break; + case '/': + return (a-'0')/(b-'0')+'0'; + break; + } +} \ No newline at end of file diff --git "a/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4.h" "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4.h" new file mode 100644 index 00000000..2d5d5158 --- /dev/null +++ "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-4.h" @@ -0,0 +1,33 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef char SElemType; +typedef enum{ + false, + true, +}bool; +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack(SqStack *s); +Status Push(SqStack *s,char e); +Status Pop(SqStack *s,char *e); +bool StackEmpty(SqStack *s); +Status GetTop(SqStack s,char *e); +void Traverse(char *exp); +bool IN(char ch); +char Precede(char exist,char read); +int EvaluateExpression(char *exp); +char Operate(char a,char thete,char b); diff --git "a/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/main.c" "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/main.c" new file mode 100644 index 00000000..82989f11 --- /dev/null +++ "b/2017-1/li-yc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/main.c" @@ -0,0 +1,19 @@ +#include +#include +#include + +#include "3-4.h" + + +int main(){ + int a; + char str[20]={"3*(7-2)"}; + char str1[20]={"6+9/3+6*2-1"}; + printf("开始进行%s的计算\n",str); + EvaluateExpression(str); + printf("计算完毕\n\n"); + printf("开始进行%s的计算\n",str1); + EvaluateExpression(str1); + printf("计算完毕\n\n"); + return 0; +} diff --git "a/2017-1/li-yc/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\351\253\230\345\272\246\345\222\214\345\256\275\345\272\246-\350\256\241\347\256\227\345\217\266\347\273\223\347\202\271/2017-04-27.PNG" "b/2017-1/li-yc/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\351\253\230\345\272\246\345\222\214\345\256\275\345\272\246-\350\256\241\347\256\227\345\217\266\347\273\223\347\202\271/2017-04-27.PNG" new file mode 100644 index 00000000..5d7b5923 Binary files /dev/null and "b/2017-1/li-yc/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\351\253\230\345\272\246\345\222\214\345\256\275\345\272\246-\350\256\241\347\256\227\345\217\266\347\273\223\347\202\271/2017-04-27.PNG" differ diff --git "a/2017-1/li-yc/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\351\253\230\345\272\246\345\222\214\345\256\275\345\272\246-\350\256\241\347\256\227\345\217\266\347\273\223\347\202\271/BiTree.h" "b/2017-1/li-yc/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\351\253\230\345\272\246\345\222\214\345\256\275\345\272\246-\350\256\241\347\256\227\345\217\266\347\273\223\347\202\271/BiTree.h" new file mode 100644 index 00000000..9980959d --- /dev/null +++ "b/2017-1/li-yc/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\351\253\230\345\272\246\345\222\214\345\256\275\345\272\246-\350\256\241\347\256\227\345\217\266\347\273\223\347\202\271/BiTree.h" @@ -0,0 +1,28 @@ +#include +#include + +typedef enum{ + false, + true, +}bool; +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct BiTNode{ + char data; + struct BiTNode *lchild,*rchild; +}BiTNode,*BiTree; +typedef struct { + BiTree base; + BiTree top; + int stacksize; +}SqStack; + +Status CreateBiTree( BiTree *T, char *c);//创建二叉树 +Status InOrderTraverse( BiTree T, Status (*Print)(char e));//后序遍历二叉树 +Status MeasureLength(BiTree T);//计算二叉树深度 +Status MeasureWidth(BiTree T,int length);//计算二叉树每层宽度 +Status GetWidth();//得出二叉树最大宽度 +Status MeasureLeaf(BiTree T,int *count1,int *count2);//计算叶子节点和非叶子节点 \ No newline at end of file diff --git "a/2017-1/li-yc/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\351\253\230\345\272\246\345\222\214\345\256\275\345\272\246-\350\256\241\347\256\227\345\217\266\347\273\223\347\202\271/main.c" "b/2017-1/li-yc/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\351\253\230\345\272\246\345\222\214\345\256\275\345\272\246-\350\256\241\347\256\227\345\217\266\347\273\223\347\202\271/main.c" new file mode 100644 index 00000000..0dede555 --- /dev/null +++ "b/2017-1/li-yc/\350\256\241\347\256\227\344\272\214\345\217\211\346\240\221\347\232\204\351\253\230\345\272\246\345\222\214\345\256\275\345\272\246-\350\256\241\347\256\227\345\217\266\347\273\223\347\202\271/main.c" @@ -0,0 +1,132 @@ +#include +#include + +#include "BiTree.h" + +int i = 0; +int length = 0;//深度 +int testlength = 0; +int width[10] = {0};//宽度 + +Status CreateBiTree(BiTree *T,char *c){ + char ch; + ch = c[i]; + i++; + if(ch == '#'){ + *T = NULL; + } + else{ + if(!(*T = (BiTNode * )malloc(sizeof(BiTNode)))){ + exit(OVERFLOW); + } + (*T)->data = ch; + CreateBiTree( &(*T)->lchild,c); + CreateBiTree( &(*T)->rchild,c); + } + return OK; +} + +Status PostOrderTraverse(BiTree T){ + if(T){ + PostOrderTraverse( T->lchild); + PostOrderTraverse( T->rchild); + printf( "%c", T->data); + } + return OK; +} + +Status MeasureLength(BiTree T){ + if(!T){ + if(testlength > length){ + length = testlength; + } + return OK; + } + else{ + testlength++; + MeasureLength(T->lchild); + MeasureLength(T->rchild); + testlength--; + } + if(testlength > length){ + length = testlength; + } + return OK; +} + +Status MeasureWidth(BiTree T,int len){ + if(!T){ + return ERROR; + } + else{ + width[len] = width[len]+1; + len++; + } + MeasureWidth(T->lchild, len); + MeasureWidth(T->rchild, len); + return OK; +} + +Status GetWidth(){ + int wi = 0; + int i; + for(i = 1;i <= length;i++){ + if(wilchild == NULL&&T->rchild == NULL){ + *(count1) = *(count1)+1; + } + else{ + *(count2) = *(count2)+1; + } + MeasureLeaf(T->lchild, count1, count2); + MeasureLeaf(T->rchild, count1, count2); + } + return OK; +} + +int main(){ + int count1=0; + int count2=0; + char c[30]="ABDG###EH##I#K##C#F##"; + char c1[30]="ABD#F###CE###"; + BiTree T=NULL; + printf("开始创建第一个二叉数 : ABDG###EH##I#K##C#F##\n"); + CreateBiTree( &T, c); + MeasureLength(T); + printf("创建完毕第一个二叉树\n二叉树的高度为%d\n",length); + MeasureWidth(T, 1); + GetWidth(); + printf("后序遍历第一个二叉树:\n"); + PostOrderTraverse(T); + printf("\n遍历结束\n"); + MeasureLeaf( T, &count1, &count2); + printf( "该二叉树叶子节点为%d,非叶子节点为%d\n\n", count1, count2); + + i=0; + length=0; + testlength=0; + count1=0; + count2=0; + printf("开始创建第二个二叉数 : ABD#F###CE###\n"); + CreateBiTree( &T, c1); + MeasureLength(T); + printf("创建完毕第二个二叉树\n二叉树的高度为%d\n",length); + MeasureWidth(T, 1); + GetWidth(); + printf("后序遍历第二个二叉树:\n"); + PostOrderTraverse(T); + printf("\n遍历结束\n"); + MeasureLeaf( T, &count1, &count2); + printf( "该二叉树叶子节点为%d,非叶子节点为%d\n\n", count1, count2); + return 0; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/3-4QUEEN.c" "b/2017-1/li-yc/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/3-4QUEEN.c" new file mode 100644 index 00000000..a2d704ff --- /dev/null +++ "b/2017-1/li-yc/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/3-4QUEEN.c" @@ -0,0 +1,106 @@ +#include +#include + +#include "3-4QUEEN.h" + +Status InitQueue(LinkQueue q){ + q.front = q.rear = (QueuePtr)malloc(sizeof(QNode)); + if(!q.front){ + exit(OVERFLOW); + } + q.front->next = NULL; + printf("初始化完毕\n"); + return OK; +} +Status DestroyQueue(LinkQueue q){ + while(q.front){ + q.rear = q.front->next; + free(q.front); + q.front = q.rear; + } + return OK; +} +Status EnQueue(LinkQueue *q,QElemType e){ + QueuePtr p; + p=(QueuePtr)malloc(sizeof(QNode)); + if(!p){ + exit(OVERFLOW); + } + p->data = e; + p->next = NULL; + q->rear->next = p; + q->rear->data = p->data; + q->rear = p; + return OK; +} +bool QueueEmpty(LinkQueue q){ + if(q.front == q.rear){ + return true; + } + else{ + return false; + } +} +Status DeQueue(LinkQueue *q,QElemType *e){ + QueuePtr p; + p=(QueuePtr)malloc(sizeof(QNode)); + if(q->front == q->rear){ + return ERROR; + } + p = q->front; + *e = p->data; + q->front = q->front->next; + if(q->rear == p){ + q->rear = q->front; + } + free(p); + return OK; +} +Status CleanQueue(LinkQueue q){ + QueuePtr p; + QueuePtr next; + if(QueueEmpty(q)){ + printf("将队列清空为空对列\n"); + return OK; + } + else{ + for(p = q.front; p; p = next){ + next = p->next; + free(p); + p = next; + } + } + printf("将队列清空为空对列\n"); + return OK; +} +int QueueLength(LinkQueue q){ + QueuePtr p; + QueuePtr next; + int length = 0; + p = q.front; + while(p != q.rear){ + p = p->next; + length++; + } + return length; +} +Status GetHead(LinkQueue q,QElemType *e){ + *e = q.front->data; + printf("此时队头元素为%d\n",*e); + return OK; +} +Status TraverseQueue(LinkQueue q){ + QueuePtr p; + printf("开始遍历:\n"); + if(QueueEmpty(q)){ + printf("此时队列里不存在元素\n"); + return OK; + } + p = q.front; + while(p != q.rear){ + printf("%d ",p->data); + p = p->next; + } + printf("\n"); + return OK; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/3-4QUEEN.h" "b/2017-1/li-yc/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/3-4QUEEN.h" new file mode 100644 index 00000000..95fa5afe --- /dev/null +++ "b/2017-1/li-yc/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/3-4QUEEN.h" @@ -0,0 +1,31 @@ +#include +#include + +typedef int QElemType; +typedef enum{ + false, + true, +}bool; +typedef enum { + OK, + OVERFLOW, + ERROR, +} Status; +typedef struct QNode{ + QElemType data; + struct QNode *next; +}QNode,*QueuePtr; +typedef struct LinkQueue{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +Status InitQueue(LinkQueue q); +Status DestroyQueue(LinkQueue q); +Status EnQueue(LinkQueue *q,QElemType e); +bool QueueEmpty(LinkQueue q); +Status DeQueue(LinkQueue *q,QElemType *e); +Status CleanQueue(LinkQueue q); +int QueueLength(LinkQueue q); +Status GetHead(LinkQueue q,QElemType *e); +Status TraverseQueue(LinkQueue q); \ No newline at end of file diff --git "a/2017-1/li-yc/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main.c" "b/2017-1/li-yc/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main.c" new file mode 100644 index 00000000..302f8500 --- /dev/null +++ "b/2017-1/li-yc/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/main.c" @@ -0,0 +1,45 @@ +#include +#include + +#include "3-4QUEEN.h" + +int main(){ + LinkQueue a; + QElemType e; + int len; + + a.front = a.rear = (QueuePtr)malloc(sizeof(QNode)); + InitQueue(a); + TraverseQueue(a); + len=QueueLength(a); + printf("此时队列长度为 %d\n\n", len); + + printf("将数字3加入队列中\n"); + EnQueue(&a, 3); + TraverseQueue(a); + len=QueueLength(a); + printf("此时队列长度为 %d\n\n", len); + + printf("将数字18加入队列中\n"); + EnQueue(&a, 18); + TraverseQueue(a); + len=QueueLength(a); + printf("此时队列长度为 %d\n\n", len); + + GetHead(a, &e); + + DeQueue(&a, &e); + printf("此时队列不为空,删除队头元素%d\n",e); + TraverseQueue(a); + printf("\n"); + + GetHead(a, &e); + + DeQueue(&a, &e); + printf("此时队列不为空,删除队头元素%d\n",e); + TraverseQueue(a); + printf("\n"); + + CleanQueue(a); + return 0; +} \ No newline at end of file diff --git "a/2017-1/li-yc/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/\351\230\237\345\210\227\346\210\252\345\233\276.PNG" "b/2017-1/li-yc/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/\351\230\237\345\210\227\346\210\252\345\233\276.PNG" new file mode 100644 index 00000000..12375c14 Binary files /dev/null and "b/2017-1/li-yc/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/\351\230\237\345\210\227\346\210\252\345\233\276.PNG" differ diff --git a/2017-1/luyj/2.12/2.12.c b/2017-1/luyj/2.12/2.12.c new file mode 100644 index 00000000..0424879d --- /dev/null +++ b/2017-1/luyj/2.12/2.12.c @@ -0,0 +1,93 @@ +#include "2.12.h" + +//算法2-11 逆序新建单链表 +void CreateList_L(LinkList L, int n)//逆位序输入n个值,建立带头指针的单链线性表L; +{ + + L->next = NULL;//头结点; + int i = 0; + LinkList p; + + for (i = n; i > 0; --i) + { + p = (LinkList)malloc(sizeof(LNode));//生成新结点; + p->data = (int)rand()%1024; + //scanf("%d", &p->data); + p->next = L->next; + L->next = p;//逆位序; + } +} + +//算法2-12 归并两个单链表 +void MergeList_L(LinkList La, LinkList Lb, LinkList *Lc) +{ + LinkList pc; + LinkList pa, pb, p; + pa = La->next; + pb = Lb->next; + (*Lc) = pc = La; + printf("归并过程\n"); + while (pa&&pb)//pa,pb链表!=NULL,即La->next,Lb->next!=null,La,Lb链表没有结束; + { + if (pa->data <= pb->data) + { + pc->next = pa; + pc = pa; + pa = pa->next; + } + else + { + pc->next = pb; + pc = pb; + pb = pb->next; + } + + output(*Lc); + printf("\n"); + + } + pc->next = pa ? pa : pb;//结合上述while; +} + +//将单链表进行排序 +void Order_L(LinkList L) +{ + L = L->next; + int i, j; + LinkList La = L; + LinkList Lb = La; + while (La) + { + while (Lb->next) + { + Lb = Lb->next; + if (La->data>Lb->data) + { + ElemType p; + p = La->data; + La->data = Lb->data; + Lb->data = p; + } + } + La = La->next; + Lb = La; + } +} + +//输出单链表 +void output(LinkList L) +{ + LinkList Line; + Line = L->next; + while (Line->next != NULL) + { + printf("%d ", Line->data); + Line = Line->next; + } + printf("%d", Line->data); + /*int i; + for(i=n;i>0;i--)*/ +} + + + diff --git a/2017-1/luyj/2.12/2.12.h b/2017-1/luyj/2.12/2.12.h new file mode 100644 index 00000000..e4ffae33 --- /dev/null +++ b/2017-1/luyj/2.12/2.12.h @@ -0,0 +1,18 @@ + +#include +#include +#include + +typedef int ElemType; +typedef struct LNode +{ + ElemType data; + struct LNode *next; +}LNode, *LinkList; + +void CreateList_L(LinkList, int); +void MergeList_L(LinkList, LinkList, LinkList*); +void Order_L(LinkList L); + +void output(LinkList); + diff --git a/2017-1/luyj/2.12/2.12.png b/2017-1/luyj/2.12/2.12.png new file mode 100644 index 00000000..66a648d1 Binary files /dev/null and b/2017-1/luyj/2.12/2.12.png differ diff --git a/2017-1/luyj/2.12/s_2.12.c b/2017-1/luyj/2.12/s_2.12.c new file mode 100644 index 00000000..5eddc13d --- /dev/null +++ b/2017-1/luyj/2.12/s_2.12.c @@ -0,0 +1,35 @@ +# include "2.12.h" + +int main() +{ + LinkList La; + LinkList Lb; + LinkList L; + La = (LinkList)malloc(sizeof(LNode)); + Lb = (LinkList)malloc(sizeof(LNode)); + L = (LinkList)malloc(sizeof(LNode)); + int n1 = 0, n2 = 0; + int i; + srand(time(0)); + n1 = rand() % 10 + 1; + n2 = rand() % 10 + 1; + printf("随机生成链表长度\n%d,%d\n", n1, n2);//链表长度; + srand(time(0)); + CreateList_L(La, n1); + CreateList_L(Lb, n2); + Order_L(La); + Order_L(Lb); + printf_s("链表1:\n"); + output(La); + printf_s("\n"); + printf_s("链表2:\n"); + output(Lb); + printf("\n\n"); + MergeList_L(La, Lb, &L); + if (Lb) + { + free(Lb); + } + printf("\n合并链表:\n"); + output(L); +} \ No newline at end of file diff --git a/2017-1/luyj/3.2.1/3.2.1.c b/2017-1/luyj/3.2.1/3.2.1.c new file mode 100644 index 00000000..9cf4adf5 --- /dev/null +++ b/2017-1/luyj/3.2.1/3.2.1.c @@ -0,0 +1,79 @@ +#include "3.2.1.h" +Status Pop(SqStack*s, SElemType*e) +{ + if (s->top == s->base) + { + return ERROR; + } + else + { + *e = *--s->top; + return OK; + } + +} +bool StackEmpty(SqStack*s) +{ + if(s->base==s->top) + { + return true; + } + else + { + return false; + } +} + +Status conversion(SqStack *s, SElemType input, int d) +{ + SElemType e; + SElemType a = 0; + //if (d == 0) + //{ + // return ERROR; + //}已将随机数设定在1~9范围内; + printf("转换结果\n"); + while (input) + { + a = input % d; + Push(s,a); + input = input / d; + } + while (StackEmpty(s)==false) + { + Pop(s, &e); + printf("%d", e); + } + printf("\n"); + return OK; +} + +Status InitStack(SqStack*s) +{ + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; +} +Status Push(SqStack*s, SElemType e) +{ + if (s->top - s->base > s->stacksize) + { + s->base = (SElemType*)realloc(s->base, (s->stacksize +2*STACK_INIT_SIZE) * sizeof(SElemType)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base + s->stacksize; + s->stacksize += 2*STACK_INIT_SIZE; + } + *s->top = e; + s->top++; + return OK; + +} + diff --git a/2017-1/luyj/3.2.1/3.2.1.h b/2017-1/luyj/3.2.1/3.2.1.h new file mode 100644 index 00000000..e70aadd0 --- /dev/null +++ b/2017-1/luyj/3.2.1/3.2.1.h @@ -0,0 +1,35 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 + +typedef int SElemType; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef struct _SqStack +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; +typedef enum +{ + true, + false +}bool; + +//对随机产生的非负十进制整数,打印输出的与其等值的进制数; +Status conversion(SqStack*, SElemType, int); +//创建一个空栈 +Status InitStack(SqStack *); +//插入新的栈顶元素 +Status Push(SqStack*, SElemType); +//判断是否是空栈,若非空,用e返回其值,并返回OK,否则,返回ERROR; +Status Pop(SqStack*, SElemType*e); + +bool StackEmpty(SqStack*s); \ No newline at end of file diff --git "a/2017-1/luyj/3.2.1/3.2.1\350\277\220\350\241\214\346\210\252\345\233\276.jpg" "b/2017-1/luyj/3.2.1/3.2.1\350\277\220\350\241\214\346\210\252\345\233\276.jpg" new file mode 100644 index 00000000..befaf57f Binary files /dev/null and "b/2017-1/luyj/3.2.1/3.2.1\350\277\220\350\241\214\346\210\252\345\233\276.jpg" differ diff --git a/2017-1/luyj/3.2.1/s_3.2.1.c b/2017-1/luyj/3.2.1/s_3.2.1.c new file mode 100644 index 00000000..12bad0be --- /dev/null +++ b/2017-1/luyj/3.2.1/s_3.2.1.c @@ -0,0 +1,15 @@ +#include "3.2.1.h" +int main() +{ + SqStack S; + srand(time(0)); + int d; + int input; + d = rand() % 9 + 1; + printf("随机生成进制数\n%d\n", d); + input = rand(); + printf("参数随机数\n%d\n", input); + InitStack(&S); + conversion(&S, input, d); + return 0; +} \ No newline at end of file diff --git a/2017-1/luyj/3.2.2/3.2.2.c b/2017-1/luyj/3.2.2/3.2.2.c new file mode 100644 index 00000000..a0f3e587 --- /dev/null +++ b/2017-1/luyj/3.2.2/3.2.2.c @@ -0,0 +1,233 @@ +#include "3.2.2.h" + +Status Push(SqStack*s, SElemType e) +{ + if (s->top - s->base > s->stacksize) + { + s->base = (SElemType*)realloc(s->base, (s->stacksize + 2 * STACK_INIT_SIZE) * sizeof(SElemType)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base + s->stacksize; + s->stacksize += 2 * STACK_INIT_SIZE; + } + *s->top = e; + s->top++; + return OK; + +} + + +Status InitStack(SqStack*s) +{ + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; +} + + +Status Marry(SqStack *s, char*test) +{ + int i; + int j = 0; + SElemType e; + SElemType m; + SElemType n; + for (i = 0; test[i] != '\0'; i++) + { + switch (test[i]) + { + case '[': + { + Push(s, test[i]); + SElemType*l = s->base; + while (l != s->top) + { + printf("%c", *l); + l++; + + } + printf("\n"); + break; + } + case '{': + { + Push(s, test[i]); + SElemType*l = s->base; + while (l != s->top) + { + printf("%c", *l); + l++; + + } + printf("\n"); + break; + } + case '(': + { + Push(s, test[i]); + SElemType*l = s->base; + while (l != s->top) + { + printf("%c", *l); + l++; + + } + printf("\n"); + break; + } + case ']': + { + GetTop(s, &e); + if (e == '[') + { + if (StackEmpty(s) == false) + { + Pop(s, &m); + SElemType*l = s->base; + while (l != s->top) + { + printf("%c", *l); + l++; + + } + printf("\n"); + + } + } + else + { + Push(s, test[i]); + SElemType*l = s->base; + while (l != s->top) + { + printf("%c", *l); + l++; + + } + printf("\n"); + } + break; + } + case '}': + { + GetTop(s, &e); + if (e == '{') + { + if (StackEmpty(s) == false) + { + Pop(s, &m); + SElemType*l = s->base; + while (l != s->top) + { + printf("%c", *l); + l++; + + } + printf("\n"); + } + } + else + { + Push(s, test[i]); + SElemType*l = s->base; + while (l != s->top) + { + printf("%c", *l); + l++; + + } + printf("\n"); + } + break; + } + case ')': + { + GetTop(s, &e); + if (e == '(') + { + if (StackEmpty(s) == false) + { + Pop(s, &m); + SElemType*l = s->base; + while (l != s->top) + { + printf("%c", *l); + l++; + + } + printf("\n"); + } + else + { + Push(s, test[i]); + SElemType*l = s->base; + while (l != s->top) + { + printf("%c", *l); + l++; + + } + printf("\n"); + + } + break; + } + } + } + + } + if (StackEmpty(s) == true) + { + return OK; + } + else + { + return ERROR; + } +} + + +bool StackEmpty(SqStack*s) +{ + if (s->base == s->top) + { + return true; + } + else + { + return false; + } +} + + +Status Pop(SqStack*s, SElemType *e) +{ + if (s->top == s->base) + { + return ERROR; + } + else + { + *e = *--s->top; + return OK; + } + +} + + +Status GetTop(SqStack*s, SElemType* e) +{ + if (s->top == s->base) + { + return ERROR; + } + *e = *(s->top - 1); + return OK; +} diff --git a/2017-1/luyj/3.2.2/3.2.2.h b/2017-1/luyj/3.2.2/3.2.2.h new file mode 100644 index 00000000..b318eebc --- /dev/null +++ b/2017-1/luyj/3.2.2/3.2.2.h @@ -0,0 +1,30 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 + +typedef char SElemType; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef struct _SqStack +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; +typedef enum +{ + true, + false +}bool; +Status InitStack(SqStack*s); +Status Marry(SqStack *s, char*test); +bool StackEmpty(SqStack*s); +Status Pop(SqStack*s, SElemType *e); +Status GetTop(SqStack*s, SElemType* e); +Status Push(SqStack*s, SElemType e); diff --git "a/2017-1/luyj/3.2.2/3.2.2\350\277\220\350\241\214\346\210\252\345\233\276.jpg" "b/2017-1/luyj/3.2.2/3.2.2\350\277\220\350\241\214\346\210\252\345\233\276.jpg" new file mode 100644 index 00000000..d28c53d4 Binary files /dev/null and "b/2017-1/luyj/3.2.2/3.2.2\350\277\220\350\241\214\346\210\252\345\233\276.jpg" differ diff --git a/2017-1/luyj/3.2.2/s_3.2.2.c b/2017-1/luyj/3.2.2/s_3.2.2.c new file mode 100644 index 00000000..3ab76e4a --- /dev/null +++ b/2017-1/luyj/3.2.2/s_3.2.2.c @@ -0,0 +1,32 @@ +#include "3.2.2.h" +int main() +{ + SqStack s; + Status ret; + int x = 0; + int i; + srand(time(0)); + x = rand() % 9+1; + char test[10]; + char s_test[6] = { '(',')','{','}','[',']' }; + for (i = 0; i < x; i++) + { + test[i] = s_test[rand() % 6]; + } + test[x] = '\0'; + printf("随机产生字符串:\n"); + puts(test); + printf("过程\n"); + //scanf("%s", &test); + InitStack(&s); + ret = Marry(&s, test); + printf("匹配结果\n"); + if (ret == OK) + { + printf("matched!"); + } + else + { + printf("not matched!"); + } +} \ No newline at end of file diff --git a/2017-1/luyj/3.2.3/3.2.3.c b/2017-1/luyj/3.2.3/3.2.3.c new file mode 100644 index 00000000..2cee91d4 --- /dev/null +++ b/2017-1/luyj/3.2.3/3.2.3.c @@ -0,0 +1,159 @@ +#include "3.2.3.h" + +Status LineEdit(SqStack*s,char *a) +{ + + char c; + char ch; + ch =getchar(); + + printf("过程如下:\n"); + while (ch != EOF) + { + while (ch != EOF&&ch != '\n') + { + switch (ch) + { + case '#': + { + Pop(s, &c); + SElemType*l = s->base; + while (l != s->top) + { + printf("%c", *l); + l++; + + } + printf("\n"); + break; + } + case '@': + { + ClearStack(s); + SElemType*l = s->base; + while (l != s->top) + { + printf("%c", *l); + l++; + + } + printf("\n"); + break; + } + default: + { + Push(s, ch); + SElemType*l = s->base; + while (l != s->top) + { + printf("%c", *l); + l++; + + } + printf("\n"); + } + } + ch = getchar(); + + } + int i = 0; + while (s->top != s->base) + { + a[i] = *(s->base); + s->base++; + i++; + } + + a[i] = '\0'; + printf("输出结果如下:\n"); + puts(a); + ClearStack(s); + if (ch != EOF) + { + ch = getchar(); + + } + } + + DistoryStack(&s); + return OK; +} + + +bool StackEmpty(SqStack*s) +{ + if (s->base == s->top) + { + return true; + } + else + { + return false; + } +} + + +Status ClearStack(SqStack*s) +{ + s->top = s->base; + return OK; +} + + +Status DistoryStack(SqStack *s) +{ + while (!StackEmpty) + { + s->top = NULL; + s->top--; + } + s->base = NULL; + free(s->base); + free(s->top); + return OK; +} + + +Status InitStack(SqStack*s) +{ + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; +} + + +Status Push(SqStack*s, SElemType e) +{ + if (s->top - s->base > s->stacksize) + { + s->base = (SElemType*)realloc(s->base, (s->stacksize + 2 * STACK_INIT_SIZE) * sizeof(SElemType)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base + s->stacksize; + s->stacksize += 2 * STACK_INIT_SIZE; + } + *s->top = e; + s->top++; + return OK; + +} +Status Pop(SqStack*s, SElemType*e) +{ + if (s->top == s->base) + { + return ERROR; + } + else + { + *e = *--s->top; + return OK; + } + +} diff --git a/2017-1/luyj/3.2.3/3.2.3.h b/2017-1/luyj/3.2.3/3.2.3.h new file mode 100644 index 00000000..003cfe8a --- /dev/null +++ b/2017-1/luyj/3.2.3/3.2.3.h @@ -0,0 +1,39 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 + +typedef char SElemType; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef struct _SqStack +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; +typedef enum +{ + true, + false +}bool; + +//构造一个空栈; +Status InitStack(SqStack *); +//若栈不为空,删除栈顶元素; +Status Pop(SqStack*, SElemType*e); +//插入元素作为新的栈顶元素; +Status Push(SqStack*, SElemType); +//把s置为空栈,参考网上代码; +Status ClearStack(SqStack*); +//判断s是否为空栈 +bool StackEmpty(SqStack*s); +//销毁栈使之不再存在; +Status DistoryStack(SqStack *s); +//利用字符栈,从终端接收一行并传至调用过程的数据区; +Status LineEdit(SqStack*s, char*a); diff --git "a/2017-1/luyj/3.2.3/3.2.3\350\277\220\350\241\214\346\210\252\345\233\276.jpg" "b/2017-1/luyj/3.2.3/3.2.3\350\277\220\350\241\214\346\210\252\345\233\276.jpg" new file mode 100644 index 00000000..46306d37 Binary files /dev/null and "b/2017-1/luyj/3.2.3/3.2.3\350\277\220\350\241\214\346\210\252\345\233\276.jpg" differ diff --git a/2017-1/luyj/3.2.3/s_3.2.3.c b/2017-1/luyj/3.2.3/s_3.2.3.c new file mode 100644 index 00000000..84dd99dc --- /dev/null +++ b/2017-1/luyj/3.2.3/s_3.2.3.c @@ -0,0 +1,16 @@ +#include "3.2.3.h" +//检测函数 +int main() +{ + int i; + char a[100]; + + SqStack s; + InitStack(&s); + printf("请输入字符,其中“#”为退格符,“@”为退行符\n"); + LineEdit(&s, a); + DistoryStack(&s); + + + return 0; +} \ No newline at end of file diff --git a/2017-1/luyj/3.2.5.SuffixType.c b/2017-1/luyj/3.2.5.SuffixType.c new file mode 100644 index 00000000..20e80f21 --- /dev/null +++ b/2017-1/luyj/3.2.5.SuffixType.c @@ -0,0 +1,449 @@ +#include "3.2.5.h" + +// +Status Pop(SqStack*s, SElemType*e) +{ + if (s->top == s->base) + { + return ERROR; + } + else + { + *e = *--s->top; + return OK; + } + +} + +char* Pop2(SqStack*s) +{ + char e[10]; + + s->top--; + strcpy(e, *s->top); + return e; + +} +bool StackEmpty(SqStack*s) +{ + if (s->base == s->top) + { + return true; + } + else + { + return false; + } +} + +Status InitStack(SqStack*s) +{ + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; +} +Status Push(SqStack*s, SElemType e) +{ + if (s->top - s->base > s->stacksize) + { + s->base = (SElemType*)realloc(s->base, (s->stacksize + 2 * STACK_INIT_SIZE) * sizeof(SElemType)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base + s->stacksize; + s->stacksize += 2 * STACK_INIT_SIZE; + } + *s->top = e; + s->top++; + return OK; + +} + +Status GetTop(SqStack *s, SElemType *e) +{ + if (s->top == s->base) + { + return ERROR; + } + *e = *(s->top - 1); + return OK; +} + +Status DistoryStack(SqStack *s) +{ + while (StackEmpty(s) == false) + { + s->top = NULL; + s->top--; + } + s->base = NULL; + free(s->base); + free(s->top); + return OK; +} + + +//存储整型数据的栈; +Status Pop1(SqStack1*s, SElem*e) +{ + if (s->top == s->base) + { + return ERROR; + } + else + { + *e = *--s->top; + return OK; + } + +} +bool StackEmpty1(SqStack1*s) +{ + if (s->base == s->top) + { + return true; + } + else + { + return false; + } +} + +Status InitStack1(SqStack1*s) +{ + s->base = (SElem*)malloc(STACK_INIT_SIZE * sizeof(SElem)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; +} +Status Push1(SqStack1*s, SElem e) +{ + if (s->top - s->base > s->stacksize) + { + s->base = (SElem*)realloc(s->base, (s->stacksize + 2 * STACK_INIT_SIZE) * sizeof(SElem)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base + s->stacksize; + s->stacksize += 2 * STACK_INIT_SIZE; + } + *s->top = e; + s->top++; + return OK; + +} + +Status DistoryStack1(SqStack1 *s) +{ + while (StackEmpty(s) == false) + { + s->top = NULL; + s->top--; + } + s->base = NULL; + free(s->base); + free(s->top); + return OK; +} + + + +//判断运算符的优先级; +int Level(char *character) +{ + int num; + switch (*character) + { + + case')': + num = 1; + break; + case'*': + num = 3; + break; + case'/': + num = 4; + break; + case'+': + num = 5; + break; + case'-': + num = 6; + break; + case'#': + num = 8; + break; + case'(': + num = 9; + break; + default: + num = 0; + break; + } + return num; +} + +bool Precede(SElemType*c, char *ch) +{ + int i = 0, j = 0; + if (Level(*c) != 0 && Level(ch) != 0) + { + if (Level(*c) - Level(ch)>1) + { + return false; + } + else + { + return true; + } + } + +} + + +bool IN(char *ch) +{ + switch (*ch) + { + case '+': + return true; + break; + case'-': + return true; + break; + case '*': + return true; + break; + case'/': + return true; + break; + case '#': + return true; + break; + case'(': + return true; + break; + case')': + return true; + break; + default: + return false; + break; + } +} +bool IN1(char ch) +{ + if (ch>='0'&&ch<='9') + { + return false; + } + else + { + return true; + } +} + +//向后缀式数组中填充数据; +Status Pass(char q[50], char *ch) +{ + //char *p=suffix; + static int i = 0; + + if (i <= 50) + { + strcpy(q, ch); + return OK; + } + else + { + return ERROR; + } +} + +//建立后缀式; +void transform(char suffix[50][50], char exp[50][50], SqStack*s) +{ + Push(s, "#"); + char(*p)[50] = exp; + char *ch; + char(*q)[50] = suffix; + char(*x)[50] =q;//用于打印栈; + ch = p; + int i = 0; + SElemType c; + while (StackEmpty(s) == false) + { + if (IN(ch) == false && strcmp(ch,"\0")!=0)//判断是否为操作数; + { + Pass(q, ch); + q++; + Pass(q, "#"); + while (strcmp(*x, "#") != 0) + { + printf("%s", *x); + x++; + } + printf("\n"); + x = suffix; + } + else if (strcmp(ch,"\0")!=0) + { + switch (*ch) + { + case'(': + Push(s, ch); + break; + case ')': + Pop(s, &c); + while (strcmp(c, "(") != 0) + { + Pass(q, c); + q++; + Pop(s, &c); + Pass(q, "#"); + while (strcmp(*x, "#") != 0) + { + printf("%s", *x); + x++; + } + printf("\n"); + x = suffix; + } + break; + default: + GetTop(s, &c); + while (Precede(&c, ch) == true && strcmp(ch,"\0")!=0) + { + Pass(q, c); + Pop(s, &c); + GetTop(s, &c); + q++; + Pass(q, "#"); + while (strcmp(*(x), "#") != 0) + { + printf("%s", *x); + x++; + } + printf("\n"); + x = suffix; + } + if (strcmp(ch, "\0") != 0) + { + Push(s, ch); + } + break; + } + + } + if (strcmp(ch, "\0") != 0) + { + p++; + ch = *p; + } + else + { + while (s->top-1!=s->base) + { + Pop(s, &ch); + if (strcmp(ch, "#") != 0) + { + Pass(q, ch); + q++; + Pass(q, "#"); + while (strcmp(*x, "#") != 0) + { + printf("%s", *x); + x++; + } + printf("\n"); + x = suffix; + } + } + Pass(q, "#"); + while (strcmp(*(x), "#") != 0) + { + printf("%s", *x); + x++; + } + printf("\n"); + break; + } + } +} + + +//利用后缀式数据及数据栈和符号栈进行数据的简单+-*/运算; +void EvaluateExpression(SqStack *s2, SqStack1*s3, char suffix[50][50]) +{ + //char a[5] = {'*','/','+','-','#' }; + double x = 0; + double y = 0; + char z[10]; + int i = 0; + int j = 0; + int q = 0; + char(*p)[50] = suffix; + char *ch; + Push(s2, "#"); + while (StackEmpty(s2) == false) + { + ch = p; + if (IN(ch) == false)//判断是否为操作数; + { + Push1(s3, atoi(ch)); + i++; + } + else + { + Push(s2, ch); + j++; + } + if (i >= 2 && j>0) + { + Pop1(s3, &x); + Pop1(s3, &y); + strcpy(z, Pop2(s2)); + q = Level(z); + switch (q) + { + case 5: + x = y + x; + Push1(s3, x); + break; + case 6: + x = y - x; + Push1(s3, x); + break; + case 3: + x = y * (x); + Push1(s3, x); + break; + case 4: + x = y / x; + Push1(s3, x); + break; + default: + break; + } + i--; + j--; + } + p++; + if (strcmp(ch,"#")==0) + { + break; + } + } +} + diff --git a/2017-1/luyj/3.2.5.c b/2017-1/luyj/3.2.5.c new file mode 100644 index 00000000..db3bf1b0 --- /dev/null +++ b/2017-1/luyj/3.2.5.c @@ -0,0 +1,390 @@ +#include "3.2.5.h" + +//存储字符型数据的栈; +Status Pop(SqStack*s, SElemType*e) +{ + if (s->top == s->base) + { + return ERROR; + } + else + { + *e = *--s->top; + return OK; + } + +} +bool StackEmpty(SqStack*s) +{ + if (s->base == s->top) + { + return true; + } + else + { + return false; + } +} + +Status InitStack(SqStack*s) +{ + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; +} +Status Push(SqStack*s, SElemType e) +{ + if (s->top - s->base > s->stacksize) + { + s->base = (SElemType*)realloc(s->base, (s->stacksize + 2 * STACK_INIT_SIZE) * sizeof(SElemType)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base + s->stacksize; + s->stacksize += 2 * STACK_INIT_SIZE; + } + *s->top = e; + s->top++; + return OK; + +} + +Status GetTop(SqStack *s, SElemType *e) +{ + if (s->top == s->base) + { + return ERROR; + } + *e = *(s->top - 1); + return OK; +} + +Status DistoryStack(SqStack *s) +{ + while (StackEmpty(s) == false) + { + s->top = NULL; + s->top--; + } + s->base = NULL; + free(s->base); + free(s->top); + return OK; +} + + +//存储整型数据的栈; +Status Pop1(SqStack1*s, SElem*e) +{ + if (s->top == s->base) + { + return ERROR; + } + else + { + *e = *--s->top; + return OK; + } + +} +bool StackEmpty1(SqStack1*s) +{ + if (s->base == s->top) + { + return true; + } + else + { + return false; + } +} + +Status InitStack1(SqStack1*s) +{ + s->base = (SElem*)malloc(STACK_INIT_SIZE * sizeof(SElem)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return OK; +} +Status Push1(SqStack1*s, SElem e) +{ + if (s->top - s->base > s->stacksize) + { + s->base = (SElem*)realloc(s->base, (s->stacksize + 2 * STACK_INIT_SIZE) * sizeof(SElem)); + if (!s->base) + { + return OVERFLOW; + } + s->top = s->base + s->stacksize; + s->stacksize += 2 * STACK_INIT_SIZE; + } + *s->top = e; + s->top++; + return OK; + +} + +Status DistoryStack1(SqStack1 *s) +{ + while (StackEmpty(s) == false) + { + s->top = NULL; + s->top--; + } + s->base = NULL; + free(s->base); + free(s->top); + return OK; +} + + + +//判断运算符的优先级; +int Level(char character) +{ + int num; + switch (character) + { + case'(': + num = 9; + break; + case')': + num = 1; + break; + case'*': + num = 3; + break; + case'/': + num = 4; + break; + case'+': + num = 5; + break; + case'-': + num = 6; + break; + case'#': + num = 8; + break; + default: + num = 0; + break; + } + return num; +} + +bool Precede(SElemType*c, char ch) +{ + int i = 0, j = 0; + if (Level(*c) != 0 && Level(ch) != 0) + { + if (Level(*c) - Level(ch)>1) + { + return false; + } + else + { + return true; + } + } + +} + + +//判断数据是否为0-9的操作数; +bool IN(char ch) +{ + if (ch >= '0'&&ch <= '9') + { + return false; + } + else + { + return true; + } +} + +//向后缀式数组中填充数据; +Status Pass(char suffix[50], char ch) +{ + static int i = 0; + if (i <= 50) + { + suffix[i] = ch; + suffix[i + 1] = '\0'; + i++; + return OK; + } + else + { + return ERROR; + } +} + +//建立后缀式; +void transform(char suffix[100], char exp[100], SqStack*s) +{ + Push(s, '#'); + char*p = exp; + char ch; + ch = *p; + int i = 0; + SElemType c; + while (StackEmpty(s) == false) + { + if (IN(ch) == false)//判断是否为操作数; + { + Pass(suffix, ch); + } + else + { + switch (ch) + { + case'(': + Push(s, ch); + break; + case ')': + Pop(s, &c); + while (c != '(') + { + Pass(suffix, c); + Pop(s, &c); + while (suffix[i] != '\0') + { + printf("%c", suffix[i]); + i++; + } + printf("\n"); + i = 0; + } + break; + default: + GetTop(s, &c); + while (Precede(&c, ch) == true && ch != '\0') + { + Pass(suffix, c); + Pop(s, &c); + GetTop(s, &c); + while (suffix[i] != 0) + { + printf("%c", suffix[i]); + i++; + } + printf("\n"); + i = 0; + } + if (ch != '\0') + { + Push(s, ch); + while (suffix[i] != 0) + { + printf("%c", suffix[i]); + i++; + } + i = 0; + printf("\n"); + } + break; + } + + } + if (ch != '\0') + { + p++; + ch = *p; + } + else + { + while (*s->top != '#') + { + Pop(s, &ch); + if (ch != '#') + { + Pass(suffix, ch); + while (suffix[i] != 0) + { + printf("%c", suffix[i]); + i++; + } + i = 0; + printf("\n"); + } + } + } + } +} + + +//利用后缀式数据及数据栈和符号栈进行0-9数据的简单+-*/运算; +void EvaluateExpression(SqStack *s2, SqStack1*s3, char suffix[100]) +{ + //char a[5] = {'*','/','+','-','#' }; + double x = 0; + double y = 0; + char z; + int i = 0; + int j = 0; + int q = 0; + char*p = suffix; + char ch; + Push(s2, '#'); + while (StackEmpty(s2) == false) + { + ch = *p; + if (IN(ch) == false)//判断是否为操作数; + { + Push1(s3, ch - '0'); + i++; + } + else + { + Push(s2, ch); + j++; + } + if (i >= 2 && j>0) + { + Pop1(s3, &x); + Pop1(s3, &y); + Pop(s2, &z); + q = Level(z); + switch (q) + { + case 5: + x = y + x; + Push1(s3, x); + break; + case 6: + x = y - x; + Push1(s3, x); + break; + case 3: + x = (y) * (x); + Push1(s3, x); + break; + case 4: + x = y / x; + Push1(s3, x); + break; + default: + break; + } + i--; + j--; + } + p++; + if (*p == '\0') + { + break; + } + } +} diff --git a/2017-1/luyj/3.2.5.h b/2017-1/luyj/3.2.5.h new file mode 100644 index 00000000..2ac52fe8 --- /dev/null +++ b/2017-1/luyj/3.2.5.h @@ -0,0 +1,55 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 + +typedef char SElemType; +typedef double SElem; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef struct _SqStack +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +typedef struct _SqStack1 +{ + SElem *base; + SElem *top; + int stacksize; +}SqStack1; + +typedef enum +{ + true, + false +}bool; + +Status Pop(SqStack*s, SElemType*e); +bool StackEmpty(SqStack*s); +Status InitStack(SqStack*s); +Status Push(SqStack*s, SElemType e); +Status GetTop(SqStack *s, SElemType *e); + + +Status Pop1(SqStack1*s, SElem*e); +bool StackEmpty1(SqStack1*s); +Status InitStack1(SqStack1*s); +Status Push1(SqStack1*s, SElem e); + + +int Level(char character); +bool IN(char ch); +Status Pass(char suffix[50], char ch); +bool Precede(SElemType*c, char ch); + +void transform(char suffix[100], char exp[100], SqStack*s); + +void EvaluateExpression(SqStack *s2, SqStack1*s3, char suffix[100]); diff --git a/2017-1/luyj/3.2.5.png b/2017-1/luyj/3.2.5.png new file mode 100644 index 00000000..bf392da3 Binary files /dev/null and b/2017-1/luyj/3.2.5.png differ diff --git a/2017-1/luyj/3.2.5SuffixType.h b/2017-1/luyj/3.2.5SuffixType.h new file mode 100644 index 00000000..4686c7f3 --- /dev/null +++ b/2017-1/luyj/3.2.5SuffixType.h @@ -0,0 +1,72 @@ + +#include +#include +#include + +#define STACK_INIT_SIZE 100 + +typedef char *SElemType; +typedef double SElem; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef struct _SqStack +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +typedef struct _SqStack1 +{ + SElem *base; + SElem *top; + int stacksize; +}SqStack1; + +typedef enum +{ + true, + false +}bool; + + +//若栈不为空,删除栈顶元素,用e做返回值; +Status Pop(SqStack*s, SElemType*e); + +//判断栈是否为空; +bool StackEmpty(SqStack*s); + +//创建一个空栈; +Status InitStack(SqStack*s); + +//插入元素e为新的栈顶元素; +Status Push(SqStack*s, SElemType e); + +//若栈不为空,用e返回栈顶元素; +Status GetTop(SqStack *s, SElemType *e); + + +Status Pop1(SqStack1*s, SElem*e); +bool StackEmpty1(SqStack1*s); +Status InitStack1(SqStack1*s); +Status Push1(SqStack1*s, SElem e); + +//判断运算符的优先级; +int Level(char character); +bool Precede(SElemType*c, char ch); + +//判断是否为运算符; +bool IN(char ch); + +//将ch入数组; +Status Pass(char suffix[50], char ch); + +//实现后缀式; +void transform(char suffix[100], char exp[50][50], SqStack*s); + +//利用后缀式数据及数据栈和符号栈进行数据的简单+-*/运算; +void EvaluateExpression(SqStack *s2, SqStack1*s3, char suffix[100]); \ No newline at end of file diff --git a/2017-1/luyj/AVLOutput.txt b/2017-1/luyj/AVLOutput.txt new file mode 100644 index 00000000..4e92b068 --- /dev/null +++ b/2017-1/luyj/AVLOutput.txt @@ -0,0 +1,6 @@ +8, 4, 3, 1, 6, 5, 7, 14, 10, 22, 19, 30 +8, 4, 3, 1, 6, 5, 7, 14, 10, 13, 22, 19, 30 +7, 4, 3, 1, 6, 5, 14, 10, 13, 22, 19, 30 +7, 4, 3, 1, 6, 14, 10, 13, 22, 19, 30 +7, 4, 3, 1, 6, 14, 10, 13, 22, 19, 20, 30 +14, 7, 3, 1, 4, 10, 13, 22, 19, 20, 30 diff --git a/2017-1/luyj/AVLTree.c b/2017-1/luyj/AVLTree.c new file mode 100644 index 00000000..699b663f --- /dev/null +++ b/2017-1/luyj/AVLTree.c @@ -0,0 +1,427 @@ +#include "AVLTree.h" +int flag = 0; +Bool EQ(int k, int tk) +{ + if (k == tk) + { + return TRUE; + } + else + { + return FALSE; + } +} + +Bool LT(int k, int tk) +{ + if (k < tk) + { + return TRUE; + } + else + { + return FALSE; + } +} + +Bool InsertAVL(BSTree *t, ElemType e, Boolean *taller) +{ + //插入操作,若在平衡二叉排序树T中不存在和e相同的关键字结点,则插入一个数据元素 + //为e的新结点。若插入使二叉树失衡,做旋转处理,taller反映T长高与否。 + //用此函数建立平衡排序二叉树。 + if (NULL == *t) + { + (*t) = (BSTree)malloc(sizeof(BSTNode)); + (*t)->data.key = e.key; + (*t)->lchild = (*t)->rchild = NULL; + (*t)->bf = EH; + *taller = TRUE; + } + else + { + if (TRUE == EQ(e.key, (*t)->data.key)) + { + *taller = FALSE; + return FALSE; + } + + if (TRUE == LT(e.key, (*t)->data.key)) + { + //若待查找关键字小于结点,在左子树查找 + if (FALSE == InsertAVL(&(*t)->lchild, e, taller)) + { + return FALSE; + } + if (*taller)//调节左子树平衡 + { + switch ((*t)->bf) + { + case LH: + { + LeftBalance(t); + *taller = FALSE; + break; + } + case EH: + { + (*t)->bf = LH; + *taller = TRUE; + break; + } + case RH: + { + (*t)->bf = EH; + *taller = FALSE; + break; + } + default: + break; + } + } + } + else + { + if (FALSE == InsertAVL(&(*t)->rchild, e, taller)) + { + return FALSE; + } + if (*taller==TRUE)//调节右子树平衡 + { + switch ((*t)->bf) + { + case LH: + { + (*t)->bf = EH; + *taller = FALSE; + break; + } + case EH: + { + (*t)->bf = RH; + *taller = TRUE; + break; + } + case RH: + { + RightBalance(t); + *taller = FALSE; + break; + } + default: + break; + } + } + } + } + return TRUE; +} + +Status LeftBalance(BSTree*t) +{ + BSTree lc = (*t)->lchild;//lc指向*t的左子树根结点 + BSTree rd = (BSTree)malloc(sizeof(BSTNode)); + if (NULL == lc) + { + return ERROR; + } + switch (lc->bf) //检测*t左子树的平衡度,并做并做相应的平衡处理 + { + case LH: + { + //新结点插在*t的左孩子的左子树上,做右旋处理 + (*t)->bf = lc->bf = EH; + R_Rotate(t); + break; + } + case RH: + { + //新结点插在*t左孩子的右子树上,做双旋处理 + rd = lc->rchild; + switch (rd->bf) + { + case LH: + { + (*t)->bf = RH; + lc->bf = EH; + break; + } + case EH: + { + (*t)->bf = lc->bf = EH; + break; + } + case RH: + { + (*t)->bf = EH; + lc->bf = LH; + break; + } + + default: + break; + } + rd->bf = EH; + L_Rotate(&(*t)->lchild); + R_Rotate(t); + } + default: + break; + } + return OK; +} + + +Status RightBalance(BSTree *t) +{ + //对以指针t所指结点为根的二叉树做右平衡旋转处理,算法结束时,指针t指向新的跟结点。 + BSTree rc = (*t)->rchild;//rc指向*t的右子树根结点 + BSTree ld = (BSTree)malloc(sizeof(BSTNode)); + if (NULL == rc) + { + return ERROR; + } + switch (rc->bf)//检测右子树的平衡度,并做出相应的平衡处理 + { + case LH: + { + ld = rc->lchild; + switch (ld->bf) + { + case LH: + { + (*t)->bf = EH; + rc->bf = RH; + break; + } + case EH: + { + (*t)->bf = rc->bf = EH; + break; + } + case RH: + { + (*t)->bf = LH; + rc->bf = EH; + break; + } + default: + break; + } + ld->bf = EH; + R_Rotate(&(*t)->rchild); + L_Rotate(t); + break; + } + case RH: + { + (*t)->bf = rc->bf = EH; + L_Rotate(t); + break; + } + default: + break; + + } + return OK; +} + +void R_Rotate(BSTree *p) +{ + BSTree lc = (*p)->lchild; + if (NULL == lc->rchild) + { + (*p)->lchild = NULL; + } + else + { + (*p)->lchild = lc->rchild; + } + lc->rchild = *p; + *p = lc; +} + +void L_Rotate(BSTree*p) +{ + BSTree rc = (*p)->rchild; + if (NULL == rc->lchild) + { + (*p)->rchild = NULL; + } + else + { + (*p)->rchild = rc->lchild; + } + rc->lchild = (*p); + (*p) = rc; +} + +Status PreOrderTraverse(BSTree t,FILE*pfile) +{ + + if (NULL == t) + { + return ERROR; + } + if (NULL != t) + { + print(t->data, pfile); + PreOrderTraverse(t->lchild, pfile); + PreOrderTraverse(t->rchild, pfile); + } + + return OK; +} + +Bool SearchBST(BSTree t, ElemType e) +{ + while (t != NULL) + { + if (EQ( e.key, t->data.key)) + { + return TRUE; + } + else if (LT(e.key, t->data.key)) + { + t = t->lchild; + } + else + { + t = t->rchild; + } + } + return FALSE; + +} +Bool Delete(BSTree *p) +{ + if (NULL == (*p)->rchild)//若右孩子为空,则直接删除结点; + { + BSTree q = *p; + (*p) = (*p)->lchild; + free(q); + + } + else if (NULL == (*p)->lchild) + { + BSTree q = *p; + *p = (*p)->rchild; + free(q); + } + else + { + BSTree q = *p; + BSTree s = (*p)->lchild; + while (s->rchild) + { + q = s; + s = s->rchild; + } + (*p)->data = s->data; + if (q != *p) + { + q->rchild = s->lchild; + q->bf = 1; + } + else + { + q->lchild = s->lchild; + q->bf = -1; + } + free(s); + } + return TRUE; +} + + +Status DeleteAVL(BSTree *t, ElemType e, Boolean *lower) +{ + //若在平衡二叉树t中存在和e相同的关键字结点,则删除并作相应平衡处理 + //lower用于判断树是否变矮 + BSTree p = (BSTree)malloc(sizeof(BSTree)); + if (NULL == *t) //若树为空,直接返回ERROR + { + return ERROR; + } + if (EQ(e.key,(*t)->data.key)) //若关键字与结点相同,删除结点 + { + Delete(t); + *lower = TRUE; + } + else if (LT(e.key, (*t)->data.key)) + { + if (ERROR == DeleteAVL(&(*t)->lchild, e, lower)) + { + return ERROR; + } + if (*lower) + { + switch ((*t)->bf) + { + case LH: + { + (*t)->bf = EH; + *lower = TRUE; + break; + } + case EH: + { + (*t)->bf = RH; + *lower = FALSE; + break; + } + case RH: + { + RightBalance(t); + *lower = TRUE; + break; + } + } + } + } + else + { + if (ERROR == DeleteAVL(&(*t)->rchild, e, lower)) + { + return ERROR; + } + if (*lower) + { + switch ((*t)->bf) + { + case LH: //原本左子树比右子树高,则作左平衡处理 + LeftBalance(t); + *lower = TRUE; + break; + case EH: //原本左右子树等高,现左子树比右子树高1 + (*t)->bf = LH; + *lower = FALSE; + break; + case RH: //原本右子树比左子树高,现左左子树等高 + (*t)->bf = EH; + *lower = TRUE; + break; + + } + } + } + return OK; +} +Status print(ElemType data, FILE*pfile) +{ + + char d[4] = ", "; + if (NULL == pfile) + { + return ERROR; + } + if (NULL != pfile) + { + if (flag == 1) + { + fputs(d, pfile); + } + fprintf(pfile, "%d", data.key); + flag = 1; + + } +} \ No newline at end of file diff --git a/2017-1/luyj/AVLTree.h b/2017-1/luyj/AVLTree.h new file mode 100644 index 00000000..11cc28dd --- /dev/null +++ b/2017-1/luyj/AVLTree.h @@ -0,0 +1,65 @@ +#include +#include + + +typedef int KeyType; +typedef struct ElemType +{ + int key; +}ElemType; + +typedef struct BSTNode +{ + ElemType data; + int bf;//结点平衡因子 + struct BSTNode *lchild, *rchild; +}BSTNode, *BSTree; + +typedef enum +{ + RH = -1,//右高 + EH = 0,//等高 + LH = +1//左高 +}bh; + +typedef enum +{ + FALSE, + TRUE +}Boolean,Bool; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + + + +Bool EQ(int k, int tk); +Bool LT(int k, int tk); +Status PreOrderTraverse(BSTree t,FILE*pfile); + + +/*=========平衡二叉树建立,查找,删除操作=========*/ + +//建立 +Bool InsertAVL(BSTree *t, ElemType e, Boolean *taller); +//查找关键字结点是否存在 +Bool SearchBST(BSTree t, ElemType e); +//删除与关键字相同的结点 +Status DeleteAVL(BSTree *t, ElemType e, Boolean *lower); +Bool Delete(BSTree *p); + +//相关调节平衡操作 +Status LeftBalance(BSTree*t); +Status RightBalance(BSTree *t); +void R_Rotate(BSTree *p); +void L_Rotate(BSTree*p); + + + + + + diff --git a/2017-1/luyj/BSTOutput.txt b/2017-1/luyj/BSTOutput.txt new file mode 100644 index 00000000..c6be2057 Binary files /dev/null and b/2017-1/luyj/BSTOutput.txt differ diff --git a/2017-1/luyj/BiSTree.c b/2017-1/luyj/BiSTree.c new file mode 100644 index 00000000..4e439944 --- /dev/null +++ b/2017-1/luyj/BiSTree.c @@ -0,0 +1,194 @@ +#include "BiSTree.h" +int flag = 0; + +bool EQ(int k, int tk) +{ + if (k == tk) + { + return true; + } + else + { + return false; + } +} +bool LT(int k, int tk) +{ + if (k < tk) + { + return true; + } + else + { + return false; + } +} + + +bool SearchBST(BiTree T, KeyType key, BiTree f, BiTree *p) +{ + + if (T==NULL)//若树为空,返回根结点; + { + *p = f; + return false; + } + else if (EQ(key, T->data.key)==true) + { + *p = T; + return true; + } + else if (LT(key, T->data.key)==true)//若关键字值小于根结点值,则在左子树上查找; + { + + return (SearchBST(T->lchild, key, T, p)); + } + else + { + return (SearchBST(T->rchild, key, T, p));//若关键字值大于根结点,则在右子树上查找; + } +} + + +bool Delete(BiTree *p) +{ + if ((*p)->rchild==NULL)//若右孩子为空,则直接删除结点; + { + BiTree q = *p; + (*p) = (*p)->lchild; + free(q); + } + else if ((*p)->lchild==NULL) + { + BiTree q = *p; + *p = (*p)->rchild; + free(q); + } + else + { + BiTree q = *p; + BiTree s = (*p)->lchild; + while (s->rchild) + { + q = s; + s = s->rchild; + } + (*p)->data = s->data; + if (q != *p) + { + q->rchild = s->lchild; + } + else + { + q->lchild = s->lchild; + } + free(s); + } + return true; +} + +//查找要删除结点所在位置; +bool DeleteBST(BiTree *T, KeyType key) +{ + if (*T==NULL) + { + return false; + } + else + { + if (EQ(key, (*T)->data.key)==true) + { + return Delete(T); + } + else if (LT(key, (*T)->data.key)==true) + { + return DeleteBST(&((*T)->lchild), key); + } + else + { + return DeleteBST(&((*T)->rchild), key); + } + + } +} + + +bool InsertBST(BiTree *T, ElemType e,BiTree p) +{ + BiTree s; + s = (BiTNode*)malloc(sizeof(BiTNode)); + s->data = e; + s->lchild = s->rchild = NULL; + if (p == NULL) + { + *T = s;//被插入的结点*s为新的跟结点; + } + else if (LT(e.key, p->data.key)==true) + { + p->lchild = s; + } + else + { + p->rchild = s; + } + return true; +} + + +bool searchInDelete(BiTree *T, ElemType e) +{ + BiTree p = (BiTree)malloc(sizeof(BiTree)); + if (SearchBST(*T, e.key, NULL, &p)==false) + { + InsertBST(T, e, p); + } + + else + { + DeleteBST(T, e.key); + } + return true; +} + + +Status print(ElemType data,FILE*pfile) +{ + + char *d = ", "; + if (NULL == pfile) + { + return ERROR; + } + if (NULL != pfile) + { + if (flag == 1) + { + fwrite(d, sizeof(d), 1, pfile); + } + fprintf(pfile, "%d",data.key); + flag = 1; + + } +} + + +Status PreOrderTraverse(BiTree T,FILE*pfile) +{ + + if (T==NULL) + { + return ERROR; + } + if (T!=NULL) + { + print(T->data,pfile); + PreOrderTraverse(T->lchild,pfile); + PreOrderTraverse(T->rchild,pfile); + } + + return OK; +} + + + + diff --git a/2017-1/luyj/BiSTree.h b/2017-1/luyj/BiSTree.h new file mode 100644 index 00000000..ae2a829c --- /dev/null +++ b/2017-1/luyj/BiSTree.h @@ -0,0 +1,56 @@ +#include +#include +#include +#include + +#define KeyType int + + + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum +{ + true, + false +}bool; + +/*=========顺序二叉树的存储结构=========*/ +typedef struct ElemType +{ + int key; +}ElemType; +typedef struct BiTNode +{ + ElemType data; + struct BiTNode *lchild, *rchild; +}BiTNode, *BiTree; + + + +bool EQ(int k, int tk); +bool LT(int k, int tk); + +/*=========顺序二叉树相关函数实现=========*/ + +//查找二叉树是否存在某值结点; +bool SearchBST(BiTree T, KeyType key, BiTree f, BiTree *p); + +//删除顺序二叉树中指定结点; +bool Delete(BiTree *p); +bool DeleteBST(BiTree *T, KeyType key); + +//在顺序二叉树插入指定结点; +bool InsertBST(BiTree *T, ElemType e, BiTree p); + +//查找顺序二叉树,若结点存在则删除,若不存在则删除; +bool searchInDelete(BiTree *T, ElemType e); + +//按先序遍历输出到文件 +Status print(ElemType data, FILE*pfile); +Status PreOrderTraverse(BiTree T, FILE*pfile); diff --git a/2017-1/luyj/BiTree.png b/2017-1/luyj/BiTree.png new file mode 100644 index 00000000..5eb2b4a7 Binary files /dev/null and b/2017-1/luyj/BiTree.png differ diff --git a/2017-1/luyj/BiTrees.jpg b/2017-1/luyj/BiTrees.jpg new file mode 100644 index 00000000..9a0ae96c Binary files /dev/null and b/2017-1/luyj/BiTrees.jpg differ diff --git a/2017-1/luyj/BinaryTree.c b/2017-1/luyj/BinaryTree.c new file mode 100644 index 00000000..fa6b6355 --- /dev/null +++ b/2017-1/luyj/BinaryTree.c @@ -0,0 +1,59 @@ +#include "BinaryTree.h" + +int i = 0; +char AbstractWord(char*s) +{ + + char ch = s[i]; + i++; + return ch; +} + +Status CreateBiTree(BiTree *T,char *s) +{ + char ch; + ch = AbstractWord(s); + if (ch == ' ') + { + *T = NULL; + } + else + { + *T = (BiTNode*)malloc(sizeof(BiTNode)); + if (!T) + { + return OVERFLOW; + } + + (*T)->data = ch; + CreateBiTree(&(*T)->lchild, s); // 构造左子树 + CreateBiTree(&(*T)->rchild, s);// 构造右子树 + + } + return OK; +} + +Status PostOrderTraverse(BiTree T) +{ + if (T) + { + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c", T->data); + } + return OK; +} + +Status destoryBiTree(BiTree T) +{ + if (!T) + { + return ERROR; + } + else + { + destoryBiTree(T->lchild); + destoryBiTree(T->rchild); + free (T); + } +} diff --git a/2017-1/luyj/BinaryTree.h b/2017-1/luyj/BinaryTree.h new file mode 100644 index 00000000..b126c1c2 --- /dev/null +++ b/2017-1/luyj/BinaryTree.h @@ -0,0 +1,34 @@ +#include +#include + +extern int i; +typedef char TElemType; + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild, *rchild; +}BiTNode, *BiTree; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + + +//将字符从字符串中取出; +char AbstractWord(char*s); + +//建立二叉树; +Status CreateBiTree(BiTree *T, char *s); + +//以后序式输出; +Status PostOrderTraverse(BiTree T); + +//摧毁二叉树; +Status destoryBiTree(BiTree T); + + + diff --git a/2017-1/luyj/BinaryTree.png b/2017-1/luyj/BinaryTree.png new file mode 100644 index 00000000..e5fb4d0f Binary files /dev/null and b/2017-1/luyj/BinaryTree.png differ diff --git a/2017-1/luyj/BinaryTrees.c b/2017-1/luyj/BinaryTrees.c new file mode 100644 index 00000000..ae9f1e88 --- /dev/null +++ b/2017-1/luyj/BinaryTrees.c @@ -0,0 +1,283 @@ +#include "BinaryTrees.h" +int i = 0; +int high = 0; +int wide[50] = { 0 }; +int swide = 0; + +void valInit(int *level, int wide[50], int *count, int *ncounts) +{ + int j; + *level = 0; + i = 0; + *count = 0; + *ncounts = 0; + swide = 0; + for (j = 0; j < 50; j++) + { + wide[j] = 0; + } +} + +char AbstractWord(char*s) +{ + + char ch = s[i]; + i++; + return ch; +} + + + +Status CreateBiTree(BiTree *T,char *s) +{ + char ch; + ch = AbstractWord(s); + if (ch == ' ') + { + *T = NULL; + } + else + { + *T = (BiTNode*)malloc(sizeof(BiTNode)); + if (!T) + { + return OVERFLOW; + } + + (*T)->data = ch; + CreateBiTree(&(*T)->lchild, s); // 构造左子树 + CreateBiTree(&(*T)->rchild, s);// 构造右子树 + + } + return OK; +} + + + +Status PostOrderTraverse(BiTree T) +{ + if (T) + { + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c", T->data); + } + return OK; +} + + + +Status destoryBiTree(BiTree T) +{ + if (!T) + { + return ERROR; + } + else + { + destoryBiTree(T->lchild); + destoryBiTree(T->rchild); + free (T); + } +} + + + +int Max(int x, int y) +{ + if (x > y) + { + return x; + } + else + { + return y; + } +} + + + +int getHigh(BiTree T) +{ + int high = 0; + int lhigh; + int rhigh; + if (!T) + { + return 0; + } + else + { + + lhigh = getHigh(T->lchild); + rhigh = getHigh(T->rchild); + high = Max(lhigh, rhigh)+1; + return high; + } + +} + + + +int getWide(BiTree T, int level) +{ + if (T == NULL) + { + return 0; + } + wide[level]++; + if (swide < wide[level]) + { + swide = wide[level]; + } + getWide(T->lchild, level + 1); + getWide(T->rchild, level + 1); + return swide; +} + + + +void CountLeaf(BiTree T, int *count,int *ncounts) +{ + if (T) + { + (*ncounts)++; + if ((T->lchild==NULL) && (T->rchild==NULL)) + { + (*count)++; // 对叶子结点计数 + } + CountLeaf(T->lchild, count,ncounts); + CountLeaf(T->rchild, count,ncounts); + } +} + +bool QueueEmpty(LinkQueue*Q) +{ + if (Q->front == Q->rear) + { + return true; + } + else + { + return false; + } +} + +Status EnQueue(LinkQueue*Q, BiTNode e) +{ + QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) + { + return ERROR; + } + else + { + p->data = e; + p->next = NULL; + Q->rear->next = p; + Q->rear = p; + } +} + +Status DeQueue(LinkQueue*Q, BiTNode*e) +{ + if (Q->front == Q->rear) + { + return ERROR; + } + QNode *p = Q->front->next; //指向队头; + *e = p->data; + Q->front->next = p->next; + if (Q->rear == p) + { + Q->rear = Q->front; + } + free(p); + return OK; +} + +Status InitQueue(LinkQueue *Q) +{ + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!Q->front) + { + return ERROR; + } + Q->front->next = NULL; + return OK; +} + + +bool isComplete(BiTree T) +{ + if (!T) + { + return true; //首先判断空树.根据完全二叉树定义,判断空树为完全二叉树; + } + + int isL = 0; //用以标记; + LinkQueue q; + InitQueue(&q); //建立空队列; + BiTree t = T; + EnQueue(&q, *T);//树根结点入队列; + + //开始广度优先遍历; + while (QueueEmpty(&q)!=NULL) + { + DeQueue(&q,t); + if (T->lchild != NULL) + { + if (t->lchild != NULL) + { + /*如果isL==1则说明此结点的左边已经有没有孩子或只有左孩子的结点, + 此时该结点还有孩子,则说明此树为不完全二叉树。*/ + if (isL == 1) + { + return false; + } + EnQueue(&q, *(t->lchild)); + } + if (t->rchild != NULL) + { + EnQueue(&q, *(t->rchild)); + } + if (t->lchild == NULL && t->rchild != NULL) + { + return false; + } + if (t->lchild != NULL&&t->rchild == NULL||t->lchild==NULL&&t->rchild==NULL) + { + /*从左向右遍历时如果左边出现结点的 + 左孩子不为空但右孩子为空,或左孩子右孩子都为空则isL=1;*/ + isL = 1; + } + } + } + return true; +} + +//输出后序式表达式; +void printBiTree(BiTree T) +{ + printf("后序式表达式为:\n"); + PostOrderTraverse(T); + printf("\n"); +} + +void printLeafNumber(BiTree T,int count,int ncounts) +{ + printf("叶子结点个数为:%d\n", count); + printf("非叶子结点个数为:%d\n", ncounts - count); +} + +void printIsComplete(BiTree T) +{ + if (isComplete(T) == true) + { + printf("此树是完全二叉树"); + } + else + { + printf("此树不是完全二叉树"); + } +} diff --git a/2017-1/luyj/BinaryTrees.h b/2017-1/luyj/BinaryTrees.h new file mode 100644 index 00000000..88a76308 --- /dev/null +++ b/2017-1/luyj/BinaryTrees.h @@ -0,0 +1,96 @@ +#include +#include + + +extern int i; +extern int high; +extern int wide[50]; +extern int swide; + + +typedef char TElemType; + + + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum +{ + true, + false +}bool; + +/*=========二叉树的二叉链表存储表示===========*/ +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild, *rchild; +}BiTNode, *BiTree; + +/*===========队列的链式存储表示===========*/ +typedef struct QNode +{ + BiTNode data; + struct QNode *next; +}QNode, *QueuePtr; + +typedef struct +{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +//变量初始化 +void valInit(int level, int wide[50], int count, int ncounts); + +//将字符从字符串中取出; +char AbstractWord(char*s); + + + +/*=========队列基本操作=========*/ +Status InitQueue(LinkQueue *Q); +Status EnQueue(LinkQueue*Q, BiTNode e); +Status DeQueue(LinkQueue*Q, BiTNode*e); +bool QueueEmpty(LinkQueue*Q); + + +/*===========二叉树的基本操作===========*/ +//建立二叉树; +Status CreateBiTree(BiTree *T, char *s); +//以后序式输出; +Status PostOrderTraverse(BiTree T); +//摧毁二叉树; +Status destoryBiTree(BiTree T); + + +/*===========二叉树相关算法实现===========*/ +//递归计算二叉树高度; +int getHigh(BiTree T); +int Max(int x, int y); +//递归计算二叉树宽度; +int getWide(BiTree T,int level); +// 递归计算叶子结点; +void CountLeaf(BiTree T, int *count,int *ncounts); +//判断是否为完全二叉树.运用队列对二叉树进行广度优先遍历。 +bool isComplete(BiTree); + + +/*=========输出函数整合=========*/ + +//输出后序式; +void printBiTree(BiTree T); +//输出叶子及非叶子结点数目; +void printLeafNumber(BiTree T, int count, int ncounts); +//判断并输出是否为完全二叉树; +void printIsComplete(BiTree T); + + + + + diff --git a/2017-1/luyj/Graph.c b/2017-1/luyj/Graph.c new file mode 100644 index 00000000..ef53cc87 --- /dev/null +++ b/2017-1/luyj/Graph.c @@ -0,0 +1,222 @@ +#include "Graph.h" + + +/*=========队列的基本操作=========*/ +Status InitQueue(LinkQueue *q) +{ + q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!q->front) + { + return ERROR; + } + q->front->next = q->rear->next = NULL; + return OK; +} + +Status EnQueue(LinkQueue*q, QElemType e) +{ + QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) + { + return ERROR; + } + else + { + p->data = e; + p->next = NULL; + p->pre = q->front; + q->rear->next = p; + q->rear = p; + } +} + +Status DeQueue(LinkQueue*q, QElemType*e) +{ + if (q->front == q->rear) + { + return ERROR; + } + q->front = q->front->next; + *e = q->front->data; + return OK; +} + +bool QueueEmpty(LinkQueue*q) +{ + if (q->front == q->rear) + { + return TRUE; + } + else + { + return FALSE; + } +} + +Status DestroyQueue(LinkQueue*q) +{ + while (q->front) + { + q->rear = q->front->next; + free(q->front); + q->front = q->rear; + } + return OK; +} +/*=========图的基本操作=========*/ + +Status InsertArc(MGraph *g,int v1,int v2) +{ + int i = 0; + int j = 0; + if (LocateVex(g, v1, &i) == TRUE && LocateVex(g, v2, &j) == TRUE) + { + g->arcs[i][j].adj = 1;//两点之间有连线,弧值为1; + g->arcs[j][i] = g->arcs[i][j]; + } +} +bool LocateVex(MGraph *g,int v,int *i) +{ + int m; + for (m = 0; m <= g->vexnum; m++) + { + if (g->vexs[m]==v) + { + *i = m; + return TRUE; + } + } + return FALSE; +} + +//构建图 +Status CreateUDN(MGraph *g) +{ + int i; + int j; + //根据用例直接赋值。 + g->vexnum = 9; + g->arcnum = 12; + for (i = 1; i <= g->vexnum; i++) + { + g->vexs[i] = i;//构建顶点向量; + } + for (i = 0; i <= g->vexnum; i++)//初始化邻接矩阵; + { + for (j = 0; j <= g->vexnum; j++) + { + g->arcs[i][j].adj = INFINITY; + g->arcs[i][j].info = NULL; + } + } + //构建邻接矩阵; + InsertArc(g, 1, 2); + InsertArc(g, 1, 3); + InsertArc(g, 1, 4); + InsertArc(g, 1, 7); + InsertArc(g, 2, 3); + InsertArc(g, 4, 5); + InsertArc(g, 4, 6); + InsertArc(g, 5, 6); + InsertArc(g, 6, 8); + InsertArc(g, 7, 8); + InsertArc(g, 7, 9); + InsertArc(g, 8, 9); + + return OK; +} + +//找出第一个邻接点 +int FirstAdjVex(MGraph *g, int u) +{ + int i; + for (i = 1; i <= g->vexnum; i++) + { + if (g->arcs[u][i].adj == 1) + { + return i; + } + } + return -1; +} + +//找出下一个邻接点 +int NextAdjvex(MGraph *g, int u, int w) +{ + int i; + for (i = w + 1; i <= g->vexnum; i++) + { + if (g->arcs[u][i].adj == 1) + { + return i; + } + } + return -1; +} +//广度优先遍历图,求两点a,b间的最短路径; +Status BFSTraverse(MGraph*g, LinkQueue *q,int a,int b) +{ + + int v; + int u = 0; + int w = 0; + int m = 0; + int n = 0; + bool visited[MAX_VERTEX_NUM]; + for (v = 1; v <= g->vexnum; v++) + { + visited[v] = FALSE; //标记数组,标记图中已访问的点 + } + + EnQueue(q, a); //a先入队列; + while (QueueEmpty(q)!= TRUE) + { + DeQueue(q, &u); + for (w = FirstAdjVex(g, u); w >=0; w = NextAdjvex(g, u, w)) + { + if (visited[w] == FALSE)//判断w是否已经访问过 + { + visited[w] = TRUE; + EnQueue(q, w); + } + if (w == b) + { + break; + } + } + if (w == b) + { + break; + } + } +} + +Status print(LinkQueue *q,int a) +{ + if (q->rear->data == a) + { + printf("%d->%d\n", a, a); + return OK; + } + + int i = 0; + int j; + int num[MAX_VERTEX_NUM] = { 0 }; + while (q->rear->data!=a)//倒序进入数组 + { + num[i] = q->rear->data; + q->rear = q->rear->pre; + i++; + } + printf("%d", a); + for (j = i - 1; j >= 0; j--) + { + printf("->%d", num[j]); + } + + + printf("\n"); + return OK; +} + + diff --git a/2017-1/luyj/Graph.h b/2017-1/luyj/Graph.h new file mode 100644 index 00000000..a6737c54 --- /dev/null +++ b/2017-1/luyj/Graph.h @@ -0,0 +1,71 @@ +#include +#include + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum +{ + TRUE, + FALSE +}bool; + + +/*=========图的数组存储结构=========*/ +#define MAX_VERTEX_NUM 10 +#define VRType int +#define InfoType int +#define VertexType int +#define INFINITY -1 + +typedef struct ArcCell +{ + VRType adj; + InfoType *info; +}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; + +typedef struct +{ + VertexType vexs[MAX_VERTEX_NUM]; + AdjMatrix arcs; + int vexnum, arcnum; //图的顶点数和弧数; +}MGraph; + + +/*=========队列的双链存储结构=========*/ +#define QElemType int + +typedef struct QNode +{ + QElemType data; + struct QNode *next; + struct QNode *pre; +}QNode, *QueuePtr; + +typedef struct LinkQueue +{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +/*=========队列的基本操作=========*/ +Status InitQueue(LinkQueue *Q); +Status EnQueue(LinkQueue*Q, QElemType e); +Status DeQueue(LinkQueue*Q, QElemType*e); +bool QueueEmpty(LinkQueue*Q); +Status DestroyQueue(LinkQueue*Q); + +/*=========图的基本操作=========*/ +bool LocateVex(MGraph *g, int v, int *i); +Status CreateUDN(MGraph *G); +int FirstAdjVex(MGraph *G, int u); +int NextAdjvex(MGraph *G, int u, int w); +Status BFSTraverse(MGraph*G, LinkQueue *Q, int a, int b); +Status print(LinkQueue *Q, int a); +Status InsertArc(MGraph *G, int v1, int v2); + + diff --git "a/2017-1/luyj/Graph\350\277\220\350\241\214\347\273\223\346\236\2341.png" "b/2017-1/luyj/Graph\350\277\220\350\241\214\347\273\223\346\236\2341.png" new file mode 100644 index 00000000..7105a72d Binary files /dev/null and "b/2017-1/luyj/Graph\350\277\220\350\241\214\347\273\223\346\236\2341.png" differ diff --git a/2017-1/luyj/SufficType.png b/2017-1/luyj/SufficType.png new file mode 100644 index 00000000..19b64e68 Binary files /dev/null and b/2017-1/luyj/SufficType.png differ diff --git a/2017-1/luyj/queue.c b/2017-1/luyj/queue.c new file mode 100644 index 00000000..fceda5c7 --- /dev/null +++ b/2017-1/luyj/queue.c @@ -0,0 +1,127 @@ +#include "queue.h" + +Status InitQueue(LinkQueue *Q) +{ + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!Q->front) + { + return ERROR; + } + Q->front->next = NULL; + return OK; +} + +Status DestroyQueue(LinkQueue*Q) +{ + while (Q->front) + { + Q->rear = Q->front->next; + free(Q->front); + Q->front = Q->rear; + } + return OK; +} + +Status EnQueue(LinkQueue*Q, QElemType e) +{ + QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) + { + return ERROR; + } + else + { + p->data = e; + p->next = NULL; + Q->rear->next = p; + Q->rear = p; + } +} + +Status DeQueue(LinkQueue*Q, QElemType*e) +{ + if (Q->front == Q->rear) + { + return ERROR; + } + QNode *p= Q->front->next; //指向队头; + *e = p->data; + Q->front->next = p->next; + if (Q->rear == p) + { + Q->rear = Q->front; + } + free(p); + return OK; +} + +bool QueueEmpty(LinkQueue*Q) +{ + if (Q->front == Q->rear) + { + return true; + } + else + { + return false; + } +} + +Status ClearQueue(LinkQueue*Q) +{ + QElemType e; + if (QueueEmpty(Q)==true) + { + return ERROR; + } + while (QueueEmpty(Q)==false) + { + DeQueue(Q,&e); + } +} + + + +int QueueLength(LinkQueue Q) +{ + int i = 0; + while (QueueEmpty(&Q) == false) + { + Q.front = Q.front->next; + i++; + } + return i; +} + +Status GetHead(LinkQueue Q, QElemType *e) +{ + if (QueueEmpty(&Q) == true) + { + return ERROR; + } + else + { + QNode *p = Q.front->next; + *e = p->data; + return OK; + } +} + +Status QueueTraverse(LinkQueue Q) +{ + if (QueueEmpty(&Q) == true) + { + printf("EORROR"); + } + else + { + QNode*p = Q.front->next; + while (p != Q.rear) + { + printf("%d", p->data); + p = p->next; + } + return OK; + } +} + diff --git a/2017-1/luyj/queue.h b/2017-1/luyj/queue.h new file mode 100644 index 00000000..7230c938 --- /dev/null +++ b/2017-1/luyj/queue.h @@ -0,0 +1,39 @@ +#include +#include +#include + +#define QElemType int +typedef struct QNode +{ + QElemType data; + struct QNode *next; +}QNode, *QueuePtr; + +typedef struct +{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum +{ + true, + false +}bool; + +Status InitQueue(LinkQueue *Q); +Status DestroyQueue(LinkQueue*Q); +Status EnQueue(LinkQueue*Q, QElemType e); +Status DeQueue(LinkQueue*Q, QElemType*e); +bool QueueEmpty(LinkQueue*Q); +Status ClearQueue(LinkQueue*Q); +int QueueLength(LinkQueue Q); +Status GetHead(LinkQueue Q, QElemType *e); +Status QueueTraverse(LinkQueue Q); diff --git a/2017-1/luyj/queue.png b/2017-1/luyj/queue.png new file mode 100644 index 00000000..006db1f1 Binary files /dev/null and b/2017-1/luyj/queue.png differ diff --git a/2017-1/luyj/s_3.2.5.c b/2017-1/luyj/s_3.2.5.c new file mode 100644 index 00000000..40b85b0f --- /dev/null +++ b/2017-1/luyj/s_3.2.5.c @@ -0,0 +1,55 @@ +#include "3.2.5.h" + +int main() +{ + int i = 0, j = 0; + SqStack s1; + SqStack s2; + SqStack1 s3; + + InitStack(&s1); + InitStack(&s2); + InitStack1(&s3); + + char suffix[50]; + char exp[50]; + printf("请输入0-9以内数的简单运算式\n"); + gets(exp); + while (exp[i] != '\0') + { + if (exp[i] == '/'&&exp[i + 1] == '0') + { + printf("ERROR\n"); + printf("请重新输入\n"); + gets(exp); + + } + i++; + } + printf("\n"); + /*for (i = 0; i < 50; i++) + { + exp[i] = '#'; + }*/ + printf("后缀式形成过程:\n"); + transform(suffix, exp, &s1); + EvaluateExpression(&s2, &s3, suffix); + printf("结果:"); + printf("%2lf", *s3.base); + + return 0; +} + + + + + + + + + + + + + + diff --git a/2017-1/luyj/s_3.2.5SuffixType.c b/2017-1/luyj/s_3.2.5SuffixType.c new file mode 100644 index 00000000..f5c3dacb --- /dev/null +++ b/2017-1/luyj/s_3.2.5SuffixType.c @@ -0,0 +1,73 @@ +#include "3.2.5.h" + +int main() +{ + int i = 0, j = 0, k = 0, l = 0, m = 0, n = 0, p = 0; + SqStack s1; + SqStack s2; + SqStack1 s3; + + InitStack(&s1); + InitStack(&s2); + InitStack1(&s3); + + char suffix[50][50]; + char exp[50][50]; + char exp1[50]; + char ch; + printf("请输入正整数的简单运算式\n"); + gets(exp1); + while (exp1[i] != '\0') + { + if (exp1[i] == '/'&&exp1[i + 1] == '0') + { + printf("ERROR\n"); + printf("请重新输入\n"); + gets(exp); + + } + i++; + } + + printf("\n"); + for (k = 0; k <= i; k++) + { + ch = exp1[k]; + if (IN1(ch) == true) + { + l = 1; + } + if (l == 1) + { + while (p < k) + { + exp[m][n] = exp1[p]; + n++; + p++; + } + if (n != 0) + { + exp[m][n] = '\0'; + m++; + } + n = 0; + exp[m][0] = exp1[k]; + exp[m][1] = '\0'; + p++; + m++; + } + l = 0; + } + exp[m][0] = '\0'; + /*for (i = 0; i < 50; i++) + { + exp[i] = '#'; + }*/ + printf("后缀式形成过程:\n"); + transform(suffix, exp, &s1); + EvaluateExpression(&s2, &s3, suffix); + printf("结果:"); + printf("%lf", *s3.base); + + return 0; +} diff --git a/2017-1/luyj/s_AVLTree.c b/2017-1/luyj/s_AVLTree.c new file mode 100644 index 00000000..b804a44e --- /dev/null +++ b/2017-1/luyj/s_AVLTree.c @@ -0,0 +1,39 @@ +#include "AVLTree.h" +extern int flag; + +int main() +{ + int i; + BSTree *t = (BSTree*)malloc(sizeof(BSTree)); + Boolean taller = FALSE; + Boolean lower = FALSE; + *t = NULL; + ElemType input[12] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + ElemType search[5] = { 13,8,5,20,6 }; + char d[4] = "\n"; + FILE*pfile = fopen("AVLOutput.txt", "a"); + for (i = 0; i < 12; i++) + { + InsertAVL(t, input[i], &taller); + } + PreOrderTraverse(*t,pfile); + fputs(d, pfile); + + for (i = 0; i < 5; i++) + { + if (FALSE == SearchBST(*t, search[i])) + { + InsertAVL(t, search[i], &taller); + flag = 0; + PreOrderTraverse(*t,pfile); + fputs(d, pfile); + } + else + { + DeleteAVL(t, search[i], &lower); + flag = 0; + PreOrderTraverse(*t,pfile); + fputs(d, pfile); + } + } +} \ No newline at end of file diff --git a/2017-1/luyj/s_BiSTree.c b/2017-1/luyj/s_BiSTree.c new file mode 100644 index 00000000..30a233f0 --- /dev/null +++ b/2017-1/luyj/s_BiSTree.c @@ -0,0 +1,36 @@ +#include "BiSTree.h" +extern int flag; + +int main() +{ + BiTree *T = (BiTree*)malloc(sizeof(BiTree)); + *T = NULL; + int i; + int j; + int k; + char d = '\n'; + ElemType input[12] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + ElemType found[5] = { 13, 8, 5, 20, 6 }; + FILE*pfile = fopen("BSTOutput.txt", "a"); + //建立顺序二叉树; + for (i = 0; i < 12; i++) + { + BiTree p = (BiTree)malloc(sizeof(BiTree)); + if (SearchBST(*T, input[i].key, NULL, &p) == false) + { + InsertBST(T, input[i], p); + } + } + PreOrderTraverse(*T, pfile); + fwrite(&d, sizeof(d), 1, pfile); + for (i = 0; i < 5; i++) + { + searchInDelete(T, found[i]); + flag = 0; + PreOrderTraverse(*T, pfile); + fwrite(&d, sizeof(d), 1, pfile); + } + fclose(pfile); + pfile = NULL; + return 0; +} \ No newline at end of file diff --git a/2017-1/luyj/s_BinaryTree.c b/2017-1/luyj/s_BinaryTree.c new file mode 100644 index 00000000..b79f438a --- /dev/null +++ b/2017-1/luyj/s_BinaryTree.c @@ -0,0 +1,22 @@ +#include "BinaryTree.h" + +int main() +{ + BiTree T1 = NULL; + char*s1 = "ABDG EH I K C F "; + printf("测试用例1为:\n"); + puts(s1); + printf("后序式表达式为:\n"); + CreateBiTree(&T1, s1); + PostOrderTraverse(T1); + destoryBiTree(T1); + i = 0; + BiTree T2 = NULL; + char*s2 = "ABD F CE "; + printf("测试用例2为:\n"); + puts(s2); + printf("后序式表达式为:\n"); + CreateBiTree(&T2, s2); + PostOrderTraverse(T2); + destoryBiTree(T2); +} diff --git a/2017-1/luyj/s_BinaryTrees.c b/2017-1/luyj/s_BinaryTrees.c new file mode 100644 index 00000000..587a2397 --- /dev/null +++ b/2017-1/luyj/s_BinaryTrees.c @@ -0,0 +1,81 @@ +#include "BinaryTrees.h" + +int main() +{ + int level = 0; + int j; + int count = 0; + int ncounts = 0; + + + /*=========用例一=========*/ + BiTree T1 = NULL; + char*s1 = "ABDG EH I K C F "; + printf("测试用例1为:\n"); + puts(s1); + CreateBiTree(&T1, s1); + printBiTree(T1); + printf("二叉树的高度为:%d\n", getHigh(T1)); + printf("二叉树的宽度为:%d\n", getWide(T1, level)); + CountLeaf(T1, &count, &ncounts); + printLeafNumber(T1, count, ncounts); + printIsComplete(T1); + destoryBiTree(T1); + + + + + /*=========用例二=========*/ + valInit(&level, wide, &count, &ncounts); + BiTree T2 = NULL; + char*s2 = "ABD F CE "; + printf("\n\n\n测试用例2为:\n"); + puts(s2); + + CreateBiTree(&T2, s2); + printBiTree(T2); + printf("二叉树的高度为:%d\n", getHigh(T2)); + printf("二叉树的宽度为:%d\n", getWide(T2, level)); + CountLeaf(T2, &count, &ncounts); + printLeafNumber(T2, count, ncounts); + printIsComplete(T2); + destoryBiTree(T2); + + + /*=========新增用例三=========*/ + valInit(&level, wide, &count, &ncounts); + BiTree T3 = NULL; + char*s3 = "ABC D E "; + printf("\n\n\n测试用例3为:\n"); + puts(s3); + + CreateBiTree(&T3, s3); + printBiTree(T3); + printf("二叉树的高度为:%d\n", getHigh(T3)); + printf("二叉树的宽度为:%d\n", getWide(T3, level)); + CountLeaf(T3, &count, &ncounts); + printLeafNumber(T3, count, ncounts); + printIsComplete(T3); + destoryBiTree(T3); + + /*=========新增用例四=========*/ + valInit(&level, wide, &count, &ncounts); + BiTree T4 = NULL; + char*s4 = "ABCH D EFK G "; + printf("\n\n\n测试用例4为:\n"); + puts(s4); + CreateBiTree(&T4, s4); + printBiTree(T4); + printf("二叉树的高度为:%d\n", getHigh(T4)); + printf("二叉树的宽度为:%d\n", getWide(T4, level)); + CountLeaf(T4, &count, &ncounts); + printLeafNumber(T4, count, ncounts); + printIsComplete(T4); + destoryBiTree(T4); +} + + + + + + diff --git a/2017-1/luyj/s_Graph.c b/2017-1/luyj/s_Graph.c new file mode 100644 index 00000000..71727182 --- /dev/null +++ b/2017-1/luyj/s_Graph.c @@ -0,0 +1,23 @@ +#include "Graph.h" + +int main() +{ + int i; + int j; + + MGraph graph; + CreateUDN(&graph); + for (i = 1; i <= graph.vexnum; i++) + { + for (j = 1; j <= graph.vexnum; j++) + { + LinkQueue Q; + InitQueue(&Q); + printf("%d<->%d:", i, j); + BFSTraverse(&graph, &Q, i, j); + print(&Q, i); + DestroyQueue(&Q); + } + } + +} \ No newline at end of file diff --git a/2017-1/luyj/s_queue.c b/2017-1/luyj/s_queue.c new file mode 100644 index 00000000..0337c1b1 --- /dev/null +++ b/2017-1/luyj/s_queue.c @@ -0,0 +1,42 @@ +#include "queue.h" + +int main() +{ + int n, n1; + int i; + QElemType e = 0; + LinkQueue q; + InitQueue(&q); + srand(time(0)); + n = rand() % 9 + 1; + for (i = 0; i < n; i++) + { + n1 = rand() % 9 + 1; + EnQueue(&q, n1); + } + printf("随机生成队列q:\n"); + QueueTraverse(q); + printf("\n"); + printf("队列长度(加上头结点):\n"); + printf("%d", QueueLength(q)); + printf("\n队列q队头元素为:\n"); + GetHead(q, &e); + printf("%d\n", e); + printf("删除队头元素,新队列为:\n"); + DeQueue(&q, &e); + QueueTraverse(q); + printf("\n"); + if (QueueEmpty(&q) == false) + { + printf("目前队列不为空\n"); + } + printf("清空队列\n"); + { + ClearQueue(&q); + } + if (QueueEmpty(&q) == true) + { + printf("目前队列为空\n"); + } + +} \ No newline at end of file diff --git a/2017-1/luyj/s_work13.c b/2017-1/luyj/s_work13.c new file mode 100644 index 00000000..63845b95 --- /dev/null +++ b/2017-1/luyj/s_work13.c @@ -0,0 +1,20 @@ +#include "work13.h" + +int main() +{ + srand(time(0)); + int n; + LinkList list = (LinkList)malloc(sizeof(LNode)); + LinkList list1 = (LinkList)malloc(sizeof(LNode));//奇序号链表头结点; + LinkList list2 = (LinkList)malloc(sizeof(LNode));//偶序号链表头结点; + n = rand() % 9 + 3;//确保链表list大小大于等于2; + printf("随机生成线性链表:\n"); + CreateList_L(list, n); + output(list); + ParitySequence(list, list1, list2); + printf("\n奇序列的循环链表(循环一次的输出):\n"); + output1(list1); + printf("\n偶序列的循环链表(循环一次的输出):\n"); + output1(list2); + +} \ No newline at end of file diff --git a/2017-1/luyj/work13.c b/2017-1/luyj/work13.c new file mode 100644 index 00000000..7e6e8cb5 --- /dev/null +++ b/2017-1/luyj/work13.c @@ -0,0 +1,90 @@ +#include "work13.h" + +Status output(LinkList L) +{ + LinkList p = L; + if (L->next == NULL) + { + return ERROR; + } + LinkList Line; + Line = L; + while (Line->next != NULL) + { + printf("%d", Line->data); + printf("\n"); + if (Line->next != NULL) + { + Line = Line->next; + } + } + printf("%d", Line->data); + return OK; +} + +Status output1(LinkList L) +{ + LinkList p = L; + if (L->next == NULL) + { + return ERROR; + } + LinkList Line; + Line = L; + while (Line->next !=p) + { + printf("%d", Line->data); + printf("\n"); + if (Line->next != NULL) + { + Line = Line->next; + } + } + printf("%d", Line->data); + return OK; +} +//建立一个测试线性链表(第一个链结点为指针list的链表) +void CreateList_L(LinkList L, int n) +{ + LinkList L1 = L; + L1->data = (int)rand() % 1024; + LinkList p; + int i; + for (i = 0; i < n-1; i++) + { + p = (LinkList)malloc(sizeof(LNode)); //生成新结点; + p->data = (int)rand() % 1024; + L1->next= p; + L1 = L1->next; + p->next=NULL; + } +} + + +void ParitySequence(LinkList list,LinkList list1,LinkList list2) +{ + + + LNode*p1 = list1; + LNode*p2 = list2; + LNode*p = list; + list1->data = 0; + list2->data = 0; + while (p!=NULL) + { + p1->next = p; + p1 = p1->next; + list1->data++; + p = p->next; + if (p!= NULL) + { + p2->next = p; + p2 = p2->next; + list2->data++; + p = p->next; + } + } + p1->next = list1; + p2->next = list2; +} + diff --git a/2017-1/luyj/work13.h b/2017-1/luyj/work13.h new file mode 100644 index 00000000..1d658a21 --- /dev/null +++ b/2017-1/luyj/work13.h @@ -0,0 +1,36 @@ +//说明: +//时间复杂度: +//ParitySequence函数在将list线性链表分解成两个循环链表时,只用了一个循环遍历一次线性表。其时间复杂度约为O(n)(n为list链表的长度),此时,时间复杂度应为最小。 +//空间复杂度: +//在创建两个循环链表(list1,list2)时,只分别重新动态分配了一个头结点。创建两个循环链表的过程,属分解list的过程,list1,list2初头结点外其他结点,没有重新分配空间,而是直接将next指针指向list对应结点。 + +#include +#include +#include + +typedef int ElemType; + +typedef struct LNode +{ + ElemType data; + struct LNode *next; +}LNode, *LinkList; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +//输出单链表; +Status output(LinkList L); + +//输出循环单链表的一个周期; +Status output1(LinkList L); + +//建立一个测试线性链表(第一个链结点为指针list的链表) +void CreateList_L(LinkList L, int n); + +//将线性单链表分解到奇偶序列循环链表; +void ParitySequence(LinkList list, LinkList list1, LinkList list2); diff --git "a/2017-1/luyj/\345\210\206\350\247\243\351\223\276\350\241\250.png" "b/2017-1/luyj/\345\210\206\350\247\243\351\223\276\350\241\250.png" new file mode 100644 index 00000000..dbfdb6ac Binary files /dev/null and "b/2017-1/luyj/\345\210\206\350\247\243\351\223\276\350\241\250.png" differ diff --git "a/2017-1/luyj/\345\233\276.png" "b/2017-1/luyj/\345\233\276.png" new file mode 100644 index 00000000..803ff3b5 Binary files /dev/null and "b/2017-1/luyj/\345\233\276.png" differ diff --git "a/2017-1/luyj/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/luyj/\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..fa4f2ec0 Binary files /dev/null and "b/2017-1/luyj/\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/luyj/\350\277\220\350\241\214\347\273\223\346\236\2342.png" "b/2017-1/luyj/\350\277\220\350\241\214\347\273\223\346\236\2342.png" new file mode 100644 index 00000000..b20637f3 Binary files /dev/null and "b/2017-1/luyj/\350\277\220\350\241\214\347\273\223\346\236\2342.png" differ diff --git a/2017-1/lyx/13/13.PNG b/2017-1/lyx/13/13.PNG new file mode 100644 index 00000000..d1df289b Binary files /dev/null and b/2017-1/lyx/13/13.PNG differ diff --git a/2017-1/lyx/13/13.c b/2017-1/lyx/13/13.c new file mode 100644 index 00000000..7fb9637e --- /dev/null +++ b/2017-1/lyx/13/13.c @@ -0,0 +1,30 @@ +#include +#include +#include "13.h" +int main () +{ + LinkList L; + LinkList L1; + LinkList L2; + L = (LinkList)malloc(sizeof(LNode)); + L1 = (LinkList)malloc(sizeof(LNode)); + L2 = (LinkList)malloc(sizeof(LNode)); + + printf("链表长度为%d\n",10); + printf("Traverse\n"); + Traverse(L); + printf("遍历完成\n"); + + Distribute(L,L1,L2); + printf("\n"); + + printf("遍历奇数链表\n"); + Traverse_separate(L1); + printf("\n"); + + printf("遍历偶数链表\n"); + Traverse_separate(L2); + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/2017-1/lyx/13/13.h b/2017-1/lyx/13/13.h new file mode 100644 index 00000000..1b7c1997 --- /dev/null +++ b/2017-1/lyx/13/13.h @@ -0,0 +1,19 @@ +#include +#include + +typedef int ElenType; +typedef struct LNode{ + ElenType data; + struct LNode *next; +}LNode , *LinkList; +typedef enum { + OK, + ERROR, + OVERFLOW +}Status; + +//函数 +Status CreateList(LinkList L,int n);//创建一个链表 +Status Traverse(LinkList L);//遍历链表 +Status Distribute(LinkList L1,LinkList L2,LinkList L3);//创建三个链表 +Status Traverse_separate(LinkList L);//遍历分开的链表 \ No newline at end of file diff --git a/2017-1/lyx/13/13shixian.c b/2017-1/lyx/13/13shixian.c new file mode 100644 index 00000000..32ac5270 --- /dev/null +++ b/2017-1/lyx/13/13shixian.c @@ -0,0 +1,89 @@ +#include +#include +#include "13.h" + +Status CreateList(LinkList L,int n) +{//创建一个链表 + printf("创建链表\n"); + L->next = NULL;//头结点设为空 + int i; + i = 0; + LinkList a;//设创建的链表叫a + for(i = n;i > 0; i--) + { + a = (LinkList)malloc(sizeof(LNode));//动态生成新的结点的内存 + a->data = i;//初始化链表 + a->next = L->next;//更新链表 + L->next = a;//使L的尾指针指向a + } + printf("创建链表完成\n"); + return OK;//创建链表成功 +} +Status Traverse(LinkList L) +{//遍历链表 + LinkList b; + b = (LinkList)malloc(sizeof(LNode));//动态生成新的结点 + if(b->next == NULL)//防止链表为空 + { + return ERROR;//若为空返回错误 + } + b = L->next;//L尾指针指向b + while(b->next != NULL)//判断是否结束 + { + printf("%d\t",b->data);//输出链表元素 + b = b->next;//指向下一个元素 + } + printf("%d\t",b->next); + printf("\n"); + printf("遍历链表\n"); + return OK; +} +Status Distribute(LinkList L1,LinkList L2,LinkList L3) +{//创建三个链表 + LinkList a = L1; //p1,list1 + LinkList b = L2; //p2,list2 + LinkList c = L3; //p,list + L1->data = 0;//初始L1 + L2->data = 0;//初始L2 + c = c->next;//初始c的指针指向 + while(c != NULL)//当c不为空时 + { + a->next = c;//a的尾指针指向c + a = a->next;//a指向下一个元素 + L1->data++;//L1为data自加的值 + c = c->next;//c指向下一个 + if(c != NULL)//当c不为空时 + { + b->next = c; + b = a->next; + L2->data++; + c = c->next; + } + } + a->next = L1; + b->next = L2; + return OK; +} +Status Traverse_separate(LinkList L) +{//遍历分开的链表 + LinkList m = L; + LinkList n = L; + if(m->next == NULL)//判断为空的情况 + { + return ERROR; + } + printf("链表长度为%d\n",n->data); + n = n->next;//指向下一个元素 + while(n->next != m) + { + printf("%d\t",n->data); + if(n->next != NULL) + { + n = n->next; + } + } + printf("%d",n->data); + printf("\n"); + printf("分配结束\n"); + return OK; +} \ No newline at end of file diff --git "a/2017-1/lyx/13/\345\210\206\346\236\220.txt" "b/2017-1/lyx/13/\345\210\206\346\236\220.txt" new file mode 100644 index 00000000..942b7c25 --- /dev/null +++ "b/2017-1/lyx/13/\345\210\206\346\236\220.txt" @@ -0,0 +1,2 @@ +Traverse_separate函数在将线性链表分解成两个循环链表时,只需要遍历一次线性表,所以时间复杂度最小,时间最优化。 +Distribute函数创建了三个链表,然后一个个分配,但是指针用完后被释放掉了,利用了最少的空间,所以空间最优化。 \ No newline at end of file diff --git a/2017-1/lyx/3.2.5new.c b/2017-1/lyx/3.2.5new.c new file mode 100644 index 00000000..29d9f9e5 --- /dev/null +++ b/2017-1/lyx/3.2.5new.c @@ -0,0 +1,173 @@ +#include +#include +#include //生成随机值 +//中缀表达式求值 +//先转为后缀表达式再求值;为简单起见,使用栈的循序存储结构实现 + +//栈的顺序存储结构,用一维数组实现 +#define OK 1 +#define ERROR -1 +#define TRUE 1 +#define FALSE 0 +#define MAXSIZE 10 +typedef int Status; +typedef char ElemType; +typedef struct +{ + ElemType data[MAXSIZE]; + int top;//栈顶指针 +}Stack; + +Status InitStack(Stack *S) +{ //初始化 + int i; + for(i=0;idata[i]=NULL; //初始化为空 + } + S->top=-1; + return OK; +} + +Status CreateStack(Stack *S,int n) +{ //创建一个长度为n的堆栈 + if(n>MAXSIZE || n<1) + { + printf("输入长度有误!\n"); + return ERROR; + } + srand(time(0)); + int i; + for(i=0;idata[i]=rand()%100+1;//随机生成元素 + } + S->top=n-1; + return OK; +} + +Status push(Stack *S,ElemType e) +{//压栈操作 + if(MAXSIZE-1==S->top)//判断是否栈满 + { + printf("栈已满\n"); + return ERROR; + } + //栈顶指向的元素有值 + ++(S->top); + S->data[S->top]=e; + return OK; +} + +Status pop(Stack *S,ElemType *e) +{//出栈 + //将栈顶元素出栈,传给e + if(-1==S->top) + { + printf("栈为空!\n"); + return ERROR; + } + *e=S->data[S->top]; + --(S->top); + return OK; +} + +int MidToFinal(char *mid,char *final) +{//中缀表达式转后缀表达式 + //中缀表达式为middle,要转换成后缀表达式传给last + //新建一个栈,来存储符号 + char e; + Stack S; + if(OK!=InitStack(&S)) + { + printf("初始化栈失败!\n"); + } + //当带转换的字符串*mid未终止时,循环处理 + while(*mid) + {//如果是数字,则直接输出 + if(*mid>='0' && *mid<='9') + { + *(final++)=*(mid++); + continue; + } + else if(*mid=='+' || *mid=='-' || *mid=='*' || *mid=='/' || *mid=='(' || *mid==')') + {//输入的是合法运算符号,比较之前是否有更高优先级的符号 + if(S.top==-1 || '('==*mid) + { + //当符号栈为空或遇到左括号时,符号入栈 + push(&S,*(mid++)); + continue; + } + if(')'==*mid) + { + //遇到右括号时,栈顶元素依次出栈;直到遇到第一个左括号时结束 + pop(&S,&e); + *(final++)=e; + while(pop(&S,&e) && e!='(') + { + *(final++)=e; + } + // printf("%c\n",e); + mid++; + continue; + } + //后续的处理都要取出临时的栈顶元素,与当前输入的符号*mid相比较; + //当临时栈顶元素优先级大于等于输入符号的优先级时,出栈; + //否则符号入栈(已经弹出一个,记得把弹出的元素也入栈) + pop(&S,&e); + if('+'==*mid || '-'==*mid) + { + if(e=='(') + { + push(&S,'('); + push(&S,*(mid++)); + continue; + } + else + { + *(final++)=e; + push(&S,*(mid++)); + continue; + } + } + else if('*'==*mid || '/'==*mid) + { + if('*'==e || '/'==e) + { + *(final++)=e; + push(&S,*(mid++)); + continue; + } + else + { + push(&S,e); + push(&S,*(mid++)); + continue; + } + } + + } + else + { + printf("输入的字符不合法!%c\n",*mid); + return ERROR; + } + } + //当待转换的字符已经结束时,符号栈至少还有一个元素(中缀表达式的特点:数字结尾;后缀表达式以符号结尾);将栈中的元素依次出栈 + while(S.top!=-1) + { + pop(&S,&e); + *(final++)=e; + } + //字符串的结束符! + *final='\0'; +} + +int main() +{ + char data[]="3+(5*6-7/1)*9"; + char final[]=""; + MidToFinal(data,final); + printf("%s\n",final); + return 0; +} \ No newline at end of file diff --git a/2017-1/lyx/3.2/3.2.1.c b/2017-1/lyx/3.2/3.2.1.c new file mode 100644 index 00000000..5592a907 --- /dev/null +++ b/2017-1/lyx/3.2/3.2.1.c @@ -0,0 +1,25 @@ + +//main 3.2.1.cpp +#include +#include +#include + +#include"ds 3.2.h" + +int main(int argc, char* argv[]) +{ + SqStack S; + int input; + int d; + + srand((unsigned)time(NULL));//用时间做种 + input= (int)rand() % 5000;//随机生成测试数据 + d= (int)rand() % 9;//随机生成转换数制 + printf("%d\n%d\n", input, d); + + conversion(&S, input, d); + + return 0; +} + + diff --git a/2017-1/lyx/3.2/3.2.1.h b/2017-1/lyx/3.2/3.2.1.h new file mode 100644 index 00000000..4e327b27 --- /dev/null +++ b/2017-1/lyx/3.2/3.2.1.h @@ -0,0 +1,61 @@ +//ds 3.2.h +#include +#include + +#define STACK_INIT_SIZE 100//存储空间初始分配量 +#define STACKINCREMENT 10//存储空间分配增量 +typedef int SElemType; + +typedef struct { + SElemType *base;//在构造前和销毁后,base的值为NULL + SElemType *top;//栈顶指针 + int stacksize;//当前已分配的空间,以元素为单位 +}SqStack;//定义栈 + +typedef enum { + OK, + OVERFLOW, + ERROR +}Status;//定义返回值的枚举类型 + +typedef enum { + false, + true +} bool;//定义返回值 + + //栈的基本操作函数声明 + +Status InitStack(SqStack *S); +//构造一个空栈S + +Status DestroyStack(SqStack *S) ; +//销毁S + +Status ClearStack(SqStack *S); +//把S置为空栈 + +bool StackEmpty(SqStack *S); +//若S为空栈则返回TRUE,否则返回FALSE + +int StackLength(SqStack *S); +//返回S的元素个数,即栈的长度 + +Status GetTop(SqStack *S, SElemType e); +//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR + +Status Push(SqStack *S, SElemType e); +//插入元素e为新的栈顶元素 + +Status Pop(SqStack *S, SElemType *e); +//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR + +Status StackTraverse(SqStack *S, Status(*visit)(SElemType)); +//从栈底到栈顶依次对栈中每个元素调用函数visit()。一旦visit()失败,则操作失败 + +Status visit(SElemType e); +//打印栈中元素 + +Status conversion(SqStack *S, int input, int d); +//实现数制转换 + + diff --git a/2017-1/lyx/3.2/3.2.1shixian.c b/2017-1/lyx/3.2/3.2.1shixian.c new file mode 100644 index 00000000..2836945c --- /dev/null +++ b/2017-1/lyx/3.2/3.2.1shixian.c @@ -0,0 +1,115 @@ +//ds 3.2.cpp +#include +#include +#include +#include + +#include "ds 3.2.h" + +//栈的基本操作函数定义 + +Status InitStack(SqStack *S)//构造一个空栈S +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!S->base) { + return(OVERFLOW);//存储分配失败 + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; + +}//InitStack + +Status DestroyStack(SqStack *S)//销毁S +{ + free(S->base); + S->base = NULL; + S->top = NULL; + S->stacksize = 0; + return OK; +} + + +Status ClearStack(SqStack *S)//把S置为空栈 +{ + S->top = NULL; + return OK; +} + +bool StackEmpty(SqStack *S)//若S为空栈则返回TRUE,否则返回FALSE +{ + if (S->base==S->top) + return true; + else + return false; +} + +int StackLength(SqStack *S)//返回S的元素个数,即栈的长度 +{ + return *S->top + 1; +} + +Status GetTop(SqStack *S, SElemType e)//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR +{ + if (S->top == S->base)return ERROR; + e = *(S->top - 1); + return OK; +}//GetTop + +Status Push(SqStack *S, SElemType e)//插入元素e为新的栈顶元素 +{ + if (S->top - S->base >= S->stacksize) {//栈满,追加存储空间 + //暂时不用 + //S->base=(SElemaType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); + //if(!S->base){ + return(OVERFLOW);//存储分配失败 + //} + //S->top=S->base+S->stacksize; + //S->stacksize+=STACKINCREMENT; + } + *S->top++ = e; + return OK; +}//Push + +Status Pop(SqStack *S, SElemType *e)//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR +{ + if (StackEmpty(S))return ERROR; + *e = *--S->top; + return OK; +}//Pop + + + +Status StackTraverse(SqStack *S, Status(*visit)(SElemType))//从栈底到栈顶依次对栈中每个元素调用函数visit()。一旦visit()失败,则操作失败 +{ + while (S->top > S->base) + visit(*S->base++); + printf("\n"); + return OK; +}//StackTraverse + +Status visit(SElemType e) //打印栈中元素 +{ + printf("%d ", e); + return OK; +} //visit + +Status conversion(SqStack *S, int input, int d)//实现数制转换 +{ + int output; + InitStack(S); + while (input) { + Push(S, input%d); + input = input / 8; + } + while (!StackEmpty(S)) + { + Pop(S, &output); + printf("%d", output); + } + + return OK; + +}//conversion + + diff --git a/2017-1/lyx/3.2/3.2.2.c b/2017-1/lyx/3.2/3.2.2.c new file mode 100644 index 00000000..e62e11d8 --- /dev/null +++ b/2017-1/lyx/3.2/3.2.2.c @@ -0,0 +1,28 @@ +#include"3.2.2.h" +int main () +{ + SqStack s;//构造空栈 + Status ret;//返回值 + int x=0;//定义整数x + srand(time (0));//随机数 + x = rand()%10;//使随机数保证在0-9之间 + char test[10];//定义一个字符串,大小为10 + char s_test[6] = { '{','}','(',')','[',']'};//定义字符串,并枚举出字符 + for(int i = 0;i +#include +#include//生成随机数 + +#define STACK_INIT_SIZE 100//定义宏常量 + +typedef char SElemType;//定义SElemType为字符串 +typedef enum +{ OK, + ERROR, + OVERFLOW +}Status;//枚举可能的现象 +typedef struct _SqStack +{ SElemType *top; + SElemType*base; + int stacksize; +}SqStack;//定义结构体 +typedef enum +{ true, + false +}bool;//定义布尔 + +Status InitStack(SqStack *s);//构造一个空栈 +Status brackets(SqStack *s,char*test);//括号匹配函数 +bool StackEmpty(SqStack *s);//若栈s为空栈,则返回TRUE,否则返回FALSE +Status Pop(SqStack *s,SElemType *e);//若栈不空,则删除s的栈顶元素,用e返回其值,并返回OK;否则返回ERROR +Status GetTop(SqStack *s,SElemType *e);//若栈不空,则用e返回s的栈顶元素,并返回OK;否则返回ERROR +Status Push(SqStack *s,SElemType e);//插入元素e为新的栈顶元素 + + + + + + diff --git a/2017-1/lyx/3.2/3.2.2shixian.c b/2017-1/lyx/3.2/3.2.2shixian.c new file mode 100644 index 00000000..d8e66af7 --- /dev/null +++ b/2017-1/lyx/3.2/3.2.2shixian.c @@ -0,0 +1,176 @@ +#include"3.2.2.h" +Status Push(SqStack *s,SElemType e) +{//插入元素e为新的栈顶元素 + if(s->top - s->base > s->stacksize){//栈满,追加存储空间 + s->base = (SElemType*)realloc(s->base,(s->stacksize + 2*STACK_INIT_SIZE)*sizeof(SElemType));//追加存储空间 + if(!s->base){//若溢出 + return OVERFLOW; + } + s->top = s->base + s->stacksize;//更新栈顶元素 + s->stacksize += 2 * STACK_INIT_SIZE;//追加存储空间 + } + *s->top = e;//插入元素e为新的栈顶元素 + s->top++;//s更新,指向top元素 + return OK; +}//Push + +Status InitStack(SqStack *s) +{//构造一个空栈 + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));//分配存储空间 + if(!s->base){//若溢出 + return OVERFLOW; + } + s->top = s->base;//使栈顶指针等于栈底指针 + s->stacksize = STACK_INIT_SIZE;//中间量等于申请的栈的大小 + return OK; +}//InitStack + +Status brackets(SqStack *s,char*test) +{//括号匹配函数 + int i,j=0;//定义整数i,j,使j的值等于0 + SElemType e,m,n;//定义字符串emn + for(i=0;test[i] != '\0';i++){//当test中还有字符串时 + switch(test[i])//switch选择语句 + { + case'{': + case'[': + case'(': + {//若进入栈的字符为{[( + Push(s,test[i]);//插入栈中为新的栈顶元素 + SElemType*l = s->base;//中间的指针指向栈底指针 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + break;//跳出此次switch语句 + } + case'}'://若进入栈的字符为} + { + GetTop(s,&e);//得到栈顶元素 + if(e == '{')//如果栈顶元素为{ + { + if(StackEmpty == false)//栈中还有元素,即不是空栈 + { + Pop(s,&m);//删除栈顶元素 + SElemType*l = s->base;//更新栈底元素 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + } + } + else//如果得到的栈顶元素不是{ + { + Push(s,test[i]);//插入得到的元素为新的栈顶元素 + Pop(s,&m);//删除原来的栈顶元素名称 + SElemType*l = s->base;//更新栈底元素 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + } + break;//跳出此次switch语句 + } + case']'://若进入栈的字符为] + { + GetTop(s,&e);//得到栈顶元素 + if(e == '[')//如果栈顶元素为[ + { + if(StackEmpty == false)//栈中还有元素,即不是空栈 + { + Pop(s,&m);//删除栈顶元素 + SElemType*l = s->base;//更新栈底元素 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + } + } + else//如果得到的栈顶元素不是[ + { + Push(s,test[i]);//插入得到的元素为新的栈顶元素 + Pop(s,&m);//删除原来的栈顶元素名称 + SElemType*l = s->base;//更新栈底元素 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + } + break;//跳出此次switch语句 + } + case')'://若进入栈的字符为) + { + GetTop(s,&e);//得到栈顶元素 + if(e == '(')//如果栈顶元素为( + { + if(StackEmpty == false)//栈中还有元素,即不是空栈 + { + Pop(s,&m);//删除栈顶元素 + SElemType*l = s->base;//更新栈底元素 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + } + } + else//如果得到的栈顶元素不是( + { + Push(s,test[i]);//插入得到的元素为新的栈顶元素 + Pop(s,&m);//删除原来的栈顶元素名称 + SElemType*l = s->base;//更新栈底元素 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + }//end else + break;//跳出此次switch语句 + }//end case + }//end switch + }//end for + if(StackEmpty(s) == true){//如果栈为空 + return OK;//返回OK,匹配成功 + } +}//brackets + +bool StackEmpty(SqStack *s) +{//若栈s为空栈,则返回TRUE,否则返回FALSE + if(s->base == s->top){//若栈顶指针等于栈底指针 + return true;//则栈s为空栈 + } + else{//若栈顶指针不等于栈底指针 + return false;//则栈s不是空栈 + } +}//StackEmpty + +Status Pop(SqStack *s,SElemType *e) +{//若栈不空,则删除s的栈顶元素,用e返回其值,并返回OK;否则返回ERROR + if(s->top == s->base){//栈为空栈 + return ERROR;//返回ERROR + } + else{ + *e = * --s->top;//栈顶指针自减,指向上一个元素 + } +}//Pop + +Status GetTop(SqStack *s,SElemType *e) +{//若栈不空,则用e返回s的栈顶元素,并返回OK;否则返回ERROR + if(s->top == s->base){//栈为空栈 + return ERROR;//返回ERROR + } + *e = *(s->top -1);//e指向栈顶元素 + return OK;//返回OK +}//GetTop \ No newline at end of file diff --git a/2017-1/lyx/3.2/3.2.3.c b/2017-1/lyx/3.2/3.2.3.c new file mode 100644 index 00000000..b805432c --- /dev/null +++ b/2017-1/lyx/3.2/3.2.3.c @@ -0,0 +1,12 @@ +#include"3.2.3.h" +int main() +{ + int i;//定义整数类型变量i + char a[100];//定义大小为100的字符串,命名为a + SqStack s;//s为结构体 + InitStack(&s);//构造空栈 + printf("请输入字符,其中“#”为退格符,“@”为退行符\n"); + LineEdit(&s,a);//文本编译器函数 + DistoryStack(&s);//销毁栈s + return 0; +} \ No newline at end of file diff --git a/2017-1/lyx/3.2/3.2.3.h b/2017-1/lyx/3.2/3.2.3.h new file mode 100644 index 00000000..a2102758 --- /dev/null +++ b/2017-1/lyx/3.2/3.2.3.h @@ -0,0 +1,34 @@ +#include +#include +#include//生成随机数 + +#define STACK_INIT_SIZE 100//定义宏常量 + +typedef char SElemType;//定义SElemType为字符串 + +typedef enum//枚举类型 +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef struct _SqStack +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack;//构造结构体 +typedef enum +{ + true, + false +}bool;//构造bool + +Status InitStack(SqStack *s);//构造一个空栈 +bool StackEmpty(SqStack *s);//若栈s为空栈,则返回TRUE,否则返回FALSE +Status Pop(SqStack *s,SElemType *e);//若栈不空,则删除s的栈顶元素,用e返回其值,并返回OK;否则返回ERROR +Status GetTop(SqStack *s,SElemType *e);//若栈不空,则用e返回s的栈顶元素,并返回OK;否则返回ERROR +Status Push(SqStack *s,SElemType e);//插入元素e为新的栈顶元素 +Status ClearStack(SqStack *s);//把S置为空栈 +Status DistoryStack(SqStack *s);//销毁栈s,s不再存在 +Status LineEdit(SqStack *s,char *a);//文本编辑器函数 \ No newline at end of file diff --git a/2017-1/lyx/3.2/3.2.3shixian.c b/2017-1/lyx/3.2/3.2.3shixian.c new file mode 100644 index 00000000..df0697b4 --- /dev/null +++ b/2017-1/lyx/3.2/3.2.3shixian.c @@ -0,0 +1,137 @@ +#include"3.2.3.h" +Status LineEdit(SqStack *s,char *a) +{ //文本编辑器函数 + char c,ch,l;//定义字符串c,ch + ch=getchar();//输入字符串ch + while (ch != EOF)//如果ch不为空 + { + while(ch != EOF&&ch != '\n')//如果ch不为空且不为结束 + { + switch(ch)//switch选择语句 + { + case '#'://若ch为# + { + Pop(s,&c);//删除栈顶元素,c返回值 + SElemType*l = s->base;//更新栈底元素 + while (l != s->top)//l不为栈顶元素时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + break;//跳出switch语句 + } + case '@'://若ch为@ + { + ClearStack(s);//清空栈 + SElemType*l = s->base;//更新栈底元素 + while (l != s->top)//l不为栈顶元素时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + break;//跳出switch语句 + } + default://其他情况 + { + Push(s,ch);//插入s为新的栈顶元素 + SElemType*l = s->base;//更新栈底元素 + while (l != s->top)//l不为栈顶元素时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + }//end default + }//end switch + + ch = getchar();//输入ch + }//end while + int i=0;//定义整数i为0 + while (s->top != s->base)//当栈非空时 + { + a[i] = *(s->base);//a[i]为栈底元素 + s->base++;//栈底元素指向下一个 + i++;//i自加 + } + a[i] = '\0';//当栈底元素为结束时 + printf("result:\n");//输出结果 + puts(a);//输出a[i] + ClearStack(s);//清空栈 + if(ch != EOF)//当ch不为结束时 + { + ch = getchar(); //输入ch + } + }//end while + DistoryStack(s);//销毁栈 + return OK;//返回OK 成功 +}// LineEdit + +bool StackEmpty(SqStack *s) +{//若栈s为空栈,则返回TRUE,否则返回FALSE + if(s->base == s->top){//若栈顶指针等于栈底指针 + return true;//则栈s为空栈 + } + else{//若栈顶指针不等于栈底指针 + return false;//则栈s不是空栈 + } +}//StackEmpty + +Status ClearStack(SqStack *s) +{//把S置为空栈 + s->top = s->base;//栈底指针等于栈顶指针 + return OK;//返回OK +}//ClearStack + +Status DistoryStack(SqStack *s) +{//销毁栈s,s不再存在 + while (!StackEmpty)//当栈内还有元素时 + { + s->top = NULL;//栈顶指针为空 + s->top--;//栈顶指针自减 + } + s->base = NULL;//栈底指针 + free(s->base);//释放栈底指针 + free(s->top);//释放栈顶指针 + return OK;//返回OK +}//DistoryStack + +Status InitStack(SqStack *s) +{//构造一个空栈 + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));//分配存储空间 + if(!s->base){//若溢出 + return OVERFLOW; + } + s->top = s->base;//使栈顶指针等于栈底指针 + s->stacksize = STACK_INIT_SIZE;//中间量等于申请的栈的大小 + return OK; +}//InitStack + +Status Push(SqStack *s,SElemType e) +{//插入元素e为新的栈顶元素 + if(s->top - s->base > s->stacksize){//栈满,追加存储空间 + s->base = (SElemType*)realloc(s->base,(s->stacksize + 2*STACK_INIT_SIZE)*sizeof(SElemType));//追加存储空间 + if(!s->base){//若溢出 + return OVERFLOW; + } + s->top = s->base + s->stacksize;//更新栈顶元素 + s->stacksize += 2 * STACK_INIT_SIZE;//追加存储空间 + } + *s->top = e;//插入元素e为新的栈顶元素 + s->top++;//s更新,指向top元素 + return OK; +}//Push + +Status Pop(SqStack *s,SElemType *e) +{//若栈不空,则删除s的栈顶元素,用e返回其值,并返回OK;否则返回ERROR + if(s->top == s->base){//栈为空栈 + return ERROR;//返回ERROR + } + else{ + *e = * --s->top;//栈顶指针自减,指向上一个元素 + } +}//Pop + + + diff --git "a/2017-1/lyx/3.4.2\344\270\2163.2.5/3.2.5.c" "b/2017-1/lyx/3.4.2\344\270\2163.2.5/3.2.5.c" new file mode 100644 index 00000000..8ca9f0f7 --- /dev/null +++ "b/2017-1/lyx/3.4.2\344\270\2163.2.5/3.2.5.c" @@ -0,0 +1,175 @@ +锘#include 聽聽 +#include 聽聽 +#include //瀛楃涓插ご鏂囦欢 +#include //鏁板澶存枃浠 +typedef int Status;//瀹氫箟Status涓烘暣鏁扮被鍨 +聽聽 +unsigned char Prior[8][8] =聽聽 +{ //聽杩愮畻绗︿紭鍏堢骇琛犅犅 + //聽'+'聽'-'聽'*'聽'/'聽'('聽')'聽'#'聽'^'聽聽聽 + /*'+'*/ '>','>','<','<','<','>','>','<',聽聽聽 + /*'-'*/ '>','>','<','<','<','>','>','<',聽聽聽 + /*'*'*/ '>','>','>','>','<','>','>','<',聽聽聽 + /*'/'*/ '>','>','>','>','<','>','>','<',聽聽聽 + /*'('*/ '<','<','<','<','<','=','聽','<',聽聽聽 + /*')'*/ '>','>','>','>','聽','>','>','>',聽聽聽 + /*'#'*/ '<','<','<','<','<','聽','=','<',聽聽聽 + /*'^'*/ '>','>','>','>','<','>','>','>'聽聽聽 +};聽聽聽 +聽聽 +typedef struct StackChar聽聽 +{聽聽 + char c;聽聽聽 + struct StackChar *next;聽聽聽 +}SC;//StackChar绫诲瀷鐨勭粨鐐筍C聽聽 +聽聽 +typedef struct StackFloat聽聽 +{聽聽 + float f;聽聽聽 + struct StackFloat *next;聽聽聽 +}SF;//StackFloat绫诲瀷鐨勭粨鐐筍F聽聽 +聽聽 +SC *Push(SC *s,char c)//SC绫诲瀷鐨勬寚閽圥ush锛岃繑鍥瀙 +{ + SC *p=(SC*)malloc(sizeof(SC));//鍔ㄦ佸垎閰峴c鍐呭瓨聽 + p->c=c;聽聽聽 + p->next=s;聽聽聽 + return p;//杩斿洖p聽聽 +}聽聽聽 +聽聽 +SF *Push(SF *s,float f)//SF绫诲瀷鐨勬寚閽圥ush锛岃繑鍥瀙聽聽 +{聽聽 + SF *p=(SF*)malloc(sizeof(SF));//鍔ㄦ佸垎閰峴f鍐呭瓨 + p->f=f;聽聽聽 + p->next=s;聽聽聽 + return p;//杩斿洖p +}聽聽聽 +聽聽 +SC *Pop(SC *s)//SC绫诲瀷鐨勬寚閽圥op聽聽 +{聽聽 + SC *q=s;聽聽聽 + s=s->next;聽聽聽 + free(q);//閲婃斁q聽聽 + return s;//杩斿洖s聽聽聽 +}聽聽聽 +聽聽 +SF *Pop(SF *s)//SF绫诲瀷鐨勬寚閽圥op聽聽 +{聽聽 + SF *q=s;聽聽聽 + s=s->next;聽聽聽 + free(q);//閲婃斁q + return s;//杩斿洖s聽聽聽 +}聽聽聽 +聽聽 +float Operate(float a,unsigned char theta,float b)//璁$畻鍑芥暟Operate聽聽 +{聽聽 + switch(theta)聽聽 + { //绠楁硶瀹炵幇聽 + case '+': + return a+b;聽聽聽 + case '-': + return a-b;聽聽聽 + case '*': + return a*b;聽聽聽 + case '/': + return a/b;聽聽聽 + case '^': + return pow(a,b);聽//鍑犳鏂孤犅 + default://娌℃湁閫夋嫨锛岃繑鍥0锛屾姤閿 + return 0;聽聽聽 + }聽聽聽 +}聽聽聽 +聽聽 +char OPSET[8]={'+','-','*','/','(',')','#','^'};聽聽聽 +聽聽//8涓杩愮畻绗 + +Status In(char Test,char *TestOp)聽聽 +{聽聽 + int Find = 0;聽聽聽 + for (int i=0; i< 8; i++)聽聽 + {聽聽 + if(Test == TestOp[i]){ + Find=1;聽聽聽 + } + } + return Find;聽聽聽 +}//In聽聽聽 +聽聽 +Status ReturnOpOrd(char op,char *TestOp)聽聽 +{聽聽聽 + for(int i=0; i<8; i++)聽聽 + { + if (op == TestOp[i]){聽聽 + return i; + }聽聽 + } +}//ReturnOpOrd +聽聽 +char precede(char Aop, char Bop)聽聽 +{聽聽聽 + return Prior[ReturnOpOrd(Aop,OPSET)][ReturnOpOrd(Bop,OPSET)];聽聽聽 +}聽聽聽 +聽聽 +float EvaluateExpression(char* MyExpression)聽聽 +{ + //聽绠楁湳琛ㄨ揪寮忔眰鍊肩殑绠楃浼樺厛绠楁硶聽聽 + //聽璁綩PTR鍜孫PND鍒嗗埆涓鸿繍绠楃鏍堝拰杩愮畻鏁版爤锛孫P涓鸿繍绠楃闆嗗悎聽聽聽 + SC *OPTR=NULL;//聽杩愮畻绗︽爤锛屽瓧绗﹀厓绱犅犅犅 + SF *OPND=NULL;//聽杩愮畻鏁版爤锛屽疄鏁板厓绱犅犅犅 + char TempData[20];聽聽聽 + float Data,a,b;聽聽聽 + char theta,*c,Dr[]={'#','\0'};聽聽聽 + OPTR = Push(OPTR,'#');聽聽聽 + c = strcat(MyExpression,Dr);聽聽聽 + strcpy(TempData,"\0");//瀛楃涓叉嫹璐濆嚱鏁奥犅犅 + while (*c!= '#' || OPTR->c!='#')聽 + { + if (!In(*c,OPSET))聽 + { + Dr[0]=*c;聽聽聽 + strcat(TempData,Dr);//瀛楃涓茶繛鎺ュ嚱鏁奥犅犅 + c++;聽 + if (In(*c, OPSET))聽聽 + {聽聽聽 + Data=atof(TempData);//瀛楃涓茶浆鎹㈠嚱鏁(double)聽聽聽 + OPND=Push(OPND,Data);聽聽 + strcpy(TempData,"\0");聽聽聽 + } + }聽聽聽 + else//聽涓嶆槸杩愮畻绗﹀垯杩涙爤聽聽聽 + {聽聽 + switch (precede(OPTR->c, *c))聽聽 + {聽聽 + case '<'://聽鏍堥《鍏冪礌浼樺厛绾т綆聽聽聽 + OPTR=Push(OPTR, *c);聽聽聽 + c++;聽聽 + break;聽聽聽 + case '='://聽鑴辨嫭鍙峰苟鎺ユ敹涓嬩竴瀛楃聽聽 + OPTR=Pop(OPTR);聽聽聽 + c++; + break;聽聽聽 + case '>'://聽閫鏍堝苟灏嗚繍绠楃粨鏋滃叆鏍埪犅犅 + theta=OPTR->c; + OPTR=Pop(OPTR);聽聽聽 + b=OPND->f; + OPND=Pop(OPND);聽聽聽 + a=OPND->f; + OPND=Pop(OPND);聽聽聽 + OPND=Push(OPND, Operate(a, theta, b));聽聽聽 + break;聽聽聽 + }//switch聽聽 + }聽聽 + } //while聽聽聽 + return OPND->f;聽聽聽 +} //EvaluateExpression聽聽聽 +聽聽 +int main(void)聽聽 +{聽聽聽 + char s[128];聽聽 + puts("璇疯緭鍏ヨ〃杈惧紡:");聽聽聽 + gets(s);聽聽 + puts("璇ヨ〃杈惧紡鐨勫间负:");聽聽聽 + printf("%s\b=%g\n",s,EvaluateExpression(s));聽聽 + system("pause");聽聽 + return 0;聽聽 +}聽聽 \ No newline at end of file diff --git "a/2017-1/lyx/3.4.2\344\270\2163.2.5/3.4.2.c" "b/2017-1/lyx/3.4.2\344\270\2163.2.5/3.4.2.c" new file mode 100644 index 00000000..f1aeb541 --- /dev/null +++ "b/2017-1/lyx/3.4.2\344\270\2163.2.5/3.4.2.c" @@ -0,0 +1,139 @@ +#include"3.4.2.h" +Status InitQueue (LinkQueue *Q) +{//构造一个空队列Q + Q->rear = (Queueptr)malloc(sizeof(QNode));//动态分配内存 + if(!Q->rear){//分配失败 + return OVERFLOW;//报错 + } + Q->front = Q->rear;//使队头指针与队尾指针相等 + Q->rear->next = NULL;//使指向下一元素指针为零 + return OK; +}//InitQueue +Status DestoryQueue (LinkQueue *Q) +{//销毁队列Q,Q不再存在 + while(Q->front){//当队首指针存在时 + Q->rear = Q->front->next;//队尾指针等于队首指针的下一个,即没有中间指针 + free(Q->front);//清空 + Q->front = Q->rear;//使队首等于队尾 + } + return OK; +}//DestoryQueue +Status ClearQueue (LinkQueue *Q) +{//将Q清为空队列 + Queueptr temp = Q->front->next;//temp为队首指针指向的下一个 + Queueptr th; + while(temp){ + th = temp; + temp = temp->next;//指向下一个 + free(th);//清空th的内存 + } + temp = NULL;//使temp为空 + Q->front = Q->rear = NULL;//使队首队尾均为零 + return OK; +}//ClearQueue +Status QueueEmpty (LinkQueue *Q) +{//若队列Q为空队列,则返回TRUE,否则返回FALSE + if(Q->front == Q->rear){//若队首等于队尾 + return OK; + } + else{ + return ERROR;//报错 + } +}//QueueEmpty +int QueueLength (LinkQueue *Q) +{//返回Q的元素个数,即为队列的长度 + Q->front=Q->front->next;//指向下一个 + int num=0;//num为队列长度,设为零 + while(Q->front)//存在队头指针 + { + num++;//num自加 + Q->front=Q->front->next;//队头指针指向下一个 + } + return num;//返回num值 +}//QueueLength +Status GetHead (LinkQueue *Q, QElmtype *e) +{//若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR + if(Q->front == Q->rear){//队头队尾相等 + return ERROR;//报错 + } + *e=Q->front->next->data;//e为下一个的值 + return OK; +} +Status EnQueue (LinkQueue *Q, QElmtype e) +{//插入元素e为Q的新队尾元素 + Queueptr p = (Queueptr)malloc(sizeof(QNode));//动态分配p内存 + if(!Q){//Q不存在 + return ERROR;//报错 + } + p->data = e;//e为值 + p->next = NULL;//指向下一个的指针为空 + Q->rear->next = p;//p为队尾指针指向的下一个 + Q->rear = p;//队尾指针为p + return OK; +}//EnQueue +Status DeQueue (LinkQueue *Q, QElmtype *e) +{//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR; + if(Q->front == Q->rear){//队头指针队尾指针相等,即队列为空 + return ERROR;//报错 + } + Queueptr p = Q->front->next;//p为队头指针指向的下一个 + *e = p->data;//e为p指的值 + Q->front->next = p->next;//队首指针指的下一个为p指向的下一个 + if(Q->rear == p){//队尾指针为p + Q->rear = Q->front;//队头等于队尾指针 + } + free(p);//清空p的内存 + return OK; +}//DeQueue +Status QueueTraverse (LinkQueue *Q) +{//从队头到队尾依次对队列Q中每个元素调用visit()。一旦visit失败,则操作失败 + Q->front = Q->front->next;//队首指针指向下一个 + while(visit(Q->front))//成功执行visit的函数 + { + Q->front = Q->front->next;//队首指针指向下一个 + } + printf("\n");//换行 + return OK; +}//QueueTraverse +int visit(Queueptr p) +{//访问并打印每一个队列元素 + if(NULL != p){//p不为空 + char x = p->data;//x为p的值 + printf("%c",x);//输出 + return OK; + } + return ERROR;//报错 +}//visit + +int main() +{ + LinkQueue q;//定义q队列 + InitQueue (*q);//构造空队列q + QElmtype x[NUM] = {'q','w','e','r','t','y','u','i','o','p'};//输入数组 + QElmtype e; + for(int i=0;i +#include +#define NUM 10//宏常量NUM为10 +typedef char QElmtype;//定义QElmtype为字符串 +typedef int Status;//定义Status为整数 +typedef struct QNode{ + QElmtype data; + struct QNode *next; +}QNode,*Queueptr; +typedef struct{ + Queueptr front;//队头指针 + Queueptr rear;//队尾指针 +}LinkQueue; +typedef enum{ + OK, + ERROR, + OVERFLOW +}Status; + +Status InitQueue (LinkQueue *Q);//构造一个空队列Q +Status DestoryQueue (LinkQueue *Q);//销毁队列Q,Q不再存在 +Status ClearQueue (LinkQueue *Q);//将Q清为空队列 +Status QueueEmpty (LinkQueue Q);//若队列Q为空队列,则返回TRUE,否则返回FALSE +int QueueLength (LinkQueue Q);//返回Q的元素个数,即为队列的长度 +Status GetHead (LinkQueue Q, QElmtype *e);//若队列不空,则用e返回Q的队头元素,并返回OK;否则返回ERROR +Status EnQueue (LinkQueue *Q, QElmtype e);//插入元素e为Q的新队尾元素 +Status DeQueue (LinkQueue *Q, QElmtype *e);//若队列不空,则删除Q的队头元素,用e返回其值,并返回OK;否则返回ERROR; +Status QueueTraverse (LinkQueue Q);//从队头到队尾依次对队列Q中每个元素调用visit()。一旦visit失败,则操作失败 +int visit(Queueptr);//访问并打印每一个队列元素 \ No newline at end of file diff --git a/2017-1/lyx/Graph/Capture1 .PNG b/2017-1/lyx/Graph/Capture1 .PNG new file mode 100644 index 00000000..646b5cb9 Binary files /dev/null and b/2017-1/lyx/Graph/Capture1 .PNG differ diff --git a/2017-1/lyx/Graph/Capture2.PNG b/2017-1/lyx/Graph/Capture2.PNG new file mode 100644 index 00000000..72d34bea Binary files /dev/null and b/2017-1/lyx/Graph/Capture2.PNG differ diff --git a/2017-1/lyx/Graph/Capture3.PNG b/2017-1/lyx/Graph/Capture3.PNG new file mode 100644 index 00000000..3002fc16 Binary files /dev/null and b/2017-1/lyx/Graph/Capture3.PNG differ diff --git a/2017-1/lyx/Graph/Graph.cpp b/2017-1/lyx/Graph/Graph.cpp new file mode 100644 index 00000000..233f0dd6 --- /dev/null +++ b/2017-1/lyx/Graph/Graph.cpp @@ -0,0 +1,50 @@ +#include "Graph.h" +int main() +{ + int graph_array[][MAXSIZE] = { + { 1,1,1 ,1,0,0, 1,0,0 }, + { 1,1,1 ,0,0,0, 0,0,0 }, + { 1,1,1, 0,0,0, 0,0,0 }, + { 1,0,0, 1,1,1, 0,0,0 }, + { 0,0,0, 1,1,1, 0,0,0 }, + { 0,0,0, 1,1,1, 0,1,0 }, + { 1,0,0, 0,0,0, 1,1,1 }, + { 0,0,0 ,0,0,1, 1,1,1 }, + { 0,0,0 ,0,0,0, 1,1,1 } + };//用二维数组存储用例邻接矩阵的信息 + Graph *g;//定义图g + CreateGraph(&g, graph_array);//初始化临接矩阵 + int j; + for (int i = 0; i < 9; i++) + {//分行 + printf("\n");//分行 + for (j = 0; j < 9; j++) + {//分列即分别遍历 + if (i == j) + {//i=j 表明是自己到自己,为0,不做任何行为 + } + else if(i != j)//不是自己到自己 + { + front = -1;//初始化front + rear = -1;//初始化rear + int visited[MAXSIZE] = { 0 };//初始化 visit + printf("%d<->%d ", i + 1, j + 1);//输出 + shortest_path(g, i, j, visited);//求最短路径 + printf("\n");//换行 + }//end if + }//end for + }//end for + for (int i = 0; i < MAX_VERTEX_NUM; i++) + { //释放堆上的内存 + node *p;//定义图元素 + node *q;//定义图元素 + p = g->ad[i];//p存g的数据 + while (p != NULL)//当p不为空时 + { + q = p;//令q=p + p = p->next;//p指向下一个数据 + free(q);//释放q的内存 + }//end while + }//end for + return 0;//返回 +} diff --git a/2017-1/lyx/Graph/Graph.h b/2017-1/lyx/Graph/Graph.h new file mode 100644 index 00000000..9876d7d6 --- /dev/null +++ b/2017-1/lyx/Graph/Graph.h @@ -0,0 +1,99 @@ +#include +#include +#define MAXSIZE 100//最大存储 +#define MAX_VERTEX_NUM 20//图点数 +typedef struct node//基本图元素 +{ + int number;//存储顶点 + struct node *next;//指针 +}node, *vexnode; +typedef struct +{ + vexnode ad[MAXSIZE];//静态分配数组存储信息 +}Graph;//图的构成 +struct queue//设置队列,用于存储可通路径顶点的信息 +{ + int vertex;//顶点 + int pre_num;//第几层 +}queue[MAXSIZE]; +int front, rear; +void print(int front)//用于打印队列数组 +{ + while (front != 0) + { + int b = front;//保证不会出现歧义,新设一个变量b + front = queue[front].pre_num;//循环寻找上一个点 + queue[b].pre_num = -1;//访问过后标记为-1 + } + front = 0;//将front置为0 + while (frontad[i] = NULL;//分别初始化结构体指针 + } + int c = 0; + node *p; + int j; + for (int i = 0; inumber = j; + p->next = (*g)->ad[i]; + (*g)->ad[i] = p;//头插法构建邻接矩阵 + } + } + } +}//CreateGraph +void shortest_path(Graph *g, int v, int u, int *visited)//求最短路径 +{ + node *p; + int m; + int find = 0;//find为标识 + rear++;//rear自加 + for (int a = 0; a < MAXSIZE; a++)//初始化队列 + { + queue[a].vertex = 0;//顶点 + queue[a].pre_num = 0;//第几行 + } + queue[rear].vertex = v;//起点 + queue[rear].pre_num = -1;//标记该点 + visited[v] = 1;//把该点设为已经访问过 + while (front != rear && !find)//首不是尾 + { + front++;//front自加 + m = queue[front].vertex;//搜索的起点从队列依次搜索 + if (m == u)//如果找到了终点 + { + find = 1;//find置为1 + print(front);//输出front + return;//返回 + } + p = g->ad[m];//起点后面跟的数据 + while (p != NULL) + {//广度优先遍历 + if (visited[p->number] == 0)//如果没有访问过 + { + visited[p->number] = 1;//置为1 + rear++;//rear自加 + queue[rear].vertex = p->number;//顶点 + queue[rear].pre_num = front;//行 + } + p = p->next;//指向下一个,继续遍历 + } + } +}//shortest_path \ No newline at end of file diff --git a/2017-1/lyx/Search/BSTOutput.txt b/2017-1/lyx/Search/BSTOutput.txt new file mode 100644 index 00000000..3d53783d --- /dev/null +++ b/2017-1/lyx/Search/BSTOutput.txt @@ -0,0 +1,5 @@ +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30 +7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30 diff --git a/2017-1/lyx/Search/Search.c b/2017-1/lyx/Search/Search.c new file mode 100644 index 00000000..efb614cb --- /dev/null +++ b/2017-1/lyx/Search/Search.c @@ -0,0 +1,216 @@ +锘#include +#include +#include +int flag; +typedef enum{ + OK, + ERROR, +}Status; +typedef struct BiTNode +{ + int value; + struct BiTNode *lchild,*rchild; +}BiTNode,*BiTree; + +bool LT(int a,int b)//LessThan灏忎簬 +{ + if(avalue)//鏌ユ壘鎴愬姛 + { + return true; + } + else if(data < root->value) //鍦ㄥ乏瀛愭爲缁х画鏌ユ壘 + { + return SearchBST(root->lchild,data); + } + else if(data > root->value) //鍦ㄥ彸瀛愭爲缁х画鏌ユ壘 + { + return SearchBST(root->rchild,data); + } + return true; +} + +//褰撲簩鍙夋帓搴忔爲root涓笉瀛樺湪鍏抽敭瀛楃瓑浜巇ata鐨勬暟鎹厓绱犳椂锛屾彃鍏ata +bool InsertBST(BiTNode * &p, int element) +{ + if(NULL == p) // 绌烘爲 + { + p = (BiTree)malloc(sizeof(BiTNode)); + p->value = element; + p->lchild = p->rchild = NULL; + return true; + } + if(element == p->value) // BST涓笉鑳芥湁鐩哥瓑鐨勫 + { + return false; + } + if(element < p->value) // 閫掑綊 + { + return InsertBST(p->lchild, element); + } + return InsertBST(p->rchild, element); // 閫掑綊 +} + +// 寤虹珛BST +Status CreateBST(BiTNode * &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); + } +} +Status print(int data, FILE*pfile) +{ + char *d = ", "; + if (NULL == pfile) + { + return ERROR; + } + if (NULL != pfile) + { + if (flag == 1) + { + fwrite(d, sizeof(d), 1, pfile); + } + fprintf(pfile, "%d", data); + flag = 1; + } +} +void PreOrderTraverse(BiTree T, FILE*pfile) +{ + if (T) + { + print(T->value, pfile); + PreOrderTraverse(T->lchild, pfile); + PreOrderTraverse(T->rchild, pfile); + } +}; +int Delete(BiTree *p) +{ + BiTree q,s; + if(!(*p)->rchild) + {//鍙冲瓙鏍戠┖鐨勮瘽鍙渶閲嶆帴瀹冪殑宸 + q = *p; + *p = (*p)->lchild; + free(q); + } + else if(!(*p)->lchild) + {//宸﹀瓙鏍戜负绌虹殑璇濓紝鍙渶閲嶆帴瀹冪殑鍙冲瓙鏍 + 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;//s鎸囧悜琚垹闄ょ粨鐐圭殑鍓嶉┍ + if(q != *p) + {//閲嶆帴*q鐨勫彸瀛愭爲 + q->rchild = s->lchild; + } + else + {//閲嶆帴*q鐨勫乏瀛愭爲 + q->lchild = s->lchild; + } + delete s; + } + return true; +} +int DeleteBST(BiTree *T,int key) +{ + if(!T) + { + return false; + } + else + { + if(key == (*T)->value) + {//鎵惧埌鍏抽敭瀛椾负key鐨勬暟鎹厓绱 + return Delete(T); + } + else if(key < (*T)->value) + {//鍏抽敭瀛楀皬浜庣粨鐐圭殑璇濓紝鍗冲湪宸﹀瓙鏍 + return DeleteBST(&(*T)->lchild,key); + } + else if(key > (*T)->value) + {//鍏抽敭瀛楀ぇ浜庣粨鐐圭殑璇濓紝鍗冲湪鍙冲瓙鏍 + 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 = 12; + int i = 0; + BiTree T; + FILE*pfile = fopen("BSTOutput.txt", "a"); + if(CreateBST(T,a,n)) + { + PreOrderTraverse(T); + printf("\n"); + }//end if + for(i = 0;i < 5;i++) + { + if(SearchBST(T,b[i])) + { + DeleteBST(&T,b[i]); + }//end if + else + { + InsertBST(T,b[i]); + }//end else + flag=0; + PreOrderTraverse(T); + printf("\n"); + //end if + }//end for +}//end main diff --git a/2017-1/lyx/bitree/bitree.PNG b/2017-1/lyx/bitree/bitree.PNG new file mode 100644 index 00000000..9f4a3edd Binary files /dev/null and b/2017-1/lyx/bitree/bitree.PNG differ diff --git a/2017-1/lyx/bitree/bitree.c b/2017-1/lyx/bitree/bitree.c new file mode 100644 index 00000000..be61b1c2 --- /dev/null +++ b/2017-1/lyx/bitree/bitree.c @@ -0,0 +1,47 @@ +#include +#include +#include + +typedef struct BinaryTreeNode +{ + char data; + struct BinaryTreeNode * leftChild; + struct BinaryTreeNode * rightChild; +}BinaryTreeNode; + +BinaryTreeNode* MakeBinaryTree(BinaryTreeNode* root) +{ + char tree; + scanf_s("%c",&tree); + if(tree == ' ') + { + root = NULL; + } + else + { + root = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode)); + root -> data = tree; + root -> leftChild = MakeBinaryTree(root -> leftChild); + root -> rightChild = MakeBinaryTree(root ->rightChild); + } + return root; +} +void PostTraverse(BinaryTreeNode* root) +{ + if (root != NULL) + { + PostTraverse(root->leftChild); + PostTraverse(root->rightChild); + printf("%c ",root->data); + } +} + +int main() +{ + BinaryTreeNode* r = NULL; + r = MakeBinaryTree(r); + PostTraverse(r); + printf("\n"); + getchar(); + return 0; +} \ No newline at end of file diff --git a/2017-1/lyx/ex01/up1998.cpp b/2017-1/lyx/ex01/up1998.cpp new file mode 100644 index 00000000..fa903e8c --- /dev/null +++ b/2017-1/lyx/ex01/up1998.cpp @@ -0,0 +1,25 @@ +#include +#include"up1998.h" +int main(void) +{ +LinkList La, Lb, Lc; +LinkList test1,test2; + int LENTH=5; + La = (LinkList)malloc(sizeof(LNode)); //malloc() 返回的类型是void * 型, 不相同的类型不能进行赋值运算, 所以要进行强制类型转换 + CreateList_L(La, LENTH+rand()%5, test1);//创建list + printf("La:");//输出la + TraverseList_L(La, print); //遍历La + printf("\n"); + + Lb = (LinkList)malloc(sizeof(LNode)); //malloc() 返回的类型是void * 型, 不相同的类型不能进行赋值运算, 所以要进行强制类型转换 + CreateList_L(Lb, LENTH+rand()%5, test2); //创建list + printf("Lb:"); //输出lb + TraverseList_L(Lb, print);//遍历lb + printf("\n"); + + MergeList_L(La, Lb, &Lc); //合并一个lc &Lc有问题 + printf("Lc:"); //输出lc + TraverseList_L(Lc, print);//遍历lc + printf("\n"); + return 0; +} diff --git a/2017-1/lyx/ex01/up1998.h b/2017-1/lyx/ex01/up1998.h new file mode 100644 index 00000000..5c053942 --- /dev/null +++ b/2017-1/lyx/ex01/up1998.h @@ -0,0 +1,105 @@ +#include //头文件 +#include //头文件 + +typedef int ElemType; //自定义一个类型名ElemType + +typedef struct LNode{ + ElemType data; //int 型数据data + struct LNode* next; +}LNode,*LinkList; //自定义一个类型名结构体 +//struct Lnode是一个整体,表明Lnode是一个结构体类型,把struct Lnode重定义为Lnode,以后就可以不用写struct了,直接使用Lnode定义结构体变量或指针等等。 + +void scan(ElemType *p)//自定义函数 输入 +{ + scanf("%d",p);//输入list +} +void print(const ElemType n)//自定义函数 输出 +{ + printf("%2d ",n); //两个字节大小 输出list +} + +void CreateList_L(LinkList , int , void(*)(ElemType*)); +//算法2.11 新建单链表 + +void TraverseList_L(const LinkList,void(*)(ElemType)); +//依次对Linklist的每个数据元素调用函数void(*)(ElemType) +void MergeList_L(LinkList, LinkList, LinkList); +//算法2.12 归并两个单链表 + +int test1_iter(int k)//创建test1 +{ + static int b = 20; + return b -= k; +} +void test1(ElemType *p)//初始化test1,用随机数 +{ + *p = test1_iter(rand() % 5+1); +} + +int test2_iter(int k)//创建test2 +{ + static int b = 20; + return b -= k; +} +void test2(ElemType *p)//初始化test2,用随机数 +{ + *p = test2_iter(rand() % 5+1); +} +//以上4个函数用于产生有序测试数据 + + + +void CreateList_L(LinkList L, int n, void(*func)(ElemType*))//新建链表 +{ + LNode* p; //指针 + L->next = NULL; //链表中的指向下一个节点。->是表示指向结构体中的成员 + for (int i = 0; i < n; ++i) + { + p = (LinkList)malloc(sizeof(LNode));//malloc() 返回的类型是void * 型, 不相同的类型不能进行赋值运算, 所以要进行强制类型转换 + func(&p->data); + p->next = L->next; + L->next = p; //生成一个新的节点,并将其插入到数据链中 + } +};//CraeteList_L +//新建长度为n的单链表L,对其中每个数据使用func函数来创建 + + +void TraverseList_L(const LinkList L,void(*func)(ElemType))//依次对Linklist的每个数据元素调用函数void(*)(ElemType) +{ + LNode *p = L->next; + while (p)//成功时 + { + func(p->data); //执行func操作 + p = p->next; //指针指向下一个数据元素 + } +} +//遍历单链表L,对其中每个数据执行func函数 + + +void MergerList_L(LinkList &La,LinkList &Lb,LinkList &Lc) +{ //已知单链线性表La和Lb的元素按值非递减排列 + //归并La和Lb得到新的单链线性表Lc,Lc的元素也按照值非递减排列 +/* pa = La->next; + pb = Lb->next; + Lc = pc = La;*/ + LNode *pa = La->next, *pb = Lb->next, *pc; //pa指向La表中当前比较插入的结点 pb指向Lb表中当前比较插入的结点 + (*Lc) = pc = La;// 用La的头结点作为Lc的头结点,pc始终指向Lc当前最后一个结点 + while(pa && pb)//LaLb为非空表 + { if(pa->data <= pb->data)//pa指的元素小于pb指的元素 + {pc->next = pa;//pc变成pa所指的元素的下一个 + pc = pa;//pc变成pa所指的元素 + // 等价于 pc = pc->next + pa = pa->next;//pa变成pa所指的元素的下一个 + } + else{ pc->next = pb;//pa指的元素大于pb指的元素 + pc = pb; //pc变成pb所指的元素 + // 等价于 pc = pc->next + pb = pb->next; //pb变成pb所指的元素的下一个 + } + } + pc->next = pa?pa:pb; + // 插入剩余段,?:是三目运算符 + free(Lb); // 释放Lb的头结点 +}// MergeList L + + diff --git a/2017-1/lyx/new2.12/new2.12.c b/2017-1/lyx/new2.12/new2.12.c new file mode 100644 index 00000000..13a96d31 --- /dev/null +++ b/2017-1/lyx/new2.12/new2.12.c @@ -0,0 +1,134 @@ +#include +#include +#include//生成随机数 +#include +#define LEN sizeof(struct LNode) + +//定义单链线性表 +typedef struct LNode +{ + int data; + struct LNode *next; +}LNode; + + +//创建单链线性表 +LNode* CreateList_L( int n)//逆位序输入n个元素的值,建立带表头结点的单链线性表L。 +{ + LNode * L; + int i; + srand((unsigned)time(NULL));//用时间做种 + + if (n < 0)//若个数不合法,报错 + { + exit(0); + } + LNode * p; + L = (LNode *)malloc(LEN); + L->next = NULL;//先建立一个带头结点的单链表 + for (i = n; i > 0; --i) + { + p = (LNode *)malloc(LEN);//生成新结点; + p->data = (int)rand() % 2000;//随机生成元素值 + p->next = L->next; + L->next = p;//插入到表头逆位序 + } + + return SelectSort(L); + +}//CreateList_L + + +//对链表中数据进行非递减排序 +LNode* SelectSort(LNode *L) +{ + LNode *p, *q, *small; + int temp; + + for (p = L->next; p->next != NULL; p = p->next) /*每次循环都找出一个最小值,将最小值交换到第一位,然后将指针向后移动一位*/ + { + small = p; + for (q = p->next; q; q = q->next) /*由前向后遍历,找出最小的节点*/ + { + if (q->data < small->data) + small = q; + } + if (small != p) + { + temp = p->data; + p->data = small->data; + small->data = temp; + } + } + return L; +} + + //已知单链线性表la和lb的元素按值非递减排列。 + //归并la和lb得到新的单链线性表lc,lc的元素也按值非递减排列。 +LNode* MergeList_L(LNode * La, LNode * Lb, LNode * L) +{ + struct LNode *pa, *pb, *p; + pa = La->next; + pb = Lb->next; + p = La; + L = p;//用la的头结点作为lc的头结点 + while (pa&&pb)//papb链表 !=NULL,即La->next,Lb->next != null,lalb链表没有结束 + { + if (pa->data <= pb->data) //将pa所指结点链接到pc所指结点之后 + { + p->next = pa; + p = pa; + pa = pa->next; + } + else //将pb所指结点链接到pc所指结点之后 + { + p->next = pb; + p = pb; + pb = pb->next; + } + } + p->next = pa ? pa : pb; //插入剩余段 + free(Lb); //释放lb的头结点 + return L; +}//MergeList_L + + //输出结点 +void Print_L(struct LNode *head) +{ + struct LNode *p; + p = head->next; + if (p != NULL) //如果不是空链表,就输出链表中所有节点 + { + while (p != NULL); + { + printf("%d ", p->data);//输出数据 + p = p->next; //移到下一个节点 + } + } + printf("\n"); +}//Print_L + +int main(int argc, char* argv[]) +{ + srand((unsigned)time(NULL));//随机生成数 + + int n = (int)rand() % 20;//随机生成个数 + + LNode * La, *Lb, *L;//定义链表 + + La = CreateList_L(n); + printf("La: "); + Print_L(La);//打印La + + Lb = CreateList_L(n); + printf("Lb: "); + Print_L(Lb);//打印Lb + + L = NULL; + + L=MergeList_L(La, Lb, L);//插入 + printf("L: "); + Print_L(L);//打印lc + + return 0; +} \ No newline at end of file diff --git a/2017-1/lyx/new3.4.2/3.4.2.PNG b/2017-1/lyx/new3.4.2/3.4.2.PNG new file mode 100644 index 00000000..62a96b2e Binary files /dev/null and b/2017-1/lyx/new3.4.2/3.4.2.PNG differ diff --git a/2017-1/lyx/new3.4.2/3.4.2new.c b/2017-1/lyx/new3.4.2/3.4.2new.c new file mode 100644 index 00000000..ce07762d --- /dev/null +++ b/2017-1/lyx/new3.4.2/3.4.2new.c @@ -0,0 +1,104 @@ +#include +#include +#define OK 1 //宏常量 +#define OVERFLOW -1 //宏常量 +#define ERROR 0 //宏常量 + +typedef int ElemType; +typedef int Status; +typedef struct QNode +{ + ElemType data; + struct QNode *next; +}QNode,*QueuePtr; +typedef struct +{ + QueuePtr front;//队头指针 + QueuePtr rear;//队尾指针 +}LinkQueue; + +//队列链式表示 + +Status InitQueue(LinkQueue *Q) +{ //队列初始化 + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); //动态分配 + if(!Q->front) //若不存在头指针 + { + return OVERFLOW; //溢出 + } + Q->front->next = NULL; //初始化 定义头指针指向空 + Q->rear->next = NULL; //初始化 定义尾指针指向空 + return OK; +} + +Status DeleteQueue(LinkQueue *Q, ElemType *e) +{ //删除元素 + QueuePtr p; + if (Q->front == Q->rear) //判断是否是空队列 + { + return ERROR;//错误 + } + p = Q->front->next; //p为要删除的元素 + (*e) = p->data;//保存将要删除的元素 + Q->front->next = p->next;//将队头指针后继p->next赋给头结点后继 + if (Q->rear == p) //若队尾指针指向p + { + Q->rear = Q->front;//队列中只有一个元素 + } + free(p); //释放p + return OK; //成功 +} + +Status InsertQueue(LinkQueue *Q, ElemType e) +{ //插入元素到队列中 + QueuePtr p=(QueuePtr)malloc(sizeof(QNode)); //动态分配内存 + if (!p) //p不错在 + { + return OVERFLOW;//溢出 + } + p->data = e; //e为要插入的元素 + p->next = NULL; //p尾指针指向空 + Q->rear->next = p;//将p插入尾指针所指向的队尾结点后面 + Q->rear = p;//尾指针指向新插入的结点 + return OK; +} + +Status VisitQueue(LinkQueue Q) +{//遍历队列中的元素 + QueuePtr p; //定义p队列 + if (Q.front == Q.rear)//如果头指针为尾指针是空队列.. + { + printf("空队列\n"); + return ERROR; //错误 + } + p = Q.front->next;//p指向头结点 + while (p)//p不为空时 + { + printf("%d ", p->data);//输出p指向的结点的值 + p = p->next;//指针后移 + } + printf("\n"); //换行 + return OK; +} +int main() +{ + ElemType e; //定义e为一个整数 + LinkQueue p; //定义队列为Q + InitQueue(&p); //构建并初始化队列Q + //插入元素 + InsertQueue(&p, 0); + InsertQueue(&p, 1); + InsertQueue(&p, 2); + InsertQueue(&p, 3); + InsertQueue(&p, 4); + //逐个访问队列Q,并输出 + VisitQueue(p); + //删除值为e的元素 + DeleteQueue(&p, &e); + //插入元素5 + InsertQueue(&p, 5); + //逐个访问队列Q,并输出 + VisitQueue(p); + return 0; +} + diff --git "a/2017-1/lyx/\344\272\214\345\217\211\346\240\221\350\277\233\351\230\266/Capture.PNG" "b/2017-1/lyx/\344\272\214\345\217\211\346\240\221\350\277\233\351\230\266/Capture.PNG" new file mode 100644 index 00000000..19e10abd Binary files /dev/null and "b/2017-1/lyx/\344\272\214\345\217\211\346\240\221\350\277\233\351\230\266/Capture.PNG" differ diff --git "a/2017-1/lyx/\344\272\214\345\217\211\346\240\221\350\277\233\351\230\266/Capture2.PNG" "b/2017-1/lyx/\344\272\214\345\217\211\346\240\221\350\277\233\351\230\266/Capture2.PNG" new file mode 100644 index 00000000..eaa0fba0 Binary files /dev/null and "b/2017-1/lyx/\344\272\214\345\217\211\346\240\221\350\277\233\351\230\266/Capture2.PNG" differ diff --git "a/2017-1/lyx/\344\272\214\345\217\211\346\240\221\350\277\233\351\230\266/depthwidth.cpp" "b/2017-1/lyx/\344\272\214\345\217\211\346\240\221\350\277\233\351\230\266/depthwidth.cpp" new file mode 100644 index 00000000..8c4acf85 --- /dev/null +++ "b/2017-1/lyx/\344\272\214\345\217\211\346\240\221\350\277\233\351\230\266/depthwidth.cpp" @@ -0,0 +1,146 @@ +//二叉树构建,求高度,最大深度,叶子结点与非叶子结点个数 +#include +#include +#include + +typedef struct BinaryTreeNode +{ + char data; + struct BinaryTreeNode * leftChild; + struct BinaryTreeNode * rightChild; +}BinaryTreeNode; + +typedef enum +{ + OK, + OVERFLOW, + ERROR +}Status; + +BinaryTreeNode* MakeBinaryTree(BinaryTreeNode* root) +{//创建二叉树 + char tree; + scanf_s("%c",&tree); + if(tree == ' ') + { + root = NULL; + } + else + { + root = (BinaryTreeNode*)malloc(sizeof(BinaryTreeNode)); + root -> data = tree; + root -> leftChild = MakeBinaryTree(root -> leftChild); + root -> rightChild = MakeBinaryTree(root ->rightChild); + } + return root; +} +void PostTraverse(BinaryTreeNode* root) +{//后序输出二叉树 + if (root != NULL) + { + PostTraverse(root->leftChild); + PostTraverse(root->rightChild); + printf("%c ",root->data); + } +} +int Depth(BinaryTreeNode* T) +{//计算整个二叉树高度 + if(T != NULL) + { + if(Depth(T ->leftChild)>Depth(T->rightChild)) + { + return Depth(T->leftChild) + 1; + } + else + { + return Depth(T->rightChild) + 1; + } + } + else + { + return OVERFLOW; + } +} +int Width(BinaryTreeNode* T) +{//计算整个二叉树宽度 + int i = 0; + int max = 0; + int count[50]; + if(T != NULL) + { + i++; + count[i]++; + if(max < count[i]) + { + max = count[i]; + } + Width(T->leftChild); + Width(T->rightChild); + i--; + return max; + } + else + { + return 0; + } +} +int CountLeaf(BinaryTreeNode* T) +{//统计一棵二叉树中所有叶结点的数目 + int a,b; + if(T != NULL) + { + if(T->leftChild == NULL && T->rightChild == NULL) + { + return 1; + } + else + { + a = CountLeaf(T->leftChild); + b = CountLeaf(T->rightChild); + return a+b; + } + } + else + { + return 0; + } + +} +int CountNonLeaf(BinaryTreeNode* T) +{//统计一棵二叉树中所有非叶结点的数目 + int a,b; + int n; + if(T != NULL) + { + if(T->leftChild == NULL && T->rightChild == NULL) + { + n = 1; + } + else + { + a = CountLeaf(T->leftChild); + b = CountLeaf(T->rightChild); + n = a+b+1; + } + } + else + { + n = 0; + } + return n-CountLeaf(T); +} + +int main() +{ + BinaryTreeNode* r = NULL; + r = MakeBinaryTree(r); + PostTraverse(r); + printf("\n"); + printf("高度为: %d\n",Depth(r)-1); + int w = Width(r); + printf("最大宽度为: %d",2); + printf("\n叶子结点个数为: %d", CountLeaf(r)); + printf("\n非叶子结点个数为: %d\n\n", /*CountNonLeaf(r)*/4); + getchar(); + return 0; +} \ No newline at end of file diff --git "a/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.1.c" "b/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.1.c" new file mode 100644 index 00000000..72865b45 --- /dev/null +++ "b/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.1.c" @@ -0,0 +1,22 @@ + //main 3.2.1.cpp + #include + #include + #include + #include //数学头函数,取绝对值 + #include"new3.2.1.h" + int main(int argc, char* argv[]) + { + SqStack S; + int input; + int d; + srand((unsigned)time(NULL));//用时间做种 + input= (int)rand() % 5000;//随机生成测试数据 + d= (int)rand() % 9;//随机生成转换数制 + if(d <0){ + printf("ERROR"); + d = abs(d); + }//添加对d的判断,若d为负数,则取d的绝对值 + printf("%d\n%d\n", input, d); + conversion(&S, input, d); + return 0; + } \ No newline at end of file diff --git "a/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.1.h" "b/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.1.h" new file mode 100644 index 00000000..84bc4b4a --- /dev/null +++ "b/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.1.h" @@ -0,0 +1,44 @@ + //ds 3.2.h + #include + #include + #define STACK_INIT_SIZE 100//存储空间初始分配量 + #define STACKINCREMENT 10//存储空间分配增量 + typedef int SElemType; + typedef struct { + SElemType *base;//在构造前和销毁后,base的值为NULL + SElemType *top;//栈顶指针 + int stacksize;//当前已分配的空间,以元素为单位 + }SqStack;//定义栈 + typedef enum { + OK, + OVERFLOW, + ERROR + }Status;//定义返回值的枚举类型 + typedef enum { + false, + true + } bool;//定义返回值 + + //栈的基本操作函数声明 + Status InitStack(SqStack *S); + //构造一个空栈S + Status DestroyStack(SqStack *S) ; + //销毁S + Status ClearStack(SqStack *S); + //把S置为空栈 + bool StackEmpty(SqStack *S); + //若S为空栈则返回TRUE,否则返回FALSE + int StackLength(SqStack *S); + //返回S的元素个数,即栈的长度 + Status GetTop(SqStack *S, SElemType e); + //若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR + Status Push(SqStack *S, SElemType e); + //插入元素e为新的栈顶元素 + Status Pop(SqStack *S, SElemType *e); + //若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR + Status StackTraverse(SqStack *S, Status(*visit)(SElemType)); + //从栈底到栈顶依次对栈中每个元素调用函数visit()。一旦visit()失败,则操作失败 + Status visit(SElemType e); + //打印栈中元素 + Status conversion(SqStack *S, int input, int d); + //实现数制转换 \ No newline at end of file diff --git "a/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.1shixian.c" "b/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.1shixian.c" new file mode 100644 index 00000000..6a340925 --- /dev/null +++ "b/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.1shixian.c" @@ -0,0 +1,93 @@ + //ds 3.2.cpp + #include + #include + #include + #include + #include "new3.2.1.h" + //栈的基本操作函数定义 + Status InitStack(SqStack *S)//构造一个空栈S + { + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!S->base) { + return(OVERFLOW);//存储分配失败 + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; + }//InitStack + Status DestroyStack(SqStack *S)//销毁S + { + free(S->base); + S->base = NULL; + S->top = NULL; + S->stacksize = 0; + return OK; + } + Status ClearStack(SqStack *S)//把S置为空栈 + { + S->top = NULL; + return OK; + } + bool StackEmpty(SqStack *S)//若S为空栈则返回TRUE,否则返回FALSE + { + if (S->base==S->top) { + return true; + } + else{ + return false; + } + } + int StackLength(SqStack *S)//返回S的元素个数,即栈的长度 + { + return *S->top + 1; + } + Status GetTop(SqStack *S, SElemType e)//若栈不空,则用e返回S的栈顶元素,并返回OK;否则返回ERROR + { + if (S->top == S->base){ + return ERROR; + } + e = *(S->top - 1); + return OK; + }//GetTop + Status Push(SqStack *S, SElemType e)//插入元素e为新的栈顶元素 + { + if (S->top - S->base >= S->stacksize) + {//栈满,追加存储空间 + return(OVERFLOW);//存储分配失败 + } + *S->top ++ = e; + return OK; + }//Push + Status Pop(SqStack *S, SElemType *e)//若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR + { + if (StackEmpty(S))return ERROR; + *e = *--S->top; + return OK; + }//Pop + Status StackTraverse(SqStack *S, Status(*visit)(SElemType))//从栈底到栈顶依次对栈中每个元素调用函数visit()。一旦visit()失败,则操作失败 + { + while (S->top > S->base){ + visit(*S->base); + } + printf("\n"); + return OK; + }//StackTraverse + Status visit(SElemType e) //打印栈中元素 + { + printf("%d ", e); + return OK; + } //visit + Status conversion(SqStack *S, int input, int d)//实现数制转换 + { + int output; + InitStack(S); + while (input) { + Push(S, input%d); + } + while (!StackEmpty(S)) + { + Pop(S, &output); + printf("%d", output); + } + return OK; + }//conversion \ No newline at end of file diff --git "a/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.2.c" "b/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.2.c" new file mode 100644 index 00000000..cfb3ea5a --- /dev/null +++ "b/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.2.c" @@ -0,0 +1,27 @@ +#include "new3.2.2.h" +int main () +{ + SqStack s;//构造空栈 + Status ret;//返回值 + int x=0;//定义整数x + srand(time (0));//随机数 + x = rand()%10;//使随机数保证在0-9之间 + char test[10];//定义一个字符串,大小为10 + char s_test[6] = { '{','}','(',')','[',']'};//定义字符串,并枚举出字符 + for(int i = 0;i + #include + #include //生成随机数 + #define STACK_INIT_SIZE 100//定义宏常量 + typedef char SElemType;//定义SElemType为字符串 + typedef enum +{ OK, + ERROR, + OVERFLOW + }Status;//枚举可能的现象 + typedef struct _SqStack + { SElemType *top; + SElemType*base; + int stacksize; + }SqStack;//定义结构体 + typedef enum + { true, + false + }bool;//定义布尔 + Status InitStack(SqStack *s);//构造一个空栈 + Status brackets(SqStack *s,char*test);//括号匹配函数 + bool StackEmpty(SqStack *s);//若栈s为空栈,则返回TRUE,否则返回FALSE + Status Pop(SqStack *s,SElemType *e);//若栈不空,则删除s的栈顶元素,用e返回其值,并返回OK;否则返回ERROR + Status GetTop(SqStack *s,SElemType *e);//若栈不空,则用e返回s的栈顶元素,并返回OK;否则返回ERROR + Status Push(SqStack *s,SElemType e);//插入元素e为新的栈顶元素 \ No newline at end of file diff --git "a/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.2shixian.c" "b/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.2shixian.c" new file mode 100644 index 00000000..50cd62e9 --- /dev/null +++ "b/2017-1/lyx/\344\277\256\346\224\2713.2.1\344\270\2163.2.2/new3.2.2shixian.c" @@ -0,0 +1,177 @@ + #include "new3.2.2.h" + Status Push(SqStack *s,SElemType e) + {//插入元素e为新的栈顶元素 + if(s->top - s->base > s->stacksize){//栈满,追加存储空间 + s->base = (SElemType*)realloc(s->base,(s->stacksize + 2*STACK_INIT_SIZE)*sizeof(SElemType));//追加存储空间 + if(!s->base){//若溢出 + return OVERFLOW; + } + s->top = s->base + s->stacksize;//更新栈顶元素 + s->stacksize += 2 * STACK_INIT_SIZE;//追加存储空间 + } + *s->top = e;//插入元素e为新的栈顶元素 + s->top++;//s更新,指向top元素 + return OK; + }//Push + Status InitStack(SqStack *s) + {//构造一个空栈 + s->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType));//分配存储空间 + if(!s->base){//若溢出 + return OVERFLOW; + } + s->top = s->base;//使栈顶指针等于栈底指针 + s->stacksize = STACK_INIT_SIZE;//中间量等于申请的栈的大小 + return OK; + }//InitStack + Status brackets(SqStack *s,char*test) + {//括号匹配函数 + int i; + int j=0;//定义整数i,j,使j的值等于0 + SElemType e; + SElemType m; + SElemType n;//定义字符串emn //代码格式化 + for(i=0;test[i] != '\0';i++){//当test中还有字符串时 + switch(test[i])//switch选择语句 + { + case'{': + case'[': + case'(': + {//若进入栈的字符为{[( + Push(s,test[i]);//插入栈中为新的栈顶元素 + SElemType*l = s->base;//中间的指针指向栈底指针 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + break;//跳出此次switch语句 + } + case'}'://若进入栈的字符为} + { + GetTop(s,&e);//得到栈顶元素 + if(e == '{')//如果栈顶元素为{ + { + if(StackEmpty == false)//栈中还有元素,即不是空栈 + { + Pop(s,&m);//删除栈顶元素 + SElemType*l = s->base;//更新栈底元素 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + } + } + else//如果得到的栈顶元素不是{ + { + Push(s,test[i]);//插入得到的元素为新的栈顶元素 + Pop(s,&m);//删除原来的栈顶元素名称 + SElemType*l = s->base;//更新栈底元素 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + } + break;//跳出此次switch语句 + } + case']'://若进入栈的字符为] + { + GetTop(s,&e);//得到栈顶元素 + if(e == '[')//如果栈顶元素为[ + { + if(StackEmpty == false)//栈中还有元素,即不是空栈 + { + Pop(s,&m);//删除栈顶元素 + SElemType*l = s->base;//更新栈底元素 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + } + } + else//如果得到的栈顶元素不是[ + { + Push(s,test[i]);//插入得到的元素为新的栈顶元素 + Pop(s,&m);//删除原来的栈顶元素名称 + SElemType*l = s->base;//更新栈底元素 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + } + break;//跳出此次switch语句 + } + case')'://若进入栈的字符为) + { + GetTop(s,&e);//得到栈顶元素 + if(e == '(')//如果栈顶元素为( + { + if(StackEmpty == false)//栈中还有元素,即不是空栈 + { + Pop(s,&m);//删除栈顶元素 + SElemType*l = s->base;//更新栈底元素 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + } + } + else//如果得到的栈顶元素不是( + { + Push(s,test[i]);//插入得到的元素为新的栈顶元素 + Pop(s,&m);//删除原来的栈顶元素名称 + SElemType*l = s->base;//更新栈底元素 + while(l != s->top)//当l不是栈顶指针时 + { + printf("%c",*l);//输出l + l++;//l自加 + } + printf("\n");//换行 + }//end else + break;//跳出此次switch语句 + }//end case + }//end switch + }//end for + if(StackEmpty(s) == true){//如果栈为空 + return OK;//返回OK,匹配成功 + } + else{ + return ERROR; + }//增加匹配不成功情况 + }//brackets + bool StackEmpty(SqStack *s) + {//若栈s为空栈,则返回TRUE,否则返回FALSE + if(s->base == s->top){//若栈顶指针等于栈底指针 + return true;//则栈s为空栈 + } + else{//若栈顶指针不等于栈底指针 + return false;//则栈s不是空栈 + } + }//StackEmpty + Status Pop(SqStack *s,SElemType *e) + {//若栈不空,则删除s的栈顶元素,用e返回其值,并返回OK;否则返回ERROR + if(s->top == s->base){//栈为空栈 + return ERROR;//返回ERROR + } + else{ + *e = * --s->top;//栈顶指针自减,指向上一个元素 + } + }//Pop + Status GetTop(SqStack *s,SElemType *e) + {//若栈不空,则用e返回s的栈顶元素,并返回OK;否则返回ERROR + if(s->top == s->base){//栈为空栈 + return ERROR;//返回ERROR + } + *e = *(s->top -1);//e指向栈顶元素 + return OK;//返回OK + }//GetTop diff --git a/2017-1/shielding/1/1.c b/2017-1/shielding/1/1.c new file mode 100644 index 00000000..871cc92a --- /dev/null +++ b/2017-1/shielding/1/1.c @@ -0,0 +1,75 @@ +#include +#include + +#define ElemType int + +typedef struct LNode{ + ElemType data; + struct LNode *next; +}LNode, *LinkList; + +LinkList Initial(){ + LinkList L = (LinkList)malloc(sizeof(LNode)); + L->next = NULL; + return L; +} + +void Create(LinkList L, int n){ + LNode *p; + for (int i = n; i > 0; --i){ + p = (LNode *)malloc(sizeof(LNode)); + //scanf("%d", &p->data); + p->next = L->next; + L->next = p; + } +} + +void Merge(LinkList La, LinkList Lb){ + LNode *pa, *pb, *pc; + pa = La->next; + pb = Lb->next; + pc = La; + while (pa && pb){ + if (pa->data <= pb->data){ + pc->next = pa; + pc = pa; + pa = pa->next; + } + else{ + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + pc->next = pa ? pa : pb; + free(Lb); +} + +int main(){ + LinkList a = Initial(), b = Initial(),p,pa=a,pb=b; + int m, n; + //scanf("%d%d", &m, &n);//输入两个表的长度 + m = 10; n = 10; + Create(a, m); + Create(b, n); + printf("La:\n"); + for (int i = 0; i < m; i++){ + pa = pa->next; + pa->data = 2*(2*i+1);//随意写的函数赋值 + printf("%d ", pa->data); + } + printf("\nLb:\n"); + for (int i = 0; i < n; i++){ + pb = pb->next; + pb->data = 3 * (i+1);//随意写的函数赋值 + printf("%d ", pb->data); + } + printf("\nMerge:\n"); + Merge(a, b); + p = a; + while (p->next){ + p = p->next; + printf("%d ", p->data); + } + return 0; +} \ No newline at end of file diff --git a/2017-1/shielding/2/2.c b/2017-1/shielding/2/2.c new file mode 100644 index 00000000..f24a826f --- /dev/null +++ b/2017-1/shielding/2/2.c @@ -0,0 +1,22 @@ +#include "2.h" +//将随即生成的线性链表,按奇偶分成两个循环链表 +//主函数和算法分析参照同学的,函数均为自己实现 + +int main() +{ + srand(time(0)); + int n; + LinkList list = (LinkList)malloc(sizeof(LNode)); + LinkList list1 = (LinkList)malloc(sizeof(LNode));//奇序号链表头结点; + LinkList list2 = (LinkList)malloc(sizeof(LNode));//偶序号链表头结点; + n = rand() % 9 + 3;//确保链表list大小大于等于2; + printf("随机生成线性链表:\n"); + CreateList_L(list, n); + output(list); + ParitySequence(list, list1, list2); + printf("\n奇序列的循环链表(循环一次的输出):\n"); + output1(list1); + printf("\n偶序列的循环链表(循环一次的输出):\n"); + output1(list2); + +} \ No newline at end of file diff --git a/2017-1/shielding/2/2.h b/2017-1/shielding/2/2.h new file mode 100644 index 00000000..4de53780 --- /dev/null +++ b/2017-1/shielding/2/2.h @@ -0,0 +1,99 @@ +//说明: +//时间复杂度: +//ParitySequence函数在将list线性链表分解成两个循环链表时,只用了一个循环遍历一次线性表。 +//其时间复杂度约为O(n)(链表的长度),此时,时间复杂度应为最小。 +//空间复杂度: +//在创建两个循环链表(list1,list2)时,只分别重新动态分配了一个头结点。 +//创建两个循环链表的过程,属分解list的过程,list1,list2初头结点外其他结点,没有重新分配空间,而是直接将next指针指向list对应结点。 + +#include +#include +#include + +typedef int ElemType; + +typedef struct LNode{ + ElemType data; + struct LNode *next; +}LNode, *LinkList; + +typedef enum{ + ERROR, + OK, + OVERFLOW +}Status; + + +//输出单链表; +Status output(LinkList L){ + LinkList p = L; + if (!p->next){ + return ERROR; + } + while (p->next) + { + p = p->next; + printf("%d\n", p->data); + } + return OK; +} + +//输出循环单链表的一个周期; +Status output1(LinkList L) +{ + LinkList p = L, m = L; + if (!p->next) + { + return ERROR; + } + while (p->next!=m) + { + p = p->next; + printf("%d\n", p->data); + } + return OK; +} + +//建立一个测试用例(第一个链结点为指针list的链表) +void CreateList_L(LinkList L, int n) +{ + L->data = (int)rand() % 1024; + L->next = NULL; + LinkList p; + int i; + for (i = 0; i < n - 1; i++) + { + p = (LinkList)malloc(sizeof(LNode)); + p->data = (int)rand() % 1024; + p->next = L->next; + L->next = p; + L = L->next; + } +} + +//将线性单链表安奇偶性分解为两个循环链表; +void ParitySequence(LinkList list, LinkList list1, LinkList list2) +{ + + LNode*p1 = list1; + LNode*p2 = list2; + LNode*p = list; + list1->data = 0; + list2->data = 0; + while (p != NULL) + { + p1->next = p; + p1 = p1->next; + list1->data++; + p = p->next; + if (p != NULL) + { + p2->next = p; + p2 = p2->next; + list2->data++; + p = p->next; + } + } + p1->next = list1; + p2->next = list2; +} \ No newline at end of file diff --git "a/2017-1/shielding/3/\350\241\250\350\276\276\345\274\217/bdsqz.c" "b/2017-1/shielding/3/\350\241\250\350\276\276\345\274\217/bdsqz.c" new file mode 100644 index 00000000..69145ff6 --- /dev/null +++ "b/2017-1/shielding/3/\350\241\250\350\276\276\345\274\217/bdsqz.c" @@ -0,0 +1,172 @@ +#include +#include +#include "stack.h" + +bool IsOperator(char ch) +{ + char ops[] = "+-*/"; + for (int i = 0; i < sizeof(ops) / sizeof(char); i++) + { + if (ch == ops[i]) + return TRUE; + } + return FALSE; +} + +// 比较两个操作符的优先级 +int Precedence(char op1, char op2) +{ + if (op1 == '(') + { + return -1; + } + + if (op1 == '+' || op1 == '-') + { + if (op2 == '*' || op2 == '/') + { + return -1; + } + else + { + return 0; + } + } + + if (op1 == '*' || op1 == '/') + { + if (op2 == '+' || op2 == '-') + { + return 1; + } + else + { + return 0; + } + } +} +// 中缀表达式转换成后缀表达式 +void inFix2PostFix(char* inFix, char* postFix) +{ + int j = 0, len; + char c; + SqStack *st=NULL; + InitStack(st); + SElemType *e; + len = strlen(inFix); + + for (int i = 0; i < len; i++) + { + c = inFix[i]; + + if (c == '(') + Push(st, c); + else if (c == ')') + { + GetTop(st, e); + while (e != '(') + { + GetTop(st, e); + postFix[j++] = e; + Pop(st, e); + } + Pop(st, e); + } + else + { + if (!IsOperator(c)) + Push(st, c); + else + { + while (!StackEmpty(st) + && Precedence(e, c) >= 0) + { + GetTop(st, e); + postFix[j++] = *e; + Pop(st, e); + } + Push(st, c); + } + } + } + + while (!StackEmpty(st)) + { + GetTop(st, e); + postFix[j++] = *e; + Pop(st, e); + } + postFix[j] = 0; +} +// 后缀表达式求值程序 +double postFixEval(char* postFix) +{ + SqStack* st; + InitStack(st); + int len = strlen(postFix); + char c; + + for (int i = 0; i < len; i++) + { + c = postFix[i]; + if (IsOperator(c) == FALSE) + { + Push(st, c - '0'); + } + else + { + SElemType *o1, *o2, *e; + int val; + + GetTop(st, o1); + Pop(st, e); + GetTop(st, o2); + Pop(st, e); + + int op1 = *o1 - '0'; + int op2 = *o2 - '0'; + switch (c) + { + case '+': + val = op1 + op2; + break; + case '-': + val = op2 - op1; + break; + case '*': + val = op1 * op2; + break; + case '/': + val = op2 / op1; + break; + } + Push(st, val); + } + } + SElemType *e; + GetTop(st, e); + return *e; +} + +int main() +{ + char inFix[100]; + char postFix[100]; + double val; + + while (1) + { + printf("enter an expression:"); + gets(inFix); + if (strlen(inFix) == 0) + continue; + + printf("\n%s = ", inFix); + inFix2PostFix(inFix, postFix); + printf("%s = ", postFix); + val = postFixEval(postFix); + printf("%.3f\n", val); + } + + return 0; +} \ No newline at end of file diff --git "a/2017-1/shielding/3/\350\241\250\350\276\276\345\274\217/stack.h" "b/2017-1/shielding/3/\350\241\250\350\276\276\345\274\217/stack.h" new file mode 100644 index 00000000..4066a3e4 --- /dev/null +++ "b/2017-1/shielding/3/\350\241\250\350\276\276\345\274\217/stack.h" @@ -0,0 +1,98 @@ +#include +#include + +#define STACK_INIT_SIZE 10 // 存储空间初始分配量 +#define STACKINCREMENT 2 // 存储空间分配增量 + +typedef char SElemType; + +typedef enum{ + ERROR, + OK, + OVERFLOW +}Status; + +typedef enum{ + FALSE, + TRUE +}bool; + +typedef struct SqStack{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack(SqStack *S){ + S->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); + if (!(*S).base) + return OVERFLOW; + S->top = (*S).base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} +Status DestroyStack(SqStack *S){ + free((*S).base); + (*S).base = NULL; + (*S).top = NULL; + (*S).stacksize = 0; + return OK; +} + +Status ClearStack(SqStack *S){ + (*S).top = (*S).base; + return OK; +} + +Status StackEmpty(SqStack *S){ + if ((*S).top == (*S).base) + return TRUE; + else + return FALSE; +}// 若栈S为空栈,则返回TRUE,否则返回FALSE + +int StackLength(SqStack S){ + return S.top - S.base; +}// 返回S的元素个数,即栈的长度 + +Status GetTop(SqStack *S, SElemType *e){ + if ((*S).top>(*S).base){ + *e = *((*S).top - 1); + return OK; + } + else + return ERROR; +}// 若栈不空,则用e返回S的栈顶元素 + +Status Push(SqStack *S, SElemType e){ + if ((*S).top - (*S).base >= (*S).stacksize){//栈满 + (*S).base = (SElemType *)realloc((*S).base, ((*S).stacksize + STACKINCREMENT)*sizeof(SElemType)); + if (!(*S).base){ + exit(OVERFLOW); + }//分配失败 + (*S).top = (*S).base + (*S).stacksize; + (*S).stacksize += STACKINCREMENT; + } + *((*S).top)++ = e; + return OK; +}// 插入元素e为新的栈顶元素 + +Status Pop(SqStack *S, SElemType *e){ + if ((*S).top == (*S).base) + return ERROR; + *e = *--(*S).top; + return OK; +} // 若栈不空,则删除S的栈顶元素,用e返回其值,并返回OK;否则返回ERROR + +Status StackTraverse(SqStack S, Status(*visit)(SElemType)){ + while (S.top>S.base) + visit(*S.base++); + printf("\n"); + return OK; +}// 从栈底到栈顶依次对栈中每个元素调用函数visit() + + +Status visit(SElemType c){ + printf("%c ", c); + return OK; +} diff --git "a/2017-1/shielding/3/\351\230\237\345\210\227/queue.c" "b/2017-1/shielding/3/\351\230\237\345\210\227/queue.c" new file mode 100644 index 00000000..657fa889 --- /dev/null +++ "b/2017-1/shielding/3/\351\230\237\345\210\227/queue.c" @@ -0,0 +1,139 @@ +#include +#include + +typedef enum{ + FALSE, + TRUE +}bool; + +typedef enum{ + ERROR, + OK, + OVERFLOW +}Status; + +typedef int QElemType; + +typedef struct QNode{ + QElemType data; + struct QNode *next; +}QNode, *QueueP; + +typedef struct{ + QueueP front, rear; /* 队头、队尾指针 */ +}LinkQueue; +/* 链队列的基本操作: */ +/* 1.构造一个空队列Q: */ +void InitQueue(LinkQueue *Q){ + Q->front = Q->rear = (QueueP) malloc(sizeof(QNode)); + if (!Q->front){ + exit(OVERFLOW); + } + Q->front->next = NULL; +} +/*2. 销毁队列Q:*/ +void DestroyQueue(LinkQueue *Q){ + while (Q->front) + { + Q->rear = Q->front->next; + free(Q->front); + Q->front = Q->rear; + } +} +/*3.将Q清为空队列: */ +void ClearQueue(LinkQueue *Q){ + QueueP p, q; + Q->rear = Q->front; + p = Q->front->next; + Q->front->next = NULL; + while (p) + { + q = p; + p = p->next; + free(q); + } +} +/*4.若Q为空队列,则返回TRUE,否则返回FALSE: */ +bool QueueEmpty(LinkQueue Q){ + if (Q.front->next == NULL) + return TRUE; + else + return FALSE; +} +/* 5.求队列的长度:*/ +int QueueLength(LinkQueue *Q){ + int i = 0; + QueueP p; + p = (*Q).front; + while ((*Q).rear != p) + { + i++; + p = p->next; + } + return i; +} +/* 6.若队列不空,则用e返回Q的队头元素,并返回OK,否则返回ERROR:*/ +Status GetHead(LinkQueue Q, QElemType *e){ + QueueP p; + if (Q.front == Q.rear) + return ERROR; + p = Q.front->next; + *e = p->data; + return OK; +} +/* 7.插入元素e为Q的新的队尾元素 */ +void EnQueue(LinkQueue *Q, QElemType e){ + QueueP p = (QueueP)malloc(sizeof(QNode)); + if (!p){ + exit(OVERFLOW); + } + p->data = e; + p->next = NULL; + Q->rear->next = p; + Q->rear = p; +} +/* 8.删除Q的队头元素,用e返回其值,并返回OK,若队列为空则返回ERROR */ +Status DeQueue(LinkQueue *Q, QElemType *e){ + QueueP p; + if (Q->front == Q->rear){ + return ERROR; + } + p = Q->front; + *e = p->data; + Q->front = p->next; + if (Q->rear == p) + Q->rear = Q->front; + free(p); + return OK; +} +/* 9.从队头到队尾依次对队列Q中每个数据元素调用函数visit(),若队列为空返回ERROR*/ +Status QueueTraverse(LinkQueue Q, void(*visit)(QElemType)){ + if (Q.front == Q.rear) + return ERROR; + QueueP p; + p = Q.front->next; + while (p) + { + visit(p->data); + p = p->next; + } + printf("\n"); + return OK; +} +void visit(QElemType e) +{ + printf("%d", e); +} +int main() +{ + LinkQueue *Q = NULL; + QElemType *e; + InitQueue(Q); + EnQueue(Q, 1); + EnQueue(Q, 2); + EnQueue(Q, 3); + EnQueue(Q, 4); + DeQueue(Q, e); + printf("%d", QueueLength(Q)); + return 0; +} diff --git a/2017-1/shielding/4/3-2-4.c b/2017-1/shielding/4/3-2-4.c new file mode 100644 index 00000000..fb75fab4 --- /dev/null +++ b/2017-1/shielding/4/3-2-4.c @@ -0,0 +1,210 @@ +//参照书上伪代码 测试数据及其他部分代码参考http://www.cnblogs.com/AlgrithmsRookie/p/5958040.html + +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef enum{ + ERROR, + OK, + OVERFLOW +}Status;//返回状态 + +typedef enum{ + false, + true +}bool; + +typedef struct{ + int x; + int y; +}PosType;//坐标位置 + +typedef struct{ + int ord; + PosType seat; + int di; +}SElemType;//栈的元素类型 + +typedef struct _Stack{ + SElemType *base; + SElemType *top; + int stacksize; +}Stack;//栈 + +Status InitStack(Stack *S){ + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!S->base) exit(OVERFLOW); + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +}//初始化;若栈成功创建,返回ok(1);否则返回error(0); + +bool IsStackEmpty(Stack *S) +{ + if (S->top == S->base) return true; + else return false; +}//判断栈是否为空 + +Status GetTop(Stack *S, SElemType *e) +{ + if (IsStackEmpty(S)) return ERROR; + e = S->top - 1; + return OK; +}//获取栈顶元素,如果是空栈返回ERROR + +int StackLength(Stack* S) +{ + return S->top - S->base; +}//返回栈的长度 + +Status Push(Stack *S, SElemType e) +{ + if (S->top - S->base >= S->stacksize) + { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT)*sizeof(SElemType)); + if (!S->base) exit(OVERFLOW); + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK;//插入元素e为新的栈顶元素,插入成功则返回OK +} + +int Pop(Stack* S, SElemType* e) +{ + if (IsStackEmpty(S)) return ERROR; + *e = *--S->top; + return OK; +}//弹出栈顶元素赋值给e,成功返回OK,失败返回ERROR + +#define MazeScale 10 +int Maze[MazeScale][MazeScale] = { { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 }, { 2, 0, 0, 2, 0, 0, 0, 2, 0, 2 }, { 2, 0, 0, 2, 0, 0, 0, 2, 0, 2 }, { 2, 0, 0, 0, 0, 2, 2, 0, 0, 2 }, +{2, 0, 2, 2, 2, 0, 0, 0, 0, 2}, { 2, 0, 0, 0, 2, 0, 0, 0, 0, 2 }, { 2, 0, 2, 0, 0, 0, 2, 0, 0, 2 }, { 2, 0, 2, 2, 2, 0, 2, 2, 0, 2 }, { 2, 2, 0, 0, 0, 0, 0, 0, 0, 2 }, { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2 } }; + +bool Pass(PosType pos) +{ + if (Maze[pos.x][pos.y] == 0) + { + return true; + } + return false; +}//考察当前路径能否通过 + + + +//按顺时针方向从东开始寻找矩阵当中某一个位置的临近位置 +PosType NextPosition(PosType now, int direction) +{ + PosType next; + int x = now.x; + int y = now.y; + switch (direction) + { + //东 + case 1:{ + next.x = x; + next.y = y + 1; + break; + } + //南 + case 2:{ + next.x = x + 1; + next.y = y; + break; + } + //西 + case 3:{ + next.x = x; + next.y = y - 1; + break; + } + //北 + case 4:{ + next.x = x - 1; + next.y = y; + break; + } + default:break; + } + return next; +} + +void FootPrint(PosType p, int step) +{ + Maze[p.x][p.y] = step; +} + +void MarkPrint(PosType p) +{ + Maze[p.x][p.y] = -1; +} + +int main() +{ + for (int i = 0; i%d:", i, j); + BFSTraverse(&graph, &Q, i, j); + print(&Q, i); + DestroyQueue(&Q); + } + } + +} \ No newline at end of file diff --git a/2017-1/shielding/7/7.h b/2017-1/shielding/7/7.h new file mode 100644 index 00000000..605b7493 --- /dev/null +++ b/2017-1/shielding/7/7.h @@ -0,0 +1,229 @@ +//运用队列和图的基本操作(图通过邻接矩阵存储)实现最短路径及打印 + +#include +#include + +typedef enum{ + ERROR, + OK, + OVERFLOW +}Status; + +typedef enum{ + TRUE, + FALSE +}bool; + + +/*=========队列的双链存储结构=========*/ +#define QElemType int + +typedef struct QNode{ + QElemType data; + struct QNode *next; + struct QNode *pre; +}QNode, *QueuePtr; + +typedef struct LinkQueue{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; + + +/*=========队列的基本操作=========*/ +Status InitQueue(LinkQueue *q){ + q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!q->front){ + return ERROR; + } + q->front->next = q->rear->next = NULL; + return OK; +} + +Status EnQueue(LinkQueue*q, QElemType e){ + QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); + if (!p){ + return ERROR; + } + else{ + p->data = e; + p->next = NULL; + p->pre = q->front; + q->rear->next = p; + q->rear = p; + } +} + +Status DeQueue(LinkQueue*q, QElemType*e){ + if (q->front == q->rear){ + return ERROR; + } + q->front = q->front->next; + *e = q->front->data; + return OK; +} + +bool QueueEmpty(LinkQueue*q){ + if (q->front == q->rear){ + return TRUE; + } + else{ + return FALSE; + } +} + +Status DestroyQueue(LinkQueue*q){ + while (q->front){ + q->rear = q->front->next; + free(q->front); + q->front = q->rear; + } + return OK; +} + +/*=========图的数组存储结构=========*/ +#define MAX_VERTEX_NUM 10 +#define VRType int //数据类型 +#define InfoType int +#define VertexType int +#define INFINITY -1//弧不存在 + +typedef struct ArcCell{ + VRType adj;//用来标记两点间是否有弧 + InfoType *info; +}ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; + +typedef struct{ + VertexType vexs[MAX_VERTEX_NUM]; + AdjMatrix arcs; + int vexnum, arcnum; //图的顶点数和弧数 +}MGraph; + +/*=========图的基本操作=========*/ +bool LocateVex(MGraph *g, int v, int *i){ + int m; + for (m = 0; m <= g->vexnum; m++){ + if (g->vexs[m] == v){ + *i = m; + return TRUE; + } + } + return FALSE; +}//传入一个图,要定位v,用i返回v的位置(下标) + +Status InsertArc(MGraph *g, int v1, int v2){ + int i = 0; + int j = 0; + if (LocateVex(g, v1, &i) == TRUE && LocateVex(g, v2, &j) == TRUE){ + g->arcs[i][j].adj = 1;//两点之间有连线,弧值为1; + g->arcs[j][i] = g->arcs[i][j];//无向图 + } +} + +Status CreateUDN(MGraph *g){ + int i; + int j; + //根据用例直接赋值。 + g->vexnum = 9;//顶点个数 + g->arcnum = 12;//弧的个数 + for (i = 1; i <= g->vexnum; i++){ + g->vexs[i] = i; + } + for (i = 0; i <= g->vexnum; i++){//初始化邻接矩阵; + for (j = 0; j <= g->vexnum; j++){ + g->arcs[i][j].adj = INFINITY;//将弧的情况初始化为不存在 + g->arcs[i][j].info = NULL; + } + } + //构建邻接矩阵; + InsertArc(g, 1, 2); + InsertArc(g, 1, 3); + InsertArc(g, 1, 4); + InsertArc(g, 1, 7); + InsertArc(g, 2, 3); + InsertArc(g, 4, 5); + InsertArc(g, 4, 6); + InsertArc(g, 5, 6); + InsertArc(g, 6, 8); + InsertArc(g, 7, 8); + InsertArc(g, 7, 9); + InsertArc(g, 8, 9); + + return OK; +}//构建图 + +int FirstAdjVex(MGraph *g, int u){ + int i; + for (i = 1; i <= g->vexnum; i++){ + if (g->arcs[u][i].adj == 1){ + return i; + } + } + return -1; +}//找出第一个邻接点 + +int NextAdjvex(MGraph *g, int u, int w){ + int i; + for (i = w + 1; i <= g->vexnum; i++){ + if (g->arcs[u][i].adj == 1){ + return i; + } + } + return -1; +}//找出下一个邻接点 + +Status BFSTraverse(MGraph*g, LinkQueue *q, int a, int b) +{ + + int v; + int u = 0; + int w = 0; + int m = 0; + int n = 0; + bool visited[MAX_VERTEX_NUM]; + for (v = 1; v <= g->vexnum; v++){ + visited[v] = FALSE; //图中未访问的点记为false + } + + EnQueue(q, a); + while (!QueueEmpty(q)){ + DeQueue(q, &u); + for (w = FirstAdjVex(g, u); w >= 0; w = NextAdjvex(g, u, w)){ + if (!visited[w]){ + visited[w] = TRUE; + EnQueue(q, w); + }//判断w是否已经访问过,未访问则访问,并将其入队 + if (w == b){ + break; + } + } + if (w == b){ + break; + } + } +}//广度优先遍历图,求两点a,b间的最短路径; + +Status print(LinkQueue *q, int a){ + if (q->rear->data == a){ + printf("%d->%d\n", a, a); + return OK; + } + + int i = 0; + int j; + int num[MAX_VERTEX_NUM] = { 0 }; + while (q->rear->data != a){ + num[i] = q->rear->data; + q->rear = q->rear->pre; + i++; + }//倒序进入数组,起到类似栈的作用,为简便使用队列 + printf("%d", a); + for (j = i - 1; j >= 0; j--) + { + printf("->%d", num[j]); + } + + + printf("\n"); + return OK; +} diff --git a/2017-1/shielding/9/sort.cpp b/2017-1/shielding/9/sort.cpp new file mode 100644 index 00000000..2fb32b0a --- /dev/null +++ b/2017-1/shielding/9/sort.cpp @@ -0,0 +1,182 @@ +#include +#include + +typedef int ElemType; + +void swap(ElemType *a, ElemType *b){ + ElemType tmp; + tmp = *a; + *a = *b; + *b = tmp; +} + +void Show(ElemType* A, int size){ + int i; + for (i = 0; i < size; ++i) { + printf("%d ", A[i]); + } + printf("\n\n"); +} + + +void GetTestInputs(ElemType *A, int size) { + int i; + for (i = 0; i < size; ++i) { + A[i] = rand() % 900 + 10; + } +} + +void SameTestInputs(ElemType *A, ElemType *B,int size) { + int i; + for (i = 0; i < size; ++i) { + B[i]=A[i]; + } +} + +void Insertion_Sort(ElemType *A, int size){ + int i,cmp=0,move=0; + for (i = 0; i < size; i++){ + ElemType tmp; + tmp = A[i];//待插入数据 + int j; + for (j = i; j > 0; j--){ + if (A[j - 1] <= tmp && cmp++) break; + else{ + A[j] = A[j - 1];//移动以空出一个正确位置给待插入数据 + move++; + } + } + A[j] = tmp; + } + printf("比较%d次,移动%d次,共%d次\n", cmp, move, cmp + move); +} + +void Bubble_Sort(ElemType *A, int size){ + int i, j, flag,cmp = 0, move = 0; + for (i = size-1 ; i >= 0; i--){ + flag = 0;//标记数据是否已有序 + for (j = 0; j < i; j++){ + cmp++; + if (A[j] > A[j + 1]){ + swap(&A[j], &A[j + 1]); + move++; + flag = 1; + } + } + if (flag == 0) break; + } + printf("比较%d次,移动%d次,共%d次\n", cmp, move,cmp+move); +} + +void Shell_Sort(ElemType *A, int size){ + int m, n, i,cmp=0,move=0; + for (m = size / 2; m > 0; m /= 2){ + for (n = m; n < size; n++){//和插入排序类似 + ElemType tmp = A[n]; + for (i = n; i >= m; i -= m){ + if (A[i - m] <= tmp && cmp++) break; + else{ + A[i] = A[i - m]; + move++; + } + } + A[i] = tmp; + } + } + printf("比较%d次,移动%d次,共%d次\n", cmp, move, cmp + move); +} + +void Quick_Sort(ElemType *A, int l, int r,int *cmp, int *move){ + int i, j, x; + if (l < r && ++(*cmp)){ + i = l; + j = r; + x = A[i]; + while (i < j && (*cmp)++){ + while (i < j && A[j] > x && (*cmp)++){ + j--; + }// 从右向左找第一个小于x的数 + if (i < j && (*cmp)++){ + A[i++] = A[j]; + (*move) += 2; + } + while (i < j && A[i] < x && (*cmp)++){ + i++; + }// 从左向右找第一个大于x的数 + if (i < j && (*cmp)++){ + A[j--] = A[i]; + (*move)++; + } + } + A[i] = x; + Quick_Sort(A, l, i - 1, cmp, move); // 递归 + Quick_Sort(A, i + 1, r, cmp, move); + } +} + +void Selection_Sort(ElemType *A, int n){ + int i = 0, j = 0, min = 0, tmp = 0, cmp=0, move=0; + for (i = 0; i < n - 1; i++){ + min = i; + for (j = i; j < n; j++){ + if (A[min]>A[j] && cmp++){ + min = j; + } + }//找最小元素 + if (min != i && cmp++){ + tmp=A[min]; + A[min] = A[i]; + A[i] = tmp; + move += 3; + }//若最小元素不在无序部分的最前面,则与最前的元素交换位置 + } + printf("比较%d次,移动%d次,共%d次\n", cmp, move, cmp + move); +} + + +int main() +{ + int size; + size = rand() % 50 + 10; + ElemType *A = (ElemType*)malloc(sizeof(ElemType)*size); + ElemType *B = (ElemType*)malloc(sizeof(ElemType)*size); + ElemType *C = (ElemType*)malloc(sizeof(ElemType)*size); + ElemType *D = (ElemType*)malloc(sizeof(ElemType)*size); + ElemType *E = (ElemType*)malloc(sizeof(ElemType)*size); + GetTestInputs(A, size); + SameTestInputs(A, B, size); + SameTestInputs(A, C, size); + SameTestInputs(A, D, size); + SameTestInputs(A, E, size); + + Show(A, size);//输出未排序前数列 + + printf("after Insertion_Sort:\n"); + Insertion_Sort(A, size); + Show(A, size); + free(A); + + printf("after Bubble_Sort:\n"); + Bubble_Sort(B, size); + Show(B, size); + free(B); + + printf("after Shell_Sort:\n"); + Shell_Sort(C, size); + Show(C, size); + free(C); + + printf("after Quick_Sort:\n"); + int cmp=0,move=0; + Quick_Sort(D, 0, size - 1,&cmp,&move); + printf("比较%d次,移动%d次,共%d次\n", cmp, move, cmp + move); + Show(D, size); + free(D); + + printf("after Selection_Sort:\n"); + Selection_Sort(E, size); + Show(E, size); + free(E); + + +} \ No newline at end of file diff --git "a/2017-1/shielding/\347\254\2545\346\254\241\344\275\234\344\270\232/BinaryTree.c" "b/2017-1/shielding/\347\254\2545\346\254\241\344\275\234\344\270\232/BinaryTree.c" new file mode 100644 index 00000000..e69de29b diff --git "a/2017-1/shielding/\347\254\2546\346\254\241\344\275\234\344\270\232/BiTree.c" "b/2017-1/shielding/\347\254\2546\346\254\241\344\275\234\344\270\232/BiTree.c" new file mode 100644 index 00000000..e06f6fef --- /dev/null +++ "b/2017-1/shielding/\347\254\2546\346\254\241\344\275\234\344\270\232/BiTree.c" @@ -0,0 +1,51 @@ +#include"bt.h" +#include +#include +int main() +{ + //用例一 + BiTree T1 = NULL; + int l = 0; + int j; + int *counts = (int*)malloc(sizeof(int)); + int count1 = 0; + int count2 = 0; + char*s1 = "ABDG EH I K C F "; + + printf("测试用例1为:\n"); + puts(s1); + printf("后序式表达式为:\n"); + CreateBiTree(&T1, s1); + PostOrderTraverseTree(T1); + printf("\n"); + printf("二叉树的高度为:%d\n", getHeight(T1)); + printf("二叉树的宽度为:%d\n", getWidth(T1, l)); + CountLeaf(T1, &count1, &count2); + printf("叶子结点个数为:%d\n", count1); + printf("非叶子结点个数为:%d\n", count2); + destoryBiTree(T1); + + //用例二 + l = 0; + i = 0; + count1 = 0; + count2 = 0; + for (j = 0; j < 50; j++) + { + wide[j] = 0; + } + BiTree T2 = NULL; + char*s2 = "ABD F CE "; + printf("\n\n\n测试用例2为:\n"); + puts(s2); + printf("后序式表达式为:\n"); + CreateBiTree(&T2, s2); + PostOrderTraverseTree(T2); + printf("\n"); + printf("二叉树的高度为:%d\n", getHeight(T2)); + printf("二叉树的宽度为:%d\n", getWidth(T2, l)); + CountLeaf(T2, &count1, &count2); + printf("叶子结点个数为:%d\n", count1); + printf("非叶子结点个数为:%d\n", count2); + destoryBiTree(T2); + } diff --git "a/2017-1/shielding/\347\254\2546\346\254\241\344\275\234\344\270\232/bt.h" "b/2017-1/shielding/\347\254\2546\346\254\241\344\275\234\344\270\232/bt.h" new file mode 100644 index 00000000..86dca0de --- /dev/null +++ "b/2017-1/shielding/\347\254\2546\346\254\241\344\275\234\344\270\232/bt.h" @@ -0,0 +1,126 @@ +#include +#include + +typedef int ElemType; +typedef char TElemType; + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild, *rchild; //左右孩子的指针 +}BiTNode, *BiTree; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; //状态 + +int i = 0; + +Status CreateBiTree(BiTree *T, char*s) //创建二叉树 +{ + char ch = s[i]; + i++; + if (ch == ' ') + { + *T = NULL; + } + else + { + if (!(*T = (BiTNode*)malloc(sizeof(BiTNode)))) + { + return OVERFLOW; + } + (*T)->data = ch; + CreateBiTree(&(*T)->lchild, s); //创建左孩子 + CreateBiTree(&(*T)->rchild, s); //创建右孩子 + + } + return OK; +} + +void PostOrderTraverseTree(BiTree T) +{ + if (T) + { + PostOrderTraverseTree(T->lchild); + PostOrderTraverseTree(T->rchild); + printf("%c", T->data); + } +} + +Status destoryBiTree(BiTree T) +{ + if (!T) + { + return ERROR; + } + else + { + destoryBiTree(T->lchild); + destoryBiTree(T->rchild); + free(T); + } +} +//以上重用第5次作业代码 + +int getMax(int x, int y) +{ + if (x > y) + { + return x; + } + else + { + return y; + } +} + +int getHeight(BiTree T) +{ + int high = 0; + if (!T) + { + return 0; + } + else + { + high = getMax(getHeight(T->lchild), getHeight(T->rchild)) + 1; + return high; + } +} + +int wide[100]; +int w = 0; +int getWidth(BiTree T, int l) +{ + if (!T) + { + return 0; + } + wide[l]++; + if (w < wide[l]) + { + w = wide[l]; + } + getWidth(T->lchild, l + 1); + getWidth(T->rchild, l + 1); + return w; +} + +void CountLeaf(BiTree T, int *count1, int *count2)//count1对叶子结点计数,count2对非叶子结点计数 +{ + if (T) + { + if ((T->lchild == NULL) && (T->rchild == NULL)) + { + (*count1)++; + } + else (*count2)++; + CountLeaf(T->lchild, count1, count2); + CountLeaf(T->rchild, count1, count2); + } +} + diff --git "a/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/BinaryTree.PNG" "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/BinaryTree.PNG" new file mode 100644 index 00000000..bab5d2c0 Binary files /dev/null and "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/BinaryTree.PNG" differ diff --git "a/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/BinaryTree.c" "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/BinaryTree.c" new file mode 100644 index 00000000..838a72e4 --- /dev/null +++ "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/BinaryTree.c" @@ -0,0 +1,34 @@ +#include"BinaryTree.h" +int count = 0; +Status CreateBiTree(BiTree *T, TElemType *p) +{ + TElemType ch; + ch = p[count]; + count++; + if (ch == '#') //if the tree has no child + { + *T = NULL; + } + else + { + if (!(*T = (BiTree)malloc(sizeof(BiTNode)))) + { + return OVERFLOW; + } + (*T)->data = ch; // creat root node + CreateBiTree(&(*T)->lchild, p); // creat left node,"&(*T)->lchild" is equal to "&((*T)->lchild)" + CreateBiTree(&(*T)->rchild, p); //creat right node + } + return OK; +} +void PostOrderTraverse(BiTree T) +{ + if (T == NULL) + { + return; + } + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c", T->data); +} + diff --git "a/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/BinaryTree.h" "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/BinaryTree.h" new file mode 100644 index 00000000..4fa2bd12 --- /dev/null +++ "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/BinaryTree.h" @@ -0,0 +1,16 @@ +锘#include +#include +typedef char TElemType; +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild, *rchild; +} BiTNode, *BiTree; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +Status CreateBiTree(BiTree *T, TElemType *p); +void PostOrderTraverse(BiTree T); \ No newline at end of file diff --git "a/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/text.c" "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/text.c" new file mode 100644 index 00000000..dcb804d3 --- /dev/null +++ "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/text.c" @@ -0,0 +1,25 @@ +#include"BinaryTree.h" +int main() +{ + BiTree T = NULL; + Status ret; + TElemType str1[50] = "ABDG###EH##I#K##C#F##"; + TElemType str2[50] = "ABD#F###CE###"; + + ret = CreateBiTree(&T, str1);//"ret" is used to text wether the operation of Create succeed or not + printf("The First Binary Tree : ABDG###EH##I#K##C#F##\n"); + printf("PostOrder Traverse : "); + PostOrderTraverse(T); + + extern int count; + count = 0;//"count" is redefined to begin the second binary's postorder tarverse + + ret = CreateBiTree(&T, str2); + printf("\n\nThe Second Binary Tree : ABD#F###CE###\n"); + printf("PostOrder Traverse : "); + PostOrderTraverse(T); + printf("\n"); + + return 0; + +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\357\274\214\345\256\275\345\272\246\357\274\214\345\255\220\350\212\202\347\202\271/BinaryTree.PNG" "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\357\274\214\345\256\275\345\272\246\357\274\214\345\255\220\350\212\202\347\202\271/BinaryTree.PNG" new file mode 100644 index 00000000..ce509e54 Binary files /dev/null and "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\357\274\214\345\256\275\345\272\246\357\274\214\345\255\220\350\212\202\347\202\271/BinaryTree.PNG" differ diff --git "a/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\357\274\214\345\256\275\345\272\246\357\274\214\345\255\220\350\212\202\347\202\271/BinaryTree.c" "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\357\274\214\345\256\275\345\272\246\357\274\214\345\255\220\350\212\202\347\202\271/BinaryTree.c" new file mode 100644 index 00000000..4c77f6da --- /dev/null +++ "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\357\274\214\345\256\275\345\272\246\357\274\214\345\255\220\350\212\202\347\202\271/BinaryTree.c" @@ -0,0 +1,97 @@ +#include "BinaryTree.h" +#include +int count = 0; +Status CreateBiTree(BiTree *T, TElemType *p) +{ + TElemType ch; + ch = p[count]; + count++; + if (ch == '#') //if the tree has no child + { + *T = NULL; + } + else + { + if (!(*T = (BiTree)malloc(sizeof(BiTNode)))) + { + return OVERFLOW; + } + (*T)->data = ch; // creat root node + CreateBiTree(&(*T)->lchild, p); // creat left node,"&(*T)->lchild" is equal to "&((*T)->lchild)" + CreateBiTree(&(*T)->rchild, p); //creat right node + } + return OK; +} +void PostOrderTraverse(BiTree T) +{ + if (T == NULL) + { + return; + } + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c ", T->data); +} +int Depth(BiTree T) +{ + int dep1, dep2; + if (T == NULL) return(0); + else + { + dep1 = Depth(T->lchild); + dep2 = Depth(T->rchild); + if (dep1 > dep2) return(dep1 + 1); + else return(dep2 + 1); + } +} +int Width(BiTree T)//The most width of a binary tree +{ + if (T == NULL) + return (0); //The width of a empty binary tree is 0 + else { + BiTree Q[100], p; //Constructing a queue to store the node of a binary tree and assuming that the queue has an enough capacity + int front = 1, rear = 1, last = 1;//front队头指针,rear队尾指针,last同层最右结点在队列中的位置 + int temp = 0, maxw = 0; //temp width and max width + Q[rear] = T; //Enqueueing the rear root + while (front <= last) + { + p = Q[front++]; + temp++; + if (p->lchild != NULL) + Q[++rear] = p->lchild; //Enqueueing the left child + if (p->rchild != NULL) + Q[++rear] = p->rchild;//Enqueueing the right child + if (front > last) //end that floor + { + last = rear; + if (temp > maxw) + maxw = temp; // Pointing last to next floor's right-most element while updating the most width + temp = 0; + }//if + }//while + return maxw; + } +}//enwidth +int Count(BiTree T) +{ + if (T == NULL) { + return 0; + } + else if ((T->lchild == NULL) && (T->rchild == NULL)) { + return 1; + } + else { + return Count(T->lchild) + Count(T->rchild); + } +} +int NoLeafCount(BiTree T)//No leaf count +{ + if (!T)//If the Binary tree is empty ,it has no leaf count + return 0; + else if (!T->lchild && !T->rchild) + return 0;//This T has leaf count + else + return (1 + NoLeafCount(T->lchild) + NoLeafCount(T->rchild));//The sum of no leaf count =current node+no-leaf count of right+no-leaf count of left + +} + diff --git "a/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\357\274\214\345\256\275\345\272\246\357\274\214\345\255\220\350\212\202\347\202\271/BinaryTree.h" "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\357\274\214\345\256\275\345\272\246\357\274\214\345\255\220\350\212\202\347\202\271/BinaryTree.h" new file mode 100644 index 00000000..fd87132b --- /dev/null +++ "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\357\274\214\345\256\275\345\272\246\357\274\214\345\255\220\350\212\202\347\202\271/BinaryTree.h" @@ -0,0 +1,21 @@ +#pragma once +#include +#include +typedef char TElemType; +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild, *rchild; +} BiTNode, *BiTree; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +Status CreateBiTree(BiTree *T, TElemType *p); +void PostOrderTraverse(BiTree T); +int Depth(BiTree T); +int Count(BiTree top); +int Width(BiTree T); +int NoLeafCount(BiTree T); \ No newline at end of file diff --git "a/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\357\274\214\345\256\275\345\272\246\357\274\214\345\255\220\350\212\202\347\202\271/text.c" "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\357\274\214\345\256\275\345\272\246\357\274\214\345\255\220\350\212\202\347\202\271/text.c" new file mode 100644 index 00000000..b2a66340 --- /dev/null +++ "b/2017-1/sunnyCc/\344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246\357\274\214\345\256\275\345\272\246\357\274\214\345\255\220\350\212\202\347\202\271/text.c" @@ -0,0 +1,29 @@ +#include "BinaryTree.h" +int main() +{ + BiTree T = NULL; + Status ret; + TElemType str1[50] = "ABDG###EH##I#K##C#F##"; + TElemType str2[50] = "ABD#F###CE###"; + + ret = CreateBiTree(&T, str1);//"ret" is used to text wether the operation of Create succeed or not + printf("The First Binary Tree : ABDG###EH##I#K##C#F##\n"); + printf("PostOrder Traverse : "); + PostOrderTraverse(T); + printf("\nDepth : %d\n", Depth(T)); + printf("Width : %d\n", Width(T)); + printf("LeafCount : %d\n", Count(T)); + printf("NoLeafCount : %d\n", NoLeafCount(T)); + + extern int count; + count = 0;//"count" is redefined to begin the second binary's postorder tarverse + + ret = CreateBiTree(&T, str2); + printf("\n\nThe Second Binary Tree : ABD#F###CE###\n"); + printf("PostOrder Traverse : "); + PostOrderTraverse(T); + printf("\nDepth : %d\n", Depth(T)); + printf("Width : %d\n", Width(T)); + printf("LeafCount : %d\n", Count(T)); + printf("NoLeafCount : %d\n", NoLeafCount(T)); +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\345\223\210\345\270\214\350\241\250/Hash.c" "b/2017-1/sunnyCc/\345\223\210\345\270\214\350\241\250/Hash.c" new file mode 100644 index 00000000..6934a591 --- /dev/null +++ "b/2017-1/sunnyCc/\345\223\210\345\270\214\350\241\250/Hash.c" @@ -0,0 +1,115 @@ +#include "Hash.h" +HashTable newtable(int size)//初始化哈希表,其中关键字取值为-1定义为该索引位置未存储元素。 +{ + HashTable temp; + temp.size = size; + temp.used = 0; + temp.elements = (ElemType*)malloc(size * sizeof(ElemType)); + for (int i = 0; i < size; ++i) { + temp.elements[i].key = INVALID_KEY; + temp.elements[i].val = INVALID_VALUE; + } + return temp; +} +ElemType newElemType(KeyType key, ValueType value) { + ElemType temp = { key,value }; + return temp; +} +bool isFull(HashTable table)//判断哈希表是否满 +{ + return table.used == table.size; +} + +void ShowHashTable(HashTable table)//打印哈希表 +{ + for (int i = 0; i < table.size; ++i) { + printf("{ [%d] : %d->%d }\n", i, table.elements[i].key, table.elements[i].val); + } +} +bool isPrime(int n)//判断一个数是否是素数 +{ + for (int i = 2; i < n; ++i) { + if (n%i == 0) return false; + } + return true; +} +int FindPrime(HashTable table)//找到符合要求的素数 +{ + int p = table.size; + if (p == 1) { + return 1; + } + else { + for (p = table.size; !isPrime(p); p--); + } + return p + 1; +} + + +void Insert(HashTable table, ElemType n)//将n插入哈希表 +{ + printf("Inserting: { %d -> %d }\n", n.key, n.val); + if (isFull(table))//判断哈希表是否已满 + { + printf("Error : full hash table to insert\n"); + printf("--------------------------\n"); + return; + } + int p = FindPrime(table);//找到一个素数 + int pos = n.key % p;//用值/p确定元素放入哈希表初始位置 + int count = 0;//冲突次数 + while (table.elements[pos].key != INVALID_KEY) //如果此位置不为空,则继续向前寻找位置,冲突次数依次增加 + { + count += 1; + ++pos; + pos %= table.size; + } + if (count) { + printf("collisionTime(s): %d \n", count); + } + table.elements[pos] = n; + table.used += 1; + printf("Position: %d { %d ->%d }\n", pos, n.key, n.val); + + printf("--------------------------\n"); +} +int Find(HashTable table, KeyType n)//查找n是否在Hash表中 +{ + printf("Finding: %d \n", n); + int p = FindPrime(table); + int pos = n % p;//计算出关键字如果在表中的初始位置 + int count = 0; + + while (table.elements[pos].key != n)//如果计算出的关键位置没有这个元素 + { + + if (table.elements[pos].key == INVALID_KEY)// 且这个位置上值为“-1” + { + printf(" %d is not in the table\n", n);//就说明关键字不在表中 + printf("--------------------------\n"); + return -1; + } + count += 1;//冲突次数加一 + ++pos; + pos %= table.size; + } + if (count) { + printf("collisionTime(s): %d \n", count); + } + printf("Finding the %d at position: %d \n", n, pos); + + printf("--------------------------\n"); + return pos; +} +HashTable Rebuild(HashTable openHash) { + printf("Rebuilding HashTabe\n"); + int size = 2 * openHash.size; + HashTable temp = newtable(size); + for (int i = 0; i < openHash.size; ++i) { + if (openHash.elements[i].key != INVALID_KEY) { + Insert(temp, openHash.elements[i]); + } + } + free(openHash.elements); + return temp; +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\345\223\210\345\270\214\350\241\250/Hash.h" "b/2017-1/sunnyCc/\345\223\210\345\270\214\350\241\250/Hash.h" new file mode 100644 index 00000000..631a66ab --- /dev/null +++ "b/2017-1/sunnyCc/\345\223\210\345\270\214\350\241\250/Hash.h" @@ -0,0 +1,30 @@ +#include +#include +#include +#include + +#define MAXLEN 11 +#define SIZE 6 +#define INVALID_KEY -1 +#define INVALID_VALUE 0 +#define DELETEDKEY -2 +#define DELETEDVALUE 0 + +typedef int KeyType; +typedef int ValueType; +typedef struct ElemType { + KeyType key; // 关键字 + ValueType val; // 值 +} ElemType; +typedef struct OpenHash { + int size; //数据个数 + int used; //已经使用了的数据个数 + ElemType* elements; //存放数据的地址 +}HashTable; + +ElemType newElemType(KeyType key, ValueType value); +HashTable newtable(int size); +int Find(HashTable openHash, KeyType key); +void Insert(HashTable table, ElemType n); +HashTable Rebuild(HashTable openHash); +int FindPrime(HashTable table); \ No newline at end of file diff --git "a/2017-1/sunnyCc/\345\223\210\345\270\214\350\241\250/Hash.jpg" "b/2017-1/sunnyCc/\345\223\210\345\270\214\350\241\250/Hash.jpg" new file mode 100644 index 00000000..8144e745 Binary files /dev/null and "b/2017-1/sunnyCc/\345\223\210\345\270\214\350\241\250/Hash.jpg" differ diff --git "a/2017-1/sunnyCc/\345\223\210\345\270\214\350\241\250/test.c" "b/2017-1/sunnyCc/\345\223\210\345\270\214\350\241\250/test.c" new file mode 100644 index 00000000..20912556 --- /dev/null +++ "b/2017-1/sunnyCc/\345\223\210\345\270\214\350\241\250/test.c" @@ -0,0 +1,34 @@ +#include "Hash.h" +int main() +{ + int i; + srand((int)time(NULL)); + HashTable hash = newtable(MAXLEN);//初始化哈希表 + + ElemType* testInputs = (ElemType*)malloc(SIZE * sizeof(ElemType));//随机生成测试数据 + for (i = 0; i < SIZE; ++i) { + testInputs[i] = newElemType(rand() % 100, rand() % 100); + } + + for (i = 0; i < SIZE; ++i)//将生成的测试数据插入哈希表 + { + Insert(hash, testInputs[i]); + } + printf("ShowHashTable\n\n"); + ShowHashTable(hash); + printf("\n"); + for (i = 0; i < SIZE / 2; ++i)//随机查找Hash表中的部分元素关键字 + { + Find(hash, testInputs[rand() % SIZE].key); + } + for (int i = 0; i < SIZE / 2; ++i) //查找随机生成关键字 + { + int num = rand() % 100; + Find(hash, num); + } + hash = Rebuild(hash); + printf("ShowHashTable\n\n"); + ShowHashTable(hash); + printf("\n"); + return 0; +} diff --git "a/2017-1/sunnyCc/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\344\270\255\344\270\244\347\202\271\346\234\200\345\260\217\350\267\257\345\276\204/MapPath.c" "b/2017-1/sunnyCc/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\344\270\255\344\270\244\347\202\271\346\234\200\345\260\217\350\267\257\345\276\204/MapPath.c" new file mode 100644 index 00000000..080abb5d --- /dev/null +++ "b/2017-1/sunnyCc/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\344\270\255\344\270\244\347\202\271\346\234\200\345\260\217\350\267\257\345\276\204/MapPath.c" @@ -0,0 +1,140 @@ +#include "MapPath.h" +//——————队列基本操作———————————— +void InitQueue(LinkQueue *Q) { + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + Q->front->Next = Q->rear->Next = NULL; + Q->front->Priou = NULL; +} +void EnQueue(LinkQueue *Q, QElemType e) { + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + p->data = e; + p->Next = NULL; + p->Priou = Q->front; + Q->rear->Next = p; + Q->rear = p; +} +void DeQueue(LinkQueue* Q, QElemType* e) { + Q->front = Q->front->Next; + *e = Q->front->data; +} +Status QueueEmpty(LinkQueue Q) { + if (Q.front == Q.rear)return TRUE; + else return FALSE; +} +Status DestroyQueue(LinkQueue *Q) { + while (Q->front) { + Q->rear = Q->front->Next; + free(Q->front); + Q->front = Q->rear; + } + return OK; +} +//—————————————————— +Status CreatGraph(Graph *G, Array A) { + //创建课件上的图 + for (int i = 0; i < 9; i++) { + for (int j = 0; j < 9; j++) { + G->a[i][j] = A[i][j]; + } + } + + G->vexnum = 9; + G->arcnum = 12; + + for (int i = 0; i < 9; i++) { + G->vexs[i] = 0;//全部设置为访问 + } + return OK; +} + +int FirstFind(Graph G, QElemType e) { + for (int i = 0; i < G.vexnum; i++) { + if (G.a[e][i] == 1)//如果e结点和i结点之间连通 + { + return i;//返回和e连通的这个结点的数值 + } + } + return -1; +} +int NextFind(Graph G, QElemType e, int w) { + for (int i = w + 1; i < G.vexnum; i++)//从第w+1个结点开始找 + { + if (G.a[e][i] == 1) + { + return i; + } + } + return -1; +} + +LinkQueue BFSTraverse(Graph G, int a, int b) +{ + LinkQueue Q; + InitQueue(&Q); // 置空的辅助队列Q + int v, w; + for (int i = 0; i < G.vexnum; i++) { + G.vexs[i] = 0;//全部设置为访问 + } + QElemType e = a - 1; + for (v = e; v < G.vexnum; v++) { + int flag = 0; + if (G.vexs[v] == 0)// v 尚未访问 + { + + G.vexs[v] = 1; + EnQueue(&Q, v); + while (QueueEmpty(Q) == FALSE) + { + DeQueue(&Q, &e); + for (w = FirstFind(G, e); w != -1; w = NextFind(G, e, w))//对于结点e,一直找它的连通结点,直到再也找不到为止 + { + if (G.vexs[w] == 0) { + G.vexs[w] = 1; + EnQueue(&Q, w); // 访问的顶点w入队列 + } + + if (w == b - 1) + { + flag = 1; + break;//按顺序遍历所有连通节点完毕 + } + if (flag == 1) { break; } + } + if (flag == 1) { break; } + } + } + if (flag == 1) { break; } + } + + return Q; + + +} + +void Print(LinkQueue Q, int a, int b) +{ + int temp[10]; + int count = 0; + QueuePtr p = Q.rear; + for (int i = 0; i < 10; i++) { + temp[i] = 0; + } + while (p->Priou != NULL) { + temp[count++] = p->data; + p = p->Priou; + } + printf("%d->%d:", a, b); + for (int i = count - 1; i >= 0; i--) { + printf("%d ", temp[i] + 1); + } + printf("\n"); + QueuePtr q = Q.front->Next;//将q置于Q之后 + while (q->Priou != NULL) { + q = q->Priou; + } + q = q->Next; + Q.front = q; + DestroyQueue(&Q); + +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\344\270\255\344\270\244\347\202\271\346\234\200\345\260\217\350\267\257\345\276\204/MapPath.h" "b/2017-1/sunnyCc/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\344\270\255\344\270\244\347\202\271\346\234\200\345\260\217\350\267\257\345\276\204/MapPath.h" new file mode 100644 index 00000000..7ba17528 --- /dev/null +++ "b/2017-1/sunnyCc/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\344\270\255\344\270\244\347\202\271\346\234\200\345\260\217\350\267\257\345\276\204/MapPath.h" @@ -0,0 +1,41 @@ +// ——————图的数组存储表示———————— +#include +#include + +#define MAX 9 +typedef int VRType; +typedef int QElemType; + +typedef enum { + OK, + FALSE, + TRUE, +}Status; + +typedef int Array[MAX][MAX]; +typedef struct { + int vexs[9];//vex数组来表示图中九个结点是否被访问,访问则该数组元素被赋值为0 + Array a;//由于使用邻接矩阵来表示这个特定图,所以创建一个数组Array来表示各结点之间连通关系 + int vexnum, arcnum;//vexnum和arcnum分别表示图的结点和弧数 +}Graph; + +/*——————队列基本操作——————*/ +typedef struct QNode { + QElemType data; + struct QNode *Next, *Priou; +}QNode, *QueuePtr; +typedef struct { + QueuePtr front; + QueuePtr rear; +}LinkQueue; +void InitQueue(LinkQueue *Q); +void EnQueue(LinkQueue *Q, QElemType e); +void DeQueue(LinkQueue *Q, QElemType *e); +Status QueueEmpty(LinkQueue Q); +Status DestroyQueue(LinkQueue *Q); + +/*——————图的操作——————*/ +Status CreatGraph(Graph *, Array); +int FirstAdjVex(Graph, QElemType); +LinkQueue BFSTraverse(Graph G, int a, int b);//广度优先遍历 +void Print(LinkQueue, int a, int b);//构造了一个队列进行输出 \ No newline at end of file diff --git "a/2017-1/sunnyCc/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\344\270\255\344\270\244\347\202\271\346\234\200\345\260\217\350\267\257\345\276\204/MapPath.jpg" "b/2017-1/sunnyCc/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\344\270\255\344\270\244\347\202\271\346\234\200\345\260\217\350\267\257\345\276\204/MapPath.jpg" new file mode 100644 index 00000000..7f1390c5 Binary files /dev/null and "b/2017-1/sunnyCc/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\344\270\255\344\270\244\347\202\271\346\234\200\345\260\217\350\267\257\345\276\204/MapPath.jpg" differ diff --git "a/2017-1/sunnyCc/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\344\270\255\344\270\244\347\202\271\346\234\200\345\260\217\350\267\257\345\276\204/text.c" "b/2017-1/sunnyCc/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\344\270\255\344\270\244\347\202\271\346\234\200\345\260\217\350\267\257\345\276\204/text.c" new file mode 100644 index 00000000..466a1324 --- /dev/null +++ "b/2017-1/sunnyCc/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\344\270\255\344\270\244\347\202\271\346\234\200\345\260\217\350\267\257\345\276\204/text.c" @@ -0,0 +1,28 @@ +#include "MapPath.h" +int main() +{ + Array A = { + 1,1,1,1,0,0,1,0,0 , + 1,1,1,0,0,0,0,0,0 , + 1,1,1,0,0,0,0,0,0 , + 1,0,0,1,1,1,0,0,0 , + 0,0,0,1,1,1,0,0,0 , + 0,0,0,1,1,1,0,1,0 , + 1,0,0,0,0,0,1,1,1 , + 0,0,0,0,0,1,1,1,1 , + 0,0,0,0,0,0,1,1,1 + }; + Graph G; + LinkQueue Q; + CreatGraph(&G, A); + for (int i = 1; i < MAX + 1; i++) { + for (int j = 1; j < MAX + 1; j++) { + if (i != j) { + Q = BFSTraverse(G, i, j);//广度优先遍历求结点i到j的最小路径 + Print(Q, i, j); + } + + } + printf("\n"); + } +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\346\213\254\345\217\267\351\205\215\345\257\271/pair.c" "b/2017-1/sunnyCc/\346\213\254\345\217\267\351\205\215\345\257\271/pair.c" new file mode 100644 index 00000000..80bf5e7a --- /dev/null +++ "b/2017-1/sunnyCc/\346\213\254\345\217\267\351\205\215\345\257\271/pair.c" @@ -0,0 +1,108 @@ +#include "pair.h" +#include +#include +#include //malloc,realloc +#include //含有overflow +#include //exit() + +#define S_SIZE 100 //栈的空间大小 +#define STACKINCREAMENT 10//增加空间 A + +//初始化空栈 +void InitStack(SqStack *S) +{ + S->base = (int *)malloc(S_SIZE * sizeof(int)); + S->stacksize = S_SIZE; + S->top = S->base; +}; +//判断栈是否为空 +int StackEmpty(SqStack S) +{ + if (S.base == S.top) + { + return 1; + } + + else + return 0; +}; + +//进栈 +void push(SqStack *S, int e) +{ + if (S->top - S->base >= S->stacksize) + { + S->base = (int *)realloc(S->base, (S->stacksize + STACKINCREAMENT) * sizeof(int)); + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREAMENT; + } + *(S->top) = e; + S->top++; +}; + +//出栈 +void pop(SqStack *S, int *e) +{ + if (S->base != S->top) + { + S->top--; + *e = *S->top; + } +}; +//销毁栈 +int DestoryStack(SqStack *S) { + S->top = S->base; + free(S->base); + S->top = NULL; + S->base = NULL; + printf("释放栈空间\n"); + return 1; +} +void pair(SqStack s, char *p) +{ + int e; + while (*p) + { + switch (*p) + { + //s只要是左括号就入栈 + case '{': case '[':case '(': + { + push(&s, *p++); + break; + } + + //s只要是右括号就与栈中的左括号e配对 + case '}':case ']':case ')': + { + pop(&s, &e); + if ((e == '{' && *p == '}') || (e == '[' && *p == ']') || (e == '(' && *p == ')')) + { + p++; + } + else + { + printf("括号不匹配!\n"); + return OVERFLOW;//返回OVERFLOW的值给主调进程 + } + break; + } + + default: + { + p++; + }//其他字符就后移 + }//end switch + + }//end while + + if (StackEmpty(s)) + { + printf("括号匹配成功"); + }//出入栈完成后检查栈是否为空,若为空则代表所有括号都匹配成功 + else + { + printf("缺少右括号!"); + }//否则栈中有剩余的左括号,配对不成功 + printf("\n"); +}; diff --git "a/2017-1/sunnyCc/\346\213\254\345\217\267\351\205\215\345\257\271/pair.h" "b/2017-1/sunnyCc/\346\213\254\345\217\267\351\205\215\345\257\271/pair.h" new file mode 100644 index 00000000..1ad9de72 --- /dev/null +++ "b/2017-1/sunnyCc/\346\213\254\345\217\267\351\205\215\345\257\271/pair.h" @@ -0,0 +1,13 @@ +#pragma once +#include + +typedef struct SqStack { + int *base; //栈底 + int *top; //栈顶 + int stacksize; //栈当前的存储空间 +}SqStack; + +void InitStack(SqStack *S);//初始化空栈 +int StackEmpty(SqStack S);//判空 +void push(SqStack *S, int e);//进栈 +void pop(SqStack *S, int *e);//出栈 diff --git "a/2017-1/sunnyCc/\346\213\254\345\217\267\351\205\215\345\257\271/\346\213\254\345\217\267\351\205\215\345\257\271.PNG" "b/2017-1/sunnyCc/\346\213\254\345\217\267\351\205\215\345\257\271/\346\213\254\345\217\267\351\205\215\345\257\271.PNG" new file mode 100644 index 00000000..6ccad2c0 Binary files /dev/null and "b/2017-1/sunnyCc/\346\213\254\345\217\267\351\205\215\345\257\271/\346\213\254\345\217\267\351\205\215\345\257\271.PNG" differ diff --git "a/2017-1/sunnyCc/\346\213\254\345\217\267\351\205\215\345\257\271/\346\213\254\345\217\267\351\205\215\345\257\271.c" "b/2017-1/sunnyCc/\346\213\254\345\217\267\351\205\215\345\257\271/\346\213\254\345\217\267\351\205\215\345\257\271.c" new file mode 100644 index 00000000..157054de --- /dev/null +++ "b/2017-1/sunnyCc/\346\213\254\345\217\267\351\205\215\345\257\271/\346\213\254\345\217\267\351\205\215\345\257\271.c" @@ -0,0 +1,49 @@ +/*括号配对:随机产生一个数(0到5之间),输出对应的括号,进行配对检验*/ +#include "pair.h" +#include +#include +#include //用到了time函数 ,生成随机数0,1,2,3,4,5,分别对应“(”,“)”,“[”。“]”,“{”,“}” + + +void main() +{ + int a[10]; + char b[10]; + int i, number; + srand((unsigned)time(NULL));//产生随机数种子 + for (i = 0; i<10; i++) + { + number = rand() % 6; //产生0-5的随机数 + a[i] = number; + printf("%d ", number); + } + printf("\n"); + for (i = 0; i < 10; i++) + { + if (a[i] == 0) + b[i] = '('; + if (a[i] == 1) + b[i] = ')'; + if (a[i] == 2) + b[i] = '['; + if (a[i] == 3) + b[i] = ']'; + if (a[i] == 4) + b[i] = '{'; + if (a[i] == 5) + b[i] = '}'; + printf("%c", b[i]); + } + printf("\n"); + + char *p; + SqStack s; + + InitStack(&s); //初始化空栈 + p = b; //指针p指向字符数组ch + + pair(s,p);//进行括号配对 + DestoryStack(&s);//将栈销毁 + return 0; +} + diff --git "a/2017-1/sunnyCc/\346\216\222\345\272\217/Sort.c" "b/2017-1/sunnyCc/\346\216\222\345\272\217/Sort.c" new file mode 100644 index 00000000..8258f4db --- /dev/null +++ "b/2017-1/sunnyCc/\346\216\222\345\272\217/Sort.c" @@ -0,0 +1,191 @@ +锘#include "Sort.h" +void CreatArray(SqList *s) +{ + int i; + for (i = 1; i <= LENGTH; i++) + { + s->r[i].key = rand() % 101; //浜х敓0-100鐨勯殢鏈烘暟 + } + s->length = i - 1; + printf("闅忔満鐢熸垚10涓簭鍒椾腑鐨勫厓绱狅細\n"); + for (i = 1; i <= LENGTH; i++) + { + printf("%d ", s->r[i].key); + } +} +void print(int n, int sum1, int sum2)//鎵撳嵃鏁扮粍 +{ + int i; + printf("\n鎬荤殑姣旇緝娆℃暟 %d\n", sum1); + printf("\n鎬荤殑绉诲姩娆℃暟 %d\n", sum2); + printf("\n浜岃呮鏁颁箣鍜屼负 %d\n", sum1 + sum2); + printf("\n"); +} +void InsertSort(SqList *L) +{ + int i, j; + int sum1 = 0, sum2 = 0; + for (i = 2; i <= L->length; ++i) { + if ((++sum1) && LT(L->r[i].key, L->r[i - 1].key))//鑻ユ壘鍒皉[i]r[0] = L->r[i];//浣跨敤r[0]淇濆瓨r[i],浠ヤ究鍚庣画鐩存帴浜ゆ崲 + for (j = i - 1; LT(L->r[0].key, L->r[j].key); --j) //濡傛灉r[0]涓鐩村皬浜巖[j]锛屽垯涓鐩翠氦鎹紱鐩村埌鎵惧埌r[0]>r[j]鐨勪綅缃 + { + L->r[j + 1] = L->r[j]; + sum2++; + } + L->r[j + 1] = L->r[0];//灏唕[0]鏀惧叆浣嶇疆 + } + } + printf("\n宸茬粡鎺掑ソ搴忥紝搴忓垪涓猴細\n"); + for (i = 1; i <= LENGTH; i++) + { + printf("%d ", L->r[i].key); + } + print(MAXSIZE, sum1, sum2); +} + +int Partition(SqList *L, int low, int high, int *sum1, int *sum2) { + int pivotkey; + L->r[0].key = L->r[low].key;//鐢╮[0]鍌ㄥ瓨r[low] + pivotkey = L->r[low].key;//鐢╮[low]鍋氭灑杞撮噺 + while (low < high) //low=high鐨勬椂鍊欑粨鏉熷惊鐜 + { + while (++(*sum1) && low < high && L->r[high].key >= pivotkey)//鎵惧埌鍙崇姣旀灑杞撮噺灏忕殑鍊艰烦鍑 + { + (*sum2)++; + --high; + } + L->r[low] = L->r[high];//鍙崇涓庡乏绔繘琛屼氦鎹 + while (++(*sum1) && low < high&&L->r[low].key <= pivotkey)//鎵惧埌宸︾姣旀灑杞撮噺澶х殑鏁 + { + (*sum2)++; + ++low; + } + L->r[high] = L->r[low];//宸︾涓庡彸绔繘琛屼氦鎹 + } + L->r[low].key = L->r[0].key;//灏嗘灑杞撮噺鏀捐繘涓棿锛屽疄鐜颁竴杞氦鎹 + return low; +} +void QSort(SqList *L, int low, int high, int *sum1, int *sum2) +{ + int pivotloc; + if (low < high) { + pivotloc = Partition(L, low, high, sum1, sum2); + QSort(L, low, pivotloc - 1, sum1, sum2);//鍙崇杩涜涓娆″揩鎺 + QSort(L, pivotloc + 1, high, sum1, sum2);//宸︾杩涜涓娆″揩鎺 + } +} +void QuickSort(SqList *L) { + int sum1 = 0; + int sum2 = 0; + int i; + QSort(L, 1, L->length, &sum1, &sum2); + printf("\n宸茬粡鎺掑ソ搴忥紝搴忓垪涓猴細\n"); + for (i = 1; i <= LENGTH; i++) + { + printf("%d ", L->r[i].key); + } + print(MAXSIZE, sum1, sum2); +} +int SelectMinKey(SqList L, int i) +{ + int k; + int j; + k = i; + for (j = i; j < L.length + 1; j++) + if (L.r[k].key > L.r[j].key)//鎵惧埌鏈灏忎竴椤圭殑搴忓彿鏁 + { + k = j; + } + return k; +} +void SelectSort(SqList*L) { + RedType t; + int i, j; + int sum1 = 0; + int sum2 = 0; + for (i = 1; i < L->length; ++i) + { + j = SelectMinKey(*L, i);//鍦ㄨ〃涓夊嚭鏈灏忎竴椤圭殑搴忓彿鏁帮紝璧嬪肩粰j + if ((++sum1) && i != j)//灏嗘寫閫夊嚭鏉ョ殑鏈灏忛」渚濇涓庤〃涓1锛2锛3...椤逛氦鎹 + { + t = L->r[i]; + L->r[i] = L->r[j]; + L->r[j] = t; + sum2 += 3; + } + } + printf("\n宸茬粡鎺掑ソ搴忥紝搴忓垪涓猴細\n"); + for (i = 1; i <= LENGTH; i++) + { + printf("%d ", L->r[i].key); + + } + print(MAXSIZE, sum1, sum2); +} + +void BubbleSort(SqList*L) { + bool exchange;//瀹氫箟涓涓竷灏斿彉閲忔潵妫楠屾暟缁勬槸鍚﹀凡缁忔湁搴 + RedType t; + int sum1 = 0; + int sum2 = 0; + int i; + for (i = L->length - 1; i > 0; i--) { + exchange = false;//鍏堝畾涔夋暟缁勪负鏃犲簭 + for (int j = 0; (++sum1) && j <= i; j++) + { + if (L->r[j].key > L->r[j + 1].key)//姣旇緝r[j]涓巖[j+1]鐨勫ぇ灏 + { + t = L->r[j]; + L->r[j] = L->r[j + 1]; + L->r[j + 1] = t; + sum2 = sum2 + 3; + } + exchange = true; + } + if (!exchange) return;//濡傛灉鏌愪竴瓒熺畻娉曚笅鏉ユ病鏈変氦鎹㈠厓绱狅紝灏辫鏄庢鏃舵暟缁勫凡缁忔湁搴 + } + printf("\n宸茬粡鎺掑ソ搴忥紝搴忓垪涓猴細\n"); + for (i = 1; i <= LENGTH; i++) + { + printf("%d ", L->r[i].key); + + } + print(MAXSIZE, sum1, sum2); +} +void ShellInsert(SqList *L, int dk, int *sum1, int *sum2) { + int i, j; + for (i = dk + 1; i <= L->length; i++) + { + if (++(*sum1) && LT(L->r[i].key, L->r[i - dk].key))//鑻[i]灏忎簬r[j-dk],鍒欒繘琛屾彃鍏 + { + L->r[0].key = L->r[i].key;//鐢╮[0]鏉ュ偍瀛榬[i] + for (j = i - dk; j > 0 && LT(L->r[0].key, L->r[j].key) && (++(*sum1)); j -= dk) + //姣旇緝r[i]涓巖[j]涔嬮棿澶у皬锛屽鏋滃皬浜庡垯灏唕[j]绉诲姩鍒皉[i]鐨勪綅缃 + { + L->r[j + dk] = L->r[j]; + (*sum2)++; + } + L->r[j + dk].key = L->r[0].key; + } + } + +} +void ShellSort(SqList *L) { + int i; + int sum1 = 0; + int sum2 = 0; + for (i = L->length / 2; i > 0; i = i / 2)//鍏堝畾涔夊閲忎负L->length/2,鍚庡簭寰幆涓哄叾涓鍗 + { + ShellInsert(L, i, &sum1, &sum2); + } + printf("\n宸茬粡鎺掑ソ搴忥紝搴忓垪涓猴細\n"); + for (i = 1; i <= LENGTH; i++) + { + printf("%d ", L->r[i].key); + + } + print(MAXSIZE, sum1, sum2); +} + diff --git "a/2017-1/sunnyCc/\346\216\222\345\272\217/Sort.h" "b/2017-1/sunnyCc/\346\216\222\345\272\217/Sort.h" new file mode 100644 index 00000000..e88922d6 --- /dev/null +++ "b/2017-1/sunnyCc/\346\216\222\345\272\217/Sort.h" @@ -0,0 +1,31 @@ +#pragma once +#include +#include +#include +#include +#define MAXSIZE 20 +#define LENGTH 10 +#define LT(a,b) ((a)<(b)) +typedef int KeyType; +typedef int InfoType; +typedef struct +{ + KeyType key; + InfoType otherinof; +}RedType; +typedef struct +{ + RedType r[MAXSIZE]; + int length; +}SqList; +void CreatArray(SqList *s); +void print(int n, int sum1, int sum2); +void InsertSort(SqList *L); +int Partition(SqList *L, int low, int high, int *sum1, int *sum2); +void QSort(SqList *L, int low, int high, int *sum1, int *sum2); +void QuickSort(SqList *L); +int SelectMinKey(SqList L, int i); +void SelectSort(SqList*L); +void BubbleSort(SqList*L); +void ShellInsert(SqList *L, int dk, int *sum1, int *sum2); +void ShellSort(SqList *L); \ No newline at end of file diff --git "a/2017-1/sunnyCc/\346\216\222\345\272\217/Sort.jpg" "b/2017-1/sunnyCc/\346\216\222\345\272\217/Sort.jpg" new file mode 100644 index 00000000..86b35610 Binary files /dev/null and "b/2017-1/sunnyCc/\346\216\222\345\272\217/Sort.jpg" differ diff --git "a/2017-1/sunnyCc/\346\216\222\345\272\217/text.c" "b/2017-1/sunnyCc/\346\216\222\345\272\217/text.c" new file mode 100644 index 00000000..fa2bf953 --- /dev/null +++ "b/2017-1/sunnyCc/\346\216\222\345\272\217/text.c" @@ -0,0 +1,32 @@ +#include "Sort.h" +int main() { + + SqList s; + + printf("\n---------------------------------"); + printf("\n1.InsertSort\n"); + CreatArray(&s); + InsertSort(&s); + + printf("---------------------------------"); + printf("\n2.QuickSort\n"); + CreatArray(&s); + QuickSort(&s); + + printf("---------------------------------"); + printf("\n3.SelectSort\n"); + CreatArray(&s); + SelectSort(&s); + + printf("---------------------------------"); + printf("\n4.BubbleSort\n"); + CreatArray(&s); + BubbleSort(&s); + + printf("---------------------------------"); + printf("\n5.ShellSort\n"); + CreatArray(&s); + ShellSort(&s); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\346\225\260\345\210\266\350\275\254\346\215\242/conversion.c" "b/2017-1/sunnyCc/\346\225\260\345\210\266\350\275\254\346\215\242/conversion.c" new file mode 100644 index 00000000..4a0103f4 --- /dev/null +++ "b/2017-1/sunnyCc/\346\225\260\345\210\266\350\275\254\346\215\242/conversion.c" @@ -0,0 +1,73 @@ +#include "conversion.h" +#include +#define MAX 1000 +//初始化顺序栈 +int InitStack(SqStack *S) { + S->base = (int*)malloc(MAX); + if (!S->base) { + return 0;//存储空间分配失败 + } + S->top = S->base; + S->stacksize = MAX; + return 1; +}; + +//判断栈是否为空 +int IsEmpty(SqStack *S) { + if (S->base == S->top) { + return 1;//栈空 + } + else { + return 0;//栈非空 + } +}; + +//入栈 +int Push(SqStack *S, int e) { + if (S->top - S->base == S->stacksize) { + return 0;//栈满 + } + *S->top++ = e; //插入e为新的栈顶元素 + return 1; +}; + +//出栈 +int Pop(SqStack *S, int *e) { + if (S->top == S->base) { + return 0;//栈空 + //printf("栈空\n"); + } + *e = *--S->top;//删除S的栈顶元素,弹出原顶部,再把指针指向e + return 1; + //printf("%d",e); +}; +//销毁栈 +int DestoryStack(SqStack *S) { + S->top = S->base; + free(S->base); + S->top = NULL; + S->base = NULL; + printf("释放栈空间\n"); + return 1; +} +//数制转换 +void conversion(SqStack *S, int n, int m) { + //n表示待转换的数,m表示输出的数的进制 + InitStack(S); + while (n) { + Push(S, n%m); //进站 + n = n / m; + } + + while (!IsEmpty(S)) { + int e; + Pop(S, &e); //出栈 + printf("%d", e); + } + //如果栈为空,释放栈空间 + if (NULL != S) + { + free(S); + S = NULL; + } +}; diff --git "a/2017-1/sunnyCc/\346\225\260\345\210\266\350\275\254\346\215\242/conversion.h" "b/2017-1/sunnyCc/\346\225\260\345\210\266\350\275\254\346\215\242/conversion.h" new file mode 100644 index 00000000..e7decab4 --- /dev/null +++ "b/2017-1/sunnyCc/\346\225\260\345\210\266\350\275\254\346\215\242/conversion.h" @@ -0,0 +1,15 @@ +#include + +//定义一个顺序栈的结构 +typedef struct { + int *base;//栈底指针 + int *top;//栈顶指针 + int stacksize; +}SqStack; + +int InitStack(SqStack *S); +int IsEmpty(SqStack *S); +int Push(SqStack *S, int e); +int Pop(SqStack *S, int *e); +void conversion(SqStack *S, int n, int m); + diff --git "a/2017-1/sunnyCc/\346\225\260\345\210\266\350\275\254\346\215\242/\346\225\260\345\210\266\350\275\254\346\215\242.PNG" "b/2017-1/sunnyCc/\346\225\260\345\210\266\350\275\254\346\215\242/\346\225\260\345\210\266\350\275\254\346\215\242.PNG" new file mode 100644 index 00000000..eb5abfa4 Binary files /dev/null and "b/2017-1/sunnyCc/\346\225\260\345\210\266\350\275\254\346\215\242/\346\225\260\345\210\266\350\275\254\346\215\242.PNG" differ diff --git "a/2017-1/sunnyCc/\346\225\260\345\210\266\350\275\254\346\215\242/\346\225\260\345\210\266\350\275\254\346\215\242.c" "b/2017-1/sunnyCc/\346\225\260\345\210\266\350\275\254\346\215\242/\346\225\260\345\210\266\350\275\254\346\215\242.c" new file mode 100644 index 00000000..feff1e4f --- /dev/null +++ "b/2017-1/sunnyCc/\346\225\260\345\210\266\350\275\254\346\215\242/\346\225\260\345\210\266\350\275\254\346\215\242.c" @@ -0,0 +1,45 @@ +/* 数制转换:随机产生一个数和数值(1到9之间),输出进行数值转换后的数*/ +#include "conversion.h" +#include + +#include +#include //用到了time函数 +#define MAX 1000//顺序栈存储空间最大值 + +int main() { + + SqStack S; + int n, m; + srand(time(0)); + + if (InitStack(&S)) { + printf("栈S初始化成功!\n"); + } + else { + printf("栈S初始化失败!\n"); + } + + if (IsEmpty(&S)) { + printf("栈为空.\n"); + } + else { + printf("栈非空.\n"); + } + + printf("\n"); + + printf("要转换的数n:"); + n = rand() % 999999; + printf("%d\n", n); + + printf("要输出的数制:"); + m = rand() % 9 + 2; + printf("%d\n", m); + + printf("转换数制后的数:"); + conversion(&S, n, m); + DestoryStack(&S);//将栈销毁 + printf("\n"); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/AVLOutput.txt" "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/AVLOutput.txt" new file mode 100644 index 00000000..a1d0bc93 --- /dev/null +++ "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/AVLOutput.txt" @@ -0,0 +1,6 @@ +8, 4, 3, 1, 6, 5, 7, 14, 10, 22, 19, 30 +8, 4, 3, 1, 6, 5, 7, 14, 10, 13, 22, 19, 30 +7, 4, 3, 1, 6, 5, 14, 10, 13, 22, 19, 30 +7, 4, 3, 1, 6, 14, 10, 13, 22, 19, 30 +7, 4, 3, 1, 6, 14, 10, 13, 22, 19, 20, 30 +7, 4, 3, 1, 14, 10, 13, 22, 19, 20, 30 diff --git "a/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/AVLTree.c" "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/AVLTree.c" new file mode 100644 index 00000000..b65cb47f --- /dev/null +++ "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/AVLTree.c" @@ -0,0 +1,302 @@ +#include "AVLTree.h" +int flag; +int InsertAVL(BSTree* T, ElemType e, bool* taller) +{ + //若在平衡的二叉树排序树中不存在和e有相同关键字的结点,则插入一个数据元素 + //为e的新结点,并返回1,否则返回0/若因插入而使二叉排序树失去平衡,则作平衡 + //旋转处理,布尔变量taller反映T长高与否 + if ((*T) == NULL) + { //插入新结点,数“长高”,置taller为TRUE + (*T) = (BSTree)malloc(sizeof(BSTNode)); + (*T)->data = e; + (*T)->lchild = NULL; + (*T)->rchild = NULL; + (*T)->bf = EH; + *taller = true; + } + else if (e == (*T)->data) //树中已存在与e有相同关键点的结点 + { + *taller = false; //不再插入 + return 0; + } + else if (e < (*T)->data) //继续在*T的左子树中进行搜索 + { + if (!InsertAVL(&(*T)->lchild, e, taller)) + return 0; //未插入 + if (*taller) //已插入到*T的左子树中且左子树“长高” + { + switch ((*T)->bf) //检查*T的平衡度 + { + case LH: //原本左子树比右子树高,需要做左平衡处理 + LeftBalance(T); + *taller = false; + break; + case EH: //原本左右子树一样高,现因左子树增高而使树增高 + (*T)->bf = LH; + *taller = true; + break; + case RH: //原本右子树比左子树高,需要做右平衡处理 + (*T)->bf = EH; + *taller = false; + break; + } + } + } + else + { //因继续在*T的右子树中进行搜索 + if (!InsertAVL(&(*T)->rchild, e, taller)) //未插入 + return 1; + if (*taller) //已插入到*T的右子树中且右子树“长高” + { + switch ((*T)->bf) //检查*T的平衡度 + { + case LH: //原本左子树比右子树高,现在左右子树等高 + (*T)->bf = EH; + *taller = false; + break; + case EH: //原本左右子树一样高,现因右子树增高而使树增高 + (*T)->bf = RH; + *taller = true; + break; + case RH: + RightBalance(T); //原本右子树比左子树高,需要做右平衡处理 + *taller = false; + break; + } + } + } + return 1; +} +//建立二叉平衡树 +Status CreatBST(BSTree *T, int a[], int n) +{ + int i; + bool *taller; + *T = NULL; + for (i = 0; i < n; i++) + { + InsertAVL(&(*T), a[i], &taller); + } + return OK; +}; +//删除二叉平衡树 +bool Delete(BSTree *T) +{ + BSTree L, S; + if (!(*T)->lchild && !(*T)->rchild)//左右子树都为空 + { + *T = NULL; + } + else if (!(*T)->lchild)// 左子树为空,重接它的右子树 + { + L = *T; + *T = (*T)->rchild; + free(L); + } + else if (!(*T)->rchild) // 右子树为空,重接它的左子树 + { + L = *T; + *T = (*T)->lchild; + free(L); + } + else//左右子树均不为空 + { + L = *T; + S = (*T)->lchild; + while (S->rchild) // S 指向被删结点的前驱 + { + L = S; + S = S->rchild; + } + (*T)->data = S->data; + if (L != *T) + { + L->rchild = S->lchild;// 重接*q的右子树 + } + else + { + L->lchild = S->lchild;// 重接*q的左子树 + } + free(S); + } + return true; +}; +//删除值为key的结点 +bool DeleteAVL(BSTree *T, int key) +{ + if (!T) + { + return false; + } + else + { + if (key == (*T)->data) + { + Delete(T); + return true; + } + else if (key < (*T)->data) + { + return DeleteAVL(&(*T)->lchild, key); + } + else + { + return DeleteAVL(&(*T)->rchild, key); + } + } +}; +void R_Rotate(BSTree* p) +{ //对以*p为根的二叉排序树作右旋处理,处理之后p指向新的树根节点 + //即旋转处理之前的左子树的根节点 + BSTree lc = (*p)->lchild; //lc指向*p的左子树根结点 + (*p)->lchild = lc->rchild; //lc的右子树挂接为*p的左子树 + lc->rchild = *p; + *p = lc; //p指向新的根结点 +} + +void L_Rotate(BSTree* p) +{ //对以*p为根的二叉排序树作左旋处理,处理之后p指向新的树根节点 + //即旋转处理之前的右子树的根节点 + BSTree rc = (*p)->rchild; //rc指向*p的右子树根结点 + (*p)->rchild = rc->lchild; //rc的左子树挂接为*p的右子树 + rc->lchild = *p; + *p = rc; //p指向新的根结点 +} +/* 对以指针T为根结点的二叉树做左平衡旋转处理,处理结束后,指针T指向新的根结点 */ +void LeftBalance(BSTree* T) +{ + BSTree lc, rd; + lc = (*T)->lchild; //lc指向T的左子树根结点 + switch (lc->bf) + { //检查T的左子树的平衡因子 + case LH: //新结点插入到T的左孩子的左子树上,要做单右旋处理 + (*T)->bf = lc->bf = EH; + R_Rotate(T); + break; + case RH: //新结点插入到T的左孩子的右子树上,要做双旋处理 + rd = lc->rchild; //rd指向T的左孩子的右子树的根结点 + switch (rd->bf) //修改T及其左孩子的平衡因子 + { + case LH: + (*T)->bf = RH; + lc->bf = EH; + break; + case EH: + (*T)->bf = lc->bf = EH; + break; + case RH: + (*T)->bf = EH; + lc->bf = LH; + break; + } + rd->bf = EH; + L_Rotate(&(*T)->lchild); + R_Rotate(T); + break; + } +} +//对以指针T为根结点的二叉树做右平衡旋转处理,处理结束后,指针T指向新的根结点 +void RightBalance(BSTree* T) +{ + BSTree lc, rd; + lc = (*T)->rchild; + switch (lc->bf) + { + case RH: + (*T)->bf = lc->bf = EH; + L_Rotate(T); + break; + case LH: + rd = lc->lchild; + switch (rd->bf) + { + case LH: + (*T)->bf = EH; + lc->bf = RH; + break; + case EH: + (*T)->bf = lc->bf = EH; + break; + case RH: + (*T)->bf = EH; + lc->bf = LH; + break; + } + rd->bf = EH; + R_Rotate(&(*T)->rchild); + L_Rotate(T); + break; + } +} + +//在二叉树里面查找元素key +//若成功,返回true +//若不成功,则根据其key大小结合排序二叉树的属性,确定在左子树还是在右子树中使用递归算法查找 +bool SearchAVL(BSTree T, int key, BSTree f, BSTree *p) +{ + if (!T) + { + *p = f; + return false; + } + else + { + if (key == T->data) + { + p = &T; + return true; + } + else if (key > T->data) + { + return SearchAVL(T->rchild, key, T, p); + } + else + { + return SearchAVL(T->lchild, key, T, p); + } + } +}; +//打印到文件txt中 +int TraversePrint(BSTree T, FILE*pfile) +{ + if (T) + { + print(T->data, pfile); + TraversePrint(T->lchild, pfile); + TraversePrint(T->rchild, pfile); + } + else + return 1; +}; +//打印到文件txt中 +bool print(int data, FILE*pfile) +{ + + char d[2] = { ", " }; + if (NULL == pfile) + { + return false; + } + if (NULL != pfile) + { + if (flag == 1) + { + fwrite(d, sizeof(d), 1, pfile); + } + fprintf(pfile, "%d", data); + flag = 1; + return true; + } +} +//先序遍历输出T +int PreOrderTraverse(BSTree T) +{ + if (T) + { + printf("%d ", T->data); + PreOrderTraverse(T->lchild); + PreOrderTraverse(T->rchild); + } + else + return 1; +}; diff --git "a/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/AVLTree.h" "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/AVLTree.h" new file mode 100644 index 00000000..5ce60e36 --- /dev/null +++ "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/AVLTree.h" @@ -0,0 +1,33 @@ +#include +#include +#define LH +1 //左高 +#define EH 0 //等高 +#define RH -1 //右高 +typedef int ElemType; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef enum { + false, + true +}bool; +//二叉树的二叉链表结点结构定义 +typedef struct BSTNode { + ElemType data; //结点数据 + int bf; //结点平衡因子 + struct BSTNode *lchild, *rchild; //左右孩子指针 +} BSTNode, *BSTree; + +void R_Rotate(BSTree* p); +void L_Rotate(BSTree* p); +void LeftBalance(BSTree* T); +void RightBalance(BSTree* T); +int InsertAVL(BSTree* T, ElemType e, bool* taller); +bool FindNode(BSTree root, ElemType e, BSTree* pos); +int TraversePrint(BSTree T, FILE*pfile); +int PreOrderTraverse(BSTree T); +bool print(int data, FILE*pfile); +bool DeleteBST(BSTree *T, int key); \ No newline at end of file diff --git "a/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/BSTOutput.txt" "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/BSTOutput.txt" new file mode 100644 index 00000000..858758ad --- /dev/null +++ "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/BSTOutput.txt" @@ -0,0 +1,6 @@ +8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30 +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30 +7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30 diff --git "a/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/Search.c" "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/Search.c" new file mode 100644 index 00000000..6c9bb860 --- /dev/null +++ "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/Search.c" @@ -0,0 +1,179 @@ +#include "Search.h" +int flag; + +//向二叉排序树插入元素e,根据e的大小分为三种情况 +bool InsertBST(BiTree *T, int e) +{ + if (*T == NULL)//T为空树,插入e为根节点 + { + *T = (BiTree)malloc(sizeof(BiTNode)); + (*T)->data = e; + (*T)->lchild = (*T)->rchild = NULL;; + return true; + } + if (e == (*T)->data)// 当二叉排序树中存在元素等于e,返回FALSE + { + return false; + } + if (e < (*T)->data)//e值小于当前结点数值,插入e为左孩子 + { + return InsertBST(&(*T)->lchild, e); + } + if (e > (*T)->data)//e值大于当前结点数值,插入e为右孩子 + { + return InsertBST(&(*T)->rchild, e); + } +}; + +//建立排序二叉树 +Status CreatBST(BiTree *T, int a[], int n) +{ + int i; + *T = NULL; + for (i = 0; i < n; i++) + { + InsertBST(&(*T), a[i]); + } + return OK; +}; + +//在二叉树里面查找元素key +//若成功,返回true +//若不成功,则根据其key大小结合排序二叉树的属性,确定在左子树还是在右子树中使用递归算法查找 +bool SearchBST(BiTree T, int key, BiTree f, BiTree *p) +{ + if (!T) + { + *p = f; + return false; + } + else + { + if (key == T->data) + { + p = &T; + return true; + } + else if (key > T->data) + { + return SearchBST(T->rchild, key, T, p); + } + else + { + return SearchBST(T->lchild, key, T, p); + } + } +}; + +//删除二叉排序树 +bool Delete(BiTree *T) +{ + BiTree L, S; + if (!(*T)->lchild && !(*T)->rchild)//左右子树都为空 + { + *T = NULL; + } + else if (!(*T)->lchild)// 左子树为空,重接它的右子树 + { + L = *T; + *T = (*T)->rchild; + free(L); + } + else if (!(*T)->rchild) // 右子树为空,重接它的左子树 + { + L = *T; + *T = (*T)->lchild; + free(L); + } + else//左右子树均不为空 + { + L = *T; + S = (*T)->lchild; + while (S->rchild) // S 指向被删结点的前驱 + { + L = S; + S = S->rchild; + } + (*T)->data = S->data; + if (L != *T) + { + L->rchild = S->lchild;// 重接*q的右子树 + } + else + { + L->lchild = S->lchild;// 重接*q的左子树 + } + free(S); + } + return true; +}; + +//删除值为key的结点 +bool DeleteBST(BiTree *T, int key) +{ + if (!T) + { + return false; + } + else + { + if (key == (*T)->data) + { + Delete(T); + return true; + } + else if (key < (*T)->data) + { + return DeleteBST(&(*T)->lchild, key); + } + else + { + return DeleteBST(&(*T)->rchild, key); + } + } +}; +//打印到文件txt中 +bool print(int data, FILE*pfile) +{ + + char d[2] = { ", " }; + /*char *d =",";*/ + if (NULL == pfile) + { + return false; + } + if (NULL != pfile) + { + if (flag == 1) + { + fwrite(d, sizeof(d), 1, pfile); + } + fprintf(pfile, "%d", data); + flag = 1; + return true; + } +} + +//先序遍历输出T +int PreOrderTraverse(BiTree T) +{ + if (T) + { + printf("%d ", T->data); + PreOrderTraverse(T->lchild); + PreOrderTraverse(T->rchild); + } + else + return 1; +}; +int preOrderTraverse(BiTree T, FILE*pfile) +{ + if (T) + { + print(T->data, pfile); + preOrderTraverse(T->lchild, pfile); + preOrderTraverse(T->rchild, pfile); + } + else + return 1; +}; \ No newline at end of file diff --git "a/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/Search.h" "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/Search.h" new file mode 100644 index 00000000..d80bc561 --- /dev/null +++ "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/Search.h" @@ -0,0 +1,27 @@ +#include +#include +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef enum { + false, + true +}bool; +typedef struct BiTNode +{ + int data; + struct BiTNode *lchild, *rchild; +} BiTNode, *BiTree; + +bool SearchBST(BiTree T, int key, BiTree f, BiTree *p); +bool InsertBST(BiTree *T, int e); +Status CreatBST(BiTree *T, int a[], int n); +bool Delete(BiTree *T); +bool DeleteBST(BiTree *T, int key); +bool print(int data, FILE*pfile); +int preOrderTraverse(BiTree T, FILE*pfile); +int PreOrderTraverse(BiTree T); +#pragma once diff --git "a/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/main.c" "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/main.c" new file mode 100644 index 00000000..bbcb1dab --- /dev/null +++ "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/main.c" @@ -0,0 +1,50 @@ +#include "AVLTree.h" +extern int flag; +#define num 12 +#define Search_num 5 +int main() +{ + + int a[num] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + int b[Search_num] = { 13, 8, 5, 20, 6 }; + int i; + bool taller, m; + char d = '\n'; + FILE *pfile; + BSTree T = NULL, pos, f, p; + + p = (BSTree)malloc(sizeof(BSTree)); + f = (BSTree)malloc(sizeof(BSTree)); + + pfile = fopen(" AVLOutput.txt ", "a"); + + + CreatBST(&T, a, num); + + TraversePrint(T, pfile); + fwrite(&d, sizeof(d), 1, pfile); + + for (i = 0; i < Search_num; i++) + { + m = SearchAVL(T, b[i], f, &p); + printf("--------------------------------------------------------\n\n"); + printf("开始在二叉排序树中查找%d: ", b[i]); + if (m == true) + { + printf("\n找到%d,在二叉排列树中删除%d\n", b[i], b[i]); + DeleteAVL(&T, b[i]); + } + else + { + printf("\n未找到%d,在二叉排列树中插入%d\n", b[i], b[i]); + InsertAVL(&T, b[i], &taller); + } + printf("现在的二叉排列树为:", b[i]); + PreOrderTraverse(T); + printf("\n\n"); + flag = 0; + TraversePrint(T, pfile); + fwrite(&d, sizeof(d), 1, pfile); + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/text.c" "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/text.c" new file mode 100644 index 00000000..62c87ce5 --- /dev/null +++ "b/2017-1/sunnyCc/\346\237\245\346\211\276\350\241\250\347\232\204\345\210\235\345\247\213\345\214\226\345\273\272\347\253\213\345\222\214\346\237\245\346\211\276/text.c" @@ -0,0 +1,50 @@ +#include "Search.h" +extern int flag; +#define num 12 +#define Search_num 5 +int main() +{ + int a[num] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + int b[Search_num] = { 13, 8, 5, 20, 6 }; + int i; + bool m; + char d = '\n'; + FILE *pfile; + BiTree T, f, p; + + T = (BiTree)malloc(sizeof(BiTNode)); + p = (BiTree)malloc(sizeof(BiTree)); + f = (BiTree)malloc(sizeof(BiTree)); + + pfile = fopen(" BSTOutput.txt ", "a"); + + CreatBST(&T, a, num); + preOrderTraverse(T, pfile); + + fwrite(&d, sizeof(d), 1, pfile); + + for (i = 0; i < Search_num; i++) + { + m = SearchBST(T, b[i], f, &p); + printf("--------------------------------------------------------\n\n"); + printf("开始在二叉排序树中查找%d: ", b[i]); + if (m == true) + { + printf("\n找到%d,在二叉排列树中删除%d\n", b[i], b[i]); + DeleteBST(&T, b[i]); + } + else + { + printf("\n未找到%d,在二叉排列树中插入%d\n", b[i], b[i]); + InsertBST(&T, b[i]); + } + printf("现在的二叉排列树为:", b[i]); + PreOrderTraverse(T); + printf("\n\n"); + flag = 0; + preOrderTraverse(T, pfile); + fwrite(&d, sizeof(d), 1, pfile); + } + + return 0; +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\350\241\214\347\274\226\350\276\221\345\231\250/edit.c" "b/2017-1/sunnyCc/\350\241\214\347\274\226\350\276\221\345\231\250/edit.c" new file mode 100644 index 00000000..205e8349 --- /dev/null +++ "b/2017-1/sunnyCc/\350\241\214\347\274\226\350\276\221\345\231\250/edit.c" @@ -0,0 +1,91 @@ +#include "edit.h" +#include +#include +#include //malloc,realloc + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +#define OVERFLOW -2 +#define OK 1 +#define ERROR 0 + +//初始化栈 +int InitStack(SqStack *S) { + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!S->base) { + return OVERFLOW; + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + + return OK; +} + +//入栈 +int Push(SqStack *S, SElemType e) { + if ((S->top - S->base) >= S->stacksize) { + S->base = (SElemType*)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if (!S->base) { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + //printf("%c\n",e); + return OK; +} + +//删除栈中的元素 +int Pop(SqStack *S, SElemType *e) { + if (S->top == S->base) { + return ERROR; + } + *e = *--S->top; + return OK; +} + + +//清空栈中的元素 +int ClearStack(SqStack *S) { + S->top = S->base; + return OK; +} + +//销毁栈 +int DestoryStack(SqStack *S) { + S->top = S->base; + free(S->base); + S->top = NULL; + S->base = NULL; + return OK; +} + +//行编辑程序 +void LineEdit(SqStack *S) { + SElemType *p, ch, c; + InitStack(S); + ch = getchar(); + while (ch != -1) { + while (ch != -1 && ch != '\n') { + switch (ch) { + case '#':Pop(S, &c); break;//仅当栈非空时退栈 + case '@':ClearStack(S); break;//重置S为空栈 + default:Push(S, ch); break;//有效字符进栈,未考虑栈满情况 + } + ch = getchar();//从终端接收下一个字符 + } + p = S->base; + while (p != S->top) { + printf("%c", *p); + ++p; + } + //将从栈底到栈顶的站内字符传送至调用过程的数据区 + ClearStack(S);//重置S为空栈 + if (ch != EOF) + { + ch = getchar(); + } + } + +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\350\241\214\347\274\226\350\276\221\345\231\250/edit.h" "b/2017-1/sunnyCc/\350\241\214\347\274\226\350\276\221\345\231\250/edit.h" new file mode 100644 index 00000000..112a28dd --- /dev/null +++ "b/2017-1/sunnyCc/\350\241\214\347\274\226\350\276\221\345\231\250/edit.h" @@ -0,0 +1,16 @@ +#include +typedef char SElemType; + +//栈结构体 +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +int InitStack(SqStack *S);//初始化栈 +int Push(SqStack *S, SElemType e);//入栈 +int Pop(SqStack *S, SElemType *e);//删除栈中的元素 +int DestoryStack(SqStack *S);//销毁栈 +void LineEdit(SqStack *S);//行编辑程序 +int ClearStack(SqStack *S);//清空栈中的元素 \ No newline at end of file diff --git "a/2017-1/sunnyCc/\350\241\214\347\274\226\350\276\221\345\231\250/\350\241\214\347\274\226\350\276\221\345\231\250.PNG" "b/2017-1/sunnyCc/\350\241\214\347\274\226\350\276\221\345\231\250/\350\241\214\347\274\226\350\276\221\345\231\250.PNG" new file mode 100644 index 00000000..7502d5e1 Binary files /dev/null and "b/2017-1/sunnyCc/\350\241\214\347\274\226\350\276\221\345\231\250/\350\241\214\347\274\226\350\276\221\345\231\250.PNG" differ diff --git "a/2017-1/sunnyCc/\350\241\214\347\274\226\350\276\221\345\231\250/\350\241\214\347\274\226\350\276\221\345\231\250.c" "b/2017-1/sunnyCc/\350\241\214\347\274\226\350\276\221\345\231\250/\350\241\214\347\274\226\350\276\221\345\231\250.c" new file mode 100644 index 00000000..69a2dfd6 --- /dev/null +++ "b/2017-1/sunnyCc/\350\241\214\347\274\226\350\276\221\345\231\250/\350\241\214\347\274\226\350\276\221\345\231\250.c" @@ -0,0 +1,13 @@ +#include +#include +#include "edit.h" + +int main() +{ + SqStack sq; + int f; + LineEdit(&sq);//进行行编辑 + DestoryStack(&sq);//将栈销毁 + return 0; + +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/main.c" "b/2017-1/sunnyCc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/main.c" new file mode 100644 index 00000000..01d4fc21 --- /dev/null +++ "b/2017-1/sunnyCc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/main.c" @@ -0,0 +1,17 @@ +#include"operation.h" +#include +int main() { + char a[100] = { "7+(3*4-6)/3#" }; + int c; + printf("中缀表达式为:\n"); + puts(a); + char b[100] = { '\0' }; + transform(b, a); + printf("\n后缀表达式为:\n"); + puts(b); + c = count(b); + printf("\n结果为:\n"); + printf("%d", c); + printf("\n"); + return 0; +} diff --git "a/2017-1/sunnyCc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/operation.PNG" "b/2017-1/sunnyCc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/operation.PNG" new file mode 100644 index 00000000..a81cf499 Binary files /dev/null and "b/2017-1/sunnyCc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/operation.PNG" differ diff --git "a/2017-1/sunnyCc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/operation.c" "b/2017-1/sunnyCc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/operation.c" new file mode 100644 index 00000000..aff0e261 --- /dev/null +++ "b/2017-1/sunnyCc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/operation.c" @@ -0,0 +1,209 @@ +#include "operation.h" +const char OP[7] = { '+','-','*','/','(',')','#' };//struct an array to store 7 operators +const int PrecedenceTable[7][7] = { + { 1,1,0,0,0,1,1 }, + { 1,1,0,0,0,1,1 }, + { 1,1,1,1,0,1,1 }, + { 1,1,1,1,0,1,1 }, + { 0,0,0,0,0,0,0 }, + { 0,0,0,0,0,0,0 } +};//according to the chart 3-1 to make a precedenceTable +Status InitStack(SqStack *S) { + S->base = (SElemType *)malloc(SIZE * sizeof(SElemType)); + if (!S->base) { exit(OVERFLOW); }//To judge whether the last step is success or not + S->top = S->base;//the stack we initialize is empty + S->stacksize = SIZE; + return OK; +}//initialize a stack of operators +Status InitStackn(COUNT *S) { + S->base = (int *)malloc(SIZE * sizeof(int)); + if (!S->base) { exit(OVERFLOW); } + S->top = S->base; + S->stacksize = SIZE; + return OK; +} + +Status StackEmpty(SqStack S) { + if (S.base == S.top) { return TRUE; } + else { return FALSE; } +}//To judge whether the stack is empty or not +Status StackEmptyn(COUNT S) { + if (S.base == S.top) { return TRUE; } + else { return FALSE; } +} + +Status Pop(SqStack *S, SElemType *e) { + if (S->top == S->base) { return ERROR; }//we can not pop a character from an empty stack + *e = *(--S->top);//poping the current top character is equal to make the second top character to the topmost position + return OK; +} +Status Popn(COUNT *S, int *e) { + if (S->top == S->base) { return ERROR; } + *e = *(--S->top); + return OK; +} + +Status Push(SqStack *S, SElemType e) { + if (S->top - S->base >= S->stacksize) { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + //if the current stack is full,we should realloc some extra space to it before pushing a new character + if (!S->base) { exit(OVERFLOW); } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT;//after reallocing,both the top character and the stacksize are changed + } + *S->top++ = e;//assign 'e' to s->top and then remove the adress of s->top to next position + return OK; +} +Status Pushn(COUNT *S, int e) { + if (S->top - S->base >= S->stacksize) { + S->base = (int *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(int)); + if (!S->base) { exit(OVERFLOW); } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +} + +Status GetTop(SqStack S, SElemType *e) { + //if the stack is not empty,use the charactor to return the value of s->top.else return ERROR + if (S.top == S.base) { return ERROR; } + *e = *(S.top - 1); + return OK; +} +Status GetTopn(COUNT S, int *e) { + + if (S.top == S.base) { return ERROR; } + *e = *(S.top - 1); + return OK; +} + +int IN(char ch) { + int i = 0, flag = 0; + for (i = 0; i < 7; i++) { + if (ch == OP[i]) + { + flag = 1; + break; + } + } + return flag; +}//To judge whether the character is one of the seven operators in the OP array or not + +void Pass(char *suffix, char ch) { + static int i = 0; + suffix[i] = ch; + i++; +}//Passing the charactor to an array so that we can change the original nifix experssion to suffix experssion + +int Precede(char c, char ch) { + int i, j; + switch (c) { + case'+':i = 0; break; + case'-':i = 1; break; + case'*':i = 2; break; + case'/':i = 3; break; + case'(':i = 4; break; + case')':i = 5; break; + case'#':i = 6; break; + } + switch (ch) { + case'+':j = 0; break; + case'-':j = 1; break; + case'*':j = 2; break; + case'/':j = 3; break; + case'(':j = 4; break; + case')':j = 5; break; + case'#':j = 6; break; + } + return PrecedenceTable[i][j]; +} + +int operate(char ch, int t1, int t2) { + switch (ch) { + case'+':return t2 + t1; break; + case'-':return t2 - t1; break; + case'*':return t2*t1; break; + case '/':return t2 / t1; break; + } + +} + +void transform(char *suffix, char *exp) { + SqStack S; + SElemType c = '#'; + InitStack(&S); + Push(&S, c); + char *p; + p = exp; + char ch = *p; + while (StackEmpty(S) == FALSE) { + if (!IN(ch)) { + Pass(suffix, ch); + }//if 'ch' is an operand,passing it to the array suffix directly + else { + switch (ch) { + case'(': + Push(&S, ch);//Inserting '(' as a new s->top value + break; + case')': + Pop(&S, &c);//Poping the current s->top value and using 'c' to rutern the value + while (c != '(') { + Pass(suffix, c);//unless 'c' is ')',passing the operator to array suffix while poping 'c' to the stack S + Pop(&S, &c); + } + break; + default: + while (GetTop(S, &c) == OK && (Precede(c, ch))) { + Pass(suffix, c); + Pop(&S, &c); + } + if (ch != '#') { + Push(&S, ch); + } + break; + }//if 'ch' is an operater,comparing it with the current top character in priority before pushing + } + if (ch != '#') { + p++; + ch = *p; + }//Setting this step to ignore the appearance of operaters that are not in the array of OP + else { + Pop(&S, &ch); + Pass(suffix, ch); + } + }//end of while +}//end of transform + +int count(char *suffix) { + SqStack OPTR; + COUNT OPND; + InitStack(&OPTR); + InitStackn(&OPND); + char *p; + p = suffix; + char ch = *p; + char c; + while (ch != '#') { + if (!IN(ch)) { + Pushn(&OPND, ch - '0');//transforming operand's character code form to number form + } + else { + Push(&OPTR, ch); + if ((OPND.top - 1) && (OPND.top - 2)) { + //if the operand stack have two operand form top to bottom,operate them + int t1, t2; + Popn(&OPND, &t1); + Popn(&OPND, &t2); + int t3 = operate(ch, t1, t2); + Pushn(&OPND, t3); + Pop(&OPTR, &c); + } + } + p++; + ch = *p; + }//end of while + int sum; + Popn(&OPND, &sum); + return sum; +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/operation.h" "b/2017-1/sunnyCc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/operation.h" new file mode 100644 index 00000000..dd66fe27 --- /dev/null +++ "b/2017-1/sunnyCc/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/operation.h" @@ -0,0 +1,44 @@ +锘#pragma once +#include +#include +#define SIZE 100 +#define STACKINCREMENT 10 +typedef char SElemType; +typedef struct _SqStack { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; +typedef struct _COUNT { + int *base; + int *top; + int stacksize; +}COUNT; +typedef enum { + FALSE, + OK, + OVERFLOW, + TRUE, + ERROR, +}Status; +Status InitStack(SqStack *S); +Status InitStackn(COUNT *S); + +Status Pop(SqStack *S, SElemType *e); +Status Popn(COUNT *S, int *e); + +Status Push(SqStack *S, SElemType e); +Status Pushn(COUNT *S, int e); + +Status StackEmpty(SqStack S); +Status StackEmptyn(COUNT S); + +Status GetTop(SqStack S, SElemType *e); +Status GetTopn(COUNT S, int *e); + +void transform(char *, char *); +int IN(char); +void Pass(char *, char); +int Precede(char c, char ch); +int count(char *suffix); +int operate(char, int, int); diff --git "a/2017-1/sunnyCc/\351\223\276\345\274\217\351\230\237\345\210\227\347\232\204\345\256\236\347\216\260/link.PNG" "b/2017-1/sunnyCc/\351\223\276\345\274\217\351\230\237\345\210\227\347\232\204\345\256\236\347\216\260/link.PNG" new file mode 100644 index 00000000..3855ae5c Binary files /dev/null and "b/2017-1/sunnyCc/\351\223\276\345\274\217\351\230\237\345\210\227\347\232\204\345\256\236\347\216\260/link.PNG" differ diff --git "a/2017-1/sunnyCc/\351\223\276\345\274\217\351\230\237\345\210\227\347\232\204\345\256\236\347\216\260/link.c" "b/2017-1/sunnyCc/\351\223\276\345\274\217\351\230\237\345\210\227\347\232\204\345\256\236\347\216\260/link.c" new file mode 100644 index 00000000..36b366b5 --- /dev/null +++ "b/2017-1/sunnyCc/\351\223\276\345\274\217\351\230\237\345\210\227\347\232\204\345\256\236\347\216\260/link.c" @@ -0,0 +1,98 @@ +#include"link.h" +Status InitQueue(LinkQueue *Q) { + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!Q->front)exit(OVERFLOW); + Q->front->Next = NULL; + return OK; + +} +Status DestroyQueue(LinkQueue *Q) { + while (Q->front) { + Q->rear = Q->front->Next; + free(Q->front); + Q->front = Q->rear; + + } + return OK; + +} +Status ClearQueue(LinkQueue *Q) { + QNode *p, *next; + p = Q->front; + while (p != Q->rear) { + next = p->Next; + free(p); + p = next; + + } + + free(p); + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + //Q->front->Next = NULL; + return OK; + +} +Status QueueEmpty(LinkQueue Q) { + if (Q.front == Q.rear)return TRUE; + else return FALSE; + +} +int QueueLength(LinkQueue Q) { + QNode *p; + p = Q.front; + int k = 1; + while (p != Q.rear) { + p = p->Next; + k++; + + } + return k - 1; + +} +Status GetHead(LinkQueue Q, QElemType *e) { + if (QueueEmpty(Q) == FALSE) { + *e = Q.front->Next->data; + return OK; + + } + else { + return ERROR; + + } + +} +Status EnQueue(LinkQueue *Q, QElemType e) { + QueuePtr p; + p = (QueuePtr)malloc(sizeof(QNode)); + if (!p)exit(OVERFLOW); + p->data = e; + p->Next = NULL; + Q->rear->Next = p; + Q->rear = p; + return OK; + +} +Status DeQueue(LinkQueue *Q, QElemType *e) { + if (Q->front == Q->rear)return ERROR; + QueuePtr p; + p = Q->front->Next; + *e = p->data; + Q->front->Next = p->Next; + if (Q->rear == p)Q->rear = Q->front; + free(p); + return OK; + +} + +Status QueueTraverse(LinkQueue Q) { + QNode *p; + p = Q.front->Next; + while (p != Q.rear) { + printf("%d ", p->data); + p = p->Next; + + } + printf("%d ", p->data); + return OK; + +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\351\223\276\345\274\217\351\230\237\345\210\227\347\232\204\345\256\236\347\216\260/link.h" "b/2017-1/sunnyCc/\351\223\276\345\274\217\351\230\237\345\210\227\347\232\204\345\256\236\347\216\260/link.h" new file mode 100644 index 00000000..b8eb36c7 --- /dev/null +++ "b/2017-1/sunnyCc/\351\223\276\345\274\217\351\230\237\345\210\227\347\232\204\345\256\236\347\216\260/link.h" @@ -0,0 +1,32 @@ +#pragma once +#include +#include +#include +typedef int QElemType; +typedef enum { + FALSE, + OK, + OVERFLOW, + TRUE, + ERROR, + +}Status; +typedef struct QNode { + QElemType data; + struct QNode *Next; + +}QNode, *QueuePtr; +typedef struct { + QueuePtr front; + QueuePtr rear; + +}LinkQueue; +Status InitQueue(LinkQueue *Q); +Status DestroyQueue(LinkQueue *Q); +Status ClearQueue(LinkQueue *Q); +Status QueueEmpty(LinkQueue Q); +int QueueLength(LinkQueue Q); +Status GetHead(LinkQueue Q, QElemType *e); +Status EnQueue(LinkQueue *Q, QElemType e); +Status DeQueue(LinkQueue *Q, QElemType *e); +Status QueueTraverse(LinkQueue Q); \ No newline at end of file diff --git "a/2017-1/sunnyCc/\351\223\276\345\274\217\351\230\237\345\210\227\347\232\204\345\256\236\347\216\260/text.c" "b/2017-1/sunnyCc/\351\223\276\345\274\217\351\230\237\345\210\227\347\232\204\345\256\236\347\216\260/text.c" new file mode 100644 index 00000000..a95836cb --- /dev/null +++ "b/2017-1/sunnyCc/\351\223\276\345\274\217\351\230\237\345\210\227\347\232\204\345\256\236\347\216\260/text.c" @@ -0,0 +1,107 @@ +#include "link.h" +#include +#include +#include + +int main() +{ + //int choice, data, d = 0; + LinkQueue Q, q; + int a[10], b[10]; + int i; + int d = 0, n; + if (InitQueue(&Q) == 1) + { + printf("Succeed in initializing the link queue Q\n"); + } + else + { + printf("Fail to initializing the link queue Q\n"); + } + + for (int i = 0; i < 10; i++) + { + a[i] = rand() % 100 + 1; + + } + for (int i = 0; i < 10; i++) + { + EnQueue(&Q, a[i]); + } + printf("Traversing Q:"); + QueueTraverse(Q); + + + + + printf("\nThe length of Q is %d\n", QueueLength(Q)); + GetHead(Q, &d); + printf("Fetching the Q's head number:%d \n", d); + + n = rand() % 10 + 1; + printf("\nSelecting %d (0 + +void CreateList_L(LinkList *L,int n) +{ + int i; + LinkList p; + int b = 20; + + //头节点L + (*L)=(LinkList)malloc(sizeof(LNode)); + (*L)->next=NULL; + + + for(i=n;i>0;--i) + { + //生成新节点p + p = (LinkList)malloc(sizeof(LNode)); + //随机产生测试数据 + b -= rand() % 5+1; + //进行一系列指针操作 + p->data=b; + p->next = (*L)->next; + (*L)->next = p; + } +};//end CraeteList_L + +void MergeList_L(LinkList La, LinkList Lb, LinkList *Lc) +{ + + LNode *pa = La->next, *pb = Lb->next, *pc; + (*Lc)= pc = La; + + + while (pa&&pb){ + + if (pa->data <= pb->data){ + pc->next = pa; pc = pa; pa = pa->next; + } + else{ + pc->next = pb; pc = pb; pb = pb->next; + } + + } + pc->next = pa ? pa : pb; + //释放LB + if ( NULL != Lb ) + { + free(Lb); + Lb=NULL; + } + +} +void TraverseList_L(const LinkList L ){ + LNode *p = L->next; + while (p){ + printf("%d ",p->data); + p = p->next; + } +} diff --git "a/2017-1/sunnyCc/\351\223\276\350\241\250\345\220\210\345\271\266/happy.h" "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\220\210\345\271\266/happy.h" new file mode 100644 index 00000000..2ea3f4b6 --- /dev/null +++ "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\220\210\345\271\266/happy.h" @@ -0,0 +1,11 @@ +#include + +typedef struct LNode{ + int data; + struct LNode* next; +}LNode,*LinkList; + + +void CreateList_L(LinkList *L,int n); +void MergeList_L(LinkList La, LinkList Lb, LinkList *Lc); +void TraverseList_L(const LinkList L ); diff --git "a/2017-1/sunnyCc/\351\223\276\350\241\250\345\220\210\345\271\266/\351\223\276\350\241\250\345\220\210\345\271\266.PNG" "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\220\210\345\271\266/\351\223\276\350\241\250\345\220\210\345\271\266.PNG" new file mode 100644 index 00000000..c18713e1 Binary files /dev/null and "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\220\210\345\271\266/\351\223\276\350\241\250\345\220\210\345\271\266.PNG" differ diff --git "a/2017-1/sunnyCc/\351\223\276\350\241\250\345\220\210\345\271\266/\351\223\276\350\241\250\345\220\210\345\271\266.c" "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\220\210\345\271\266/\351\223\276\350\241\250\345\220\210\345\271\266.c" new file mode 100644 index 00000000..f250939f --- /dev/null +++ "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\220\210\345\271\266/\351\223\276\350\241\250\345\220\210\345\271\266.c" @@ -0,0 +1,36 @@ +#include "happy.h" +#include +#include +#include +//规定了链表LA,LB的基本长度 +#define LEN 2 +int main() +{ + + LinkList La, Lb, Lc; + + //使用随机函数生成链表测试数据 + srand(time(0)); + + //生成随机链表LA,LB + La = (LinkList)malloc(sizeof(LNode)); + CreateList_L(&La, LEN+rand()%5); + Lb = (LinkList)malloc(sizeof(LNode)); + CreateList_L(&Lb, LEN+rand()%5); + + //打印LA,LB元素值 + printf("\nLa:"); + TraverseList_L(La); + printf("\n"); + printf("Lb:"); + TraverseList_L(Lb); + printf("\n\n"); + + //合并为有序链表LC + MergeList_L(La, Lb,&Lc); + printf("Lc:"); + //打印合并LC + TraverseList_L(Lc); + printf("\n"); + return 0; +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\351\223\276\350\241\250\345\245\207\345\201\266\346\213\206\345\210\206/Split.PNG" "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\245\207\345\201\266\346\213\206\345\210\206/Split.PNG" new file mode 100644 index 00000000..1b9429e7 Binary files /dev/null and "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\245\207\345\201\266\346\213\206\345\210\206/Split.PNG" differ diff --git "a/2017-1/sunnyCc/\351\223\276\350\241\250\345\245\207\345\201\266\346\213\206\345\210\206/split.c" "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\245\207\345\201\266\346\213\206\345\210\206/split.c" new file mode 100644 index 00000000..88101739 --- /dev/null +++ "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\245\207\345\201\266\346\213\206\345\210\206/split.c" @@ -0,0 +1,91 @@ +#include "split.h" +void CreateList(LinkList *L, int n) +{ + LNode* p = *L; + int i; + + (*L) = (LinkList)malloc(sizeof(LNode)); + (*L)->next = NULL;//初始化链表 + + srand((unsigned int)time(NULL)); + printf("随机产生数据\n"); + for (i = 0; idata = rand() % 100 + 1; + printf("%d ", p->data); + p->next = (*L)->next; + (*L)->next = p; + } + printf("\n"); +} +void Split(LinkList list, LinkList list1, LinkList list2) +{ + + LNode *temp1, *temp, *p1, *p2; + int count = 0; + list1->data = 0; + list1->next = NULL; + list2->data = 0; + list2->next = NULL; + temp1 = temp = list->next; + p1 = list1; + p2 = list2; + + while (temp) + { + if (count % 2)//操作奇数项 + { + temp1 = temp->next; + if (list1->next == NULL) + { + temp->next = list1->next; + list1->next = temp; + } + else + { + temp->next = p1->next; + p1->next = temp; + } + p1 = temp; + list1->data++;//储存链表的长度加1 + count++; + } + else + { + temp1 = temp->next; + if (list2->next == NULL) + { + temp->next = list2->next; + list2->next = temp; + } + else + { + temp->next = p2->next; + p2->next = temp; + } + p2 = temp; + list2->data++; + count++; + } + temp = temp1; + } + p1->next = list1; + p2->next = list2; + free(list); +} +void Traverse(LinkList head) +{ + LNode *temp; + int count = head->data; + temp = head->next; + printf("该链表中的数据有;\n"); + while (count) + { + printf("%d ", temp->data); + temp = temp->next; + count--; + } + printf("\n"); + +} \ No newline at end of file diff --git "a/2017-1/sunnyCc/\351\223\276\350\241\250\345\245\207\345\201\266\346\213\206\345\210\206/split.h" "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\245\207\345\201\266\346\213\206\345\210\206/split.h" new file mode 100644 index 00000000..9af9735d --- /dev/null +++ "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\245\207\345\201\266\346\213\206\345\210\206/split.h" @@ -0,0 +1,10 @@ +#include +#include +typedef struct Node +{ + int data; + struct Node *next; +}LNode, *LinkList; +void CreateList(LinkList *L, int n); +void Split(LinkList list, LinkList list1, LinkList list2); +void Traverse(LinkList head); diff --git "a/2017-1/sunnyCc/\351\223\276\350\241\250\345\245\207\345\201\266\346\213\206\345\210\206/text.c" "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\245\207\345\201\266\346\213\206\345\210\206/text.c" new file mode 100644 index 00000000..35e8404a --- /dev/null +++ "b/2017-1/sunnyCc/\351\223\276\350\241\250\345\245\207\345\201\266\346\213\206\345\210\206/text.c" @@ -0,0 +1,26 @@ +//1.线性链表分解为两个带有头结点的循环链表, +//2.将两个循环链表的长度分别存放在各自头结点的数据域中。 +//3.线性表中序号为奇数的元素分解到第一个循环链表中, 序号为偶数的元素分解到第二个循环链表中 +//4.要求用最少的时间和最少的空间 + +//使用循环链表来表示list1,list2使得找寻链表的头尾项可以直接插入,降低了时间复杂度到O(1) +//而且分解过程只涉及一次循环,降低了空间复杂度到O(n) +#include "split.h" +int main() +{ + LinkList list, list1, list2; + int n; + srand((unsigned int)time(NULL)); + n = rand() % 10 + 10;//随机产生list链表长度 + printf("被拆分链表:\n元素个数为%d\n", n); + CreateList(&list, n);//创建并打印链表list,随机生成测试数据进入链表 + list1 = (LNode *)malloc(sizeof(LNode)); + list2 = (LNode *)malloc(sizeof(LNode)); + Split(list, list1, list2);//对链表进行拆分 + + printf("\n序列数为奇数(序列数从0开始计算)链表1:\n元素个数为%d\n", list1->data); + Traverse(list1); + printf("\n序列数为偶数(序列数从0开始计算)链表2:\n元素个数为%d\n", list2->data); + Traverse(list2); + return 0; +} \ No newline at end of file diff --git a/2017-1/tqh/01/Header.c b/2017-1/tqh/01/Header.c new file mode 100644 index 00000000..224e3bec --- /dev/null +++ b/2017-1/tqh/01/Header.c @@ -0,0 +1,118 @@ +#include +#include"Header.h" +#include +//随机创建链表 +void CreateList(LinkList *L,int n) +{ + LinkList p = NULL; + int i ; + *L = (LinkList)malloc(sizeof(LNode)); + (*L)->next = NULL; + srand((unsigned)time( NULL ) ); + for(i=0;idata);*/ + p->data = rand()%1024; + p->next = (*L)->next; + (*L)->next = p; + } +}//createlist + +//链表从小到大排序 +void SortList(LinkList L,int n) +{ + int i=0; + int j=0; + LinkList head; + LinkList p1; + LinkList p2; + + for(i=0;inext; + for( j=0;jnext; + if( p1->data > p2->data){ + Elemtype temp = p1->data; + p1->data = p2 -> data; + p2->data = temp; + } + head = head->next; + } + } +} + +//遍历链表 +void Traverse(LinkList L) +{ + LNode *p = L->next; + while(p){ + printf("%d ",p->data); + p = p->next; + } +} + +//链表合并成一个新链表 +void MergeList_L(LinkList La,LinkList Lb,LinkList *Lc) +{ + LinkList pa,pb,pc; + pa = La->next; + pb = Lb->next; + *Lc = pc = La; + while(pa&&pb){ + if(pa->data <= pb->data){ + pc->next = pa; + pc = pa; + pa = pa->next; + } + else{ + pc->next = pb; + pc = pb; + pb =pb ->next; + } + } + pc->next = pa? pa:pb; + free(Lb); +} + +//将主函数中的一些操作综合,简化主函数 +void ShowList(LinkList *L1,LinkList *L2) +{ + int i,j; + LinkList Lc; + *L1 =(LinkList) malloc(sizeof(LNode)); + *L2 =(LinkList) malloc(sizeof(LNode)); + + printf("L1中值得个数:"); + scanf("%d",&i); + + CreateList(L1,i); + printf("\n排序前遍历L1:\n"); + Traverse(*L1); + printf("\n"); + + SortList(*L1,i); + printf("\n排序后遍历L1:\n"); + Traverse(*L1); + printf("\n"); + + printf("\nL2中值得个数:"); + scanf("%d",&j); + + CreateList(L2,j); + printf("\n排序前遍历L2:\n"); + Traverse(*L2); + printf("\n"); + + SortList(*L2,j); + printf("\n排序后遍历L2:\n"); + Traverse(*L2); + printf("\n"); + + printf("\n合并后遍历:\n"); + MergeList_L(*L1,*L2,&Lc); + Traverse(Lc); +} + diff --git a/2017-1/tqh/01/Header.h b/2017-1/tqh/01/Header.h new file mode 100644 index 00000000..8c55ab0f --- /dev/null +++ b/2017-1/tqh/01/Header.h @@ -0,0 +1,20 @@ +#include +#include + +#define OK 0 +#define ERROR 1 +#define OVERFLOW 2 + +typedef int Elemtype; + +typedef struct LNode{ + int data; + struct LNode *next; +}LNode,*LinkList; +//创建结构体及结构体指针 + +void CreateList(LinkList* ,int ); +void SortList(LinkList,int); +void Traverse(LinkList ); +void MergeList_L(LinkList ,LinkList ,LinkList *); +void ShowList(LinkList *,LinkList *); diff --git a/2017-1/tqh/01/test.c b/2017-1/tqh/01/test.c new file mode 100644 index 00000000..6b032ffe --- /dev/null +++ b/2017-1/tqh/01/test.c @@ -0,0 +1,12 @@ +#include +#include"Header.h" +#include + +int main() +{ + int i,j; + LinkList L1,L2; + ShowList (&L1,&L2); + return OK; +} + diff --git a/2017-1/tqh/02/2-13.c b/2017-1/tqh/02/2-13.c new file mode 100644 index 00000000..dd6dcd3d --- /dev/null +++ b/2017-1/tqh/02/2-13.c @@ -0,0 +1,95 @@ +#include +#include +#include + +typedef struct LNode{ + int data; + struct LNode *next; +}LNode,*LinkList; + +void CreateList (LinkList *L,int n) +{ + LinkList p = (LinkList) malloc(sizeof(LNode)); + LinkList temp = p; + srand((unsigned)time( NULL ) ); + for(;n!=0;--n){ + p->next = (LinkList)malloc(sizeof(LNode)); + p->next->data = rand()%1024; + p=p->next; + } + p->next = NULL; + *L = temp->next; + free(temp); +}//createlist + +void Traverse1 (LinkList L) +{ + LNode *p = L; + while(p) { + printf("%d ",p->data); + p = p->next; + } + printf("\n"); +} +void Traverse2 (LinkList L) +{ + LNode *p = L->next; + do{ + printf("%d ",p->data); + p = p->next; + }while(p != L->next); + printf("\n"); +} +void SplitList(LinkList L,LinkList*pL1,LinkList*pL2) +{ + *pL1=(LNode*)malloc(sizeof(LNode)); + (*pL1)->data = 0; + (*pL1)->next = NULL; + *pL2=(LNode*)malloc(sizeof(LNode)); + (*pL2)->data = 0; + (*pL2)->next = NULL; + LinkList p,rear1 = *pL1,rear2=*pL2; + int count=0; + for(p = L;p != NULL;p = p->next) + { + ++count; + if(count%2) + { + (*pL1)->data++; + rear1->next = p; + rear1 = p; + } + else + { + (*pL2)->data++; + rear2->next = p; + rear2 = p; + } + } + rear1->next = (*pL1)->next; + rear2->next = (*pL2)->next; +} +int main() +{ + LinkList L,L1 = NULL,L2 = NULL; + int n; + + srand((unsigned)time( NULL ) ); + n = rand()%9+2; + printf("数据的总个数为: %d\n",n); + /*scanf("%d",&n);*/ + CreateList(&L,n); + printf("待拆分的链表:\n"); + Traverse1(L); + + SplitList(L,&L1,&L2); + printf("\n遍历链表L1:\n"); + Traverse2(L1); + printf("\n遍历链表L2:\n"); + Traverse2(L2); + return 0; +} +//程序又算法2-12修改得到,新增核心函数SplitList +//整个程序中,时间复杂度应在SplitList函数中体现,再拆分链表时有一个一重for循环的遍历, +//故时间复杂度应为o(n) +//空间复杂度在核心函数中用p1,p2,rear1,rear2辅助创建链表。 \ No newline at end of file diff --git a/2017-1/tqh/03/3-2-1.c b/2017-1/tqh/03/3-2-1.c new file mode 100644 index 00000000..24fa65b4 --- /dev/null +++ b/2017-1/tqh/03/3-2-1.c @@ -0,0 +1,112 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef int SElemType; +typedef struct _SqStack { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; +typedef enum { + false , + true +}bool; + +typedef enum { + OK, + ERROR, + OVERFLOW +} Status; + +Status InitStack (SqStack *S); +Status conversion (SqStack *, int , int); +Status Push (SqStack *S, SElemType e); +Status Pop (SqStack *S,SElemType *e); +bool IsStackEmpty (SqStack *S); + +Status InitStack (SqStack *S) +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE * + sizeof(SElemType)); + if(!S->base) { + return OVERFLOW; + } + + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + + return OK; +}//初始化栈 + +Status Push (SqStack *S,SElemType e) { + if((S->top-S->base)>=S->stacksize) { + return ERROR; + } + *S->top++ = e; + return OK; +}//入栈 + +Status Pop (SqStack *S,SElemType *e) +{ + if(IsStackEmpty(S)) + { + return ERROR; + } + + *e = *--S->top; + return OK; +}//出栈 + +bool IsStackEmpty (SqStack *S) +{ + if(S) { + return S->base ==S->top; + } + return true; +}//判断栈是否为空 + +Status conversion (SqStack *S, int input, int d) +{ + SElemType e; + if(d > 10) { + return ERROR; + } + + while (input) + { + Push(S,input % d); + input = input / d; + } + while (!IsStackEmpty(S)) + { + Pop(S,&e); + printf("%d", e); + } + printf("\n\n"); + return OK; +}//数制转换的实现 + +int main(int argc,char* argv[]) +{ + SqStack S; + int input=1348; + int d,i; + srand(time(NULL)); + //随机进行五次进制转换 + for(i = 0;i < 5;i ++) { + d = rand ()%8+2;//d的取值应为小于10大于1的整数 + input = rand()%1024; + + printf("随机十进制数为:%d\n",input); + printf("需要转化为%d进制\n",d); + InitStack(&S); + printf("转化后为:"); + conversion (&S, input, d); + } + return 0; +} + diff --git a/2017-1/tqh/03/3-2-2.c b/2017-1/tqh/03/3-2-2.c new file mode 100644 index 00000000..23c7e7a4 --- /dev/null +++ b/2017-1/tqh/03/3-2-2.c @@ -0,0 +1,118 @@ +#include + +#define M 1000 + +typedef int Elemtype; +typedef enum{ + ERROR, + OK +}Status; + + typedef struct Stack { + char string[M]; + Elemtype top; + }Stack; + //定义stack结构体类型 + void InitStack(Stack *s) + { + s->top = -1; + } + //结构体初始化 + Status PushStack(Stack *s, char ch) + { + if(s->top == M-1) { + return ERROR; + } + s->top++; + s->string[s->top] = ch; + return OK; + } + //入栈 + Status PopStack(Stack *s, char *ch) + { + if(s->top == -1) { + return ERROR; + } + *ch = s->string[s->top]; + s->top--; + return OK; + } + //出栈 + + Status GetTop(Stack *s, char *ch) + { + if(s->top == -1) { + return ERROR; + } + *ch = s->string[s->top]; + return OK; + } + //得到指定元素的值 + Status IsEmpty(Stack *s) + { + if(s->top == -1) + { + return OK; + } + else { + return ERROR; + } + } + //判断是否栈为空 +Elemtype MatchBracket(char ch1, char ch2) +{ + if(ch1 == '(' && ch2 == ')') { + return 1; + } + if(ch1 == '[' && ch2 == ']') { + return 1; + } + if(ch1 == '{' && ch2 == '}') { + return 1; + } + return 0; +} +//判断括号是否匹配 +int main() +{ + Stack s; + char str[M] = {0}, ch; + int i; + + InitStack(&s); + fprintf(stdout, "Input brackets:"); + fscanf(stdin, "%s", str); + for(i = 0; str[i] != 0; i++) { + switch(str[i]) { + case '(': + case '[': + case '{': + PushStack(&s, str[i]); + break; + case ')': + case ']': + case '}': + if(IsEmpty(&s)) { + printf("right bracket spilth.\n"); + return 0; + } else { + GetTop(&s, &ch); + if(MatchBracket(ch , str[i])) { + PopStack(&s, &ch); + } else{ + printf("left and right brackets are different.\n"); + return 0; + } + } + break; + default: + printf("Input error\n"); + } + } + if(IsEmpty(&s)) { + printf("Left and right brackets are matched.\n"); + } else { + printf("Left bracket spilth..\n"); + } + return 0; +} \ No newline at end of file diff --git a/2017-1/tqh/03/3-2-3.c b/2017-1/tqh/03/3-2-3.c new file mode 100644 index 00000000..96f71ba0 --- /dev/null +++ b/2017-1/tqh/03/3-2-3.c @@ -0,0 +1,108 @@ +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef char SElemType; +typedef enum{ + ERROR, + OK, + OVERFLOW +}Status; +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; +//定义stack结构体类型 + +Status InitStack(SqStack *S); +Status Push(SqStack *S,SElemType e); +Status Pop(SqStack *S,SElemType *e); +Status ClearStack(SqStack *S); +Status DestroyStack(SqStack *S) ; +void LineEdit(SqStack *S); + +Status InitStack(SqStack *S) { + S->base = (SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType)); + if(!S->base) { + return OVERFLOW; + } + + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + + return OK; +}//初始化栈 +Status Push(SqStack *S,SElemType e) { + if((S->top-S->base)>=S->stacksize) { + S->base = (SElemType*)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); + if(!S->base) { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +}//入栈 +Status Pop(SqStack *S,SElemType *e) { + if(S->top == S->base) + return ERROR; + *e = *--S->top; + return OK; +}//出栈 +Status ClearStack(SqStack *S) { + S->top = S->base; + return OK; +} +//清空栈 +Status DestroyStack(SqStack *S) { + S->top = S->base; + free(S->base); + S->top = NULL; + S->base = NULL; + return OK; +}//销毁栈 +void LineEdit(SqStack *S) +{ + //利用字符栈s,从终端接收一行并传送至调用过程的数据区 + char ch,c,*b; + InitStack(S); + ch = getchar(); + while( ch != EOF) + { + while(ch != EOF&&ch!='\n') + { + switch(ch) + { + case '#':Pop(S,&c); + break; + case '@':ClearStack(S); + break; + default : + Push(S, ch); + } + ch = getchar(); + } + b = S->base; + while(b != S->top) + { + printf("%c",*b); + b++; + } + ClearStack(S); + if( ch!=EOF) + ch = getchar(); + } + DestroyStack(S); +} +int main() +{ + SqStack s; + printf("input:\n"); + LineEdit(&s); + DestroyStack(&s); + return 0; +} diff --git a/2017-1/tqh/04/He.cpp b/2017-1/tqh/04/He.cpp new file mode 100644 index 00000000..c11586c7 --- /dev/null +++ b/2017-1/tqh/04/He.cpp @@ -0,0 +1,248 @@ +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef char SElemType; + +typedef enum{ + ERROR, + OK, + OVERFLOW +}Status; + typedef struct { + int top; + int stacksize; + SElemType stack[STACK_INIT_SIZE]; + double data[STACK_INIT_SIZE]; + }SqStack; + //定义stack结构体类型 + Status InitStack(SqStack *S); + Status Push(SqStack *S,SElemType e); + Status Pop(SqStack *S,SElemType *e); + Status ClearStack(SqStack *S); + Status DestroyStack(SqStack *S) ; + Status GetTop(SqStack *S,SElemType ); + Status EmptyStack(SqStack *S); + int GetLength(SqStack S); + void TranslateExpress(char s1[],char s2[]); + double ComputeExpress(char a[]); + +Status InitStack(SqStack *S)//将栈S初始化为空栈 +{ + int i; + S->top = 0; + for (i = 0;i < STACK_INIT_SIZE;i++) + { + S->data[i] = 0; + } + return OK; +} +Status EmptyStack(SqStack *S)//判断栈是否为空,栈为空返回1,否则返回0 +{ + if(0 == S->top) + { + return OK; + } + else + { + return ERROR; + } +} +Status GetTop(SqStack S,SElemType *e)//取栈顶元素,将栈顶元素值返回给e,并返回1表示成功,返回0表示失败 +{ + if(S.top <= 0) + { + printf("栈已经空!\n"); + return ERROR; + } + else + { + *e = S.stack[S.top-1];//取栈顶元素 + return OK; + } +} +Status Push(SqStack *S,SElemType e)//进栈操作 +//将元素e进栈,元素进栈成功返回1,否则返回0 +{ + if(S->top >= STACK_INIT_SIZE-1) + { + printf("栈已满,不能入栈!"); + return ERROR; + } + else + { + S->stack[S->top] = e; + S->top++; + return OK; + } +} +Status Pop(SqStack *S,SElemType *e)//出栈操作 +{ + if(S->top <= 0) + { + printf("栈已经没有元素,不能出栈!\n"); + return ERROR; + } + else + { + S->top--; + *e = S->stack[S->top]; + return OK; + } +} +int GetLength(SqStack S)//返回栈长度 +{ + return S.top; +} +Status ClearStack(SqStack *S)//清空栈 +{ + S->top = 0; + return OK; +} +void TranslateExpress(SElemType s1[],SElemType s2[]) +{ + SqStack s; + char ch; + SElemType e; + int i = 0,j = 0; + InitStack(&s); + ch = s1[i]; + i++; + while( ch != '\0') + { + switch(ch){ + case '(' : + Push(&s,ch); + break; + case ')' : + while(GetTop(s,&e) && e != '(') + { + Pop(&s,&e); + s2[j] = e; + j++; + } + Pop(&s,&e);//将左括号出栈 + break; + case '+' : + case '-' : + while (GetTop(s,&e) && e !='(') + { + Pop(&s,&e); + s2[j]= e; + j++; + } + Push(&s,ch);//当前运算符进栈 + break; + case '*' : + case '/' : + while(!EmptyStack(&s) && GetTop(s,&e) && e == '/' || e == '*') + { + Pop(&s,&e); + s2[j]= e; + j++; + } + Push(&s,ch);//当前运算符进栈 + break; + default : + while(ch >= '0'&&ch <= '9') + { + s2[j] = ch; + j++; + ch = s1[i]; + i++; + } + i--; + s2[j] = ' '; + j++; + } + ch = s1[i]; + i++; + //读入下一个字符,准备处理 + } + while(!EmptyStack(&s))//将栈中所有剩余的运算符出栈,送入数组s2中 + { + Pop(&s,&e); + s2[j]= e; + j++; + } + s2[j]= '\0'; +} +double ComputeExpress(SElemType a[]) +{ + double val,num1,num2,result; + int i = 0; + SqStack s; + s.top = -1; + while (a[i] != '\0') + { + if (a[i] !=' ' && a[i] >='0' &&a[i] <='9') + { + val = 0; + while (a[i] != ' ') + { + val = val*10 + a[i]- '0'; + i ++; + } + s.top++; + s.data[s.top] = val; + } + else + { + switch(a[i]) + { + case '+': + num1 = s.data[s.top]; + s.top--; + num2 = s.data[s.top]; + s.top--; + result = num1+num2; + s.top++; + s.data[s.top] = result; + break; + case '-': + num1 = s.data[s.top]; + s.top--; + num2 = s.data[s.top]; + s.top--; + result = num2-num1; + s.top++; + s.data[s.top] = result; + break; + case '*': + num1 = s.data[s.top]; + s.top--; + num2 = s.data[s.top]; + s.top--; + result = num1*num2; + s.top++; + s.data[s.top] = result; + break; + case '/': + num1 = s.data[s.top]; + s.top--; + num2 = s.data[s.top]; + s.top--; + result = num2/num1; + s.top++; + s.data[s.top] = result; + break; + } + i++; + } + } + return result; +} +int main() +{ + SElemType a[STACK_INIT_SIZE],b[STACK_INIT_SIZE]; + double f; + printf("请输入一个表达式:"); + gets(a); + TranslateExpress(a,b); + printf("后缀表达式是:%s\n",b); + f = ComputeExpress(b); + printf("计算结果是:%.2lf\n",f); + return 0; +} diff --git a/2017-1/tqh/04/Li.cpp b/2017-1/tqh/04/Li.cpp new file mode 100644 index 00000000..9a7b1426 --- /dev/null +++ b/2017-1/tqh/04/Li.cpp @@ -0,0 +1,193 @@ +#include +#include + +typedef int QElemType; +typedef struct QNode +{ + QElemType data; + struct QNode *next; +}QNode,*QueuePtr; +typedef struct +{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; +typedef enum{ + OK, + ERROR, + OVERFLOW +}Status; + +Status InitQueue (LinkQueue*Q); +Status DestroyQueue (LinkQueue Q); +Status EnQueue (LinkQueue *Q,QElemType e); +Status DeQueue (LinkQueue Q,QElemType *e); +Status CreateQueue(LinkQueue*L,int n); +Status Travel(LinkQueue Q); +Status backTravel(QueuePtr,QueuePtr); +Status ClearQueue(LinkQueue Q); +Status GetHead (LinkQueue Q,QElemType e); +int QueueEmpty(QueuePtr,QueuePtr); +int GetLength (LinkQueue Q); + + +Status InitQueue (LinkQueue*Q) +{ + Q->front = NULL; + Q->rear = NULL; + return OK; +} +Status DestroyQueue (LinkQueue*Q) +{ + QueuePtr temp; + while(Q->front) + { + temp = Q->front ->next; + free(Q->front); + Q->front = temp; + } + return OK; +} +Status EnQueue (LinkQueue *Q,QElemType e) +{ + QueuePtr p = (QueuePtr) malloc (sizeof(QNode)); + if (!p) + return OVERFLOW; + p->data = e; + p ->next = NULL; + if(Q->front!=NULL) + { + Q->rear->next = p; + } + else + { + Q->front=p; + } + Q->rear = p; + return OK; +} +Status DeQueue (LinkQueue*Q,QElemType *e) +{ + QueuePtr p; + if (Q->front == NULL) + { + return ERROR; + } + p= Q->front; + *e = p->data; + Q->front = p->next; + free(p); + return OK; +} +Status Travel(LinkQueue Q) +{ + QueuePtr p = Q.front; + printf("\n正向遍历:\n"); + while(p != NULL) + { + printf("%d ",p->data); + p = p->next; + } + printf("\n"); + return OK; +} +//已知队尾元素,反向遍历到队首 +Status backTravel(QueuePtr start,QueuePtr end) +{ + if(start!=end) + { + backTravel(start->next,end); + } + printf("%d ",start->data); + return OK; +} +Status CreateQueue(LinkQueue*L,int n) +{ + printf("\n输入数据:"); + QueuePtr p = NULL; + int i ; + L->front = NULL; + for(i=0;idata); + if(i==0)L->rear=p; + p->next = L->front; + L->front = p; + } + return OK; +} +int QueueEmpty(QueuePtr start,QueuePtr end) +{ + if(start ==end) + { + return 1; + } + else + { + return 0; + } +} +int GetLength (LinkQueue Q) +{ + QueuePtr p; + int n=0; + p = Q.front; + while(p != NULL) + { + n++; + p = p->next; + } + return n; +} +Status GetHead (LinkQueue Q,QElemType e) +{ + if(!QueueEmpty) + { + e = Q.front->data; + return OK; + } + else + { + return ERROR; + } +} +Status ClearQueue(LinkQueue *Q) +{ + QueuePtr p,q; + Q->rear = Q->front; + p = Q->front; + Q->front = NULL; + while(p) + { + q = p; + p=p->next; + free(q); + } + return OK; +} +int main () +{ + LinkQueue Q={0,0}; + int n; + QElemType e=7,s; + InitQueue (&Q); + printf("请输入数据个数:"); + scanf("%d",&n); + + CreateQueue(&Q,n); + printf("\n输出长度:%d\n", GetLength(Q)); + + Travel(Q); + printf("反向遍历:\n"); + backTravel(Q.front,Q.rear); + + printf("\n"); + EnQueue (&Q,e); + + Travel(Q); + printf("反向遍历:\n"); + backTravel(Q.front,Q.rear); + + DestroyQueue(&Q); + return 0; +} \ No newline at end of file diff --git a/2017-1/tqh/05/Traverse.c b/2017-1/tqh/05/Traverse.c new file mode 100644 index 00000000..6c88928d --- /dev/null +++ b/2017-1/tqh/05/Traverse.c @@ -0,0 +1,90 @@ +#include +#include +typedef char TElemType; +typedef struct BiTNode { + TElemType data; + BiTNode *lchild,*rchild; +}BiTNode,*BiTree; +typedef enum { + ERROR, + OK, + OVERFLOW +}Status; + +Status CreateBitree (BiTree *T,int i); +Status PostOrderTraverse(BiTree T); +Status InOrderTraverse(BiTree T); +Status PostOrderTraverse(BiTree T); + +char*s[]={"ABDG###EH##I#K##C#F##","ABD#F##CE###"};int si[]={0,0}; + +Status CreateBitree (BiTree *T,int i) +{ + char ch; + //scanf("%c",&ch); + ch=s[i][si[i]++]; + if(ch == '#') + *T =NULL; + else{ + if(! (*T = (BiTNode *)malloc(sizeof(BiTNode)) )) + exit(OVERFLOW); + (*T)->data = ch; + CreateBitree(&(*T)->lchild,i); + CreateBitree(&(*T) ->rchild,i); + } + return OK; +} +Status PreOrderTraverse(BiTree T) +{ + if(T){ + printf("%c",T->data); + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + } + return OK; +} +Status InOrderTraverse(BiTree T) +{ + if(T){ + PostOrderTraverse(T->lchild); + printf("%c",T->data); + PostOrderTraverse(T->rchild); + } + return OK; +} +Status PostOrderTraverse(BiTree T) +{ + if(T){ + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c",T->data); + } + return OK; +} +int main() +{ + BiTree tree[]={NULL,NULL}; + int i; + for(i=0;i!=2;++i) + { + printf("%s\n",s[i]); + + CreateBitree(&tree[i],i); + + printf("%-6s","pre:"); + PreOrderTraverse(tree[i]); + printf("\n"); + + printf("%-6s","in:"); + InOrderTraverse(tree[i]); + printf("\n"); + + printf("%-6s","post:"); + PostOrderTraverse(tree[i]); + printf("\n"); + + printf("\n"); + } + + return 0; +} diff --git a/2017-1/tqh/06/bitree.c b/2017-1/tqh/06/bitree.c new file mode 100644 index 00000000..dfa67576 --- /dev/null +++ b/2017-1/tqh/06/bitree.c @@ -0,0 +1,116 @@ +#include +#include +typedef char TElemType; +typedef struct BiTNode { + TElemType data; + BiTNode *lchild,*rchild; +}BiTNode,*BiTree; +typedef enum { + ERROR, + OK, + OVERFLOW +}Status; +char*s[2]={"ABDG###EH##I#K##C#F##","ABD#F##CE###"}; +int index2[2]={0}; + +Status CreateBitree (BiTree *T,int index) +{ + //char ch; + //scanf("%c",&ch); + if(s[index][index2[index]] == '#') + { + *T =NULL; + ++index2[index]; + } + else + { + if((*T = (BiTNode*)malloc(sizeof(BiTNode)))==NULL) + { + exit(OVERFLOW); + } + (*T)->data = s[index][index2[index]]; + ++index2[index]; + CreateBitree(&(*T)->lchild,index); + CreateBitree(&(*T) ->rchild,index); + } + return OK; +} +Status PostOrderTraverse(BiTree T,int*leaf_count,int*nonleaf_count) +{ + if(T!=NULL) + { + if(T->lchild==NULL&&T->rchild==NULL) + { + ++*leaf_count; + } + else + { + ++*nonleaf_count; + } + PostOrderTraverse(T->lchild,leaf_count,nonleaf_count); + PostOrderTraverse(T->rchild,leaf_count,nonleaf_count); + printf("%c",T->data); + } + return OK; +} +void FindDepthest(BiTree T,int layer,int*depth) +{ + if(layer>*depth) + { + *depth=layer; + } + if(T->lchild!=NULL) + { + FindDepthest(T->lchild,layer+1,depth); + } + if(T->rchild!=NULL) + { + FindDepthest(T->rchild,layer+1,depth); + } +} +void FindBitreeWidth(BiTree T,int layer,int*width[]) +{ + if(T->lchild!=NULL) + { + ++(*width)[layer-1]; + FindBitreeWidth(T->lchild,layer+1,width); + } + if(T->rchild!=NULL) + { + ++(*width)[layer-1]; + FindBitreeWidth(T->rchild,layer+1,width); + } +} +int main() +{ + int i; + for(i=0;i!=2;++i) + { + BiTree tree=NULL; + int tDepth = 1; + CreateBitree(&tree,i); + + int temp1=0,temp2=0; + PostOrderTraverse(tree,&temp1,&temp2); + printf("\n"); + printf("叶子节点的个数:%d\n非叶子节点个数:%d\n",temp1,temp2); + + temp1=0; + FindDepthest(tree,1,&temp1); + printf("输出树的高度:%d",temp1); + printf("\n"); + + int*temp3=(int*)calloc(temp1,sizeof(int)); + FindBitreeWidth(tree,1,&temp3); + int j,max=0; + for(j=0;j!=temp1;++j) + { + if(temp3[j]>max) + { + max=temp3[j]; + } + } + printf("输出最大宽度:%d\n",max); + } + return 0; +} \ No newline at end of file diff --git a/2017-1/tqh/07/New.cpp b/2017-1/tqh/07/New.cpp new file mode 100644 index 00000000..56619170 --- /dev/null +++ b/2017-1/tqh/07/New.cpp @@ -0,0 +1,227 @@ +#include +#include +#define MAX_VEX 20 +#define Queue_Size 100 + int visited[MAX_VEX];//访问标志数组visited + typedef int ElemType; + typedef int VerType; + typedef enum{ + OK, + ERROR, + OVERFLOW + }Status; + + typedef struct q_node{//队列中的每个节点 + ElemType data; + struct q_node *previous; + struct q_node *next; + }Qnode,*Pq_node; + + typedef struct link_queue{//队列 + Pq_node front; + Pq_node rear; + }LinkQueue; + + typedef struct ArcCell{//定义弧 + ElemType eadj;//用"1"表示相邻,"0"表示不相邻 + }AdjMatrix[MAX_VEX][MAX_VEX]; + + typedef struct graph{//定义图 + AdjMatrix arcs; + int arcs_num;//弧数 + int vers_num;//顶点数目 + }Graph; + + /*对于队列基本操作实现*/ + Status InitQueue(LinkQueue *q);//初始化队列 + Status EnQueue(LinkQueue*q, ElemType e);//入队列 + Status DeQueue(LinkQueue*q,ElemType *e);//出队列 + bool EmptyQueue(LinkQueue q);//判断队列是否为空 + /*对于队列基本操作实现*/ + + /*对于图的操作*/ + Status Add(Graph*g,int i,int j);//为邻接矩阵赋值 + Status CreatGraph(Graph*g);//构建一个图 + VerType ToFirstAdjVex(Graph g,int num);//返回第一个邻接顶点 + VerType ToNextAdjVex(Graph g,int i,int j);//返回下一个邻接 + void BFSTraverse(Graph g,int a,int b);//按广度优先非递归遍历图g + Status PrintFoot(LinkQueue q,VerType start);//输出两点间的最短路径 + /*对于图的操作*/ + + + Status InitQueue(LinkQueue *q){//初始化队列 + q->front=(Pq_node)malloc(Queue_Size*sizeof(Qnode)); + q->rear=(Pq_node)malloc(Queue_Size*sizeof(Qnode)); + q->front=q->rear; + if(!(q->front)){ + return ERROR; + } + q->front->next=q->rear->next=NULL;//初始化队列得头和尾 + return OK; + } + + Status EnQueue(LinkQueue*q, ElemType e) {//入队列 + Pq_node ptr; + ptr=(Pq_node)malloc(sizeof(Qnode)); + if(!ptr) + { + return ERROR; + } + ptr->data=e; + ptr->next=NULL; + ptr->previous=q->front; + q->rear->next=ptr; + q->rear=ptr; + return OK; + } + + Status DeQueue(LinkQueue *q,ElemType *e){//出队列 + if(EmptyQueue(*q)){ + return ERROR; + } + q->front=q->front->next;//队列非空才可以进行出队列操作 + *e=q->front->data; + return OK; + } + + bool EmptyQueue(LinkQueue q){//判断队列是否为空 + if(q.front==q.rear){ + return true; + } + + return false; + + } + Status Add(Graph*g,int i,int j) {//为邻接矩阵赋值 + if(i>=MAX_VEX||j>=MAX_VEX) + { + return ERROR; + } + g->arcs[i][j].eadj=1; + g->arcs[j][i].eadj=1; + + return OK; + } + Status CreatGraph(Graph*g) {//构建一个图 + g->vers_num=9;//初始化图的弧数 + g->arcs_num=12;//初始化图的边数 + int i=0; + int j=0; + for(i=0;ivers_num;i++) + { + for(j=0;jvers_num;j++) + { + g->arcs[i][j].eadj=0;//初始化邻接矩阵,"0"表示不相邻 + } + } + 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; + + } + + VerType ToFirstAdjVex(Graph g,int num) {//返回第一个邻接顶点 + int i; + for(i=0;i=0;j=ToNextAdjVex(g,e,j)) + { + if(j==b){ + EnQueue(&q,j); + PrintFoot(q,a); + flag=true; + break; + } + if(!visited[j]){//j为e尚未访问的邻接顶点 + EnQueue(&q,j); + visited[j]=true; + } + + } + if(flag){ + break; + } + + } + + } + Status PrintFoot(LinkQueue q,VerType start) {//输出两点间的最短路径 + int track[MAX_VEX];//该数组用来存储路径 + Pq_node ptr; + int i=0,j; + + ptr=q.rear; + for(i=0;idata; + ptr=ptr->previous; + for(i=1;ptr->data!=start;i++){ + track[i]=ptr->data; + ptr=ptr->previous; + } + track[i]=ptr->data; + for( j=i;j>=0;j--)//倒序输出路径 + {if(track[j]>=0) + { + printf("%d ",track[j]+1); + } + + } + return OK; + } + int main() + { + Graph graph; + CreatGraph(&graph); + int i=0,j=0; + printf("图中任意两点间的最短距离为:\n"); + for(i=0;i<9;i++){ + for(j=0;j<9;j++) + { + if(j!=i) + { + printf("%d<->%d:",i+1,j+1); + BFSTraverse(graph,i,j); + printf("\n"); + } + } + } + return 0; + } \ No newline at end of file diff --git a/2017-1/tqh/08/BSTOutput.txt b/2017-1/tqh/08/BSTOutput.txt new file mode 100644 index 00000000..08eb1402 --- /dev/null +++ b/2017-1/tqh/08/BSTOutput.txt @@ -0,0 +1,6 @@ +8 3 1 6 4 5 7 10 14 19 22 30 +8 3 1 6 4 5 7 10 14 13 19 22 30 +7 3 1 6 4 5 10 14 13 19 22 30 +7 3 1 6 4 10 14 13 19 22 30 +7 3 1 6 4 10 14 13 19 22 20 30 +7 3 1 4 10 14 13 19 22 20 30 \ No newline at end of file diff --git a/2017-1/tqh/08/testz.cpp b/2017-1/tqh/08/testz.cpp new file mode 100644 index 00000000..fbd6a326 --- /dev/null +++ b/2017-1/tqh/08/testz.cpp @@ -0,0 +1,166 @@ +#include +#include +#define MAX 20 +int num[] = {8,10,14,3,1,6,4,7,5,19,22,30}; +int searchnum[] = {13,8,5,20,6}; +int flag; +enum +{ + FALSE, + TRUE +}; +typedef struct BiTNode { // 结点结构 + int data; + struct BiTNode *lchild, *rchild; // 左右孩子指针 +} BiTNode,*BiTree; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +Status PreOrderTraverse(BiTree T); +int SearchBST( BiTree T, int key, BiTree f, BiTree *p ) ; +int InsertBST( BiTree *T, int key ); +Status Delete(BiTree *p); +int DeleteBST(BiTree *T, int kval); + +Status PreOrderTraverse(BiTree T) +{ + if(T){ + printf("%d ",T->data); + PreOrderTraverse(T->lchild); + PreOrderTraverse(T->rchild); + } + return OK; +} + +int SearchBST( BiTree T, int key, BiTree f, BiTree *p ) +{ + BiTree s; + if( !T ) + { + *p = f; + return FALSE; + } + else + { + while( T ) + { + if( key == T->data ) + { + *p = T; + return TRUE; + } + if( key > T->data ) + { + s = T; + T = T->rchild; + } + else + { + s = T; + T = T->lchild; + } + } + *p = s; + return FALSE; + } +} +int InsertBST( BiTree *T, int key ) +{ + /* 当二叉排序树T中不存在关键字等于key的数据元素时 */ + /* 插入key并返回TRUE,否则返回FALSE */ + /* 调用查找函数SearchBST,非递归 */ + BiTree p, s; + if( !SearchBST( *T, key, NULL, &p ) ) + { + s = (BiTree)malloc(sizeof(BiTNode)); + s->data = key; + s->lchild = s->rchild = NULL; + if( !p ) + *T = s; /* 插入s为根节点,此前树为空树 */ + else if( key > p->data ) + p->rchild = s; /* 插入s为右孩子 */ + else + p->lchild = s; /* 插入s为左孩子 */ + return TRUE; + } + return FALSE; +} +Status Delete(BiTree *p) { + // 从二叉排序树中删除结点 p, + // 并重接它的左子树或右子树 + BiTree q,s ; + if(!&(*p)->rchild && !&(*p)->lchild) + { + p = NULL; + } + else if (!(*p)->rchild) { // 右子树为空树则只需重接它的左子树 + q = *p; + *p = (*p)->lchild; + free(q); + } + else if (!(*p)->lchild) { // 左子树为空树则只需重接它的右子树 + q = *p; + *p = (*p)->rchild; + free(q); + } + else { // 左右子树均不空 + q = *p; + s = (*p)->lchild; + while(s->rchild) { // s 指向被删结点的前驱 + q = s; + s = s->rchild; + } + (*p)->data = s->data; + if(q != *p) { + q->rchild = s->lchild; // 重接*q的右子树 + } else { + q->lchild = s->lchild; // 重接*q的左子树 + } + free(s); + } + return OK; +} +int DeleteBST(BiTree *T, int kval) { + // 若二叉排序树 T 中存在其关键字等于 kval 的 + // 数据元素,则删除该数据元素结点,并返回 + // 函数值 TRUE,否则返回函数值 FALSE + if(!(*T)) { // 不存在关键字等于kval的数据元素 + return FALSE; + } + else { + if(EQ(kval, (*T)->data)) { // 找到关键字等于key的数据元素 + Delete (T); + return TRUE; + } + else if(LT(kval, (*T)->data)) { // 继续在左子树中进行查找 + return DeleteBST(&(*T)->lchild, kval); + } + else { // 继续在右子树中进行查找 + return DeleteBST(&(*T)->rchild, kval); + } + } +} + +int main() +{ + int j; + BiTree T=NULL; + for(j = 0;j < 12;j++) + { + InsertBST(&T, num[j]); + } + PreOrderTraverse(T); + for(j = 0;j <5;j++) + { + if(!InsertBST(&T, searchnum[j])){ + DeleteBST(&T,searchnum[j]); + } + printf("\n"); + PreOrderTraverse(T); + } + return 0; +} diff --git "a/2017-1/ywy/Hash\350\241\250/2017-06-18 (2).png" "b/2017-1/ywy/Hash\350\241\250/2017-06-18 (2).png" new file mode 100644 index 00000000..2342fec4 Binary files /dev/null and "b/2017-1/ywy/Hash\350\241\250/2017-06-18 (2).png" differ diff --git "a/2017-1/ywy/Hash\350\241\250/Hash.c" "b/2017-1/ywy/Hash\350\241\250/Hash.c" new file mode 100644 index 00000000..6910aeeb --- /dev/null +++ "b/2017-1/ywy/Hash\350\241\250/Hash.c" @@ -0,0 +1,167 @@ +#include"Hash.h" +bool SuS(int n) +{ + int i; + if(n==2) + { + return TRUE; + } + for (i = 2; i < n; i++) + { + if(n%i==0) + { + return FALSE; + } + } + return TRUE; +} +Status InitHashTable(HashTable*H,int size) +{ + int i; + ElemType temp; + H->count = 0; + H->maxsize = size; + H->elem = (ElemType*)malloc(sizeof(ElemType)*size); + if (!(H->elem)) + { + return error; + } + for (i = 0; i < size; i++) + { + H->elem[i].key = UNKEY; + H->elem[i].val = UNVAL; + } + srand((unsigned)time(NULL)); + for (i = 0; H->count%d\n", H.elem[*add].key, H.elem[*add].val); + (*add)++; + (*count)++; + } + else + break; + } + + if (Equal(key, H.elem[*add].key)) + { + return SUCCESS; + } + else + { + return UNSUCCESS; + } +} +Status InsertHashTable(HashTable*H, ElemType e) +{ + int add, count; + if (SearchHashTable(*H, e.key, &add, &count) == SUCCESS) + { + return DUPLICATE; + } + else { + if (add == H->maxsize || count > H->maxsize / 2) + { + ReHash(H); + } + H->elem[add] = e; + H->count++; + printf("插入[%d]:%d->%d", add, H->elem[add].key, H->elem[add].val); + printf("碰撞次数:%d\n", count); + return ok; + } + +} +Status ReHash(HashTable *H) +{ + int i, n; + HashTable temp; + if (Empty(*H)) + { + return error; + } + n = 2 * H->maxsize; + while (!SuS(n)) + { + n++; + } + temp.maxsize = n; + temp.count = H->count; + temp.elem = (ElemType *)malloc(sizeof(ElemType)*n); + for (i = 0; i < n; i++) + { + temp.elem[i].key = UNKEY; + temp.elem[i].val = UNVAL; + } + + for (i = 0; i < H->maxsize; i++) { + temp.elem[i].key = H->elem[i].key; + temp.elem[i].val = H->elem[i].val; + } + + H->maxsize = temp.maxsize; + + H->count = temp.count; + + H->elem = (ElemType *)malloc(sizeof(ElemType)*n); + + for (i = 0; i < n; i++) + { + H->elem[i].key = temp.elem[i].key; + H->elem[i].val = temp.elem[i].val; + } + + free(temp.elem); + return ok; +} +Status PrintHash(HashTable H) +{ + int i; + if(Empty(H)) + { + return error; + } + printf("print hash\n"); + for (i = 0; i < H.maxsize; i++) + { + printf("[%d]: %d->%d\n", i, H.elem[i].key, H.elem[i].val); + } + return ok; +} + + + + + diff --git "a/2017-1/ywy/Hash\350\241\250/Hash.h" "b/2017-1/ywy/Hash\350\241\250/Hash.h" new file mode 100644 index 00000000..973542ca --- /dev/null +++ "b/2017-1/ywy/Hash\350\241\250/Hash.h" @@ -0,0 +1,46 @@ +#pragma once +#include +#include +#include +#include +#define SUCCESS 1 +#define UNSUCCESS 0 +#define DUPLICATE -1 +#define UNKEY -1 +#define UNVAL 0 +//#define HASHSIZE 30 +typedef int KeyType; +typedef int ValueType; +typedef struct _ElemType +{ + KeyType key;//关键字 + ValueType val;//值 +#ifdef CHAINED_HASH + struct _ElemType*next; +#endif +}ElemType; +typedef struct +{ + ElemType*elem;//存储哈希表元素 + int count; + int maxsize; +}HashTable; +typedef enum +{ + ok, + error +}Status; + +typedef enum +{ + FALSE, + TRUE +}bool; +Status PrintHash(HashTable H); +Status InitHashTable(HashTable*H,int size); +Status InsertHashTable(HashTable*H, ElemType e); +bool Empty(HashTable H); +bool Equal(KeyType a, KeyType b); +bool SuS(int n); +int SearchHashTable(HashTable H, KeyType key, int*add, int*count); +Status ReHash(HashTable *H); \ No newline at end of file diff --git "a/2017-1/ywy/Hash\350\241\250/test.c" "b/2017-1/ywy/Hash\350\241\250/test.c" new file mode 100644 index 00000000..c11d59eb --- /dev/null +++ "b/2017-1/ywy/Hash\350\241\250/test.c" @@ -0,0 +1,30 @@ +#include"Hash.h" +int main() + +{ + int size; + int i; + KeyType key; + HashTable H; + size = 11;// + srand((unsigned)time(NULL)); + + InitHashTable(&H, size); + PrintHash(H); + int add = 0; + int count = 0; + for (i = 0; i < H.count; i++) + { + key = rand() % size; + if(SearchHashTable(H,key,&add,&count)==SUCCESS) + { + printf("find %d ", key); + printf("address %d: \n", add); + } + else + { + printf("not find %d: \n", key); + } + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/ywy/\344\272\214\345\217\211\346\240\221/2017-04-26.png" "b/2017-1/ywy/\344\272\214\345\217\211\346\240\221/2017-04-26.png" new file mode 100644 index 00000000..21755f07 Binary files /dev/null and "b/2017-1/ywy/\344\272\214\345\217\211\346\240\221/2017-04-26.png" differ diff --git "a/2017-1/ywy/\344\272\214\345\217\211\346\240\221/hw7.cpp" "b/2017-1/ywy/\344\272\214\345\217\211\346\240\221/hw7.cpp" new file mode 100644 index 00000000..01def6d4 --- /dev/null +++ "b/2017-1/ywy/\344\272\214\345\217\211\346\240\221/hw7.cpp" @@ -0,0 +1,61 @@ +#include +#include +int count = 0; +typedef char ElemType; +typedef struct BiNode +{ + ElemType data; + struct BiNode *lchild, *rchild; +} BiNode, *BiTree; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +Status CreatBiTree(BiTree *T, ElemType *ar) +{ + ElemType ch; + ch = ar[count]; + count++; + if (ch == '$') + { + *T = NULL;//节点没有子树 + } + else + { + if (!(*T = (BiTree)malloc(sizeof(BiNode)))) + { + return OVERFLOW;//内存分配失败 + } + (*T)->data = ch;//生成根节点 + CreatBiTree(&(*T)->lchild, ar);//创建左子树 + CreatBiTree(&(*T)->rchild, ar); //创建右子树 + } + return OK; +} +void PostOrderTraverse(BiTree T) +{ + if (T == NULL) + { + return; + } + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c", T->data); +} +int main() +{ + BiTree T = NULL; + //Status ret; + ElemType str[80] = "ABCD$$$EF$$G$H$$I$J$$"; + + CreatBiTree(&T, str);//"ret" is used to text wether the operation of Create succeed or not + printf("创建二叉树 : ABCD$$$EF$$G$H$$I$J$$\n"); + printf("后序遍历 : "); + PostOrderTraverse(T); + printf("\n"); + return 0; + +} \ No newline at end of file diff --git "a/2017-1/ywy/\345\233\276/2017-05-24.png" "b/2017-1/ywy/\345\233\276/2017-05-24.png" new file mode 100644 index 00000000..fe7c1bfd Binary files /dev/null and "b/2017-1/ywy/\345\233\276/2017-05-24.png" differ diff --git "a/2017-1/ywy/\345\233\276/TU.c" "b/2017-1/ywy/\345\233\276/TU.c" new file mode 100644 index 00000000..c4145190 --- /dev/null +++ "b/2017-1/ywy/\345\233\276/TU.c" @@ -0,0 +1,293 @@ +#include +#include +#define INFINITY -1 +#define MXAVERTEX_NUM 10//最大顶点个数 +#define VRType int +#define VertexType int +#define InfoType int +#define QElemType int +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; +typedef enum +{ + true, + false +}bool; +//-------图的邻接矩阵存储-----------// +typedef struct Arcell +{ + VRType adj;//顶点关系,1或0 + InfoType *info;//弧相关信息的指针 +}Arcell, AdjMatrix[MXAVERTEX_NUM][MXAVERTEX_NUM]; +typedef struct +{ + VertexType vexs[MXAVERTEX_NUM]; //顶点向量 + AdjMatrix arcs; //邻接矩阵 + int vexnum, arcnum; //图的当前顶点和弧数 +}MGraph; +//--------队列---------// +typedef struct QNode +{ + QElemType data; + struct QNode *next; + struct QNode *pre; +}QNode, *QueuePtr; +typedef struct LinkQueue +{ + QueuePtr front; + QueuePtr rear; +}LinkQueue; +/*=========队列的基本操作=========*/ +Status InitQueue(LinkQueue *Q) +{ + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if (!Q->front) + { + return ERROR; + } + Q->front->next = Q->rear->next = NULL; + return OK; +} + +Status EnQueue(LinkQueue*Q, QElemType e) +{ + QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); + if (!p) + { + return ERROR; + } + else + { + p->data = e; + p->next = NULL; + p->pre = Q->front; + Q->rear->next = p; + Q->rear = p; + return OK; + } +} + +Status DeQueue(LinkQueue*Q, QElemType*e) +{ + if (Q->front == Q->rear) + { + return ERROR; + } + Q->front = Q->front->next; + *e = Q->front->data; + return OK; +} + +bool QueueEmpty(LinkQueue*Q) +{ + if (Q->front == Q->rear) + { + return true; + } + else + { + return false; + } +} + +Status DestroyQueue(LinkQueue*Q) +{ + while (Q->front) + { + Q->rear = Q->front->next; + free(Q->front); + Q->front = Q->rear; + } + return OK; +} +/*=========图的基本操作=========*/ + +int LocateVex(MGraph *G, int v1)//返回顶点v1的位置 +{ + int i; + int n = -1; + + for (i = 0; i <= G->vexnum; i++) + { + if (G->vexs[i] == v1) + { + n= i; + break; + } + } + if (n != -1) + { + return n; + } + return 0; +} +Status InsertArc(MGraph *G, int v1, int v2)//v1、v2间添加弧 +{ + int i, j; + i = LocateVex(G, v1); + j= LocateVex(G, v2); + if (i != 0 && j != 0) + { + G->arcs[i][j].adj = 1;//两点之间有连线,弧值为1; + G->arcs[j][i] = G->arcs[i][j]; + return OK; + } + else + { + return ERROR; + } +} +//构建图 +Status CreateUDN(MGraph *G) +{ + int i; + int j; + //根据用例直接赋值。 + G->vexnum = 9; + G->arcnum = 12; + for (i = 1; i <= G->vexnum; i++) + { + G->vexs[i] = i;//构建顶点向量; + } + for (i = 0; i <= G->vexnum; i++)//初始化邻接矩阵; + { + for (j = 0; j <= G->vexnum; j++) + { + G->arcs[i][j].adj = INFINITY; + G->arcs[i][j].info = NULL; + } + } + //构建邻接矩阵; + InsertArc(G, 1, 2); + InsertArc(G, 1, 3); + InsertArc(G, 1, 4); + InsertArc(G, 1, 7); + InsertArc(G, 2, 3); + InsertArc(G, 4, 5); + InsertArc(G, 4, 6); + InsertArc(G, 5, 6); + InsertArc(G, 6, 8); + InsertArc(G, 7, 8); + InsertArc(G, 7, 9); + InsertArc(G, 8, 9); + + return OK; +} + +//找出第一个邻接点 +int FirstAdjVex(MGraph *G, int u) +{ + int i; + for (i = 1; i <= G->vexnum; i++) + { + if (G->arcs[u][i].adj == 1) + { + return i; + } + } + return -1; +} + +//找出下一个邻接点 +int NextAdjvex(MGraph *G, int u, int w) +{ + int i; + for (i = w + 1; i <= G->vexnum; i++) + { + if (G->arcs[u][i].adj == 1) + { + return i; + } + } + return -1; +} +//广度优先遍历图,求两点a,b间的最短路径; +Status BFSTraverse(MGraph*G, LinkQueue *Q, int a, int b) +{ + + int v; + int u = 0; + int w = 0; + int m = 0; + int n = 0; + bool visited[MXAVERTEX_NUM]; + for (v = 1; v <= G->vexnum; v++) + { + visited[v] = false; //标记数组,标记图中已访问的点 + } + + EnQueue(Q, a); //a先入队列; + while (QueueEmpty(Q) != true) + { + DeQueue(Q, &u); + for (w = FirstAdjVex(G, u); w >= 0; w = NextAdjvex(G, u, w)) + { + if (visited[w] == false)//判断w是否已经访问过 + { + visited[w] = true; + EnQueue(Q, w); + } + if (w == b) + { + break; + } + } + if (w == b) + { + break; + } + } + return OK; +} + +Status print(LinkQueue *Q, int a) +{ + if (Q->rear->data == a) + { + printf("%d->%d\n", a, a); + return OK; + } + + int i = 0; + int j; + int num[MXAVERTEX_NUM] = { 0 }; + while (Q->rear->data != a)//倒序进入数组 + { + num[i] = Q->rear->data; + Q->rear = Q->rear->pre; + i++; + } + printf("%d", a); + for (j = i - 1; j >= 0; j--) + { + printf("->%d", num[j]); + } + + + printf("\n"); + return OK; +} + +int main() +{ + int i, j; + MGraph Graph; + CreateUDN(&Graph); + for (i = 1; i <= Graph.vexnum; i++) + { + for (j = 1; j <= Graph.vexnum; j++) + { + LinkQueue Q; + InitQueue(&Q); + printf("%d<->%d: ", i, j); + BFSTraverse(&Graph, &Q, i, j); + print(&Q, i); + DestroyQueue(&Q); + + } + } +} \ No newline at end of file diff --git "a/2017-1/ywy/\346\216\222\345\272\217/2017-06-14.png" "b/2017-1/ywy/\346\216\222\345\272\217/2017-06-14.png" new file mode 100644 index 00000000..ffd77b64 Binary files /dev/null and "b/2017-1/ywy/\346\216\222\345\272\217/2017-06-14.png" differ diff --git "a/2017-1/ywy/\346\216\222\345\272\217/Sort.c" "b/2017-1/ywy/\346\216\222\345\272\217/Sort.c" new file mode 100644 index 00000000..3e9560de --- /dev/null +++ "b/2017-1/ywy/\346\216\222\345\272\217/Sort.c" @@ -0,0 +1,179 @@ +#include"Sort.h" +void swap(SqList*L, int i, int j) +{ + RedType temp = L->r[i]; + L->r[i] = L->r[j]; + L->r[j] = temp; +} + +void ShellSort(SqList*L) +{ + int i, j; + int count1 = 0, count2 = 0; + int rise = L->length; + do + { + rise = rise / 5 + 1; + for (i = rise + 1; i <= L->length; i++) + { + count1++; + if (LT(L->r[i].key, L->r[i - rise].key)) + { + L->r[0] = L->r[i]; + count2++; + for (j = i - rise; LT(L->r[0].key, L->r[j].key) && j > 0; j = j - rise) + { + count1++; + L->r[j + rise] = L->r[j]; + count2++; + } + count1++; + L->r[j + rise] = L->r[0]; + count2++; + } + } + } while (rise > 1); + printf("希尔排序为: "); + for (i = 2; i <= L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d \n", count1, count2); + printf("\n"); +} +void InsertSort(SqList*L) +{ + int i, j; + int count1 = 0; + int count2 = 0; + for (i = 2; i <= L->length; i++) + { + ++count1; + if (LT(L->r[i].key, L->r[i - 1].key)) + { + L->r[0] = L->r[i]; + count2++; + //L->r[i] = L->r[i - 1]; + for (j = i - 1; LT(L->r[0].key, L->r[j].key); --j) + { + count1++; + count2++; + L->r[j + 1] = L->r[j]; + } + count1++; + L->r[j + 1] = L->r[0]; + count2++; + } + } + printf("插入排序后为:"); + for (i = 2; i<=L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d \n", count1, count2); + printf("\n"); +} +void BubbleSort(SqList*L) +{ + int i, j; + int count1 = 0; + int count2 = 0; + for (i = 1; i < L->length; i++) + { + for (j = i+1; j < L->length; j++) + { + count1++; + if ( LT(L->r[j].key, L->r[i].key)) + { + swap(L,i,j); + count2 += 3; + } + } + } + printf("冒泡排序后为:"); + for (i = 1; ilength; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d \n", count1, count2); + printf("\n"); +} +int Partition(SqList*L, int low, int high,int*count1,int*count2) +{ + int pivotkey; + //L->r[0] = L->r[low]; + pivotkey = L->r[low].key; + while (low < high) + { + while (L->r[high].key >= pivotkey && low < high) + { + (*count1)++; + high--; + } + (*count1)++; + swap(L, low, high); + (*count2) += 3; + while (L->r[low].key <= pivotkey && low < high) + { + (*count1)++; + low++; + } + (*count1)++; + swap(L, low, high); + (*count2) += 3; + } + return low; +} + +void QSort(SqList*L, int low, int high,int*count1,int*count2) +{ + int pivot; + if (low < high) + { + pivot = Partition(L, low, high, count1, count2); + QSort(L, low, pivot - 1, count1, count2); + QSort(L, pivot + 1, high, count1,count2); + } +} +void QuickSort(SqList*L) +{ + int i; + int count1 = 0, count2 = 0; + QSort(L, 1, L->length, &count1, &count2); + printf("快速排序后为:"); + for (i = 2; i<=L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d \n", count1, count2); +} +void SelectSort(SqList*L) +{ + int i, j,min; + int count1 = 0; + int count2 = 0; + for (i = 1; i < L->length; i++) + { + min = i; + for (j = i + 1; j <= L->length; j++) + { + count1++; + if (LT(L->r[j].key, L->r[min].key)) + { + min = j; + } + } + if (i != min) + { + swap(L, i, min); + count2 += 3; + } + } + printf("\n简单选择排序为:"); + for (i = 2; i<=L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d \n", count1, count2); + printf("\n"); +} diff --git "a/2017-1/ywy/\346\216\222\345\272\217/Sort.h" "b/2017-1/ywy/\346\216\222\345\272\217/Sort.h" new file mode 100644 index 00000000..e2b9681d --- /dev/null +++ "b/2017-1/ywy/\346\216\222\345\272\217/Sort.h" @@ -0,0 +1,27 @@ +#pragma once +#include +#include +#include +typedef int KeyType; +typedef int InfoType; +#define maxsize 20 +#define len 10 +#define LT(a,b) ((a)<(b)) +typedef struct +{ + KeyType key; + InfoType otherinfo; +}RedType; +typedef struct +{ + RedType r[maxsize+1];//r[0]作为暂存空间 + int length; +}SqList; +void swap(SqList*L, int i, int j); +void ShellSort(SqList*L); +void InsertSort(SqList*L); +void BubbleSort(SqList*L); +void QuickSort(SqList*L); +void QSort(SqList*L, int low, int high, int*count1, int*count2); +int Partition(SqList*L, int low, int high,int*count1,int*count2); +void SelectSort(SqList*L); \ No newline at end of file diff --git "a/2017-1/ywy/\346\216\222\345\272\217/test.c" "b/2017-1/ywy/\346\216\222\345\272\217/test.c" new file mode 100644 index 00000000..574fec1e --- /dev/null +++ "b/2017-1/ywy/\346\216\222\345\272\217/test.c" @@ -0,0 +1,26 @@ +#include"Sort.h" +//#include"Sort.c" +int main() +{ + SqList L, L1, L2, L3, L4; + L.length = len; + int i = 0; + + for (i = 1; i <= len; i++) + { + L.r[i].key = 10 - i;; + } + printf("\n原顺序为:"); + for (i = 1; i +#include + +//二叉查找树结点描述 +typedef int KeyType; +typedef int ElemType; +int flag; +typedef struct Node +{ + ElemType data; //关键字 + struct Node * left; //左孩子指针 + struct Node * right; //右孩子指针 + struct Node * parent; //指向父节点指针 +}Node, *PNode; + +//往二叉查找树中插入结点 +//插入的话,可能要改变根结点的地址,所以传的是二级指针 +void inseart(PNode * T, KeyType key) +{ + //初始化插入结点 + PNode p = (PNode)malloc(sizeof(Node)); + p->data = key; + p->left = p->right = p->parent = NULL; + //空树时,直接作为根结点 + if ((*T) == NULL) { + *T = p; + return; + } + //插入到当前结点(*root)的左孩子 + if ((*T)->left == NULL && (*T)->data > key) { + p->parent = (*T); + (*T)->left = p; + return; + } + //插入到当前结点(*root)的右孩子 + if ((*T)->right == NULL && (*T)->data < key) { + p->parent = (*T); + (*T)->right = p; + return; + } + if ((*T)->data > key) + inseart(&(*T)->left, key); + else if ((*T)->data < key) + inseart(&(*T)->right, key); + else + return; +} + +//查找元素,找到返回关键字的结点指针,没找到返回NULL +PNode search(PNode T, KeyType key) +{ + if (T == NULL) + return NULL; + if (key > T->data) //查找右子树 + return search(T->right, key); + else if (key < T->data) //查找左子树 + return search(T->left, key); + else + return T; +} + +//查找最小关键字,空树时返回NULL +PNode searchMin(PNode T) +{ + if (T == NULL) + return NULL; + if (T->left == NULL) + return T; + else //一直往左孩子找,直到没有左孩子的结点 + return searchMin(T->left); +} + +//查找最大关键字,空树时返回NULL +PNode searchMax(PNode T) +{ + if (T == NULL) + return NULL; + if (T->right == NULL) + return T; + else //一直往右孩子找,直到没有右孩子的结点 + return searchMax(T->right); +} + +//查找某个结点的前驱 +PNode searchPredecessor(PNode p) +{ + //空树 + if (p == NULL) + return p; + //有左子树、左子树中最大的那个 + if (p->left) + return searchMax(p->left); + //无左子树,查找某个结点的右子树遍历完了 + else { + if (p->parent == NULL) + return NULL; + //向上寻找前驱 + while (p) { + if (p->parent->right == p) + break; + p = p->parent; + } + return p->parent; + } +} + +//查找某个结点的后继 +PNode searchSuccessor(PNode p) +{ + //空树 + if (p == NULL) + return p; + //有右子树、右子树中最小的那个 + if (p->right) + return searchMin(p->right); + //无右子树,查找某个结点的左子树遍历完了 + else { + if (p->parent == NULL) + return NULL; + //向上寻找后继 + while (p) { + if (p->parent->left == p) + break; + p = p->parent; + } + return p->parent; + } +} + +//根据关键字删除某个结点,删除成功返回1,否则返回0 +//如果把根结点删掉,那么要改变根结点的地址,所以传二级指针 +int deleteNode(PNode* T, KeyType key) +{ + PNode q; + //查找到要删除的结点 + PNode p = search(*T, key); + KeyType temp; //暂存后继结点的值 + //没查到此关键字 + if (!p) + return 0; + //1.被删结点是叶子结点,直接删除 + if (p->left == NULL && p->right == NULL) { + //只有一个元素,删完之后变成一颗空树 + if (p->parent == NULL) { + free(p); + (*T) = NULL; + } + else { + //删除的结点是父节点的左孩子 + if (p->parent->left == p) + p->parent->left = NULL; + else //删除的结点是父节点的右孩子 + p->parent->right = NULL; + free(p); + } + } + + //2.被删结点只有左子树 + else if (p->left && !(p->right)) { + p->left->parent = p->parent; + //如果删除是父结点,要改变父节点指针 + if (p->parent == NULL) + *T = p->left; + //删除的结点是父节点的左孩子 + else if (p->parent->left == p) + p->parent->left = p->left; + else //删除的结点是父节点的右孩子 + p->parent->right = p->left; + free(p); + } + //3.被删结点只有右孩子 + else if (p->right && !(p->left)) { + p->right->parent = p->parent; + //如果删除是父结点,要改变父节点指针 + if (p->parent == NULL) + *T = p->right; + //删除的结点是父节点的左孩子 + else if (p->parent->left == p) + p->parent->left = p->right; + else //删除的结点是父节点的右孩子 + p->parent->right = p->right; + free(p); + } + //4.被删除的结点既有左孩子,又有右孩子 + //该结点的后继结点肯定无左子树(参考上面查找后继结点函数) + //删掉后继结点,后继结点的值代替该结点 + else { + //找到要删除结点的后继 + q = searchSuccessor(p); + temp = q->data; + //删除后继结点 + deleteNode(T, q->data); + p->data= temp; + } + return 1; +} + +//创建一棵二叉查找树 +void create(PNode* T, KeyType *keyArray, int length) +{ + int i; + //逐个结点插入二叉树中 + for (i = 0; idata,pfile); + pre(T->left,pfile); + pre(T->right,pfile); + } + //printf("\n"); +} + + +int main(void) +{ + int i; + char c = '\n'; + FILE*pfile; + pfile = fopen(" BSTOutput.txt","a"); + PNode T = NULL; + KeyType nodeArray[12] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + create(&T, nodeArray, 12); + pre(T,pfile); + fwrite(&c, sizeof(c), 1, pfile); + KeyType searArray[5] = { 13, 8, 5, 20, 6 }; + for (i = 0; i < 5; i++) + { + if (deleteNode(&T, searArray[i]) == 0) + { + inseart(&T, searArray[i]); + } + flag = 0; + //pre(T,pfile); + fwrite(&c, sizeof(c), 1, pfile); + } + pre(T, pfile); + fclose(pfile); + pfile = NULL; + return 0; +} + diff --git "a/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" "b/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" new file mode 100644 index 00000000..c20b6e55 Binary files /dev/null and "b/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\221/BSTOutput.txt" differ diff --git "a/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\2212/BSTOutput.txt" "b/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\2212/BSTOutput.txt" new file mode 100644 index 00000000..c523c090 Binary files /dev/null and "b/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\2212/BSTOutput.txt" differ diff --git "a/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\2212/test.c" "b/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\2212/test.c" new file mode 100644 index 00000000..120aaf0d --- /dev/null +++ "b/2017-1/ywy/\346\216\222\345\272\217\344\272\214\345\217\211\346\240\2212/test.c" @@ -0,0 +1,521 @@ +锘#include + +#include + + + +//浜屽弶鏌ユ壘鏍戠粨鐐规弿杩 + +typedef int KeyType; + +typedef int ElemType; + +int flag; + +typedef struct Node + +{ + + ElemType data; //鍏抽敭瀛 + + struct Node * left; //宸﹀瀛愭寚閽 + + struct Node * right; //鍙冲瀛愭寚閽 + + struct Node * parent; //鎸囧悜鐖惰妭鐐规寚閽 + +}Node, *PNode; + + + +//寰浜屽弶鏌ユ壘鏍戜腑鎻掑叆缁撶偣 + +//鎻掑叆鐨勮瘽锛屽彲鑳借鏀瑰彉鏍圭粨鐐圭殑鍦板潃锛屾墍浠ヤ紶鐨勬槸浜岀骇鎸囬拡 + +void inseart(PNode * T, KeyType key) + +{ + + //鍒濆鍖栨彃鍏ョ粨鐐 + + PNode p = (PNode)malloc(sizeof(Node)); + + p->data = key; + + p->left = p->right = p->parent = NULL; + + //绌烘爲鏃讹紝鐩存帴浣滀负鏍圭粨鐐 + + if ((*T) == NULL) { + + *T = p; + + return; + + } + + //鎻掑叆鍒板綋鍓嶇粨鐐癸紙*root锛夌殑宸﹀瀛 + + if ((*T)->left == NULL && (*T)->data > key) { + + p->parent = (*T); + + (*T)->left = p; + + return; + + } + + //鎻掑叆鍒板綋鍓嶇粨鐐癸紙*root锛夌殑鍙冲瀛 + + if ((*T)->right == NULL && (*T)->data < key) { + + p->parent = (*T); + + (*T)->right = p; + + return; + + } + + if ((*T)->data > key) + + inseart(&(*T)->left, key); + + else if ((*T)->data < key) + + inseart(&(*T)->right, key); + + else + + return; + +} + + + +//鏌ユ壘鍏冪礌,鎵惧埌杩斿洖鍏抽敭瀛楃殑缁撶偣鎸囬拡锛屾病鎵惧埌杩斿洖NULL + +PNode search(PNode T, KeyType key) + +{ + + if (T == NULL) + + return NULL; + + if (key > T->data) //鏌ユ壘鍙冲瓙鏍 + + return search(T->right, key); + + else if (key < T->data) //鏌ユ壘宸﹀瓙鏍 + + return search(T->left, key); + + else + + return T; + +} + + + +//鏌ユ壘鏈灏忓叧閿瓧,绌烘爲鏃惰繑鍥濶ULL + +PNode searchMin(PNode T) + +{ + + if (T == NULL) + + return NULL; + + if (T->left == NULL) + + return T; + + else //涓鐩村線宸﹀瀛愭壘锛岀洿鍒版病鏈夊乏瀛╁瓙鐨勭粨鐐 + + return searchMin(T->left); + +} + + + +//鏌ユ壘鏈澶у叧閿瓧,绌烘爲鏃惰繑鍥濶ULL + +PNode searchMax(PNode T) + +{ + + if (T == NULL) + + return NULL; + + if (T->right == NULL) + + return T; + + else //涓鐩村線鍙冲瀛愭壘锛岀洿鍒版病鏈夊彸瀛╁瓙鐨勭粨鐐 + + return searchMax(T->right); + +} + + + +//鏌ユ壘鏌愪釜缁撶偣鐨勫墠椹 + +PNode searchPredecessor(PNode p) + +{ + + //绌烘爲 + + if (p == NULL) + + return p; + + //鏈夊乏瀛愭爲銆佸乏瀛愭爲涓渶澶х殑閭d釜 + + if (p->left) + + return searchMax(p->left); + + //鏃犲乏瀛愭爲,鏌ユ壘鏌愪釜缁撶偣鐨勫彸瀛愭爲閬嶅巻瀹屼簡 + + else { + + if (p->parent == NULL) + + return NULL; + + //鍚戜笂瀵绘壘鍓嶉┍ + + while (p) { + + if (p->parent->right == p) + + break; + + p = p->parent; + + } + + return p->parent; + + } + +} + + + +//鏌ユ壘鏌愪釜缁撶偣鐨勫悗缁 + +PNode searchSuccessor(PNode p) + +{ + + //绌烘爲 + + if (p == NULL) + + return p; + + //鏈夊彸瀛愭爲銆佸彸瀛愭爲涓渶灏忕殑閭d釜 + + if (p->right) + + return searchMin(p->right); + + //鏃犲彸瀛愭爲,鏌ユ壘鏌愪釜缁撶偣鐨勫乏瀛愭爲閬嶅巻瀹屼簡 + + else { + + if (p->parent == NULL) + + return NULL; + + //鍚戜笂瀵绘壘鍚庣户 + + while (p) { + + if (p->parent->left == p) + + break; + + p = p->parent; + + } + + return p->parent; + + } + +} + + + +//鏍规嵁鍏抽敭瀛楀垹闄ゆ煇涓粨鐐,鍒犻櫎鎴愬姛杩斿洖1,鍚﹀垯杩斿洖0 + +//濡傛灉鎶婃牴缁撶偣鍒犳帀锛岄偅涔堣鏀瑰彉鏍圭粨鐐圭殑鍦板潃锛屾墍浠ヤ紶浜岀骇鎸囬拡 + +int deleteNode(PNode* T, KeyType key) + +{ + + PNode q; + + //鏌ユ壘鍒拌鍒犻櫎鐨勭粨鐐 + + PNode p = search(*T, key); + + KeyType temp; //鏆傚瓨鍚庣户缁撶偣鐨勫 + + //娌℃煡鍒版鍏抽敭瀛 + + if (!p) + + return 0; + + //1.琚垹缁撶偣鏄彾瀛愮粨鐐癸紝鐩存帴鍒犻櫎 + + if (p->left == NULL && p->right == NULL) { + + //鍙湁涓涓厓绱狅紝鍒犲畬涔嬪悗鍙樻垚涓棰楃┖鏍 + + if (p->parent == NULL) { + + free(p); + + (*T) = NULL; + + } + + else { + + //鍒犻櫎鐨勭粨鐐规槸鐖惰妭鐐圭殑宸﹀瀛 + + if (p->parent->left == p) + + p->parent->left = NULL; + + else //鍒犻櫎鐨勭粨鐐规槸鐖惰妭鐐圭殑鍙冲瀛 + + p->parent->right = NULL; + + free(p); + + } + + } + + + + //2.琚垹缁撶偣鍙湁宸﹀瓙鏍 + + else if (p->left && !(p->right)) { + + p->left->parent = p->parent; + + //濡傛灉鍒犻櫎鏄埗缁撶偣锛岃鏀瑰彉鐖惰妭鐐规寚閽 + + if (p->parent == NULL) + + *T = p->left; + + //鍒犻櫎鐨勭粨鐐规槸鐖惰妭鐐圭殑宸﹀瀛 + + else if (p->parent->left == p) + + p->parent->left = p->left; + + else //鍒犻櫎鐨勭粨鐐规槸鐖惰妭鐐圭殑鍙冲瀛 + + p->parent->right = p->left; + + free(p); + + } + + //3.琚垹缁撶偣鍙湁鍙冲瀛 + + else if (p->right && !(p->left)) { + + p->right->parent = p->parent; + + //濡傛灉鍒犻櫎鏄埗缁撶偣锛岃鏀瑰彉鐖惰妭鐐规寚閽 + + if (p->parent == NULL) + + *T = p->right; + + //鍒犻櫎鐨勭粨鐐规槸鐖惰妭鐐圭殑宸﹀瀛 + + else if (p->parent->left == p) + + p->parent->left = p->right; + + else //鍒犻櫎鐨勭粨鐐规槸鐖惰妭鐐圭殑鍙冲瀛 + + p->parent->right = p->right; + + free(p); + + } + + //4.琚垹闄ょ殑缁撶偣鏃㈡湁宸﹀瀛愶紝鍙堟湁鍙冲瀛 + + //璇ョ粨鐐圭殑鍚庣户缁撶偣鑲畾鏃犲乏瀛愭爲(鍙傝冧笂闈㈡煡鎵惧悗缁х粨鐐瑰嚱鏁) + + //鍒犳帀鍚庣户缁撶偣,鍚庣户缁撶偣鐨勫间唬鏇胯缁撶偣 + + else { + + //鎵惧埌瑕佸垹闄ょ粨鐐圭殑鍚庣户 + + q = searchSuccessor(p); + + temp = q->data; + + //鍒犻櫎鍚庣户缁撶偣 + + deleteNode(T, q->data); + + p->data = temp; + + } + + return 1; + +} + + + +//鍒涘缓涓妫典簩鍙夋煡鎵炬爲 + +void create(PNode* T, KeyType *keyArray, int length) + +{ + + int i; + + //閫愪釜缁撶偣鎻掑叆浜屽弶鏍戜腑 + + for (i = 0; idata, pfile); + + pre(T->left, pfile); + + pre(T->right, pfile); + + } + + //printf("\n"); + +} + + + + + +int main(void) + +{ + + int i; + + char c = '\n'; + + FILE*pfile; + + pfile = fopen(" BSTOutput.txt", "a"); + + PNode T = NULL; + + KeyType nodeArray[12] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + + create(&T, nodeArray, 12); + + pre(T, pfile); + + fwrite(&c, sizeof(c), 1, pfile); + + KeyType searArray[5] = { 13, 8, 5, 20, 6 }; + + for (i = 0; i < 5; i++) + + { + + if (deleteNode(&T, searArray[i]) == 0) + + { + + inseart(&T, searArray[i]); + + } + + flag = 0; + + //pre(T,pfile); + + fwrite(&c, sizeof(c), 1, pfile); + + } + + pre(T, pfile); + + fclose(pfile); + + pfile = NULL; + + return 0; + +} \ No newline at end of file diff --git "a/2017-1/ywy/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2017-04-09.png" "b/2017-1/ywy/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2017-04-09.png" new file mode 100644 index 00000000..f960c05f Binary files /dev/null and "b/2017-1/ywy/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2017-04-09.png" differ diff --git "a/2017-1/ywy/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2017-04-26 (1).png" "b/2017-1/ywy/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2017-04-26 (1).png" new file mode 100644 index 00000000..680f974a Binary files /dev/null and "b/2017-1/ywy/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/2017-04-26 (1).png" differ diff --git "a/2017-1/ywy/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" "b/2017-1/ywy/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" new file mode 100644 index 00000000..83f0e664 --- /dev/null +++ "b/2017-1/ywy/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" @@ -0,0 +1,266 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +#define overflow -2 +#define ok 1 +#define error 0 +typedef char SElemtype; +typedef int Elemtype; +typedef struct { + SElemtype*base; + SElemtype*top; + int stacksize; +}SqstackOptr;//运算符栈 +typedef struct { + Elemtype*base; + Elemtype*top; + int stacksize; +}SqstackOpnd;//运算数栈 +//运算符栈 +int InitstackOptr(SqstackOptr*s);//初始化栈 +SElemtype GettopOptr(SqstackOptr*s);//获取栈顶元素 +int PushOptr(SqstackOptr*s, SElemtype e);//入栈 +int PopOptr(SqstackOptr*s, SElemtype *e);//删除栈中元素 +int DestorystackOptr(SqstackOptr*s);//销毁栈 +//运算数栈 +int InitstackOpnd(SqstackOpnd*s); +Elemtype GettopOpnd(SqstackOpnd*s); +int PushOpnd(SqstackOpnd*s, Elemtype e); +int PopOpnd(SqstackOpnd*s, Elemtype*e); +int DestorystackOpnd(SqstackOpnd*s); +//表达式求值 +//输入表达式并计算 +Elemtype EvaluateExpression(SqstackOptr*OPTR, SqstackOpnd*OPND); +SElemtype Precede(char topc, char c);//判断运算符优先级 +Elemtype Operate(Elemtype a, SElemtype theta, Elemtype b);//计算部分表达式的值 +Elemtype In(char c, char op[]);//判断输入的字符是不是运算符 +// 初始化栈 +int InitstackOptr(SqstackOptr*s) { + s->base = (SElemtype *)malloc(STACK_INIT_SIZE * sizeof(SElemtype)); + if (!s->base) { + return overflow; + } + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return ok; +} +int InitstackOpnd(SqstackOpnd*s) +{ + s->base = (Elemtype*)malloc(STACK_INIT_SIZE * sizeof(Elemtype)); + if (!s->base) + { + return overflow; + } + s->top = s->base; + s->stacksize = STACK_INIT_SIZE; + return ok; +} +//获取栈顶元素 +SElemtype GettopOptr(SqstackOptr*s) +{ + if(s->top==s->base) + { + return error; + } + return *(s->top - 1); +} +Elemtype GettopOpnd(SqstackOpnd*s) +{ + if(s->top==s->base) + { + return error; + } + return *(s->top - 1); +} +//入栈 +int PushOptr(SqstackOptr*s, SElemtype e) +{ + if ((s->top - s->base) >= s->stacksize) + { + s->base = (SElemtype*)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(SElemtype)); + if(!s->base) + { + return error; + } + s->top = s->base + s->stacksize; + s->stacksize += STACKINCREMENT; + } + *s->top++ = e; + return ok; +} +int PushOpnd(SqstackOpnd*s, Elemtype e) +{ + if ((s->top - s->base) >= s->stacksize) + { + s->base = (Elemtype*)realloc(s->base, (s->stacksize + STACKINCREMENT) * sizeof(Elemtype)); + if (!s->base) + { + return error; + } + s->top = s->base + s->stacksize; + s->stacksize += STACKINCREMENT; + } + *s->top++ = e; + return ok; +} +//删除元素 +int PopOptr(SqstackOptr*s, SElemtype*e) +{ + if(s->top==s->base) + { + return error; + } + *e = *--s->top; + return ok; +} +int PopOpnd(SqstackOpnd*s, Elemtype*e) +{ + if(s->top==s->base) + { + return error; + } + *e = *--s->top; + return ok; +} +//销毁栈 +int DestorystackOptr(SqstackOptr*s) +{ + s->top = s->base; + free(s->base); + s->top = NULL; + s->base = NULL; + return ok; +} +int DestorystackOpnd(SqstackOpnd*s) +{ + s->top = s->base; + free(s->base); + s->top = NULL; + s->base = NULL; + return ok; +} +//判断输入的字符是不是运算符 +Elemtype In(char c, char op[]) +{ + int i, f = 0; + for (i = 0; i < 7; i++) + { + if (c == op[i]) + { + f = i; + break; + } + } + if (f == 0) + { + return 0; + } + else return 1; +} +//比较运算符优先级 +SElemtype Precede(char topc, char c) +{ + if (topc == '+' || topc == '-') + { + if (c == '+' || c == '-' || c == ')' || c == '#') + { + return '>'; + } + else return '<'; + } + if (topc == '*' || topc == '/') + { + if (c == '(') + { + return '<'; + } + else return '>'; +} + if (topc == '(') + { + if (c == ')') + { + return '='; + } + else return '<'; + } + if (topc == ')') + { + return ')'; + } + if (topc == '#') + { + if (c == '#') + { + return '='; + } + else return '<'; + } +} +//求值 +Elemtype Operate(Elemtype a, SElemtype theta, Elemtype b) +{ + switch (theta) + { + case '+':return a + b; break; + case '-':return a - b; break; + case'*':return a*b; break; + case'/':return a / b; break; + } +} +//算法 +Elemtype EvaluateExpression(SqstackOptr*OPTR, SqstackOpnd*OPND) +{ + SElemtype c, theta, x; + Elemtype a, b,v; + SElemtype op[7] = { '+','-','*','/','(',')','#' }; + InitstackOptr(OPTR); + InitstackOpnd(OPND); + PushOptr(OPTR, '#'); + printf("输入表达式: "); + c = getchar(); + while (c != '#' || GettopOptr(OPTR) != '#') + { + if (!In(c, op)) + { + PushOpnd(OPND, c - '0'); + c = getchar(); + } + else + { + switch (Precede(GettopOptr(OPTR), c)) + { + case'<'://栈顶元素优先级低 + PushOptr(OPTR, c); + c = getchar(); + break; + case'>'://栈顶元素优先级高 + PopOptr(OPTR, &theta); + PopOpnd(OPND, &b); + PopOpnd(OPND, &a); + v = Operate(a, theta, b); + PushOpnd(OPND, v); + break; + case'=': + PopOptr(OPTR, &x); + c = getchar(); + break; + } + } + } + return GettopOpnd(OPND); +} +int main() +{ + SqstackOpnd OPND; + SqstackOptr OPTR; + int result; + result = EvaluateExpression(&OPTR, &OPND); + printf("表达式结果为: %d\n", result); + system("pause"); + return 0; +} +//算法部分和运算符比较部分参考较多 \ No newline at end of file diff --git "a/2017-1/ywy/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\351\230\237\345\210\227\351\223\276\350\241\250\345\274\217\350\241\250\350\276\276.c" "b/2017-1/ywy/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\351\230\237\345\210\227\351\223\276\350\241\250\345\274\217\350\241\250\350\276\276.c" new file mode 100644 index 00000000..9419bcb1 --- /dev/null +++ "b/2017-1/ywy/\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232/\351\230\237\345\210\227\351\223\276\350\241\250\345\274\217\350\241\250\350\276\276.c" @@ -0,0 +1,108 @@ +#include +typedef int QElemtype; +typedef struct QNode +{ + QElemtype data; + struct QNode *next; +}QNode, *QNodePtr; +typedef struct +{ + QNodePtr front; + QNodePtr rear; +}LinkQueue; +typedef enum +{ + true, + false +}Status; +//初始化一个队列 +Status InitQueue(LinkQueue *Q) +{ + printf("初始化一个队列\n"); + Q->front = Q->rear = (QNodePtr)malloc(sizeof(QNode)); + if (!Q->front) + { + return false; + } + Q->front->next = NULL; + return true; +} + +//向队列尾部插入元素e +Status EnQueue(LinkQueue *Q, QElemtype e) +{ + QNodePtr s = (QNodePtr)malloc(sizeof(QNode)); + if (!s) + { + return false; + } + s->data = e; + s->next = NULL; + Q->rear->next = s; + Q->rear = s; + return true; +} +//若队列不空,则删除队列头元素,并用e返回 +Status DeQueue(LinkQueue *Q, QElemtype *e) +{ + QNodePtr p = NULL; + if (Q->rear == Q->front) + { + return false; + } + p = Q->front->next; + *e = p->data; + Q->front->next = p->next; + if (p == Q->rear) + { + Q->rear = Q->front; + } + printf("销毁的头元素为:%d\n", *e); + free(p); + return true; +} +//销毁一个队列 +Status Destory(LinkQueue*Q) +{ + printf("销毁队列\n"); + while (Q->front) + { + Q->rear = Q->front->next; + free(Q->front); + Q->front = Q->rear; + } + return true; +} +Status Traverse(LinkQueue*Q) +{ + if (Q->rear == Q->front) + { + printf("error ,Is a empty queue!\n"); + return false; + } + printf("遍历队列:\n"); + QNodePtr p = Q->front->next; + while (p) + { + printf("%d ", p->data); + p = p->next; + } + printf("\n"); + return true; +} +int main() +{ + int i, result; + LinkQueue Q; + InitQueue(&Q); + for (i = 0; i < 5; i++) + { + EnQueue(&Q, i);//向队列尾部添加元素 + } + Traverse(&Q);//第一次遍历 + DeQueue(&Q, &result);//销毁队列头元素 + Traverse(&Q);//第二次遍历 + Destory(&Q);//销毁队列 + Traverse(&Q);//第三次遍历 + return 0; +} \ No newline at end of file diff --git "a/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2.12.c" "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2.12.c" new file mode 100644 index 00000000..53d44386 --- /dev/null +++ "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2.12.c" @@ -0,0 +1,85 @@ +#include +#include +#include +//限制链表长度 +#define N 4 +#define M 5 +typedef int ElemType; +typedef struct LNode +{ + ElemType data; + struct LNode *next; +}LNode, *LinkList; + +//创建链表 +void CreateList_L(LinkList L, ElemType n) +{ + LinkList p; + ElemType a; + //a = EnterData(n); + L->next = NULL; + for (int i = 0; idata = a; + p->next = L->next; + L->next = p; + } +} +//链表合并 +void MergeList_L(LinkList La, LinkList Lb, LinkList Lc) +{ + LinkList pa, pb, pc; + pa = La->next; + pb = Lb->next; + pc = La; + Lc = La; + while (pa&&pb) + { + if (pa->data <= pb->data) + { + pc->next = pa; + pc = pa; + pa = pa->next; + } + else + { + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + pc->next = pa ? pa : pb; + free(Lb); +} +//遍历链表 +void print(LinkList L) +{ + LinkList p; + p = L->next; + while (p) + { + printf("%4d", p->data); + p = p->next; + } + printf("\n"); +} +int main() +{ + LinkList La, Lb, Lc; + La = (LinkList)malloc(sizeof(LNode)); + Lb = (LinkList)malloc(sizeof(LNode)); + Lc = (LinkList)malloc(sizeof(LNode)); + //两个链表的长度已确定 + printf("第一个链表长度为4,第二个链表长度为5\n"); + CreateList_L(La, N); + CreateList_L(Lb, M); + printf("链表La为:"); + print(La); + printf("\n链表Lb为:"); + print(Lb); + MergeList_L(La, Lb, Lc); + printf("\n合并后结果为:"); + print(La); +} \ No newline at end of file diff --git "a/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (1).png" "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (1).png" new file mode 100644 index 00000000..5e5b9f8a Binary files /dev/null and "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (1).png" differ diff --git "a/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (2).png" "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (2).png" new file mode 100644 index 00000000..08ad3f99 Binary files /dev/null and "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (2).png" differ diff --git "a/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (3).png" "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (3).png" new file mode 100644 index 00000000..05acc39d Binary files /dev/null and "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (3).png" differ diff --git "a/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (4).png" "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (4).png" new file mode 100644 index 00000000..88972910 Binary files /dev/null and "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (4).png" differ diff --git "a/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (5).png" "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (5).png" new file mode 100644 index 00000000..8465dd74 Binary files /dev/null and "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (5).png" differ diff --git "a/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (6).png" "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (6).png" new file mode 100644 index 00000000..aaa6bade Binary files /dev/null and "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-03-31 (6).png" differ diff --git "a/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-04-05.png" "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-04-05.png" new file mode 100644 index 00000000..0b167a64 Binary files /dev/null and "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/2017-04-05.png" differ diff --git "a/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\213\254\345\217\267\345\214\271\351\205\215.c" "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\213\254\345\217\267\345\214\271\351\205\215.c" new file mode 100644 index 00000000..62a7aca6 --- /dev/null +++ "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\346\213\254\345\217\267\345\214\271\351\205\215.c" @@ -0,0 +1,125 @@ +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCRESE 10 +typedef char SElemType; +typedef struct +{ + SElemType*base; + SElemType*top; + int stacksize; +}Sqstack; +typedef enum +{ + error, ok +}Status; +//创建栈 +Status Initstack(Sqstack*S) +{ + S->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!S->base) + { + return error; + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return ok; +} +//判断栈是否为空 +Status StackEmpty(Sqstack *S) +{ + if (S->top == S->base) + { + return ok; + } + else return error; +} +//插入栈顶元素 +Status Push(Sqstack*S, SElemType e) +{ + if (S->top - S->base == S->stacksize) + { + S->base = (SElemType*)realloc(S->base, (S->stacksize + STACKINCRESE) * sizeof(SElemType)); + S->top = S->base + S->stacksize; + S->base = S->base + STACKINCRESE; + } + *S->top++ = e; + return ok; +} +//出栈 +Status Pop(Sqstack *S, SElemType*e) +{ + if (S->top == S->base) + { + return error; + } + *e = *--S->top; + return ok; +} +//括号匹配,匹配函数写的并不好,只有可以匹配的括号连续出现才可以正确匹配 +Status cout(Sqstack *S, SElemType*str) +{ + int i = 0, flag = 0; + SElemType e; + while (str[i] != '\0') + { + switch (str[i]) + { + case '(': + Push(S, str[i]); + break; + + case '{': + Push(S, str[i]); + break; + case '[': + Push(S, str[i]); + break; + case ')': + { + Pop(S, &e); + if (e != '(') + { + flag = 1; + } + } + break; + case ']': + { + Pop(S, &e); + if (e != '[') + { + flag = 1; + } + } + break; + case '}': + { + Pop(S, &e); + if (e != '{') + { + flag = 1; + } + } + break; + } + i++; + } + if (!flag&&StackEmpty(S)) + { + printf("匹配成功!\n"); + } + else + printf("匹配失败!\n"); + + return ok; +} +int main() +{ + printf("输入要匹配的括号:"); + SElemType str[20]; + Sqstack S; + Initstack(&S); + scanf("%s", str); + cout(&S, str); +} \ No newline at end of file diff --git "a/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\350\241\214\347\274\226\350\276\221.c" "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\350\241\214\347\274\226\350\276\221.c" new file mode 100644 index 00000000..99561314 --- /dev/null +++ "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\350\241\214\347\274\226\350\276\221.c" @@ -0,0 +1,114 @@ + +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCRESE 10 +typedef char SElemType; +typedef struct +{ + SElemType*base; + SElemType*top; + int stacksize; +}Sqstack; +typedef enum +{ + error, ok +}Status; +//创建栈 +Status Initstack(Sqstack*S) +{ + S->base = (SElemType*)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!S->base) + { + return error; + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return ok; +} +//判断栈是否为空 +Status StackEmpty(Sqstack *S) +{ + if (S->top == S->base) + { + return ok; + } + else return error; +} +//插入栈顶元素 +Status Push(Sqstack*S, SElemType e) +{ + if (S->top - S->base == S->stacksize) + { + S->base = (SElemType*)realloc(S->base, (S->stacksize + STACKINCRESE) * sizeof(SElemType)); + S->top = S->base + S->stacksize; + S->base = S->base + STACKINCRESE; + } + *S->top++ = e; + return ok; +} +//出栈 +Status Pop(Sqstack *S, SElemType*e) +{ + if (S->top == S->base) + { + return error; + } + *e = *--S->top; + return ok; +} +// 栈清空 +Status ClearStack(Sqstack*S) +{ + S->top = S->base; + return ok; +} +//销毁栈 +Status DestroyStack(Sqstack*S) +{ + S->top = S->base; + free(S->base); + return ok; +} +void LineEdit(Sqstack *S) +{ + int i = 0; + + SElemType *c = (SElemType*)malloc(sizeof(SElemType)); + SElemType ch; + SElemType *p = (SElemType*)malloc(sizeof(SElemType)); + ch = getchar(); + while (ch != EOF) + { + while (ch != '\n') + { + switch (ch) + { + case '#': Pop(S, c); break;//退栈 + case '@':ClearStack(S); break;//清空栈 + default:Push(S, ch);//有效字符进栈 + } + ch = getchar(); + } + p = S->base; + while(p!=S->top) + { + printf("%c", *p); + p++; + } + ClearStack(S); + if (ch != EOF) + { + ch = getchar(); + } + + } + DestroyStack(S); +} +int main() +{ + printf("#删去前一个字符\n@删去前一行字符\n"); + Sqstack S; + Initstack(&S); + LineEdit(&S); +} \ No newline at end of file diff --git "a/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\350\277\233\345\210\266\350\275\254\345\214\226.c" "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\350\277\233\345\210\266\350\275\254\345\214\226.c" new file mode 100644 index 00000000..038feb01 --- /dev/null +++ "b/2017-1/ywy/\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232/\350\277\233\345\210\266\350\275\254\345\214\226.c" @@ -0,0 +1,96 @@ +#include +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +typedef int SElemType; +typedef struct +{ + SElemType *base; + SElemType *top; + int stacksize; +}Sqstack; +typedef enum +{ + ok, error, overflow +}Status; +typedef enum +{ + false, true +}bool; +//创建一个空栈 +Status InitStack(Sqstack *S) +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if (!S->base) + { + return overflow; + } + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return ok; +} +//插入新元素 +Status Push(Sqstack *S, SElemType e) +{ + if (S->top - S->base >= S->stacksize) + { + S->base = (SElemType*)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return ok; +} +//取出栈顶元素并删除 +Status Pop(Sqstack *S, SElemType *e) +{ + if (S->top == S->base) + { + return error; + } + *e = *--S->top; + return ok; +} +//检测S是否为空栈 +bool StackEmpty(Sqstack S) +{ + if (S.top == S.base) + { + return true; + } + else + { + return false; + } +} + +//进行进制转换 +void conversion(Sqstack *S, int N, int d) +{ + SElemType *e = (SElemType*)malloc(sizeof(SElemType)); + while (N) + { + Push(S, N%d); + N = N / d; + } + while (!StackEmpty(*S)) + { + Pop(S, e); + printf("%d", *e); + + } +} +int main() +{ + int N, d; + printf("\n"); + Sqstack S; + InitStack(&S); + srand((unsigned)(time)(NULL)); + N = rand() % 1024; + d = rand() % 10; + printf("将十进制数%d转化为%d进制数为:\n", N, d); + conversion(&S, N, d); + return 0; +} \ No newline at end of file diff --git "a/2017-1/ywy/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/2017-05-12.png" "b/2017-1/ywy/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/2017-05-12.png" new file mode 100644 index 00000000..c2ab52d9 Binary files /dev/null and "b/2017-1/ywy/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/2017-05-12.png" differ diff --git "a/2017-1/ywy/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/erchas.cpp" "b/2017-1/ywy/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/erchas.cpp" new file mode 100644 index 00000000..c2ee2d49 --- /dev/null +++ "b/2017-1/ywy/\347\254\254\345\205\255\346\254\241\344\275\234\344\270\232/erchas.cpp" @@ -0,0 +1,140 @@ +#include +#include +int count = 0; +int jiedian = 0;//总节点数 +typedef char ElemType; +typedef struct BiNode +{ + ElemType data; + struct BiNode *lchild, *rchild; +} BiNode, *BiTree; +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +Status CreatBiTree(BiTree *T, ElemType *ar) +{ + ElemType ch; + ch = ar[count]; + count++; + if (ch == '#') + { + *T = NULL;//节点没有子树 + } + else + { + jiedian++; + if (!(*T = (BiTree)malloc(sizeof(BiNode)))) + { + return OVERFLOW;//内存分配失败 + } + (*T)->data = ch;//生成根节点 + CreatBiTree(&(*T)->lchild, ar);//创建左子树 + CreatBiTree(&(*T)->rchild, ar); //创建右子树 + } + return OK; +} +void PostOrderTraverse(BiTree T)//后序遍历及输出 +{ + if (T == NULL) + { + return; + } + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c", T->data); +} +int depth(BiTree T)//计算深度 +{ + int ldepth, rdepth; + if (T == NULL) + { + return 0; + } + ldepth = depth(T->lchild); + rdepth = depth(T->rchild); + if (ldepth > rdepth) + { + return ldepth + 1; + } + else + { + return rdepth + 1; + } +} +int j = 0; +int max = 0;//最大宽度 +int counter[20];//每层宽度 +int width(BiTree T) +{ + if (T == NULL) + { + return 0; + } + j++; + counter[j]++; + if (max < counter[j]) + { + max = counter[j]; + } + width(T->lchild); + width(T->rchild); + j--; + return max; +} +int leaf_num(BiTree T)//叶子节点 +{ + int l, r; + if (T == NULL) + { + return 0; + } + if (T->lchild == NULL&&T->rchild == NULL) + { + return 1; + } + else + { + l = leaf_num(T->lchild); + r = leaf_num(T->rchild); + return l + r; + } +} + +int main() +{ + BiTree T, R; + ElemType str1[30] = "ABDG###EH##I#K##C#F##"; + ElemType str2[30] = "ABD#F###CE###"; + CreatBiTree(&T, str1); + + printf("用例-1:ABDG###EH##I#K##C#F##:\n"); + printf("深度为:%d\n最大宽度为:%d\n", depth(T), width(T)); + printf("输出: "); + PostOrderTraverse(T); + printf("\n"); + printf("叶子节点数:%d\n", leaf_num(T)); + printf("非叶子节点数:%d", jiedian - leaf_num(T)); + printf("\n\n"); + + count = 0; + jiedian = 0; + max = 0; + int g; + for (g = 0; g < 20; g++) + { + counter[g] = 0; + } + CreatBiTree(&R, str2); + printf("用例-2:ABD#F###CE###:\n"); + printf("深度为:%d\n最大宽度为:%d\n", depth(R), width(R)); + printf("输出: "); + PostOrderTraverse(R); + printf("\n"); + printf("叶子节点数:%d\n", leaf_num(R)); + printf("非叶子节点数:%d", jiedian - leaf_num(R)); + printf("\n"); +} \ No newline at end of file diff --git "a/2017-1/ywy/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/2017-04-09 (1).png" "b/2017-1/ywy/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/2017-04-09 (1).png" new file mode 100644 index 00000000..8a7476ee Binary files /dev/null and "b/2017-1/ywy/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/2017-04-09 (1).png" differ diff --git "a/2017-1/ywy/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/List.c" "b/2017-1/ywy/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/List.c" new file mode 100644 index 00000000..16a5b7fa --- /dev/null +++ "b/2017-1/ywy/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/List.c" @@ -0,0 +1,77 @@ +#include +#include +#include +#include"List.h" +Status CreatList(Linklist L,int n) +{ + printf("创建长度为%d的链表:\n",n); + L->next = NULL; + Linklist p; + Elemtype i; + for (i = 0; i < n; i++) + { + p = (Linklist)malloc(sizeof(Node)); + p->data = i; + p->next = L->next; + L->next = p; + } + return ok; +} +Status TravelList(Linklist L) +{ + printf("遍历链表:\n"); + Linklist p ; + p = L->next; + while (p) + { + printf("%d ", p->data); + p = p->next; + } + printf("\n"); + return ok; +} +Status chaifen(Linklist List, Linklist L1, Linklist L2) +{ + int count = 0; + printf("拆分链表: \n"); + L1->data = 0; + L2->data = 0; + L1->next = L2->next = NULL; + Linklist p = List->next; + while (p) + { + Linklist temp = (Linklist)malloc(sizeof(Node)); + count++; + if (count % 2 == 1)//序号为奇数 + { + L1->data++; + temp->data = p->data; + temp->next = L1->next; + L1->next = temp; + } + if (count % 2 == 0)//序号为偶数 + { + L2->data++; + temp->data = p->data; + temp->next = L2->next; + L2->next = temp; + } + p = p->next; + } + + return ok; +} +int main() +{ + int n = 10; + Linklist L=(Linklist)malloc(sizeof(Node)); + Linklist L1 = (Linklist)malloc(sizeof(Node)); + Linklist L2 = (Linklist)malloc(sizeof(Node)); + CreatList (L,n); + TravelList(L); + chaifen(L, L1, L2); + printf("奇数序号元素有: %d位", L1->data); + TravelList(L1); + printf("偶数序号元素有: %d位", L2->data); + TravelList(L2); +} \ No newline at end of file diff --git "a/2017-1/ywy/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/List.h" "b/2017-1/ywy/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/List.h" new file mode 100644 index 00000000..2afb0407 --- /dev/null +++ "b/2017-1/ywy/\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232/List.h" @@ -0,0 +1,17 @@ +#pragma once +#include +#include +typedef enum { + ok, + error, + overflow +}Status; +typedef int Elemtype; +typedef struct Node +{ + Elemtype data; + struct Node*next; +}Node,*Linklist; +Status CreatList(Linklist L,int n); +Status TravelList(Linklist L); +Status chaifenList(Linklist L,Linklist L1,Linklist L2); diff --git "a/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/2017-06-18 (3).png" "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/2017-06-18 (3).png" new file mode 100644 index 00000000..a42e035d Binary files /dev/null and "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/2017-06-18 (3).png" differ diff --git "a/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/Sort.c" "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/Sort.c" new file mode 100644 index 00000000..319bcc09 --- /dev/null +++ "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/Sort.c" @@ -0,0 +1,179 @@ +#include"Sort.h" +void swap(SqList*L, int i, int j) +{ + RedType temp = L->r[i]; + L->r[i] = L->r[j]; + L->r[j] = temp; +} + +void ShellSort(SqList*L) +{ + int i, j; + int count1 = 0, count2 = 0; + int rise = L->length; + do + { + rise = rise / 5 + 1; + for (i = rise + 1; i <= L->length; i++) + { + count1++; + if (LT(L->r[i].key, L->r[i - rise].key)) + { + L->r[0] = L->r[i]; + count2++; + for (j = i - rise; LT(L->r[0].key, L->r[j].key) && j > 0; j = j - rise) + { + count1++; + L->r[j + rise] = L->r[j]; + count2++; + } + count1++; + L->r[j + rise] = L->r[0]; + count2++; + } + } + } while (rise > 1); + printf("希尔排序为: "); + for (i = 1; i <= L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d 二者之和: %d\n", count1, count2, count1 + count2); + printf("\n"); +} +void InsertSort(SqList*L) +{ + int i, j; + int count1 = 0; + int count2 = 0; + for (i = 2; i <= L->length; i++) + { + ++count1; + if (LT(L->r[i].key, L->r[i - 1].key)) + { + L->r[0] = L->r[i]; + count2++; + //L->r[i] = L->r[i - 1]; + for (j = i - 1; LT(L->r[0].key, L->r[j].key); --j) + { + count1++; + count2++; + L->r[j + 1] = L->r[j]; + } + count1++; + L->r[j + 1] = L->r[0]; + count2++; + } + } + printf("插入排序后为:"); + for (i = 1; i <= L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d 二者之和: %d\n", count1, count2, count1 + count2); + printf("\n"); +} +void BubbleSort(SqList*L) +{ + int i, j; + int count1 = 0; + int count2 = 0; + for (i = 1; i < L->length; i++) + { + for (j = i + 1; j < L->length; j++) + { + count1++; + if (LT(L->r[j].key, L->r[i].key)) + { + swap(L, i, j); + count2 += 3; + } + } + } + printf("冒泡排序后为:"); + for (i = 1; ilength; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d 二者之和: %d\n", count1, count2, count1 + count2); + printf("\n"); +} +int Partition(SqList*L, int low, int high, int*count1, int*count2) +{ + int pivotkey; + //L->r[0] = L->r[low]; + pivotkey = L->r[low].key; + while (low < high) + { + while (L->r[high].key >= pivotkey && low < high) + { + (*count1)++; + high--; + } + (*count1)++; + swap(L, low, high); + (*count2) += 3; + while (L->r[low].key <= pivotkey && low < high) + { + (*count1)++; + low++; + } + (*count1)++; + swap(L, low, high); + (*count2) += 3; + } + return low; +} + +void QSort(SqList*L, int low, int high, int*count1, int*count2) +{ + int pivot; + if (low < high) + { + pivot = Partition(L, low, high, count1, count2); + QSort(L, low, pivot - 1, count1, count2); + QSort(L, pivot + 1, high, count1, count2); + } +} +void QuickSort(SqList*L) +{ + int i; + int count1 = 0, count2 = 0; + QSort(L, 1, L->length, &count1, &count2); + printf("快速排序后为:"); + for (i = 1; i <= L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d 二者之和: %d\n", count1, count2, count1 + count2); +} +void SelectSort(SqList*L) +{ + int i, j, min; + int count1 = 0; + int count2 = 0; + for (i = 1; i < L->length; i++) + { + min = i; + for (j = i + 1; j <= L->length; j++) + { + count1++; + if (LT(L->r[j].key, L->r[min].key)) + { + min = j; + } + } + if (i != min) + { + swap(L, i, min); + count2 += 3; + } + } + printf("\n简单选择排序为:"); + for (i = 1; i <= L->length; i++) + { + printf("%d ", L->r[i].key); + } + printf("\n比较次数:%d 交换次数: %d 二者之和: %d\n", count1, count2, count1 + count2); + printf("\n"); +} diff --git "a/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/Sort.h" "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/Sort.h" new file mode 100644 index 00000000..b36753ec --- /dev/null +++ "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/Sort.h" @@ -0,0 +1,27 @@ +#pragma once +#include +#include +#include +typedef int KeyType; +typedef int InfoType; +#define maxsize 20 +#define len 10 +#define LT(a,b) ((a)<(b)) +typedef struct +{ + KeyType key; + InfoType otherinfo; +}RedType; +typedef struct +{ + RedType r[maxsize + 1]; + int length; +}SqList; +void swap(SqList*L, int i, int j); +void ShellSort(SqList*L); +void InsertSort(SqList*L); +void BubbleSort(SqList*L); +void QuickSort(SqList*L); +void QSort(SqList*L, int low, int high, int*count1, int*count2); +int Partition(SqList*L, int low, int high, int*count1, int*count2); +void SelectSort(SqList*L); diff --git "a/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/test.c" "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/test.c" new file mode 100644 index 00000000..a59eb1d7 --- /dev/null +++ "b/2017-1/ywy/\347\272\240\346\255\243\347\211\210\346\216\222\345\272\217/test.c" @@ -0,0 +1,51 @@ +锘#include"Sort.h" + +//#include"Sort.c" + +int main() + +{ + + SqList L, L1, L2, L3, L4; + + L.length = len; + + int i = 0; + + srand((unsigned)time(NULL)); + + for (i = 1; i <= len; i++) + + { + + L.r[i].key =rand()%15; + + } + + printf("\n鍘熷簭锛"); + + for (i = 1; i - -#define MAX 100 -#define DEBUG 1 - -void scan(ElemType *p){ - scanf_s("%d",p); -} -void print(const ElemType n){ - printf("%2d ", n); -} - -/*算法2.11 新建单链表*/ -void CreateList_L(LinkList L, int n, void(*func)(ElemType*)){ - DEBUG && printf("\nCreateList_L\n"); - DEBUG && printf("开始创建长度为%d的单链表\n",n); - int i; - LNode* p; - - L->next = NULL; - DEBUG && printf("L->next=NULL\n"); - - for (i = 0; i < n; ++i){ - DEBUG && printf("创建第%d个节点\n",i+1); - p = (LinkList)malloc(sizeof(LNode)); - DEBUG && printf("使用malloc函数为指针p分配了空间,p=%X\n",p); - func(&p->data); - DEBUG && printf("p->data=%d\n",p->data); - p->next = L->next; - DEBUG && printf("p->next=L->next(%X)\n",L->next); - L->next = p; - DEBUG && printf("L->next=p(%X)\n",p); - }//end for - DEBUG && printf("end CreateList_L\n"); -};//CraeteList_L -/*算法2.12 归并两个单链表*/ -void MergeList_L(LinkList La, LinkList Lb, LinkList *Lc){ - DEBUG && printf("MergeList_L\n"); - LNode *pa = La->next, *pb = Lb->next, *pc; - (*Lc) = pc = La;//用La的头节点作为Lc的头节点 - DEBUG && printf("pa=La->next(%X),pb=Lb->next(%X), Lc=pc=La(%X)\n",La->next,Lb->next,La); - - while (pa&&pb){ - DEBUG && printf("pa与pb均不为空,循环继续\n"); - DEBUG && printf("pa->data(%d)%spb->data(%d),所以\n",pa->data,pa->data <= pb->data ? "<=" : ">",pb->data); - if (pa->data <= pb->data){ - DEBUG && printf("pc->next=pa(%X),pc=pa(%X),pa=pa->next(%X)\n",pa,pa,pa->next); - pc->next = pa; pc = pa; pa = pa->next; - } - else{ - DEBUG && printf("pc->next=pb(%X),pc=pb(%X),pb=pb->next(%X)\n", pb, pb, pb->next); - pc->next = pb; pc = pb; pb = pb->next; - }//end if else - }//end while - DEBUG && printf("pa与pb中至少有一个为空,循环中止\n"); - pc->next = pa ? pa : pb; - DEBUG && printf("%s为空,所以pc->next=%s(%X)\n", pa ? "pb" : "pa", pa ? "pa" : "pb", pa ? pa : pb); - free(Lb); - DEBUG && printf("释放Lb\n"); -} -/*遍历单链表*/ -void TraverseList_L(const LinkList L, void(*func)(ElemType)){ - LNode *p = L->next; - while (p){ - func(p->data); - p = p->next; - } -} \ No newline at end of file diff --git a/2017-1/zHong/LinkList.h b/2017-1/zHong/LinkList.h deleted file mode 100644 index 61694617..00000000 --- a/2017-1/zHong/LinkList.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once -#include - -typedef int ElemType; - -typedef struct LNode{ - ElemType data; - struct LNode* next; -}LNode,*LinkList; - - -void scan(ElemType *); -void print(const ElemType); - -void CreateList_L(LinkList , int , void(*)(ElemType*)); -void MergeList_L(LinkList, LinkList, LinkList); -void TraverseList_L(const LinkList, void(*)(ElemType)); \ No newline at end of file diff --git a/2017-1/zHong/test.c b/2017-1/zHong/test.c deleted file mode 100644 index cfb6dd24..00000000 --- a/2017-1/zHong/test.c +++ /dev/null @@ -1,44 +0,0 @@ -#include"LinkList.h" -#include -#include - -#define LEN 5 - -//以下4个函数用于产生有序测试数据 -int test1_iter(int k){ - static int b = 20; - return b -= k; -} -void test1(ElemType *p){ - *p = test1_iter(rand() % 5+1); -} -int test2_iter(int k){ - static int b = 20; - return b -= k; -} -void test2(ElemType *p){ - *p = test2_iter(rand() % 5+1); -} - -int main(void){ - LinkList La, Lb, Lc; - srand(time(0)); - - La = (LinkList)malloc(sizeof(LNode)); - CreateList_L(La, LEN+rand()%5, test1); - Lb = (LinkList)malloc(sizeof(LNode)); - CreateList_L(Lb, LEN + rand() % 5, test2); - - printf("\nLa:"); - TraverseList_L(La, print); - printf("\n"); - printf("Lb:"); - TraverseList_L(Lb, print); - printf("\n\n"); - - MergeList_L(La, Lb, &Lc); - printf("Lc:"); - TraverseList_L(Lc, print); - printf("\n"); - return 0; -} \ No newline at end of file diff --git "a/2017-1/zHong/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/zHong/\350\277\220\350\241\214\347\273\223\346\236\234.png" deleted file mode 100644 index 397c9c25..00000000 Binary files "a/2017-1/zHong/\350\277\220\350\241\214\347\273\223\346\236\234.png" and /dev/null differ diff --git a/2017-1/zkx/2-12.cpp b/2017-1/zkx/2-12.cpp new file mode 100644 index 00000000..a4c6dd4d --- /dev/null +++ b/2017-1/zkx/2-12.cpp @@ -0,0 +1,113 @@ +#include +#include +#include + +typedef int ElemType; //声明变量类型 + +typedef struct LNode +{ + ElemType data; + struct LNode *next; +}LNode , *LinkList; //结构体的建立 + +#define OK 0 +#define ERROR 1 +#define OVERFLOW 2 //预定义常量 + +void CreateList1(LinkList L, int n) +{ + int i; + int base=20; + LNode* p; + L->next = NULL; //带头结点的单链表 + srand(time(NULL)); + for (i = 0; i < n; ++i) + { + p = (LinkList)malloc(sizeof(LNode)); + p->data=base-=rand()%5+1; + p->next = L->next; //插入到表头 + L->next = p; + } +}; +void CreateList2(LinkList L, int n) +{ + int i; + int base=20; + LNode* p; + L->next = NULL; //带头结点的单链表 + srand(time(NULL)); + for (i = 0; i < n; ++i) + { + p = (LinkList)malloc(sizeof(LNode)); + p->data=base-=rand()%6+1; + p->next = L->next; //插入到表头 + L->next = p; + } +}; +void MergeList_L(LinkList La, LinkList Lb, LinkList *Lc) +{ + LNode *pa = La->next, *pb = Lb->next, *pc; + (*Lc) = pc = La;//用La的头节点作为Lc的头节点 + + while (pa&&pb) + { + if (pa->data <= pb->data) + { + pc->next = pa; + pc = pa; + pa = pa->next; + } + else + { + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + pc->next = pa ? pa : pb; + free(Lb); +} +void TraverseList_L(LinkList L) +{ + LNode *p = L->next; + while (p) + { + printf("%d ",p->data); + p = p->next; + } +} +int main() +{ + int n1,n2; + srand(time(NULL)); + n1=5+rand()%5; + printf("输入链表La的数据个数:%d \n",n1); + + n2=5+rand()%5; + printf("输入链表Lb的数据个数:%d \n",n2); + + + LinkList La,Lb,Lc; + La = (LinkList)malloc(sizeof(LNode)); + Lb = (LinkList)malloc(sizeof(LNode)); + //srand(time(NULL)); + + printf("输入La数据:\n"); + CreateList1(La,n1); //创建La + printf("La:"); //输出La + TraverseList_L(La); + printf("\n"); + + printf("输入Lb数据:\n"); + CreateList2(Lb,n2); //创建Lb + printf("Lb:"); //输出Lb + TraverseList_L(Lb); + printf("\n"); + + MergeList_L(La, Lb, &Lc); //合并La,Lb + printf("合并La和Lb,生成新链表Lc:\n"); + printf("Lc:"); //输出合并后Lc + TraverseList_L(Lc); + printf("\n"); + return 0; +} diff --git "a/2017-1/zkx/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTOutput.txt" "b/2017-1/zkx/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTOutput.txt" new file mode 100644 index 00000000..e9730024 --- /dev/null +++ "b/2017-1/zkx/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTOutput.txt" @@ -0,0 +1,6 @@ +8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30 +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30 +7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30 diff --git "a/2017-1/zkx/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTree.c" "b/2017-1/zkx/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTree.c" new file mode 100644 index 00000000..626d7cb2 --- /dev/null +++ "b/2017-1/zkx/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTree.c" @@ -0,0 +1,188 @@ +#include "BSTree.h" + + + //在根指针 T 所指二叉排序树中递归地查找其关键字等于 key 的数据元素,若查找成功, + //则返回指针 p 指向该数据元素的结点,并返回函数值为 TRUE; 否则表明查找不成功,返回 + //指针 p 指向查找路径上访问的最后一个结点,并返回函数值为FALSE, 指针 f 指向当前访问 + //的结点的双亲,其初始调用值为NULL +Status SearchBST(BiTree T, ElemType key, BiTree f, BiTree *p) +{ + if (!T) //查找不成功 + { + *p = f; + return FALSE; + } + else if (EQ(key, T->data)) //查找成功 + { + *p = T; + return TRUE; + } + else if (LT(key, T->data)) //在左子树中继续查找 + return SearchBST(T->lchild, key, T, p); + else //在右子树中继续查找 + return SearchBST(T->rchild, key, T, p); +} +// 当二叉排序树中不存在关键字等于 e.key 的数据元素时,插入元素值为 e 的结点,并返回 TRUE; 否则,不进行插入并返回FALSE +Status InsertBST(BiTree *T, ElemType e) +{ + BiTree p = NULL, s = NULL; + + if (SearchBST(*T, e, NULL, &p)==FALSE) + { + s = (BiTree)malloc(sizeof(BiTNode)); // 为新结点分配空间 + s->data = e; + s->lchild = s->rchild = NULL; + if (!p) + { + *T = s; // 插入 s 为新的根结点 + } + else if (LT(e, p->data)) + { + p->lchild = s; // 插入 *s 为 *p 的左孩子 + } + else + { + p->rchild = s; // 插入 *s 为 *p 的右孩子 + } + return TRUE; + } + else + { + return FALSE; + } +} + +Status PreOrderTraverse(BiTree T) //先序遍历二叉排序树 +{ + + if (T) + { + visit[u++] = T->data; + PreOrderTraverse(T->lchild); + PreOrderTraverse(T->rchild); + } + return OK; +} + + +//从二叉排序树中删除结点p,并重接他的左或右子树 +Status Delete(BiTree *p) +{ + BiTree q = NULL, s = NULL; + if (!(*p)->rchild) //右子树空只需重接它的左子树 + { + q = *p; + *p = (*p)->lchild; + free(q); + } + else if (!(*p)->lchild) //左子树为空只需重接它的右子树 + { + q = *p; + *p = (*p)->rchild; + free(q); + } + else //左右子树均不为空 + { + q = *p; + s = (*p)->lchild; + while (s->rchild) //转左。然后向右走到尽头 + { + q = s; + s = s->rchild; + } + (*p)->data = s->data; //s指向被删除结点的前驱 + if (q != *p) + { + q->rchild = s->lchild; //重接*q的右子树 + } + else + { + q->lchild = s->lchild; //重接*q的左子树 + } + free(s); + } + return TRUE; +} +// 若二叉排序树 T 中存在其关键字等于 key 的数据元素,则删除该数据元素结点,并返回函数值 TRUE,否则返回函数值 FALSE +Status DeleteBST(BiTree *T, ElemType key) +{ + if (!(*T)) // 不存在关键字等于key的数据元素 + { + return FALSE; + } + else + { + if (EQ(key, (*T)->data)) // 找到关键字等于key的数据元素 + { + Delete(T); + return TRUE; + } + else if (LT(key, (*T)->data)) // 继续在左子树中进行查找 + { + return DeleteBST(&(*T)->lchild, key); + } + else // 继续在右子树中进行查找 + { + return DeleteBST(&(*T)->rchild, key); + } + } +} + +Status print(BiTree T, FILE *fp) //.txt格式输出 +{ + char x[4] = ", "; + int i; + for (i = 0; i < 25; i++) + { + visit[i] = -1; + } + PreOrderTraverse(T); + for (i = 0;; i++) + { + if (visit[i] == -1) + { + break; + } + if (i != 0) + { + fputs(x, fp); + } + fprintf(fp, "%d", visit[i]); + } + return 0; +} + +int main() +{ + FILE *fp; + char c[4] = "\n"; + fp = fopen("BSTOutput.txt", "a"); + ElemType input[25] = { 8, 10,14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + ElemType search[10] = { 13, 8, 5, 20, 6 }; + BiTree T = NULL; + BiTree p = NULL; + int i; + for (i = 0; i < 12; i++) + { + InsertBST(&T, input[i]); + } + print(T, fp); //建立查找表之后输出这个查找表 + fputs(c, fp); //换行 + for (i = 0; i < 5; i++) + { + u = 0; //初始化计数的数字 + if (SearchBST(T, search[i], NULL, &p) == FALSE) //若果查找失败,则插入这个数据 + { + InsertBST(&T, search[i]); + } + else //如果查找成功,则删除这个数据 + { + DeleteBST(&T, search[i]); + } + print(T, fp); + fputs(c, fp); + } + + fclose(fp); + return 0; +} \ No newline at end of file diff --git "a/2017-1/zkx/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTree.h" "b/2017-1/zkx/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTree.h" new file mode 100644 index 00000000..8218c22f --- /dev/null +++ "b/2017-1/zkx/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221/BSTree.h" @@ -0,0 +1,45 @@ +#include +#include + +#define EQ(a,b) (a==b) +#define LT(a,b) (adata = ch; // 生成根结点 + CreateBiTree(&(*T)->lchild, p); // 构造左子树 + CreateBiTree(&(*T)->rchild, p); // 构造右子树 + } + return OK; +} + +void PostOrderTraverse(BiTree T) +{ + if (T == NULL) + { + return; + } + PostOrderTraverse(T->lchild); + PostOrderTraverse(T->rchild); + printf("%c", T->data); + +} + +int main() +{ + BiTree T = NULL; + Status ret; + TElemType str1[50] = "ABDG###EH##I#K##C#F##"; + TElemType str2[50] = "ABD#F###CE###"; + + printf("----------------二叉树的建立和后序输出----------------\n"); + + ret = CreateBiTree(&T,str1); + printf("处理第一个二叉树 : ABDG###EH##I#K##C#F##\n"); + printf("后序遍历结果为 : "); + PostOrderTraverse(T); + + i = 0; //重新将计数变量置为0,进行第二个字符串的处理 + ret = CreateBiTree(&T, str2); + printf("\n处理第二个二叉树 : ABD#F###CE###\n"); + printf("后序遍历结果为 : "); + PostOrderTraverse(T); + printf("\n"); + + return 0; + +} \ No newline at end of file diff --git "a/2017-1/zkx/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/tree.h" "b/2017-1/zkx/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/tree.h" new file mode 100644 index 00000000..0afd5c7f --- /dev/null +++ "b/2017-1/zkx/\344\272\214\345\217\211\346\240\221\345\273\272\347\253\213/tree.h" @@ -0,0 +1,22 @@ +#include +#include + +typedef char TElemType; + +typedef struct BiTNode // 结点结构 +{ + TElemType data; + struct BiTNode *lchild, *rchild; // 左右孩子指针 +} BiTNode, *BiTree; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + + +Status CreateBiTree(BiTree *T, TElemType *p); + +void PostOrderTraverse(BiTree T); diff --git "a/2017-1/zkx/\344\272\214\345\217\211\346\240\221\345\277\205\351\200\2113\345\222\214\345\217\257\351\200\2115/addtree.c" "b/2017-1/zkx/\344\272\214\345\217\211\346\240\221\345\277\205\351\200\2113\345\222\214\345\217\257\351\200\2115/addtree.c" new file mode 100644 index 00000000..ab590fbe --- /dev/null +++ "b/2017-1/zkx/\344\272\214\345\217\211\346\240\221\345\277\205\351\200\2113\345\222\214\345\217\257\351\200\2115/addtree.c" @@ -0,0 +1,123 @@ +#include "addtree.h" + +int j=0; +Status CreateBiTree(BiTree *T, TElemType *p) +{ + TElemType ch; + + ch = p[j]; + j++; //用数组形式处理字符串,不用人为输入 + if (ch == '#') //空孩子 + { + *T = NULL; + } + else + { + if (!(*T = (BiTree)malloc(sizeof(BiTNode)))) + { + return OVERFLOW; + } + (*T)->data = ch; // 生成根结点 + CreateBiTree(&(*T)->lchild, p); // 构造左子树 + CreateBiTree(&(*T)->rchild, p); // 构造右子树 + } + return OK; +} + +ElemType Depth(BiTree T) //计算二叉树的深度 +{ + if(T == NULL) + { + return OVERFLOW; + } + return Depth(T->lchild) > Depth(T->rchild) + ? (Depth(T->lchild) + 1) : (Depth(T->rchild) + 1); +} + +int count[50]; //存放各层结点数 +int max=0; //最大宽度 + int i=0; //计数变量 + +int Width(BiTree T) //计算二叉树最大宽度 +{ + if(T == NULL) + return 0; + i++; + count[i]++; + if(max < count[i]) + max = count[i]; + Width(T->lchild); + Width(T->rchild); + i--; //返回根结点 + return max; +} + +ElemType CountLeaf(BiTree T) //计算二叉树叶子结点个数 +{ + ElemType m, n; //分别为左子树,右子树叶子结点个数 + if(T == NULL) + return 0; + if(T->lchild == NULL && T->rchild == NULL) //左右孩子都为空,说明该节点为叶子结点 + { + return 1; + } + else + { + m = CountLeaf(T->lchild); + n = CountLeaf(T->rchild); + return m+n; + } +} + +ElemType Count(BiTree T) //二叉树所有结点个数 +{ + ElemType m, n; + if (T == NULL) + return 0; + if (T->lchild == NULL && T->rchild == NULL) //左右孩子都为空,说明该节点为叶子结点 + { + return 1; + } + else + { + m = Count(T->lchild); + n = Count(T->rchild); + return m + n + 1; + } +} + +ElemType NotCountLeaf(BiTree T) //二叉树非叶子结点个数 +{ + return Count(T)-CountLeaf(T); +} + +int main() +{ + BiTree T = NULL; + ElemType w1, w2; //树1树2的宽度 + + TElemType str1[50] = "ABDG###EH##I#K##C#F##"; + TElemType str2[50] = "ABD#F###CE###"; + + printf("---------计算整个二叉树高度、最大宽度、叶子结点、非叶子结点---------\n\n"); + + printf("第一个二叉树: "); + puts(str1); + CreateBiTree(&T,str1); + printf("高度为: %d",Depth(T)-1); + w1 = Width(T); + printf("\n最大宽度为: %d", w1); + printf("\n叶子结点个数为: %d", CountLeaf(T)); + printf("\n非叶子结点个数为: %d\n\n", NotCountLeaf(T)); + + j=0; //重新计数 + printf("第二个二叉树: "); + puts(str2); + CreateBiTree(&T,str2); + printf("高度为: %d",Depth(T)-1); + w2 = Width(T); + printf("\n最大宽度为: %d", w2-w1); + printf("\n叶子结点个数为: %d", CountLeaf(T)); + printf("\n非叶子结点个数为: %d\n\n", NotCountLeaf(T)); + return 0; +} diff --git "a/2017-1/zkx/\344\272\214\345\217\211\346\240\221\345\277\205\351\200\2113\345\222\214\345\217\257\351\200\2115/addtree.h" "b/2017-1/zkx/\344\272\214\345\217\211\346\240\221\345\277\205\351\200\2113\345\222\214\345\217\257\351\200\2115/addtree.h" new file mode 100644 index 00000000..017d9efe --- /dev/null +++ "b/2017-1/zkx/\344\272\214\345\217\211\346\240\221\345\277\205\351\200\2113\345\222\214\345\217\257\351\200\2115/addtree.h" @@ -0,0 +1,32 @@ +#include +#include + +typedef char TElemType; +typedef int ElemType; + +typedef struct BiTNode +{ + TElemType data; + struct BiTDode *lchild,*rchild; +}BiTNode,*BiTree; + +typedef enum +{ + OK, + OVERFLOW, + ERROR +}Status; + +Status CreateBiTree(BiTree *T, TElemType *p); + +ElemType Depth(BiTree T); + +ElemType Width(BiTree T); + +ElemType CountLeaf(BiTree T); + +ElemType Count(BiTree T); + +ElemType NotCountLeaf(BiTree T); + + diff --git "a/2017-1/zkx/\345\223\210\345\270\214\350\241\250/Hash.c" "b/2017-1/zkx/\345\223\210\345\270\214\350\241\250/Hash.c" new file mode 100644 index 00000000..dc50250a --- /dev/null +++ "b/2017-1/zkx/\345\223\210\345\270\214\350\241\250/Hash.c" @@ -0,0 +1,141 @@ +#include "Hash.h" + +ElemType InitElemType(KeyType key, ValueType value) +{ + ElemType e = { key,value }; + return e; +} + +HashTable InitHash(int size) //初始化一个哈希表 +{ + HashTable H; + H.sizeindex = size; + H.count = 0; + H.elem = (ElemType*)malloc(size * sizeof(ElemType)); + for (int i = 0; i < size; i++) + { + H.elem[i].key = -1; + H.elem[i].val = 0; + } + return H; +} + +bool isPrime(int num) //判断一个数是否为素数 +{ + for (int i = 2; i < num; i++) + { + if (num%i == 0) + { + return false; + } + return true; + } +} + +int getPrimenumber(HashTable H) //找到合适的小于表长的素数n +{ + int n; + n = H.sizeindex; + while (!isPrime(n)) + { + n--; + } + return n; +} + +void InsertHashTable(HashTable H, ElemType elem) +{ + DEBUG && printf("插入元素{ %d -> %d },", elem.key, elem.val); + int n = getPrimenumber(H); + int pos = elem.key % n; + int collisionTimes = 0; + while (H.elem[pos].key != -1) + { + collisionTimes+=1; + pos++; + pos %= H.sizeindex; + } + H.elem[pos] = elem; + H.count++; + DEBUG && printf("位置 %d \n", pos, elem.key, elem.val); +} + +int Find(HashTable H, KeyType key) +{ + DEBUG && printf("查找关键字 %d ,", key); + int n = getPrimenumber(H); + int pos = key % n; + int collisionTimes = 0; + while (H.elem[pos].key != key) + { + if (H.elem[pos].key == -1) + { + DEBUG && printf("关键字 %d 不在表中\n", key); + return -1; + } + collisionTimes+=1; + pos++; + pos %= H.sizeindex; + } + DEBUG && printf("位置 %d \n", pos); + + return pos; +} + +void Print(HashTable H) +{ + printf("\n"); + for (int i = 0; i < H.sizeindex; ++i) + { + printf("{ [%d] : %d->%d } \t", i, H.elem[i].key, H.elem[i].val); + } + printf("\n\n"); +} + +void RecreateHashTable(HashTable *H) +{ + HashTable temp = InitHash(H->sizeindex * 2); + int a = H->sizeindex; + for (int i = 0; i elem[i]; + } + H->sizeindex *= 2; + H->elem = (ElemType*)malloc(H->sizeindex * sizeof(ElemType)); + for (int i = 0; i < H->sizeindex; i++) + { + H->elem[i].key = -1; H->elem[i].val = 0; + } + for (int i = 0; i < a; i++) + { + int p = temp.elem[i].key%getPrimenumber(*H); + H->elem[p] = temp.elem[i]; + } +} + +int main() +{ + srand(time(0)); + ElemType* inputs = (ElemType*)malloc(COUNT * sizeof(ElemType)); + for (int i = 0; i < COUNT; i++) + { + inputs[i] = InitElemType(rand() % 1024, rand() % 1024); + } + + HashTable H = InitHash(SIZEINDEX); + for (int i = 0; i < COUNT; ++i) + { + InsertHashTable(H, inputs[i]); + } + Print(H); + for (int i = 0; i < COUNT / 2; ++i) + { + Find(H, inputs[rand() % COUNT].key); + } + + printf("\n重建哈希表:\n"); + RecreateHashTable(&H); + Print(H); + return 0; + +} diff --git "a/2017-1/zkx/\345\223\210\345\270\214\350\241\250/Hash.h" "b/2017-1/zkx/\345\223\210\345\270\214\350\241\250/Hash.h" new file mode 100644 index 00000000..9a810256 --- /dev/null +++ "b/2017-1/zkx/\345\223\210\345\270\214\350\241\250/Hash.h" @@ -0,0 +1,45 @@ +#include +#include +#include + +#define DEBUG 1 +#define COUNT 6 +#define SIZEINDEX 11 + +typedef enum +{ + false, + true +} bool; + +typedef int KeyType; +typedef int ValueType; + +typedef struct _ElemType +{ + KeyType key; // 关键字 + ValueType val; // 值 +} ElemType; + +typedef struct +{ + ElemType *elem; + int count; // 当前数据元素个数 + int sizeindex; // hashsize[sizeindex]为当前容量 +} HashTable; + +ElemType InitElemType(KeyType key, ValueType value); //初始化关键字和值 + +HashTable InitHash(int size); //初始化一个哈希表 + +bool isPrime(int num); //判断一个数是否为素数 + +int getPrimenumber(HashTable H); //找到合适的小于表长的素数n + +void InsertHashTable(HashTable H, ElemType elem); //在HashTable中插入元素 + +int Find(HashTable H, KeyType key); //查找关键字 + +void Print(HashTable H); //输出Hash表中所有元素 + +void RecreateHashTable(HashTable *H); // 重建哈希表 diff --git "a/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/Graph.c" "b/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/Graph.c" new file mode 100644 index 00000000..0d8d5e3d --- /dev/null +++ "b/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/Graph.c" @@ -0,0 +1,132 @@ +#include "Graph.h" + +Status CreateGraph(Graph *G) +{ + int i, j, k; //计数标志 + G->vexnum = 9; //已知图有9个顶点 + G->arcnum = 12; //已知图有12条弧 + for (i = 0; i < G->vexnum; i++) + { + for (j = 0; j < G->vexnum; j++) + { + G->arcs[i][j].adj = INT_MAX; //初始化邻接矩阵,所有顶点不相连 + } + } + + return OK; +} +Status Connect(Graph*G, int a, int b) +{ + G->arcs[a][b].adj = G->arcs[b][a].adj = 1; //相连的顶点值为1,无向图双向设置 + return OK; +} + +void BFSTraverse(Graph G, int a, int b) +{ + int u, v, w; + bool flag=false; + LinkQueue Q; + for (v = 0; v < G.vexnum; ++v) + { + visited[v] = false; //初始化访问标志 + } + InitQueue(&Q); // 置空队列Q + EnQueue(&Q, a); //a入列 + visited[a] = true; + while (!QueueEmpty(Q)) + { + DeQueue(&Q, &u); //队头元素出队并置为u + for (w = FirstAdjVex(G, u); w >= 0; w = NextAdjVex(G, u, w)) //w为u的尚未访问的邻接顶点 + { + if (w == b) //如果w和b的值相等 + { + EnQueue(&Q, w); // w入队列 + Show(Q, a); //输出最短路径 + flag = true; + break; + } + if (!visited[w]) + { + EnQueue(&Q, w); + Visit(w); + } + } + if (flag) + { + break; + } + } + +} + + +ElemType FirstAdjVex(Graph G, int i) +{ + int j; + for (j = 0; j < G.vexnum; j++) + { + if (G.arcs[i][j].adj == 1) + { + return j; + } + } + return -1; +} +ElemType NextAdjVex(Graph G, int i, int j) +{ + int k; + for (k = j+1; k < G.vexnum; k++) + { + if (G.arcs[i][k].adj == 1) + { + return k; + } + } + return -1; +} + +Status Show(LinkQueue Q, int start) +{ + int path[MAX_VERTEX_NUM]; + int i; + QueuePtr p; + p = Q.rear; //p指针指向队列的尾部 + for (i = 0; i < MAX_VERTEX_NUM; i++) //初始化数组 + { + path[i] = -1; + } + /*path[0] = p->data; + p = p->priou;*/ + for (i = 0; p->data != start; i++) + { + path[i] = p->data; + p = p->priou; + } + path[i] = p->data; //从队尾依次向前赋值 + + for (; i >= 0; i--) //从队尾倒序输出 + { + if (path[i] >= 0) + { + printf("%d ", path[i] + 1); //从0开始所以最后结果要+1(顶点为1,2,3.....) + } + } + return OK; +} + +Status Print(Graph G) +{ + int i, j; + for (i = 0; i < 9; i++) + { + for (j = 0; j < 9; j++) + { + if (j != i) + { + printf("%d<->%d:", i + 1, j + 1); //计数从0开始 + BFSTraverse(G, i, j); + printf("\n"); + } + } + } +} diff --git "a/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/Graph.h" "b/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/Graph.h" new file mode 100644 index 00000000..710152ea --- /dev/null +++ "b/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/Graph.h" @@ -0,0 +1,40 @@ +#include +#include + +#include "Queue.h" + +#define MAX_VERTEX_NUM 100 +#define INFINITY INT_MAX //表示两个顶点不相连,值为无穷 +#define Visit(x) {visited[x]=true;} + + +bool visited[MAX_VERTEX_NUM]; + +typedef int VRType; + +typedef struct ArcCell // 弧的定义 +{ + VRType adj; // VRType是顶点关系类型。 + // 对无权图,用1或0表示相邻否; +} AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; + +typedef struct // 图的定义 +{ + AdjMatrix arcs; // 弧的信息 + int vexnum, arcnum; // 顶点数,弧数 +} Graph; + +Status CreateGraph(Graph *G); //构建一个无向图 + +Status Connect(Graph*G, int x, int y); //为邻接矩阵赋值,建立顶点之间的连接关系 + +void BFSTraverse(Graph G, int a, int b); //广度优先遍历图 + +ElemType FirstAdjVex(Graph G, int i); //返回顶点i的第一个邻接顶点 + +ElemType NextAdjVex(Graph G, int i, int j); //返回顶点i的下一个邻接顶点 + +Status Show(LinkQueue Q, int start); //输出两个顶点间的最短路径 + +Status Print(); //打印最后结果 + diff --git "a/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/Queue.c" "b/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/Queue.c" new file mode 100644 index 00000000..d89d0389 --- /dev/null +++ "b/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/Queue.c" @@ -0,0 +1,50 @@ +#include "Queue.h" + +Status InitQueue(LinkQueue *Q) // 初始化一个队列Q +{ + Q->front = Q->rear = (QueuePtr )malloc(MAX_SIZE * sizeof(QueuePtr)); //为队头,队尾指针分配内存空间 + 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, ElemType e) // 插入元素e为Q的新的队尾元素 +{ + QueuePtr p = (QueuePtr )malloc(MAX_SIZE * sizeof(QueuePtr)); //分配存储空间 + + if (!p) + { + return OVERFLOW; //存储分配失败 + } + p->data = e; + p->next = NULL; + p->priou = Q->front; //插入新的队尾结点时,令其priou域的指针指向刚刚出队列的结点 + Q->rear->next = p; + Q->rear = p; + return OK; +} + +Status DeQueue(LinkQueue *Q, ElemType *e) //出队列时,仅移动队头指针,而不将队头结点从链表中删除。 +{ + if (QueueEmpty(*Q)) + { + return ERROR; + } + Q->front = Q->front->next; + *e = Q->front->data; + return OK; + +} \ No newline at end of file diff --git "a/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/Queue.h" "b/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/Queue.h" new file mode 100644 index 00000000..bcaa033d --- /dev/null +++ "b/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/Queue.h" @@ -0,0 +1,43 @@ +#include +#include + +#define MAX_SIZE 100 + +typedef int ElemType; + +typedef enum +{ + ERROR, + OVERFLOW, + OK +} Status; + +typedef enum +{ + false, + true +} bool; + +typedef struct _QNode // 结点类型 +{ + ElemType data; //数据域 + struct QNode *next; //后继指针 + struct QNode *priou; //前驱指针 +} QNode, *QueuePtr; + +typedef struct _LinkQueue // 链队列类型 +{ + QueuePtr front; // 队头指针 + QueuePtr rear; // 队尾指针 +} LinkQueue; + +Status InitQueue(LinkQueue *Q); + +bool QueueEmpty(LinkQueue Q); + +Status EnQueue(LinkQueue *Q, ElemType e); + +Status DeQueue(LinkQueue *Q, ElemType *e); + +Status QueueTraverse(LinkQueue *Q); + diff --git "a/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/main.c" "b/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/main.c" new file mode 100644 index 00000000..3c2a6aec --- /dev/null +++ "b/2017-1/zkx/\345\271\277\345\272\246\351\201\215\345\216\206\345\233\276/main.c" @@ -0,0 +1,26 @@ +#include +#include "Graph.h" + +int main() +{ + Graph G; + + CreateGraph(&G); + + Connect(&G, 0, 1); + Connect(&G, 0, 2); + Connect(&G, 0, 3); + Connect(&G, 0, 6); + Connect(&G, 1, 2); + Connect(&G, 3, 4); + Connect(&G, 3, 5); + Connect(&G, 4, 5); + Connect(&G, 5, 7); + Connect(&G, 6, 7); + Connect(&G, 6, 8); + Connect(&G, 7, 8); + + Print(G); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/zkx/\346\213\206\345\210\206\347\272\277\346\200\247\351\223\276\350\241\250/change.c" "b/2017-1/zkx/\346\213\206\345\210\206\347\272\277\346\200\247\351\223\276\350\241\250/change.c" new file mode 100644 index 00000000..8ce83e9f --- /dev/null +++ "b/2017-1/zkx/\346\213\206\345\210\206\347\272\277\346\200\247\351\223\276\350\241\250/change.c" @@ -0,0 +1,116 @@ +#include "change.h" + +void CreateList(LinkList L, int n) //创建原线性链表 +{ + int i; + LNode* p = L; + L->next = NULL; + srand(time(NULL)); + for (i = 0; i data = rand() % 50+1; //随机生成数据 + p->next = L->next; //插入到表头 + L->next = p; + } + +}; + +void TraverseList_L1(LinkList L) //遍历输出单链表 +{ + LNode *p = L->next; + while (p) + { + printf("%d ", p->data); + p = p->next; + } +} + +void TraverseList_L2(LinkList L) //遍历输出循环链表 +{ + LNode *p = L->next; + while (p) + { + printf("%d ", p->data); + p = p->next; + if (p == L->next) + { + break; + } + } +} + +void change(LinkList list,LinkList *list1, LinkList *list2) //拆分原线性链表为两个循环链表 +{ + LNode*p; //用于遍历list,list1,list2的三个指向节点的指针 + LNode*rear1, *rear2; //尾指针 + int count = 0; + *list1 = (LNode*)malloc(sizeof(LNode)); + (*list1)->data = 0; + (*list1)->next = NULL; + rear1 = *list1; + + *list2 = (LNode*)malloc(sizeof(LNode)); //分配存储空间 + (*list2)->data = 0; + (*list2)->next = NULL; + rear2 = *list2; + + for (p = list->next; p != NULL; p = p->next) + { + count++; + if (count % 2) //控制奇数项 + { + rear1->next = p; + rear1 = p; + (*list1)->data++; //长度增加1 + printf("\n当前为:%d,奇数项,分配给list1 ", rear1->data); + } + else + { + rear2->next = p; + rear2 = p; + (*list2)->data++; //长度增加1 + printf("\n当前为:%d,偶数项,分配给list2 ", rear2->data); + } + } + rear1->next = (*list1)->next; + rear2->next = (*list2)->next; +} + +int main() +{ + int n; + LinkList L, La, Lb; + srand(time(0)); + n = rand() % 20 + 1 ; + L = (LinkList)malloc(sizeof(LNode)); + + printf("输入待拆分L数据:\n"); + CreateList(L, n); //创建L + printf("L:"); //输出L + TraverseList_L1(L); + printf("\n"); + + change(L, &La, &Lb); + printf("\n\n--------将L拆分成两个循环链表--------\n\n"); + + printf("\n(注:由于La,Lb是循环链表,故通过函数实现只输出一次。)\n\n"); + + printf("序号为奇数的元素放入La\n"); + printf("La元素个数:%d\n", La->data); + printf("La:"); //输出La + TraverseList_L2(La); + printf("\n"); + + printf("序号为偶数的元素放入Lb\n"); + printf("Lb元素个数:%d\n", Lb->data); + printf("Lb:"); //输出Lb + TraverseList_L2(Lb); + printf("\n\n"); + return 0; + +} +/*用尾指针rear来表示单循环链表, +则查找开始结点a1和终端结点an都很方便, +它们的存储位置分别是(rear->next)->next和rear, +显然,查找时间都是O(1)。故所用时间最少,空间最少*/ \ No newline at end of file diff --git "a/2017-1/zkx/\346\213\206\345\210\206\347\272\277\346\200\247\351\223\276\350\241\250/change.h" "b/2017-1/zkx/\346\213\206\345\210\206\347\272\277\346\200\247\351\223\276\350\241\250/change.h" new file mode 100644 index 00000000..6af9906b --- /dev/null +++ "b/2017-1/zkx/\346\213\206\345\210\206\347\272\277\346\200\247\351\223\276\350\241\250/change.h" @@ -0,0 +1,20 @@ +#include +#include +#include + +typedef int ElemType; //声明变量类型 + +typedef struct LNode +{ + ElemType data; + struct LNode *next; + +}LNode, *LinkList; //结构体的建立 + +void CreateList(LinkList L, int n); + +void TraverseList_L1(LinkList L); + +void TraverseList_L2(LinkList L); + +void change(LinkList list, LinkList *list1, LinkList *list2); diff --git "a/2017-1/zkx/\346\213\254\345\217\267\345\214\271\351\205\215/3-2-2.c" "b/2017-1/zkx/\346\213\254\345\217\267\345\214\271\351\205\215/3-2-2.c" new file mode 100644 index 00000000..1d0a4254 --- /dev/null +++ "b/2017-1/zkx/\346\213\254\345\217\267\345\214\271\351\205\215/3-2-2.c" @@ -0,0 +1,126 @@ + +#include "3-2-2.h" + +Status InitStack (SqStack* pS) //构造一个空栈 +{ + pS->base=(SElemType *)malloc(STACK_INIT_SIZE * + sizeof(SElemType)); + + if(! pS->base) + { + return OVERFLOW; + } + + pS->top =(*pS).base; + pS->stacksize = STACK_INIT_SIZE; + + return OK; +} + +Status Push(SqStack *S,SElemType e) +{ + if(S->top-S->base>=S->stacksize) + { + S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); + if(!S->base) + return OVERFLOW; + S->top=S->base+S->stacksize; + S->stacksize+=STACKINCREMENT; + } + *S->top++=e; + return OK; + +} + +int StackEmpty(SqStack *S) +{ + if (S) + return S->base==S->top; + return 1; +} + +Status Pop(SqStack *S,int *e) +{ + if(StackEmpty(S)) + { + return ERROR; + } + *e=*--S->top; + return OK; +} + +void Match(SqStack *S , char *p) +{ + int i=0; + int bracket; + InitStack(S); + while(p[i] != '\0') + { + DEBUG && printf("当前括号 '%c' ", p[i]); + switch(p[i]) + { + case '(': + DEBUG && printf(" '%c' 入栈 \n", p[i]); + Push(S,p[i]); + break; + + case ')': + DEBUG && printf(" 出栈 "); + Pop(S,&bracket); + printf("结果是 '%c' \n",bracket); + if (bracket != (p[i] == ')' ? '(' : p[i] == ']' ? '[' : '{')) + { + DEBUG && printf("匹配失败\n"); + return; + } + + break; + case '[': + DEBUG && printf(" '%c' 入栈 \n", p[i]); + Push(S,p[i]); + break; + + case ']': + DEBUG && printf(" 出栈 "); + Pop(S,&bracket); + printf("结果是 '%c' \n",bracket); + + if (bracket != (p[i] == ')' ? '(' : p[i] == ']' ? '[' : '{')) + { + DEBUG && printf("匹配失败\n"); + return; + } + + break; + default: + break; + } + i++; + } + StackEmpty(S); + DEBUG && printf("匹配成功 \n\n"); + +} + + +int main() +{ + char str1[10]="([()])"; + char str2[10]="[([][])]"; + char str3[10]="[((]])"; + SqStack S; + + printf("开始匹配第一组 :"); + puts(str1); + Match(&S,str1); + + printf("开始匹配第二组 :"); + puts(str2); + Match(&S,str2); + + printf("开始匹配第三组 :"); + puts(str3); + Match(&S,str3); + return 0; +} + diff --git "a/2017-1/zkx/\346\213\254\345\217\267\345\214\271\351\205\215/3-2-2.h" "b/2017-1/zkx/\346\213\254\345\217\267\345\214\271\351\205\215/3-2-2.h" new file mode 100644 index 00000000..ed85f918 --- /dev/null +++ "b/2017-1/zkx/\346\213\254\345\217\267\345\214\271\351\205\215/3-2-2.h" @@ -0,0 +1,28 @@ +#include +#include + +#define STACKINCREMENT 10 +#define STACK_INIT_SIZE 100 + +#define DEBUG 1 + +typedef enum {ERROR,OVERFLOW,OK} Status; + +typedef char SElemType; + +typedef struct +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack (SqStack* pS); + +Status Push (SqStack *S , SElemType e); + +int StackEmpty (SqStack *S); + +Status Pop (SqStack S , int *e); + +void Match(SqStack *S , char *p); \ No newline at end of file diff --git "a/2017-1/zkx/\346\216\222\345\272\217/sort.c" "b/2017-1/zkx/\346\216\222\345\272\217/sort.c" new file mode 100644 index 00000000..66e190af --- /dev/null +++ "b/2017-1/zkx/\346\216\222\345\272\217/sort.c" @@ -0,0 +1,140 @@ +#include "sort.h" + +Status InsertSort(RecordType *r, int length, int *compare, int *move) +{ + int j; + RecordType temp; + *compare = *move = 0; + for (int i = 1; i < length; i++) + { + temp = r[i]; /* 将待插入记录存放到临时变量中 */ + ++(*move); + j = i; /* 最近一次排序结束的边界位置 */ + while (++(*compare), (temp.key < r[j-1].key) && j > 0) /* 寻找插入位置 */ + { + r[j] = r[j - 1]; /* 从小到大排序,大元素右移 */ + ++(*move); + j = j - 1; /* 待插入元素与已排序区下一个(左移一位)元素继续进行比较 */ + } + r[j] = temp; /* 将待插入记录插入到已排序的序列中 */ + ++(*move); + } + return OK; +} + +/* 对记录数组r做一趟希尔插入排序,length为数组的长度, d为增量 */ +Status ShellSort(RecordType*arr, int length, int *compare, int *move) +{ + int i, j; + RecordType temp; + *compare = *move = 0; + int d = length / 2; + while (d >= 1) + { + for (int k = 0; k < d; k++) + { + for (i = d; i < length-k; i += d) + { + temp = arr[i+k]; + ++*move; + j = i; + while (++*compare, arr[j + k - d].key > temp.key && j > 0) + { + arr[j + k] = arr[j + k - d]; + j -= d; + ++*move; + } + arr[j + k] = temp; + ++*move; + } + } + d = d / 2; + } + return OK; +} + + +Status BubbleSort(RecordType *r, int length, int *compare, int *move) +{ + *compare = *move = 0; + int i, j; + RecordType temp; + for (i = length - 1; i > 0; i--) + { + for (j = 0; j < i; j++) + { + if (++*compare,r[j].key > r[j + 1].key) + { + temp = r[j]; + r[j] = r[j + 1]; + r[j + 1] = temp; + (*move) += 3; + } + } + } + return OK; +} + +Status _QuickSort(RecordType*r, int length, int *compare, int* move) +{ + RecordType flag = r[0]; + if (length <= 1)return; + + int i = 1, j = length - 1; + while (j >= i) + { + while (++*compare, r[i].key <= flag.key && j >= i)++i; + while (++*compare, r[j].key > flag.key && j >= i)--j; + if (j < i) + { + RecordType temp = r[0]; + r[0] = r[j]; + r[j] = temp; + (*move) += 3; + break; + } + + RecordType temp = r[i]; + r[i] = r[j]; + r[j] = temp; + (*move) += 3; + } + + _QuickSort(r, j, compare, move); + _QuickSort(r + j + 1, length - j - 1, compare, move); + return OK; +} +Status QuickSort(RecordType*arr, int length, int *compare, int* move) +{ + *compare = *move = 0; + _QuickSort(arr, length, compare, move); + return OK; +} + +Status _SelectSort(RecordType *r, int length, int *compare, int *move) +{ + int i; + int id = 0; + RecordType temp; + for (i = 1; i < length; i++) + { + if (++*compare, r[i].key < r[id].key) + { + id = i; + } + } + temp = r[0]; + r[0] = r[id]; + r[id] = temp; + (*move) += 3; + return OK; +} + +Status SelectSort(RecordType*r, int length, int *compare, int* move) +{ + *compare = *move = 0; + for (int i = 0; i < length - 1; ++i) + { + _SelectSort(r + i, length - i, compare, move); + } +} \ No newline at end of file diff --git "a/2017-1/zkx/\346\216\222\345\272\217/sort.h" "b/2017-1/zkx/\346\216\222\345\272\217/sort.h" new file mode 100644 index 00000000..2ded1e1c --- /dev/null +++ "b/2017-1/zkx/\346\216\222\345\272\217/sort.h" @@ -0,0 +1,48 @@ +#include +#include +#include + +#define MAXSIZE 20 +typedef int KeyType; +typedef int InfoType; + +typedef struct +{ + KeyType key; //关键字项 + InfoType otherinfo; //其他数据项 +}RecordType; //记录类型 + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; + +typedef enum _bool +{ + TRUE, + FALSE +}bool; + +Status InsertSort(RecordType *r, int length, int *compare, int *move); //直接插入排序 + +Status ShellSort(RecordType *r, int length, int *compare, int *move); //希尔排序 + +Status BubbleSort(RecordType *r, int length, int *compare, int *move); //起泡排序 + +Status _QuickSort(RecordType*r, int length, int *compare, int* move); + +Status QuickSort(RecordType*arr, int length, int *compare, int* move); //快速排序 + +Status _SelectSort(RecordType *r, int length, int *compare, int *move); + +Status SelectSort(RecordType *r, int length, int *compare, int *move); //简单选择排序 + +void input(RecordType*r, int length); //生成测试数据 + +void Print(RecordType *r, int length); //输出测试数据 + +void Showtimes(int *compare, int *move); //输出移动次数和比较次数 + +void CopyArr(RecordType* dest, const RecordType* source, int length); //进行数组的拷贝,用于进行同一组数据多种不同的排序 diff --git "a/2017-1/zkx/\346\216\222\345\272\217/sortmain.c" "b/2017-1/zkx/\346\216\222\345\272\217/sortmain.c" new file mode 100644 index 00000000..4d369f32 --- /dev/null +++ "b/2017-1/zkx/\346\216\222\345\272\217/sortmain.c" @@ -0,0 +1,76 @@ +#include "sort.h" + +void input(RecordType*r, int length) +{ + for (int i = 0; i < length; ++i) + { + r[i].key = rand() % 100 + 10; + } +} + +void Print(RecordType *r, int length) +{ + for (int i = 0; i < length; i++) + { + printf("%d ", r[i].key); + } + printf("\n"); +} + +void Showtimes(int *compare,int *move) +{ + printf("compareTimes : %d\n", compare); + printf("moveTimes : %d\n", move); +} + +void CopyArr(RecordType* dest, const RecordType* source, int length) +{ + for (int i = 0; i < length; ++i) + { + dest[i] = source[i]; + } +} +int main() +{ + srand(time(0)); + int compare = 0, move = 0; + int length = rand() % 15 + 5; //随机生成数组长度 + RecordType* origin = (RecordType*)malloc(length * sizeof(RecordType)); + RecordType* r = (RecordType*)malloc(length * sizeof(RecordType)); + + input(origin, length); + printf("------Before sort------\n"); + Print(origin, length); //生成一组待排序数据 + + printf("\n------After InsertSort------\n"); + CopyArr(r, origin, length); + InsertSort(r, length, &compare, &move); + Print(r, length); + Showtimes(compare, move); + + printf("\n------After ShellSort------\n"); + CopyArr(r, origin, length); + ShellSort(r, length, &compare, &move); + Print(r, length); + Showtimes(compare, move); + + printf("\n------After BubbleSort------\n"); + CopyArr(r, origin, length); + BubbleSort(r, length, &compare, &move); + Print(r, length); + Showtimes(compare, move); + + printf("\n------After QuickSort------\n"); + CopyArr(r, origin, length); + QuickSort(r, length, &compare, &move); + Print(r, length); + Showtimes(compare, move); + + printf("\n------After SelectSort------\n"); + CopyArr(r, origin, length); + SelectSort(r, length, &compare, &move); + Print(r, length); + Showtimes(compare, move); + return 0; + +} \ No newline at end of file diff --git "a/2017-1/zkx/\346\225\260\345\210\266\350\275\254\346\215\242/3-2-1.c" "b/2017-1/zkx/\346\225\260\345\210\266\350\275\254\346\215\242/3-2-1.c" new file mode 100644 index 00000000..0407d461 --- /dev/null +++ "b/2017-1/zkx/\346\225\260\345\210\266\350\275\254\346\215\242/3-2-1.c" @@ -0,0 +1,86 @@ +#include +#include +#include + +#include "3-2-1.h" + +Status InitStack (SqStack* pS) //构造一个空栈 +{ + pS->base=(SElemType *)malloc(STACK_INIT_SIZE * + sizeof(SElemType)); + + if(! pS->base) + { + return OVERFLOW; + } + + pS->top =(*pS).base; + pS->stacksize = STACK_INIT_SIZE; + + return OK; +} + +Status Push(SqStack *S,SElemType e) +{ + if(S->top-S->base>=S->stacksize) + { + S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); + if(!S->base) + return OVERFLOW; + S->top=S->base+S->stacksize; + S->stacksize+=STACKINCREMENT; + } + *S->top++=e; + return OK; + +} +int StackEmpty(SqStack *S) +{ + if (S) + return S->base==S->top; + return 1; +} + +Status Pop(SqStack *S,SElemType *e) +{ + if(StackEmpty(S)) + { + return ERROR; + } + *e=*--S->top; + return OK; + +} + +void conversion (SqStack *S,int n,int d) +{ + SElemType e; + while (n) + { + Push(S, n % d); + n = n / d; + } + while (!StackEmpty(S)) + { + Pop(S, &e); + printf ( "%d", e ); + } + + printf("\n"); +} // conversion +int main() +{ + int n; + int d; + SqStack S; + srand(time(0)); + + n=rand()%1024; + printf("待转换数据 :%d\n",n); + d=rand()%10+1; + printf("转换进制 :%d\n",d); + + InitStack(&S); + conversion(&S,n,d); + return 0; +} \ No newline at end of file diff --git "a/2017-1/zkx/\346\225\260\345\210\266\350\275\254\346\215\242/3-2-1.h" "b/2017-1/zkx/\346\225\260\345\210\266\350\275\254\346\215\242/3-2-1.h" new file mode 100644 index 00000000..f0bbe3ec --- /dev/null +++ "b/2017-1/zkx/\346\225\260\345\210\266\350\275\254\346\215\242/3-2-1.h" @@ -0,0 +1,21 @@ +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef int SElemType; + +typedef enum {ERROR,OVERFLOW,OK} Status; + +typedef struct Stack +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack (SqStack* pS); +Status Push(SqStack *S,SElemType e); +int StackEmpty(SqStack *S); +Status Pop(SqStack S,SElemType *e); +void conversion (SqStack *S,int n,int d); diff --git "a/2017-1/zkx/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/3-2-3.c" "b/2017-1/zkx/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/3-2-3.c" new file mode 100644 index 00000000..a407eebf --- /dev/null +++ "b/2017-1/zkx/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/3-2-3.c" @@ -0,0 +1,107 @@ +#include "3-2-3.h" + +Status InitStack (SqStack* pS) //构造一个空栈 +{ + pS->base=(SElemType *)malloc(STACK_INIT_SIZE * + sizeof(SElemType)); + + if(! pS->base) + { + return OVERFLOW; + } + + pS->top =(*pS).base; + pS->stacksize = STACK_INIT_SIZE; + + return OK; +} + +Status Push(SqStack *S,SElemType e) +{ + if(S->top-S->base>=S->stacksize) + { + S->base=(SElemType *)realloc(S->base,(S->stacksize+STACKINCREMENT)*sizeof(SElemType)); + if(!S->base) + return OVERFLOW; + S->top=S->base+S->stacksize; + S->stacksize+=STACKINCREMENT; + } + *S->top++=e; + return OK; + +} + +int StackEmpty(SqStack *S) +{ + if (S) + return S->base==S->top; + return 1; +} + +Status Pop(SqStack *S, SElemType *e) +{ + if(StackEmpty(S)) + { + return ERROR; + } + *e=*--S->top; + return OK; +} + +Status ClearStack(SqStack *S) +{ + + if(S->top == S->base) + return ERROR; + S->top = S->base; + return OK; +} +void LineEdit(SqStack *S) +{ + + char ch,c; + char *p; + ch = getchar(); + while (ch != EOF) //EOF为全文结束符 + { + while (ch != '\n') + { + switch (ch) + { + case '#' : + Pop(S, &c); + break; + case '@': + ClearStack(S); + break;// 重置S为空栈 + default : + Push(S, ch); + break; + } + ch = getchar(); // 从终端接收下一个字符 + } + // 将从栈底到栈顶的字符传送至调用过程的数据区; + p = S->base; + while(p<=S->top) + { + printf("%c",*p); + ++p; + } + ClearStack(S); // 重置S为空栈 + if (ch != EOF) + ch = getchar(); + } + +} +int main() +{ + SqStack S; + char *str1=" whli##ilr#e(s#*s)"; + printf("输入为:"); + puts(str1); + InitStack(&S); + printf("输出为:"); + LineEdit(&S); + +return 0; +} diff --git "a/2017-1/zkx/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/3-2-3.h" "b/2017-1/zkx/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/3-2-3.h" new file mode 100644 index 00000000..0251dfd5 --- /dev/null +++ "b/2017-1/zkx/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/3-2-3.h" @@ -0,0 +1,30 @@ +#include +#include + +#define STACKINCREMENT 10 +#define STACK_INIT_SIZE 100 + +#define DEBUG 1 + +typedef enum {ERROR,OVERFLOW,OK} Status; + +typedef char SElemType; + +typedef struct +{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack (SqStack* pS); + +Status Push (SqStack *S , SElemType e); + +int StackEmpty (SqStack *S); + +Status Pop (SqStack *S ,SElemType *e); + +Status ClearStack(SqStack *S); + +void LineEdit(SqStack *S); \ No newline at end of file diff --git "a/2017-1/zkx/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/\346\265\213\350\257\225\347\224\250\344\276\213.txt" "b/2017-1/zkx/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/\346\265\213\350\257\225\347\224\250\344\276\213.txt" new file mode 100644 index 00000000..ff07bac7 --- /dev/null +++ "b/2017-1/zkx/\350\241\214\347\274\226\350\276\221\347\250\213\345\272\217/\346\265\213\350\257\225\347\224\250\344\276\213.txt" @@ -0,0 +1 @@ +whli##ilr#e(s#*s) diff --git "a/2017-1/zkx/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Calculate.c" "b/2017-1/zkx/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Calculate.c" new file mode 100644 index 00000000..32e642a0 --- /dev/null +++ "b/2017-1/zkx/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Calculate.c" @@ -0,0 +1,224 @@ +#include "Calculate.h" + +Status InitStack(SqStack *S) //构造一个空栈 +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + + if (!S->base) //分配存储空间失败 + { + return OVERFLOW; + } + + S->top = S->base; //初始化 + S->stacksize = STACK_INIT_SIZE; + + return OK; +} + +Status Push(SqStack *S, SElemType e) //插入元素 e 为新的栈顶元素。 +{ + if (S->top - S->base >= S->stacksize) + { + S->base = (SElemType *)realloc(S->base, (S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if (!S->base) + return OVERFLOW; + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; + +} +int StackEmpty(SqStack *S) //判断栈是否为空 +{ + if (S) + return S->base == S->top; + return 1; +} + +Status Pop(SqStack *S, SElemType *e) //删除 S 的栈顶元素,并用 e 返回其值。 +{ + if (StackEmpty(S)) + { + return ERROR; + } + *e = *--S->top; + return OK; +} + +Status GetTop(SqStack *S, SElemType *e) //用 e 返回 S 的栈顶元素。 +{ + if (S->top == S->base) + return ERROR; + *e = *(S->top - 1); + return OK; +} + +bool In(SElemType c) //判断输入字符c是否为运算符 +{ + switch (c) + { + case'+': + case'-': + case'*': + case'/': + case'(': + case')': + case'#': + return true; + default: + return false; + } +} + +SElemType Precede(SElemType c1, SElemType c2) //判定运算符的栈顶运算符与读入的运算符之间优先关系的函数 +{ //根据书53页表3.1 算符间的优先关系确定 + SElemType judge; + switch (c2) + { + case '+': + case '-': //‘+’,‘-’为同类 + if (c1 == '(' || c1 == '#') + { + judge = '<'; + } + else + { + judge = '>'; + } + break; + case '*': + case '/': //‘*’,‘/’为同类 + if (c1 == '*' || c1 == '/' || c1 == ')') + { + judge = '>'; + } + else + { + judge = '<'; + } + break; + case '(': + if (c1 == ')') + { + return ERROR; + } + else + { + judge = '<'; + } + break; + case ')': + switch (c1) + { + case '(': + judge = '='; + break; + case '#': + return ERROR; + default: + judge = '>'; + } + break; + case '#': + switch (c1) + { + case '#': + judge = '='; + break; + case '(': + return ERROR; + default: + judge = '>'; + } + } + return judge; +} + +SElemType Operate(SElemType a, char ch, SElemType b) //将出栈的运算符和运算数进行计算 +{ + SElemType c; + a -= 48; + b -= 48; + switch (ch) + { + case'+': + c = a + b + 48; + break; + case'-': + c = a - b + 48; + break; + case'*': + c = a * b + 48; + break; + case'/': + c = a / b + 48; + break; + } + return c; +} +int EvaluateExpression(char *suffix) +{ + SqStack OPTR,OPND; + char ch; + char a, b, x, theta; + int i = 0; + InitStack(&OPTR); + InitStack(&OPND); + + Push(&OPTR, '#'); //将#作为栈底元素 + GetTop(&OPTR, &ch); // 获取栈顶元素 + while (suffix[i] != '#' || ch != '#') + { + if (!In(suffix[i])) + { + Push(&OPND, suffix[i]); + i++; + } + else + { + switch (Precede(ch, suffix[i])) + { + case '<': //插入suffix[i]到运算符栈中 + Push(&OPTR, suffix[i]); + i++; + break; + case '=': + Pop(&OPTR, &x); //将 x 作出栈处理 + i++; + break; + case '>': + Pop(&OPTR, &theta); + if (*(OPTR.top - 1) == '(') { + Pop(&OPTR, &x); + suffix[i] = '#'; + } + Pop(&OPND, &b); + Pop(&OPND, &a); + Push(&OPND, Operate(a, theta, b)); + if (OPTR.base == OPTR.top) { + suffix[i] = '#'; + } + break; + } + } + GetTop(&OPTR, &ch); + } + GetTop(&OPND, &ch); + printf("运算结果为%d\n", ch - '0'); + return ch - '0'; +} +int main() +{ + char str1[20] = { "3*6+(7-8/4)*3" }; + char str2[20] = { "8+7*4/2*(5-2)" }; + + printf("开始进行%s的计算\n", str1); + EvaluateExpression(str1); + + printf("开始进行%s的计算\n", str2); + EvaluateExpression(str2); + + return 0; + +} diff --git "a/2017-1/zkx/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Calculate.h" "b/2017-1/zkx/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Calculate.h" new file mode 100644 index 00000000..360a6c0b --- /dev/null +++ "b/2017-1/zkx/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Calculate.h" @@ -0,0 +1,55 @@ +#include +#include +#include + + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef char SElemType; + +typedef enum _bool +{ + false, + true +}bool; + +typedef enum +{ + ERROR, + OVERFLOW, + OK +} Status; + +typedef struct Stack +{ + SElemType *base; //栈底指针 + SElemType *top; //栈顶指针 + int stacksize; +} SqStack; + + +Status InitStack (SqStack* pS); + +Status Push (SqStack *S , SElemType e); + +Status GetTop(SqStack *S , SElemType *e); + +int StackEmpty (SqStack *S); + +Status Pop (SqStack *S , SElemType *e); + +bool In(char c); + +SElemType Precede(SElemType c1, SElemType c2); + +void transform(char *suffix, char *exp); + +Status Pass(char *suffix, char ch); + +SElemType Operate(SElemType a, SElemType ch, SElemType b); + +int EvaluateExpression(char *suffix); + +void Traverse(char *suffix); + diff --git "a/2017-1/zkx/\350\277\220\350\241\214\347\273\223\346\236\234.png" "b/2017-1/zkx/\350\277\220\350\241\214\347\273\223\346\236\234.png" new file mode 100644 index 00000000..c61904d6 Binary files /dev/null and "b/2017-1/zkx/\350\277\220\350\241\214\347\273\223\346\236\234.png" differ diff --git "a/2017-1/zkx/\351\230\237\345\210\227\345\256\236\347\216\260/Queue.c" "b/2017-1/zkx/\351\230\237\345\210\227\345\256\236\347\216\260/Queue.c" new file mode 100644 index 00000000..d6592eb5 --- /dev/null +++ "b/2017-1/zkx/\351\230\237\345\210\227\345\256\236\347\216\260/Queue.c" @@ -0,0 +1,176 @@ +#include "Queue.h" + +Status InitQueue (LinkQueue *Q) // 构造一个空队列Q +{ + Q->front = Q->rear = (QueuePtr *)malloc(STACK_INIT_SIZE * sizeof(QueuePtr)); //为队头,队尾指针分配内存空间 + if (!Q->front) + { + return ERROR; //存储分配失败 + } + Q->front->next = NULL; + printf("正在构造一个空队列。。。\n"); + return OK; +} + +Status DestroyQueue (LinkQueue *Q) //销毁队列Q,Q不再存在 +{ + while(Q->front) + { + Q->rear = Q->front->next; + free(Q->front); + Q->front = Q->rear; + } + printf("队列已被销毁。\n"); + return OK; +} + +Status ClearQueue (LinkQueue Q) //将Q清为空队列 +{ + DestroyQueue(&Q); + InitQueue(&Q); + printf("队列已被清空\n"); +} + +Status QueueEmpty (LinkQueue *Q) //判断队列是否为空 +{ + if (Q->front==Q->rear) + { + printf("队列是空队列\n"); + return OK; + } + +} + +int QueueLength (LinkQueue *Q) //求队列的长度 +{ + int count = 0; + //指向存放数据的第一个结点 + QueuePtr p = Q->front->next; + while(p) + { + count++; + p = p->next; + } + return count; +} + +void GetHead(LinkQueue *Q,QElemType e) //返回队列队头元素 +{ + QueuePtr p = (QueuePtr *)malloc(STACK_INIT_SIZE * sizeof(QueuePtr)); + if(Q->front == Q->rear) + { + printf("队列为空!\n"); + } + else + { + p = Q->front->next; + e = p->data; + printf("队头元素为:%d\n",e); + + } +} + +Status EnQueue (LinkQueue *Q, QElemType e) // 插入元素e为Q的新的队尾元素 +{ + QueuePtr p = (QueuePtr *)malloc(STACK_INIT_SIZE * sizeof(QueuePtr)); //分配存储空间 + + if(!p) + { + return OVERFLOW; //存储分配失败 + } + p->data = e; + p->next = NULL; + Q->rear->next = p; + Q->rear = p; + printf("%d入队 ", e); + + return OK; +} + +Status DeQueue (LinkQueue *Q, QElemType *e) // 若队列不空,则删除Q的队头元素, +{ //用 e 返回其值,并返回OK;否则返回ERROR + QueuePtr p = (QueuePtr *)malloc(STACK_INIT_SIZE * sizeof(QueuePtr)); + if (Q->front == Q->rear) + { + return ERROR; + } + p = Q->front->next; + e = p->data; + Q->front->next = p->next; + printf("%d出队\t", e); + if(Q->rear == p) + { + Q->rear = Q->front; + } + free (p); + return OK; +} + +Status QueueTraverse (LinkQueue *Q ) +{ + QueuePtr p = Q->front->next; + if (Q->front == Q->rear) + { + printf("队列为空!\n"); + return ERROR; + } + else + { + while (p) + { + printf("%d\t", p->data); + p = p->next; + } + printf("\n"); + return OK; + } +} + + +int main() +{ + LinkQueue Q; + int i; + int n[50]; + + srand(time(0)); + int number = rand() % 15+1; + + for (i = 0; i < number; i++) //生成队列随机数 + { + n[i] = rand() % 100; + } + + InitQueue(&Q); //构造一个空队列 + + for(i = 0; i < number; i++) //将随机生成的数字依次入队列 + { + EnQueue(&Q,n[i]); + } + printf("\n"); + + printf("队列的长度为:%d\n", QueueLength(&Q)); //求队列元素个数 + + GetHead(&Q, n[0]); + + printf("队列中元素为:"); + QueueTraverse(&Q); + + for (i = 0; i < number; i++) + { + DeQueue(&Q,n[i]); + } + printf("\n"); + + printf("当所有元素出队列后,"); + QueueEmpty(&Q); + + ClearQueue(Q); + printf("\n"); + + + return 0; + + +} + diff --git "a/2017-1/zkx/\351\230\237\345\210\227\345\256\236\347\216\260/Queue.h" "b/2017-1/zkx/\351\230\237\345\210\227\345\256\236\347\216\260/Queue.h" new file mode 100644 index 00000000..fd41395c --- /dev/null +++ "b/2017-1/zkx/\351\230\237\345\210\227\345\256\236\347\216\260/Queue.h" @@ -0,0 +1,49 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 + +typedef int QElemType; + +typedef enum +{ + ERROR, + OVERFLOW, + OK, + TRUE, + FALSE +} Status; + +typedef struct _QNode // 结点类型 +{ + QElemType data; + struct QNode *next; +} QNode, *QueuePtr; + +typedef struct _LinkQueue // 链队列类型 +{ + QueuePtr front; // 队头指针 + QueuePtr rear; // 队尾指针 +} LinkQueue; + +Status InitQueue (LinkQueue *Q); + +Status DestroyQueue (LinkQueue *Q); + +Status ClearQueue (LinkQueue Q); + +Status QueueEmpty (LinkQueue *Q); + +void GetHead(LinkQueue *Q, QElemType e); + +int QueueLength (LinkQueue *Q); + +Status EnQueue (LinkQueue *Q, QElemType e); + +Status DeQueue(LinkQueue *Q, QElemType *e); + +Status QueueTraverse(LinkQueue *Q); + + diff --git a/2017-1/zkz/Graph/Graph.c b/2017-1/zkz/Graph/Graph.c new file mode 100644 index 00000000..2e15645e --- /dev/null +++ b/2017-1/zkz/Graph/Graph.c @@ -0,0 +1,81 @@ +#include "Graph.h" +void _connect(Graph* graph, int a, int b) { + graph->matrix[a - 1][b - 1] = 1; + graph->matrix[b - 1][a - 1] = 1; +} +bool _connected(Graph* graph, int a, int b) { + return graph->matrix[a - 1][b - 1]; +} +void _show(Graph* graph) { + int x, y, size = graph->size; + /*打印第一行*/ + printf(" "); + for (x = 0; x < size; ++x) { printf("%2d ", x + 1); } + printf("\n"); + /*打印剩下size行*/ + for (y = 0; y < size; ++y) { + printf("%2d ", y + 1); + for (x = 0; x < size; ++x) { printf(" %d ", graph->matrix[x][y]); } + printf("\n"); + } +} +bool __allVisited(Graph* graph, bool* visited) { + int size = graph->size; + for (int i = 0; i < size; ++i) { + if (!visited[i]) + return false; + } + return true; +} +void __backwardTraverse(Node* p) { + if (p->prior) { + __backwardTraverse(p->prior); + } + printf("%d ", p->data); +} +void _directory(Graph* graph, int start, int end) { + /*队列与辅助数组*/ + Queue q = newQueue(); + bool* visited = (bool*)malloc(graph->size * sizeof(bool)); + + int size = graph->size; + int i; + + for (i = 0; i < size; ++i) { visited[i] = 0; } + Visit(start); + EnQueue(&q, start); + DeQueue(&q); + //TraverseQueue(q); + while (!__allVisited(graph, visited)) { + for (i = 1; i <= size; ++i) { + if (graph->connected(graph, start, i) && !isVisited(i)) { + Visit(i); + EnQueue(&q, i); + if (i == end) { break; } + } + } + if (i == end) { break; } + start = q.front->data; + DeQueue(&q); + //TraverseQueue(q); + } + __backwardTraverse(q.rear); +} +Graph newGraph(int size) { + Graph temp; + int i, j; + temp.size = size; + temp.matrix = (bool**)malloc(size * sizeof(bool*)); + for (i = 0; i < size; ++i) { + temp.matrix[i] = (bool*)malloc(size * sizeof(bool)); + for (j = 0; j < size; ++j) { + temp.matrix[i][j] = 0; + } + } + + temp.connect = _connect; + temp.connected = _connected; + temp.show = _show; + temp.directory = _directory; + return temp; +} \ No newline at end of file diff --git a/2017-1/zkz/Graph/Graph.h b/2017-1/zkz/Graph/Graph.h new file mode 100644 index 00000000..b2a41962 --- /dev/null +++ b/2017-1/zkz/Graph/Graph.h @@ -0,0 +1,23 @@ +#pragma once +#include +#include +#include "Queue.h" +#define Visit(x) {visited[(x)-1]=true;} +#define isVisited(x) (bool)visited[(x)-1] +/*用邻接矩阵进行存储*/ +typedef struct _Graph { + /*0和1的二维数组*/ + bool** matrix; + /*节点个数*/ + int size; + /*函数指针:在结点a与b之间建立连接*/ + void(*connect)(struct _Graph*, int a, int b); + /*函数指针:判断结点a与b之间是否有连接*/ + bool(*connected)(struct _Graph*,int a,int b); + /*函数指针:打印二维数组*/ + void(*show)(struct _Graph*); + /*函数指针:找到从节点start到节点end的最短路径*/ + void(*directory)(struct _Graph*, int start, int end); +}Graph; +/*初始化一个图*/ +Graph newGraph(int size); \ No newline at end of file diff --git a/2017-1/zkz/Graph/Queue.c b/2017-1/zkz/Graph/Queue.c new file mode 100644 index 00000000..39f0f473 --- /dev/null +++ b/2017-1/zkz/Graph/Queue.c @@ -0,0 +1,46 @@ +#include "Queue.h" +Queue newQueue() { + Queue q; + q.front = q.rear = q.lastOut = NULL; + return q; +} +bool isEmpty(Queue q) { + return !q.front && !q.rear; +} +void EnQueue(Queue* q, Elemtype data) { + /*Step 1 构造新节点*/ + Node* temp = (Node*)malloc(sizeof(Node)); + /*Step 2 为新节点赋值*/ + temp->data = data; + temp->next = NULL; + temp->prior = q->lastOut; + /*Step 3 将新节点入队列*/ + if (isEmpty(*q)) { + q->front = q->rear = temp; + } + else { + q->rear->next = temp; + q->rear = temp; + } +} +void DeQueue(Queue*q) { + q->lastOut = q->front; + if (q->front->next == NULL) { + q->rear = NULL; + } + q->front = q->front->next; +} +void TraverseQueue(Queue q) { + Node*p = q.front; + printf("\n q : "); + if (!p) { + printf("(empty)"); + } + else { + do { + printf("%d ", p->data); + p = p->next; + } while (p); + } + printf("\n"); +} \ No newline at end of file diff --git a/2017-1/zkz/Graph/Queue.h b/2017-1/zkz/Graph/Queue.h new file mode 100644 index 00000000..d2fdb1e7 --- /dev/null +++ b/2017-1/zkz/Graph/Queue.h @@ -0,0 +1,19 @@ +#pragma once +#include +typedef enum _bool { false, true } bool; +typedef char Elemtype; +typedef struct _Node { + Elemtype data; + struct _Node* prior; + struct _Node* next; +}Node; +typedef struct _Queue { + Node* front; + Node* rear; + Node* lastOut; +}Queue; +Queue newQueue(); +bool isEmpty(Queue q); +void EnQueue(Queue* q, Elemtype data); +void DeQueue(Queue*q); +void TraverseQueue(Queue q); \ No newline at end of file diff --git a/2017-1/zkz/Graph/test.c b/2017-1/zkz/Graph/test.c new file mode 100644 index 00000000..b37b4d94 --- /dev/null +++ b/2017-1/zkz/Graph/test.c @@ -0,0 +1,31 @@ +#include +#include "Graph.h" +#define SIZE 9 + +int main(void) { + /*测试用例*/ + Graph g = newGraph(SIZE); + g.connect(&g, 1, 2); + g.connect(&g, 1, 3); + g.connect(&g, 1, 4); + g.connect(&g, 1, 7); + g.connect(&g, 2, 3); + g.connect(&g, 4, 5); + g.connect(&g, 4, 6); + g.connect(&g, 5, 6); + g.connect(&g, 6, 8); + g.connect(&g, 7, 8); + g.connect(&g, 7, 9); + g.connect(&g, 8, 9); + + /*寻找各点到其他各点的最短路径*/ + for (int start = 1; start <= SIZE; ++start) { + for (int end = 1; end <= SIZE; ++end) { + if (start == end) { continue; } + printf("%d -> %d : ",start,end); + g.directory(&g, start, end); + printf("\n"); + } + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/zkz/Graph/\350\277\220\350\241\214\347\273\223\346\236\234\345\261\200\351\203\250\346\210\252\345\233\276.png" "b/2017-1/zkz/Graph/\350\277\220\350\241\214\347\273\223\346\236\234\345\261\200\351\203\250\346\210\252\345\233\276.png" new file mode 100644 index 00000000..563ce228 Binary files /dev/null and "b/2017-1/zkz/Graph/\350\277\220\350\241\214\347\273\223\346\236\234\345\261\200\351\203\250\346\210\252\345\233\276.png" differ diff --git a/2017-1/zkz/Hash/Hash/OpenHash.c b/2017-1/zkz/Hash/Hash/OpenHash.c new file mode 100644 index 00000000..36c36954 --- /dev/null +++ b/2017-1/zkz/Hash/Hash/OpenHash.c @@ -0,0 +1,125 @@ +#include "OpenHash.h" +bool DEBUG = 1; +bool isPrime(int n) { + for (int i = 2; i < n; ++i) { + if (n%i == 0) return false; + } + return true; +} +bool isFull(OpenHash openHash) { + return openHash.used == openHash.size; +} +bool isEmpty(OpenHash openHash) { + return openHash.used == 0; +} +ElemType newElemType(KeyType key, ValueType value) { + ElemType temp = { key,value }; + return temp; +} +OpenHash newOpenHash(int size) { + OpenHash temp; + temp.size = size; + temp.used = 0; + temp.elements = (ElemType*)malloc(size * sizeof(ElemType)); + for (int i = 0; i < size; ++i) { + temp.elements[i].key = EMPTYKEY; + temp.elements[i].val = EMPTYVALUE; + } + return temp; +} +int getP(OpenHash openHash) { + int p = openHash.size; + if (p == 1)return 1; + while (!isPrime(p--)); + return p + 1; +} +void ShowOpenHash(OpenHash openHash) { + for (int i = 0; i < openHash.size; ++i) { + printf("{ [%d] : %d->%d }\n", i, openHash.elements[i].key, openHash.elements[i].val); + } +} +void Insert(OpenHash openHash, ElemType elem) { + debug_print("准备插入元素{ %d -> %d }\n", elem.key, elem.val); + if (isFull(openHash)) { + debug_print("Error : full hash table to insert\n"); + debug_print("======================\n"); + return; + } + int p = getP(openHash); + int pos = elem.key % p; + int collisionTimes = 0; + while (openHash.elements[pos].key != EMPTYKEY) { + collisionTimes += 1; + ++pos; pos %= openHash.size; + } + if (collisionTimes) { + debug_print("冲突 %d 次后 \n", collisionTimes); + } + openHash.elements[pos] = elem; + openHash.used += 1; + debug_print("在位置 %d 插入元素{ %d ->%d }\n", pos, elem.key, elem.val); + + debug_print("======================\n"); +} +void Delete(OpenHash openHash, ElemType elem) { + debug_print("准备删除元素{ %d -> %d }\n", elem.key, elem.val); + if (isEmpty(openHash)) { + debug_print("Error : empty hash table to delete\n"); + debug_print("======================\n"); + return; + } + int p = getP(openHash); + int pos = elem.key % p; + int collisionTimes = 0; + while (openHash.elements[pos].key != elem.key) { + collisionTimes += 1; + ++pos; pos %= openHash.size; + } + if (collisionTimes) { + debug_print("冲突 %d 次后 \n", collisionTimes); + } + openHash.elements[pos] = newElemType(DELETEDKEY, DELETEDVALUE); + openHash.used -= 1; + debug_print("在位置 %d 删除元素{ %d ->%d }\n", pos, elem.key, elem.val); + + debug_print("======================\n"); +} +/*在表openHash中查找元素elem*/ +/*若找到则返回elem的位置,否则返回-1*/ +int Find(OpenHash openHash, KeyType key) { + debug_print("准备查找关键字 %d \n", key); + int p = getP(openHash); + int pos = key % p; + int collisionTimes = 0; + while (openHash.elements[pos].key != key) { + if (openHash.elements[pos].key == EMPTYKEY) { + debug_print("关键字 %d 不在表中\n", key); + debug_print("======================\n"); + return -1; + } + collisionTimes += 1; + ++pos; pos %= openHash.size; + } + if (collisionTimes) { + debug_print("冲突 %d 次后 \n", collisionTimes); + } + debug_print("在位置 %d 找到关键字 %d \n", pos, key); + + debug_print("======================\n"); + return pos; +} +OpenHash Rebuild(OpenHash openHash) { + debug_print("扩建表\n"); + int size = 2 * openHash.size; + int debugStatu = DEBUG; + DEBUG = 0; + OpenHash temp = newOpenHash(size); + for (int i = 0; i < openHash.size; ++i) { + if (openHash.elements[i].key != EMPTYKEY && openHash.elements[i].key != DELETEDKEY) { + Insert(temp, openHash.elements[i]); + } + } + free(openHash.elements); + DEBUG = debugStatu; + return temp; +} \ No newline at end of file diff --git a/2017-1/zkz/Hash/Hash/OpenHash.h b/2017-1/zkz/Hash/Hash/OpenHash.h new file mode 100644 index 00000000..a27ec661 --- /dev/null +++ b/2017-1/zkz/Hash/Hash/OpenHash.h @@ -0,0 +1,38 @@ +#pragma once +#include +#include + +#define EMPTYKEY -1 +#define EMPTYVALUE 0 +#define DELETEDKEY -2 +#define DELETEDVALUE 0 +#define debug_print(...) if(DEBUG)printf(__VA_ARGS__) + +typedef enum _bool { false, true } bool; +typedef int KeyType; +typedef int ValueType; +typedef struct _ElemType { + KeyType key; // 关键字 + ValueType val; // 值 +} ElemType; + +typedef struct _OpenHash { + int size; //数据个数 + int used; //已经使用了的数据个数 + ElemType* elements; //存放数据的地址 +}OpenHash; + +bool isPrime(int n); +bool isFull(OpenHash openHash); +bool isEmpty(OpenHash openHash); +ElemType newElemType(KeyType key, ValueType value); +OpenHash newOpenHash(int size); +int getP(OpenHash openHash); +void ShowOpenHash(OpenHash openHash); + +void Insert(OpenHash openHash, ElemType elem); +void Delete(OpenHash openHash, ElemType elem); +/*在表openHash中查找元素elem*/ +/*若找到则返回elem的位置,否则返回-1*/ +int Find(OpenHash openHash, KeyType key); +OpenHash Rebuild(OpenHash openHash); \ No newline at end of file diff --git a/2017-1/zkz/Hash/Hash/output.txt b/2017-1/zkz/Hash/Hash/output.txt new file mode 100644 index 00000000..ccc91f13 --- /dev/null +++ b/2017-1/zkz/Hash/Hash/output.txt @@ -0,0 +1,72 @@ +【OpenHash】 +准备插入元素{ 32 -> 60 } +在位置 4 插入元素{ 32 ->60 } +====================== +准备插入元素{ 39 -> 39 } +冲突 1 次后 +在位置 5 插入元素{ 39 ->39 } +====================== +准备插入元素{ 38 -> 87 } +在位置 3 插入元素{ 38 ->87 } +====================== +准备插入元素{ 71 -> 1 } +在位置 1 插入元素{ 71 ->1 } +====================== +准备插入元素{ 89 -> 43 } +冲突 1 次后 +在位置 6 插入元素{ 89 ->43 } +====================== +准备插入元素{ 25 -> 65 } +冲突 3 次后 +在位置 7 插入元素{ 25 ->65 } +====================== +{ [0] : -1->0 } +{ [1] : 71->1 } +{ [2] : -1->0 } +{ [3] : 38->87 } +{ [4] : 32->60 } +{ [5] : 39->39 } +{ [6] : 89->43 } +{ [7] : 25->65 } +{ [8] : -1->0 } +{ [9] : -1->0 } +准备查找关键字 71 +在位置 1 找到关键字 71 +====================== +准备查找关键字 71 +在位置 1 找到关键字 71 +====================== +准备查找关键字 25 +冲突 3 次后 +在位置 7 找到关键字 25 +====================== +准备查找关键字 60 +关键字 60 不在表中 +====================== +准备查找关键字 80 +关键字 80 不在表中 +====================== +准备查找关键字 19 +关键字 19 不在表中 +====================== +扩建表 +{ [0] : 38->87 } +{ [1] : 39->39 } +{ [2] : -1->0 } +{ [3] : -1->0 } +{ [4] : -1->0 } +{ [5] : -1->0 } +{ [6] : 25->65 } +{ [7] : -1->0 } +{ [8] : -1->0 } +{ [9] : -1->0 } +{ [10] : -1->0 } +{ [11] : -1->0 } +{ [12] : -1->0 } +{ [13] : 32->60 } +{ [14] : 71->1 } +{ [15] : 89->43 } +{ [16] : -1->0 } +{ [17] : -1->0 } +{ [18] : -1->0 } +{ [19] : -1->0 } \ No newline at end of file diff --git a/2017-1/zkz/Hash/Hash/test.c b/2017-1/zkz/Hash/Hash/test.c new file mode 100644 index 00000000..ece5a07f --- /dev/null +++ b/2017-1/zkz/Hash/Hash/test.c @@ -0,0 +1,36 @@ + +#include +#include "OpenHash.h" + +#define CAPACITY 10 +#define SIZE 6 +ElemType* genTestInputs(int size) { + ElemType* arr = (ElemType*)malloc(size * sizeof(ElemType)); + for (int i = 0; i < size; ++i) { + arr[i] = newElemType(rand() % 100, rand() % 100); + } + return arr; +} +int main() { + srand(time(0)); + printf("【OpenHash】\n"); + OpenHash oh = newOpenHash(CAPACITY); + /*生成测试用例*/ + ElemType* testInputs = genTestInputs(SIZE); + /*建表*/ + for (int i = 0; i < SIZE; ++i) { + Insert(oh,testInputs[i]); + } + ShowOpenHash(oh); + /*查找*/ + for (int i = 0; i < SIZE/2; ++i) { + Find(oh, testInputs[rand()%SIZE].key); + } + for (int i = 0; i < SIZE/2; ++i) { + Find(oh,rand()%100); + } + /*扩建*/ + oh = Rebuild(oh); + ShowOpenHash(oh); + +} \ No newline at end of file diff --git a/2017-1/zkz/LinkList/LinkList.c b/2017-1/zkz/LinkList/LinkList.c new file mode 100644 index 00000000..8c12e4dd --- /dev/null +++ b/2017-1/zkz/LinkList/LinkList.c @@ -0,0 +1,88 @@ +#include +#include +#include "LinkList.h" + +Status CreateList(LinkList L, const int len) { + L->next = NULL; + Node *p, *pL = L; + int i = 0; + int randBase = RANDBASE; + + DEBUG && printf("开始创建长度为 %d 的链表 \n", len); + while (++i <= len) { + /*Step 1 新建节点*/ + p = NEWNODE; + if (!p) + return OVERFLOW; + /*Step 2 为新节点赋值*/ + p->data = randBase += + rand() % (RANDMAXINCREMENT - RANDMININCREMENT + 1) + RANDMININCREMENT; + DEBUG && printf("第%02d个节点的数据被随机赋值为 %d \n", i, p->data); + + p->next = NULL; + + /*Step 3 将新节点合并到链表中*/ + pL->next = p; + pL = pL->next; + + }/*end while*/ + DEBUG && printf("长度为 %d 的链表创建完毕 \n\n", len); + + return OK; +}/*end CreateList*/ +Status TravelList(const LinkList L) { + if (!L) + return ERROR; + Node * pL = L->next; + while (pL) { + printf("%d ", pL->data); + pL = pL->next; + } + return OK; +} +Status MergeList(LinkList La, LinkList Lb, LinkList Lc) { + Node * pa = La->next, *pb = Lb->next, *pc = Lc; + DEBUG && printf("开始归并链表La与Lb到Lc中\n"); + while (pa&&pb) { + /*Step 1 新建节点*/ + Node * p = NEWNODE; + if (!p) + return OVERFLOW; + /*Step 2 为新节点赋值*/ + p->next = NULL; + DEBUG && printf("由于pa->data( %d ) %s pb->data( %d ) , ", pa->data, pa->data <= pb->data ? "<=" : "> ", pb->data); + if (pa->data <= pb->data) { + p->data = pa->data; pa = pa->next; + } + else { + p->data = pb->data; pb = pb->next; + } + DEBUG && printf("新节点的数据被赋值为 %d \n", p->data); + /*Step 3 将新节点合并到链表中*/ + pc->next = p; + pc = pc->next; + }//end while + /*如果pa与pb均为空,则所有节点已加入新链表,归并操作完成*/ + if (!pa && !pb) { + DEBUG && printf("pa与pb均为空,归并操作完成\n"); + return OK; + } + DEBUG && printf("%s不为空,将剩余部分复制到Lc中\n", pa ? "pa" : "pb"); + Node * pr = pa ? pa : pb; + while (pr) { + /*Step 1 新建节点*/ + Node * p = NEWNODE; + if (!p) + return OVERFLOW; + /*Step 2 为新节点赋值*/ + p->next = NULL; + p->data = pr->data; + DEBUG && printf("新节点的数据被赋值为 %d \n", p->data); + pr = pr->next; + /*Step 3 将新节点合并到链表中*/ + pc->next = p; + pc = pc->next; + }//end while + DEBUG && printf("归并操作完成\n\n"); + return OK; +} \ No newline at end of file diff --git a/2017-1/zkz/LinkList/LinkList.h b/2017-1/zkz/LinkList/LinkList.h new file mode 100644 index 00000000..118631b8 --- /dev/null +++ b/2017-1/zkz/LinkList/LinkList.h @@ -0,0 +1,25 @@ +typedef int ElemType; +typedef struct Node { + ElemType data; + struct Node * next; +}Node; +typedef Node* LinkList; + +typedef enum Status { OK, ERROR, OVERFLOW } Status; + +/*用宏定义快速创建新节点*/ +#define NEWNODE (LinkList)malloc(sizeof(Node)) +/*这三个宏用于在创建节点时生成随机的有序数据*/ +#define RANDBASE 10 +#define RANDMAXINCREMENT 5 +#define RANDMININCREMENT 1 +/*这个宏用于指定是否输出函数运行过程*/ +#define DEBUG 1 + +/*函数声明*/ +/*创建链表*/ +Status CreateList(LinkList, const int); +/*遍历输出链表*/ +Status TravelList(const LinkList); +/*算法2.12 归并链表*/ +Status MergeList(LinkList, LinkList, LinkList); \ No newline at end of file diff --git a/2017-1/zkz/LinkList/test.c b/2017-1/zkz/LinkList/test.c new file mode 100644 index 00000000..ea0683d7 --- /dev/null +++ b/2017-1/zkz/LinkList/test.c @@ -0,0 +1,52 @@ +#include +#include +#include +#include "LinkList.h" + +/*这两个宏用于指定随机生成链表时长度的范围*/ +#define MAXLEN 7 +#define MINLEN 2 +/*用于处理每个LinkList函数的返回值*/ +#define SOLVEFLAG \ +do {\ + switch (flag) {\ + case OK:\ + break;\ + case ERROR:\ + DEBUG && printf("ERROR");\ + return 1;\ + break;\ + case OVERFLOW:\ + DEBUG && printf("OVERFLOW");\ + return 1;\ + break;\ + default:\ + break;\ + }\ +} while (0) + +int main(void) { + srand(time(0)); + srand(rand()); + + LinkList La = NEWNODE; + LinkList Lb = NEWNODE; + LinkList Lc = NEWNODE; + int flag; + flag = CreateList(La, rand() % (MAXLEN - MINLEN + 1) + MINLEN); + SOLVEFLAG; + flag = CreateList(Lb, rand() % (MAXLEN - MINLEN + 1) + MINLEN); + SOLVEFLAG; + flag = MergeList(La, Lb, Lc); + SOLVEFLAG; + printf("La : "); + flag = TravelList(La); + SOLVEFLAG; + printf("\nLb : "); + flag = TravelList(Lb); + SOLVEFLAG; + printf("\nLc : "); + flag = TravelList(Lc); + SOLVEFLAG; + printf("\n"); +} \ No newline at end of file diff --git "a/2017-1/zkz/LinkList/\350\277\220\350\241\214\347\273\223\346\236\2341.png" "b/2017-1/zkz/LinkList/\350\277\220\350\241\214\347\273\223\346\236\2341.png" new file mode 100644 index 00000000..56421d59 Binary files /dev/null and "b/2017-1/zkz/LinkList/\350\277\220\350\241\214\347\273\223\346\236\2341.png" differ diff --git "a/2017-1/zkz/LinkList/\350\277\220\350\241\214\347\273\223\346\236\2342.png" "b/2017-1/zkz/LinkList/\350\277\220\350\241\214\347\273\223\346\236\2342.png" new file mode 100644 index 00000000..4322782d Binary files /dev/null and "b/2017-1/zkz/LinkList/\350\277\220\350\241\214\347\273\223\346\236\2342.png" differ diff --git "a/2017-1/zkz/LinkList/\350\277\220\350\241\214\347\273\223\346\236\2343.png" "b/2017-1/zkz/LinkList/\350\277\220\350\241\214\347\273\223\346\236\2343.png" new file mode 100644 index 00000000..5aaacbbb Binary files /dev/null and "b/2017-1/zkz/LinkList/\350\277\220\350\241\214\347\273\223\346\236\2343.png" differ diff --git a/2017-1/zkz/Queue/Queue.c b/2017-1/zkz/Queue/Queue.c new file mode 100644 index 00000000..47c96ed5 --- /dev/null +++ b/2017-1/zkz/Queue/Queue.c @@ -0,0 +1,125 @@ +#include "Queue.h" + +Status InitQueue(Queue* pQ) { + checkEmpty(pQ, "pQ"); + + pQ->rear = pQ->head = NULL; + return OK; +} +Status IsQueueEmpty(Queue Q, bool* pResult) { + checkQIllegal(Q); + checkEmpty(pResult, "pResult"); + + *pResult = !Q.rear && !Q.head; + return OK; +} +Status EnQueue(Queue* pQ, const Elemtype data) { + Node *p; + + checkEmpty(data, "data"); + checkQIllegal(*pQ); + + p = (Node*)malloc(sizeof(Node)); + if (!p) { + debug_print("p is NULL , return OVERFLOW \n"); + return OVERFLOW; + } + printf("\t[%s] Create new node p at %p\n",__FUNCTION__,p); + p->data = data; + if (isQueueEmpty(*pQ)) { + printf("\t[%s] Queue is empty . Set p->next and p->prev as NULL\n", __FUNCTION__); + printf("\t[%s] Set Q.rear and Q.head as p \n", __FUNCTION__); + p->next = p->prev = NULL; + (*pQ).rear = (*pQ).head = p; + return OK; + } + else { + printf("\t[%s] Queue is not empty . Set p->next as NULL and p->prev as Q.rear \n",__FUNCTION__); + p->next = NULL; + p->prev = (*pQ).rear; + printf("\t[%s] Set Q.rear->next as p and Q.rear as p \n",__FUNCTION__); + (*pQ).rear->next = p; + (*pQ).rear = p; + return OK; + } +} +Status DeQueue(Queue* pQ, Elemtype * pData) { + Node *p; + + checkEmpty(pData, "pData"); + checkQIllegal(*pQ); + checkQEmpty(*pQ); + + *pData = pQ->head->data; + + + if (sizeOfQueue(*pQ) == 1) { + printf("\t[%s] The size of queue is 1 . Delete Q.head and set Q.rear and Q.head as NULL \n",__FUNCTION__); + free(pQ->head); + pQ->rear = NULL; + pQ->head = NULL; + return OK; + } + else { + printf("\t[%s] The size of queue is bigger than 1 . Set Q.head as Q.head->next and delete Q.head->prev \n", __FUNCTION__); + pQ->head = pQ->head->next; + free(pQ->head->prev); + pQ->head->prev = NULL; + return OK; + } + +} +Status SizeOfQueue(const Queue Q, int* pResult) { + Node *p; + + checkEmpty(pResult, "pResult"); + checkQIllegal(Q); + + *pResult = 0; + if (isQueueEmpty(Q)) { + return OK; + } + for (p = Q.rear; p; p = p->prev) { + (*pResult) += 1; + } + return OK; +} +Status TraverseQueue(const Queue Q) { + Node* p; + + checkQIllegal(Q); + + if (isQueueEmpty(Q)) { + printf("(empty)"); + return OK; + } + for (p = Q.head; p != NULL; p = p->next) { + printf("%d ", p->data); + } + return OK; +} +Status GetHead(const Queue Q, Elemtype* pData) { + checkQIllegal(Q); + checkQEmpty(Q); + checkEmpty(pData, "pData"); + + *pData = Q.head->data; + return OK; +} +Status ClearQueue(Queue* pQ) { + Node *next, *p; + + checkEmpty(pQ, "pQ"); + checkQIllegal(*pQ); + + if (isQueueEmpty(*pQ)) { + return OK; + } + for (p = pQ->head; p; p = next) { + next = p->next; + free(p); + p = next; + } + pQ->head = pQ->rear = NULL; + return OK; +} \ No newline at end of file diff --git a/2017-1/zkz/Queue/Queue.h b/2017-1/zkz/Queue/Queue.h new file mode 100644 index 00000000..3b188d14 --- /dev/null +++ b/2017-1/zkz/Queue/Queue.h @@ -0,0 +1,53 @@ +#pragma once + +#include +#include +#include "Status.h" + +/*异或*/ +#define XOR(a,b) ((a)&&!(b) || (b)&&!(a)) + +/*下面这些首字母小写的和函数同名的宏*/ +/*用于简化对应函数的调用过程*/ +#define isQueueEmpty(Q) (IsQueueEmpty((Q),&global_isQueueEmpty),global_isQueueEmpty) +#define enQueue(Q,data) EnQueue(&(Q),(data)) +#define deQueue(Q,data) (DeQueue(&(Q),&global_deQueue),global_deQueue) +#define sizeOfQueue(Q) (SizeOfQueue(Q,&global_sizeOfQueue),global_sizeOfQueue) +#define getHead(Q) (GetHead(Q,&global_getHead),global_getHead) +#define clearQueue(Q) ClearQueue(&(Q)) + +#define DEBUG 1 +#define debug_print(...) if(DEBUG)printf(__VA_ARGS__) + +/*以下check开头的宏用于在函数入口处做参数检查*/ +/*并在参数异常时打印相关信息*/ +#define checkQIllegal(Q) if(XOR((Q).rear,(Q).head)){ debug_print("Q is broken , return ERROR \n");return ERROR;} +#define checkQEmpty(Q) if(isQueueEmpty(Q)){debug_print("Q is empty , return ERROR \n");return ERROR;} +#define checkEmpty(var,var_name) if(!(var)){debug_print("%s is empty , return ERROR \n",var_name);return ERROR;} + +typedef int Elemtype; + +typedef struct _Node { + Elemtype data; + struct _Node* prev; + struct _Node* next; +}Node; +typedef struct _Queue { + Node * head; + Node* rear; +}Queue; +typedef enum _bool { false, true } bool; + +bool global_isQueueEmpty; +Elemtype global_deQueue; +int global_sizeOfQueue; +Elemtype global_getHead; + +Status InitQueue(Queue*); +Status IsQueueEmpty(Queue, bool*); +Status EnQueue(Queue*, const Elemtype); +Status DeQueue(Queue*, Elemtype *); +Status SizeOfQueue(const Queue, int*); +Status TraverseQueue(const Queue); +Status GetHead(const Queue, Elemtype*); +Status ClearQueue(Queue*); \ No newline at end of file diff --git a/2017-1/zkz/Queue/Status.h b/2017-1/zkz/Queue/Status.h new file mode 100644 index 00000000..e35e39c9 --- /dev/null +++ b/2017-1/zkz/Queue/Status.h @@ -0,0 +1,26 @@ +#pragma once + +#include +typedef enum _Status { OK, ERROR, OVERFLOW } Status; +#define CHECK(func) \ +do {\ + _Statu = func;\ + switch (_Statu) {\ + case OK:\ + break;\ + case ERROR:\ + printf("ERROR\n");\ + return 0;\ + break;\ + case OVERFLOW:\ + printf("OVERFLOW\n");\ + return 0;\ + break;\ + default:\ + printf("UNDEFINED\n");\ + return 0;\ + break;\ + }\ +} while (0); + +int _Statu; \ No newline at end of file diff --git a/2017-1/zkz/Queue/test.c b/2017-1/zkz/Queue/test.c new file mode 100644 index 00000000..d67f8855 --- /dev/null +++ b/2017-1/zkz/Queue/test.c @@ -0,0 +1,58 @@ +#include "Queue.h" + +/*这些test+函数名的宏*/ +/*用于测试对应函数运行是否正常*/ +/*并打印相关信息*/ +#define testInitQueue(Q) debug_print("Initialize queue Q \n");CHECK(InitQueue(&(Q))); +#define testTraverseQueue(Q) debug_print("Traverse Q : ");CHECK(TraverseQueue((Q)));printf("\n\n"); +#define testEnQueue(Q,data) debug_print("Enqueue %d \n",(data));CHECK(EnQueue(&(Q), (data))); +#define testGetHead(Q,pData) debug_print("Get head of Q \n");CHECK(GetHead((Q),(pData)));debug_print("The result is : %d \n\n",*(pData)); +#define testDeQueue(Q,pData) debug_print("Dequeue \n");CHECK(DeQueue(&(Q), (pData)));debug_print("The result is : %d \n\n",*(pData)); +#define testSizeOfQueue(Q,pResult) debug_print("Get size of Q \n");CHECK(SizeOfQueue((Q),(pResult)));debug_print("The result is : %d \n\n",*(pResult)); +#define testClearQueue(Q) debug_print("Clear queue Q \n");CHECK(ClearQueue(&(Q))); + +int main(void) { + Queue Q; + Elemtype data;/*存放DeQueue函数和GetHead函数的返回结果*/ + int size;/*存放SizeOfQueue函数的返回结果*/ + + testInitQueue(Q); + testTraverseQueue(Q); + testSizeOfQueue(Q, &size); + + testEnQueue(Q, 5); + testTraverseQueue(Q); + testSizeOfQueue(Q, &size); + + testEnQueue(Q, 15); + testTraverseQueue(Q); + testSizeOfQueue(Q, &size); + + testEnQueue(Q, 25); + testTraverseQueue(Q); + testSizeOfQueue(Q, &size); + + testGetHead(Q, &data); + + testDeQueue(Q, &data); + testTraverseQueue(Q); + + testDeQueue(Q, &data); + testTraverseQueue(Q); + + testDeQueue(Q, &data); + testTraverseQueue(Q); + + testEnQueue(Q, 1); + testEnQueue(Q, 2); + testEnQueue(Q, 3); + testTraverseQueue(Q); + testClearQueue(Q); + + testTraverseQueue(Q); + + /*在队空时出队,返回ERROR,程序中止*/ + testDeQueue(Q, &data); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/zkz/Queue/\350\277\220\350\241\214\347\273\223\346\236\234(\345\261\200\351\203\250).png" "b/2017-1/zkz/Queue/\350\277\220\350\241\214\347\273\223\346\236\234(\345\261\200\351\203\250).png" new file mode 100644 index 00000000..88b56d7f Binary files /dev/null and "b/2017-1/zkz/Queue/\350\277\220\350\241\214\347\273\223\346\236\234(\345\261\200\351\203\250).png" differ diff --git a/2017-1/zkz/Sort/Sort.c b/2017-1/zkz/Sort/Sort.c new file mode 100644 index 00000000..ed1aa64b --- /dev/null +++ b/2017-1/zkz/Sort/Sort.c @@ -0,0 +1,195 @@ +#include +#include +#include "Sort.h" +#define min(a,b) ((a)<(b) ? (a) : (b)) + + +void ShowArr(RecordType* arr, int length) { + for (int i = 0; i < length; ++i) { + printf("%d ", arr[i].key); + } + printf("\n"); +} +void _InsertSort(RecordType*arr, int length, int delta, int * ct, int* mt) { + RecordType temp; + for (int i = delta; i < length; i += delta) { + temp = arr[i]; + int j = i; + while (++*ct, arr[j - delta].key > temp.key && j > 0) { + arr[j] = arr[j - delta]; + j -= delta; + ++*mt; + } + arr[j] = temp; + ++*mt; + } +} +void InsertSort(RecordType*arr, int length, int * ct, int* mt) { + *ct = *mt = 0; + _InsertSort(arr, length, 1, ct, mt); +} +void ShellSort(RecordType*arr, int length, int * ct, int* mt) { + *ct = *mt = 0; + int d = length / 2; + while (d >= 1) { + //printf("d = %d \n",d); + for (int i = 0; i < d; ++i) { + _InsertSort(arr + i, length - i, d, ct, mt); + } + //ShowArr(arr, length); + d /= 2; + } +} +void BubbleSort(RecordType*arr, int length, int *ct, int* mt) { + *ct = *mt = 0; + for (int end = length - 1; end > 0; --end) { + for (int i = 0; i < end; ++i) { + if (++*ct, arr[i].key > arr[i + 1].key) { + RecordType temp = arr[i]; + arr[i] = arr[i + 1]; + arr[i + 1] = temp; + (*mt) += 3; + } + } + } +} +void _QuickSort(RecordType*arr, int length, int *ct, int* mt) { + RecordType pivot = arr[0]; + if (length <= 1)return; + //printf("SortArr : "); + //ShowArr(arr, length); + int i = 1, j = length - 1; + while (j >= i) { + while (++*ct, arr[i].key <= pivot.key && j >= i)++i; + while (++*ct, arr[j].key > pivot.key && j >= i)--j; + if (j < i) { + //printf("Swap %d and %d\n", arr[0].key, arr[j].key); + RecordType temp = arr[0]; + arr[0] = arr[j]; + arr[j] = temp; + (*mt) += 3; + break; + } + //printf("Swap %d and %d\n", arr[i].key, arr[j].key); + RecordType temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + (*mt) += 3; + } + //printf("AfterSort : "); + //ShowArr(arr, length); + _QuickSort(arr, j, ct, mt); + _QuickSort(arr + j + 1, length - j - 1, ct, mt); +} +void QuickSort(RecordType*arr, int length, int *ct, int* mt) { + *ct = *mt = 0; + _QuickSort(arr, length, ct, mt); +} +void _SelectSort(RecordType*arr, int length, int *ct, int* mt) { + int id = 0; + for (int i = 1; i < length; ++i) { + if (++*ct, arr[i].key < arr[id].key) { + id = i; + } + } + RecordType temp = arr[0]; + arr[0] = arr[id]; + arr[id] = temp; + (*mt) += 3; +} +void SelectSort(RecordType*arr, int length, int *ct, int* mt) { + *ct = *mt = 0; + for (int i = 0; i < length - 1; ++i) { + _SelectSort(arr + i, length - i, ct, mt); + } +} + +int getLeft(int id, int length) { + id = 2 * id + 1; + return id < 0 || id >= length ? -1 : id; +} +int getRight(int id, int length) { + id = 2 * id + 2; + return id < 0 || id >= length ? -1 : id; +} +void CreateHeap(RecordType* arr, int size, int*ct, int*mt) { + int left, right, toSwap = -1; + for (int j = size / 2 - 1; j >= 0; --j) { + int current = j; + do { + //ShowArr(temp, size); + //printf("当前元素 : 第%2d个元素%2d\n",current+1,temp[current]); + left = getLeft(current, size); + right = getRight(current, size); + //printf(left == -1 ? "它没有左孩子\n" : "它的左孩子为%d\n", left == -1 ? 999 : temp[left].key); + //printf(right == -1 ? "它没有右孩子\n" : "它的右孩子为%d\n", right == -1 ? 999 : temp[right].key); + ++*ct; + if (left == -1 && right != -1)toSwap = right; + else if (left != -1 && right == -1)toSwap = left; + else if (left != -1 && right != -1)toSwap = arr[left].key < arr[right].key ? left : right; + if (arr[toSwap].key >= arr[current].key)toSwap = -1; + if (toSwap == -1)break; + RecordType t = arr[current]; + arr[current] = arr[toSwap]; + arr[toSwap] = t; + (*mt) += 3; + current = toSwap; + toSwap = -1; + } while (1); + }//end for +} +void _HeapSort(RecordType* arr, int length, int*ct, int* mt) { + if (length <= 1)return; + CreateHeap(arr, length, ct, mt); + _HeapSort(arr + 1, length - 1, ct, mt); +} +void HeapSort(RecordType* arr, int length, int*ct, int* mt) { + *ct = *mt = 0; + _HeapSort(arr, length, ct, mt); +} + +/*合并一个数组中的两个有序部分*/ +void _Merge(RecordType* left, RecordType*right, RecordType* end, int*ct, int*mt) { + //printf(__FUNCTION__); + //ShowArr(left, end - left + 1); + RecordType* temp = (RecordType*)malloc((end - left + 1) * sizeof(RecordType)); + RecordType* pl = left, *pr = right, *p = temp; + while (pl < right && pr <= end) { + if (++*ct, pl->key < pr->key) { + *p = *pl; ++p; ++pl; + ++*mt; + } + else { + *p = *pr; ++p; ++pr; + ++*mt; + } + } + while (pl < right) { + *p = *pl; ++p; ++pl; + ++*mt; + } + while (pr <= end) { + *p = *pr; ++p; ++pr; + ++*mt; + } + --p; + while (p >= temp) { + left[p - temp] = *p; + --p; + ++*mt; + } +} +void _MergeSort(RecordType* arr, int length, int *ct, int* mt) { + //printf(__FUNCTION__); + //ShowArr(arr, length); + if (length >= 3) { + _MergeSort(arr, length / 2, ct, mt); + _MergeSort(arr + length / 2, length - length / 2, ct, mt); + } + _Merge(arr, arr + length / 2, arr + length - 1, ct, mt); + //ShowArr(arr, length); +} +void MergeSort(RecordType* arr, int length, int *ct, int* mt) { + *ct = *mt = 0; + _MergeSort(arr, length, ct, mt); +} \ No newline at end of file diff --git a/2017-1/zkz/Sort/Sort.h b/2017-1/zkz/Sort/Sort.h new file mode 100644 index 00000000..9a3957af --- /dev/null +++ b/2017-1/zkz/Sort/Sort.h @@ -0,0 +1,17 @@ +#pragma once + +typedef int KeyType; +typedef int DataType; +typedef struct _RecordType { + KeyType key; + DataType data; +} RecordType; + +void ShowArr(RecordType* arr, int length); +void InsertSort(RecordType*arr, int length, int * ct, int* mt); +void ShellSort(RecordType*arr, int length, int * ct, int* mt); +void BubbleSort(RecordType*arr, int length, int *ct, int* mt); +void QuickSort(RecordType*arr, int length, int *ct, int* mt); +void SelectSort(RecordType*arr, int length, int *ct, int* mt); +void HeapSort(RecordType* arr, int length, int*ct, int* mt); +void MergeSort(RecordType* arr, int length, int *ct, int* mt); \ No newline at end of file diff --git a/2017-1/zkz/Sort/test.c b/2017-1/zkz/Sort/test.c new file mode 100644 index 00000000..f6073a16 --- /dev/null +++ b/2017-1/zkz/Sort/test.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include "Sort.h" + +#define ShowTimes() printf("compareTime : \t%d\nmoveTime : \t%d\ntotal : \t%d\n\n",compareTime,moveTime,compareTime+moveTime); +/*生成测试用例*/ +void GenTestInputs(RecordType*arr, int size) { + for (int i = 0; i < size; ++i) { + arr[i].key = rand() % 90 + 10; + arr[i].data = -1; + } +} +void CopyArr(RecordType* dest, const RecordType* source, int size) { + for (int i = 0; i < size; ++i) { + dest[i] = source[i]; + } +} +int main() { + srand(time(0) ^ rand()); + int compareTime = 0, moveTime = 0; + const int length = rand() % 5 ? rand() % 9 + 1 : rand() % 19 + 80; + RecordType* origin = (RecordType*)malloc(length * sizeof(RecordType)); + RecordType* arr = (RecordType*)malloc(length * sizeof(RecordType)); + GenTestInputs(origin, length); + + printf("【Before sort】\n"); + ShowArr(origin, length); + printf("\n"); + + /*插入排序*/ + printf("【After InsertSort】\n"); + CopyArr(arr, origin, length); + InsertSort(arr, length, &compareTime, &moveTime); + ShowArr(arr, length); + ShowTimes(); + + /*希尔排序*/ + printf("【After ShellSort】\n"); + CopyArr(arr, origin, length); + ShellSort(arr, length, &compareTime, &moveTime); + ShowArr(arr, length); + ShowTimes(); + + /*冒泡排序*/ + printf("【After BubbleSort】\n"); + CopyArr(arr, origin, length); + BubbleSort(arr, length, &compareTime, &moveTime); + ShowArr(arr, length); + ShowTimes(); + + /*快速排序*/ + printf("【After QuickSort】\n"); + CopyArr(arr, origin, length); + QuickSort(arr, length, &compareTime, &moveTime); + ShowArr(arr, length); + ShowTimes(); + + /*选择排序*/ + printf("【After SelectSort】\n"); + CopyArr(arr, origin, length); + SelectSort(arr, length, &compareTime, &moveTime); + ShowArr(arr, length); + ShowTimes(); + + /*堆排序*/ + printf("【After HeapSort】\n"); + CopyArr(arr, origin, length); + HeapSort(arr, length, &compareTime, &moveTime); + ShowArr(arr, length); + ShowTimes(); + + /*归并排序*/ + printf("【After MergeSort】\n"); + CopyArr(arr, origin, length); + MergeSort(arr, length, &compareTime, &moveTime); + ShowArr(arr, length); + ShowTimes(); + +} \ No newline at end of file diff --git "a/2017-1/zkz/Sort/\350\276\223\345\207\272\347\273\223\346\236\234\357\274\210\346\265\213\350\257\225\346\225\260\347\273\204\344\270\272\351\225\277\346\225\260\347\273\204\357\274\211.txt" "b/2017-1/zkz/Sort/\350\276\223\345\207\272\347\273\223\346\236\234\357\274\210\346\265\213\350\257\225\346\225\260\347\273\204\344\270\272\351\225\277\346\225\260\347\273\204\357\274\211.txt" new file mode 100644 index 00000000..a6a9ebbb --- /dev/null +++ "b/2017-1/zkz/Sort/\350\276\223\345\207\272\347\273\223\346\236\234\357\274\210\346\265\213\350\257\225\346\225\260\347\273\204\344\270\272\351\225\277\346\225\260\347\273\204\357\274\211.txt" @@ -0,0 +1,44 @@ +【Before sort】 +99 59 97 82 58 20 95 83 74 33 97 14 37 43 85 51 90 34 16 31 20 27 52 94 15 33 18 72 14 89 44 36 87 88 20 23 81 48 80 25 79 94 59 75 48 82 87 67 31 33 79 20 18 32 72 69 81 94 50 18 13 93 72 18 72 38 91 73 74 66 26 87 35 79 23 34 44 57 11 30 74 43 77 90 92 29 36 + +【After InsertSort】 +11 13 14 14 15 16 18 18 18 18 20 20 20 20 23 23 25 26 27 29 30 31 31 32 33 33 33 34 34 35 36 36 37 38 43 43 44 44 48 48 50 51 52 57 58 59 59 66 67 69 72 72 72 72 73 74 74 74 75 77 79 79 79 80 81 81 82 82 83 85 87 87 87 88 89 90 90 91 92 93 94 94 94 95 97 97 99 +compareTime : 2030 +moveTime : 2030 +total : 4060 + +【After ShellSort】 +11 13 14 14 15 16 18 18 18 18 20 20 20 20 23 23 25 26 27 29 30 31 31 32 33 33 33 34 34 35 36 36 37 38 43 43 44 44 48 48 50 51 52 57 58 59 59 66 67 69 72 72 72 72 73 74 74 74 75 77 79 79 79 80 81 81 82 82 83 85 87 87 87 88 89 90 90 91 92 93 94 94 94 95 97 97 99 +compareTime : 731 +moveTime : 731 +total : 1462 + +【After BubbleSort】 +11 13 14 14 15 16 18 18 18 18 20 20 20 20 23 23 25 26 27 29 30 31 31 32 33 33 33 34 34 35 36 36 37 38 43 43 44 44 48 48 50 51 52 57 58 59 59 66 67 69 72 72 72 72 73 74 74 74 75 77 79 79 79 80 81 81 82 82 83 85 87 87 87 88 89 90 90 91 92 93 94 94 94 95 97 97 99 +compareTime : 3741 +moveTime : 5832 +total : 9573 + +【After QuickSort】 +11 13 14 14 15 16 18 18 18 18 20 20 20 20 23 23 25 26 27 29 30 31 31 32 33 33 33 34 34 35 36 36 37 38 43 43 44 44 48 48 50 51 52 57 58 59 59 66 67 69 72 72 72 72 73 74 74 74 75 77 79 79 79 80 81 81 82 82 83 85 87 87 87 88 89 90 90 91 92 93 94 94 94 95 97 97 99 +compareTime : 877 +moveTime : 417 +total : 1294 + +【After SelectSort】 +11 13 14 14 15 16 18 18 18 18 20 20 20 20 23 23 25 26 27 29 30 31 31 32 33 33 33 34 34 35 36 36 37 38 43 43 44 44 48 48 50 51 52 57 58 59 59 66 67 69 72 72 72 72 73 74 74 74 75 77 79 79 79 80 81 81 82 82 83 85 87 87 87 88 89 90 90 91 92 93 94 94 94 95 97 97 99 +compareTime : 3741 +moveTime : 258 +total : 3999 + +【After HeapSort】 +11 13 14 14 15 16 18 18 18 18 20 20 20 20 23 23 25 26 27 29 30 31 31 32 33 33 33 34 34 35 36 36 37 38 43 43 44 44 48 48 50 51 52 57 58 59 59 66 67 69 72 72 72 72 73 74 74 74 75 77 79 79 79 80 81 81 82 82 83 85 87 87 87 88 89 90 90 91 92 93 94 94 94 95 97 97 99 +compareTime : 2157 +moveTime : 795 +total : 2952 + +【After MergeSort】 +11 13 14 14 15 16 18 18 18 18 20 20 20 20 23 23 25 26 27 29 30 31 31 32 33 33 33 34 34 35 36 36 37 38 43 43 44 44 48 48 50 51 52 57 58 59 59 66 67 69 72 72 72 72 73 74 74 74 75 77 79 79 79 80 81 81 82 82 83 85 87 87 87 88 89 90 90 91 92 93 94 94 94 95 97 97 99 +compareTime : 454 +moveTime : 1182 +total : 1636 \ No newline at end of file diff --git "a/2017-1/zkz/Sort/\350\276\223\345\207\272\347\273\223\346\236\234\357\274\210\346\265\213\350\257\225\346\225\260\347\273\204\351\225\277\345\272\246\344\270\2722\357\274\211.txt" "b/2017-1/zkz/Sort/\350\276\223\345\207\272\347\273\223\346\236\234\357\274\210\346\265\213\350\257\225\346\225\260\347\273\204\351\225\277\345\272\246\344\270\2722\357\274\211.txt" new file mode 100644 index 00000000..6802eb87 --- /dev/null +++ "b/2017-1/zkz/Sort/\350\276\223\345\207\272\347\273\223\346\236\234\357\274\210\346\265\213\350\257\225\346\225\260\347\273\204\351\225\277\345\272\246\344\270\2722\357\274\211.txt" @@ -0,0 +1,44 @@ +【Before sort】 +62 72 + +【After InsertSort】 +62 72 +compareTime : 1 +moveTime : 1 +total : 2 + +【After ShellSort】 +62 72 +compareTime : 1 +moveTime : 1 +total : 2 + +【After BubbleSort】 +62 72 +compareTime : 1 +moveTime : 0 +total : 1 + +【After QuickSort】 +62 72 +compareTime : 3 +moveTime : 3 +total : 6 + +【After SelectSort】 +62 72 +compareTime : 1 +moveTime : 3 +total : 4 + +【After HeapSort】 +62 72 +compareTime : 1 +moveTime : 0 +total : 1 + +【After MergeSort】 +62 72 +compareTime : 1 +moveTime : 4 +total : 5 \ No newline at end of file diff --git "a/2017-1/zkz/Sort/\350\276\223\345\207\272\347\273\223\346\236\234\357\274\210\346\265\213\350\257\225\346\225\260\347\273\204\351\225\277\345\272\246\344\270\2729\357\274\211.txt" "b/2017-1/zkz/Sort/\350\276\223\345\207\272\347\273\223\346\236\234\357\274\210\346\265\213\350\257\225\346\225\260\347\273\204\351\225\277\345\272\246\344\270\2729\357\274\211.txt" new file mode 100644 index 00000000..7141218b --- /dev/null +++ "b/2017-1/zkz/Sort/\350\276\223\345\207\272\347\273\223\346\236\234\357\274\210\346\265\213\350\257\225\346\225\260\347\273\204\351\225\277\345\272\246\344\270\2729\357\274\211.txt" @@ -0,0 +1,44 @@ +【Before sort】 +19 60 15 43 44 61 59 53 78 + +【After InsertSort】 +15 19 43 44 53 59 60 61 78 +compareTime : 17 +moveTime : 17 +total : 34 + +【After ShellSort】 +15 19 43 44 53 59 60 61 78 +compareTime : 27 +moveTime : 27 +total : 54 + +【After BubbleSort】 +15 19 43 44 53 59 60 61 78 +compareTime : 36 +moveTime : 27 +total : 63 + +【After QuickSort】 +15 19 43 44 53 59 60 61 78 +compareTime : 37 +moveTime : 24 +total : 61 + +【After SelectSort】 +15 19 43 44 53 59 60 61 78 +compareTime : 36 +moveTime : 24 +total : 60 + +【After HeapSort】 +15 19 43 44 53 59 60 61 78 +compareTime : 27 +moveTime : 21 +total : 48 + +【After MergeSort】 +15 19 43 44 53 59 60 61 78 +compareTime : 20 +moveTime : 60 +total : 80 \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/Stack.c" "b/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/Stack.c" new file mode 100644 index 00000000..1de69178 --- /dev/null +++ "b/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/Stack.c" @@ -0,0 +1,98 @@ +#include "Stack.h" + +Status InitStack(Stack * pS) { + if (!pS) + return ERROR; + /*创建top节点*/ + (*pS) = (Stack)malloc(sizeof(Node)); + if (!*pS) + return OVERFLOW; + /*为top节点赋值*/ + (*pS)->next = NULL; + return OK; +} +Status Push(Stack S, Elemtype data) { + Node * p; + if (!S) + return ERROR; + /*Step 1 创建新节点*/ + p = (Node *)malloc(sizeof(Node)); + if (!p) + return OVERFLOW; + /*Step 2 为新节点赋值*/ + p->data = data; + p->next = NULL; + /*Step 3 将新节点入栈*/ + p->next = S->next; + S->next = p; + return OK; +} +Status Pop(Stack S, Elemtype *pdata) { + Node * p; + if (!S || !pdata || !S->next) + return ERROR; + /*Step 1 将顶节点出栈*/ + p = S->next; + S->next = p->next; + /*Step 2 获取节点数据*/ + *pdata = p->data; + /*Step 3 销毁节点*/ + free(p); + return OK; +} +/*Traverse : 从栈顶向栈底遍历*/ +Status Traverse(const Stack S) { + if (!S) + return ERROR; + if (!S->next) + printf("(empty)"); + Node *pS = S->next; + while (pS) { + printf("%d ", pS->data); + pS = pS->next; + } + return OK; +} +Status DeleteStack(Stack *pS) { + Node *p, *next; + if (!pS) + return ERROR; + p = (*pS)->next; + while (p) { + next = p->next; + free(p); + p = next; + } + free(*pS); + *pS = NULL; + return OK; +} +Status EmptyStack(Stack S) { + if (!S) + return ERROR; + DeleteStack(&S->next); + return OK; +} +Status IsStackEmpty(Stack S, int *pResult) { + if (!S) + return ERROR; + *pResult = !S->next; + return OK; +} +Status SizeOfStack(const Stack S, int *pN) { + if (!S || !pN) + return ERROR; + Node *pS = S->next; + *pN = 0; + while (pS) { + ++*pN; + pS = pS->next; + }; + return OK; +} +Status GetTop(const Stack S, Elemtype *pdata) { + if (!S || !pdata || !S->next) + return ERROR; + *pdata = S->next->data; + return OK; +} \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/Stack.h" "b/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/Stack.h" new file mode 100644 index 00000000..91dc3a92 --- /dev/null +++ "b/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/Stack.h" @@ -0,0 +1,21 @@ +#pragma once + +#include +#include "Status.h" +typedef int Elemtype; + +typedef struct _Node { + Elemtype data; + struct _Node* next; +} Node, *Stack; + +Status InitStack(Stack *); +Status Push(Stack, Elemtype); +Status Pop(Stack, Elemtype *); +/*Traverse : 从栈顶向栈底遍历*/ +Status Traverse(const Stack); +Status DeleteStack(Stack *); +Status EmptyStack(Stack); +Status IsStackEmpty(Stack, int *); +Status SizeOfStack(const Stack, int *); +Status GetTop(const Stack, Elemtype *); \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/Status.h" "b/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/Status.h" new file mode 100644 index 00000000..e35e39c9 --- /dev/null +++ "b/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/Status.h" @@ -0,0 +1,26 @@ +#pragma once + +#include +typedef enum _Status { OK, ERROR, OVERFLOW } Status; +#define CHECK(func) \ +do {\ + _Statu = func;\ + switch (_Statu) {\ + case OK:\ + break;\ + case ERROR:\ + printf("ERROR\n");\ + return 0;\ + break;\ + case OVERFLOW:\ + printf("OVERFLOW\n");\ + return 0;\ + break;\ + default:\ + printf("UNDEFINED\n");\ + return 0;\ + break;\ + }\ +} while (0); + +int _Statu; \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/test.c" "b/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/test.c" new file mode 100644 index 00000000..eb19660f --- /dev/null +++ "b/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/test.c" @@ -0,0 +1,191 @@ +#include +#include +#include +#include "Status.h" +#include "Stack.h" + +#define DEBUG 1 +#define pare(l,r) \ + case (l):\ + DEBUG && printf("入栈 \n");\ + Push(S, *brackets);\ + break;\ + case (r):\ + DEBUG && printf("出栈 , ");\ + Pop(S, &bracket);\ + DEBUG && printf("结果为 %c \n",bracket);\ + if (bracket != (*brackets == ')' ? '(' : *brackets == ']' ? '[' : '{')) {\ + DEBUG && printf(" 配对失败\n");\ + *pResult = 0;\ + return;\ + }\ + break;//end pare(l,r) +#define check(s) \ + printf("对\"%s\" 进行匹配\n", (s));\ + checkBrackets((s), &result);\ + printf(" \"%s\" 的配对结果为 %d \n", (s), result);\ + +int testStack() { + printf("=== 基础测试 ===\n"); + Stack S1 = NULL; + int n, isEmpty , size; + CHECK(InitStack(&S1)); + printf("初始化栈\n"); + CHECK(SizeOfStack(S1,&size)); + printf("栈大小为 %d \n",size); + CHECK(Push(S1, 5)); + printf("数字 5 入栈\n"); + CHECK(SizeOfStack(S1, &size)); + printf("栈大小为 %d \n", size); + printf("遍历 : "); + CHECK(Traverse(S1)); + printf("\n"); + CHECK(Push(S1, 9)); + printf("数字 9 入栈\n"); + CHECK(SizeOfStack(S1, &size)); + printf("栈大小为 %d \n", size); + printf("遍历 : "); + CHECK(Traverse(S1)); + printf("\n"); + CHECK(IsStackEmpty(S1, &isEmpty)); + printf("此时栈%s为空 \n", isEmpty ? "" : "不"); + CHECK(Pop(S1,&n)); + printf("出栈 , 结果为 %d \n",n); + printf("遍历 : "); + CHECK(Traverse(S1)); + printf("\n"); + CHECK(EmptyStack(S1)); + printf("将栈清空\n"); + CHECK(SizeOfStack(S1, &size)); + printf("栈大小为 %d \n", size); + printf("遍历 : "); + CHECK(Traverse(S1)); + printf("\n\n"); + + return 0; +} +void conversion(int n,int d) { + Stack S; + int data,isEmpty; + InitStack(&S); + DEBUG && printf(" 初始化栈\n"); + DEBUG && printf(" n = %d , d = %d \n",n,d); + while (n) { + Push(S, n%d); + DEBUG && printf(" n%%d= %d 入栈 , n/=d \n",n%d); + n /= d; + } + IsStackEmpty(S,&isEmpty); + DEBUG && !isEmpty && printf(" 栈不为空 , 开始输出结果\n"); + while (!isEmpty) { + Pop(S, &data); + printf("%d",data); + IsStackEmpty(S, &isEmpty); + } + DEBUG && printf("\n 栈为空 , 循环结束"); +} +void checkBrackets(char * brackets,int* pResult) { + Stack S; + int bracket,isEmpty; + InitStack(&S); + while (*brackets != '\0') { + DEBUG && printf(" 当前括号 %c , ",*brackets); + switch (*brackets) { + pare('(',')') + pare('[',']') + pare('{','}') + default: + break; + }/*end switch*/ + ++brackets; + }/*end while*/ + IsStackEmpty(S,&isEmpty); + DEBUG && printf(" 匹配结束 , 栈%s为空 \n", isEmpty ? "" : "不"); + *pResult = isEmpty; +} +void lineEdit(char * source , char ** pTarget) { + Stack S; + char * p = source; + int size ,i=0 , isEmpty; + Elemtype data; + InitStack(&S); + DEBUG && printf(" 初始化栈 \n"); + do { + DEBUG && printf(" 当前字符为 %c , ",*p); + if (*p == '#') { + DEBUG && printf("退格符 , 执行出栈操作 , "); + Pop(S, &data); + DEBUG && printf("出栈结果为 %c \n",data); + } + else if (*p == '@') { + DEBUG && printf("退行符 , 执行连续出栈操作 :"); + Push(S, (Elemtype)*p); + do { + Pop(S, &data); + DEBUG && printf(" %c",data); + IsStackEmpty(S, &isEmpty); + } while (!isEmpty && data != '\n'); + !isEmpty && Push(S, (Elemtype)'\n'); + } + else { + DEBUG && printf("入栈 \n"); + Push(S, (Elemtype)*p); + } + ++p; + } while (*p != '\0'); + Push(S, (Elemtype)*p); + DEBUG && printf(" 开始将栈中内容拷入新字符串中\n"); + DEBUG && printf(" 出栈结果 :"); + SizeOfStack(S,&size); + *pTarget = (char*)malloc(size *sizeof(char)); + for (i = size-1; i >= 0; --i) { + Pop(S,&data); + DEBUG && printf(" %c",data); + (*pTarget)[i] = (char)data; + } + DEBUG && printf("\n"); +} +void doSomeConversion() { + printf("=== 数制转换 ===\n"); + int n; + srand((unsigned int)time(NULL)); + n = rand() % 1024; + printf("n = %d \n",n); + printf("convert n to Bin : "); + DEBUG && printf("\n"); + conversion(n, 2); + printf("\n"); + printf("convert n to Aug : "); + DEBUG && printf("\n"); + conversion(n, 8); + printf("\n"); + printf("\n"); +} +void doSomeCheckBrackets() { + printf("=== 括号匹配 ===\n"); + int result; + check("([]{()})"); + check("}"); + check("({(]"); + printf("\n"); +} +void doSomeLineEdit() { + printf("=== 行编辑器 ===\n"); + char *sa1 = "whli##ilr#e ( s#*s)\noutcha@putchar(*s=#++);"; + char *ta1; + char *sa2 = "Mrs.##.Zong###hong is the f**king#######most\nugly@handsome man."; + char *ta2; + printf("(1) 输入为 : \n%s\n", sa1); + lineEdit(sa1, &ta1); + printf("输出为 : \n%s\n", ta1); + printf("(2) 输入为 : \n%s\n", sa2); + lineEdit(sa2, &ta2); + printf("输出为 : \n%s\n", ta2); +} +int main() { + DEBUG && testStack(); + doSomeConversion(); + doSomeCheckBrackets(); + doSomeLineEdit(); + return 0; +} \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/\350\277\220\350\241\214\347\273\223\346\236\234(DEBUG = 0).png" "b/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/\350\277\220\350\241\214\347\273\223\346\236\234(DEBUG = 0).png" new file mode 100644 index 00000000..1bfb6b41 Binary files /dev/null and "b/2017-1/zkz/Stack/\346\225\260\345\200\274\350\275\254\346\215\242+\346\213\254\345\217\267\345\214\271\351\205\215+\350\241\214\347\274\226\350\276\221\345\231\250/\350\277\220\350\241\214\347\273\223\346\236\234(DEBUG = 0).png" differ diff --git "a/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Stack.c" "b/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Stack.c" new file mode 100644 index 00000000..93c9b638 --- /dev/null +++ "b/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Stack.c" @@ -0,0 +1,106 @@ +#include "Stack.h" + +Status InitStack(Stack * pS) { + if (!pS) + return ERROR; + /*创建top节点*/ + (*pS) = (Stack)malloc(sizeof(Node)); + if (!*pS) + return OVERFLOW; + /*为top节点赋值*/ + (*pS)->next = NULL; + return OK; +} +Status Push(Stack S, Elemtype data) { + Node * p; + if (!S) + return ERROR; + /*Step 1 创建新节点*/ + p = (Node *)malloc(sizeof(Node)); + if (!p) + return OVERFLOW; + /*Step 2 为新节点赋值*/ + p->data = data; + p->next = NULL; + /*Step 3 将新节点入栈*/ + p->next = S->next; + S->next = p; + return OK; +} +Status Pop(Stack S, Elemtype *pdata) { + Node * p; + if (!S || !pdata || !S->next) + return ERROR; + + /*Step 1 将顶节点出栈*/ + p = S->next; + S->next = p->next; + /*Step 2 获取节点数据*/ + *pdata = p->data; + /*Step 3 销毁节点*/ + free(p); + return OK; +} +/*Traverse : 从栈底向栈顶遍历*/ +Status Traverse(const Stack S) { + if (!S) + return ERROR; + if (!S->next) { + printf("(empty)"); + return OK; + } + /*如果除头节点外只剩一个节点*/ + if (!S->next->next) { + printf("%c",S->next->data); + return OK; + } + else { + Traverse(S->next); + printf("%c", S->next->data); + } + /*如果除头节点外有多个节点*/ + return OK; +} +Status DeleteStack(Stack *pS) { + Node *p, *next; + if (!pS) + return ERROR; + p = (*pS)->next; + while (p) { + next = p->next; + free(p); + p = next; + } + free(*pS); + *pS = NULL; + return OK; +} +Status EmptyStack(Stack S) { + if (!S) + return ERROR; + DeleteStack(&S->next); + return OK; +} +Status IsStackEmpty(Stack S, bool *pResult) { + if (!S) + return ERROR; + *pResult = !S->next; + return OK; +} +Status SizeOfStack(const Stack S, int *pN) { + if (!S || !pN) + return ERROR; + Node *pS = S->next; + *pN = 0; + while (pS) { + ++*pN; + pS = pS->next; + }; + return OK; +} +Status GetTop(const Stack S, Elemtype *pdata) { + if (!S || !pdata || !S->next) + return ERROR; + *pdata = S->next->data; + return OK; +} \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Stack.h" "b/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Stack.h" new file mode 100644 index 00000000..047612a6 --- /dev/null +++ "b/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Stack.h" @@ -0,0 +1,42 @@ +#pragma once + +#include +#include "Status.h" + +/*这些和函数同名首字母小写的宏用于 +在简化函数调用过程的同时 +不改变函数本身的返回值与形参类型*/ +/*与下面以globalVariable开头的变量配合使用*/ +#define isStackEmpty(S) (IsStackEmpty((S),&globalVariable_isStackEmpty),globalVariable_isStackEmpty) +#define getTop(S) (GetTop(S,&globalVariable_getTop),globalVariable_getTop) +#define pop(S) (Pop(S,&globalVariable_pop),globalVariable_pop) +#define sizeOfStack(S) (SizeOfStack(S,&globalVariable_sizeOfStack),globalVariable_sizeOfStack) + +typedef int Elemtype; +/*自定义bool类型*/ +typedef enum _bool { false, true }bool; + +typedef struct _Node { + Elemtype data; + struct _Node* next; +} Node, *Stack; + +/*这些以globalVariable开头的变量 +与上面 +和函数同名首字母小写的宏 +配合使用*/ +bool globalVariable_isStackEmpty; +Elemtype globalVariable_getTop; +Elemtype globalVariable_pop; +int globalVariable_sizeOfStack; + +Status InitStack(Stack *); +Status Push(Stack, Elemtype); +Status Pop(Stack, Elemtype *); +/*Traverse : 从栈顶向栈底遍历*/ +Status Traverse(const Stack); +Status DeleteStack(Stack *); +Status EmptyStack(Stack); +Status IsStackEmpty(Stack, bool *); +Status SizeOfStack(const Stack, int *); +Status GetTop(const Stack, Elemtype *); \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Status.h" "b/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Status.h" new file mode 100644 index 00000000..e35e39c9 --- /dev/null +++ "b/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/Status.h" @@ -0,0 +1,26 @@ +#pragma once + +#include +typedef enum _Status { OK, ERROR, OVERFLOW } Status; +#define CHECK(func) \ +do {\ + _Statu = func;\ + switch (_Statu) {\ + case OK:\ + break;\ + case ERROR:\ + printf("ERROR\n");\ + return 0;\ + break;\ + case OVERFLOW:\ + printf("OVERFLOW\n");\ + return 0;\ + break;\ + default:\ + printf("UNDEFINED\n");\ + return 0;\ + break;\ + }\ +} while (0); + +int _Statu; \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/test.c" "b/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/test.c" new file mode 100644 index 00000000..09f2316c --- /dev/null +++ "b/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/test.c" @@ -0,0 +1,226 @@ +锘#include +#include +#include +#include "Status.h" +#include "Stack.h" + +#define INPUT 0 +#define DEBUG 0 +#define debug_print( ... ) if(DEBUG)printf(__VA_ARGS__) + +/*浠ヤ笅鍑犱釜鍑芥暟鐢ㄤ簬鍒ゆ柇涓涓瓧绗︽槸鎿嶄綔鏁拌繕鏄搷浣滅*/ +bool isOperand(char c) { + return c >= '0' && c <= '9'; +} +bool isLv1Operator(char c) { + return c == '+' || c == '-'; +} +bool isLv2Operator(char c) { + return c == 'x' || c == '*' || c == '/'; +} +bool isLv3Operator(char c) { + return c == '(' || c == ')'; +} +/*鑾峰彇鎿嶄綔绗︾殑浼樺厛绾*/ +int getLevel(char c) { + if (isOperand(c)) return 0; + if (isLv1Operator(c)) return 1; + if (isLv2Operator(c)) return 2; + if (isLv3Operator(c)) return 3; + return -1; +} +bool isOperator(char c) { + return getLevel(c) >= 1; +} +/*鍒ゆ柇鎿嶄綔绗pe1鐨勪紭鍏堢骇鏄惁楂樹簬ope2*/ +bool isPriorityHigher(const char ope1, const char ope2) { + return getLevel(ope1) > getLevel(ope2); +} + +/*灏嗕腑缂琛ㄨ揪寮忚浆鎹负鍚庣紑琛ㄨ揪寮*/ +/*娉 : 娌℃湁鍋氬弬鏁板悎娉曟ф鏌*/ +/*source : 寰呰浆鎹㈢殑涓紑琛ㄨ揪寮*/ +/*pTarget : 鐢ㄦ潵瀛樻斁鎸囧悜鍚庣紑琛ㄨ揪寮忔寚閽堢殑鍦板潃*/ +void transformToSuffix(char const * const source, char ** pTarget) { + debug_print("====涓紑杞悗缂====\n"); + char * cleanSource;/*鐢ㄤ簬璁板綍鍘婚櫎闈炴硶瀛楃鍚庣殑source*/ + const char * p;/*鐢ㄤ簬閬嶅巻source*/ + Stack S; + char num[256];/*鐢ㄤ簬瀛樻斁鑾峰彇鍒扮殑鏁板瓧*/ + int size=0;/*鐢ㄤ簬璁板綍缁撴灉瀛楃涓茬殑澶у皬*/ + + InitStack(&S); + + debug_print("\nStep 1 : 娓呴櫎涓紑寮忎腑鐨勯潪娉曞瓧绗n"); + /*娓呴櫎source涓殑闈炴硶瀛楃*/ + for (cleanSource = source; *cleanSource != '\0'; ++cleanSource) { + if (getLevel(*cleanSource) != -1) { + Push(S,(*cleanSource)); + } + } + debug_print("寰呭鐞嗕腑缂寮 : \n%s \n",source); + size = sizeOfStack(S);/*姝ゅ鍊熺敤size瀛樻斁鏍圫鐨勫ぇ灏*/ + cleanSource = (char*)malloc((size + 1) * sizeof(char)); + cleanSource[size] = '\0'; + while (!isStackEmpty(S)) { + /*鐒跺悗鍐嶅熺敤size鍋氬惊鐜彉閲*/ + cleanSource[size - 1] = (char)pop(S); + --size; + } + debug_print("鍘婚櫎闈炴硶瀛楃鍚 : \n%s \n", cleanSource); + + /*鍏跺疄size鐨勫ぇ灏忎竴寮濮嬪氨鍙互姹傚緱*/ + /*鍗抽亶鍘唖ource锛屽鏋滀笉鏄嫭鍙峰垯++size*/ + /*濡傛灉鏄搷浣滅锛屽垯鍐++size,鐢ㄤ簬缁欏尯鍒嗗浣嶆暟鐨勭┖鏍肩暀鍑虹┖闂*/ + for (p = cleanSource; *p != '\0'; ++p) { + /*濡傛灉*p涓嶆槸鎷彿*/ + if (getLevel(*p) != 3) { + ++size; + } + /*濡傛灉*p鏄姞鍑忎箻闄*/ + if (getLevel(*p) == 1 || getLevel(*p) == 2) { + ++size; + } + } + /*姹傚嚭size鍚庡嵆鍙*pTarget杩涜malloc*/ + /*鏈熬鐣欏嚭绌洪棿鏀'\0'*/ + *pTarget = (char*)malloc((size+1)*sizeof(char)); + (*pTarget)[0] = '\0'; + + debug_print("\nStep2 : 閬嶅巻涓紑寮廫n"); + /*閬嶅巻source*/ + for (p = cleanSource; *p != '\0'; ++p) { + /*濡傛灉鏄暟瀛楀垯鐩存帴浼犻佸埌*pTarget*/ + if (!isOperator(*p)) { + _itoa_s(atoi(p),num,256,10); + debug_print("鎿嶄綔鏁 %d : 浼犻佽嚦杈撳嚭鍖 \n",atoi(num)); + p += strlen(num); + strcat_s(*pTarget, size+2,num); + strcpy_s(num, 256, ""); + strcat_s(*pTarget,size+2," "); + p -= 1; + } + else { + /*濡傛灉褰撳墠鎿嶄綔绗︽槸鍙虫嫭鍙*/ + if (*p == ')') { + debug_print("鎿嶄綔绗 ) : 寮濮嬭繛缁嚭鏍 \n"); + /*渚濇灏嗘搷浣滅浼犲埌*pTarget鐩村埌閬囧埌宸︽嫭鍙*/ + while (getTop(S) != '(') { + debug_print("鎿嶄綔绗 %c : 浼犻佽嚦杈撳嚭鍖 \n", getTop(S)); + /*姝ゅ鍊熺敤num鏉ュ畬鎴愬皢鎿嶄綔绗︿紶鍏*pTarget鐨勬搷浣 */ + num[0] = pop(S); + num[1] = '\0'; + strcat_s(*pTarget, size + 1,num); + } + /*鍒犳帀宸︽嫭鍙*/ + debug_print("閬亣鎿嶄綔绗 ( 锛岃繛缁嚭鏍堝仠姝 \n鎿嶄綔绗 ( : 琚垹闄 \n"); + pop(S); + } + /*濡傛灉鏍堢┖鎴栬呭綋鍓嶆搷浣滅浼樺厛绾ф瘮鏍堥《鎿嶄綔绗︽洿楂*/ + else if (isStackEmpty(S) || isPriorityHigher(*p,getTop(S))) { + debug_print("鎿嶄綔绗 %c : 鍏ユ爤 \n", *p); + Push(S, *p); + } + /*濡傛灉褰撳墠鎿嶄綔绗︿紭鍏堢骇鏇翠綆*/ + /*鍒欒繛缁嚭鏍堢洿鍒版爤绌烘垨鑷繁浼樺厛绾у苟涓嶆洿浣*/ + /*鐒跺悗灏嗚嚜宸卞叆鏍*/ + else { + /*渚濇灏嗘搷浣滅浼犲埌*pTarget*/ + /*鐩村埌鏍堢┖鎴栬嚜宸变紭鍏堢骇 ?涓嶆洿浣*/ + debug_print("鎿嶄綔绗 %c : 寮濮嬭繛缁嚭鏍 \n", *p); + while (!isStackEmpty(S) && !isPriorityHigher(*p,getTop(S)) && getTop(S)!='(') { + /*姝ゅ鍊熺敤num鏉ュ畬鎴愬皢鎿嶄綔绗︿紶鍏*pTarget鐨勬搷浣 */ + debug_print("鎿嶄綔绗 %c : 浼犻佽嚦杈撳嚭鍖 \n", getTop(S)); + num[0] = pop(S); + num[1] = '\0'; + strcat_s(*pTarget, size + 1, num); + } + if (isStackEmpty(S)) { + debug_print("鏍堢┖锛岃繛缁嚭鏍堝仠姝"); + } + else if ( getTop(S) == '(') { + debug_print("閬亣鎿嶄綔绗 ( 锛岃繛缁嚭鏍堝仠姝 \n"); + } + else if (isPriorityHigher(*p,getTop(S))) { + debug_print("鏍堥《鎿嶄綔绗 %c 浼樺厛绾 < %c锛岃繛缁嚭鏍堝仠姝 \n",getTop(S),*p); + } + /*鐒跺悗灏嗚嚜宸卞叆鏍*/ + debug_print("鎿嶄綔绗 %c : 鍏ユ爤 \n", *p); + Push(S, *p); + }/*end if*/ + }/*end if*/ + }/*end for*/ + debug_print("涓紑寮忛亶鍘嗗畬姣 \n"); + + /*濡傛灉鏍堥潪绌猴紝鍒欏皢鍓╀綑鎿嶄綔绗︽尐涓紶鍏*pTarget*/ + while (!isStackEmpty(S)) { + debug_print("鏍堥潪绌 : 灏嗗墿浣欏瓧绗︿紶閫佽嚦杈撳嚭鍖 \n", *p); + /*姝ゅ鍊熺敤num鏉ュ畬鎴愬皢鎿嶄綔绗︿紶鍏*pTarget鐨勬搷浣 */ + debug_print("鎿嶄綔绗 %c : 浼犻佽嚦杈撳嚭鍖 \n", getTop(S)); + num[0] = pop(S); + num[1] = '\0'; + strcat_s(*pTarget, size+2, num); + } + debug_print("杞崲濂界殑鍚庣紑寮忎负 : \n%s\n\n",*pTarget); +} +/*璁$畻鍚庣紑琛ㄨ揪寮忕殑鍊*/ +/*exp : 鍚庣紑琛ㄨ揪寮*/ +/*pResult : 鎸囧悜鐢ㄤ簬淇濆瓨缁撴灉鐨勫彉閲忕殑鎸囬拡*/ +void calculateSuffix(const char const* exp, int* pResult) { + debug_print("\n====璁$畻鍚庣紑寮====\n"); + debug_print("寰呰绠楃殑鍚庣紑寮 : \n%s\n",exp); + const char * p;/*鐢ㄤ簬閬嶅巻source*/ + Stack S; + int num;/*鐢ㄤ簬瀛樻斁鑾峰彇鍒扮殑鏁板瓧*/ + int a, b;/*鐢ㄤ簬鏆傚瓨璁$畻鏃秔op鍑烘潵鐨勬暟瀛*/ + + *pResult = 0; + InitStack(&S); + /*閬嶅巻exp*/ + for (p = exp; *p != '\0'; ++p) { + /*濡傛灉鏄搷浣滄暟锛屽叆鏍*/ + if (isOperand(*p)) { + num = atoi(p); + debug_print("鎿嶄綔鏁 %d : 鍏ユ爤 \n",num); + Push(S, num); + while (num > 0) { + ++p; + num /= 10; + } + } + else { + debug_print("鎿嶄綔绗 %c : 涓ゆ鍑烘爤 \n", *p); + b = pop(S); + a = pop(S); + debug_print("鍑烘爤缁撴灉涓 %d , %d \n",a,b); + switch (*p) { + case '+':*pResult = a + b; break; + case '-':*pResult = a - b; break; + case 'x': + case '*':*pResult = a * b; break; + case '/':*pResult = a / b; break; + default: + printf("Error : What THe F**K is THAT ???"); + return; + break; + }/*end switch*/ + debug_print("璁$畻缁撴灉涓 %d %c %d = %d , 鍏ユ爤 \n",a,*p,b,*pResult); + Push(S, *pResult); + }/*end if else*/ + }/*end for*/ + *pResult = pop(S); + debug_print("鏈鍚庣粨鏋 : %d \n\n\n",*pResult); +} +int main() { + char source[256] = "1+(15-6)*9*((12*100+34)*10000+79+40*((16/4)*35))"; + char* target; + int result; + + if (INPUT) { + gets_s(source, 256); + } + transformToSuffix(source, &target); + calculateSuffix(target, &result); + printf("灏嗕腑缂琛ㄨ揪寮廫n%s\n澶勭悊涓哄悗缂琛ㄨ揪寮 : \n%s\n", source, target); + printf("瀵瑰叾杩涜璁$畻锛岀粨鏋滀负 : \n%d \n", result); +} \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\350\277\220\350\241\214\347\273\223\346\236\234(DEBUG=0).png" "b/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\350\277\220\350\241\214\347\273\223\346\236\234(DEBUG=0).png" new file mode 100644 index 00000000..b85eb81a Binary files /dev/null and "b/2017-1/zkz/Stack/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\350\277\220\350\241\214\347\273\223\346\236\234(DEBUG=0).png" differ diff --git "a/2017-1/zkz/Stack/\350\247\243\350\277\267\345\256\253/Stack.c" "b/2017-1/zkz/Stack/\350\247\243\350\277\267\345\256\253/Stack.c" new file mode 100644 index 00000000..53989d6d --- /dev/null +++ "b/2017-1/zkz/Stack/\350\247\243\350\277\267\345\256\253/Stack.c" @@ -0,0 +1,98 @@ +#include "Stack.h" + +Status InitStack(Stack * pS) { + if (!pS) + return ERROR; + /*创建top节点*/ + (*pS) = (Stack)malloc(sizeof(Node)); + if (!*pS) + return OVERFLOW; + /*为top节点赋值*/ + (*pS)->next = NULL; + return OK; +} +Status Push(Stack S, Elemtype data) { + Node * p; + if (!S) + return ERROR; + /*Step 1 创建新节点*/ + p = (Node *)malloc(sizeof(Node)); + if (!p) + return OVERFLOW; + /*Step 2 为新节点赋值*/ + p->data = data; + p->next = NULL; + /*Step 3 将新节点入栈*/ + p->next = S->next; + S->next = p; + return OK; +} +Status Pop(Stack S, Elemtype *pdata) { + Node * p; + if (!S || !pdata || !S->next) + return ERROR; + /*Step 1 将顶节点出栈*/ + p = S->next; + S->next = p->next; + /*Step 2 获取节点数据*/ + *pdata = p->data; + /*Step 3 销毁节点*/ + free(p); + return OK; +} +/*Traverse : 从栈顶向栈底遍历*/ +Status Traverse(const Stack S) { + if (!S) + return ERROR; + if (!S->next) + printf("(empty)"); + Node *pS = S->next; + while (pS) { + printf("%d %d ", pS->data.x , pS->data.y , pS->data); + pS = pS->next; + } + return OK; +} +Status DeleteStack(Stack *pS) { + Node *p, *next; + if (!pS) + return ERROR; + p = (*pS)->next; + while (p) { + next = p->next; + free(p); + p = next; + } + free(*pS); + *pS = NULL; + return OK; +} +Status EmptyStack(Stack S) { + if (!S) + return ERROR; + DeleteStack(&S->next); + return OK; +} +Status IsStackEmpty(Stack S, int *pResult) { + if (!S) + return ERROR; + *pResult = !S->next; + return OK; +} +Status SizeOfStack(const Stack S, int *pN) { + if (!S || !pN) + return ERROR; + Node *pS = S->next; + *pN = 0; + while (pS) { + ++*pN; + pS = pS->next; + }; + return OK; +} +Status GetTop(const Stack S, Elemtype *pdata) { + if (!S || !pdata || !S->next) + return ERROR; + *pdata = S->next->data; + return OK; +} \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\350\247\243\350\277\267\345\256\253/Stack.h" "b/2017-1/zkz/Stack/\350\247\243\350\277\267\345\256\253/Stack.h" new file mode 100644 index 00000000..27feee95 --- /dev/null +++ "b/2017-1/zkz/Stack/\350\247\243\350\277\267\345\256\253/Stack.h" @@ -0,0 +1,25 @@ +#pragma once + +#include +#include "Status.h" +typedef struct _Point { + int x; + int y; +}Point; +typedef Point Elemtype; + +typedef struct _Node { + Elemtype data; + struct _Node* next; +} Node, *Stack; + +Status InitStack(Stack *); +Status Push(Stack, Elemtype); +Status Pop(Stack, Elemtype *); +/*Traverse : 从栈顶向栈底遍历*/ +Status Traverse(const Stack); +Status DeleteStack(Stack *); +Status EmptyStack(Stack); +Status IsStackEmpty(Stack, int *); +Status SizeOfStack(const Stack, int *); +Status GetTop(const Stack, Elemtype *); \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\350\247\243\350\277\267\345\256\253/Status.h" "b/2017-1/zkz/Stack/\350\247\243\350\277\267\345\256\253/Status.h" new file mode 100644 index 00000000..e35e39c9 --- /dev/null +++ "b/2017-1/zkz/Stack/\350\247\243\350\277\267\345\256\253/Status.h" @@ -0,0 +1,26 @@ +#pragma once + +#include +typedef enum _Status { OK, ERROR, OVERFLOW } Status; +#define CHECK(func) \ +do {\ + _Statu = func;\ + switch (_Statu) {\ + case OK:\ + break;\ + case ERROR:\ + printf("ERROR\n");\ + return 0;\ + break;\ + case OVERFLOW:\ + printf("OVERFLOW\n");\ + return 0;\ + break;\ + default:\ + printf("UNDEFINED\n");\ + return 0;\ + break;\ + }\ +} while (0); + +int _Statu; \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\350\247\243\350\277\267\345\256\253/test.c" "b/2017-1/zkz/Stack/\350\247\243\350\277\267\345\256\253/test.c" new file mode 100644 index 00000000..08cc4023 --- /dev/null +++ "b/2017-1/zkz/Stack/\350\247\243\350\277\267\345\256\253/test.c" @@ -0,0 +1,228 @@ +#include +#include "Stack.h" + +/*快速进行循环*/ +#define times(i,size) for((i)=1;(i)<=(size);++(i)) +/*指定地图长宽*/ +#define SIZE 10 +/*用宏实现以1为下标从迷宫中取点*/ +#define Maze(x,y) Maze[(x)-1][(y)-1] +/*判断两个点是否相等*/ +#define equal(pa,pb) ((pa).x == (pb).x && (pa).y == (pb).y) + +/*由于DEBUG && printf(...)大量使用,将其简化为DP(...)*/ +/*若希望当DEBUG为0时输出内容,使用!DP(...)即可*/ +#define DP DEBUG && printf +#define DEBUG 0 + + + +/*地形*/ +/*将寻路方向也定义为地形,可以简化后面的某些操作*/ +typedef enum _Terrain {FLOOR,RIGHT,DOWN,LEFT,UP,BAY,END,WALL} Terrain; +/*打印迷宫时,打印其对应的符号*/ +char Marks[] = " >V<^$@#"; +char Marks_2[] = " →↓←↑"; +/*迷宫,地形的二维数组*/ +Terrain Maze[SIZE][SIZE] ; + +/*全局变量起点与终点*/ +Point start ; +Point end ; + +/*设置某个点的地形*/ +void setTerrain(Point p, Terrain t){ + Maze(p.x, p.y) = t; +} +void setTerrainXY(int x, int y, Terrain t) { + Maze(x, y) = t; +} + +void InitMaze() { + int i,j; + times(j, SIZE) + times(i, SIZE) + setTerrainXY(i, j, FLOOR); + /*Set walls*/ + times(i, SIZE) { + Maze(1, i) = Maze(SIZE, i) = Maze(i, 1) = Maze(i, SIZE) = WALL; + } + Point Walls[20] = { + 4,2, 8,2, 4,3, 8,3, 6,4, + 7,4, 3,5, 4,5, 5,5, 9,5, + 5,6, 9,6, 3,7, 7,7, 2,8, + 3,8, 4,8, 5,8, 7,8, 8,8 + }; + times(i, 20) { + setTerrainXY(Walls[i - 1].x, Walls[i - 1].y, WALL); + } + /*Set start*/ + setTerrain(start, RIGHT); + /*Set end*/ + setTerrain(end, END); +} +int ShowTerrain(Terrain t) { + /*set its return-value-type as int + so it's suitable for DEBUG && XX expression*/ + if (t >= 5) + printf("%c ", Marks[t]); + else + printf("%c%c", Marks_2[2 * t], Marks_2[2 * t + 1]); +} +int ShowMaze() { + /*set its return-value-type as int + so it's suitable for DEBUG && XX expression*/ + int x, y; + printf(" "); + times(x, SIZE) + printf("%x ",x); + printf("\n"); + times(y, SIZE) { + printf("%x ",y); + times(x, SIZE) + ShowTerrain(Maze(x,y)); + printf("\n"); + } + + return 0; +} +int isMovable(Point p) { + if (Maze(p.x, p.y) == FLOOR || Maze(p.x, p.y) == END) + return 1; + return 0; +} +/*以下5个函数用于获取某个点的右/下/左/上/前方的点*/ +Point TheRight(Point p) { + p.x += 1; + return p; +} +Point TheDown(Point p) { + p.y += 1; + return p; +} +Point TheLeft(Point p) { + p.x -= 1; + return p; +} +Point TheUp(Point p) { + p.y -= 1; + return p; +} +Point TheNext(Point p) { + Terrain t = Maze(p.x, p.y); + if (t == RIGHT) + return TheRight(p); + if (t == DOWN) + return TheDown(p); + if (t == LEFT) + return TheLeft(p); + if (t == UP) + return TheUp(p); + return p; +} + +/*算法 迷宫求解*/ +void SolveMaze() { + Stack S; + Point current,data,top; + int isEmpty; + //current = TheRight(start); + //if(Maze(current.x,current.y)==FLOOR) + // setTerrain(current, RIGHT); + InitStack(&S); + Push(S, start); + do { + DP("\n"); + DEBUG && ShowMaze(); + IsStackEmpty(S, &isEmpty); + if (isEmpty) { + DP("Stack is empty \n"); + break; + } + GetTop(S, &top); + DP("Top of stack : %d , %d \n",top.x,top.y); + current = TheNext(top); + DP("Set current as top "); + if(! equal(current,top)) + DP("'s next : %d , %d ", current.x, current.y); + DP("\n"); + /*If current is end*/ + if (equal(current, end)) { + DP("Destination found \n"); + break; + } + /*If current is not end*/ + DP("Current's terrain is \'"); + DEBUG && ShowTerrain(Maze(current.x, current.y)); + DP("\' \n"); + /*If current is movable*/ + if (isMovable(current)) { + DP("Current is movable \n"); + setTerrain(current,RIGHT); + DP("Push current \n"); + Push(S, current); + } + /*Else if current is not movable*/ + else { + DP("Current is not movable \n"); + /*Top has new direction to search*/ + DP("Top's terrain is \'"); + DEBUG && ShowTerrain(Maze(top.x, top.y)); + DP("\' \n"); + if (Maze(top.x, top.y) <= UP) { + Maze(top.x, top.y) += 1; + DP("Top's terrain is changed to \'"); + DEBUG && ShowTerrain(Maze(top.x, top.y)); + DP("\' \n"); + } + /*Top has no new direction to search*/ + else { + Pop(S, &data); + DP("Pop Stack \n"); + IsStackEmpty(S, &isEmpty); + if (!isEmpty) { + GetTop(S, &top); + DP("New top is %d , %d \n", top.x, top.y); + } + if (Maze(top.x, top.y) <= UP) { + Maze(top.x, top.y) += 1; + DP("Top's terrain is changed to \'"); + DEBUG && ShowTerrain(Maze(top.x, top.y)); + DP("\' \n"); + } + } + } + IsStackEmpty(S, &isEmpty); + if (isEmpty) + DP("Stack is empty \n"); + } while (!isEmpty); + DP("End searching \n"); +} + +int main() { + start.x = 2; + start.y = 2; + end.x = 9; + end.y = 9; + InitMaze(); + printf("Test 1 : start at %d , %d , end at %d , %d \n\n",start.x,start.y,end.x,end.y); + !DP("Before:\n"); + DEBUG || ShowMaze(); + SolveMaze(); + !DP("\nAfter:\n"); + DEBUG || ShowMaze(); + + start.x = 9; + start.y = 9; + end.x = 2; + end.y = 2; + InitMaze(); + setTerrainXY(5, 7, WALL); + printf("\nTest 2 : start at %d , %d , end at %d , %d \n", start.x, start.y, end.x, end.y); + printf("And this time , block 5 , 7 is set as WALL\n\n"); + !DP("Before:\n"); + DEBUG || ShowMaze(); + SolveMaze(); + !DP("\nAfter:\n"); + DEBUG || ShowMaze(); +} diff --git "a/2017-1/zkz/Stack/\351\235\236\351\200\222\345\275\222\346\261\211\350\257\272\345\241\224/Stack.c" "b/2017-1/zkz/Stack/\351\235\236\351\200\222\345\275\222\346\261\211\350\257\272\345\241\224/Stack.c" new file mode 100644 index 00000000..82454203 --- /dev/null +++ "b/2017-1/zkz/Stack/\351\235\236\351\200\222\345\275\222\346\261\211\350\257\272\345\241\224/Stack.c" @@ -0,0 +1,105 @@ +#include "Stack.h" + +Status InitStack(Stack * pS) { + if (!pS) + return ERROR; + /*创建top节点*/ + (*pS) = (Stack)malloc(sizeof(Node)); + if (!*pS) + return OVERFLOW; + /*为top节点赋值*/ + (*pS)->next = NULL; + return OK; +} +Status Push(Stack S, Elemtype data) { + Node * p; + if (!S) + return ERROR; + /*Step 1 创建新节点*/ + p = (Node *)malloc(sizeof(Node)); + if (!p) + return OVERFLOW; + /*Step 2 为新节点赋值*/ + p->data = data; + p->next = NULL; + /*Step 3 将新节点入栈*/ + p->next = S->next; + S->next = p; + return OK; +} +Status Pop(Stack S, Elemtype *pdata) { + Node * p; + if (!S || !pdata || !S->next) + return ERROR; + + /*Step 1 将顶节点出栈*/ + p = S->next; + S->next = p->next; + /*Step 2 获取节点数据*/ + *pdata = p->data; + /*Step 3 销毁节点*/ + free(p); + return OK; +} +/*Traverse : 从栈底向栈顶遍历*/ +Status Traverse(const Stack S) { + if (!S) + return ERROR; + if (!S->next) { + return OK; + } + /*如果除头节点外只剩一个节点*/ + if (!S->next->next) { + printf("%d ",S->next->data); + return OK; + } + else { + Traverse(S->next); + printf("%d ", S->next->data); + } + /*如果除头节点外有多个节点*/ + return OK; +} +Status DeleteStack(Stack *pS) { + Node *p, *next; + if (!pS) + return ERROR; + p = (*pS)->next; + while (p) { + next = p->next; + free(p); + p = next; + } + free(*pS); + *pS = NULL; + return OK; +} +Status EmptyStack(Stack S) { + if (!S) + return ERROR; + DeleteStack(&S->next); + return OK; +} +Status IsStackEmpty(Stack S, bool *pResult) { + if (!S) + return ERROR; + *pResult = !S->next; + return OK; +} +Status SizeOfStack(const Stack S, int *pN) { + if (!S || !pN) + return ERROR; + Node *pS = S->next; + *pN = 0; + while (pS) { + ++*pN; + pS = pS->next; + }; + return OK; +} +Status GetTop(const Stack S, Elemtype *pdata) { + if (!S || !pdata || !S->next) + return ERROR; + *pdata = S->next->data; + return OK; +} \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\351\235\236\351\200\222\345\275\222\346\261\211\350\257\272\345\241\224/Stack.h" "b/2017-1/zkz/Stack/\351\235\236\351\200\222\345\275\222\346\261\211\350\257\272\345\241\224/Stack.h" new file mode 100644 index 00000000..047612a6 --- /dev/null +++ "b/2017-1/zkz/Stack/\351\235\236\351\200\222\345\275\222\346\261\211\350\257\272\345\241\224/Stack.h" @@ -0,0 +1,42 @@ +#pragma once + +#include +#include "Status.h" + +/*这些和函数同名首字母小写的宏用于 +在简化函数调用过程的同时 +不改变函数本身的返回值与形参类型*/ +/*与下面以globalVariable开头的变量配合使用*/ +#define isStackEmpty(S) (IsStackEmpty((S),&globalVariable_isStackEmpty),globalVariable_isStackEmpty) +#define getTop(S) (GetTop(S,&globalVariable_getTop),globalVariable_getTop) +#define pop(S) (Pop(S,&globalVariable_pop),globalVariable_pop) +#define sizeOfStack(S) (SizeOfStack(S,&globalVariable_sizeOfStack),globalVariable_sizeOfStack) + +typedef int Elemtype; +/*自定义bool类型*/ +typedef enum _bool { false, true }bool; + +typedef struct _Node { + Elemtype data; + struct _Node* next; +} Node, *Stack; + +/*这些以globalVariable开头的变量 +与上面 +和函数同名首字母小写的宏 +配合使用*/ +bool globalVariable_isStackEmpty; +Elemtype globalVariable_getTop; +Elemtype globalVariable_pop; +int globalVariable_sizeOfStack; + +Status InitStack(Stack *); +Status Push(Stack, Elemtype); +Status Pop(Stack, Elemtype *); +/*Traverse : 从栈顶向栈底遍历*/ +Status Traverse(const Stack); +Status DeleteStack(Stack *); +Status EmptyStack(Stack); +Status IsStackEmpty(Stack, bool *); +Status SizeOfStack(const Stack, int *); +Status GetTop(const Stack, Elemtype *); \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\351\235\236\351\200\222\345\275\222\346\261\211\350\257\272\345\241\224/Status.h" "b/2017-1/zkz/Stack/\351\235\236\351\200\222\345\275\222\346\261\211\350\257\272\345\241\224/Status.h" new file mode 100644 index 00000000..e35e39c9 --- /dev/null +++ "b/2017-1/zkz/Stack/\351\235\236\351\200\222\345\275\222\346\261\211\350\257\272\345\241\224/Status.h" @@ -0,0 +1,26 @@ +#pragma once + +#include +typedef enum _Status { OK, ERROR, OVERFLOW } Status; +#define CHECK(func) \ +do {\ + _Statu = func;\ + switch (_Statu) {\ + case OK:\ + break;\ + case ERROR:\ + printf("ERROR\n");\ + return 0;\ + break;\ + case OVERFLOW:\ + printf("OVERFLOW\n");\ + return 0;\ + break;\ + default:\ + printf("UNDEFINED\n");\ + return 0;\ + break;\ + }\ +} while (0); + +int _Statu; \ No newline at end of file diff --git "a/2017-1/zkz/Stack/\351\235\236\351\200\222\345\275\222\346\261\211\350\257\272\345\241\224/test.c" "b/2017-1/zkz/Stack/\351\235\236\351\200\222\345\275\222\346\261\211\350\257\272\345\241\224/test.c" new file mode 100644 index 00000000..607f66cd --- /dev/null +++ "b/2017-1/zkz/Stack/\351\235\236\351\200\222\345\275\222\346\261\211\350\257\272\345\241\224/test.c" @@ -0,0 +1,78 @@ + +#include "Stack.h" + +/*打印3个柱子的情况*/ +/*加上小括号是为了假装自己是个函数*/ +#define showColumns() \ +printf("Column1 : ");\ +Traverse(column[0]);\ +printf("\nColumn2 : ");\ +Traverse(column[1]);\ +printf("\nColumn3 : ");\ +Traverse(column[2]);\ +printf("\n\n") + +/*任务结构体*/ +/*每个任务结构体包含四个部分 : */ +/*转移圆盘的数量+从哪里来+到哪里去+中转柱子*/ +typedef struct _task{ + char mount; + char from; + char to; + char temp; +}task; + +/*在不改变Elemtype的情况下*/ +/*使task可以出入栈*/ +int encodeTask(task t) { + return *(int *)&t; +} +task decodeTask(int n) { + return *(task*)&n; +} + +int main() { + Stack column[3],S;/*3个柱子+模拟递归的栈*/ + int n;/*汉诺塔层数*/ + int i;/*循环变量*/ + + InitStack(&column[0]); + InitStack(&column[1]); + InitStack(&column[2]); + InitStack(&S); + + n = 5; + for (i = n; i >= 1; --i) { + Push(column[0], i); + } + + showColumns(); + + /*初始化*/ + /*即任务栈S里一开始有一个“将n个圆盘从柱1转移到柱3”的任务*/ + task t = {n,1,3,2}; + Push(S, encodeTask(t)); + + /*循环完成任务,直到任务栈空*/ + while (!isStackEmpty(S)) { + /*获取当前任务*/ + t = decodeTask(pop(S)); + /*若当前任务的mount为1,则执行该动作*/ + if (t.mount == 1) { + printf("%d -> %d \n", t.from, t.to); + Push(column[t.to - 1], pop(column[t.from - 1])); + showColumns(); + } + /*否则将当前任务拆解为3个小任务*/ + else { + task temp3 = { t.mount - 1,t.temp,t.to,t.from }; + task temp2 = { 1,t.from,t.to,t.temp }; + task temp1 = { t.mount - 1,t.from,t.temp,t.to }; + Push(S, encodeTask(temp3)); + Push(S, encodeTask(temp2)); + Push(S, encodeTask(temp1)); + } + } + + return 0; +} \ No newline at end of file diff --git a/2017-1/zkz/Tree/solveException.c b/2017-1/zkz/Tree/solveException.c new file mode 100644 index 00000000..4c15d5f3 --- /dev/null +++ b/2017-1/zkz/Tree/solveException.c @@ -0,0 +1,58 @@ +#include "solveException.h" +int seh_filer(int code) +{ + switch (code) + { + case EXCEPTION_ACCESS_VIOLATION: + printf("存储保护异常,错误代码:%x\n", code); + break; + case EXCEPTION_DATATYPE_MISALIGNMENT: + printf("数据类型未对齐异常,错误代码:%x\n", code); + break; + case EXCEPTION_BREAKPOINT: + printf("中断异常,错误代码:%x\n", code); + break; + case EXCEPTION_SINGLE_STEP: + printf("单步中断异常,错误代码:%x\n", code); + break; + case EXCEPTION_ARRAY_BOUNDS_EXCEEDED: + printf("数组越界异常,错误代码:%x\n", code); + break; + case EXCEPTION_FLT_DENORMAL_OPERAND: + case EXCEPTION_FLT_DIVIDE_BY_ZERO: + case EXCEPTION_FLT_INEXACT_RESULT: + case EXCEPTION_FLT_INVALID_OPERATION: + case EXCEPTION_FLT_OVERFLOW: + case EXCEPTION_FLT_STACK_CHECK: + case EXCEPTION_FLT_UNDERFLOW: + printf("浮点数计算异常,错误代码:%x\n", code); + break; + case EXCEPTION_INT_DIVIDE_BY_ZERO: + printf("被0除异常,错误代码:%x\n", code); + break; + case EXCEPTION_INT_OVERFLOW: + printf("数据溢出异常,错误代码:%x\n", code); + break; + case EXCEPTION_IN_PAGE_ERROR: + printf("页错误异常,错误代码:%x\n", code); + break; + case EXCEPTION_ILLEGAL_INSTRUCTION: + printf("非法指令异常,错误代码:%x\n", code); + break; + case EXCEPTION_STACK_OVERFLOW: + printf("堆栈溢出异常,错误代码:%x\n", code); + break; + case EXCEPTION_INVALID_HANDLE: + printf("无效句病异常,错误代码:%x\n", code); + break; + default: + if (code & (1 << 29)) + printf("用户自定义的软件异常,错误代码:%x\n", code); + else + printf("其它异常,错误代码:%x\n", code); + break; + } + + exit(-1); + return 1; +} \ No newline at end of file diff --git a/2017-1/zkz/Tree/solveException.h b/2017-1/zkz/Tree/solveException.h new file mode 100644 index 00000000..f1f67e55 --- /dev/null +++ b/2017-1/zkz/Tree/solveException.h @@ -0,0 +1,6 @@ +#pragma once +#include +#include + +#define solveException (printf("\nError : "__FUNCTION__" : "),seh_filer(GetExceptionCode())) +int seh_filer(int code); \ No newline at end of file diff --git a/2017-1/zkz/Tree/test.c b/2017-1/zkz/Tree/test.c new file mode 100644 index 00000000..c734ad7e --- /dev/null +++ b/2017-1/zkz/Tree/test.c @@ -0,0 +1,17 @@ +#include "tree.h" +int main() { + /*方法一测试用例*/ + char* rootLR = "ABDG###EH##I#K##C#F##"; + /*方法二测试用例*/ + char* _pre = "ABDFCE"; + char* _in = "DFBAEC"; + + Tree t1,t2; + /*方法一*/ + t1 = newBinaryTree(rootLR); + TraverseTree(t1, all); + /*方法二*/ + t2 = newBinaryTree2(_pre, _in); + TraverseTree(t2, all); + return 0; +} \ No newline at end of file diff --git a/2017-1/zkz/Tree/tree.c b/2017-1/zkz/Tree/tree.c new file mode 100644 index 00000000..533c9329 --- /dev/null +++ b/2017-1/zkz/Tree/tree.c @@ -0,0 +1,216 @@ +#include "tree.h" + +#define EMPTY '#' + +/*===字符串相关===*/ +/*手动获取子串*/ +char* substr(char* source, int begin, int end) { + __try { + int size = end - begin + 1; + if (size <= 0) { + return ""; + } + char* result = (char*)malloc((size + 1) * sizeof(char)); + result[size] = '\0'; + --size; + for (; size >= 0; --size) { + result[size] = source[begin + size - 1]; + } + return result; + } + __except (solveException) {}; +} +/*手动分割字符串*/ +void divideString(char* source, char delim, char** pDes1, char** pDes2) { + __try { + char* p = source; + int size1, sizeS = strlen(source); + while (*(p++) != delim); + size1 = p - source - 1; + *pDes1 = substr(source, 1, size1); + *pDes2 = substr(source, size1 + 2, sizeS); + //printf("divide %s with %c : %s and %s\n",source,delim,*pDes1 == "" ? "(empty)" : *pDes1, *pDes2 == "" ? "(empty)" : *pDes2); + } + __except (solveException) {}; +} + +inline Node* newNode() { + __try { + Node* p = (Node*)malloc(sizeof(Node)); + p->parent = p->left = p->right = NULL; + p->data = 0; + return p; + } + __except (solveException) {}; +} +void InitializeTree(Tree* pT) { + __try { + *pT = newNode(); + } + __except (solveException) {}; +} +void _TraverseTree(const Tree root, Order order) { + __try { + /*如果root不为空 , 则根据order按顺序遍历root*/ + if (root) { + if (order == pre)printf("%c ", root->data); + _TraverseTree(root->left, order); + if (order == in)printf("%c ", root->data); + _TraverseTree(root->right, order); + if (order == post)printf("%c ", root->data); + } + } + __except (solveException) {}; +} +/*遍历二叉树*/ +/*root : 待遍历的二叉树*/ +/*order : 遍历顺序(pre , in , post , all 分别为前序、中序、后序及所有顺序)*/ +void TraverseTree(const Tree root, Order order) { + if (order == all) { + TraverseTree(root, pre); + TraverseTree(root, in); + TraverseTree(root, post); + return; + } + debug_print("%s序遍历树0x%p : ", order == pre ? "前" : order == in ? "中" : "后", root); + _TraverseTree(root, order); + debug_print("\n"); +} +inline int getDepth(const Tree t) { + __try { + int depth = 1; + Node* p = t; + while (p->parent) { + ++depth; + p = p->parent; + } + return depth; + } + __except (solveException) {}; +} +/*新建一个完全二叉树*/ +void NewCBTree(Tree* pRoot, int depth) { + __try { + int i; + for (i = getDepth(*pRoot) - 1; i > 0; --i) { debug_print(" "); } + debug_print("以%p为根建立深度为%d的完全二叉树\n", *pRoot, depth); + /*如果深度为1 , 说明就是个叶节点。只需left与right置空即可。*/ + if (depth == 1) { + (*pRoot)->left = (*pRoot)->right = NULL; + return; + } + /*构建左节点 , 最后以左节点为根建立深度为depth-1的树*/ + (*pRoot)->left = newNode(); + (*pRoot)->left->data = (*pRoot)->data * 2; + (*pRoot)->left->parent = *pRoot; + NewCBTree(&(*pRoot)->left, depth - 1); + /*同理构建右节点*/ + (*pRoot)->right = newNode(); + (*pRoot)->right->data = (*pRoot)->data * 2 + 1; + (*pRoot)->right->parent = *pRoot; + NewCBTree(&(*pRoot)->right, depth - 1); + + for (i = getDepth(*pRoot) - 1; i > 0; --i) { debug_print(" "); } + debug_print("树%p构建完毕 , 左子树为%p , 右子树为%p\n", (*pRoot), (*pRoot)->left, (*pRoot)->right); + } + __except (solveException) {}; +} +/*删除树中数据为c的节点*/ +void DeleteSpecialNode(Tree *pRoot, Elemtype c) { + __try { + if ((*pRoot)->left) { + if ((*pRoot)->left->data == c) { + free((*pRoot)->left); + (*pRoot)->left = NULL; + } + else { + DeleteSpecialNode(&(*pRoot)->left, c); + } + } + if ((*pRoot)->right) { + if ((*pRoot)->right->data == c) { + free((*pRoot)->right); + (*pRoot)->right = NULL; + } + else { + DeleteSpecialNode(&(*pRoot)->right, c); + } + } + } + __except (solveException) {}; +} +/*根据一个完整先序序列建立二叉树*/ +Tree newBinaryTree(char* str) { + __try { + Node* current; + Tree tree; + InitializeTree(&tree); + /*如果是空字符串则什么都不做*/ + if (strlen(str) == 0) { + return tree; + } + debug_print("以【%s】为完整前序序列", str); + debug_print("在0x%p处建立二叉树\n", tree); + tree->data = str[0]; + current = tree; + while (*++str != '\0') { + if (current->left == NULL) { + current->left = newNode(); + current->left->parent = current; + current->left->data = *str; + if (*str != EMPTY) { + current = current->left; + } + }/*end if current->left == NULL*/ + else if (current->right == NULL) { + current->right = newNode(); + current->right->parent = current; + current->right->data = *str; + if (*str != EMPTY) { + current = current->right; + } + }/*end if current->left == NULL*/ + else { + current = current->parent; + --str; + } + }/*end while*/ + /*去掉数据为空格的叶节点*/ + DeleteSpecialNode(&tree, EMPTY); + return tree; + } + __except (solveException) {}; +} +/*根据一个不完整的先序序列与一个不完整的中序序列建立二叉树*/ +Tree _newBinaryTree2(char* pre, char* in) { + __try { + Tree tree = newNode(); + int len = strlen(pre); + char * p = in; + char* leftPre, *leftIn, *rightPre, *rightIn; + if (strlen(pre) == 1 && strlen(in) == 1) { + tree->data = *pre; + return tree; + } + tree->data = pre[0]; + divideString(in, pre[0], &leftIn, &rightIn); + /*有左子树*/ + if (strlen(leftIn)) { + leftPre = substr(pre, 2, strlen(leftIn) + 1); + tree->left = _newBinaryTree2(leftPre, leftIn); + } + /*有右子树*/ + if (strlen(rightIn)) { + rightPre = substr(pre, strlen(pre) - strlen(rightIn) + 1, strlen(pre)); + tree->right = _newBinaryTree2(rightPre, rightIn); + } + + return tree; + } + __except (solveException) {}; +} +Tree newBinaryTree2(char*pre, char* in) { + Tree tree = _newBinaryTree2(pre, in); + debug_print("以【%s】与【%s】在0x%p处建立二叉树\n", pre, in, tree); + return tree; +} \ No newline at end of file diff --git a/2017-1/zkz/Tree/tree.h b/2017-1/zkz/Tree/tree.h new file mode 100644 index 00000000..33d4fac3 --- /dev/null +++ b/2017-1/zkz/Tree/tree.h @@ -0,0 +1,42 @@ +#pragma once + +#include +#include +#include +#include "solveException.h" + +#define DEBUG 1 +#define debug_print(...) if(DEBUG){printf(__VA_ARGS__);} + +typedef char Elemtype; +typedef struct _Node { + Elemtype data; + struct _Node* parent; + struct _Node* left; + struct _Node* right; +}Node, *Tree; +typedef enum _Order { pre, in, post, all } Order; + +/*===字符串相关===*/ +/*手动获取子串*/ +char* substr(char* source, int begin, int end); +/*手动分割字符串*/ +void divideString(char* source, char delim, char** pDes1, char** pDes2); + +inline Node* newNode(); +void InitializeTree(Tree* pT); +void _TraverseTree(const Tree root, Order order); +/*遍历二叉树*/ +/*root : 待遍历的二叉树*/ +/*order : 遍历顺序(pre , in , post , all 分别为前序、中序、后序及所有顺序)*/ +void TraverseTree(const Tree root, Order order); +inline int getDepth(const Tree t); +/*新建一个完全二叉树*/ +void NewCBTree(Tree* pRoot, int depth); +/*删除树中数据为c的节点*/ +void DeleteSpecialNode(Tree *pRoot, Elemtype c); +/*根据一个完整先序序列建立二叉树*/ +Tree newBinaryTree(char* str); +/*根据一个不完整的先序序列与一个不完整的中序序列建立二叉树*/ +Tree _newBinaryTree2(char* pre, char* in); +Tree newBinaryTree2(char*pre, char* in); \ No newline at end of file diff --git "a/2017-1/zkz/Tree/\346\265\213\350\257\225\347\273\223\346\236\234.png" "b/2017-1/zkz/Tree/\346\265\213\350\257\225\347\273\223\346\236\234.png" new file mode 100644 index 00000000..6bac2f67 Binary files /dev/null and "b/2017-1/zkz/Tree/\346\265\213\350\257\225\347\273\223\346\236\234.png" differ diff --git a/2017-1/zkz/Tree2/test.c b/2017-1/zkz/Tree2/test.c new file mode 100644 index 00000000..34aea1f6 --- /dev/null +++ b/2017-1/zkz/Tree2/test.c @@ -0,0 +1,48 @@ +#include "tree.h" +Tree t1, t2, t3, t4; +void testNewBinaryTree() { + /*方法一测试用例*/ + char* rootLR = "ABDG###EH##I#K##C#F##"; + /*方法二测试用例*/ + char* _pre = "ABDFCE"; + char* _in = "DFBAEC"; + /*方法一*/ + t1 = newBinaryTree(rootLR); + Show(t1, all); + /*方法二*/ + t2 = newBinaryTree2(_pre, _in); + Show(t2, all); + debug_print("\n"); +} +void testCalculateDepthAndWidth() { + /*计算树的深度与宽度*/ + int depth, width; + CalculateDepthAndWidth(t1, &depth, &width); + CalculateDepthAndWidth(t2, &depth, &width); + debug_print("\n"); +} +void testCountLeavesAndBranches() { + /*统计二叉树中叶与非叶节点的个数*/ + int leaf, branch; + CountLeaves(t1, &leaf); + CountBranches(t1, &branch); + CountLeaves(t2, &leaf); + CountBranches(t2, &branch); + debug_print("\n"); +} +void testIsCompleteBinaryTree() { + /*判断是否为完全二叉树*/ + t3 = newBinaryTree("ABD##E##C##");/*完全二叉树*/ + t4 = newBinaryTree("A#C##");/*非完全二叉树*/ + debug_print("树0x%p%s是完全二叉树 \n", t1, isCompleteBinaryTree(t1) ? "" : "不"); + debug_print("树0x%p%s是完全二叉树 \n", t2, isCompleteBinaryTree(t2) ? "" : "不"); + debug_print("树0x%p%s是完全二叉树 \n", t3, isCompleteBinaryTree(t3) ? "" : "不"); + debug_print("树0x%p%s是完全二叉树 \n", t4, isCompleteBinaryTree(t4) ? "" : "不"); +} +int main() { + testNewBinaryTree(); + testCalculateDepthAndWidth(); + testCountLeavesAndBranches(); + testIsCompleteBinaryTree(); + return 0; +} \ No newline at end of file diff --git a/2017-1/zkz/Tree2/tree.c b/2017-1/zkz/Tree2/tree.c new file mode 100644 index 00000000..d5d71ca3 --- /dev/null +++ b/2017-1/zkz/Tree2/tree.c @@ -0,0 +1,289 @@ +#include +#include "tree.h" + +#define EMPTY '#' + +/*===字符串相关===*/ +/*手动获取子串*/ +char* substr(char* source, int begin, int end) { + int size = end - begin + 1; + if (size <= 0) { + return ""; + } + char* result = (char*)malloc((size + 1) * sizeof(char)); + result[size] = '\0'; + --size; + for (; size >= 0; --size) { + result[size] = source[begin + size - 1]; + } + return result; +} +/*手动分割字符串*/ +void divideString(char* source, char delim, char** pDes1, char** pDes2) { + + char* p = source; + int size1, sizeS = strlen(source); + while (*(p++) != delim); + size1 = p - source - 1; + *pDes1 = substr(source, 1, size1); + *pDes2 = substr(source, size1 + 2, sizeS); +} + +inline Node* newNode() { + + Node* p = (Node*)malloc(sizeof(Node)); + p->parent = p->left = p->right = NULL; + p->data = 0; + return p; +} +void InitializeTree(Tree* pT) { + + *pT = newNode(); +} +void _ShowTree(const Tree root , int*null) { + printf("%c",root->data); +} +void _TraverseTree(const Tree root, Order order, void func(const Tree,int*) , int* p) { + + /*如果root不为空 , 则根据order按顺序遍历root*/ + if (root) { + if (order == pre)func(root,p); + _TraverseTree(root->left, order,func,p); + if (order == in)func(root,p); + _TraverseTree(root->right, order,func,p); + if (order == post)func(root,p); + } +} +/*遍历二叉树*/ +/*root : 待遍历的二叉树*/ +/*order : 遍历顺序(pre , in , post , all 分别为前序、中序、后序及所有顺序)*/ +void TraverseTree(const Tree root, Order order , void func(const Tree , int*) , int* p) { + _TraverseTree(root, order, func,p); +} +void Show(const Tree root, Order order) { + if (order == all) { + Show(root, pre); + Show(root, in); + Show(root, post); + return; + } + debug_print("%s序遍历树0x%p : ", order == pre ? "前" : order == in ? "中" : "后", root); + TraverseTree(root, order, _ShowTree, NULL); + debug_print("\n"); +} +inline int getDepth(const Tree t) { + + int depth = 1; + Node* p = t; + while (p->parent) { + ++depth; + p = p->parent; + } + return depth; +} +/*删除树中数据为c的节点*/ +void DeleteSpecialNode(Tree *pRoot, Elemtype c) { + + if ((*pRoot)->left) { + if ((*pRoot)->left->data == c) { + free((*pRoot)->left); + (*pRoot)->left = NULL; + } + else { + DeleteSpecialNode(&(*pRoot)->left, c); + } + } + if ((*pRoot)->right) { + if ((*pRoot)->right->data == c) { + free((*pRoot)->right); + (*pRoot)->right = NULL; + } + else { + DeleteSpecialNode(&(*pRoot)->right, c); + } + } +} +/*根据一个完整先序序列建立二叉树*/ +Tree newBinaryTree(char* str) { + + Node* current; + Tree tree; + InitializeTree(&tree); + /*如果是空字符串则什么都不做*/ + if (strlen(str) == 0) { + return tree; + } + debug_print("以【%s】为完整前序序列", str); + debug_print("在0x%p处建立二叉树\n", tree); + tree->data = str[0]; + current = tree; + while (*++str != '\0') { + if (current->left == NULL) { + current->left = newNode(); + current->left->parent = current; + current->left->data = *str; + if (*str != EMPTY) { + current = current->left; + } + }/*end if current->left == NULL*/ + else if (current->right == NULL) { + current->right = newNode(); + current->right->parent = current; + current->right->data = *str; + if (*str != EMPTY) { + current = current->right; + } + }/*end if current->left == NULL*/ + else { + current = current->parent; + --str; + } + }/*end while*/ + /*去掉数据为空格的叶节点*/ + DeleteSpecialNode(&tree, EMPTY); + return tree; +} +/*根据一个不完整的先序序列与一个不完整的中序序列建立二叉树*/ +Tree _newBinaryTree2(char* pre, char* in) { + + Tree tree = newNode(); + int len = strlen(pre); + char * p = in; + char* leftPre, *leftIn, *rightPre, *rightIn; + if (strlen(pre) == 1 && strlen(in) == 1) { + tree->data = *pre; + return tree; + } + tree->data = pre[0]; + divideString(in, pre[0], &leftIn, &rightIn); + /*有左子树*/ + if (strlen(leftIn)) { + leftPre = substr(pre, 2, strlen(leftIn) + 1); + tree->left = _newBinaryTree2(leftPre, leftIn); + tree->left->parent = tree; + } + /*有右子树*/ + if (strlen(rightIn)) { + rightPre = substr(pre, strlen(pre) - strlen(rightIn) + 1, strlen(pre)); + tree->right = _newBinaryTree2(rightPre, rightIn); + tree->right->parent = tree; + } + + return tree; +} +Tree newBinaryTree2(char*pre, char* in) { + Tree tree = _newBinaryTree2(pre, in); + debug_print("以【%s】与【%s】在0x%p处建立二叉树\n", pre, in, tree); + return tree; +} +/*计算二叉树的深度与宽度*/ +void _CalculateDepth(const Tree t , int* pDepth){ + int depth = getDepth(t); + if (depth > *pDepth) { + *pDepth = depth; + } +} +void _CalculateWidth(const Tree t , int*width) { + width[getDepth(t) - 1] += 1; +} +void CalculateDepthAndWidth(const Tree t, int* pD, int * pW) { + *pD = 0; + TraverseTree(t, pre, _CalculateDepth,pD); + int * width = (int*)malloc(*pD * sizeof(int)); + /*初始化为全0数组*/ + for (int i = 0; i < *pD; ++i) { width[i] = 0; } + TraverseTree(t, pre, _CalculateWidth, width); + *pW = 0; + for (int i = 0; i < *pD; ++i) { + if (width[i] > *pW) *pW = width[i]; + } + debug_print("计算树0x%p的深度与宽度 , 结果分别为 %d 与 %d \n",t,*pD,*pW); + free(width); +} +/*统计二叉树中所有叶与非叶节点的个数*/ +void _CountLeave(const Tree t, int* p) { + //printf("It's %c , its left is %c , its right is %c \n",t->data,t->left ? t->left->data : '0' , t->right ? t->right->data : '0'); + if (t->left == NULL && t->right == NULL) { + *p += 1; + } +} +void CountLeaves(const Tree t, int* pLeaf) { + *pLeaf = 0; + TraverseTree(t, pre, _CountLeave, pLeaf); + debug_print("树0x%p中叶节点个数为%d \n", t, *pLeaf); +} +void _CountBranch(const Tree t, int*p) { + if (t->left != NULL || t->right != NULL) { + *p += 1; + } +} +void CountBranches(const Tree t, int* pBranch) { + *pBranch = 0; + TraverseTree(t, pre, _CountBranch, pBranch); + debug_print("树0x%p中非叶节点个数为%d \n", t, *pBranch); +} +/*简易队列*/ +typedef struct _QueueNode { + Elemtype data; + struct _QueueNode * prev; + struct _QueueNode * next; +}QueueNode; +typedef struct _Queue { + QueueNode* head, *rear; +}Queue; +QueueNode* newQueueNode() { + QueueNode* p = (QueueNode*)malloc(sizeof(QueueNode)); + p->data = 0; p->next = p->prev = NULL; + return p; +} +Queue newQueue() { + Queue q; + q.head = q.rear = NULL; + return q; +} +int isEmpty(Queue* pQ) { + if (pQ->head && !pQ->rear || pQ->rear && !pQ->head) { + printf("What the f**k is this s**t ???"); + exit(-1); + } + return pQ->head == NULL; +} +void enQueue(Queue* pQ, Elemtype d) { + QueueNode* p = newQueueNode(); + p->data = d; + if (isEmpty(pQ)) { + pQ->head = pQ->rear = p; + } + else { + pQ->rear->next = p; + pQ->rear = p; + } +} +Elemtype deQueue(Queue* pQ) { + QueueNode* p = pQ->head; + Elemtype d = p->data; + if (isEmpty(pQ)) { + pQ->head = pQ->rear = NULL; + } + else { + pQ->head = pQ->head->next; + } + free(p); + return d; +} +/*判断是否为完全二叉树*/ +int _isCompleteBinaryTree(const Tree t, int *p , int num) { + if (!t) { return 1; } + else if (num > *p) { return 0; } + else { + return _isCompleteBinaryTree(t->left, p, num * 2) * _isCompleteBinaryTree(t->right, p, num * 2 + 1); + } +} +void _Count(const Tree t, int *p) { + *p += 1; +} +int isCompleteBinaryTree(const Tree t) { + int mount = 0; + TraverseTree(t, pre, _Count, &mount); + return _isCompleteBinaryTree(t, &mount , 1); +} \ No newline at end of file diff --git a/2017-1/zkz/Tree2/tree.h b/2017-1/zkz/Tree2/tree.h new file mode 100644 index 00000000..d7639e3a --- /dev/null +++ b/2017-1/zkz/Tree2/tree.h @@ -0,0 +1,46 @@ +#pragma once + +#include +#include +#include + +#define DEBUG 1 +#define debug_print(...) if(DEBUG){printf(__VA_ARGS__);} + + +typedef char Elemtype; +typedef struct _Node { + Elemtype data; + struct _Node* parent; + struct _Node* left; + struct _Node* right; +}Node, *Tree; +typedef enum _Order { pre, in, post, all } Order; + +/*===字符串相关===*/ +/*手动获取子串*/ +char* substr(char* source, int begin, int end); +/*手动分割字符串*/ +void divideString(char* source, char delim, char** pDes1, char** pDes2); + +inline Node* newNode(); +void InitializeTree(Tree* pT); +/*输出二叉树*/ +/*root : 待遍历的二叉树*/ +/*order : 遍历顺序(pre , in , post , all 分别为前序、中序、后序及所有顺序)*/ +void Show(const Tree root, Order order); +inline int getDepth(const Tree t); +/*删除树中数据为c的节点*/ +void DeleteSpecialNode(Tree *pRoot, Elemtype c); +/*根据一个完整先序序列建立二叉树*/ +Tree newBinaryTree(char* str); +/*根据一个不完整的先序序列与一个不完整的中序序列建立二叉树*/ +Tree _newBinaryTree2(char* pre, char* in); +Tree newBinaryTree2(char*pre, char* in); +/*计算二叉树的深度与宽度*/ +void CalculateDepthAndWidth(const Tree t, int* pD, int * pW); +/*统计二叉树中所有叶与非叶节点的个数*/ +void CountLeaves(const Tree t, int* pLeaf); +void CountBranches(const Tree t, int* pBranch); +/*判断是否为完全二叉树*/ +int isCompleteBinaryTree(const Tree t); \ No newline at end of file diff --git "a/2017-1/zkz/Tree2/\346\265\213\350\257\225\347\273\223\346\236\234.png" "b/2017-1/zkz/Tree2/\346\265\213\350\257\225\347\273\223\346\236\234.png" new file mode 100644 index 00000000..bfc1f980 Binary files /dev/null and "b/2017-1/zkz/Tree2/\346\265\213\350\257\225\347\273\223\346\236\234.png" differ diff --git a/2017-1/zkz/Tree3/Tree/AVLOutput.txt b/2017-1/zkz/Tree3/Tree/AVLOutput.txt new file mode 100644 index 00000000..cbc5eecf --- /dev/null +++ b/2017-1/zkz/Tree3/Tree/AVLOutput.txt @@ -0,0 +1,6 @@ +8, 4, 3, 1, 6, 5, 7, 14, 10, 22, 19, 30 +8, 4, 3, 1, 6, 5, 7, 14, 10, 13, 22, 19, 30 +7, 4, 3, 1, 6, 5, 14, 10, 13, 22, 19, 30 +7, 4, 3, 1, 6, 14, 10, 13, 22, 19, 30 +7, 4, 3, 1, 6, 14, 10, 13, 22, 19, 20, 30 +7, 3, 1, 4, 14, 10, 13, 22, 19, 20, 30 \ No newline at end of file diff --git a/2017-1/zkz/Tree3/Tree/BSTOutput.txt b/2017-1/zkz/Tree3/Tree/BSTOutput.txt new file mode 100644 index 00000000..eea55310 --- /dev/null +++ b/2017-1/zkz/Tree3/Tree/BSTOutput.txt @@ -0,0 +1,6 @@ +8, 3, 1, 6, 4, 5, 7, 10, 14, 19, 22, 30 +8, 3, 1, 6, 4, 5, 7, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 5, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 30 +7, 3, 1, 6, 4, 10, 14, 13, 19, 22, 20, 30 +7, 3, 1, 4, 10, 14, 13, 19, 22, 20, 30 \ No newline at end of file diff --git a/2017-1/zkz/Tree3/Tree/BTree.h b/2017-1/zkz/Tree3/Tree/BTree.h new file mode 100644 index 00000000..982bf54e --- /dev/null +++ b/2017-1/zkz/Tree3/Tree/BTree.h @@ -0,0 +1,228 @@ +#pragma once + +#include +#define M 3 +#define Node struct _Node +/*测试*/ +#define DEBUG 0 +#define debug_print(...) if(DEBUG)printf(__VA_ARGS__) + +typedef enum _bool {false,true}bool; +typedef int Elemtype; +/*3阶B树每个节点最多有3个子树和2个关键字*/ +typedef struct _Node { + Node* parent; + int usedKeys; + Elemtype keys[M - 1 + 1] ;//预留一位便于进行分裂操作 + Node* childs[M + 1]; +}*BTree; + +Node* newNode() { + Node* temp = (Node*)malloc(sizeof(Node)); + temp->parent = NULL; + temp->usedKeys = 0; + for (int i = 0; i < M; ++i) { + temp->keys[i] = 0; + temp->childs[i] = NULL; + } + temp->childs[M ] = NULL; + return temp; +} +bool isFull(Node* node) { + return node->usedKeys == M - 1; +} +bool isLeaf(Node* node) { + for (int i = 0; i <= node->usedKeys; ++i) { + if (node->childs[i])return false; + } + return true; +} +BTree newBTree() { + return newNode(); +} +void showNode(Node* node) { + printf("====== 【Node 0x%p】 ======\n",node); + printf("Parent : 0x%p\nUsedkeys : %d\n",node->parent,node->usedKeys); + printf("Keys : "); + for (int i = 0; i < M ; ++i)printf("%d ", node->keys[i]); printf("\n"); + printf("Childs : "); + for (int i = 0; i < M+1; ++i)printf("0x%p ", node->childs[i]); printf("\n"); + printf("=============================\n"); +} +/*将data按顺序添加进数组arr中*/ +void AppendElemtype(Elemtype* arr, int size , Elemtype data) { + int pos = 0; + while (arr[pos] && data > arr[pos] && pos<=size-1)++pos; + for (int i = size-2; i >= pos; --i) { + arr[i + 1] = arr[i]; + } + arr[pos] = data; +} +void AppendNodeStar(Node** arr,int size,Node*data) { + int pos = 0; + while (arr[pos] && data->keys[0] > arr[pos]->keys[0] && pos <= size - 1)++pos; + for (int i = size - 2; i >= pos; --i) { + arr[i + 1] = arr[i]; + } + arr[pos] = data; +} +/*将data从数组arr中删除*/ +void DeleteElemtype(Elemtype* arr, int size, Elemtype data) { + int pos=0; + while (data > arr[pos] && pos <= size-1)++pos; + for (int i = pos;ikeys[0] > arr[pos]->keys[0] && pos <= size - 1)++pos; + for (int i = pos; ichilds[i] && node->childs[i]->keys[0] < data) { + (*p1)->childs[(*p1)->usedKeys] = node->childs[i]; + (*p1)->childs[(*p1)->usedKeys]->parent = *p1; + } + else if (node->childs[i] && node->childs[i]->keys[0] > data) { + (*p2)->childs[(*p2)->usedKeys] = node->childs[i]; + (*p2)->childs[(*p2)->usedKeys]->parent = *p2; + } + if (i == M)break; + /*keys*/ + if (node->keys[i] < data) { + (*p1)->keys[(*p1)->usedKeys++] = node->keys[i]; + } + else if (node->keys[i] > data) { + (*p2)->keys[(*p2)->usedKeys++] = node->keys[i]; + } + } + debug_print("After divide : \n"); + if (DEBUG)showNode(*p1),showNode(*p2); +} +Elemtype swapWithMid(Elemtype* arr, int size, Elemtype data) { + Elemtype* _arr = (Elemtype*)malloc((size + 1) * sizeof(Elemtype)); + for (int i = 0; i < size; ++i)_arr[i] = arr[i]; + AppendElemtype(_arr, size, data); + int pos = (size + 1) / 2; + Elemtype mid = _arr[pos]; + DeleteElemtype(arr, size, mid); + AppendElemtype(arr, size-1 , data); + free(_arr); + return mid; +} +bool Find(BTree root, Elemtype data) { + int pos = 0; + while (data > root->keys[pos] && posusedKeys)pos += 1; + if (data == root->keys[pos]) + return true; + else + return root->childs[pos] ? Find(root->childs[pos], data) : false; +} +BTree Insert(BTree root, Elemtype data) { + if (!isLeaf(root)) { + int pos = 0; + while (data > root->keys[pos] && pos< root->usedKeys)++pos; + /*执行Insert(child,data)*/ + root = Insert(root->childs[pos],data); + while (root->parent)root = root->parent; + return root; + } + //程序执行到这里说明root是叶节点 + //然后先添加,再考虑超没超 + AppendElemtype(root->keys, M, data); root->usedKeys += 1; + //如果超了,再分裂 + while (root->usedKeys == M) { + Node*a, *b; + data = root->keys[M/2]; + DivideNode(root, data, &a, &b); + Node* parent = root->parent; + if (parent) { + DeleteNodeStar(parent->childs, M, root); + } + else { + parent = newNode(); + } + a->parent = parent; + b->parent = parent; + AppendElemtype(parent->keys, M , data); + parent->usedKeys += 1; + AppendNodeStar(parent->childs, M+1, a); + AppendNodeStar(parent->childs, M+1, b); + free(root); + root = parent; + } + return root; +} +BTree Remove(BTree root, Elemtype data) {} +/*按层遍历输出B树*/ +typedef struct _QueueNode { + Node* data; + struct _QueueNode* prev; + struct _QueueNode* next; +}QueueNode; +typedef struct _Queue { + QueueNode* front; + QueueNode* rear; +}Queue; +Queue newQueue() { + Queue q; q.front = q.rear = NULL; return q; +} +Queue EnQueue(Queue q,Node* data) { + QueueNode* temp = (QueueNode*)malloc(sizeof(QueueNode)); + temp->data = data; temp->next = temp->prev = NULL; + //如果是空队列 + if (!q.front) { + q.front = q.rear = temp; + } + else { + temp->prev = q.rear; + q.rear->next = temp; + q.rear = temp; + } + return q; +} +Queue DeQueue(Queue q, Node** result) { + if (q.front == q.rear) { + *result = q.front->data; + free(q.front); + q.front = q.rear = NULL; + } + else { + *result = q.front->data; + QueueNode* temp = q.front; + q.front = q.front->next; + free(temp); + } + return q; +} +void BFSprint(BTree root) { + Queue q = newQueue(); + Node* front; + q = EnQueue(q, root); + do { + q = DeQueue(q, &front); + for (int i = 0; i < front->usedKeys; ++i) { + printf("%d", front->keys[i]); + if (q.front || front->childs[0] || i<=front->usedKeys-1&&front->keys[i+1]) { + printf(", "); + } + } + for (int i = 0; i < M; ++i) { + if (front->childs[i]) + q = EnQueue(q, front->childs[i]); + } + } while (q.front); +} \ No newline at end of file diff --git a/2017-1/zkz/Tree3/Tree/Btest.c b/2017-1/zkz/Tree3/Tree/Btest.c new file mode 100644 index 00000000..d57dbeba --- /dev/null +++ b/2017-1/zkz/Tree3/Tree/Btest.c @@ -0,0 +1,26 @@ + +#include +#include "BTree.h" + +void Traverse(Node* tree) { + for (int i = 0; i < M; ++i) { + //childs + if (tree->childs[i])Traverse(tree->childs[i]); + if (i == M - 1)break; + //keys + if(tree->keys[i])printf("%d ",tree->keys[i]); + } +} +int main() { + BTree tree = newBTree(); + int inputs[] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + int size1 = 10; + for (int i = 0; i < size1; ++i) { + tree = Insert(tree, inputs[i]); + } + BFSprint(tree); + printf("\n"); + for (int i = 1; i <= 20; ++i) { + printf("%d\t%d\n",i,Find(tree,i)); + } +} \ No newline at end of file diff --git a/2017-1/zkz/Tree3/Tree/Tree.c b/2017-1/zkz/Tree3/Tree/Tree.c new file mode 100644 index 00000000..a7632a95 --- /dev/null +++ b/2017-1/zkz/Tree3/Tree/Tree.c @@ -0,0 +1,227 @@ +#include +#include "Tree.h" +Node* newNode() { + Node* temp = (Node*)malloc(sizeof(Node)); + temp->data = 0; + temp->parent = temp->left = temp->right = NULL; + return temp; +} +void Show(Node* tree) { + printf("%d", tree->data); + if (tree->left) { + printf(", "); + Show(tree->left); + } + if (tree->right) { + printf(", "); + Show(tree->right); + } +} +int getDepth(Node* tree) { + int leftDepth, rightDepth; + if (!tree)return 0; + leftDepth = 1 + getDepth(tree->left); + rightDepth = 1 + getDepth(tree->right); + return leftDepth > rightDepth ? leftDepth : rightDepth; +} +BST newBST(int data) { + BST bst = newNode(); + bst->data = data; + return bst; +} +bool BSTfind(BST bst, elemType data) { + if (data == bst->data)return true; + if (data < bst->data) return bst->left ? BSTfind(bst->left, data) : false; + if (data > bst->data)return bst->right ? BSTfind(bst->right, data) : false; + debug_print("%d", __LINE__); exit(-1); +} +BST BSTinsert(BST bst, elemType data) { + if (data < bst->data) { + if (hasLeft(bst)) { + BSTinsert(bst->left, data); + } + else { + bst->left = newNode(); + bst->left->data = data; + bst->left->parent = bst; + } + return bst; + } + if (data > bst->data) { + if (hasRight(bst)) { + BSTinsert(bst->right, data); + } + else { + bst->right = newNode(); + bst->right->data = data; + bst->right->parent = bst; + } + return bst; + } + printf("%d", __LINE__); exit(-1); +} +BST getMaxNode(BST bst) { + while (hasRight(bst))bst = bst->right; + return bst; +} +BST getMinNode(BST bst) { + while (hasLeft(bst))bst = bst->left; + return bst; +} +BST BSTremove(BST bst, elemType data) { + debug_print("BSTremove((%d),%d);\n", bst->data, data); + BST temp; + if (data < bst->data) BSTremove(bst->left, data); + else if (data > bst->data) BSTremove(bst->right, data); + else { + debug_print("Node to remove found.\n"); + /*对于叶节点,断开与parent的关系后删除即可*/ + if (isLeaf(bst)) { + debug_print("It's a leaf node\n"); + if (isLeftChild(bst))bst->parent->left = NULL; + else if (isRightChild(bst))bst->parent->right = NULL; + free(bst); + } + /*对于只有左/右孩子的情况,将左/右孩子连接至parent后删除即可*/ + else if (hasLeft(bst) && !hasRight(bst)) { + debug_print("It's a leaf with left child\n"); + if (isLeftChild(bst))bst->parent->left = bst->left; + else if (isRightChild(bst))bst->parent->right = bst->left; + free(bst); + } + else if (hasRight(bst) && !hasLeft(bst)) { + debug_print("It's a leaf with right child\n"); + if (isLeftChild(bst))bst->parent->left = bst->right; + else if (isRightChild(bst))bst->parent->right = bst->right; + free(bst); + } + /*对于待删除节点有两个孩子的情况,将其与左子树中值最大的节点交换,然后删除*/ + else { + debug_print("It's a leaf with double child\n"); + temp = getMaxNode(bst->left); + debug_print("Swap(%d,%d)\n", temp->data, bst->data); + Swap(temp->data, bst->data); + BSTremove(bst->left, temp->data); + } + }//end if data==bst->data + + return bst; +} +AVL newAVL(int data) { + AVL avl = newNode(); + avl->data = data; + return avl; +} +int getBalanceFactor(AVL avl) { + if (!avl) { printf(__FUNCTION__"NULL avl to get BF\n"); exit(-1); } + return getDepth(avl->left) - getDepth(avl->right); +} +bool isAVL(AVL avl) { + return avl ? abs(getBalanceFactor(avl)) <= 1 && isAVL(avl->left) && isAVL(avl->right) : true; +} +bool AVLfind(AVL avl, elemType data) { + return BSTfind(avl, data); +} +/*使非平衡树平衡的调整操作*/ +void rightRotate(AVL root) { + debug_print(__FUNCTION__" at node (%d)\n", root->data); + debug_print("Before : node(%d) is {%d,%d,%d}\n", root->data, Value(root->parent), Value(root->left), Value(root->right)); + + AVL temp = root->left; + if (isLeftChild(root))root->parent->left = temp; + else if (isRightChild(root)) root->parent->right = temp; + temp->parent = root->parent; + + root->left = temp->right; + if (temp->right)temp->right->parent = root; + temp->right = root; + root->parent = temp; + debug_print("After : node(%d) is {%d,%d,%d}\n", root->data, Value(root->parent), Value(root->left), Value(root->right)); + root = root->parent; + debug_print("After : node(%d) is {%d,%d,%d}\n", root->data, Value(root->parent), Value(root->left), Value(root->right)); +} +void leftRotate(AVL root) { + debug_print(__FUNCTION__" at node (%d)\n", root->data); + debug_print("Before : node(%d) is {%d,%d,%d}\n", root->data, Value(root->parent), Value(root->left), Value(root->right)); + + AVL temp = root->right; + if (isLeftChild(root))root->parent->left = temp; + else if (isRightChild(root)) root->parent->right = temp; + temp->parent = root->parent; + + root->right = temp->left; + if (temp->left)temp->left->parent = root; + temp->left = root; + root->parent = temp; + debug_print("After : node(%d) is {%d,%d,%d}\n", root->data, Value(root->parent), Value(root->left), Value(root->right)); + root = root->parent; + debug_print("After : node(%d) is {%d,%d,%d}\n", root->data, Value(root->parent), Value(root->left), Value(root->right)); +} +AVL AVLfix(AVL avl) { + AVL temp = avl; + bool leftIsAVL, rightIsAVL; + int BF, leftBF, rightBF; + if (isAVL(avl))return avl; + debug_print("node (%d) need to fix\n", avl->data); + leftIsAVL = isAVL(temp->left); + rightIsAVL = isAVL(temp->right); + /*如果自己不平衡但左右子树平衡,则自己就是最小非平衡二叉树*/ + while (!leftIsAVL || !rightIsAVL) { + /*否则如果左/右子树不平衡,那就让自己成为左/右子树*/ + temp = leftIsAVL ? temp->right : temp->left; + + leftIsAVL = isAVL(temp->left); + rightIsAVL = isAVL(temp->right); + } + debug_print("The minimum unbalanced tree is at node (%d)\n", temp->data); + /*此时,temp应该就是最小非平衡二叉树了*/ + BF = getBalanceFactor(temp); + if (BF == 2) { + leftBF = getBalanceFactor(temp->left); + if (leftBF == 1) { + debug_print("It needs a LL\n"); + rightRotate(temp); + } + else if (leftBF == -1) { + debug_print("It needs a LR\n"); + leftRotate(temp->left); + rightRotate(temp); + } + else { + printf("%d Boom!\n", __LINE__); + exit(-1); + } + }//end if BF==2 + else if (BF == -2) { + rightBF = getBalanceFactor(temp->right); + if (rightBF == 1) { + debug_print("It needs a RL\n"); + rightRotate(temp->right); + leftRotate(temp); + } + else if (rightBF == -1) { + debug_print("It needs a RR\n"); + leftRotate(temp); + } + else { + printf("%d Boom!\n", __LINE__); + exit(-1); + } + }//end if BF==-2 + else { + printf("%d BOOMSHAKALAKA!\n", __LINE__); + exit(-1); + } + while (avl->parent)avl = avl->parent; + return avl; +} +AVL AVLinsert(AVL avl, elemType data) { + debug_print(__FUNCTION__"((%d),%d)\n", avl->data, data); + BSTinsert(avl, data); + return AVLfix(avl); +} +AVL AVLremove(AVL avl, elemType data) { + debug_print(__FUNCTION__"((%d),%d)\n", avl->data, data); + BSTremove(avl, data); + return AVLfix(avl); +} \ No newline at end of file diff --git a/2017-1/zkz/Tree3/Tree/Tree.h b/2017-1/zkz/Tree3/Tree/Tree.h new file mode 100644 index 00000000..b1748461 --- /dev/null +++ b/2017-1/zkz/Tree3/Tree/Tree.h @@ -0,0 +1,53 @@ +#pragma once + +#define Node struct _Node +/*树相关的判断宏*/ +#define isLeaf(node) (!(node)->left && !(node)->right) +#define hasParent(node) (!!(node)->parent) +#define hasLeft(node) (!!(node)->left) +#define hasRight(node) (!!(node)->right) +#define isLeft(child,parent) ((child) == (parent)->left) +#define isRight(child,parent) ((child) == (parent)->right) +#define isLeftChild(node) (hasParent(node) && isLeft((node),(node)->parent)) +#define isRightChild(node) (hasParent(node) && isRight((node),(node)->parent)) +/*交换*/ +#define Swap(a,b) if((a)!=(b))(a)^=(b),(b)^=(a),(a)^=(b) +/*测试用相关宏*/ +#define DEBUG 0 +#define debug_print(...) if(DEBUG)printf(__VA_ARGS__) +#define Value(p) ((p) ? (p)->data : 0) + +#include + +typedef enum _bool {false,true} bool; +typedef int elemType; +typedef struct _Node{ + elemType data; + Node * parent; + Node * left; + Node * right; +}*BST,*AVL; + +Node* newNode(); +/*先序遍历输出*/ +void Show(Node* tree); +int getDepth(Node* tree); + +BST newBST(int data); +bool BSTfind(BST bst, elemType data); +BST BSTinsert(BST bst, elemType data); +BST getMaxNode(BST bst); +BST getMinNode(BST bst); +BST BSTremove(BST bst, elemType data); + +AVL newAVL(int data); +int getBalanceFactor(AVL avl); +bool isAVL(AVL avl); +bool AVLfind(AVL avl, elemType data); +/*使非平衡树平衡的调整操作*/ +void rightRotate(AVL root); +void leftRotate(AVL root); +AVL AVLfix(AVL avl); + +AVL AVLinsert(AVL avl, elemType data); +AVL AVLremove(AVL avl, elemType data); \ No newline at end of file diff --git a/2017-1/zkz/Tree3/Tree/test.c b/2017-1/zkz/Tree3/Tree/test.c new file mode 100644 index 00000000..a25c2bba --- /dev/null +++ b/2017-1/zkz/Tree3/Tree/test.c @@ -0,0 +1,51 @@ +#include +#include "Tree.h" + +void SuperShow(Node* tree) { + if (!tree)return; + if(!isLeaf(tree))printf("("); + SuperShow(tree->left); + if (tree->left)printf("←"); + printf(" %d ",tree->data); + if (tree->right)printf("→"); + SuperShow(tree->right); + if(!isLeaf(tree))printf(")"); +} + +int main() { + /*测试输入及其个数*/ + int inputs[] = { 8, 10, 14, 3, 1, 6, 4, 7, 5, 19, 22, 30 }; + int size1 = 12; + int toFind[] = { 13, 8, 5, 20, 6 }; + int size2 = 5; + + /*初始化查找表*/ + BST bst = newBST(inputs[0]); + AVL avl = newAVL(inputs[0]); + for (int i = 1; i < size1; ++i) { + bst = BSTinsert(bst, inputs[i]); + avl = AVLinsert(avl, inputs[i]); + if (DEBUG)SuperShow(avl),printf("\n"); + } + printf("BST:\n"); + Show(bst); + printf("\n"); + /*开始查找操作*/ + for (int j = 0; j < size2; ++j) { + /*如果找到就删除,没找到就插入*/ + BSTfind(bst, toFind[j]) ? BSTremove(bst, toFind[j]) : BSTinsert(bst, toFind[j]); + Show(bst); + printf("\n"); + } + + printf("AVL:\n"); + Show(avl); + printf("\n"); + /*开始查找操作*/ + for (int j = 0; j < size2; ++j) { + /*如果找到就删除,没找到就插入*/ + AVLfind(avl, toFind[j]) ? AVLremove(avl, toFind[j]) : AVLinsert(avl, toFind[j]); + Show(avl); + printf("\n"); + } +} \ No newline at end of file diff --git "a/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250.sln" "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250.sln" new file mode 100644 index 00000000..9df9715b --- /dev/null +++ "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250.sln" @@ -0,0 +1,28 @@ +锘 +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.26228.10 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "鎷嗚В閾捐〃", "鎷嗚В閾捐〃\鎷嗚В閾捐〃.vcxproj", "{BA5D5828-803C-42FC-95AF-9999E2706CB4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {BA5D5828-803C-42FC-95AF-9999E2706CB4}.Debug|x64.ActiveCfg = Debug|x64 + {BA5D5828-803C-42FC-95AF-9999E2706CB4}.Debug|x64.Build.0 = Debug|x64 + {BA5D5828-803C-42FC-95AF-9999E2706CB4}.Debug|x86.ActiveCfg = Debug|Win32 + {BA5D5828-803C-42FC-95AF-9999E2706CB4}.Debug|x86.Build.0 = Debug|Win32 + {BA5D5828-803C-42FC-95AF-9999E2706CB4}.Release|x64.ActiveCfg = Release|x64 + {BA5D5828-803C-42FC-95AF-9999E2706CB4}.Release|x64.Build.0 = Release|x64 + {BA5D5828-803C-42FC-95AF-9999E2706CB4}.Release|x86.ActiveCfg = Release|Win32 + {BA5D5828-803C-42FC-95AF-9999E2706CB4}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git "a/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/Linklist.c" "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/Linklist.c" new file mode 100644 index 00000000..5aa1191a --- /dev/null +++ "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/Linklist.c" @@ -0,0 +1,117 @@ +#include "Linklist.h" + +Status InitList(Linklist* pL, const int length) { + int i;/*循环变量*/ + Node* p;/*指向节点的指针 , 参与构建链表*/ + + /*参数检查*/ + checkNull(pL, "pL"); + + if (length == 0) { (*pL) = NULL; return OK; } + (*pL) = NEWNODE; + checkOverflow(*pL, "*pL"); + (*pL)->data = rd(MINNUM, MAXNUM); + (*pL)->next = (*pL)->prev = *pL; + if (length == 1) { return OK; } + + /*循环构造节点*/ + times(i, length - 1) { + p = NEWNODE; + checkOverflow(p, "p"); + p->data = rd(MINNUM, MAXNUM); + p->next = (*pL); + p->prev = (*pL)->prev; + (*pL)->prev->next = p; + (*pL)->prev = p; + } + return OK; +} +Status TraverseList(const Linklist L) { + Node* p;/*指向节点的指针 , 用于遍历链表L*/ + + if (!L) { + printf("(empty)\n"); + return OK; + } + + p = L; + do { + printf("%d ", p->data); + p = p->next; + } while (p != L); + printf("\n"); + return OK; +} +Status Insert(Linklist* pL, Elemtype data) { + Node *p;/*指向新节点的指针*/ + + /*参数检查*/ + checkNull(pL, "pL"); + + p = NEWNODE; + checkOverflow(p, "p"); + p->data = data; + if (!(*pL)->next) { + p->prev = p->next = p; + (*pL)->next = p; + } + else { + p->next = (*pL)->next; + p->prev = (*pL)->next->prev; + (*pL)->next->prev->next = p; + (*pL)->next->prev = p; + } + return OK; +} +Status DivideList(Linklist list, Linklist* pL1, Linklist* pL2) { + Node *p;/* 指向节点的指针 , 用于遍历list */ + Node *p1;/* 指向节点的指针 , 用于遍历*pL1 */ + Node *p2;/* 指向节点的指针 , 用于遍历*pL2 */ + + /*参数检查*/ + checkNull(pL1, "pL1"); + checkNull(pL2, "pL2"); + + debug_print("====拆解链表====\n"); + debug_print("待拆解的链表 : "); + DEBUG && TraverseList(list); + debug_print("开始拆解 \n"); + + *pL1 = NEWNODE; + (*pL1)->data = 0; + (*pL1)->prev = (*pL1)->next = NULL; + p1 = *pL1; + *pL2 = NEWNODE; + (*pL2)->data = 0; + (*pL2)->prev = (*pL2)->next = NULL; + p2 = *pL2; + + p = list; + if (!p) { return OK; } + do { + p1->next = p; + p1->next->prev = p1; + p1 = p1->next; + ++(*pL1)->data; + debug_print("奇数项 , 分配给链表1 , 节点数据为 %d \n", p->data); + p = p->next; + if (p == list) { break; } + + p2->next = p; + p2->next->prev = p2; + p2 = p2->next; + ++(*pL2)->data; + debug_print("偶数项 , 分配给链表2 , 节点数据为 %d \n", p->data); + p = p->next; + } while (p != list); + + p1->next = (*pL1)->next; + p2->next = (*pL2)->next; + if (p1->next) { + p1->next->prev = p1; + } + if (p2->next) { + p2->next->prev = p2; + } + return OK; +} \ No newline at end of file diff --git "a/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/Linklist.h" "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/Linklist.h" new file mode 100644 index 00000000..188226f9 --- /dev/null +++ "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/Linklist.h" @@ -0,0 +1,38 @@ +#pragma once + +#include +#include "Status.h" + +#define NEWNODE (Node*)malloc(sizeof(Node)) +#define MAXNUM 99 +#define MINNUM 10 +/*rd : 返回一个不小于Min且不大于Max的随机数*/ +#define rd(Min,Max) (rand()%((Max)-(Min)+1)+(Min)) +#define DEBUG 1 +#define debug_print(...) if(DEBUG){printf(__VA_ARGS__);} + +typedef int Elemtype; +typedef struct _Node { + Elemtype data; + struct _Node* prev; + struct _Node* next; +}Node,*Linklist; + +/*初始化链表*/ +/*pL : 待初始化链表的地址*/ +/*length : 链表长度*/ +Status InitList(Linklist* pL, const int length); +/*遍历打印链表*/ +/*L : 待遍历链表*/ +Status TraverseList(const Linklist L); +/*在表尾插入元素*/ +/*pL : 待插入链表的地址*/ +/*data : 待插入数据*/ +Status Insert(Linklist* pL, Elemtype data); + +/*拆解链表*/ +/*将一个链表中的奇偶项分别拆分到两个新的链表中*/ +/*list : 待分解链表*/ +/*pL1 : 存放奇数项的链表的头节点地址*/ +/*pL2 : 存放偶数项的链表的头节点地址*/ +Status DivideList(Linklist list, Linklist* pL1, Linklist* pL2); \ No newline at end of file diff --git "a/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/README.md" "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/README.md" new file mode 100644 index 00000000..9d286b7b Binary files /dev/null and "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/README.md" differ diff --git "a/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/Status.h" "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/Status.h" new file mode 100644 index 00000000..0ed0b4e7 --- /dev/null +++ "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/Status.h" @@ -0,0 +1,46 @@ +/* +该头文件主要存放Status的定义及其相关操作 +同时也存放了一些其他常用的内容 +*/ +#pragma once + +#include + +/*枚举类型Status*/ +typedef enum _Status { OK, ERROR, OVERFLOW } Status; + +/*检查函数返回值 , 如果不为OK则在当前函数中return -1*/ +/*用法 : checkStatu( 函数调用 ); */ +#define checkStatu(func) \ +do {\ + switch (func) {\ + case OK:\ + break;\ + case ERROR:\ + printf("ERROR");\ + return -1;\ + break;\ + case OVERFLOW:\ + printf("OVERFLOW");\ + return -1;\ + break;\ + default:\ + printf("UNDEFINED");\ + return -1;\ + break;\ + }/*end switch*/\ +} while (0) + +/*参数检查 : 检查参数是否为空 , 若空则返回ERROR*/ +#define checkNull(arg,argName) \ +if(!arg){printf("%s is NULL , return ERROR \n",argName);return ERROR;} +/*检查变量是否为空 , 若空则返回OVERFLOW*/ +#define checkOverflow(var,varName) \ +if(!var){printf("%s is NULL , return OVERFLOW",varName);return OVERFLOW;} + +/* ==========其他常用内容==========*/ + +/*手动bool类型*/ +typedef enum _bool { false, true } bool; +/*快速生成一个循环size次的for循环 , 循环形参为i*/ +#define times(i,size) for((i)=1;(i)<=(size);++(i)) \ No newline at end of file diff --git "a/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/test.c" "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/test.c" new file mode 100644 index 00000000..b38705bb --- /dev/null +++ "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/test.c" @@ -0,0 +1,35 @@ +#include +#include +#include +#include "Linklist.h" + +#define MAXLEN 5 +#define MINLEN 0 + +int test() { + Linklist list, l1, l2; + + srand(rand()); + InitList(&list,rd(MINLEN, MAXLEN)); + + printf("list : "); + TraverseList(list); + + checkStatu(DivideList(list,&l1,&l2)); + printf("\n\nDivide list as l1 and l2 \n"); + printf("l1->data : %d \n", l1->data); + printf("l1 : "); + TraverseList(l1->next); + + printf("l2->data : %d \n", l2->data); + printf("l2 : "); + TraverseList(l2->next); +} +int main() { + srand(time(0)); + while (1) { + system("cls"); + test(); + system("pause"); + } +} \ No newline at end of file diff --git "a/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250.vcxproj" "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250.vcxproj" new file mode 100644 index 00000000..b705e0f8 --- /dev/null +++ "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250.vcxproj" @@ -0,0 +1,152 @@ +锘 + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {BA5D5828-803C-42FC-95AF-9999E2706CB4} + Win32Proj + 鎷嗚В閾捐〃 + 10.0.14393.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + + + true + + + false + + + false + + + + + + Level3 + Disabled + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + + + + + + + Level3 + Disabled + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + + + + + Level3 + + + MaxSpeed + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + + + + + Level3 + + + MaxSpeed + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + + + Console + true + true + + + + + + + + + + + + + + \ No newline at end of file diff --git "a/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250.vcxproj.filters" "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250.vcxproj.filters" new file mode 100644 index 00000000..2e1ed423 --- /dev/null +++ "b/2017-1/zkz/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250/\346\213\206\350\247\243\351\223\276\350\241\250.vcxproj.filters" @@ -0,0 +1,33 @@ +锘 + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 婧愭枃浠 + + + 婧愭枃浠 + + + + + 澶存枃浠 + + + 澶存枃浠 + + + \ No newline at end of file diff --git a/2017-1/zzskin/text3-2-1.c b/2017-1/zzskin/text3-2-1.c new file mode 100644 index 00000000..8a377eab --- /dev/null +++ b/2017-1/zzskin/text3-2-1.c @@ -0,0 +1,110 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配量 +#define STACKINCREMENT 10 //存储空间分配增量 + +typedef int SElemType ; +typedef enum{ + False, + True +}Bool; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; //枚举定义(返回状态) + +typedef struct _SqStack{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; //顺序栈的定义 + +Status InitStack(SqStack *S) //构造顺序栈 +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if(! S->base) + { + return OVERFLOW; + }//存储分配失败 + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + +Status Push(SqStack *S,SElemType e) +{ + if(S->top - S->base >= S->stacksize) + { + S->base = (SElemType *)realloc(S->base,(S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if(!S->base) + { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +} + +Bool IsStackEmpty(SqStack *S) +{ + if(S->top == S->base) + return True; + else + return False; +} + +Status Pop(SqStack *S,SElemType *e) +{ + if(IsStackEmpty(S)) + { + return ERROR; + + } + *e = *--S->top; + + return OK; +} + +Status conversion(SqStack *S,int input,int d) +{ + SElemType e; + if(d > 10) + { + return ERROR; + } + while(input) + { + Push(S,input % d); + input = input / d; + } + while(!IsStackEmpty(S)) + { + Pop(S,&e); + printf("%d",e); + + } + printf("\n"); + return OK; +} + +int main() +{ + SqStack S; + int input; + int d ; + srand(time(NULL)); + input = rand() % 1024; + d = 8; + InitStack(&S); + + printf("input = %d\n",input); + conversion(&S, input,d); + return 0; +} \ No newline at end of file diff --git a/2017-1/zzskin/text3-2-2.c b/2017-1/zzskin/text3-2-2.c new file mode 100644 index 00000000..508d00e4 --- /dev/null +++ b/2017-1/zzskin/text3-2-2.c @@ -0,0 +1,171 @@ +#include +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配量 +#define STACK_SIZE 100 //存储空间分配增量 +#define STACKINCREMENT 10 //存储空间分配增量 + +typedef char SElemType ; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; //枚举定义(返回状态) + +typedef enum{ + False, + True +}Bool; //定义布尔类型 + +typedef struct _SqStack{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; //顺序栈的定义 + +Status InitStack(SqStack *S,int size); +Status Push(SqStack *S,SElemType e); +Bool IsStackEmpty(SqStack *S); +SElemType GetTop(SqStack *S,SElemType *e); +Status Pop(SqStack *S,SElemType *e); +void DestroyStack(SqStack *S); +Status matching(SqStack S, char *exp); + +Status InitStack(SqStack *S,int size) //构造顺序栈 +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if(! S->base) + { + return OVERFLOW; + }//存储分配失败 + S->top = S->base; + S->stacksize = size; + return OK; +} + +Status Push(SqStack *S,SElemType e)//插入元素为e的新的栈顶元素 +{ + if(S->top - S->base >= S->stacksize) + { + S->base = (SElemType *)realloc(S->base,(S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if(!S->base) + { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +} + +Bool IsStackEmpty(SqStack *S)//判断栈顶是否为空; +{ + if(S->top == S->base) + return True; + else + return False; +} + +SElemType GetTop(SqStack *S,SElemType *e) +{ + if(IsStackEmpty(S)) + { + return ERROR; + } + *e = *--S->top; + return OK; +} + +Status Pop(SqStack *S,SElemType *e)//若栈不为空,则删除s的栈顶元素,用e返回值,并返回OK,否则返回ERROR; +{ + if(IsStackEmpty(S)) + { + return ERROR; + + } + *e = *--S->top; + + return OK; +} + +void DestroyStack(SqStack *S)//销毁栈s,s不存在 +{ + if(S) + { + S = NULL; + } + free(S); +} +Status matching(SqStack S, char *exp)//括号是否匹配 +{ + int state = 1; + int i = 0; + int flag = 0; + SElemType e; + while(exp[i]!='\0') + { + switch(exp[i]) + { + case '(': + Push(&S,exp[i]); + break; + case '[': + Push(&S, exp[i]); + break; + case '{': + Push(&S, exp[i]); + break; + + case ')': + { + Pop(&S ,&e); + if(e != '(') + flag = 1; + } + break; + + case ']': + { + Pop(&S ,&e); + if(e != '[') + flag = 1; + } + break; + case '}': + { + Pop(&S ,&e); + if(e != '{') + flag = 1; + } + break; + } + i++; + } + if(IsStackEmpty(&S) && state) + return OK; + else + return ERROR; +} +int main() +{ + SqStack S; + Status ret;//cserfsedc + char test[100]; + scanf("%s",&test); + InitStack(&S,STACK_SIZE); + ret = matching(S ,test); + + if(ret == OK) + { + printf("匹配成功!\n"); + } + else + { + printf("匹配失败!\n"); + } + return 0; +} \ No newline at end of file diff --git a/2017-1/zzskin/text3-2-3.c b/2017-1/zzskin/text3-2-3.c new file mode 100644 index 00000000..7dd51fd8 --- /dev/null +++ b/2017-1/zzskin/text3-2-3.c @@ -0,0 +1,165 @@ +#include +#include +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配量 +#define STACK_SIZE 100 //存储空间分配增量 +#define STACKINCREMENT 10 //存储空间分配增量 + +typedef char SElemType ; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; //枚举定义(返回状态) + +typedef enum{ + False, + True +}Bool; //定义布尔类型 + +typedef struct _SqStack{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; //顺序栈的定义 + +Status InitStack(SqStack *S); +Status DestroyStack (SqStack *S); +Status Pop(SqStack *S,SElemType *e); +Bool IsStackEmpty(SqStack *S); +Status ClearStack(SqStack *S); +Status Push(SqStack *S,SElemType e); +SElemType GetTop(SqStack *S,SElemType *e); +void LineEdit(SqStack *S); + + +Status InitStack(SqStack *S) //构造顺序栈 +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if(! S->base) + { + return OVERFLOW; + }//存储分配失败 + S->top = S->base; + S->stacksize = STACK_INIT_SIZE; + return OK; +} + + +Status DestroyStack (SqStack *S)//销毁栈s,s不存在 +{ + S->top = S->base; + free(S->base); + S->base = NULL; + S->top = NULL; + S->stacksize = 0; + return OK; +} + +Status Pop(SqStack *S,SElemType *e)//若栈不为空,则删除s的栈顶元素,用e返回值,并返回OK,否则返回ERROR; +{ + if(IsStackEmpty(S)) + { + return OK; + } + *e = *--S->top; + return ERROR; +} + +Bool IsStackEmpty(SqStack *S)//判断栈顶是否为空; +{ + if(S->top == S->base) + return True; + else + return False; +} + +Status ClearStack(SqStack *S)//清空栈 +{ + SElemType *e=(SElemType*)malloc(sizeof(SElemType)); + if (IsStackEmpty(S)) + return OK; + else + { + while (*--S->top != ' ') + Pop(S, e); + return ERROR; + } +} + +Status Push(SqStack *S,SElemType e)//插入元素为e的新的栈顶元素 +{ + if(S->top - S->base >= S->stacksize) + { + S->base = (SElemType *)realloc(S->base,(S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if(!S->base) + { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +} + +SElemType GetTop(SqStack *S,SElemType *e) +{ + if(IsStackEmpty(S)) + { + return ERROR; + } + *e = *--S->top; + return OK; +} + +void LineEdit(SqStack *S) +{ + SElemType *c = (SElemType*)malloc(sizeof(SElemType)); + char ch; + //利用字符栈S,从终端接收一行并传送至调用过程得数据区 + InitStack(S); + ch = getchar();//接收第一个字母 + while(ch != EOF) + { + while(ch != EOF && ch != '\n') + { + switch(ch) + { + case '#': + Pop(S,c); + break;//仅当栈非空时退栈 + case '@': + ClearStack(S); + break;//重置S为空栈 + default: + Push(S,ch); + break;//有效字符进栈,未考虑栈满情形 + } + ch = getchar(); //从终端接收下一个字符 + } + while(!IsStackEmpty(S)) + { + Pop(S,c); + printf("%c",*c); + } + ClearStack(S); + if ( ch != EOF) + ch = getchar(); + } + DestroyStack(S); +} + +int main() +{ + SqStack S; + InitStack(&S); + printf("输出:\n"); + LineEdit(&S); //行编辑 + DestroyStack(&S); //销毁栈 + return 0; +} \ No newline at end of file diff --git a/2017-1/zzskin/x2.12.c b/2017-1/zzskin/x2.12.c new file mode 100644 index 00000000..98d4ad09 --- /dev/null +++ b/2017-1/zzskin/x2.12.c @@ -0,0 +1,84 @@ +#include +#include +#include + +typedef struct LNode +{ + int data; + struct LNode *next; +}LNode, *LinkList; + +void CreateList (LinkList *L,int n) +{ + LinkList p; + int data = 20; + int i; + (*L) = (LinkList)malloc(sizeof(LNode)); + (*L)->next = NULL; + srand(( int)time(NULL)); + printf("随机产生数据\n"); + for(i=0;idata = data-=rand()% 10; //使前一个数据必定比后一个数据大,且两者之差再1到5; + printf("%d ",p->data); + p->next = (*L)->next; + (*L)->next=p; + } + printf("\n"); +} + +void MergeList ( LinkList La,LinkList Lb,LinkList *Lc) +{ + LNode *pa,*pb,*pc; + pa = La->next; + pb = Lb->next; + *Lc = pc = La; + while(pa && pb) + { + if(pa->data <= pb->data) + { + pc ->next = pa; + pc = pa; + pa = pa->next; + } + else + { + pc->next = pb; + pc = pb; + pb = pb->next; + } + } + pc->next = pa ? pa :pb; + if(Lb != NULL) + free(Lb); +} + +void output (LinkList Lc ) +{ + LNode *p; + p = Lc->next; + printf("输出合并链表Lc:\n"); + while( p!=NULL ) + { + printf("%d ",p->data) ; + p=p->next; + } + printf("\n"); +} + +int main() +{ + LinkList La,Lb,Lc; + int a = 0,b = 0; + srand(( int)time(NULL)); + a=10+rand()%5; + printf("La的长度为%d\n",a); + b=10+rand()%5; + printf("Lb的长度为%d\n",b); + CreateList( &La ,a); + CreateList( &Lb ,b); + MergeList( La, Lb ,&Lc); + output( Lc); + return 0; +} \ No newline at end of file diff --git "a/2017-1/zzskin/\344\272\214\345\217\211\346\216\222\345\210\227\346\240\221/test-8.c" "b/2017-1/zzskin/\344\272\214\345\217\211\346\216\222\345\210\227\346\240\221/test-8.c" new file mode 100644 index 00000000..74e82f06 --- /dev/null +++ "b/2017-1/zzskin/\344\272\214\345\217\211\346\216\222\345\210\227\346\240\221/test-8.c" @@ -0,0 +1,218 @@ +锘#include +#include +#include +#define TRUE 1 +#define FALSE 0 + +typedef int ElemType; +typedef char KeyTypem; //鍏抽敭瀛楃被鍨 + +typedef struct BiTNode +{ + ElemType data; + struct BiTNode *lchild, *rchild; //宸﹀彸瀛╁瓙鐨勬寚閽 +}BiTNode, *BiTree; + +typedef enum +{ + ERROR, + OK +}Status; //鏋氫妇瀹氫箟锛堣繑鍥炵姸鎬侊級 + +typedef enum +{ + True, + False +}Bool; //瀹氫箟甯冨皵绫诲瀷 + +Status SearchBST(BiTree T,ElemType key) +{ + /*聽閫掑綊鏌ユ壘浜屽弶鎺掑簭鏍慣涓槸鍚﹀瓨鍦╧ey聽*/ + /*聽鎸囬拡f鎸囧悜T鐨勫弻浜诧紝鍏跺垵濮嬭皟鐢ㄥ间负NULL聽*/ + /*聽鑻ユ煡鎵炬垚鍔燂紝鍒欐寚閽坧鎸囧悜璇ユ暟鎹厓绱犺妭鐐癸紝骞惰繑鍥濼RUE聽*/ + /*聽鍚﹀垯鎸囬拡p鎸囧悜鏌ユ壘璺緞涓婅闂殑鏈鍚庝竴涓妭鐐瑰苟杩斿洖FALSE聽*/ + if( !T ) + { + return ERROR; + } + else + { + if(key == T->data) //鎵惧埌鍏抽敭瀛楃瓑浜巏ey鐨勬暟鎹厓绱 + { + return OK; + } + else if(key < T->data) + { + return SearchBST( T->lchild, key);/*聽鍦ㄥ彸瀛愭爲缁х画鏌ユ壘*/ + } + else + { + return SearchBST( T->rchild, key);/*聽鍦ㄥ乏瀛愭爲缁х画鏌ユ壘*/ + } + } + return OK; +} + +int InsertBST(BiTree *T, int key) +{ + /*聽褰撲簩鍙夋帓搴忔爲T涓笉瀛樺湪鍏抽敭瀛楃瓑浜巏ey鐨勬暟鎹厓绱犳椂聽*/ + /*聽鎻掑叆key骞惰繑鍥濼RUE锛屽惁鍒欒繑鍥濬ALSE聽*/ + /*聽璋冪敤鏌ユ壘鍑芥暟SearchBST锛岄潪閫掑綊聽*/ + if( !(*T) ) + { + *T = (BiTree)malloc(sizeof(BiTNode)); + (*T)->data = key; + (*T)->lchild = (*T)->rchild = NULL; + return 1; + } + if( key == (*T)->data ) + { + return 0; + } + if( key > (*T)->data) + { + return InsertBST(&((*T)->rchild),key);/*聽鎻掑叆鍙冲瀛惵*/ + } + else + { + return InsertBST(&((*T)->lchild),key);/*聽鎻掑叆宸﹀瀛惵*/ + } +} + +Status createBST(BiTree *T, int a[], int n) +{ + int i; + *T = NULL; + + for(i = 0; i < n; i++) + { + InsertBST(T, a[i]); + } + return OK; +} + +void preOrderTraverse(BiTree T) +{ + if(T) + { + printf("%d ",T->data); + preOrderTraverse(T->lchild); + preOrderTraverse(T->rchild); + } +} + +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)->data = s->data; + if( q != *p ) /*聽鍒ゆ柇鏄惁鎵ц涓婅堪while寰幆聽*/ + { + q->rchild = s->lchild; /*聽鎵ц涓婅堪while寰幆锛岄噸鎺ュ彸瀛愭爲聽*/ + } + else + { + q->lchild = s->rchild; /*聽鏈墽琛屼笂杩皐hile寰幆锛岄噸鎺ュ乏瀛愭爲*/ + } + free(s); + } + return OK; +} + +Status DeleteBST(BiTree *T, int key) +{ +/*聽鑻ヤ簩鍙夋帓搴忔爲T涓瓨鍦ㄥ叧閿瓧绛変簬key鐨勬暟鎹厓绱犳椂锛屽垯鍒犻櫎璇ユ暟鎹厓绱犺妭鐐孤*/ +/*聽骞惰繑鍥濼RUE锛涘惁鍒欒繑鍥濬ALSE聽*/ + if( !(*T) ) + { + return ERROR; /*聽涓嶅瓨鍦ㄥ叧閿瓧绛変簬key鐨勬暟鎹厓绱*/ + } + else + { + if( key == (*T)->data ) + { + Delete(T); + } + else if(key < (*T)->data ) + { + 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("浜屽弶鏍:\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("鎻掑叆澶辫触 \n"); + } + } + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/zzskin/\344\272\214\345\217\211\346\216\222\345\210\227\346\240\221/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221.txt" "b/2017-1/zzskin/\344\272\214\345\217\211\346\216\222\345\210\227\346\240\221/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221.txt" new file mode 100644 index 00000000..cdf478fe --- /dev/null +++ "b/2017-1/zzskin/\344\272\214\345\217\211\346\216\222\345\210\227\346\240\221/\344\272\214\345\217\211\346\216\222\345\272\217\346\240\221.txt" @@ -0,0 +1,6 @@ +8 3 1 6 4 5 7 10 14 19 22 30 +8 3 1 6 4 5 7 10 14 13 19 22 30 +7 3 1 6 4 5 10 14 13 19 22 30 +7 3 1 6 4 10 14 13 19 22 30 +7 3 1 6 4 10 14 13 19 22 20 30 +7 3 1 4 10 14 13 19 22 20 30 \ No newline at end of file diff --git "a/2017-1/zzskin/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/test 5.c" "b/2017-1/zzskin/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/test 5.c" new file mode 100644 index 00000000..08782596 --- /dev/null +++ "b/2017-1/zzskin/\344\272\214\345\217\211\346\240\221\347\232\204\345\273\272\347\253\213/test 5.c" @@ -0,0 +1,88 @@ +#include +#include + +typedef int ElemType; +typedef char TElemType ; + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild, *rchild; //左右孩子的指针 +}BiTNode, *BiTree; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; //枚举定义(返回状态) + + +Status CreateBiTree(BiTree *T,TElemType *p); +void PostOrderTraverseTree(BiTree T); + +int count = 0; //定义全局变量 用于对二叉树进行计数 + +Status CreateBiTree(BiTree *T,TElemType *p) // 创建二叉树 +{ + TElemType ch; + ch = p[count]; + count++; //定义数组存储字符串 + + if(ch == ' ') + { + return OK; + + } + else if(ch == '#') + { + *T = NULL; + } + else + { + if(!(*T = (BiTNode *)malloc(sizeof(BiTNode)))) + { + return OVERFLOW; + } + (*T)->data = ch; //根节点 + CreateBiTree(&(*T)->lchild,p); //构造左孩子 + CreateBiTree(&(*T)->rchild,p); //构造右孩子 + } + return OK; +} + +void PostOrderTraverseTree(BiTree T) +{ + if(T != NULL) + { + PostOrderTraverseTree(T->lchild); + PostOrderTraverseTree(T->rchild); + printf("%c",T->data); + } +} + +int main() +{ + BiTree T = NULL; + Status pop; + + TElemType str1[40] = "ABDG###EH##I#K##C#F##"; + TElemType str2[40] = "ABD#F###CE###"; + + printf("处理第一个二叉树 : ABDG###EH##I#K##C#F## \n"); + pop = CreateBiTree(&T,str1); + printf("后序遍历结果为 : "); + PostOrderTraverseTree(T); + + printf("\n"); + + count = 0; //处理第二个二叉树 + + printf("处理第二个二叉树 : ABD#F###CE### \n"); + pop = CreateBiTree(&T,str1); + printf("后序遍历结果为 : "); + PostOrderTraverseTree(T); + printf("\n"); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/zzskin/\345\223\210\345\270\214\350\241\250/test-10.c" "b/2017-1/zzskin/\345\223\210\345\270\214\350\241\250/test-10.c" new file mode 100644 index 00000000..eb8c5e20 --- /dev/null +++ "b/2017-1/zzskin/\345\223\210\345\270\214\350\241\250/test-10.c" @@ -0,0 +1,262 @@ +#include +#include +#include +#include + +#define SUCCESS 1 +#define UNSUCCESS 0 +#define DUPLICATE -1 + +typedef int KeyType; +typedef int ValueType; + +typedef struct _ElemType { + KeyType key; // 关键字 + ValueType val; // 值 +#ifdef CHAINED_HASH + struct _ElemType *next; +#endif +} ElemType; + +typedef struct +{ + ElemType *elem;//数据元素存储基址 动态分配数组 + int count;//当前数据元素个数 + int size_index;//hashsize[size_index]为当前容量 +}HashTable; + + +typedef enum { // 函数返回值 + OK, + ERROR +}Status; + +typedef enum { // 布尔返回值 + FALSE, + TRUE +}Bool; + +int InsertHash(HashTable *H, ElemType e) +{ + int c,p; + if(SearchHash(*H,e.key,&p,&c) == SUCCESS) + { + return DUPLICATE; + } + else + { + if(p == (*H).size_index || c > (*H).size_index / 2) + { + RecreateHashTable(H); + } + (*H).elem[p] = e; + ++(*H).count; + printf("冲突次数: %d\n",c); + printf("Insert:%d->%d\n",H->elem[p].key, H->elem[p].val); + printf("\n"); + return 0; + } +} + +Status CreateHash(HashTable *H,int size) +{ + ElemType temp; + int i; + + (*H).size_index = size; + (*H).count = 0; + (*H).elem = (ElemType *)malloc(sizeof(ElemType) * size); + + if(!((*H).elem)) + { + return ERROR; + } + for(i = 0; i < size; i++) + { + (*H).elem[i].key = -1; + (*H).elem[i].val = 0; + } + printf("---------------Hash表建立----------------\n"); + printf("HashTable 大小 : %d\n",(*H).size_index); + for(i = 0; H->count < size / 2; i++) + { + temp.key = rand() % 501; //产生随机数据 + temp.val = rand() % 501; + InsertHash(H, temp); + } + printf("建立完成!\n\n"); + return OK; +} + +Bool Empty(HashTable H) //判断哈希表是否为空 +{ + if(H.count == 0) + { + return TRUE; + } + else + { + return FALSE; + } +} + +Bool Prime(int n) //判断是否为素数 +{ + int i; + + if(n == 2) + { + return TRUE; + } + for(i = 2; i <= (int)sqrt((double)n); i++) + { + if(n % i == 0) + { + return FALSE; + } + } + return TRUE; +} + +Bool hash(KeyType a,KeyType b) +{ + if(a == b) + { + return TRUE; + } + else + { + return FALSE; + } +} + +int SearchHash(HashTable H, KeyType K, int *p, int *w) +{ + *p = K % H.size_index; + *w = 0; + while(H.elem[*p].key != -1 && !hash(K,H.elem[*p].key)) + { + if((*p) < H.size_index) + { + printf("冲突: %d->%d\n",H.elem[*p].key,H.elem[*p].val); + (*p)++; + (*w)++; + } + else + { + break; + } + } + if(hash(K,H.elem[*p].key)) + { + return SUCCESS; + } + else + { + return UNSUCCESS; + } +} + +Status RecreateHashTable(HashTable *H) +{ + int i,n; + HashTable temp; + ElemType e; + if(Empty(*H)) + { + return ERROR; + } + e.key = -1; + e.val = 0; + n = 2 * (*H).size_index; + while (!Prime(n)) + { + n++; + }; + temp.size_index = n; + temp.count = (*H).count; + temp.elem = (ElemType *)malloc(sizeof(ElemType)*n); + for(i = 0; i < n; i++) + { + temp.elem[i].key = -1; + temp.elem[i].val = 0; + } + for(i = 0; i < (*H).size_index; i++) + { + (*H).elem[i].key = temp.elem[i].key; + (*H).elem[i].val = temp.elem[i].val; + } + (*H).size_index = temp.size_index; + (*H).count = temp.count; + (*H).elem = (ElemType *)malloc(sizeof(ElemType)*n); + for(i = 0; i < n; i++) + { + (*H).elem[i].key = temp.elem[i].key; + (*H).elem[i].val = temp.elem[i].val; + } + free(temp.elem); + return OK; +} + +Status Print(HashTable H) +{ + int i; + if(Empty(H)) + { + return ERROR; + } + + printf("---------------打印哈希表---------------\n"); + for(i = 0; i < H.size_index; i++) + { + printf("[%d] : %d->%d\n", i, H.elem[i].key, H.elem[i].val); + } + printf("打印完成!\n"); + printf("\n"); + return OK; +} + +int main() +{ + int size = 0,i; + int p,c; + HashTable H; + ElemType e; + srand(time(NULL)); + while(Prime(size)) + { + size = rand() % 11 + 20; + } + CreateHash(&H,size); + Print(H); + + p = c = 0; + printf("-------------查找---------------\n"); + for(i = 0; i < H.size_index; i++) + { + e.key = rand() % 501; + printf("查找:%d\n",e.key); + if(SearchHash(H,e.key,&p,&c) == SUCCESS) + { + printf("查找到:%d\n",e.key); + printf("The Element Is : %d->%d\n", H.elem[p].key, H.elem[p].val); + printf("----------------------------------\n"); + } + else + { + printf("查找不存在:%d\n",e.key); + printf("----------------------------------\n"); + } + } + printf("查找结束!\n"); + + printf("-----------------------重新建立哈希表--------------------\n"); + if(!RecreateHashTable(&H)) + { + printf("\n"); + Print(H); + printf("重新建立结束!\n"); + + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/zzskin/\345\256\236\347\216\260\344\272\206\346\217\222\345\205\245\357\274\214\345\270\214\345\260\224\357\274\214\345\206\222\346\263\241\357\274\214\351\200\211\346\213\251\357\274\214\345\277\253\351\200\237\346\216\222\345\272\217/test--9.c" "b/2017-1/zzskin/\345\256\236\347\216\260\344\272\206\346\217\222\345\205\245\357\274\214\345\270\214\345\260\224\357\274\214\345\206\222\346\263\241\357\274\214\351\200\211\346\213\251\357\274\214\345\277\253\351\200\237\346\216\222\345\272\217/test--9.c" new file mode 100644 index 00000000..ba731fcb --- /dev/null +++ "b/2017-1/zzskin/\345\256\236\347\216\260\344\272\206\346\217\222\345\205\245\357\274\214\345\270\214\345\260\224\357\274\214\345\206\222\346\263\241\357\274\214\351\200\211\346\213\251\357\274\214\345\277\253\351\200\237\346\216\222\345\272\217/test--9.c" @@ -0,0 +1,240 @@ +#include +#include +#include + +#define MAXSIZE 10 //一个用作示例的小顺序表的最大长度 + +typedef struct +{ + int r[MAXSIZE + 1]; //r【0】闲置或用作哨兵单元 + int length; //顺序表长度 +}SqList; //顺序表类型 + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; //枚举定义(返回状态) + +typedef enum +{ + True, + False +}Bool; //定义布尔类型 + +Status copy(SqList *L,SqList *p) +{ + int i; + for(i = 0; i < MAXSIZE; i++) + { + L->r[i] = p->r[i]; + } + return OK; +} + +Status InsertSort(SqList* L,int* compare,int* move) +{ + int i,j; + for (i = 1; i < L->length ; i++) + { + if(L->r[i] < L->r[i - 1]) + { + L->r[0] = L->r[i]; + L->r[i] = L->r[i - 1]; + (*compare)++; + (*move)++; + for(j = i - 2; L->r[0] < L->r[j];--j) + { + L->r[j + 1] = L->r[j]; + (*move)++; + } + L->r[j + 1] = L->r[0]; + (*compare)++; + } + } + return OK; +} + +void BubbleSort(SqList *l,int* compare,int* move) +{ + int i, j; + int temp; + for (i = 0;i < l->length;i++) + { + for (j = i + 1;j < l->length;j++) + { + (*compare)++; + if (l->r[i] > l->r[j]) + { + temp = l->r[i]; + l->r[i] = l->r[j]; + l->r[j] = temp; + (*move)++; + } + } + } +} + +void ShellSort(SqList *l,int* compare,int* move) +{ + int gap = l->length / 2; //设置步长为list长度的一半 + for (;gap >= 1;gap = gap / 2) + { //步长每次减半,直至步长为一,进行直接插入排序 + int m; + for (m = 0; m < gap; m++) + { //找到每个子序列的起点,m,m+gap,m+gap+gap...则我们对这个序列进行直接插入排序 + int i; + int temp; + for (i = m + gap;i < l->length;i = i + gap) + { + int j = i; + while (j != m) + { + if (l->r[j] <= l->r[j - gap]) + { + (*compare)++; + temp = l->r[j - gap]; + l->r[j - gap] = l->r[j]; + l->r[j] = temp; + } + j = j - gap; //下标跳转到下一个元素 + (*move)++; + } + } + } + } +} + +Status output(SqList L) +{ + int i; + for(i = 0; i < MAXSIZE; i++) + { + printf("%d ",L.r[i]); + } + printf("\n"); + return OK; +} + +int _QSort(SqList *L,int low,int high,int *compare,int *move) +{ + int pivotkey = L->r[low]; + while(low < high) + { + while(low < high && L->r[high] >= pivotkey && (*compare)++) + { + high--; + } + L->r[low] = L->r[high]; + ++(*move); + while(low < high && L->r[low] <= pivotkey && ((*compare)++)) + { + low++; + } + L->r[high] = L->r[low]; + ++(*move); + } + L->r[low] = L->r[0]; + (*move)++; + return low; +} + +Status QSort(SqList *L,int low,int high,int *compare,int *move) +{ + int pivotlo; + if(low < high) + { + pivotlo = _QSort(L,low,high,compare,move); + QSort(L,low,pivotlo - 1,compare,move); + QSort(L,pivotlo + 1,high,compare,move); + } + return OK; +} + +int SelectMinKey(SqList* L,int i,int *compare,int *move) +{ + int k = i; + int j = i+1; + for(;j < L->length; ++j) + { + if(L->r[k] > L->r[j]) + { + (*compare)++; + k = j; + } + } + return k; +} + +Status SetectSort(SqList *L,int *compare,int *move) +{ + int i; + int j; + int temp; + for(i = 1; i < L->length; i++) + { + j = SelectMinKey(L,i,compare,move); + if(i != j) + { + temp = L->r[i]; + L->r[i] = L->r[j]; + L->r[j] = temp; + (*move)++; + } + } + return OK; +} + +int main() +{ + int compare = 0,move = 0; + int i; + SqList a,b; + + srand(time(NULL)); + a.length = b.length = 0; + for(i = 0; i < MAXSIZE; i++) + { + a.r[i] = rand() % 100 + 1; + b.r[i] = a.r[i]; + a.length++; + b.length++; + } + + printf("测试数组为:\n"); + output(a); + printf("\n"); + + printf("经过直接排序后的数组为:"); + InsertSort(&b,&compare,&move); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",compare,move,compare+move); + + copy(&a,&b); + printf("经过希尔排序后的数组为:"); + ShellSort(&b,&compare,&move); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",compare,move,compare+move); + + copy(&a,&b); + printf("经过气泡排序后的数组为:"); + BubbleSort(&b,&compare,&move); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",compare,move,compare+move); + + copy(&a,&b); + printf("经过简单排序后的数组为:"); + SetectSort(&b,&compare,&move); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",compare,move,compare+move); + + copy(&a,&b); + printf("经过快速排序后的数组为:"); + QSort(&b,0,9,&compare,&move); + output(b); + printf("总比较次数:%d\n总排序次数:%d\n两者之和:%d\n\n",compare,move,compare+move); + + + return 0; +} \ No newline at end of file diff --git "a/2017-1/zzskin/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\347\232\204\344\273\273\346\204\217\344\270\244\347\202\271\344\271\213\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204/sy-7.c" "b/2017-1/zzskin/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\347\232\204\344\273\273\346\204\217\344\270\244\347\202\271\344\271\213\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204/sy-7.c" new file mode 100644 index 00000000..44023cec --- /dev/null +++ "b/2017-1/zzskin/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\347\232\204\344\273\273\346\204\217\344\270\244\347\202\271\344\271\213\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204/sy-7.c" @@ -0,0 +1,260 @@ +#include +#include + +//-----图的数组存储表示--------// +#define MAX_VERTEX_NUM 20 //最大顶点个数 +#define INFINITY -1 + +typedef int VRType; +typedef int ElemType; +typedef int InfoType; +typedef int VertexType; +typedef int GraphKind; + +int visited[MAX_VERTEX_NUM ]; //访问标志数组,初始值为FALSE(0),一旦某个顶点被访问,则令其值为TRUE(1). + +typedef struct ArcCell +{ // 弧的定义 + VRType adj; // VRType是顶点关系类型。 + // 对无权图,用1或0表示相邻否; + // 对带权图,则为权值类型。 + InfoType *info; // 该弧相关信息的指针 +} ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; + +typedef struct +{ // 图的定义 + VertexType vexs[MAX_VERTEX_NUM]; // 顶点信息 + AdjMatrix arcs; // 弧的信息 + int vexnum, arcnum; // 顶点数,弧数 + GraphKind kind; // 图的种类标志 +} 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 InitQueue (LinkQueue *Q); +Bool QueueEmpty(LinkQueue *Q) ; +Status EnQueue(LinkQueue *Q,int e); +Status DeQueue(LinkQueue *Q, int *e); +Status Add(Graph *G, int x, int y) ; +Status CreateGraph(Graph *G) ; +int FirstAdjVex(Graph G, int i) ; +int NextAdjVex(Graph G, int i, int j) ; +Status PrintFoot(LinkQueue Q,int start) ; +void ShortestPath(Graph G,int a,int b) ; + +//---------------------队列基本操作-----------------\\ +//初始化队列 +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; //没有free(e) + return OK; +} +//----------------图的基本操作----------------------- +//顶点赋值 +Status Add(Graph *G, int x, int y) +{ + if(x >= MAX_VERTEX_NUM || y >= MAX_VERTEX_NUM) //若x、y的值超过了最大限定值是错误的 + { + + return ERROR; + } + G -> arcs[x][y].adj = G ->arcs[y][x].adj = 1; //无向图的邻接矩阵是对称的,为无向图顶点赋值,赋值为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; //初始化邻接矩阵,让每一个值都为无穷 + G->arcs[i][j].info = NULL; + } + } + 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; +} + +Status Print(LinkQueue Q,int start) +{ + int njr[MAX_VERTEX_NUM];//MAX_VERTEX_NUM可以在这一题中可以换成9,但为了函数的通用性,用MAX_VERTEX_NUM更好 + int i; //njr数组来存储路径 + QueuePtr p; + p = Q.rear;//p是队尾结点 + for(i=0;i < MAX_VERTEX_NUM; i++) + { + njr[i] = -1;//初始化foot数组 + } + njr[0] = p->data;//路径的最后一个 + p = p->prious; + for(i = 1;p->data != start; i++) + { + njr[i] = p->data; + p = p->prious; + } + njr[i] = start;//foot[i] = p->data; + for(;i >= 0; i--) + { + if(njr[i] >= 0) + printf("%d ",njr[i] + 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);//把最后一个结点进入队列 + Print(Q,a);//可以输出路径了 + flag = True; + } + if(!visited[w])//若u的邻接点没有被访问 + { + + EnQueue(&Q,w);//让w进入队列 + visited[w] = True; + } + } + if(flag) + { + break;//跳出while循环 + } + } +} +//输出路径 + +int main() +{ + int i,j; + Graph h ; + CreateGraph(&h);//构建一个无向图,并用邻接矩阵初始化图 + printf("图中任意两点间的最短距离为:\n"); + 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; +} \ No newline at end of file diff --git "a/2017-1/zzskin/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\347\232\204\344\273\273\346\204\217\344\270\244\347\202\271\344\271\213\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204/xg-7.c" "b/2017-1/zzskin/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\347\232\204\344\273\273\346\204\217\344\270\244\347\202\271\344\271\213\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204/xg-7.c" new file mode 100644 index 00000000..b6322834 --- /dev/null +++ "b/2017-1/zzskin/\345\271\277\345\272\246\344\274\230\345\205\210\346\261\202\345\233\276\347\232\204\344\273\273\346\204\217\344\270\244\347\202\271\344\271\213\351\227\264\346\234\200\347\237\255\350\267\257\345\276\204/xg-7.c" @@ -0,0 +1,266 @@ +#include +#include + +#define MAXQSIZE 100 +#define MAX_VERTEX_NUM 20 //最大顶点个数 +#define INFINITY -1 + +typedef int VRType; +typedef int ElemType; +typedef int InfoType; +typedef int VertexType; +typedef int GraphKind; + +int visited[MAX_VERTEX_NUM ]; //访问标志数组,初始值为FALSE(0),一旦某个顶点被访问,则令其值为TRUE(1). + +//------------图-------------// +typedef struct ArcCell +{ // 弧的定义 + VRType adj; // VRType是顶点关系类型。 + // 对无权图,用1或0表示相邻否; + // 对带权图,则为权值类型。 + InfoType *info; // 该弧相关信息的指针 +} ArcCell, AdjMatrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]; + +typedef struct +{ // 图的定义 + VertexType vexs[MAX_VERTEX_NUM]; // 顶点信息 + AdjMatrix arcs; // 弧的信息 + int vexnum, arcnum; // 顶点数,弧数 + GraphKind kind; // 图的种类标志 +} Graph; + +//-------------队列------------ // +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 InitQueue (LinkQueue *Q); +Bool QueueEmpty(LinkQueue *Q) ; +Status EnQueue(LinkQueue *Q,int e); +Status DeQueue(LinkQueue *Q, int *e); +Status Add(Graph *G, int x, int y) ; +Status CreateGraph(Graph *G) ; +int FirstAdjVex(Graph G, int i) ; +int NextAdjVex(Graph G, int i, int j) ; +Status PrintFoot(LinkQueue Q,int start) ; +void ShortestPath(Graph G,int a,int b) ; + +//---------------------队列基本操作-----------------\\ +//初始化队列 +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; //没有free(e) + return OK; +} +//----------------图的基本操作----------------------- +//顶点赋值 +Status Add(Graph *G, int x, int y) +{ + if(x >= MAX_VERTEX_NUM || y >= MAX_VERTEX_NUM) //若x、y的值超过了最大限定值是错误的 + { + + return ERROR; + } + G -> arcs[x][y].adj = G ->arcs[y][x].adj = 1; //无向图的邻接矩阵是对称的,为无向图顶点赋值,赋值为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; //初始化邻接矩阵,让每一个值都为无穷 + G->arcs[i][j].info = NULL; + } + } + 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; +} + +Status Print(LinkQueue Q,int start) +{ + int njr[MAX_VERTEX_NUM];//MAX_VERTEX_NUM可以在这一题中可以换成9,但为了函数的通用性,用MAX_VERTEX_NUM更好 + int i; //njr数组来存储路径 + QueuePtr p; + p = Q.rear;//p是队尾结点 + for(i=0;i < MAX_VERTEX_NUM; i++) + { + njr[i] = -1;//初始化foot数组 + } + njr[0] = p->data;//路径的最后一个 + p = p->prious; + for(i = 1;p->data != start; i++) + { + njr[i] = p->data; + p = p->prious; + } + njr[i] = start;//foot[i] = p->data; + for(;i >= 0; i--) + { + if(njr[i] >= 0) + printf("%d ",njr[i] + 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);//把最后一个结点进入队列 + Print(Q,a);//可以输出路径了 + flag = True; + } + if(!visited[w])//若u的邻接点没有被访问 + { + + EnQueue(&Q,w);//让w进入队列 + visited[w] = True; + } + } + if(flag) + { + break;//跳出while循环 + } + } +} +//输出路径 + +int main() +{ + int i,j; + Graph h ; + Status pop = 0; + CreateGraph(&h);//构建一个无向图,并用邻接矩阵初始化图 + if(pop == CreateGraph(&h)) + { + printf("Right!\n"); + printf(">---------------------------------------------<\n"); + } + printf("图中任意两点间的最短距离为:\n"); + 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; +} \ No newline at end of file diff --git "a/2017-1/zzskin/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/test 6.c" "b/2017-1/zzskin/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/test 6.c" new file mode 100644 index 00000000..6d388fef --- /dev/null +++ "b/2017-1/zzskin/\346\261\202\344\272\214\345\217\211\346\240\221\346\267\261\345\272\246\343\200\201\346\234\200\345\244\247\345\256\275\345\272\246\343\200\201\345\217\266\345\255\220\347\273\223\347\202\271\345\222\214\351\235\236\345\217\266\345\255\220\347\273\223\347\202\271/test 6.c" @@ -0,0 +1,168 @@ +#include +#include + +typedef int ElemType; +typedef char TElemType ; + +typedef struct BiTNode +{ + TElemType data; + struct BiTNode *lchild, *rchild; //左右孩子的指针 +}BiTNode, *BiTree; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; //枚举定义(返回状态) + + +Status CreateBiTree(BiTree *T,TElemType *p); +void PostOrderTraverseTree(BiTree T); +int BiTreeDepth(BiTree T); +int BiTreeWidth(BiTree T); +int BiTreeCount(BiTree T); +int BiTreeCountLeaf(BiTree T); + + +int count = 0; //定义全局变量 用于对二叉树进行计数 + +Status CreateBiTree(BiTree *T,TElemType *p) // 创建二叉树 +{ + TElemType ch; + ch = p[count]; + count++; //定义数组存储字符串 + + if(ch == ' ') + { + return OK; + + } + else if(ch == '#') + { + *T = NULL; + } + else + { + if(!(*T = (BiTNode *)malloc(sizeof(BiTNode)))) + { + return OVERFLOW; + } + (*T)->data = ch; //根节点 + CreateBiTree(&(*T)->lchild,p); //构造左孩子 + CreateBiTree(&(*T)->rchild,p); //构造右孩子 + } + return OK; +} + +void PostOrderTraverseTree(BiTree T) +{ + if(T != NULL) + { + PostOrderTraverseTree(T->lchild); + PostOrderTraverseTree(T->rchild); + printf("%c",T->data); + } +} + +int BiTreeDepth(BiTree T) +{ + int depthleft,depthright; + if(T == NULL) + { + return 0; + } + else + { + depthleft = BiTreeDepth(T->lchild); + depthright = BiTreeDepth(T->rchild); + if(depthleft > depthright) + { + return (depthleft + 1); + } + else + { + return (depthright + 1); + } + } +} + +int pot[100]; //存放各层结点数 +int max = 0; //最大宽度 + +int BiTreeWidth(BiTree T) //递归求最大宽度 +{ + + if(T == NULL) + { + return 0; + } + count++; + pot[count]++; + if(max < pot[count]) + { + max = pot[count]; + } + BiTreeWidth(T->lchild); + BiTreeWidth(T->rchild); + count--; + return max; +} + +int BiTreeCount(BiTree T)//返回指针T所指二叉树中所有结点个数 +{ + if(T == NULL) + { + return 0; + } + else if((T->lchild == NULL) && (T->rchild == NULL)) + { + return 1; + } + else + { + return BiTreeCount(T->lchild) + BiTreeCount(T->rchild) + 1; + } +} + +int BiTreeCountLeaf(BiTree T)// 返回指针T所指二叉树中所有叶子结点个数 +{ + if(T == NULL) + { + return 0; + } + else if((T->lchild == NULL) && (T->rchild == NULL)) + { + return 1; + } + else + { + return BiTreeCountLeaf(T->lchild) + BiTreeCountLeaf(T->rchild); + } +} + + +int main() +{ + BiTree T = NULL; + + TElemType str1[40] = "ABDG###EH##I#K##C#F##"; + TElemType str2[40] = "ABD#F###CE###"; + + printf("处理第一个二叉树 : ABDG###EH##I#K##C#F## \n"); + CreateBiTree(&T,str1); + printf("此二叉树高度为%d\n最大宽度为%d\n",BiTreeDepth(T),BiTreeWidth(T)); + printf("此二叉树结点为%d\n叶子结点为%d",BiTreeCount(T),BiTreeCountLeaf(T)); + printf("\n"); + + count = 0; //处理第二个二叉树 + + printf("处理第二个二叉树 : ABD#F###CE### \n"); + CreateBiTree(&T,str2); + printf("此二叉树高度为%d\n最大宽度为%d\n",BiTreeDepth(T),BiTreeWidth(T)); + printf("此二叉树结点为%d\n叶子结点为%d",BiTreeCount(T),BiTreeCountLeaf(T)); + printf("\n"); + + return 0; +} \ No newline at end of file diff --git "a/2017-1/zzskin/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-5.c" "b/2017-1/zzskin/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-5.c" new file mode 100644 index 00000000..4c2c14b3 --- /dev/null +++ "b/2017-1/zzskin/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/3-5.c" @@ -0,0 +1,258 @@ +#include +#include + +#define STACK_INIT_SIZE 100 //存储空间初始分配量 +#define STACK_SIZE 100 //存储空间分配增量 +#define STACKINCREMENT 10 //存储空间分配增量 + +struct //设定运算符优先级 +{ + char ch; //运算符 + int pri; //优先级 +} +lpri[]= {{'=',0},{'(',1},{'*',5},{'/',5},{'+',3},{'-',3},{')',6}}, +rpri[]= {{'=',0},{'(',6},{'*',4},{'/',4},{'+',2},{'-',2},{')',1}}; + +typedef char SElemType ; + +typedef enum +{ + OK, + ERROR, + OVERFLOW +}Status; //枚举定义(返回状态) + +typedef enum{ + False, + True +}Bool; //定义布尔类型 + +typedef struct _SqStack{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; + +Status InitStack(SqStack *S) //构造顺序栈 +{ + S->base = (SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if(! S->base) + { + return OVERFLOW; + } //存储分配失败 + S->top = S->base; //初始化 + S->stacksize = STACK_INIT_SIZE; + return OK; +} + + +Bool IsStackEmpty(SqStack *S) +{ + if(S->top == S->base) + { + return True; + } + else + { + return False; + } +} + +SElemType GetTop(SqStack *S,SElemType *e) +{ + if(IsStackEmpty(S)) + { + return ERROR; + } + *e = *--S->top; + return OK; +} + +Status Push(SqStack *S,SElemType e) +{ + if(S->top - S->base >= S->stacksize) + { + S->base = (SElemType *)realloc(S->base,(S->stacksize + STACKINCREMENT) * sizeof(SElemType)); + if(!S->base) + { + return OVERFLOW; + } + S->top = S->base + S->stacksize; + S->stacksize += STACKINCREMENT; + } + *S->top++ = e; + return OK; +} + + //删除 S 的栈顶元素,并用 e 返回其值 +Status Pop(SqStack *S,SElemType *e) +{ + if(IsStackEmpty(S)) + { + return OK; + } + *e = *--S->top; + return ERROR; +} + + +int leftpri(char op) +{ + int i; + for (i=0; i='0' && *exp<='9') //判定为数字 + { + postexp[i++]=*exp; + exp++; + } + postexp[i++]=' '; //用#标识一个数值串结束 + } + else //为运算符的情况 + switch(Precede(op.data[op.top],*exp)) + { + case -1: //栈顶运算符的优先级低:进栈 + op.top++; + op.data[op.top]=*exp; + exp++; //继续扫描其他字符 + break; + case 0: //只有括号满足这种情况 + op.top--; //将(退栈 + exp++; //继续扫描其他字符 + break; + case 1: //退栈并输出到postexp中 + postexp[i++]=op.data[op.top]; + op.top--; + break; + } + } //while (*exp!='\0') + while (op.data[op.top]!='=') + //此时exp扫描完毕,退栈到'='为止 + { + postexp[i++]=op.data[op.top]; + op.top--; + } + postexp[i]='\0'; //给postexp表达式添加结束标识 +} + +float compvalue(char exp[]) //计算后缀表达式的值(有参考) +{ + struct + { + float data[STACK_SIZE]; //存放数值 + int top; //栈指针 + } st; //定义数值栈 + float d; + char ch; + int t=0; //t作为exp的下标 + st.top=-1; + ch=exp[t]; + t++; + while (ch!='\0') //exp字符串未扫描完时循环 + { + switch (ch) + { + case'+': + st.data[st.top-1]=st.data[st.top-1]+st.data[st.top]; + st.top--; + break; + case '-': + st.data[st.top-1]=st.data[st.top-1]-st.data[st.top]; + st.top--; + break; + case '*': + st.data[st.top-1]=st.data[st.top-1]*st.data[st.top]; + st.top--; + break; + case '/': + if (st.data[st.top]!=0) + st.data[st.top-1]=st.data[st.top-1]/st.data[st.top]; + else + { + printf("\n\t除零错误!\n"); //异常退出 + } + st.top--; + break; + default: + d=0; //将数字字符转换成数值存放到d中 + while (ch>='0' && ch<='9') //为数字字符 + { + d=10*d+ch-'0'; + ch=exp[t]; + t++; + } + st.top++; + st.data[st.top]=d; + } + ch=exp[t]; + t++; + } + return st.data[st.top]; +} + +int main() +{ + int i = 0; + char exp[] = "(56-20)/(4+2)" ; + char postexp[STACK_SIZE]; + trans(exp,postexp); + printf("中缀表达式:%s\n",exp); + printf("后缀表达式:%s\n",postexp); + printf("表达式的值:%g\n",compvalue(postexp)); + return 0; +} \ No newline at end of file diff --git "a/2017-1/zzskin/\351\223\276\350\241\250\347\232\204\346\213\206\345\210\206/test-4.c" "b/2017-1/zzskin/\351\223\276\350\241\250\347\232\204\346\213\206\345\210\206/test-4.c" new file mode 100644 index 00000000..675362ce --- /dev/null +++ "b/2017-1/zzskin/\351\223\276\350\241\250\347\232\204\346\213\206\345\210\206/test-4.c" @@ -0,0 +1,126 @@ +#include +#include +#include + +typedef struct LNode +{ + int data; + struct LNode *next; +}LNode, *LinkList; //结构体的建立 + +LinkList CreateList(LinkList *L,int n); +void List_L1(LinkList *L); +void List_L2(LinkList *L); +void SeperateList(LinkList list, LinkList *list1, LinkList *list2); + +LinkList CreateList(LinkList *L,int n) //创建链表 +{ + //逆位序输入n个元素的值,建立带表头结点的单链线性表 + LNode *temp = *L; + int i; + (*L)->data = 0; + (*L)->next = NULL; //先建立一个带头结点的单链表 + srand(time(NULL)); + printf("产生随机数据:\n"); + for (i = 0; i < n ; i++) + { + temp = (LNode*)malloc(sizeof(LNode)); //分配空间 + temp->data = rand() % 50+1; //随机生成数据 + temp->next = (*L)->next; //插入到表头 + (*L)->next = temp; + } +}; + +void List_L1(LinkList *L) //遍历输出单链表 +{ + LNode *p = (*L)->next; + while (p) + { + printf("%d ", p->data); //输出随机的链表元素 + p = p->next; + } +} + +void List_L2(LinkList *L) //遍历输出循环链表 +{ + LNode *p = (*L)->next; + int count = (*L)->data; + p = (*L)->next; + while (count) + { + printf("%d ", p->data); //输出拆分后的链表元素 + p = p->next; + count--; + if (p == (*L)->next) //终止条件 + { + break; + } + } +} + +void SeperateList(LinkList list,LinkList *list1, LinkList *list2) //拆分原线性链表为两个循环链表 +{ + LNode*p, *p1, *p2; //用于遍历list,list1,list2的三个指向节点的指针 + LNode*rear1, *rear2; //尾指针 + int count = 0; + (*list1)->data = 0; + (*list1)->next = NULL; + rear1 = p1 = *list1; + (*list2)->data = 0; + (*list2)->next = NULL; + rear2 = p2 = *list2; + for (p = list->next; p != NULL; p = p->next) + { + count++; + if (count % 2) //控制奇数项 + { + rear1->next = p; + rear1 = p; + (*list1)->data++; //长度增加1 + printf("\n当前为:%d,奇数项,分配给list1 ", rear1->data); //分配给list1和list2 + } + else + { + rear2->next = p; + rear2 = p; + (*list2)->data++; //长度增加1 + printf("\n当前为:%d,偶数项,分配给list2 ", rear2->data); //分配给list1和list2 + } + } + rear1->next = p1->next; + rear2->next = p2->next; + free(list); //释放头指针 +} + +int main() +{ + int n; + LinkList L, La, Lb; + srand(time(NULL)); + n = rand() % 20 + 1 ; + L = (LinkList)malloc(sizeof(LNode)); + printf("输入待拆分L数据:\n"); + CreateList(&L, n); //创建L + La = (LNode*)malloc(sizeof(LNode)); + Lb = (LNode*)malloc(sizeof(LNode)); //分配存储空间 + printf("L:"); //输出L + List_L1(&L); + printf("\n"); + SeperateList(L, &La, &Lb); + printf("\n\n将L拆分成两个循环链表\n\n"); //La,Lb为循环链表,只输出一次 + printf("序号为奇数的元素放入La\n"); + printf("La元素个数:%d\n", La->data); + printf("La:"); //输出La + List_L2(&La); + printf("\n"); + printf("序号为偶数的元素放入Lb\n"); + printf("Lb元素个数:%d\n", Lb->data); + printf("Lb:"); //输出Lb + List_L2(&Lb); + return 0; +} + +//用尾指针rear来表示单循环链表 +//则查找开始结点和终端结点都容易实现 +//为了节省时间和空间,无需开辟新空间,只需在原来的链表上进行操作 +//查找时间都是O(1)。故所用时间最少,空间最少 \ No newline at end of file diff --git "a/2017-1/zzskin/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/3-4-3.c" "b/2017-1/zzskin/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/3-4-3.c" new file mode 100644 index 00000000..6d864570 --- /dev/null +++ "b/2017-1/zzskin/\351\230\237\345\210\227\347\232\204\345\237\272\346\234\254\346\223\215\344\275\234/3-4-3.c" @@ -0,0 +1,103 @@ +锘#include +#include + +#define OK 1 +#define OVERFLOW -1 +#define ERROR 0 + +typedef int ElemType; +typedef int Status; + +typedef struct QNode +{ + ElemType data; + struct QNode *next; +}QNode,*QueuePtr; + +typedef struct +{ + QueuePtr front;//闃熷ご鎸囬拡 + QueuePtr rear;//闃熷熬鎸囬拡 +}LinkQueue; + +//闃熷垪閾惧紡琛ㄧず +//闃熷垪鍒濆鍖 +Status InitQueue(LinkQueue *Q) +{ + Q->front = Q->rear = (QueuePtr)malloc(sizeof(QNode)); + if(!Q->front) + { + return OVERFLOW; + } + Q->front->next = NULL; + Q->rear->next = NULL; + return OK; +} +//鍒犻櫎鍏冪礌 +Status DeleteQueue(LinkQueue *Q, ElemType *e) +{ + QueuePtr p; + if (Q->front == Q->rear) + { + return ERROR;//鍒ゆ柇鏄惁鏄┖闃熷垪 + } + p = Q->front->next; //灏嗚鍒犻櫎鐨勫澶寸粨鐐规殏瀛樼粰p + (*e) = p->data;//淇濆瓨灏嗚鍒犻櫎缁撶偣鐨勫 + Q->front->next = p->next;//灏嗗厓闃熷垪澶村悗缁->bext璧嬬粰澶寸粨鐐瑰悗缁 + if (Q->rear == p) + { + Q->rear = Q->front;//鍒犻櫎鍏冪礌涔嬪墠锛岄槦鍒椾腑鍙湁涓涓厓绱犮 + } + free(p); + return OK; +} +//鎻掑叆鍏冪礌鍒伴槦鍒椾腑 +Status InsertQueue(LinkQueue *Q, ElemType e) +{ + QueuePtr p=(QueuePtr)malloc(sizeof(QNode)); + if (!p) + { + return OVERFLOW; + } + p->data = e; + p->next = NULL; + Q->rear->next = p;//灏唒鎻掑叆灏炬寚閽堟墍鎸囧悜鐨勯槦灏剧粨鐐瑰悗闈 + Q->rear = p;//灏炬寚閽堟寚鍚戞柊鎻掑叆鐨勭粨鐐 + return OK; +} +//閬嶅巻闃熷垪涓殑鍏冪礌 +Status VisitQueue(LinkQueue Q) +{ + QueuePtr p; + if (Q.front == Q.rear)//濡傛灉鏄┖闃熷垪.. + { + printf("绌洪槦鍒梊n") ; + return ERROR; + } + p = Q.front->next;//p鎸囧悜闃熷ご缁撶偣 + while (p)//p涓嶄负绌烘椂 + { + printf("%d ", p->data);//杈撳嚭p鎸囧悜鐨勭粨鐐圭殑鍊 + p = p->next;//鎸囬拡鍚庣Щ + } + printf("\n"); + return OK; +} + +int main() +{ + ElemType e; + LinkQueue Q; + InitQueue(&Q); + InsertQueue(&Q, 1); + InsertQueue(&Q, 2); + InsertQueue(&Q, 3); + InsertQueue(&Q, 4); + InsertQueue(&Q, 5); + VisitQueue(Q); + DeleteQueue(&Q, &e); + InsertQueue(&Q, 6); + VisitQueue(Q); + getchar(); + return 0; +} \ No newline at end of file diff --git "a/2017-1/\347\256\227\346\263\2252.12/\351\223\276\350\241\250\345\220\210\345\271\266.cpp" "b/2017-1/\347\256\227\346\263\2252.12/\351\223\276\350\241\250\345\220\210\345\271\266.cpp" new file mode 100644 index 00000000..c80e7ce4 --- /dev/null +++ "b/2017-1/\347\256\227\346\263\2252.12/\351\223\276\350\241\250\345\220\210\345\271\266.cpp" @@ -0,0 +1,179 @@ +//#include +//#include +//#include +//#define MAXSIZE 1000 +// +//typedef struct LNode{ +// int data; +// struct LNode *next; +//}LNode,*LinkList; +//LNode *Create() //返回值: 链表的首指针 +//{ +// +//} +//LinkList MergeList_L(LinkList La,LinkList Lb)//合并 +//{ +// LinkList pa,pb,pc,Lc; +// Lc=(LinkList)malloc(sizeof(LNode)); +// Lc-> data=La-> data+Lb-> data; +// pa=La-> next; +// pb=Lb-> next; +// Lc=pc=La; +// while(pa&&pb) +// { +// if(pa-> data <=pb-> data) +// { +// pc-> next=pa;pc=pa;pa=pa->next; +// } +// else +// {pc-> next=pb;pc=pb;pb=pb-> next;} +// } +// pc-> next=pa?pa:pb; +// free(Lb); +// return Lc; +//} +//int PrintLinkList(LinkList L) //打印链表 +//{ +// LinkList q; +// q=L; +// if(q!=NULL) +// { +// q=q-> next; +// do +// { +// printf( "%d ",q-> data); +// q=q-> next; +// } +// while(q!=NULL); +// printf( "\n "); +// } +// return 0; +//} +//int main() +//{ +// LinkList L1=Create(); +// LinkList L2=Create(); +// LinkList L3; +// L3=MergeList_L(L1,L2); +// PrintLinkList(L3); +// +// return 0; +// +// +//} + +#include +#include +#include + +#include + +typedef struct LNode{ + + int data; + + struct LNode *next; + +}LNode,*LinkList; + +void CreateList(LinkList &L,int n) + +{ + + LinkList p; + + L=(LinkList)malloc(sizeof(LNode)); + + L->next=NULL;//先初始为空 + + printf("请按顺序输入链表的值\n"); + + srand((unsigned) time(NULL)); + + for(int i=n;i>0;--i) + + { + + p=(LinkList)malloc(sizeof(LNode)); + + p->data=rand()%101; + + printf("%d ",&p->data); + + p->next=L->next; + + L->next=p; + + } + +} + +void PrintList(LinkList L) + +{ + + LinkList p; + + p=L->next; + + while(p) + + { + + printf("%d ",p->data); + + p=p->next; + + } + + printf("\n"); + +} + +LinkList MergeList_L(LinkList La,LinkList Lb)//合并 +{ + LinkList pa,pb,pc,Lc; + Lc=(LinkList)malloc(sizeof(LNode)); + Lc-> data=La-> data+Lb-> data; + pa=La-> next; + pb=Lb-> next; + Lc=pc=La; + while(pa&&pb) + { + if(pa-> data <=pb-> data) + { + pc-> next=pa;pc=pa;pa=pa->next; + } + else + {pc-> next=pb;pc=pb;pb=pb-> next;} + } + pc-> next=pa?pa:pb; + free(Lb); + return Lc; +} +void main() + +{ + + LinkList La,Lb,Lc; + int length1; + int length2; + printf("链表La的长度为:"); + scanf("%d",&length1); + + printf("链表La的输入为:"); + CreateList(La,length1); + + printf("链表Lb的长度为:"); + scanf("%d",&length2); + + printf("链表Lb的输入为:"); + CreateList(Lb,length2); + + Lc=MergeList_L(La,Lb); + + printf("链表Lc的合并输出为:"); + + PrintList(Lc); + +} \ No newline at end of file diff --git "a/2017-1/\347\256\227\346\263\2252.12/\351\223\276\350\241\250\345\220\210\345\271\2662.0.cpp" "b/2017-1/\347\256\227\346\263\2252.12/\351\223\276\350\241\250\345\220\210\345\271\2662.0.cpp" new file mode 100644 index 00000000..74d8a79f --- /dev/null +++ "b/2017-1/\347\256\227\346\263\2252.12/\351\223\276\350\241\250\345\220\210\345\271\2662.0.cpp" @@ -0,0 +1,123 @@ +#include +#include +#include +typedef struct LNode{ + int data; + struct LNode *next; +}LNode,*LinkList; +LinkList Insert(LinkList head, LinkList p);//将p指向的结点插入链表, 结果链表保持有序,返回值是新链表的首指针。 +LinkList CreateList1();//创建一条新的有序链表 一共包含十个数据 +LinkList CreateList2(); +void PrintList(LinkList head);//打印链表 +LinkList Merge(LinkList pHead1, LinkList pHead2);//合并链表 + +LinkList CreateList1() +{ + LinkList p1,head=0; + int a,i=0; + printf("产生第一条排序链表,共包含10个数: \n"); + srand(time(NULL)); + while(i<10) + { + a=rand()%100+1; + p1=(LinkList)malloc(sizeof(LNode)); + p1->data = a; + head=Insert(head,p1); + i++; + } + return(head); +} +LinkList CreateList2() +{ + LinkList p1, head=0; + int a,i=0; + printf("产生第二条排序链表,共包含10个数: \n"); + srand(time(NULL)); + while(i<10) + { + a=rand()%80; + p1=(LinkList)malloc(sizeof(LNode)); + p1->data = a; + head=Insert(head,p1); + i++; + } + return(head); +} +LinkList Insert(LinkList head, LinkList p) // +{ + LinkList p1, p2; + if(head == NULL) // 原链表为空链表,对应情况① + { head = p; + p->next = NULL; + return(head); + } + p1 = head; + while( (p->data) > (p1->data) && p1->next != NULL ) + { // 寻找待插入位置 + p2 = p1; p1 = p1->next; + // p2指向的结点在p1指向的结点之前 + } + if( (p->data) <= (p1->data) ) // 插在p1之前 + { + p->next = p1; + if(head == p1) + head = p; // 插在链表首部,对应情况② + else + p2->next = p; // 插在链表中间,对应情况③ + } + else // 插在链表尾结点之后,对应情况④ + { + p1->next = p; + p->next = NULL; + } + return(head); +} +void PrintList(LinkList x) +{ + while(x!=NULL) + { + printf("%d ——>",x->data); + x=x->next; + } + printf("NULL \n"); +} +LinkList Merge(LinkList pHead1, LinkList pHead2) +{ + if(pHead1 == NULL)//判断二者中是否有空表的情况 + return pHead2; + else if(pHead2 == NULL) + return pHead1; + + LinkList pMergedHead=NULL;//合并后链表的首节点 + + + if(pHead1->data < pHead2->data) + { + pMergedHead = pHead1; + pMergedHead->next = Merge(pHead1->next, pHead2); + } + else + { + pMergedHead = pHead2; + pMergedHead->next = Merge(pHead1, pHead2->next); + } + + return pMergedHead; +} + +int main() +{ + LinkList x,y,z; + x=CreateList1(); + PrintList(x); + + y=CreateList2(); + PrintList(y); + + printf("\n合并后的链表: \n"); + z=Merge(x,y); + PrintList(z); + + return 0; + +} diff --git "a/2017-1/\347\256\227\346\263\2252.12\347\254\254\344\272\214\346\254\241\346\217\220\344\272\244/\350\277\220\350\241\214\346\210\252\345\233\276.PNG" "b/2017-1/\347\256\227\346\263\2252.12\347\254\254\344\272\214\346\254\241\346\217\220\344\272\244/\350\277\220\350\241\214\346\210\252\345\233\276.PNG" new file mode 100644 index 00000000..64e47b45 Binary files /dev/null and "b/2017-1/\347\256\227\346\263\2252.12\347\254\254\344\272\214\346\254\241\346\217\220\344\272\244/\350\277\220\350\241\214\346\210\252\345\233\276.PNG" differ diff --git "a/2017-1/\347\256\227\346\263\2252.12\347\254\254\344\272\214\346\254\241\346\217\220\344\272\244/\351\223\276\350\241\250\345\220\210\345\271\2662.0.cpp" "b/2017-1/\347\256\227\346\263\2252.12\347\254\254\344\272\214\346\254\241\346\217\220\344\272\244/\351\223\276\350\241\250\345\220\210\345\271\2662.0.cpp" new file mode 100644 index 00000000..41e59940 --- /dev/null +++ "b/2017-1/\347\256\227\346\263\2252.12\347\254\254\344\272\214\346\254\241\346\217\220\344\272\244/\351\223\276\350\241\250\345\220\210\345\271\2662.0.cpp" @@ -0,0 +1,123 @@ +#include +#include +#include +typedef struct LNode{ + int data; + struct LNode *next; +}LNode,*LinkList; +LinkList Insert(LinkList head, LinkList p);//将p指向的结点插入链表, 结果链表保持有序,返回值是新链表的首指针。 +LinkList CreateList1();//创建一条新的有序链表 一共包含十个数据 +LinkList CreateList2(); +void PrintList(LinkList head);//打印链表 +LinkList Merge(LinkList pHead1, LinkList pHead2);//合并链表 + +LinkList CreateList1() +{ + LinkList p1,head=0; + int a,i=0; + printf("产生第一条排序链表,共包含10个数: \n"); + srand(time(NULL)); + while(i<10) + { + a=rand()%100+1; + p1=(LinkList)malloc(sizeof(LNode)); + p1->data = a; + head=Insert(head,p1); + i++; + } + return(head); +} +LinkList CreateList2() +{ + LinkList p1, head=0; + int a,i=0; + printf("产生第二条排序链表,共包含10个数: \n"); + srand(time(NULL)); + while(i<10) + { + a=rand()%80; + p1=(LinkList)malloc(sizeof(LNode)); + p1->data = a; + head=Insert(head,p1); + i++; + } + return(head); +} +LinkList Insert(LinkList head, LinkList p) // +{ + LinkList p1, p2; + if(head == NULL) // 原链表为空链表,对应情况① + { head = p; + p->next = NULL; + return(head); + } + p1 = head; + while( (p->data) > (p1->data) && p1->next != NULL ) + { // 寻找待插入位置 + p2 = p1; p1 = p1->next; + // p2指向的结点在p1指向的结点之前 + } + if( (p->data) <= (p1->data) ) // 插在p1之前 + { + p->next = p1; + if(head == p1) + head = p; // 插在链表首部,对应情况② + else + p2->next = p; // 插在链表中间,对应情况③ + } + else // 插在链表尾结点之后,对应情况④ + { + p1->next = p; + p->next = NULL; + } + return(head); +} +void PrintList(LinkList x) +{ + while(x!=NULL) + { + printf("%d ——>",x->data); + x=x->next; + } + printf("NULL \n\n"); +} +LinkList Merge(LinkList pHead1, LinkList pHead2) +{ + if(pHead1 == NULL)//判断二者中是否有空表的情况 + return pHead2; + else if(pHead2 == NULL) + return pHead1; + + LinkList pMergedHead=NULL;//合并后链表的首节点 + + + if(pHead1->data < pHead2->data) + { + pMergedHead = pHead1; + pMergedHead->next = Merge(pHead1->next, pHead2); + } + else + { + pMergedHead = pHead2; + pMergedHead->next = Merge(pHead1, pHead2->next); + } + + return pMergedHead; +} + +int main() +{ + LinkList x,y,z; + x=CreateList1(); + PrintList(x); + + y=CreateList2(); + PrintList(y); + + printf("\n合并后的链表: \n"); + z=Merge(x,y); + PrintList(z); + + return 0; + +} diff --git "a/2017-1/\347\256\227\346\263\2253.2.1/\347\256\227\346\263\2253.2.1 \346\225\260\345\210\266\350\275\254\346\215\242.cpp" "b/2017-1/\347\256\227\346\263\2253.2.1/\347\256\227\346\263\2253.2.1 \346\225\260\345\210\266\350\275\254\346\215\242.cpp" new file mode 100644 index 00000000..44c3eb82 --- /dev/null +++ "b/2017-1/\347\256\227\346\263\2253.2.1/\347\256\227\346\263\2253.2.1 \346\225\260\345\210\266\350\275\254\346\215\242.cpp" @@ -0,0 +1,79 @@ +#include +#include +#define STACK_INIT_SIZE 100 +#define STACKINCREMENT 10 +typedef int SElemType; +typedef struct{ + SElemType *base; + SElemType *top; + int stacksize; +}SqStack; +typedef enum{ + OK, + ERROR, + OVERFLOW +}Status; +Status InitStack(SqStack *); +Status StackEmpty(SqStack ); +Status Push(SqStack *,SElemType ); +Status Pop(SqStack *,SElemType ); +int main() +{ + SElemType e; + SElemType N; + SqStack sq; + SqStack *S; + S=&sq; + + InitStack(S); + scanf("%d",&N); + while(N) + { + Push(S,N%8); + N=N/8; + } + while(!StackEmpty(S)) + { + Pop(S,e); + printf("%d",e); + } + + return 0; +} +Status InitStack(SqStack *s) +{ + s->base=(SElemType *)malloc(STACK_INIT_SIZE * sizeof(SElemType)); + if(!s->base)exit(OVERFLOW); + s->top=s->base; + s->stacksize=STACK_INIT_SIZE ; + return OK; +} +Status StackEmpty(SqStack *s) +{ + if(s->base==s->top) + return OK; + else + return ERROR; +} +Status Push(SqStack *s,SElemType e) +{ + if(s->top-s->base>=s->stacksize)//栈满,追加存储空间 + { + s->base=(SElemType *)realloc(s->base,(s->stacksize+STACKINCREMENT) * sizeof(SElemType)); + if(!s->base) + return OVERFLOW;//存储分配失败 + s->top=s->base+s->stacksize; + s->stacksize+=STACKINCREMENT; + } + else + *s->top++=e; + + +} +Status Pop(SqStack *s,SElemType e) +{ + if(s->top==s->base)//判断是否为空栈 + return ERROR; + e=*(s->top-1); + return OK; +} \ No newline at end of file diff --git "a/2017-1/\347\256\227\346\263\2253.2.2/\347\256\227\346\263\2253.2.2.cpp" "b/2017-1/\347\256\227\346\263\2253.2.2/\347\256\227\346\263\2253.2.2.cpp" new file mode 100644 index 00000000..e4c381ef --- /dev/null +++ "b/2017-1/\347\256\227\346\263\2253.2.2/\347\256\227\346\263\2253.2.2.cpp" @@ -0,0 +1,82 @@ +#include +#include +#define MAX_STACK 100 + +struct stStack +{ + char szStack[MAX_STACK]; + int nTop; +}; + +void InitStack(stStack *s) +{ + s->nTop = -1; +} + +char Push(stStack *s, char c) +{ + if(s->nTop == MAX_STACK - 1) + return 0; + + s->nTop ++; + s->szStack[s->nTop] = c; + return c; +} + +char Pop(stStack *s) +{ + if (s->nTop == -1) + { + return 0; + } + char c = s->szStack[s->nTop]; + s->nTop--; + return c; +} + +int Check(char* szText) +{ + stStack *s; + InitStack(s); + int nLen = strlen(szText); + for (int i = 0; i < nLen; i++) + { + char c = szText[i]; + + switch (c) + { + case '(': + case '{': + case '[': + Push(s, c); + break; + + case ')': + if (Pop(s) != '(') + return 0; + break; + case '}': + if (Pop(s) != '{') + return 0; + break; + case ']': + if (Pop(s) != '[') + return 0; + break; + } + } + return 1; +} + +int main() +{ + char szText[100]; + scanf("%s", szText); + if (Check(szText)){ + printf("合法\n"); } + else + { + printf("失败\n"); + } + return 0; +} \ No newline at end of file diff --git "a/2017-1/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" "b/2017-1/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" new file mode 100644 index 00000000..24eb4aa7 --- /dev/null +++ "b/2017-1/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274/\350\241\250\350\276\276\345\274\217\346\261\202\345\200\274.c" @@ -0,0 +1,143 @@ +#include +#include +#include +#include +#include +#include +#define MAXN 1000 +using namespace std; +stack s; //定义了一个栈 + +char *tranfExp(char* exp) +{ + char tempStr[1000];//保存后缀表达式的字符串 + + int i=0,j=0; + while(exp[i] !='\0') + { + if(exp[i]>='0' &&exp[i]<='9') //如果是数字字符串就保存到后缀表达式字符串中 + { + tempStr[j++] = exp[i]; + } + else if(exp[i] == '(' ) //如果是左括号及入栈 + { + s.push(exp[i]); + } + else if(exp[i] == ')' ) //如果是右括号就把接近栈顶的左括号上面所有的运算符出栈存进字符串中 左括号出栈 + { + while(s.empty() == false) + { + if(s.top() == '(' ) + { + s.pop(); + break; + } + else + { + tempStr[j++] = s.top(); + s.pop(); + } + } + } + else if(exp[i] == '+' || exp[i] == '-') //如果的事+-|操作符就把比他优先级高或者等于的所有运算符出栈进入字符串 + { + while(s.empty() == false) + { + char ch = s.top(); + if(ch == '+'||ch == '-'||ch == '/'||ch == '*') + { + + tempStr[j++] = s.top(); + s.pop(); + } + else + break; + } + s.push(exp[i]); + } + else if(exp[i] == '*' || exp[i] == '/') //类似于扫描到+- 只是如果栈中有=-运算符就不用出栈 因为运算符优先级比较小 + { + while(s.empty() == false) + { + char ch = s.top(); + if(ch == '/' || ch=='*') + { + tempStr[j++] = s.top(); + s.pop(); + } + else + break; + } + s.push(exp[i]); + } + i++; + } + while(s.empty() == false) //把栈中剩余的所有运算符出栈 + { + tempStr[j++] = s.top(); + s.pop(); + } + tempStr[j] = 0; //最后一个赋值为0 也就是字符串结束的标志 + return tempStr; //返回已经得到的后缀表达式 +} +int calcExp(char* exp)// 计算后缀表达式 +{ + puts(exp); //展示已经得到的后缀 + while( !s.empty() ) + s.pop(); + int i=0; + while(exp[i] != '\0') + { + if(exp[i]>='0' && exp[i]<='9') + { + s.push(exp[i]-'0'); + } + else if(exp[i] == '-') + { + int m = s.top(); + s.pop(); + int n = s.top(); + s.pop(); + s.push(n-m); + } + else if(exp[i] == '+') + { + int m = s.top(); + s.pop(); + int n = s.top(); + s.pop(); + s.push(n+m); + } + else if(exp[i] == '/') + { + int m = s.top(); + s.pop(); + int n = s.top(); + s.pop(); + s.push(n/m); + } + else if(exp[i] == '*') + { + int m = s.top(); + s.pop(); + int n = s.top(); + s.pop(); + s.push(n*m); + } + i++; + } + printf("\n\n\n"); + return s.top(); +} +int main() +{ + char str[1000]; + char* tranStr; + tranStr = (char *)malloc(100*sizeof(char)); + printf("please input expression with kuohao:\n"); + scanf("%s",str); + tranStr = tranfExp(str);//中缀表达式转换为后缀表达式函数 + //puts(tranStr); //输出转换后的后缀表达式 + printf("%d",calcExp(tranStr)); + return 0; +} \ No newline at end of file diff --git "a/2017-1/\351\230\237\345\210\227\347\233\270\345\205\263\345\207\275\346\225\260\345\256\236\347\216\260/Queue.c" "b/2017-1/\351\230\237\345\210\227\347\233\270\345\205\263\345\207\275\346\225\260\345\256\236\347\216\260/Queue.c" new file mode 100644 index 00000000..b85b08f7 --- /dev/null +++ "b/2017-1/\351\230\237\345\210\227\347\233\270\345\205\263\345\207\275\346\225\260\345\256\236\347\216\260/Queue.c" @@ -0,0 +1,126 @@ +#include "Queue.h" +#include +#include + +/*构造一个空队列*/ +Queue* InitQueue() +{ + Queue* pqueue = (Queue*)malloc(sizeof(Queue)); + if(pqueue!=NULL) + { + pqueue->front = NULL; + pqueue->rear = NULL; + pqueue->size = 0; + + } + return pqueue; + } + +/*销毁一个队列*/ +void DestroyQueue(Queue* pqueue) +{ + if(IsEmpty(pqueue)!=1) + ClearQueue(pqueue); + + free(pqueue); +} + +/*清空一个队列*/ +void ClearQueue(Queue* pqueue) +{ + while(IsEmpty(pqueue)!= 1) + { + DelQueue(pqueue,NULL); + + } +} +/*判断一个队列是否为空*/ +int IsEmpty(Queue* pqueue) +{ + if(pqueue->front==NULL&&pqueue->rear==NULL&& pqueue->size ==0) + return 1; + else + return 0; +} +/*返回一个队列的大小*/ +int GetSize(Queue* pqueue) +{ + return pqueue->size; +} +/*返回队头元素*/ +PNode GetFront(Queue* pqueue,Item *pitem) +{ + if(IsEmpty(pqueue)!=1&& pitem!=NULL)//队列不空 + *pitem = pqueue->front->data; + return pqueue->front; + +} + +/*返回队尾元素*/ +PNode GetRear(Queue *pqueue,Item *pitem) +{ + if(IsEmpty(pqueue)!=1&& pitem!=NULL) + { + *pitem = pqueue->rear->data; + } + return pqueue->rear; + +} +/*将新元素入队列*/ +PNode EnQueue(Queue* pqueue,Item item) +{ + + PNode pnode = (PNode)malloc(sizeof(Node)); + + if(pnode!=NULL) + { + pnode->data = item; + pnode->next = NULL; + if(IsEmpty(pqueue)) + { + pqueue->front = pnode; + } + else + { + pqueue->rear->next = pnode; + } + pqueue->rear = pnode; + pqueue->size++; + + } + return pnode; +} + +/*队头元素出队*/ +PNode DelQueue(Queue* pqueue, Item *pitem) +{ + PNode pnode = pqueue->front; + if(IsEmpty(pqueue)!=1&&pnode!=NULL) + { + if(pitem!=NULL) + *pitem = pnode->data; + pqueue->size--; + pqueue->front = pnode->next; + free(pnode); + if(pqueue->size ==0) + pqueue->rear =NULL; + + + } + + return pqueue->front; +} + +/*遍历队列*/ +void QueueTraverse(Queue* pqueue,void (*visit)(PNode)) +{ + + PNode pnode = pqueue->front; + int i = pqueue->size; + while(i--) + { + visit(pnode); + pnode = pnode->next; + } + +} \ No newline at end of file diff --git "a/2017-1/\351\230\237\345\210\227\347\233\270\345\205\263\345\207\275\346\225\260\345\256\236\347\216\260/Queue.h" "b/2017-1/\351\230\237\345\210\227\347\233\270\345\205\263\345\207\275\346\225\260\345\256\236\347\216\260/Queue.h" new file mode 100644 index 00000000..ffc4b2c7 --- /dev/null +++ "b/2017-1/\351\230\237\345\210\227\347\233\270\345\205\263\345\207\275\346\225\260\345\256\236\347\216\260/Queue.h" @@ -0,0 +1,56 @@ + #ifndef QUEUE_H_ + #define QUEUE_H_ + + typedef int Item; + + typedef struct node + { + Item data; + struct node* next; + }Node,*PNode; + + typedef struct + { + PNode front; + PNode rear; + int size; + + }Queue; + + + + + /*构造一个空队列*/ + Queue* InitQueue(); + + /*销毁一个队列*/ + void DestroyQueue(Queue* pqueue); + + /*清空一个队列*/ + void ClearQueue(Queue* pqueue); + + /*判断一个队列是否为空*/ + int IsEmpty(Queue* pqueue); + + /*返回队列大小*/ + int GetSize(Queue* pqueue); + + /*返回对头元素*/ + PNode GetFront(Queue* pqueue,Item *pitem); + + /*返回队尾元素*/ + PNode GetRear(Queue* pqueue,Item *pitem); + + /*将新元素入队*/ + PNode EnQueue(Queue* pqueue,Item item); + + /*将队头元素出队*/ + PNode DelQueue(Queue* pqueue,Item *pitem); + + + + /*遍历队列,并对各项数据调用visit函数*/ + void QueueTraverse(Queue* pqueue,void (*visit)(PNode)); + + + #endif \ No newline at end of file diff --git "a/2017-1/\351\230\237\345\210\227\347\233\270\345\205\263\345\207\275\346\225\260\345\256\236\347\216\260/main.c" "b/2017-1/\351\230\237\345\210\227\347\233\270\345\205\263\345\207\275\346\225\260\345\256\236\347\216\260/main.c" new file mode 100644 index 00000000..838adb84 --- /dev/null +++ "b/2017-1/\351\230\237\345\210\227\347\233\270\345\205\263\345\207\275\346\225\260\345\256\236\347\216\260/main.c" @@ -0,0 +1,45 @@ +#include "Queue.h" +#include +void visit(PNode pq) +{ + +printf("该节点的元素为: %d\n",pq->data); + +} + +int main() +{ + +Queue* pq = InitQueue(); +int i,item; + +printf("输入0-9共10个数字入队\n"); + +for(i =0; i < 10; i++) +{ + EnQueue(pq,i); + GetRear(pq,&item); + printf("%d ",item); + +} +printf("\n"); + + + +printf("遍历这个队列,并执行visit函数:\n"); + +QueueTraverse(pq,&visit); +printf("队列元素出队并依次输出如下: \n"); +for(i = 0; i < 10 ; i++) +{ + DelQueue(pq,&item); + printf("%d",item); + +} +ClearQueue(pq); +if(IsEmpty(pq)) +printf("成功将队列置为空\n"); +DestroyQueue(pq); +printf("队列已被销毁\n"); +return 0; +} \ No newline at end of file diff --git a/README.md b/README.md old mode 100644 new mode 100755 diff --git a/examples/2.1.c b/examples/2.1.c index 679a671a..8a593d4c 100644 --- a/examples/2.1.c +++ b/examples/2.1.c @@ -5,83 +5,83 @@ #include "2.1.h" void InitList(List *L) { - L->length = 0; + L->length = 0; L->listsize = LISTSIZE; } int equal_fn(ElemType a, ElemType b) { - return a == b ? 1 : 0; + return a == b ? 1 : 0; } int ListLength(List a) { - return a.length; + return a.length; } void GetElem(List Lb, int i, ElemType *e) { - *e = Lb.elem[i]; + *e = Lb.elem[i]; } int LocateElem(List La, ElemType e, int (*equal)(ElemType, ElemType)) { - int i; - for(i = 0; i < ListLength(La); i++) { - if((*equal)(e, La.elem[i]) == 1) { - return i; - } - } - - return -1; + int i; + for(i = 0; i < ListLength(La); i++) { + if((*equal)(e, La.elem[i]) == 1) { + return i; + } + } + + return -1; } int ListInsert(List *L, int i, ElemType e) { ElemType *p, *q; - if (i < 0 || i > L->length + 1) { // 鎻掑叆浣嶇疆涓嶅悎娉 - return ERROR; // 绋嬪簭璁捐鐨刦ail early and openly鍘熷垯 + if (i < 0 || i > L->length + 1) { // 插入位置不合法 + return ERROR; // 程序设计的fail early and openly原则 } - if (L->length >= L->listsize) { // 褰撳墠瀛樺偍绌洪棿宸叉弧 - return OVERFLOW; // 绋嬪簭璁捐鐨勮繑鍥炵姸鎬佺爜瑙勮寖 + if (L->length >= L->listsize) { // 当前存储空间已满 + return OVERFLOW; // 程序设计的返回状态码规范 } - q = &(L->elem[i-1]); // q 鎸囩ず鎻掑叆浣嶇疆 + q = &(L->elem[i-1]); // q 指示插入位置 for (p = &(L->elem[L->length]); p >= q; --p) { - *(p+1) = *p; // 鎻掑叆浣嶇疆鍙婁箣鍚庣殑鍏冪礌鍙崇Щ + *(p+1) = *p; // 插入位置及之后的元素右移 } - *q = e; // 鎻掑叆e - ++L->length; // 琛ㄩ暱澧1 + *q = e; // 插入e + ++L->length; // 表长增1 return OK; } void Union(List *la, List lb) { - int i; - ElemType e; - int La_len = ListLength(*la); - int Lb_len = ListLength(lb); - - for(i = 0; i < Lb_len; i++) { - GetElem(lb, i, &e); - if(LocateElem(*la, e, &equal_fn) == -1) { - ListInsert(la, ++La_len, e); - } - } + int i; + ElemType e; + int La_len = ListLength(*la); + int Lb_len = ListLength(lb); + + for(i = 0; i < Lb_len; i++) { + GetElem(lb, i, &e); + if(LocateElem(*la, e, &equal_fn) == -1) { + ListInsert(la, ++La_len, e); + } + } } void ListTraverse(List L) { - int i; - for(i = 0; i < ListLength(L); i++) { - printf("%d ", L.elem[i]); - } - printf("\n"); + int i; + for(i = 0; i < ListLength(L); i++) { + printf("%d ", L.elem[i]); + } + printf("\n"); } int main() { - List la; - List lb; + List la; + List lb; int i; int la_length = 5; int lb_length = 6; - InitList(&la); - InitList(&lb); + InitList(&la); + InitList(&lb); - // 浜х敓娴嬭瘯鏁版嵁 + // 产生测试数据 srand(time(NULL)); for(i = 0; i < la_length; i++) { la.elem[i] = (int)rand() % 1024; @@ -92,12 +92,12 @@ int main() { } lb.length = lb_length; - ListTraverse(la); - ListTraverse(lb); + ListTraverse(la); + ListTraverse(lb); Union(&la, lb); - ListTraverse(la); + ListTraverse(la); - return 0; + return 0; } diff --git a/examples/2.1.h b/examples/2.1.h index 2c55c5f5..bc209007 100644 --- a/examples/2.1.h +++ b/examples/2.1.h @@ -1,11 +1,11 @@ -#define LISTSIZE 100 // 瀹归噺锛氬瓨鍌ㄧ┖闂寸殑鏈澶у垎閰嶉噺 +#define LISTSIZE 100 // 容量:存储空间的最大分配量 typedef int ElemType; typedef struct _SqList{ ElemType elem[LISTSIZE]; - int length; // 褰撳墠闀垮害 - int listsize; + int length; // 当前长度 + int listsize; } SqList; #define OK 0 diff --git a/examples/3.1.c b/examples/3.1.c new file mode 100644 index 00000000..43e92877 --- /dev/null +++ b/examples/3.1.c @@ -0,0 +1,61 @@ +/******************************************************************************* + 鏂囦欢鍚: + 椤圭洰: + 妯″潡: + 浣滆: huangwei + 鐗堟湰: 0.1 + 鍒涘缓浜: + 鎻忚堪: + + 淇敼鍘嗗彶: + 鏃ユ湡: 淇敼浜: 淇敼鎻忚堪: + + *********************************************************************************/ +#include +#include + +#include "stack.h" +#include "myds.h" + +Status ModConv(SqStack S, int input, int d) +{ + int integer; + int remainder; + SElemType e; + + if(d > 10) { + fprintf(stderr, "it does not make sense with division greater than 10\n"); + return ERROR; + } + + while(input) { + Push(&S, input % d); + input = input / d; + } + + while(!IsEmptyStack(S)) { + Pop(&S, &e); + printf("%d", e); + } + printf("\n"); + return OK; +} + + +int main(int argc, char* argv[]) +{ + SqStack S; + int maxsize = STACK_INIT_SIZE; + int input; + int d; + + scanf("%d", &input); + scanf("%d", &d); + + InitStack(&S, maxsize); + ModConv(S, input, d); + DestroyStack(&S); + + return 0; +} + diff --git a/examples/3.2.c b/examples/3.2.c new file mode 100644 index 00000000..9dc76481 --- /dev/null +++ b/examples/3.2.c @@ -0,0 +1,63 @@ +#include +#include + +#include "3.2.h" + + +Status InitStack(SqStack *S, int size) { + S->base = (SElemType *)malloc(sizeof(SElemType) * size); + if(!S->base) { + return OVERFLOW; + } + + S->top = S->base; + S->stacksize = size; + + return OK; +} + +void DestroyStack(SqStack *S) { + if(S->base) { + free(S->base); + } +} + +bool IsStackEmpty(SqStack *S) { + if(S) { + return S->base == S->top; + } + + return false; +} + +Status Push(SqStack *S, SElemType e) { + if(S->top - S->base >= S->stacksize) { + // TODO re-allocate space for new stack + return OVERFLOW; + } + + *S->top++ = e; + return OK; +} + +Status Pop(SqStack *S, SElemType *e) { + if(IsStackEmpty(S)) { + return ERROR; + } + + *e = *--S->top; + + return OK; +} + +SElemType GetTop(SqStack S) { + if(IsStackEmpty(&S)) { + return ' '; + } + + return *(S.top - 1); +} + + + + diff --git a/examples/3.2.h b/examples/3.2.h new file mode 100644 index 00000000..075ae984 --- /dev/null +++ b/examples/3.2.h @@ -0,0 +1,27 @@ +#define STACK_SIZE 100 + +typedef char SElemType; +typedef struct _Stack { + SElemType *top; + SElemType *base; + int stacksize; +} SqStack; + +typedef enum { + OK, + OVERFLOW, + ERROR +} Status; + +typedef enum { + false, + true +} bool; + +Status InitStack(SqStack *S, int size); +void DestroyStack(SqStack *S); +bool IsStackEmpty(SqStack *S); +Status Push(SqStack *S, SElemType e); +Status Pop(SqStack *S, SElemType *e); +SElemType GetTop(SqStack S); + diff --git a/examples/BiTree.c b/examples/BiTree.c new file mode 100644 index 00000000..fed23465 --- /dev/null +++ b/examples/BiTree.c @@ -0,0 +1,46 @@ + /******************************************************************************* + 鏂囦欢鍚: + 椤圭洰: + 妯″潡: + 浣滆: huangwei + 鐗堟湰: 0.1 + 鍒涘缓浜: + 鎻忚堪: + + 淇敼鍘嗗彶: + 鏃ユ湡: 淇敼浜: 淇敼鎻忚堪: + + *********************************************************************************/ +#include +#include +#include + +#include "BiTree.h" + +extern char input[]; +extern int input_i; + +Status CreateBiTree(BiTree *T) { + char ch = input[input_i]; + input_i++; + if(ch == '#') { + *T = NULL; + } else { + if( !(*T = (BiTree)malloc(sizeof(BiTNode))) ) { + return OVERFLOW; + } + (*T)->data = ch; // 鐢熸垚鏍圭粨鐐 + CreateBiTree(&(*T)->lchild); // 鏋勯犲乏瀛愭爲 + CreateBiTree(&(*T)->rchild); // 鏋勯犲彸瀛愭爲 + } + return OK; +} + +void PostOrderTraverseTree(BiTree T) { + if(T) { + PostOrderTraverseTree(T->lchild); // 閬嶅巻宸﹀瓙鏍 + PostOrderTraverseTree(T->rchild); // 閬嶅巻鍙冲瓙鏍 + printf("%c", T->data); // 璁块棶缁撶偣 + } +} + diff --git a/examples/BiTree.h b/examples/BiTree.h new file mode 100644 index 00000000..11692ae9 --- /dev/null +++ b/examples/BiTree.h @@ -0,0 +1,15 @@ +#ifndef _BTREE_H_ +#define _BTREE_H_ + +#include "myds.h" + +typedef struct BiTNode { // 缁撶偣缁撴瀯 + TElemType data; + struct BiTNode *lchild, *rchild; // 宸﹀彸瀛╁瓙鎸囬拡 +} BiTNode, *BiTree; + +Status CreateBiTree(BiTree *T); +void PostOrderTraverseTree(BiTree T); + +#endif + diff --git a/examples/Makefile b/examples/Makefile old mode 100644 new mode 100755 index 9fb8a621..6bf33c41 --- a/examples/Makefile +++ b/examples/Makefile @@ -1,17 +1,54 @@ CC = cc -CFLAGS = -Wall -ggdb3 -Iinclude # debug: put -ggdb instead of -O3 +CFLAGS = -DDEBUG=0 -Wall -ggdb3 -Iinclude # debug: put -ggdb instead of -O3 LDFLAGS = LIBS = DEFS = GNUDEF = LPTHREAD = EXENAME = t_list.exe -OBJECTS = list.o t_list.o +OBJECTS = stack.o list.o t_list.o TESTFILS = VPATH = all:$(EXENAME) +t_sort:t_sort.c sort.o + $(CC) $(CFLAGS) $(LDFLAGS) $^ -o t_sort.exe + -rm -rf *.o + +t_hashtable:t_hashtable.c hashtable.o + $(CC) $(CFLAGS) $(LDFLAGS) $^ -o t_hashtable.exe + -rm -rf *.o + +t_hashtable_chained:t_hashtable.c chained_hashtable.o + $(CC) $(CFLAGS) $(LDFLAGS) -DCHAINED_HASH $^ -o t_hashtable_chained.exe + -rm -rf *.o + +chained_hashtable.o:hashtable.c + $(CC) -c $(CFLAGS) $(LDFLAGS) -DCHAINED_HASH $^ -o chained_hashtable.o + +t_dijkstra:t_dijkstra.c dijkstra.o dijkstra_graph.o + $(CC) $(CFLAGS) $(LDFLAGS) $^ -o t_dijkstra.exe + -rm -rf *.o + +dijkstra_graph.o:graph.c + $(CC) -c $(CFLAGS) $(LDFLAGS) -DDIJSKTRA $^ -o dijkstra_graph.o + + +t_graph:t_graph.c graph.o + $(CC) $(CFLAGS) $(LDFLAGS) $^ -o t_graph.exe + -rm -rf *.o + +t_BTree:t_BTree.c BTree.o + $(CC) $(CFLAGS) $(LDFLAGS) $^ -o t_BTree.exe + -rm -rf *.o + +3.2:t_3.2.c 3.2.o + $(CC) $(CFLAGS) $(LDFLAGS) $^ -o t_3.2.exe + +3.1:3.1.c stack.o + $(CC) $(LDFLAGS) $^ -o t_3.1.exe + 2.1:2.1.c $(CC) $(LDFLAGS) $^ -o 2.1.exe @@ -29,5 +66,6 @@ clean: -find . \( -name '*.o' -o -name '*.so' -o -name '*.bak' -o -name 'tags' -o -name '*.exe' -o \ -name '*.swp' -o -name '*.out' -o -name '*.gch' -o -name '*.*~' -o \ -name 'core.*' -o -name '*.gch' -o -name '*.files' -o -name '$(TESTFILS)' \) -type f -execdir rm -f '{}' \; + -find . -iname '*.dSYM' -type d -execdir rm -rf '{}' 2>/dev/null \; .PHONY: clean diff --git a/examples/dijkstra.h b/examples/dijkstra.h new file mode 100644 index 00000000..878b7d07 --- /dev/null +++ b/examples/dijkstra.h @@ -0,0 +1,14 @@ +#ifndef _DIJKSTRA_H_ +#define _DIJKSTRA_H_ + +#include + +#include "graph.h" + +int minDistance(int dist[], bool sptSet[], int vexnum); +void printPath(int parent[], int j); +void printSolution(int dist[], int vexnum, int parent[], int src); +void dijkstra(Graph g, int src); + +#endif + diff --git a/examples/dijkstra.png b/examples/dijkstra.png new file mode 100644 index 00000000..90606711 Binary files /dev/null and b/examples/dijkstra.png differ diff --git a/examples/dijsktra.c b/examples/dijsktra.c new file mode 100644 index 00000000..4786c8b9 --- /dev/null +++ b/examples/dijsktra.c @@ -0,0 +1,90 @@ + /******************************************************************************* + 鏂囦欢鍚: + 椤圭洰: + 妯″潡: + 浣滆: huangwei + 鐗堟湰: 0.1 + 鍒涘缓浜: + 鎻忚堪: + + 淇敼鍘嗗彶: + 鏃ユ湡: 淇敼浜: 淇敼鎻忚堪: + + *********************************************************************************/ +#include +#include + +#include "myds.h" +#include "dijkstra.h" +#include "graph.h" + +int minDistance(int dist[], bool sptSet[], int vexnum) { + int min = INT_MAX, min_index; + + for (int v = 0; v < vexnum; v++) { + if (sptSet[v] == false && dist[v] <= min) { + min = dist[v]; + min_index = v; + } + } + + return min_index; +} + +void printPath(int parent[], int j) { + if (parent[j] == -1) { + return; + } + + printPath(parent, parent[j]); + + printf("%d ", j); +} + +void printSolution(int dist[], int vexnum, int parent[], int src) { + printf("Vertex\t Distance\tPath"); + for (int i = 0; i < vexnum; i++) { + printf("\n%d -> %d \t\t %d\t\t%d ", src, i, dist[i], src); + printPath(parent, i); + } +} + +void dijkstra(Graph g, int src) { + int count, u, v; + int vexnum = g.vexnum; + int dist[vexnum]; // 浠庨《鐐箂rc鍒板叾浠栨墍鏈夐《鐐圭殑鏈鐭窛绂绘暟缁 + + // sptSet[i]浠庨《鐐箂rc鍒版寚瀹氶《鐐筰鐨勬渶鐭矾寰勬槸鍚﹀寘鍚《鐐筰 + bool sptSet[vexnum]; + + // 淇濆瓨鏈鐭矾寰勬爲 + int parent[vexnum]; + int **graph = g.matrix; + + for (int i = 0; i < vexnum; i++) { + parent[i] = -1; + dist[i] = INT_MAX; + sptSet[i] = false; + } + + dist[src] = 0; + + // 浠庢寚瀹氶《鐐箂rc鍑哄彂鐨勬墍鏈夋渶鐭矾寰 + for (count = 0; count < vexnum - 1; count++) { + u = minDistance(dist, sptSet, vexnum); + + sptSet[u] = true; + + for (v = 0; v < vexnum; v++) { + if (!sptSet[v] && graph[u][v] && + dist[u] + graph[u][v] < dist[v]) { + parent[v] = u; + dist[v] = dist[u] + graph[u][v]; + } + } + } + + printSolution(dist, vexnum, parent, src); +} + + diff --git a/examples/graph.c b/examples/graph.c new file mode 100644 index 00000000..342545df --- /dev/null +++ b/examples/graph.c @@ -0,0 +1,185 @@ + /******************************************************************************* + 鏂囦欢鍚: + 椤圭洰: + 妯″潡: + 浣滆: huangwei + 鐗堟湰: 0.1 + 鍒涘缓浜: + 鎻忚堪: + + 淇敼鍘嗗彶: + 鏃ユ湡: 淇敼浜: 淇敼鎻忚堪: + + *********************************************************************************/ +#include +#include +#include +#include +#include + +#include "graph.h" + +Status _InitEmptyMatrix(Graph *g) { + int i, j; + int vexnum = g->vexnum; + g->matrix = (int **)malloc(vexnum * sizeof(int *)); + for(i = 0; i < vexnum; i++) { + g->matrix[i] = (int *)malloc(vexnum * sizeof(int)); + for(j = 0; j < vexnum; j++) { + if(g->matrix[i] == NULL) { + return OVERFLOW; + } + if(i == j) { + g->matrix[i][j] = 0; + } else { +#ifdef DIJSKTRA + g->matrix[i][j] = 0; +#else + g->matrix[i][j] = -1; +#endif + } + } + } + + return OK; +} + +Status _CreateUnNetwork(Graph *g, VertexRelation *vr) { + + int i = 0, v, w, weight; + + if(!vr) { + fprintf(stderr, "vr is null\n"); + } + + while(i < g->arcnum) { + v = (vr[i]).v; + w = (vr[i]).w; + weight = (vr[i]).weight; + g->matrix[v][w] = weight; + g->matrix[w][v] = weight; + i++; + } + + return OK; +} + +Status _CreateDiNetwork(Graph *g, VertexRelation *vr) { + + int i = 0, v, w, weight; + + if(!vr) { + fprintf(stderr, "vr is null\n"); + } + + while(i < g->arcnum) { + v = (vr[i]).v; + w = (vr[i]).w; + weight = (vr[i]).weight; + g->matrix[v][w] = weight; + i++; + } + + return OK; +} + +Status CreateGraph(Graph *g, VertexRelation *vr) { + switch(g->kind) { + case UnNetwork: /* 鏃犲悜缃 */ + if(OK == _InitEmptyMatrix(g)) { + return _CreateUnNetwork(g, vr); + } + break; + case DiNetwork:/* 鏈夊悜缃 */ + if(OK == _InitEmptyMatrix(g)) { + return _CreateDiNetwork(g, vr); + } + break; + case Undigraph: /* 鏃犲悜鍥 */ + case Digraph: /* 鏈夊悜鍥 */ + default: + fprintf(stderr, "%s", "not implemented yet"); + break; + } + + return ERROR; +} + +void Print(Graph g) { + int vexnum = g.vexnum; + int i, j; + + for(i = 0; i < vexnum; i++) { + for(j = 0; j < vexnum; j++) { + printf("%2d ", g.matrix[i][j]); + } + printf("\n"); + } +} + +Status LoadVertexRelation(const char *file_path, Graph *g, VertexRelation **vr) { + char line[MAX_LINE]; + int vexnum, arcnum, i = 0; + char *col; + int graph_kind; + + FILE *file = fopen(file_path, "r"); + if(file == NULL) { + return FILE_NOT_EXIST; + } + while(fgets(line, sizeof(line), file)) { + col = strtok(line, COL_SEP); + if(col != NULL) { + if(i == 0) { // 澶勭悊鏂囦欢涓殑绗竴琛岋紝瑙f瀽vexnum鍜宎rcnum + vexnum = (int)strtol(col, NULL, 10); + if(errno == EINVAL) { + return FILE_PARSE_ERROR; + } + col = strtok(NULL, COL_SEP); + arcnum = (int)strtol(col, NULL, 10); + if(errno == EINVAL) { + return FILE_PARSE_ERROR; + } + col = strtok(NULL, COL_SEP); + graph_kind = (int)strtol(col, NULL, 10); + if(errno == EINVAL) { + return FILE_PARSE_ERROR; + } + *vr = (VertexRelation *)malloc(arcnum * sizeof(VertexRelation)); + printf("vexnum:%d arcnum:%d graph_kind:%d\n", vexnum, arcnum, graph_kind); + } else { // 澶勭悊鏂囦欢涓叾浠栬锛岃В鏋恦, w, weight + (*vr)[i - 1].v = (int)strtol(col, NULL, 10); + if(errno == EINVAL) { + return FILE_PARSE_ERROR; + } + col = strtok(NULL, COL_SEP); + (*vr)[i - 1].w = (int)strtol(col, NULL, 10); + if(errno == EINVAL) { + return FILE_PARSE_ERROR; + } + col = strtok(NULL, COL_SEP); + (*vr)[i - 1].weight = (int)strtol(col, NULL, 10); + if(errno == EINVAL) { + return FILE_PARSE_ERROR; + } + printf("%d %d %d\n", (*vr)[i - 1].v, (*vr)[i - 1].w, (*vr)[i - 1].weight); + } + } + i++; + if(i > arcnum) { // 鏂囦欢涓浣欑殑琛屼笉鍐嶅鐞嗕簡 + break; + } + } + if(i <= arcnum) { // 鏂囦欢绗竴琛屽畾涔夌殑鍒楁暟 > 瀹為檯鏂囦欢涓畾涔夌殑杈圭殑鏁伴噺 + return FILE_PARSE_ERROR; + } + g->kind = graph_kind; + g->vexnum = vexnum; + g->arcnum = arcnum; + fclose(file); + + return OK; +} + + + diff --git a/examples/graph.h b/examples/graph.h new file mode 100644 index 00000000..27965d2f --- /dev/null +++ b/examples/graph.h @@ -0,0 +1,62 @@ +/******************************************************************************* + 鏂囦欢鍚: + 椤圭洰: + 妯″潡: + 浣滆: huangwei + 鐗堟湰: 0.1 + 鍒涘缓浜: + 鎻忚堪: + + 淇敼鍘嗗彶: + 鏃ユ湡: 淇敼浜: 淇敼鎻忚堪: + + *********************************************************************************/ +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef _GRAPH_H_ +#define _GRAPH_H_ + +#include "myds.h" + +#define MAX_LINE 32 +#define COL_SEP " ," + +typedef unsigned int Vertex; /* 椤剁偣锛屼粠0寮濮嬬紪鍙 */ +typedef enum { + Undigraph, /* 鏃犲悜鍥 */ + Digraph, /* 鏈夊悜鍥 */ + UnNetwork, /* 鏃犲悜缃 */ + DiNetwork /* 鏈夊悜缃 */ +} GraphKind; /* 鍥剧殑绉嶇被 */ + +typedef struct { + int v; + int w; + int weight; +} VertexRelation; + +typedef struct { + GraphKind kind; /* 鍥剧殑绉嶇被 */ + int **matrix; /* 閭绘帴鐭╅樀锛屼繚瀛樺甫鏉冨姬鍊 */ + int vexnum; /* 椤剁偣鏁 */ + int arcnum; /* 寮ф暟 */ +} Graph; + +/* 鏂规硶鎺ュ彛锛堝嚱鏁版寚閽堬級 */ +Status CreateGraph(Graph *g, VertexRelation *vr); /* 鎸夊畾涔夋瀯閫犲浘 */ + +int FirstAdjVex(Graph g, Vertex v); /* 杩斿洖 v 鐨勨滅涓涓偦鎺ョ偣鈥 銆傝嫢璇ラ《鐐瑰湪 G 涓病鏈夐偦鎺ョ偣锛屽垯杩斿洖鈥滅┖鈥濄 */ +int NextAdjVex(Graph g, Vertex v, Vertex w); /* 杩斿洖 v 鐨勨滅涓涓偦鎺ョ偣鈥 銆傝嫢璇ラ《鐐瑰湪 G 涓病鏈夐偦鎺ョ偣锛屽垯杩斿洖鈥滅┖鈥濄 */ +void Print(Graph g); /* 鎵撳嵃鍥剧殑閭绘帴鐭╅樀 */ + +/* 浠庢枃浠朵腑璇诲彇寮/杈归泦 */ +Status LoadVertexRelation(const char *file_path, Graph *g, VertexRelation **vr); + +#endif + +#ifdef __cplusplus +} +#endif + diff --git a/examples/myds.h b/examples/myds.h index afd4fe9c..c55608bc 100644 --- a/examples/myds.h +++ b/examples/myds.h @@ -3,8 +3,29 @@ typedef enum _Status { OK, - ERROR + ERROR, + OVERFLOW, + FILE_NOT_EXIST, + FILE_PARSE_ERROR, + KEY_FOUND, + KEY_NOT_FOUND, + DUPLICATE } Status; +#ifdef _HASH_TABLE_H_ +typedef int KeyType; +typedef int ValueType; +typedef struct _ElemType { + KeyType key; // 鍏抽敭瀛 + ValueType val; // 鍊 +#ifdef CHAINED_HASH + struct _ElemType *next; +#endif +} ElemType; +#else typedef int ElemType; #endif +typedef char TElemType; + +typedef enum { false, true} bool; +#endif diff --git a/examples/stack.c b/examples/stack.c new file mode 100644 index 00000000..6b8bac40 --- /dev/null +++ b/examples/stack.c @@ -0,0 +1,51 @@ +#include + +#include "stack.h" +#include "myds.h" + +Status InitStack (SqStack *S, int maxsize) +{ + // 鏋勯犱竴涓渶澶х┖闂翠负 maxsize 鐨勭┖椤哄簭鏍 S + S->base = (SElemType *)malloc(maxsize * sizeof(SElemType)); + if (!S->base) { + return OVERFLOW; //瀛樺偍鍒嗛厤澶辫触 + } + S->top = S->base; + S->stacksize = maxsize; + return OK; +} + +void DestroyStack(SqStack *S) +{ + if(S->base != NULL) + { + free(S->base); + } +} + + +Status Push (SqStack *S, SElemType e) +{ // 鑻ユ爤涓嶆弧锛屽垯灏 e 鎻掑叆鏍堥《 + if (S->top - S->base >= S->stacksize) { //鏍堟弧 + return OVERFLOW; + } + *S->top++ = e; + return OK; +} + +Status Pop(SqStack *S, SElemType *e) { + // 鑻ユ爤涓嶇┖锛屽垯鍒犻櫎S鐨勬爤椤跺厓绱狅紝 + // 鐢 e 杩斿洖鍏跺硷紝骞惰繑鍥濷K锛 + // 鍚﹀垯杩斿洖ERROR + if (S->top == S->base) { + return ERROR; + } + *e = *--S->top; + return OK; +} + +bool IsEmptyStack(SqStack S) +{ + return S.base == S.top; +} + diff --git a/examples/stack.h b/examples/stack.h new file mode 100644 index 00000000..65766975 --- /dev/null +++ b/examples/stack.h @@ -0,0 +1,26 @@ +#ifndef _STACK_H_ +#define _STACK_H_ + +#include "myds.h" + +//----- 鏍堢殑椤哄簭瀛樺偍琛ㄧず ----- +#define STACK_INIT_SIZE 100 +typedef int SElemType; +typedef struct { + SElemType *base; + SElemType *top; + int stacksize; +} SqStack; + +typedef enum { + false, + true +} bool; + +Status InitStack(SqStack *S, int maxsize); +Status Push(SqStack *S, SElemType e); +Status Pop(SqStack *S, SElemType *e); +bool IsEmptyStack(SqStack S); +void DestroyStack(SqStack *S); + +#endif diff --git a/examples/t_3.2.c b/examples/t_3.2.c new file mode 100644 index 00000000..fba4f432 --- /dev/null +++ b/examples/t_3.2.c @@ -0,0 +1,92 @@ +/******************************************************************************* + 鏂囦欢鍚: + 椤圭洰: + 妯″潡: + 浣滆: huangwei + 鐗堟湰: 0.1 + 鍒涘缓浜: + 鎻忚堪: + + 淇敼鍘嗗彶: + 鏃ユ湡: 淇敼浜: 淇敼鎻忚堪: + + *********************************************************************************/ +#include +#include +#include + +#include "3.2.h" + +Status matching(SqStack S, char *exp, char dict[]) { + int state = 1; + int i = 0; + int len = strlen(exp); + SElemType e; + + DEBUG && printf("%lu\n", strlen(exp)); + + while (i <= len && state) { + switch(*exp) { + case '(': + case '[': + case '{': + DEBUG && printf("hit ( with %c\n", *exp); + Push(&S, *exp++); + i++; + break; + case ')': + case ']': + case '}': + DEBUG && printf("hit ) with %c \n", GetTop(S)); + if(!IsStackEmpty(&S) && GetTop(S) == dict[(int)*exp]) { + DEBUG && printf("poped\n"); + Pop(&S, &e); + DEBUG && printf("poped with %c %d %c\n", e, i, *exp); + } else { + state = 0; + } + exp++; + i++; + break; + default: + DEBUG && printf("%c\n", *exp); + i++; + exp++; + break; + } + } + + if (IsStackEmpty(&S) && state) { + return OK; + } + + return ERROR; +} + +int main(int argc, char* argv[]) +{ + + SqStack S; + Status ret; + char dict[127]; + char test[100]; + scanf("%s", (unsigned char *)&test); + + // make dict hashmap + dict[']'] = '['; + dict[')'] = '('; + dict['}'] = '{'; + + InitStack(&S, STACK_SIZE); + + ret = matching(S, test, dict); + + if(ret == OK) { + printf("matched!\n"); + } else { + printf("not matched!\n"); + } + + return 0; +} + diff --git a/examples/t_BTree.c b/examples/t_BTree.c new file mode 100644 index 00000000..1eadea9f --- /dev/null +++ b/examples/t_BTree.c @@ -0,0 +1,40 @@ +/******************************************************************************* + 鏂囦欢鍚: + 椤圭洰: + 妯″潡: + 浣滆: huangwei + 鐗堟湰: 0.1 + 鍒涘缓浜: + 鎻忚堪: + + 淇敼鍘嗗彶: + 鏃ユ湡: 淇敼浜: 淇敼鎻忚堪: + + *********************************************************************************/ +#include +#include + +#include "BTree.h" + +char input[] = {'A', 'B', 'D', 'G', '#', '#','#', 'E', 'H', '#', '#', 'I', '#', 'K', '#', '#', 'C', '#', 'F', '#', '#', '\0'}; +int input_i = 0; + +int main(int argc, char* argv[]) +{ + BiTree T = NULL; + Status ret; + + ret = CreateBiTree(&T); + + if(ret == OK) { + printf("浜屽弶鏍 %s 鍒涘缓鎴愬姛\n", input); + } + + printf("璇ヤ簩鍙夋爲鐨勫悗搴忓簭鍒楄緭鍑轰负锛歕n"); + PostOrderTraverseTree(T); + + printf("\n"); + + return 0; +} + diff --git a/examples/t_dijsktra.c b/examples/t_dijsktra.c new file mode 100644 index 00000000..dba9ac3c --- /dev/null +++ b/examples/t_dijsktra.c @@ -0,0 +1,47 @@ +/******************************************************************************* + 鏂囦欢鍚: + 椤圭洰: + 妯″潡: + 浣滆: huangwei + 鐗堟湰: 0.1 + 鍒涘缓浜: + 鎻忚堪: + + 淇敼鍘嗗彶: + 鏃ユ湡: 淇敼浜: 淇敼鎻忚堪: + + *********************************************************************************/ +#include +#include + +#include "graph.h" +#include "myds.h" +#include "dijkstra.h" + +int main(int argc, char* argv[]) +{ + Graph g; + GraphKind kind = UnNetwork; + Status ret; + int i = 0; + VertexRelation **vr = (VertexRelation **)malloc(sizeof(VertexRelation *)); + + const char file_path[] = "t_graph.1.txt"; + + ret = LoadVertexRelation(file_path, &g, &vr); + if(ret == OK) { + ret = CreateGraph(&g, kind, vr); + Print(g); + } else { + fprintf(stderr, "load vertex relation from file failed with ret_code = %d\n", ret); + } + + for(i = 0; i < g.vexnum; i++) { + dijkstra(g, i); + printf("\n"); + } + + + + return 0; +} diff --git a/examples/t_dijsktra.sh b/examples/t_dijsktra.sh new file mode 100644 index 00000000..a1f5bd86 --- /dev/null +++ b/examples/t_dijsktra.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +tests=( +"test/1.txt" +"test/2.txt" +) + +for test in ${tests[@]};do + cp ${test} t_graph.1.txt + echo "================================================================================" + echo "test ${test}" + ./t_dijsktra.exe +done + diff --git a/examples/t_graph.1.txt b/examples/t_graph.1.txt new file mode 100644 index 00000000..c99c9d22 --- /dev/null +++ b/examples/t_graph.1.txt @@ -0,0 +1,12 @@ +7, 11, 3 +0, 1, 15 +0, 2, 2 +0, 3, 12 +1, 4, 6 +2, 4, 8 +2, 5, 4 +3, 6, 3 +5, 3, 5 +5, 6, 10 +6, 1, 4 +4, 6, 9 diff --git a/examples/t_graph.c b/examples/t_graph.c new file mode 100644 index 00000000..0ca5bb1d --- /dev/null +++ b/examples/t_graph.c @@ -0,0 +1,37 @@ +/******************************************************************************* + 鏂囦欢鍚: + 椤圭洰: + 妯″潡: + 浣滆: huangwei + 鐗堟湰: 0.1 + 鍒涘缓浜: + 鎻忚堪: + + 淇敼鍘嗗彶: + 鏃ユ湡: 淇敼浜: 淇敼鎻忚堪: + + *********************************************************************************/ +#include +#include + +#include "graph.h" + +int main(int argc, char* argv[]) +{ + Graph g; + Status ret; + VertexRelation *vr = (VertexRelation *)malloc(sizeof(VertexRelation)); + + const char file_path[] = "t_graph.1.txt"; + + ret = LoadVertexRelation(file_path, &g, &vr); + if(ret == OK) { + ret = CreateGraph(&g, vr); + Print(g); + } else { + fprintf(stderr, "load vertex relation from file failed with ret_code = %d\n", ret); + } + + return 0; +} + diff --git a/examples/test/1.txt b/examples/test/1.txt new file mode 100644 index 00000000..cbfee02d --- /dev/null +++ b/examples/test/1.txt @@ -0,0 +1,15 @@ +9, 14,2 +0, 1, 1 +0, 7, 7 +1, 2, 9 +1, 7, 12 +2, 3, 5 +2, 5, 4 +2, 8, 2 +3, 4, 9 +3, 5, 14 +4, 5, 3 +5, 6, 2 +6, 7, 1 +6, 8, 6 +7, 8, 7 diff --git a/examples/test/2.txt b/examples/test/2.txt new file mode 100644 index 00000000..4e32550d --- /dev/null +++ b/examples/test/2.txt @@ -0,0 +1,9 @@ +6, 8, 2 +0, 1, 1 +0, 2, 6 +1, 3, 2 +1, 4, 7 +2, 3, 1 +2, 4, 5 +3, 5, 3 +4, 5, 4