diff --git "a/2209040050/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040050/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index a46398c00c696f5160c2aa417447e99aa34ebe72..0000000000000000000000000000000000000000 --- "a/2209040050/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { -public: - vector twoSum(vector& nums, int target) { - int n = nums.size(); - for (int i = 0; i < n; ++i) { - for (int j = i + 1; j < n; ++j) { - if (nums[i] + nums[j] == target) { - return {i, j}; - } - } - } - return {}; - } -}; \ No newline at end of file diff --git "a/2224020150/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.c" "b/2224020150/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.c" new file mode 100644 index 0000000000000000000000000000000000000000..30a70b51749e72c3459e9d8c1cccb7357ec04cac --- /dev/null +++ "b/2224020150/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.c" @@ -0,0 +1,9 @@ +#include +int main() +{ + int num1,num2=0; + scanf("%d%d",&num1,&num2); + int sum=num1+num2; + printf("%d",sum); + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.c" "b/2224020150/chapter1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.c" new file mode 100644 index 0000000000000000000000000000000000000000..59b9314487a3369553f6c3292a54b76026d49c57 --- /dev/null +++ "b/2224020150/chapter1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\347\232\204\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.c" @@ -0,0 +1,26 @@ +#include +void Subarrays(int arr[], int n,int len) +{ + for (int i = 0; i <= n-len; i++) + { + for (int j = 0; j < len; j ++) + { + printf("%d",arr[i+j]); + } + printf("\n"); + } +} +void Oddlength_Subarrays(int arr[],int n) +{ + for (int len=1;len <=n;len +=2) + { + Subarrays(arr,n,len); + } +} +int main() +{ + int arr[]={1,2,3,4,5}; + int n=sizeof(arr)/sizeof(arr[0]); + Oddlength_Subarrays(arr,n); + return 0; +} diff --git "a/2224020150/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.c" "b/2224020150/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.c" index d3b3c1d8404339e647e2382353b2090a3f9df9cd..6b8f8b7c2fc585c4f0dcd7724a4844ef0675abe3 100644 --- "a/2224020150/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.c" +++ "b/2224020150/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.c" @@ -14,7 +14,7 @@ int main() char counter=1; int sum=0; srand(time(0)); - n=rand()%100+1;/*随机函数*/ + n=rand()%100+1; printf("产生的随机数为%d\n",n); printf("不大于%d的素数序列如下\n",n); if(n>2) diff --git "a/2224020150/chapter10/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.c" "b/2224020150/chapter10/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.c" new file mode 100644 index 0000000000000000000000000000000000000000..7b801867d4902a68d418e32c88f03a68d1122e92 --- /dev/null +++ "b/2224020150/chapter10/\345\256\236\347\216\260\345\206\222\346\263\241\346\216\222\345\272\217\347\256\227\346\263\225.c" @@ -0,0 +1,63 @@ +#include +#define MaxLength 100 + +int TotalNum = 0; +void InputData( int &count, int arrt[] ); +void BubbleSort( int data[], int n ); + +int main() +{ + int count = 0, i, j; + int data[MaxLength]; + InputData( count, data ); + printf( "排序前的数据:" ); + for( i = 1; i <= count; i++ ) + { + printf( "%5d", data[i] ); + } + printf( "\n" ); + BubbleSort( data, count ); + + return 0; +} + +void BubbleSort( int data[], int n ) +{ + int i, j, k, t; + for( i = 1; i <= n-1; i++ ) + { + for( j = 1; j <= n-i; j++ ) + { + if( data[j+1] < data[j] ) + { + t = data[j+1]; + data[j+1] = data[j]; + data[j] = t; + } + } + printf( "第 %d 趟排序:", i ); + for( k = 1; k <= n; k++ ) + { + printf( "%5d", data[k] ); + } + printf( "\n" ); + } +} +void InputData( int &count, int arrt[] ) +{ + int i = 0, data; + while( 1 ) + { + printf( "input an integer(end of 65535)" ); + scanf( "%d", &data ); + if( data == 65535 ) + { + break; + } + else + { + arrt[++i] = data; + } + } + count = i; +} diff --git "a/2224020150/chapter10/\345\256\236\347\216\260\345\255\246\347\224\237\344\277\241\346\201\257\347\232\204\345\244\232\345\205\263\351\224\256\345\255\227\346\216\222\345\272\217.c" "b/2224020150/chapter10/\345\256\236\347\216\260\345\255\246\347\224\237\344\277\241\346\201\257\347\232\204\345\244\232\345\205\263\351\224\256\345\255\227\346\216\222\345\272\217.c" new file mode 100644 index 0000000000000000000000000000000000000000..c7397cb0ee352356e83ecd3d42b71e719ddc16c2 --- /dev/null +++ "b/2224020150/chapter10/\345\256\236\347\216\260\345\255\246\347\224\237\344\277\241\346\201\257\347\232\204\345\244\232\345\205\263\351\224\256\345\255\227\346\216\222\345\272\217.c" @@ -0,0 +1,217 @@ +#include +#include +#include + +#define MAXR 10 +#define MAX_SIZE 10 + +typedef struct node +{ + char xm[10]; // 姓名 + char xb; // 性别 m:男 f:女 + char bh[6]; // 班号 + struct node *next; // 指向下一个结点的指针 +}stud_node; // 学生单链表结点类型 + +typedef struct +{ + char xm[10]; // 姓名 + char xb; // 性别 m:男 f:女 + char bh[6]; // 班号 +}stud_type; // 学生记录类型 + +static void create_link(stud_node *&p, stud_type students[], int n) // 指针的引用 +{ + int i; + stud_node *s, *t; + + p = NULL; + for(i = 0; i < n; i++) + { + s = (stud_node *)malloc(sizeof(stud_node)); // 动态分配存储空间(新创建的结点) + strcpy(s->xm, students[i].xm); + s->xb = students[i].xb; + strcpy(s->bh, students[i].bh); + if(p == NULL) + { + p = s; // p和t指向新创建的结点 + t = s; + } + else + { + t->next = s; // 采用尾插法建立单链表 + t = s; // t指向新创建的结点 + } + } + t->next = NULL; // 尾结点的next域置为NULL +} + +/*------------输出学生单链表p--------------*/ +static void disp_link(stud_node *p) +{ + int i = 0; + + while(p != NULL) + { + printf(" %s(%s,%c)", p->xm, p->bh, p->xb); + p = p->next; + if((i + 1) % 5 == 0) // 显示5个数据为一行 + printf("\n"); + i++; + } + printf("\n"); +} + +/*------------销毁学生单链表p--------------*/ +static void destroy_link(stud_node *p) +{ + stud_node *pre = p, *q = pre->next; + + while(q != NULL) + { + free(pre); + pre = q; + q = q->next; + } + free(pre); +} + +static void sort_by_xb(stud_node *&p, int r, int d) // 指针的引用 +{ + stud_node *head[MAXR], *tail[MAXR], *t; // 定义各链队的首尾指针 + int j; // 循环变量 + int k; // 区分链队 女:k=0,男:k=1 + + printf("按性别排序\n"); + for(j = 0; j < r; j++) // 初始化各链队首、尾指针 + { + head[j] = tail[j] = NULL; + } + + while(p != NULL) // 对于原链表中每个结点循环 + { + if(p->xb == 'f') // 找第k个链队 + { + k = 0; + } + else + { + k = 1; + } + + if(head[k] == NULL) // 进行分配,即采用尾插法建立单链表 + { + head[k] = p; + tail[k] = p; + } + else + { + tail[k]->next = p; + tail[k] = p; + } + p = p->next; // 取下一个待排序的元素 + } + + p = NULL; + for(j = 0; j < r; j++) // 对每一个链队循环 + { + if(head[j] != NULL) + { + if(p == NULL) + { + p = head[j]; + t = head[j]; + } + else + { + t->next = head[j]; + t = tail[j]; + } + } + } + t->next = NULL; // 最后一个结点的next域置为NULL + disp_link(p); // 输出单链表 +} + +static void sort_by_bh(stud_node *&p, int r, int d) // 指针的引用 +{ + stud_node *head[MAXR], *tail[MAXR], *t; // 定义各链队的首尾指针 + int i, j; // 循环变量 + int k; // k区分链队 + + printf("按班号排序\n"); + for(i = 3; i >= 2; i--) // 从低位到高位做d趟排序 + { + for(j = 0; j < r; j++) // 初始化各链队首尾指针 + { + head[j] = tail[j] = NULL; + } + while(p != NULL) // 对于原链表中每个结点循环 + { + k = p->bh[i] - '0'; // 找第k个链队 + if(head[k] == NULL) // 进行分配,即采用尾插法建立单链表 + { + head[k] = p; + tail[k] = p; + } + else + { + tail[k]->next = p; + tail[k] = p; + } + + p = p->next; // 取下一个待排序的元素 + } + p = NULL; + for(j = 0; j < r; j++) // 对每一个链队循环 + { + if(head[j] != NULL) + { + if(p == NULL) + { + p = head[j]; + t = tail[j]; + } + else + { + t->next = head[j]; + t = tail[j]; + } + } + } + t->next = NULL; // 最后一个结点的next域置为NULL + printf(" 第%d趟:\n", d - i + 2); + disp_link(p); // 输出单链表 + } +} + +static void sort_stus(stud_type students[], int n) +{ + stud_node *p; + + create_link(p, students, n); + printf("排序前:\n"); + disp_link(p); + sort_by_xb(p, 2, 1); // 按照性别排序 + sort_by_bh(p, 10, 2); // 按照班号的后两位排序 + printf("排序后:\n"); + disp_link(p); + destroy_link(p); +} + +int main(void) +{ + int n = 10; + // 定义结构体数组并初始化 + stud_type students[MAX_SIZE] = { + {"王华", 'm', "1003"}, {"陈兵", 'm', "1020"}, + {"许可", 'f', "1022"}, {"李英", 'f', "1003"}, + {"张冠", 'm', "1021"}, {"陈强", 'm', "1002"}, + {"李真", 'f', "1002"}, {"章华", 'm', "1001"}, + {"刘丽", 'f', "1021"}, {"王强", 'm', "1022"}, + }; + + sort_stus(students, n); + + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter10/\345\256\236\347\216\260\345\277\253\351\200\237\346\216\222\345\272\217\347\256\227\346\263\225.c" "b/2224020150/chapter10/\345\256\236\347\216\260\345\277\253\351\200\237\346\216\222\345\272\217\347\256\227\346\263\225.c" new file mode 100644 index 0000000000000000000000000000000000000000..fe633974571a7437197215f8b5e7059328bbc97d --- /dev/null +++ "b/2224020150/chapter10/\345\256\236\347\216\260\345\277\253\351\200\237\346\216\222\345\272\217\347\256\227\346\263\225.c" @@ -0,0 +1,108 @@ +#include +#include + +void PrintArray(int* a, int n)//把数组中的元素打印出来 +{ + for (int i = 0; i < n; ++i) + { + printf("%d ", a[i]); + } + printf("\n"); +} +void Swap(int* p1, int* p2)//交换两个值 +{ + int tmp = *p1; + *p1 = *p2; + *p2 = tmp; +} +//挖坑法 +int PartSort1(int*a,int left,int right) +{ + int begin = left; + int end = right; + int key = a[left]; + int pivot = left; + while (begin < end) + { + //找小 + while (begin < end && a[end] >= key) + { + end--; + } + a[pivot] = a[end]; + pivot = end; + //找大 + while (begin < end && a[begin] <= key) + { + begin++; + } + a[pivot] = a[begin]; + pivot = begin; + } + pivot = begin; + a[pivot] = key; + + return pivot; +} +//左右指针法 +int PartSort2(int* a, int left, int right) +{ + int begin = left; + int end = right; + int key = a[left]; + while (begin < end) + { + //找小 + while (begin < end && a[end] >= key) + { + end--; + } + //找大 + while (begin < end && a[begin] <= key) + { + begin++; + } + Swap(&a[begin], &a[end]); + } + Swap(&a[begin], &a[left]); + return begin; +} +//前后指针法 +int PartSort3(int* a, int left, int right) +{ + int prev = left, cur = left + 1; + int key = a[left]; + while (cur <= right) + { + if (a[cur] < key && ++prev != cur) + { + Swap(&a[cur], &a[prev]); + } + cur++; + } + Swap(&a[left], &a[prev]); + return prev; +} +void QuickSort(int* a, int left,int right) +{ + if (left >= right) + { + return; + } + int keyindex = PartSort1(a, left, right); + //int keyindex = PartSort2(a, left, right);//左右指针法 + //int keyindex = PartSort3(a, left, right);//前后指针法 + QuickSort(a, left, keyindex - 1); + QuickSort(a, keyindex + 1, right); +} +void TestQuickSort() +{ + int a[] = { 6,1,2,7,9,3,4,5,10,8 }; + QuickSort(a, 0,sizeof(a) / sizeof(int)-1); + PrintArray(a, sizeof(a) / sizeof(int)); +} +int main() +{ + TestQuickSort(); + return 0; +} diff --git "a/2224020150/chapter2/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.c" "b/2224020150/chapter2/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.c" new file mode 100644 index 0000000000000000000000000000000000000000..489723b5509d7859e0a567bf51830b2ec3f96ec3 --- /dev/null +++ "b/2224020150/chapter2/\345\220\210\345\271\266\344\270\244\344\270\252\346\234\211\345\272\217\346\225\260\347\273\204.c" @@ -0,0 +1,35 @@ +#include +#include +int main() +{ + int a[5]={1,3,5,7,9}; + int b[5]={0,2,4,6,8}; + int c[10]; + int i,j,temp; + for (i=0;i<5;i++) + { + c[i]=a[i]; + } + for (j=0;j<5;j++,i++) + { + c[i]=b[j]; + } + for (i=0;i<10;i++) + { + for (j=0;j<9-i;j++) + { + if(c[j]>c[j+1]) + { + temp=c[j]; + c[j]=c[j+1]; + c[j+1]=temp; + } + } + } + for(i=0;i<10;i++) + { + printf("%d",c[i]); + } + system("pause"); + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter2/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.c" "b/2224020150/chapter2/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.c" new file mode 100644 index 0000000000000000000000000000000000000000..e449fd0df4ae21777251c621a1ff58689e6bf2a3 --- /dev/null +++ "b/2224020150/chapter2/\345\256\236\347\216\260\344\270\244\344\270\252\345\244\232\351\241\271\345\274\217\347\233\270\344\271\230\347\232\204\350\277\220\347\256\227.c" @@ -0,0 +1,155 @@ +#include +#include + +typedef struct PolyNode* Polynomial; +struct PolyNode +{ + int coef; + int expon; + Polynomial link; +}; +void attach(int c, int e, Polynomial* pRear) +{ + Polynomial P = (Polynomial)malloc(sizeof(struct PolyNode)); + P->coef = c; + P->expon = e; + P->link = NULL; + (*pRear)->link = P; + *pRear = P; +} +Polynomial readPoly() +{ + int num1 = 0, c = 0, e = 0; + printf("请输入多项式的系数:"); + scanf("%d", &num1); + Polynomial Head = (Polynomial)malloc(sizeof(struct PolyNode)); //构造一个空的头节点 + Polynomial Rear = Head; + Head->link = NULL; + while (num1--) + { + scanf("%d %d", &c, &e); + attach(c, e, &Rear); + } + Polynomial T = Head; + Head = Head->link; + free(T); + return Head; +} +int compare(int a, int b) +{ + if (a > b) return 1; + else if (a == b) return 0; + else return -1; +} +Polynomial polyAdd(Polynomial P1, Polynomial P2) +{ + Polynomial Head, Rear, T; + Head = (Polynomial)malloc(sizeof(struct PolyNode)); + Head->link = NULL; + Rear = Head; + //比较指数大小,指数大的加入结果多项式,然后指针后移。 + int sum = 0; + while (P1 && P2) + { + switch (compare(P1->expon, P2->expon)) + { + case 1: + attach(P1->coef, P1->expon, &Rear); + P1 = P1->link; + break; + case 0: + sum = P1->coef + P2->coef; + if (sum) attach(sum, P1->expon, &Rear); + P1 = P1->link; P2 = P2->link; + break; + case -1: + attach(P2->coef, P2->expon, &Rear); + P2 = P2->link; + break; + } + } + for (; P1; P1 = P1->link) attach(P1->coef, P1->expon, &Rear); + for (; P2; P2 = P2->link) attach(P2->coef, P2->expon, &Rear); + T = Head; + Head = Head->link; + free(T); + return Head; +} +void printPoly(Polynomial P) +{ + int flag = 0; + while (P) + { + if (flag) + { + printf(" "); + } + else + { + flag = 1; + } + printf("%d %d", P->coef, P->expon); + P = P->link; + } +} +Polynomial polyMulti(Polynomial P1, Polynomial P2) +{ + Polynomial T1, T2, Head, Rear, P; //需要使用多项式首元素的指针,因此设置了T1,T2 + Head = (Polynomial)malloc(sizeof(struct PolyNode)); + Head->link = NULL; + Rear = Head; + T1 = P1; + while (T1) + { + T2 = P2; Rear = Head; + while (T2) + { + //进行排序插入 + int c = T1->coef * T2->coef; + int e = T1->expon + T2->expon; + while (Rear->link && Rear->link->expon > e) + { + Rear = Rear->link; + } + if (Rear->link && Rear->link->expon == e) + { + if (c + Rear->link->coef) Rear->link->coef = c + Rear->link->coef; + else + { + Polynomial tmp = Rear->link; + Rear->link = tmp->link; + free(tmp); + } + } + else + { + P = (Polynomial)malloc(sizeof(struct PolyNode)); + P->coef = c; + P->expon = e; + P->link = Rear->link; + Rear->link = P; + Rear = Rear->link; + } + T2 = T2->link; + } + T1 = T1->link; + } + Polynomial ttt = Head; + Head = ttt->link; + free(ttt); + return Head; +} +int main() +{ + //输入多项式1 && 输入多项式2 + Polynomial P1 = readPoly(); + Polynomial P2 = readPoly(); + //计算多项式和并输出 + Polynomial P3 = polyAdd(P1, P2); + printPoly(P3); + //计算多项式乘积并输出 + Polynomial P4 = polyMulti(P1, P2); + printf("\n"); + printPoly(P4); + return 0; +} diff --git "a/2224020150/chapter2/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.c" "b/2224020150/chapter2/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.c" new file mode 100644 index 0000000000000000000000000000000000000000..e3ac531504e9e937f6bd352f63cba1207d61dd71 --- /dev/null +++ "b/2224020150/chapter2/\345\256\236\347\216\260\345\215\225\351\223\276\350\241\250\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.c" @@ -0,0 +1,160 @@ +#include +#include +typedef char ElemType; +typedef struct LNode +{ + ElemType data; + struct LNode *next; +}LinkNode; +void InitList(LinkNode *&L) +{ + L=(LinkNode *)malloc(sizeof(LinkNode)); + L->next=NULL; +} +bool ListInsert(LinkNode *&L,int i,ElemType e) +{ + int j=0; + LinkNode *p=L,*s; + if(i<=0) + return false; + while(jnext; + } + if(p==NULL) + return false; + else + { + s=(LinkNode *)malloc(sizeof(LinkNode)); + s->data=e; + s->next=p->next; + p->next=s; + return true; + } +} +void DispList(LinkNode *L) +{ + LinkNode *p=L->next; + while(p!=NULL) + { + printf("%c",p->data); + p=p->next; + } + printf("\n"); +} +int ListLength(LinkNode *L) +{ + int n=0; + LinkNode *p=L; + while (p->next!=NULL) + { + n++; + p=p->next; + } + return (n); +} +bool ListEmpty(LinkNode *L) +{ + return (L->next==NULL); +} +bool GetElem(LinkNode *L,int i,ElemType &e) +{ + int j=0; + LinkNode *p=L; + if(i<=0) + return false; + while(jnext; + } + if(p==NULL) + return false; + else + { + e=p->data; + return true; + } +} +int LocateElem(LinkNode *L,ElemType e) +{ + int i=1; + LinkNode *p=L->next; + while(p!=NULL&&p->data!=e) + { + p=p->next; + i++; + } + if(p==NULL) + return (0); + else + return (i); +} +bool ListDelete(LinkNode *&L,int i,ElemType &e) +{ + int j=0; + LinkNode *p=L,*q; + if(i<=0) + return false; + while(jnext; + } + if(p==NULL) + return false; + else + { + q=p->next; + if(q==NULL) + return false; + e=q->data; + p->next=q->next; + free(q); + return true; + } +} +void DestroyList(LinkNode *&L) +{ + LinkNode *pre=L,*p=L->next; + while(p!=NULL) + { + free(pre); + pre=p; + p=pre->next; + } + free(pre); +} + +int main() +{ + LinkNode *h; + ElemType e; + printf("单链表的基本运算如下:\n"); + printf("(1)初始化单链表h.\n"); + InitList(h); + printf("(2)依次采用尾插法插入a,b,c,d,e元素.\n"); + ListInsert(h,1,'a'); + ListInsert(h,2,'b'); + ListInsert(h,3,'c'); + ListInsert(h,4,'d'); + ListInsert(h,5,'e'); + printf("(3)输出单链表h:"); + DispList(h); + printf("(4)输出单链表h的长度:%d\n",ListLength(h)); + printf("(5)判断单链表h是否为空:%s\n",(ListEmpty(h)?"空":"非空")); + GetElem(h,3,e); + printf("(6)输出单链表h的第3个元素:%c\n",e); + printf("(7)输出元素a的位置:%d\n",LocateElem(h,'a')); + printf("(8)在第4个元素位置上插入f元素.\n"); + ListInsert(h,4,'f'); + printf("(9)输出单链表h:"); + DispList(h); + printf("(10)删除单链表h的第3个元素.\n"); + ListDelete(h,3,e); + printf("(11)输出单链表h:"); + DispList(h); + printf("(12)释放单链表h\n"); + DestroyList(h); +} diff --git "a/2224020150/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.c" "b/2224020150/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.c" new file mode 100644 index 0000000000000000000000000000000000000000..94619810eec770872a35d0cfdd3c89ee39106380 --- /dev/null +++ "b/2224020150/chapter2/\345\260\206\345\215\225\351\223\276\350\241\250\346\214\211\345\237\272\345\207\206\345\210\222\345\210\206.c" @@ -0,0 +1,84 @@ +#include +using namespace std; +class List +{ +private: + typedef struct list + { + int data; + list *next; + }*PList; + PList head, last;//头和尾指针 +public: + + List()//初始化头和尾指针 + { + head = new list; + head->next = NULL; + last = head; + } + void CreatList() + { + int n; + cout << "元素个数为" << endl; + cin >> n; + cout << "输入元素" << endl; + PList temp;//临时结点,用来数据的输入 + + for (int i = 0; i < n; ++i) + { + + temp = new list; + cin>>temp->data; + + last->next = temp; + last = temp; + } + last->next = NULL; + cout << "创建完成" << endl; + } + void OutPut() + { + cout << "结果为" << endl; + for (PList p = head->next; p; p = p->next) + cout << p->data << endl; + } + void Division()//拆分 + { + PList p = head->next; + PList q; + head->next = NULL; + last = head; + cout << "输入拆分的基准" << endl; + int x; + cin >> x; + while (p) + { + if (p->data < x)//头插法 + { + q = p->next; + p->next = head->next; + head->next = p; + if (p->next == NULL)last = p;//第一次插入时记得尾指针要向后移 + p = q;//令p=下一个要拆分的元素 + } + else//尾插法 + { + last->next = p; + last = p; + p = p->next;//因为尾插法p与下一个数据没有断开连接 + } + } + last->next = NULL; + cout << "拆分后结果为" << endl; + OutPut(); + } +}; +int main() +{ + List L; + L.CreatList(); + L.OutPut(); + L.Division(); +} + \ No newline at end of file diff --git "a/2224020150/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.c" "b/2224020150/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.c" new file mode 100644 index 0000000000000000000000000000000000000000..23015efac60efa789f288fe260813067287446fc --- /dev/null +++ "b/2224020150/chapter3/\345\256\236\347\216\260\347\216\257\345\275\242\351\230\237\345\210\227\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.c" @@ -0,0 +1,74 @@ +#include +#include +#define MaxSize 100 +typedef char ElemType; +typedef struct +{ + ElemType data[MaxSize]; + int top; //栈指针 +}SqStack; +void IniStack(SqStack *&s) //初始化 +{ + s=(SqStack *)malloc(sizeof(SqStack)); + s->top=-1; +} +void DestoryStack(SqStack *&s) +{ + free(s); +} +bool StackEmpty(SqStack *s) //判断是否为空 +{ + return (s->top==-1); +} +bool Push(SqStack *&s,ElemType e) +{ + if(s->top==MaxSize-1) //栈满 + return false ; + s->top++; + s->data[s->top]=e; + return true; +} +bool Pop(SqStack *s,ElemType e) +{ + if(s->top==-1) //栈空 + return false; + e=s->data[s->top]; + return true; +} +int main() +{ + ElemType e; + SqStack *q; + printf("环形队列基本运算如下:\n"); + printf("(1)初始化队列q\n"); + IniStack(q); + printf("(2)依次进队列元素a,b,c\n"); + if(!Push(q,'a')) + printf("\t提示:队满,不能进队\n"); + if(!Push(q,'b')) + printf("\t提示:队满,不能进队\n"); + if(!Push(q,'c')) + printf("\t提示:队满,不能进队\n"); + printf("(3)队列为%s\n",(StackEmpty(q)?"空":"非空")); + if(Pop(q,e)==0) + printf("队空,不能出队\n"); + else + printf("(4)出队一个元素%c\n",e); + printf("(5)依次进队列元素d,e,f\n"); + if(!Push(q,'d')) + printf("\t提示:队满,不能进队\n"); + if(!Push(q,'e')) + printf("\t提示:队满,不能进队\n"); + if(!Push(q,'f')) + printf("\t提示:队满,不能进队\n"); + printf("(6)出队序列\n"); + while (!StackEmpty(q)) + { + Pop(q,e); + printf("%c",e); + } + printf("\n"); + printf("(7)释放队列\n"); + DestoryStack(q); + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.c" "b/2224020150/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.c" new file mode 100644 index 0000000000000000000000000000000000000000..751aa37b83b6f5cf1b9cd6c6eaa5fe6b89dad74e --- /dev/null +++ "b/2224020150/chapter3/\345\256\236\347\216\260\351\241\272\345\272\217\346\240\210\347\232\204\345\220\204\347\247\215\345\237\272\346\234\254\350\277\220\347\256\227\347\232\204\347\256\227\346\263\225.c" @@ -0,0 +1,91 @@ +#include +#include +#define MaxSize 100 +typedef char ElemType; +typedef struct +{ + ElemType data[MaxSize]; + int top; +} SqStack; + +/* + 1.初始化栈InitStack(&s),该运算创建一个空栈,由s指向它 +实际上就是分配一个顺序栈空间,并将栈顶指针设置为-1 +*/ +void InitStack(SqStack *&s) +{ + s=(SqStack *)malloc(sizeof(SqStack)); + s->top=-1; +} + +//2.销毁栈DestroyStack(&s) +void DestroyStack(SqStack *&s) +{ + free(s); +} + +//3.判断栈是否为空 StackEmpty(s) +bool StackEmpty(SqStack *s) +{ + return (s->top == -1); +} + +//4.进栈Push(&s,e) +bool Push(SqStack *&s,ElemType e) +{ + if(s->top==MaxSize-1) //栈满的情况,即栈上溢出 + return false; + s->top++; + s->data[s->top]=e; + return true; +} + +/* + 5.出栈Pop(&s,&e),先将栈顶指针top处的元素取出放在e中 +然后将栈顶指针减1。 +*/ +bool Pop(SqStack *&s,ElemType &e) +{ + if(s->top==MaxSize-1) + return false; + e=s->data[s->top]; + s->top--; + return true; +} + +//6.取栈顶元素GetTop(s,&e) +bool GetTop(SqStack *s,ElemType &e) +{ + if(s->top == -1) + return false; + e=s->data[s->top]; + return true; +} +int main() +{ + ElemType e; + SqStack *s; + printf("(1)初始化栈s\n"); + InitStack(s); + printf("(2)判断栈s是否非空:%s\n",(StackEmpty(s)? "是":"否")); + printf("(3)依次进栈元素a、b、c、d、e\n"); + Push(s,'a'); + Push(s,'b'); + Push(s,'c'); + Push(s,'d'); + Push(s,'e'); + printf("(4)判断栈s是否非空:%s\n",((StackEmpty(s))? "是":"否")); + printf("(5)输出出栈序列"); + while(!StackEmpty(s)) //相当于StackEmpty() != NULL + { + Pop(s,e); + printf("%c ",e); + } + printf("\n"); + printf("(6)判断栈s是否非空:%s\n",((StackEmpty(s))? "是":"否")); + GetTop(s,e); + printf("(7)取栈顶元素:%c\n",e); + printf("(8)释放栈"); + DestroyStack(s); + return 0; +} diff --git "a/2224020150/chapter3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.c" "b/2224020150/chapter3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.c" new file mode 100644 index 0000000000000000000000000000000000000000..925fa545e999762ea6d209ff782e0f8127c41c0b --- /dev/null +++ "b/2224020150/chapter3/\347\224\250\346\240\210\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.c" @@ -0,0 +1,180 @@ +#include +#include +#include +#include +#define MaxSize 100000+7 +#define maxsize 20+7 +using namespace std; + +int path[maxsize][maxsize]; +int n; + +int y_pos[maxsize]; +int count=0; +bool judge(int num) +{ + for(int i=0;itop=-1; +} + +void DestroyStack(StType *&s) +{ + free(s); +} + +bool GetTop(StType *&s,Box &e) +{ + if(s->top==-1) + return false; + e=s->data[s->top]; + return true; +} + +bool push(StType *&s,Box e) +{ + if(s->top==MaxSize-1) + return false; + s->top++; + s->data[s->top]=e; + return true; +} + +bool pop(StType *&s,Box &e) +{ + if(s->top==-1) + return false; + e=s->data[s->top]; + s->top--; + return true; +} + +int GetLength(StType *s) +{ + return(s->top+1); +} + +bool EmptyStack(StType *s) +{ + return(s->top==-1); +} + + + +void SetPath(int ex,int ey,int k) +{ + int xi=ex; + int yi=ey; + for(int i=0;i0&&y1>0) + path[--x1][--y1]+=k; + while(x2top;i++) + printf("\t(%d,%d)",s->data[i].x,s->data[i].y); +} + +void SolveQ(int n) +{ + int x1,y1,di; + StType *st; + InitStack(st); + Box e; + e.x=0; + e.y=-1; + push(st,e); + while(!EmptyStack(st)) + { + GetTop(st,e); + x1=e.x; + y1=e.y; + bool find=false; + if(x1==n) + { + printSolution(); + Disp(st); + printf("\n"); + count++; + } + while(y1data[st->top].y=y1; + if(judge(x1)) + { + find=true; + e.x=x1+1; + e.y=-1; + push(st,e); + } + } + if(!find) + { + pop(st,e); + } + //pop(st,e); + } +} + + +int main() +{ + printf("please input n:\n"); + scanf("%d",&n); + memset(path,0,sizeof(path)); + memset(y_pos,0,sizeof(y_pos)); + SolveQ(n); + printf("\n count:%d \n",count); + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter4/\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\351\207\215\345\217\240\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.c" "b/2224020150/chapter4/\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\351\207\215\345\217\240\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.c" new file mode 100644 index 0000000000000000000000000000000000000000..b80aa1392420d4db6d0cfdd566a4215cb3ee338e --- /dev/null +++ "b/2224020150/chapter4/\345\210\251\347\224\250KMP\347\256\227\346\263\225\346\261\202\345\255\220\344\270\262\345\234\250\344\270\273\344\270\262\344\270\255\344\270\215\351\207\215\345\217\240\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.c" @@ -0,0 +1,81 @@ +#include +#include +#include +using namespace std; + +void fail(const char* pat, int* f){ + const int len=strlen(pat); + f[0]=-1; + for (int j=1; j < len; j++){ + if (pat[j] == pat[f[j-1]+1]) + f[j] = f[j-1]+1; + else { + int i=f[j-1]; + while ( i >=0 && pat[j] != pat[i+1] ) + i = f[i]; + if (pat[j] ==pat[i+1]) + f[j] = i+1; + else + f[j] = -1; + } + } +} + +int search(const char* tg, const char *pt, const int* f){ + const int ptLen=strlen(pt); + const int tgLen = strlen(tg); + int tgPos=0,ptPos=0; + for (; tgPos < tgLen && ptPos < ptLen;) { + if (tg[tgPos] == pt[ptPos]){ + ptPos++; + tgPos++; + } + else { + if (ptPos == 0) + tgPos ++; + else + ptPos = f[ptPos-1] + 1; + } + } + return ptPos < ptLen ? -1: tgPos - ptLen; +} + +int count(const char* tg, const char *pt, const int* f){ + const int ptLen=strlen(pt); + const int tgLen = strlen(tg); + int tgPos=0,ptPos=0,count=0; + for (; tgPos < tgLen;) { + if (tg[tgPos] == pt[ptPos]){ + ptPos++; + tgPos++; + } + else { + if (ptPos == 0) + tgPos ++; + else + ptPos = f[ptPos-1] + 1; + } + if (ptPos >= ptLen ){ + count ++; + ptPos=f[ptLen-1] +1; + } + } + return count; +} + +int main() +{ + const char *tag="abcbcbcbc"; + const char *pat="bcbc"; + int len=strlen(pat); + int *f =new int[len]; + fail(pat,f); + cout <<"失效函数位置:\n"; + for(int i=0;i +#include + +typedef struct String { //定义顺序串结构体 + char* data; //数据指针 + int length; //顺序串长度 +}Str; + +void StrAssign(Str&, char*); //生成顺序串 +int SubStr(Str, Str); //BF算法 +void GetNext(Str, int*&); //求next数组 +void GetNextval(Str, int*&); //求nextval数组 +int KMP(Str, Str, int*); //KMP算法 + +int main(int argc, const char* argv[]) +{ + char s[] = "abcabcdabcdeabcdefabcdefg"; + char t[] = "abcdeabcdefab"; + Str S, T; + int i,idx; //记录模式串在目标串中的位置 + int* next = NULL, * nextval = NULL; //next数组,nextval数组 + //建立目标串和模式串 + StrAssign(S, s); + StrAssign(T, t); + printf("简单匹配的结果(从1开始):\n"); + idx = SubStr(S, T); + if (idx != -1) { + printf("匹配成功,位置为%d。\n", idx); + } + else { + printf("匹配失败。\n"); + } + GetNext(T, next); + printf("next数组:\n"); + for (i = 0; i < T.length; i++) { + printf("%d\t", next[i]); + } + printf("\n"); + GetNextval(T, nextval); + printf("nextval数组:\n"); + for (i = 0; i < T.length; i++) { + printf("%d\t", nextval[i]); + } + printf("\n"); + printf("使用KMP的结果(从1开始):\n"); + idx = KMP(S, T, next); + if (idx != -1) { + printf("匹配成功,位置为%d。\n", idx); + } + else { + printf("匹配失败。\n"); + } + printf("使用改进KMP的结果(从1开始):\n"); + idx = KMP(S, T, nextval); + if (idx != -1) { + printf("匹配成功,位置为%d。\n", idx); + } + else { + printf("匹配失败。\n"); + } + return 0; +} +void StrAssign(Str& S, char* s) //生成顺序串 +{ + int i; + for (i = 0; s[i] != '\0'; i++); //计算字符串长度 + S.length = i; //顺序串长度 + S.data = s; //顺序串内容 +} +int SubStr(Str S, Str T) //BF算法 +{ + int i, j; + for (i = 0; i < S.length; i++) { + for (j = 0; j < T.length; j++) { + if (S.data[i + j] != T.data[j]) + break; //当目标串与模式串不匹配时结束内循环,对下一个子串进行匹配 + } + if (j >= T.length) //当模式串全部扫描完成说明模式串已经找到了匹配的位置 + return (i+1); //返回匹配的位置 + } + return -1; +} +void GetNext(Str T, int*& next) +{ + int i, j; + i = 0; + j = -1; + next = (int*)malloc(T.length * sizeof(int)); + while (next == NULL) { //预防空间申请失败 + next = (int*)malloc(T.length * sizeof(int)); + } + next[0] = -1; + while (i < T.length) { + if (j == -1 || T.data[i] == T.data[j]) { + i++; + j++; + next[i] = j; + } + else + j = next[j]; + } +} +void GetNextval(Str T, int*& nextval) +{ + int i, j; + i = 0; + j = -1; + nextval = (int*)malloc(T.length * sizeof(int)); + while (nextval == NULL) { //预防空间申请失败 + nextval = (int*)malloc(T.length * sizeof(int)); + } + nextval[0] = -1; + while (i < T.length) { + if (j == -1 || T.data[i] == T.data[j]) { + i++; + j++; + if (T.data[i] != T.data[j]) + nextval[i] = j; + else + nextval[i] = nextval[j]; + } + else + j = nextval[j]; + } +} +int KMP(Str S, Str T, int* N) +{ + int i, j; + i = j = 0; + while (i < S.length && j < T.length) { + if (j == -1 || S.data[i] == T.data[j]) { + i++; + j++; + } + else + j = N[j]; //i不变,j后退 + } + if (j >= T.length) + return (i+1-T.length); //返回匹配的位置 + else + return -1; +} diff --git "a/2224020150/chapter4/\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.c" "b/2224020150/chapter4/\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.c" new file mode 100644 index 0000000000000000000000000000000000000000..93b7fec5ca9de17fee70c84ae085fa8ba74c4c3e --- /dev/null +++ "b/2224020150/chapter4/\346\261\202\344\270\200\344\270\252\344\270\262\344\270\255\345\207\272\347\216\260\347\232\204\347\254\254\344\270\200\344\270\252\346\234\200\351\225\277\351\207\215\345\244\215\345\255\220\344\270\262.c" @@ -0,0 +1,76 @@ +#include +#define MaxSize 100 +typedef struct { + char data[MaxSize]; //串中字符 + int length; //串长 +} SqString; //声明顺序串类型 + +void init(SqString &s) +{ + s.length=0; +} + +int insert(SqString &s,char a) +{ + if(s.length>=MaxSize) + return 0;//溢出返回 + s.data[s.length++]=a; +} + +void print(SqString s) +{ + for(int i=0;isub.length) + //这里取不取等号决定了是否为第一个最长重复子串 + { + sub.length=length; + loc=i; + } + j+=length; + } + else j++; + } + } + for(i=loc,j=0;i +#include +#include +#include +static bool valid(char *ip, int len) +{ + if (len > 1 && ip[0] == '0') + { + return false; + } + if (len == 3) + { + int n = (ip[0] - '0') * 100 + (ip[1] - '0') * 10 + (ip[2] - '0'); + if (n > 255) + { + return false; + } + } + return true; +} +#define WIDTH 4 +static void dfs(char *s, int start, char *stack, int num, char **results, int *count) +{ + int i, j; + if (num == 4) + { + if (s[start] == '\0') + { + results[*count] = malloc(3 * 4 + 3 + 1); + char *p = results[*count]; + for (j = 0; j < num; j++) + { + char *q = stack + j * WIDTH; + while ((*p++ = *q++) != '\0') + { + } + if (j != 3) + { + *(p - 1) = '.'; + } + } + (*count)++; + } + } + else + { + char *p = stack + num * WIDTH; + char *q = p; + for (i = start; s[i] != '\0' && i < start + 3; i++) + { + *q++ = s[i]; + *q = '\0'; + if (!valid(p, q - p)) + { + return; + } + dfs(s, i + 1, stack, num + 1, results, count); + if (num + 1 < 4) + { + memset(stack + (num + 1) * WIDTH, 0, WIDTH); + } + } + } +} +static char **restoreIpAddresses(char *s, int *returnSize) +{ + int count = 0; + char **results = malloc(100 * sizeof(char *)); + char addr[16] = {'\0'}; + dfs(s, 0, addr, 0, results, &count); + *returnSize = count; + return results; +} +int main(int argc, char **argv) +{ + if (argc != 2) + { + fprintf(stderr, "Usage: ./test num\n"); + exit(-1); + } + int i, count = 0; + char **list = restoreIpAddresses(argv[1], &count); + for (i = 0; i < count; i++) + { + printf("%s\n", list[i]); + } +} \ No newline at end of file diff --git "a/2224020150/chapter5/\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.c" "b/2224020150/chapter5/\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.c" new file mode 100644 index 0000000000000000000000000000000000000000..ec4865e4626f18515104d55f2c991d572b26a447 --- /dev/null +++ "b/2224020150/chapter5/\347\224\250\351\200\222\345\275\222\346\226\271\346\263\225\346\261\202\350\247\243n\347\232\207\345\220\216\351\227\256\351\242\230.c" @@ -0,0 +1,44 @@ +#include +#include +int N;//皇后的数量,以及棋盘的大小 N*N +int queenPos[100];//皇后在棋盘中每一行的列号 +void NQueen(int k)//表示行位置 +{ + int i;//表示列位置 + if(k==N)//当k=n时,就是要摆放最后一行的皇后 + { + for(i=0;i + +void move(int n, char pos1, char pos3) +{ + //打印移动的过程 + // 1代表上面最小的盘子 + // 2代表中间位置的盘子 + // 3代表下面最大的盘子 + printf("盘子%d: 从 %c柱 移动到 %c柱\n", n, pos1, pos3); + +} + +void Hanoi(int n, char pos1, char pos2, char pos3) +{ + //如果是1个盘子,直接从起始柱A移动到目标柱C + if (n == 1) + { + move(n, pos1, pos3); + } + else + { + //如果盘子大于1个,需要把n-1个盘子,从起始柱pos1,通过目标柱pos3,移动到中转柱pos2 + Hanoi(n-1, pos1, pos3, pos2); + + //此时pos1上的n-1个盘子全部移动pos2上去了,那么可以直接把pos1上剩下的1个盘子,直接移动到pos3上 + move(n, pos1, pos3); + + //把pos2剩下的n-1个盘子,通过中转位置pos1,移动到目标位置pos3 + Hanoi(n-1, pos2, pos1, pos3); + } +} + +int main() +{ + //盘子个数 + int m; + printf("input the number of disks:"); + scanf("%d",&m); //盘子的数量 + //起始柱A + char pos1 = 'A'; + //中转柱B + char pos2 = 'B'; + //目标柱C + char pos3 = 'C'; + + printf("移动%d个盘子的步骤如下↓\n", n); + + //汉诺塔函数 + Hanoi(n, pos1, pos2, pos3); + + return 0; +} diff --git "a/2224020150/chapter6/\345\256\236\347\216\260\347\250\200\347\226\217\347\237\251\351\230\265\357\274\210\351\207\207\347\224\250\344\270\211\345\205\203\347\273\204\350\241\250\347\244\272\357\274\211\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.c" "b/2224020150/chapter6/\345\256\236\347\216\260\347\250\200\347\226\217\347\237\251\351\230\265\357\274\210\351\207\207\347\224\250\344\270\211\345\205\203\347\273\204\350\241\250\347\244\272\357\274\211\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.c" new file mode 100644 index 0000000000000000000000000000000000000000..b83c5c31fe8e6e28dba0446b5f2e7db748d87e77 --- /dev/null +++ "b/2224020150/chapter6/\345\256\236\347\216\260\347\250\200\347\226\217\347\237\251\351\230\265\357\274\210\351\207\207\347\224\250\344\270\211\345\205\203\347\273\204\350\241\250\347\244\272\357\274\211\347\232\204\345\237\272\346\234\254\350\277\220\347\256\227.c" @@ -0,0 +1,252 @@ +#include +#include + +#define N 4 +#define MAX_SIZE 100 // 矩阵中非零元素最多个数 + +typedef int ElemType; + +typedef struct +{ + int r; // 行号 + int c; // 列号 + ElemType d; // 元素值 +}TupNode; // 三元组定义 + +typedef struct +{ + int rows; // 行数值 + int cols; // 列数值 + int nums; // 非零元素个数 + TupNode data[MAX_SIZE]; +}TSMatrix; // 三元组顺序表定义 + +/*------------------------产生稀疏矩阵A的三元组表示t----------------------*/ +static void create_matrix(TSMatrix &t, ElemType A[N][N]) +{ + int i, j; + + t.rows = N; // 行数值 + t.cols = N; // 列数值 + t.nums = 0; // 非零元素个数 + for(i = 0; i < N; i++) + { + for(j = 0; j < N; j++) + { + if(A[i][j] != 0) + { + t.data[t.nums].r = i; // 行号 + t.data[t.nums].c = j; // 列号 + t.data[t.nums].d = A[i][j]; // 元素值 + t.nums++; // 非零元素个数增1 + } + } + } +} + +/*------------------------输出三元组表示t----------------------*/ +static void disp_matrix(TSMatrix t) +{ + int i; + + if(t.nums <= 0) + return; + printf("\t%d\t%d\t%d\n", t.rows, t.cols, t.nums); + printf("\t------------------\n"); + for(i = 0; i < t.nums; i++) + printf("\t%d\t%d\t%d\n", t.data[i].r, t.data[i].c, t.data[i].d); +} + +/*------------------------求三元组表示t的转置矩阵tb----------------------*/ +/** +* 转置矩阵: +* 把矩阵A的行换成相应的列,得到的新矩阵称为A的转置矩阵。 +*/ +static void tran_matrix(TSMatrix t, TSMatrix &tb) +{ + int p, v; + int q = 0; // q为tb.data的下标 + + tb.rows = t.cols; // 转置矩阵行数值 + tb.cols = t.rows; // 转置矩阵列数值 + tb.nums = t.nums; // 转置矩阵非零元素个数 + if(t.nums != 0) + { + for(v = 0; v < t.cols; v++) // tb.data[q]中的记录以c域的次序排列 + { + for(p = 0; p < t.nums; p++) // p为t.data的下标 + { + if(t.data[p].c == v) + { + tb.data[q].r = t.data[p].c; // 转置矩阵的行号 + tb.data[q].c = t.data[p].r; // 转置矩阵的列号 + tb.data[q].d = t.data[p].d; // 转置矩阵的元素值 + q++; + } + } + } + } +} + +/*------------------------求c=a+b----------------------*/ +static bool matrix_add(TSMatrix a, TSMatrix b, TSMatrix &c) // 引用类型形参c +{ + int i = 0; // a中非零元素个数索引 + int j = 0; // b中非零元素个数索引 + int k = 0; // c中非零元素个数 + ElemType v; + + if(a.rows != b.rows || a.cols != b.cols) // 行数或列数不等时不能进行相加运算 + return false; + // c的行列数与a的相同 + c.rows = a.rows; + c.cols = a.cols; + while(i < a.nums && j < b.nums) // 处理a和b中的元素(假设 a.nums = 6, b.nums = 4) + { + if(a.data[i].r == b.data[j].r) // a元素的行号等于b元素的行号 + { + if(a.data[i].c < b.data[j].c) // a元素的列号小于b元素的列号 + { + // 将a元素添加到c中 + c.data[k].r = a.data[i].r; + c.data[k].c = a.data[i].c; + c.data[k].d = a.data[i].d; + k++; + i++; + } + else if(a.data[i].c > b.data[j].c) // a元素的列号大于b元素的列号 + { + // 将b元素添加到c中 + c.data[k].r = b.data[j].r; + c.data[k].c = b.data[j].c; + c.data[k].d = b.data[j].d; + k++; + j++; + } + else // a元素的列号等于b元素的列号 + { + v = a.data[i].d + b.data[j].d; + if(v != 0) // 只将不为0的结果添加到c中 + { + c.data[k].r = a.data[i].r; + c.data[k].c = a.data[i].c; + c.data[k].d = v; + k++; + } + i++; + j++; + } + } + else if(a.data[i].r < b.data[j].r) // a元素的行号小于b元素的行号 + { + // 将a元素添加到c中 + c.data[k].r = a.data[i].r; + c.data[k].c = a.data[i].c; + c.data[k].d = a.data[i].d; + k++; + i++; + } + else // a元素的行号大于b元素的行号 + { + // 将b元素添加到c中 + c.data[k].r = b.data[j].r; + c.data[k].c = b.data[j].c; + c.data[k].d = b.data[j].d; + k++; + j++; + } + c.nums = k; + } + + return true; +} + +/*------------------------返回三元组t表示的A[i][j]值----------------------*/ +static int get_value(TSMatrix t, int i, int j) +{ + int k = 0; + + while(k < t.nums && (t.data[k].r != i || t.data[k].c != j)) + k++; + if(k < t.nums) + return t.data[k].d; + else + return 0; +} + +/*------------------------求c=a*b----------------------*/ +static bool matrix_mul(TSMatrix a, TSMatrix b, TSMatrix &c) // 引用类型形参c +{ + int i; + int j; + int k; + int p = 0; // 矩阵c的非零元素个数 + ElemType s; + + if(a.cols != b.rows) // a的列数不等于b的行数时不能进行乘法运算 + return false; + for(i = 0; i < a.rows; i++) // 矩阵c的行数 + { + for(j = 0; j < b.cols; j++) // 矩阵c的列数 + { + s = 0; + for(k = 0; k < a.cols; k++) + { + s = s + get_value(a, i, k) * get_value(b, k, j); // 求三元组元素 + } + if(s != 0) // 产生一个三元组元素 + { + c.data[p].r = i; // 三元组元素的行号 + c.data[p].c = j; // 三元组元素的列号 + c.data[p].d = s; // 三元组元素的元素值 + p++; + } + } + } + + c.rows = a.rows; + c.cols = b.cols; + c.nums = p; // 矩阵c的非零元素个数 + + return true; +} + +int main(void) +{ + ElemType a1[N][N] = { + {1, 0, 3, 0}, + {0, 1, 0, 0}, + {0, 0, 1, 0}, + {0, 0, 1, 1} + }; + ElemType b1[N][N] = { + {3, 0, 0, 0}, + {0, 4, 0, 0}, + {0, 0, 1, 0}, + {0, 0, 0, 2} + }; + + TSMatrix a, b, c; + + create_matrix(a, a1); + create_matrix(b, b1); + printf("a的三元组:\n"); + disp_matrix(a); + printf("b的三元组:\n"); + disp_matrix(b); + printf("a转置为c\n"); + tran_matrix(a, c); + printf("c的三元组:\n"); + disp_matrix(c); + + printf("c=a+b\n"); + matrix_add(a, b, c); + printf("c的三元组:\n"); + disp_matrix(c); + + printf("c=a*b\n"); + matrix_mul(a, b, c); + printf("c的三元组:\n"); + disp_matrix(c); + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter6/\346\261\2025x5\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.c" "b/2224020150/chapter6/\346\261\2025x5\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.c" new file mode 100644 index 0000000000000000000000000000000000000000..b5f18e429a265c91ebc98afbf3a33d4f0d9c61d4 --- /dev/null +++ "b/2224020150/chapter6/\346\261\2025x5\351\230\266\347\232\204\350\236\272\346\227\213\346\226\271\351\230\265.c" @@ -0,0 +1,66 @@ +#include + +#define MAX_LEN 10 + +/*------------------求n阶螺旋方阵a-----------------*/ +static void helix_matrix(int a[MAX_LEN][MAX_LEN], int n) +{ + int m; // 圈数 + int i; + int j; + int k = 0; + + if(n % 2 == 0) // 偶数阶螺旋方阵 + m = n / 2; + else // 奇数阶螺旋方阵 + m = n / 2 + 1; + for(i = 0; i < m; i++) + { + // 产生该圈上横行的数字 + for(j = i; j < n - i; j++) + { + k++; + a[i][j] = k; + } + // 产生该圈右竖行的数字 + for(j = i + 1; j < n - i; j++) + { + k++; + a[j][n - i - 1] = k; + } + // 产生该圈下横行的数字 + for(j = n - i - 2; j >= i; j--) + { + k++; + a[n - i - 1][j] = k; + } + // 产生该圈左竖行的数字 + for(j = n - i - 2; j >= i + 1; j--) + { + k++; + a[j][i] = k; + } + } +} + +int main(void) +{ + int n, i, j; + int a[MAX_LEN][MAX_LEN]; + + printf("输入n(n<10):"); + scanf("%d", &n); + helix_matrix(a, n); + printf("%d阶数字方阵如下:\n", n); + + for(i = 0; i < n; i++) + { + for(j = 0; j < n; j++) + { + printf("%4d", a[i][j]); + } + printf("\n"); + } + + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter6/\346\261\202\344\270\244\344\270\252\345\257\271\347\247\260\347\237\251\351\230\265\344\271\213\345\222\214\344\270\216\344\271\230\347\247\257.c" "b/2224020150/chapter6/\346\261\202\344\270\244\344\270\252\345\257\271\347\247\260\347\237\251\351\230\265\344\271\213\345\222\214\344\270\216\344\271\230\347\247\257.c" new file mode 100644 index 0000000000000000000000000000000000000000..6cf9b6fa4ee6be537ae89e9aae85ae73cecfac7a --- /dev/null +++ "b/2224020150/chapter6/\346\261\202\344\270\244\344\270\252\345\257\271\347\247\260\347\237\251\351\230\265\344\271\213\345\222\214\344\270\216\344\271\230\347\247\257.c" @@ -0,0 +1,99 @@ +#include + +#define N 4 +#define M 10 + +/*-----------------------返回压缩存储a中a[i][j]的值-----------------------*/ + +static int value(int a[], int i, int j) +{ + if(i >= j) + return a[(i * (i + 1)) / 2 + j]; + else + return a[(j * (j + 1)) / 2 + i]; +} + +/*-----------------------求压缩存储a和b的和-----------------------*/ +static void madd(int a[], int b[], int c[][N]) +{ + int i, j; + + for(i = 0; i < N; i++) + { + for(j = 0; j < N; j++) + { + c[i][j] = value(a, i, j) + value(b, i, j); + } + } +} + +/*-----------------------求压缩存储a和b的乘积-----------------------*/ +static void mult(int a[], int b[], int c[][N]) +{ + int i, j, k, sum; + + for(i = 0; i < N; i++) + { + for(j = 0; j < N; j++) + { + sum = 0; + for(k = 0; k < N; k++) + { + sum = sum + value(a, i, k) * value(b, k, j); + } + c[i][j] = sum; + } + } +} + +/*-----------------------输出压缩存储a-----------------------*/ +static void disp1(int a[]) +{ + int i, j; + + for(i = 0; i < N; i++) + { + for(j = 0; j < N; j++) + { + printf("%4d", value(a, i, j)); + } + printf("\n"); + } +} + +/*-----------------------输出对称矩阵c-----------------------*/ +static void disp2(int c[][N]) +{ + int i, j; + + for(i = 0; i < N; i++) + { + for(j = 0; j < N; j++) + { + printf("%4d", c[i][j]); + } + printf("\n"); + } +} + +int main(int argc, char *argv[]) +{ + int a[M] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; //a表示压缩存储的对称矩阵 + int b[M] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; + int c1[N][N], c2[N][N]; + + printf("a矩阵:\n"); + disp1(a); + printf("b矩阵:\n"); + disp1(b); + + madd(a, b, c1); + printf("a+b:\n"); + disp2(c1); + + mult(a, b, c2); + printf("a*b:\n"); + disp2(c2); + + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter7/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.c" "b/2224020150/chapter7/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.c" new file mode 100644 index 0000000000000000000000000000000000000000..86d1483295dbb00a4105eb0dd273207401171028 --- /dev/null +++ "b/2224020150/chapter7/\345\244\247\345\255\246\347\232\204\346\225\260\346\215\256\347\273\237\350\256\241.c" @@ -0,0 +1,204 @@ +#include +#include +#define MaxSize 66 +#include +#include +#define MaxSons 3 +typedef char ElemType; +typedef struct node +{ + ElemType data[15]; //结点的值 + struct node *sons[MaxSons]; //指向孩子结点 +}TSonNode; //孩子链存储结构中的结点类型 +typedef struct +{ + char N[15]; + char n[15]; +}array; +//读取文件内容到数组R中 +void ReadFile(array R[],FILE *fp,int &n) +{ + while((fscanf(fp,"%s",R[n].N))!=EOF&&(fscanf(fp,"%s",R[n].n))!=EOF) + n++; +} +//创建一颗树 +TSonNode *CreateTree(char str[],array R[],int n) +{ + TSonNode *t; + int k,i=0,j=0; + t=(TSonNode *)malloc(sizeof(TSonNode)); + strcpy(t->data,str); + for(k=0;ksons[k]=NULL; + while(isons[j]=CreateTree(R[i].n,R,n); + j++; + } + i++; + } + return t; +} +//输出树(孩子链存储结构) +void DispTree(TSonNode *t) +{ + int i=0; + if(t==NULL) + printf("此树为空树!\n"); + else + { + printf("%s",t->data); + if(t->sons[i]!=NULL) //若t结点至少有一个孩子 + { + printf("("); + for(i=0;isons[i]); + if(t->sons[i+1]!=NULL) + printf(","); + else + break; + } + printf(")"); + } + } +} +//销毁树 +void DestroyTree(TSonNode *t) +{ + if(t==NULL) + printf("此树为空树!\n"); + else + { + for(int i=0;isons[i]!=NULL) + DestroyTree(t->sons[i]); + else + break; + } + free(t); + } +} +//查找某一结点 +TSonNode *FindNode(TSonNode *t,char str[]) +{ + TSonNode *p; + if(t==NULL) + return NULL; + else + { + if(strcmp(t->data,str)==0) + return t; + else + { + for(int i=0;isons[i]!=NULL) + { + p=FindNode(t->sons[i],str); + if(p!=NULL) + return p; + } + } + return NULL; + } + } +} +//求某一结点的孩子个数 +int ChildCount(TSonNode *p) +{ + int count=0; + for(int i=0;isons[i]!=NULL) + count++; + else + break; + } + return count; +} +//求某棵树中的叶子结点数 +//本例中,叶子结点数等于班级数,一个叶子结点对应一个班级 +int LeafCount(TSonNode *p) +{ + int count=0; + if(p==NULL) + return 0; + else + { + if(p->sons[0]==NULL) + count++; + else + { + for(int i=0;isons[i]!=NULL) + count=count+LeafCount(p->sons[i]); + else + break; + } + } + } + return count; +} +//求某棵树的叶子结点值的和 +int LeafSumOfvalue(TSonNode *p) +{ + int sum=0; + if(p==NULL) + return 0; + else + { + if(p->sons[0]==NULL) + return atoi(p->data); + else + { + for(int i=0;isons[i]!=NULL) + sum+=LeafSumOfvalue(p->sons[i]); + else + break; + } + } + } + return sum; +} + + +int main() +{ + int n=0; + TSonNode *t; + array R[MaxSize]; + FILE *fp; + if((fp=fopen("abc1.txt","r"))==NULL) //以只读方式打开table.txt文件 + { + printf("error!cannot open the file!"); + exit(1); + } + printf("读取文件内容存入数组R中\n"); + ReadFile(R,fp,n); + printf("输出数组R:\n"); + for(int i=0;i +#include +#define MaxSize 100 +typedef char ElemType; +typedef struct node +{ + ElemType data; + struct node *lchild; + struct node *rchild; + +}BTNode; +void CreateBTree(BTNode *&b,char *str) +{ + BTNode * St[MaxSize],*p; + int top=-1,k,j=0;char ch; + b=NULL; + ch=str[j]; + while(ch!='\0') + { + switch(ch) + { + case'(':top++;St[top]=p;k=1;break; + case')':top--;break; + case',':k=2;break; + + default:p=(BTNode *)malloc(sizeof(BTNode)); + p->data=ch;p->lchild=p->rchild=NULL; + if(b==NULL) + b=p; + else + { + switch(k) + { + case 1:St[top]->lchild=p;break; + case 2:St[top]->rchild=p;break; + } + } + } + j++;ch=str[j]; + } +} +void DestroyBTree(BTNode *&b) +{ + if(b!=NULL) + { + DestroyBTree(b->lchild); + DestroyBTree(b->rchild); + free(b); + } +} +BTNode *FindNode(BTNode *b,ElemType x) +{ + BTNode *p; + if(b==NULL) + return NULL; + else if(b->data==x) + return b; + else + { + p=FindNode(b->lchild,x); + if(p!=NULL) + return p; + else + return FindNode(b->rchild,x); + } +} +BTNode *LchildNode(BTNode *p) +{ + return p->lchild; + +} +BTNode *RchildNode(BTNode *p) +{ + return p->rchild; +} +int BTHeight(BTNode *b) +{ + int lchildh,rchildh; + if(b==NULL)return(0); + else + { + lchildh=BTHeight(b->lchild); + rchildh=BTHeight(b->rchild); + return(lchildh>rchildh)?(lchildh+1):(rchildh+1); + } +} +void DispBTree(BTNode *b) +{ + if(b!=NULL) + { + printf("%c",b->data); + if(b->lchild!=NULL||b->rchild!=NULL) + { + printf("("); + DispBTree(b->lchild); + if(b->rchild!=NULL)printf(","); + DispBTree(b->rchild); + printf(")"); + } + } +} +int main() +{ + BTNode *b,*p,*lp,*rp;; + printf("二叉树的基本运算如下:\n"); + printf(" (1)创建二叉树\n"); + CreateBTree(b,"A(B(D,E(H(J,k(L,M(,N))))),C(F,G(,I)))"); + printf(" (2)输出二叉树:");DispBTree(b);printf("\n"); + printf(" (3)H结点:"); + p=FindNode(b,'H'); + if(p!=NULL) + { lp= LchildNode(p); + if(lp!=NULL)printf("左孩子为%c",lp->data); + else printf("无左孩子"); + rp=RchildNode(p); + if(rp!=NULL) printf("右孩子为%c",rp->data); + else printf("无右孩子"); + + } + printf("\n"); + printf(" (4)二叉树b的高度:%d\n",BTHeight(b)); + printf(" (5)释放二叉树b\n"); + DestroyBTree(b); + return 1; +} diff --git "a/2224020150/chapter7/\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.c" "b/2224020150/chapter7/\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.c" new file mode 100644 index 0000000000000000000000000000000000000000..f911bb547dc4a1db423158785f1473e19285a056 --- /dev/null +++ "b/2224020150/chapter7/\346\236\204\351\200\240\345\223\210\345\244\253\346\233\274\346\240\221\345\222\214\347\224\237\346\210\220\345\223\210\345\244\253\346\233\274\347\274\226\347\240\201.c" @@ -0,0 +1,138 @@ +#include +#include +#include + +typedef double DataType; //结点权值的数据类型 + +typedef struct HTNode //单个结点的信息 +{ + DataType weight; //权值 + int parent; //父节点 + int lc, rc; //左右孩子 +}*HuffmanTree; + +typedef char **HuffmanCode; //字符指针数组中存储的元素类型 + +//在下标为1到i-1的范围找到权值最小的两个值的下标,其中s1的权值小于s2的权值 +void Select(HuffmanTree& HT, int n, int& s1, int& s2) +{ + int min; + //找第一个最小值 + for (int i = 1; i <= n; i++) + { + if (HT[i].parent == 0) + { + min = i; + break; + } + } + for (int i = min + 1; i <= n; i++) + { + if (HT[i].parent == 0 && HT[i].weight < HT[min].weight) + min = i; + } + s1 = min; //第一个最小值给s1 + //找第二个最小值 + for (int i = 1; i <= n; i++) + { + if (HT[i].parent == 0 && i != s1) + { + min = i; + break; + } + } + for (int i = min + 1; i <= n; i++) + { + if (HT[i].parent == 0 && HT[i].weight < HT[min].weight&&i != s1) + min = i; + } + s2 = min; //第二个最小值给s2 +} + +//构建哈夫曼树 +void CreateHuff(HuffmanTree& HT, DataType* w, int n) +{ + int m = 2 * n - 1; //哈夫曼树总结点数 + HT = (HuffmanTree)calloc(m + 1, sizeof(HTNode)); //开m+1个HTNode,因为下标为0的HTNode不存储数据 + for (int i = 1; i <= n; i++) + { + HT[i].weight = w[i - 1]; //赋权值给n个叶子结点 + } + for (int i = n + 1; i <= m; i++) //构建哈夫曼树 + { + //选择权值最小的s1和s2,生成它们的父结点 + int s1, s2; + Select(HT, i - 1, s1, s2); //在下标为1到i-1的范围找到权值最小的两个值的下标,其中s1的权值小于s2的权值 + HT[i].weight = HT[s1].weight + HT[s2].weight; //i的权重是s1和s2的权重之和 + HT[s1].parent = i; //s1的父亲是i + HT[s2].parent = i; //s2的父亲是i + HT[i].lc = s1; //左孩子是s1 + HT[i].rc = s2; //右孩子是s2 + } + //打印哈夫曼树中各结点之间的关系 + printf("哈夫曼树为:>\n"); + printf("下标 权值 父结点 左孩子 右孩子\n"); + printf("0 \n"); + for (int i = 1; i <= m; i++) + { + printf("%-4d %-6.2lf %-6d %-6d %-6d\n", i, HT[i].weight, HT[i].parent, HT[i].lc, HT[i].rc); + } + printf("\n"); +} + +//生成哈夫曼编码 +void HuffCoding(HuffmanTree& HT, HuffmanCode& HC, int n) +{ + HC = (HuffmanCode)malloc(sizeof(char*)*(n + 1)); //开n+1个空间,因为下标为0的空间不用 + char* code = (char*)malloc(sizeof(char)*n); //辅助空间,编码最长为n(最长时,前n-1个用于存储数据,最后1个用于存放'\0') + code[n - 1] = '\0'; //辅助空间最后一个位置为'\0' + for (int i = 1; i <= n; i++) + { + int start = n - 1; //每次生成数据的哈夫曼编码之前,先将start指针指向'\0' + int c = i; //正在进行的第i个数据的编码 + int p = HT[c].parent; //找到该数据的父结点 + while (p) //直到父结点为0,即父结点为根结点时,停止 + { + if (HT[p].lc == c) //如果该结点是其父结点的左孩子,则编码为0,否则为1 + code[--start] = '0'; + else + code[--start] = '1'; + c = p; //继续往上进行编码 + p = HT[c].parent; //c的父结点 + } + HC[i] = (char*)malloc(sizeof(char)*(n - start)); //开辟用于存储编码的内存空间 + strcpy(HC[i], &code[start]); //将编码拷贝到字符指针数组中的相应位置 + } + free(code); //释放辅助空间 +} + +//主函数 +int main() +{ + int n = 0; + printf("请输入数据个数:>"); + scanf("%d", &n); + DataType* w = (DataType*)malloc(sizeof(DataType)*n); + if (w == NULL) + { + printf("malloc fail\n"); + exit(-1); + } + printf("请输入数据:>"); + for (int i = 0; i < n; i++) + { + scanf("%lf", &w[i]); + } + HuffmanTree HT; + CreateHuff(HT, w, n); //构建哈夫曼树 + + HuffmanCode HC; + HuffCoding(HT, HC, n); //构建哈夫曼编码 + + for (int i = 1; i <= n; i++) //打印哈夫曼编码 + { + printf("数据%.2lf的编码为:%s\n", HT[i].weight, HC[i]); + } + free(w); + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter7/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.c" "b/2224020150/chapter7/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.c" new file mode 100644 index 0000000000000000000000000000000000000000..e6d0bb14bb7ea1079df26f461351bd73bcfaf57a --- /dev/null +++ "b/2224020150/chapter7/\346\261\202\344\272\214\345\217\211\346\240\221\344\270\255\344\273\216\346\240\271\347\273\223\347\202\271\345\210\260\345\217\266\345\255\220\347\273\223\347\202\271\347\232\204\350\267\257\345\276\204.c" @@ -0,0 +1,322 @@ +#include +#include +#include + +#define MAX_SIZE 100 + +typedef char ElemType; +typedef struct node +{ + ElemType data; // 数据元素 + struct node *lchild; // 指向左孩子结点 + struct node *rchild; // 指向右孩子结点 +}BTNode; // 声明二叉链结点类型 + +/*-------------由括号表示串str创建二叉链b-----------------*/ +static void create_btree(BTNode *&b, char *str) // 创建二叉树(形参b:指针的引用) +{ + BTNode *p; + BTNode *St[MAX_SIZE]; // 定义一个顺序栈 + int k; + int j = 0; + int top = -1; // 栈顶指针初始化 + char ch; + + b = NULL; // 建立的二叉树初始时为空 + ch = str[j]; // 取第一个字符 + while(ch != '\0') // str未扫描完时循环 + { + switch(ch) + { + case '(': // 开始处理左子树 + top++; + St[top] = p; + k = 1; + break; + case ')': // 子树处理完毕 + top--; + break; + case ',': // 开始处理右子树 + k = 2; + break; + default: + p = (BTNode *)malloc(sizeof(BTNode)); // 动态分配结点p的存储空间 + p->data = ch; + p->lchild = p->rchild = NULL; + if(b == NULL) // 若b为空,p置为二叉树的根结点 + b = p; + else // 已建立二叉树根结点 + { + switch(k) + { + case 1: + St[top]->lchild = p; + break; + case 2: + St[top]->rchild = p; + break; + } + } + break; + } + // 取下一个字符 + j++; + ch = str[j]; + } +} + +/*--------------------------以括号表示法输出二叉树b----------------------*/ +// "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))" +static void disp_btree(BTNode *b) +{ + if(b != NULL) + { + printf("%c", b->data); + if(b->lchild != NULL || b->rchild != NULL) + { + printf("("); // 有孩子结点时才输出( + disp_btree(b->lchild); // 递归处理左子树 + if(b->rchild != NULL) // 有右孩子结点时才输出, + printf(","); + disp_btree(b->rchild); // 递归处理右子树 + printf(")"); // 有孩子结点时才输出) + } + } +} + +/*--------------------------释放二叉树b的所有结点----------------------*/ +static void destroy_btree(BTNode *&b) // 销毁二叉树(形参b:指针的引用) +{ + if(b != NULL) + { + destroy_btree(b->lchild); + destroy_btree(b->rchild); + free(b); + } +} + +/*--------------------------返回b结点的左孩子结点指针----------------------*/ +static BTNode *left_child_node(BTNode *b) +{ + return b->lchild; +} + +/*--------------------------返回b结点的右孩子结点指针----------------------*/ +static BTNode *right_child_node(BTNode *b) +{ + return b->rchild; +} + +/*--------------------------返回data域为x的结点指针----------------------*/ +static BTNode *find_node(BTNode *b, ElemType x) // 查找值为x的结点 +{ + BTNode *p; + + if(b == NULL) + return NULL; + else if(b->data == x) + return b; + else + { + p = find_node(b->lchild, x); + if(p != NULL) + return p; + else + return find_node(b->rchild, x); + } +} + +/*--------------------------求二叉树b的深度----------------------*/ +static int btree_height(BTNode *b) +{ + int left_child_height, right_child_height; + + if(b == NULL) // 空树的深度为0 + return 0; + else + { + // 求左子树的深度 + left_child_height = btree_height(b->lchild); + // 求右子树的深度 + right_child_height = btree_height(b->rchild); + + return (left_child_height > right_child_height) ? (left_child_height + 1) : (right_child_height + 1); + } +} + +/*--------------------------采用先序遍历方法输出所有从叶子结点到根结点的逆路径----------------------*/ +static void all_path1(BTNode *b, ElemType path[], int path_len) +{ + int i; + + if(b == NULL) + return; + + if(b->lchild == NULL && b->rchild == NULL) // b为叶子结点 + { + printf(" %c到根结点逆路径: %c->", b->data, b->data); + for(i = path_len - 1; i > 0; i--) // 逆序输出 + printf("%c->", path[i]); + printf("%c\n", path[0]); // 输出根结点A + } + else + { + path[path_len] = b->data; // 将当前结点放入路径中 + path_len++; // 路径长度增1 + all_path1(b->lchild, path, path_len); // 递归扫描左子树 + all_path1(b->rchild, path, path_len); // 递归扫描右子树 + } +} + +/*--------------------------采用先序遍历方法输出第一条最长的逆路径----------------------*/ +static void long_path1(BTNode *b, ElemType path[], int path_len, ElemType long_path[], int &long_path_len) // 引用long_path_len带回最长路径长度 +{ + int i; + + if(b == NULL) + { + if(path_len > long_path_len) // 若当前路径更长,将路径保存在long_path中 + { + for(i = path_len - 1; i >= 0; i--) // 逆序保存到long_path + long_path[i] = path[i]; + long_path_len = path_len; // 记录最长路径长度 + } + } + else + { + path[path_len] = b->data; // 将当前结点放入路径中 + //printf("path[%d] = %c\n", path_len, path[path_len]); + path_len++; // 路径长度增1 + long_path1(b->lchild, path, path_len, long_path, long_path_len); // 递归扫描左子树 + long_path1(b->rchild, path, path_len, long_path, long_path_len); // 递归扫描右子树 + } +} + +/*--------------------------采用后序(左、右、根)非递归遍历方法输出所有从叶子结点到根结点的逆路径----------------------*/ +static void all_path2(BTNode *b) +{ + BTNode *st[MAX_SIZE]; // 定义一个顺序栈st + int top = -1; // 栈顶指针初始化 + BTNode *p; + BTNode *r; + bool flag; + + p = b; // p->A + do + { + while(p != NULL) // 扫描结点p的所有左下结点并进栈 + { + top++; + st[top] = p; // 结点p进栈 + p = p->lchild; // 移动到左孩子 + } + r = NULL; // r指向刚刚访问的结点,初始时为空 + flag = true; // flag为真表示正在处理栈顶结点 + while(top > -1 && flag) // 栈不空且flag为真时循环 + { + p = st[top]; // 取出当前的栈顶结点p + if(p->rchild == r) // 若结点p的右孩子为空或者为刚刚访问过的结点 + { + if(p->lchild == NULL && p->rchild == NULL) // 若为叶子结点 + { + // 输出栈中所有结点值 + printf(" %c到根结点逆路径: ", p->data); + for(int i = top; i > 0; i--) + printf("%c->", st[i]->data); + // 输出根结点 + printf("%c\n", st[0]->data); + } + top--; // 退栈 + r = p; // r指向刚访问过的结点 + } + else + { + p = p->rchild; // 转向处理其右子树 + flag = false; // 表示当前不是处理栈顶结点 + } + } + }while(top > -1); // 栈不空时循环 +} + +typedef struct snode +{ + BTNode *node; // 存放当前结点指针 + int parent; // 存放双亲结点在队列中的位置 +}snode; // 声明顺序队列结点类型 + +/*--------------------------采用层次遍历方法输出所有从叶子结点到根结点的逆路径----------------------*/ +static void all_path3(BTNode *b) +{ + snode qu[MAX_SIZE]; // 定义顺序队列 + int que_front, que_rear; // 定义队头和队尾指针 + int pos; + + que_front = que_rear = -1; // 设置队列为空队列 + que_rear++; + qu[que_rear].node = b; // 根结点指针进入队列 + qu[que_rear].parent = -1; // 根结点没有双亲结点 + + while(que_front < que_rear) // 队列不为空时循环 + { + que_front++; + b = qu[que_front].node; // 队头出队列 + printf(" 从队头位置que_front = [%d], 出队元素[%c]\n", que_front, b->data); + if(b->lchild == NULL && b->rchild == NULL) // b为叶子结点 + { + /*-----------------输出叶子结点信息---------------------*/ + printf(" %c到根结点逆路径: \n", b->data); + pos = que_front; + printf(" 出队列位置 = %d: \n", pos); + printf(" %c的双亲parent = %d\n", b->data, qu[pos].parent); + /*------------------------------------------------------*/ + while(qu[pos].parent != -1) + { + printf("%c->", qu[pos].node->data); // 输出结点(叶子结点和非根结点) + pos = qu[pos].parent; // 更新pos的值 + } + printf("%c\n", qu[pos].node->data); // 最后输出根结点 + } + if(b->lchild != NULL) // 若有左孩子,将其进入队列 + { + que_rear++; + qu[que_rear].node = b->lchild; + qu[que_rear].parent = que_front; + } + if(b->rchild != NULL) // 若有右孩子,将其进入队列 + { + que_rear++; + qu[que_rear].node = b->rchild; + qu[que_rear].parent = que_front; + } + } +} + +int main(int argc, char *argv[]) +{ + BTNode *b; + ElemType path[MAX_SIZE]; + ElemType long_path[MAX_SIZE]; + int i; + int long_path_len = 0; + + create_btree(b, "A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); + printf("二叉树b: "); + disp_btree(b); + printf("\n"); + printf("先序遍历方法:\n"); + all_path1(b, path, 0); + long_path1(b, path, 0, long_path, long_path_len); + printf(" 第一条最长逆路径长度: %d\n", long_path_len); + printf(" 第一条最长逆路径: "); + for(i = long_path_len - 1; i >= 0; i--) + printf("%c ", long_path[i]); + printf("\n"); + printf("后序非递归遍历方法:\n"); + all_path2(b); + printf("层次遍历方法:\n"); + all_path3(b); + + destroy_btree(b); + + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.c" "b/2224020150/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.c" new file mode 100644 index 0000000000000000000000000000000000000000..a80b4b07e71c0ad40b2983fbf9b4d1ebb5d3d88e --- /dev/null +++ "b/2224020150/chapter8/\345\256\236\347\216\260\345\233\276\347\232\204\351\202\273\346\216\245\347\237\251\351\230\265\345\222\214\351\202\273\346\216\245\350\241\250\345\255\230\345\202\250.c" @@ -0,0 +1,144 @@ +#include +#include +#define INF 32767 //定义∞ +#define Vmax 100 //最大顶点个数 +typedef char VType; //顶点字符型 +typedef int EType; //边,矩阵元素int型 + +typedef struct +{ + VType vexs[Vmax]; //顶点表:存图的所有顶点,比如abcd四个顶点,放入数组中 + EType edges[Vmax][Vmax]; //邻接矩阵—边表 值为0 1整数 + int Vnum, Enum; // 图的顶点数和边数 +}MGraph; +typedef struct node +{ + EType adjvex; //该边的关联点编号 + struct node *next; //指向下一条边的指针 + int weight; //该边的相关信息,如权值(用整型表示) +}EdgeNode;//边结点 +typedef struct +{ + VType vertex; + EdgeNode *firstedge; //指向第一个边结点 +}VertexNode;//顶点结点 +typedef struct +{ + VertexNode vexlist[Vmax];//顶点结点数组 + int vexnum,arcnum;//顶点数、边数 +}AdjGraph;//图的邻接表 +/*创建邻接矩阵*/ +void creatGraph(MGraph &G) +{ + int i,j,from,to; + printf("请输入顶点的个数和边的条数:\n"); + scanf("%d %d",&G.Vnum,&G.Enum); + getchar(); + printf("请输入顶点:\n"); + for(i=0;ivexlist[i].firstedge=NULL; //将邻接表中所有头结点的指针域置为空 + for(i=0;i=0;j--) //检查邻接矩阵中的每个元素 + if(G.edges[i][j]!=INF) + { + p=(EdgeNode *)malloc(sizeof(EdgeNode)); + p->adjvex=j; + p->next=g->vexlist[i].firstedge; //头插法插入结点p + g->vexlist[i].firstedge=p; + } + g->vexnum=G.Vnum; G.Enum=g->arcnum; + //输出邻接表 + printf("图G的邻接表:\n"); + for(i=0;ivexnum;i++) + { + p=g->vexlist[i].firstedge; + printf("%c->",G.vexs[i]); + while(p!=NULL) + { + printf("%d->",p->adjvex); + p=p->next; + } + printf("NULL\n"); + } +} + +void DestroyAdj(AdjGraph *&G) //销毁图的邻接表 +{ int i; + EdgeNode *pre,*p; + for (i=0;ivexnum;i++) //扫描所有的单链表 + { pre=G->vexlist[i].firstedge; //p指向第i个单链表的首结点 + if (pre!=NULL) + { p=pre->next; + while (p!=NULL) //释放第i个单链表的所有边结点 + { free(pre); + pre=p; p=p->next; + } + free(pre); + } + } + free(G); //释放头结点数组 +} + +int main() +{ + MGraph G; + AdjGraph *g; + creatGraph(G); //建立邻接矩阵 + + printf("将g转换为邻接表G\n"); + MatToList(G,g); //输出邻接表G + DestroyAdj(g); //销毁邻接表 +return 1; +} \ No newline at end of file diff --git "a/2224020150/chapter8/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\351\227\256\351\242\230.c" "b/2224020150/chapter8/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\351\227\256\351\242\230.c" new file mode 100644 index 0000000000000000000000000000000000000000..60d12dcd5902ba270080ca5c81781cbc3ef09b0b --- /dev/null +++ "b/2224020150/chapter8/\346\261\202\350\247\243\345\273\272\345\205\254\350\267\257\351\227\256\351\242\230.c" @@ -0,0 +1,56 @@ +#include +#include +#include +#include +#define INF 0x3f3f3f3f +using namespace std; +struct position{ + int x,y; +}room[5001]; +bool vis[5001]; +double low[5001]; +int n; +double len(int i,int j){ + return sqrt((double)(room[i].x-room[j].x)*(room[i].x-room[j].x)+(double)(room[i].y-room[j].y)*(room[i].y-room[j].y)); +} + +void prim(){ + for(int i=1;i<=n;i++){ + low[i]=len(1,i); + //cout<low[j]){ + minn=low[j]; + temp=j; + } + } + //cout<len(temp,j)){ + low[j]=len(temp,j); + } + } + } +} +int main(){ + scanf("%d",&n); + //cout< +#include + +#define INF 32767 //定义∞ +#define MAXV 100 //最大顶点个数 + +typedef char InfoType; +/*-------------------------以下定义邻接矩阵类型---------------------------*/ +typedef struct +{ + int no; //顶点编号 + InfoType info; //顶点信息 +}VertexType; //顶点类型 + +typedef struct +{ + int edges[MAXV][MAXV]; //邻接矩阵数组(用一个二维数组存放顶点间关系(边或弧)的数据) + int n; //顶点数 + int e; //边数 + VertexType vexs[MAXV]; //存放顶点信息(用一个一维数组存放图中所有顶点数据) +}MatGraph; //完整的图邻接矩阵类型 + +//邻接表表示法-将每个顶点的邻接点串成一个单链表 +/*-----------以下定义邻接表类型--------------*/ +typedef struct ArcNode +{ + int adjvex; //该边的邻接点编号 + struct ArcNode *nextarc; //指向下一条边的指针 + int weight; //该边的相关信息,如权值(用整型表示) +}ArcNode; //边结点类型 + +typedef struct VNode +{ + InfoType info; //顶点其他信息 + int cnt; //存放顶点入度,仅用于拓扑排序 + ArcNode *firstarc; //指向第一条边 +}VNode; //邻接表结点类型 + +typedef struct +{ + VNode adjlist[MAXV]; //邻接表头结点数组 + int n; //图中顶点数 + int e; //图中边数 +}AdjGraph; //完整的图邻接表类型 + +/*-------------------------邻接矩阵的基本运算算法---------------------------*/ +/*------------由边数组A、顶点数n和边数e创建图的邻接矩阵g--------------------*/ +void CreateMat(MatGraph &g, int A[MAXV][MAXV], int n, int e) +{ + int i, j; + + g.n = n; + g.e = e; + for(i = 0; i < g.n; i++) + for(j = 0; j < g.n; j++) + g.edges[i][j] = A[i][j]; +} + +/*------------输出邻接矩阵g--------------------*/ +void DispMat(MatGraph g) +{ + int i, j; + + for(i = 0; i < g.n; i++) + { + for(j = 0; j < g.n; j++) + { + if(g.edges[i][j] != INF) + printf("%4d", g.edges[i][j]); + else + printf("%4s", "∞"); + } + printf("\n"); + } +} + +/*-------------------------邻接表的基本运算算法---------------------------*/ +/*-------------------由边数组A、顶点数n和边数e创建图的邻接表G--------------------*/ +void CreateAdj(AdjGraph *&G, int A[MAXV][MAXV], int n, int e) +{ + int i, j; + ArcNode *p; + + G = (AdjGraph *)malloc(sizeof(AdjGraph)); + for(i = 0; i < n; i++) //给邻接表中所有头结点的指针域置初值NULL + { + G->adjlist[i].firstarc = NULL; + } + + for(i = 0; i < n; i++) //检查邻接矩阵中的每个元素 + { + for(j = n - 1; j >= 0; j--) + { + if(A[i][j] != 0 && A[i][j] != INF) //存在一条边 + { + p = (ArcNode *)malloc(sizeof(ArcNode)); //创建一个结点p + p->adjvex = j; //邻接点编号 + p->weight = A[i][j]; //边的权重 + p->nextarc = G->adjlist[i].firstarc; //采用头插法插入结点p + G->adjlist[i].firstarc = p; + } + } + } + G->n = n; + G->e = e; +} + +/*-------------------输出邻接表G--------------------*/ +void DispAdj(AdjGraph *G) +{ + ArcNode *p; + + for(int i = 0; i < G->n; i++) + { + p = G->adjlist[i].firstarc; + printf("顶点%d: ", i); + while(p != NULL) + { + printf("%3d[%d]->", p->adjvex, p->weight); //邻接点编号[权重] + p = p->nextarc; + } + printf("∧\n"); + } +} + +/*-------------------销毁图的邻接表G--------------------*/ +void DestroyAdj(AdjGraph *&G) +{ + ArcNode *pre, *p; + + for(int i = 0; i < G->n; i++) + { + pre = G->adjlist[i].firstarc; //pre指向第i个单链表的首结点 + if(pre != NULL) + { + p = pre->nextarc; + while(p != NULL) //释放第i个单链表的所有边结点 + { + free(pre); + pre = p; + p = p->nextarc; + } + free(pre); + } + } + free(G); //释放头结点数组 +} + +/*---------------输出图g中所有两个顶点之间的最短路径和最短路径长度-------------*/ +static void Dispath(MatGraph g, int A[][MAXV], int path[][MAXV]) +{ + int i, j, k, s; + int apath[MAXV], d; //存放一条最短路径中间顶点(反向)及其顶点个数 + + for(i = 0; i < g.n; i++) + { + for(j = 0; j < g.n; j++) + { + if((i != j) && (A[i][j] != INF)) //若顶点i和j之间存在路径 + { + printf(" 从%d到%d的路径为:", i, j); + k = path[i][j]; + d = 0; apath[d] = j; //路径上添加终点 + while((k != -1) && (k != i)) //路径上添加中间点 + { + d++; + apath[d] = k; + k = path[i][k]; + } + d++; apath[d] = i; //路径上添加起点 + printf("%d", apath[d]); //输出起点 + for(s = d - 1; s >= 0; s--) //输出路径上的中间顶点 + printf(",%d", apath[s]); + printf(" \t路径长度为:%d\n", A[i][j]); + } + } + } +} + + +/*---------------求图g中所有两个顶点之间的最短路径长度和最短路径---------------*/ +static void Floyd(MatGraph g) +{ + int A[MAXV][MAXV], path[MAXV][MAXV]; + int i, j, k; + + for(i = 0; i < g.n; i++) + { + for(j = 0; j < g.n; j++) + { + A[i][j] = g.edges[i][j]; + if((i != j) && (g.edges[i][j] < INF)) + { + path[i][j] = i; //顶点i到j有边时 + } + else + { + path[i][j] = -1; //顶点i到j没有边时 + } + } + } + + for(k = 0; k < g.n; k++) //依次考察所有顶点 + { + for(i = 0; i < g.n; i++) + { + for(j = 0; j < g.n; j++) + { + if(A[i][j] > A[i][k] + A[k][j]) + { + A[i][j] = A[i][k] + A[k][j]; //修改最短路径长度 + path[i][j] = path[k][j]; //修改最短路径 + } + } + } + } + + //输出最短路径 + Dispath(g, A, path); +} + +int main(void) +{ + MatGraph g; + int A[MAXV][MAXV] = { + {0, 5, INF, 7, INF, INF}, + {INF, 0, 4, INF, INF, INF}, + {8, INF, 0, INF, INF, 9}, + {INF, INF, 5, 0, INF, 6}, + {INF, INF, INF, 5, 0, INF}, + {3, INF, INF, INF, 1, 0} + }; + int n = 6; //图中的顶点数 + int e = 10; //图中的边数 + + CreateMat(g, A, n, e); //建立图8.1的邻接矩阵 + printf("有向图G的邻接矩阵:\n"); + DispMat(g); + printf("弗洛伊德算法求解结果:\n"); + Floyd(g); + + return 0; +} \ No newline at end of file diff --git "a/2224020150/chapter9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.c" "b/2224020150/chapter9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.c" new file mode 100644 index 0000000000000000000000000000000000000000..47bdd104ac488d422b04db22f92a26e19d0c093a --- /dev/null +++ "b/2224020150/chapter9/\345\256\236\347\216\260\346\212\230\345\215\212\346\237\245\346\211\276\347\232\204\347\256\227\346\263\225.c" @@ -0,0 +1,65 @@ +#include + +//查找的目标元素 +#define X 15 + +//函数声明 +int BinarySearch(int* arr, int sz, int x); + +int main() +{ + //将需要查找目标元素的数字列表存入数组 + int arr[] = { 10,11,12,13,14,15,16,17,18,19,20 }; + + //求数组元素的个数 + int sz = sizeof(arr) / sizeof(arr[0]); + + //定义查找的目标元素 + int x = X; + + //数组、数组元素个数和目标元素 + int position = BinarySearch(arr, sz, x); + + //输出目标元素的位置 + if (position != -1) + { + printf("目标元素在第 %d 个位置", position); + } + else + { + printf("没有找到"); + } + + return 0; +} + +int BinarySearch(int* arr, int sz, int x) +{ + //左下标 + int left = 0; + + //右下标 + int right = sz; + + while (left <= right) + { + //中间元素下标 + int mid = (left + right) / 2; + + if (x > arr[mid]) + { + left = mid + 1; + } + else if (x < arr[mid]) + { + right = mid - 1; + } + else + { + return mid + 1; + } + } + + //没有找到 + return -1; +} \ No newline at end of file diff --git "a/2224020150/chapter9/\347\273\237\350\256\241\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\255\227\347\254\246\345\217\212\345\205\266\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.c" "b/2224020150/chapter9/\347\273\237\350\256\241\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\255\227\347\254\246\345\217\212\345\205\266\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.c" new file mode 100644 index 0000000000000000000000000000000000000000..e07b37b87487aa0e8a6c1023fff9f0b5c71423dd --- /dev/null +++ "b/2224020150/chapter9/\347\273\237\350\256\241\344\270\200\344\270\252\345\255\227\347\254\246\344\270\262\344\270\255\347\232\204\345\255\227\347\254\246\345\217\212\345\205\266\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260.c" @@ -0,0 +1,63 @@ +#include +#include +#include + + +#define MAXWORD 100 + +typedef int KeyType; + +typedef struct tnode +{ + char ch;//字符 + int count;//出现次数 + struct tnode *lchild,*rchild; +}BTree; + + +void CreaTree(BTree *&p,char c) +{ + if(p==NULL)//p为NULL,则建立一个新结点 + { + p=(BTree *)malloc(sizeof(BTree)); + p->ch=c; + p->count=1; + p->lchild=p->rchild=NULL; + } + else if(c==p->ch) + p->count++; + else if(cch) + CreaTree(p->lchild,c); + else + CreaTree(p->rchild,c); +} + +void InOrder(BTree *p)//中序遍历BST +{ + if(p!=NULL) + { + InOrder(p->lchild);//中序遍历左子树 + printf("字符 %c 在本串中出现的次数:%d \n",p->ch,p->count);//访问根节点 + InOrder(p->rchild);//中序遍历右子树 + } +} + + +int main() +{ + BTree *root=NULL; + int i=0; + char str[MAXWORD]; + printf("\n"); + printf("输入字符串:"); + gets(str); + while(str[i]!='\0') + { + CreaTree(root,str[i]); + i++; + } + printf("各字符及出现次数:\n"); + InOrder(root); + printf("\n"); + return 0; +} diff --git "a/2224020150/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\357\274\214\345\210\240\351\231\244\345\222\214\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.c" "b/2224020150/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\357\274\214\345\210\240\351\231\244\345\222\214\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.c" new file mode 100644 index 0000000000000000000000000000000000000000..bd537ae18b73b69c9a93777685822c3d30703ab0 --- /dev/null +++ "b/2224020150/chapter9/\350\256\276\350\256\241\351\253\230\346\225\210\346\217\222\345\205\245\357\274\214\345\210\240\351\231\244\345\222\214\346\214\211\345\272\217\345\217\267\346\237\245\346\211\276\347\232\204\346\225\260\346\215\256\347\273\223\346\236\204.c" @@ -0,0 +1,154 @@ +#include +#include +#include +#define MAXSIZE 100 //线性表最大存储100个元素 + +#define ERROR 1 +#define OK 0 +typedef int Status; + +typedef struct { + int *elem; //存储基地址 + int length; //存储顺序表的长度 +}SqList; + +//创建一个空的顺序表 +Status InItList(SqList *L) +{ + L->elem = (int*)malloc(sizeof(int)*MAXSIZE); //动态创建顺序表 + if(!L->elem) exit(OVERFLOW); //存储分配失败退出 + L->length = 0; //顺序表的长度为0 + return OK; +} + +//顺序表的初始化 +Status CreatList(SqList *L) +{ + int i; + char c; + printf("请输入顺序表的内容:\n"); + for(i=0;ielem[i]); + c=getchar(); //来接受是否是回车 + L->length++; + } + return OK; +} + +//销毁顺序表 +Status DeletList(SqList *L) +{ + free(L->elem); + L->elem = NULL; + return OK; +} +//查找值为x的某个元素。若成功,返回x在表中的位置;不成功,返回-1。 +int FindList(SqList L) +{ + int i,e=0; + printf("请输入要查询的元素值:"); + scanf("%d",&e); + for(i=0;iL->length+1) //判断插入位置是否合法 + return ERROR; //不合法返回1 + for(a=L->length-1; a>=i-1;a--) + { + L->elem[a+1] = L->elem[a]; //将元素依次向后移动一位 + } + L->elem[i-1] = e; //将新元素插入进去 + L->length++; //L的长度加一 + return OK; +} + +Status DelList(SqList *L,int i) //i表示要删除的位置 +{ + int a,k=0; + if(i<1 || i>L->length) //判断位置是否合法 + return ERROR; //不合法返回1 + k=L->elem[i-1]; //将第i个元素的值赋值给k + for(a=i-1;alength;a++) + { + L->elem[a] = L->elem[a+1]; //将后面的元素依次向前挪动一位 + } + L->length--; //L的长度减一 + if(L->elem[i-1] == k) //若第i个位置元素依然等于k + { + printf("删除失败!\n"); + } + else + { + printf("删除成功,被删除的元素值为%d\n",k); + } + return OK; +} + +//顺序表的遍历 +Status ValList(SqList L) +{ + int i; + for(i=0;i