diff --git a/.clang-format b/.clang-format deleted file mode 100644 index 32bbe3d9e6e50262ce77b4146f90678ffdc570f3..0000000000000000000000000000000000000000 --- a/.clang-format +++ /dev/null @@ -1,4 +0,0 @@ -BasedOnStyle: LLVM -IndentWidth: 4 -DerivePointerAlignment: false -PointerAlignment: Left diff --git a/.gitignore b/.gitignore deleted file mode 100644 index ceb0edf51bbff455d9d7bfdeae24d5d01181e425..0000000000000000000000000000000000000000 --- a/.gitignore +++ /dev/null @@ -1,10 +0,0 @@ -# default ignore -.lightly -.c_bin -.classes -logs -*.log -node_modules -dist -dist-ssr -*.local diff --git a/2209040001/chapter_1/sumOddLengthSubarrays.cpp b/2209040001/chapter_1/sumOddLengthSubarrays.cpp deleted file mode 100644 index 3f0722707013a789a7e2ef1e5880bc6c6cf17174..0000000000000000000000000000000000000000 --- a/2209040001/chapter_1/sumOddLengthSubarrays.cpp +++ /dev/null @@ -1,15 +0,0 @@ -int sumOddLengthSubarrays(int* arr, int arrSize){ - int sum = 0; - for(int start = 0; start < arrSize; start++) - { - for(int length = 0; length < arrSize; length += 2) - { - int end = start + length; - for(int index = start; index <= end && end < arrSize; index++) - { - sum += arr[index]; - } - } - } - return sum; -} \ No newline at end of file diff --git a/2209040001/chapter_1/twoSum.cpp b/2209040001/chapter_1/twoSum.cpp deleted file mode 100644 index 5c03a490c7679b201e1765e42e8ede81a350f7d7..0000000000000000000000000000000000000000 --- a/2209040001/chapter_1/twoSum.cpp +++ /dev/null @@ -1,14 +0,0 @@ -int* twoSum(int* nums, int numsSize, int target, int* returnSize) { - for (int i = 0; i < numsSize; ++i) { - for (int j = i + 1; j < numsSize; ++j) { - if (nums[i] + nums[j] == target) { - int* ret = malloc(sizeof(int) * 2); - ret[0] = i, ret[1] = j; - *returnSize = 2; - return ret; - } - } - } - *returnSize = 0; - return; -} \ No newline at end of file diff --git "a/2209040004/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040004/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 8586fde2545fdf68da7629eac83162f534281c0d..0000000000000000000000000000000000000000 --- "a/2209040004/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,19 +0,0 @@ -int* twoSum(int* nums, int numsSize, int target, int* returnSize) -{ -int i=0,j=0; -int *a=(int*)malloc(sizeof(int)*2); -for(i=0;i -int main() -{ - int num1 = 0; - int num2 = 0; - int sum = 0; - scanf("%d%d",&num1,&num2); - sum = num1+num2; - printf("sum=%d\n",sum); - return 0; - -} \ No newline at end of file diff --git "a/2209040019/chapter_1/\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.cpp" "b/2209040019/chapter_1/\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.cpp" deleted file mode 100644 index dfadc9c8f1bf422364dc06d86b9f42732b4bbaef..0000000000000000000000000000000000000000 --- "a/2209040019/chapter_1/\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.cpp" +++ /dev/null @@ -1,25 +0,0 @@ -#include -int Sumarr(int* arr, int len) -{ - int res = 0; - for (int i = 0; i < len; i++) //从头开始遍历 - { - for (int j = 1; j <=len- i ; j += 2) - //当前情况下的1,3,5..长度类型的子数组 - { - for (int m = i; m < i + j; m++)//输出当前的子数组的值 - { - res = res + arr[m];//累加值 - } - } - } - return res; -} - -int main() -{ - int arr[]={10,11,12,13,14,15}; - printf("%d\n",Sumarr(arr,sizeof(arr)/sizeof(arr[0]))); - printf("%ld\n",sizeof(arr)); - return 0; -} diff --git a/2209040021/.keep b/2209040021/.keep deleted file mode 100644 index e69de29bb2d1d6434b8b29ae775ad8c2e48c5391..0000000000000000000000000000000000000000 diff --git "a/2209040023/chap1/lc-1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040023/chap1/lc-1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index e397b4f9731e8d8805b73b6e3c75b09887e3e1b2..0000000000000000000000000000000000000000 --- "a/2209040023/chap1/lc-1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - vector twoSum(vector& nums, int target) { - int i,j,n; - for(i=0;i& arr) { - int size=arr.size(); - int result=0; - for(int i=0;i -int main(){ -int a,b; -int sum=0; -printf("请输入两个整数\n"); -scanf("%d%d",&a,&b); -printf("sum=%d",a+b); -return 0; -} \ No newline at end of file diff --git "a/2209040029/chapter 1/ lc chapter1 \344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040029/chapter 1/ lc chapter1 \344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index d58d585d9abeedd27cd09ceee20661049b803ef5..0000000000000000000000000000000000000000 --- "a/2209040029/chapter 1/ lc chapter1 \344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,19 +0,0 @@ -int* twoSum(int* nums, int numsSize, int target, int* returnSize) -{ -int i=0,j=0; -int *a=(int*)malloc(sizeof(int)*2); -for(i=0;i -#include -#include - -clock_t start , stop; -double duration1,duration2;//秒 - -void add1(long n) -{ - start=clock(); - long sum=0; - sum=(1+n)*n/2; - stop=clock(); - printf("高斯法:从1到%ld的连续整数和为:%ld\n",n,sum);//输出不是计算过程,所以放在外面 - duration1 = ((double)(stop-start))/CLOCKS_PER_SEC; - printf("高斯法的用时是%f秒\n",duration1); -} - -void add2(long n) -{ - start=0; - stop=0; - start=clock(); - long sum=0; - for(int i=1;i<=n;i++) - { - sum=i+sum; - } - stop=clock(); - printf("累加法:从1到%ld的连续整数和为:%ld\n",n,sum); - duration2 = ((double)(stop-start))/CLOCKS_PER_SEC; - printf("累加法的用时是%f秒\n",duration2); -} - -int main() -{ - long n; - scanf("%ld",&n); - add1(n); - add2(n); - return 0; -} - diff --git a/2209040034/chap1/exp1-3.cpp b/2209040034/chap1/exp1-3.cpp deleted file mode 100644 index 8574c84cd666d00b0d48c90d231f400ab5688e6b..0000000000000000000000000000000000000000 --- a/2209040034/chap1/exp1-3.cpp +++ /dev/null @@ -1,86 +0,0 @@ -#include -#include -#include - -clock_t start , stop; -double duration1,duration2,duration3;//秒 - -void primel1(long n)//找到不是素数的数 -{ - start=clock(); - long i,j,count=1,num;//count为,不是素数的数 - if(n==2) - { - count=1;//1不是素数,2是素数 - } - else - { - for(i=3;i<=n;i++) - { - for(j=2;ja[j+1]) - { - t=a[j]; - a[j]=a[j+1]; - a[j+1]=t; - } - } - } - for(i=0;i -#include -#include - -long add1(long n) -{ - long i,sum=0; - for(i=1;i<=n;i++) - sum+=i; - return sum; -} -void AddTime1(long n) -{ - clock_t t; - long sum; - t=clock(); - sum=add1(n); - t=clock()-t; - printf("方法1:\n"); - printf("结果:1~%d之和:%ld\n",n,sum); - printf("用时:%lf秒\n",((float)t)/CLOCKS_PER_SEC); -} -int main() -{ - int n; - printf("n(大于1000000):"); - scanf("%d",&n); - if(n<1000000)return 0; - AddTime1(n); - return 1; -} \ No newline at end of file diff --git "a/2209040037/chapter_1/lc-1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040037/chapter_1/lc-1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index a330fbf90fe27bfa53fcde6817845d9ebad472cb..0000000000000000000000000000000000000000 --- "a/2209040037/chapter_1/lc-1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,66 +0,0 @@ -/** -?* Note: The returned array must be malloced, assume caller calls free(). -?*/ - -int* copydata(int* nums, int numsSize){ -? ? int* ret = (int*)malloc(sizeof(int) * numsSize); -? ? if (ret == NULL) { -? ? ? ? return NULL; -? ? } -? ? memcpy(ret, nums, sizeof(int) * numsSize); -? ? return ret; -} - -int compare(const void* a, const void* b) { -? ? int x = *(int*) a; -? ? int y = *(int*) b; -? ? return x - y; -} - -void findPosition(int* nums, int numsSize, int num1, int num2, int* res) -{ -? ? for (int i = 0; i < numsSize; i++) { -? ? ? ? if (nums[i] == num1) { -? ? ? ? ? ? res[0] = i; -? ? ? ? ? ? break; -? ? ? ? } -? ? } - -? ? for (int i = 0; i < numsSize; i++) { -? ? ? ? if (nums[i] == num2 && i != res[0]) { -? ? ? ? ? ? res[1] = i; -? ? ? ? ? ? break; -? ? ? ? } -? ? } - -} - -int* twoSum(int* nums, int numsSize, int target, int* returnSize){ -? ? int* res = (int*)malloc(sizeof(int) * 2); -? ? if (res == NULL) { -? ? ? ? return NULL; -? ? } - -? ? int* arr = copydata(nums, numsSize); -? ? qsort(arr, numsSize, sizeof(int), compare); - -? ? *returnSize = 2; -? ? int left, right, sum; -? ? left = 0; -? ? right = numsSize - 1; -? ? while (left < right) { -? ? ? ? sum = arr[left] + arr[right]; -? ? ? ? if (sum == target) { -? ? ? ? ? ? findPosition(nums, numsSize, arr[left], arr[right], res); -? ? ? ? ? ? return res; -? ? ? ? } - -? ? ? ? if (sum > target) { -? ? ? ? ? ? right--; -? ? ? ? } else { -? ? ? ? ? ? left++; -? ? ? ? } -? ? } - -? ? return NULL; -} diff --git "a/2209040037/chapter_1/lc-1\345\245\207\346\225\260\345\255\220\351\233\206\346\261\202\345\222\214.cpp" "b/2209040037/chapter_1/lc-1\345\245\207\346\225\260\345\255\220\351\233\206\346\261\202\345\222\214.cpp" deleted file mode 100644 index 0be511d00eed5709d728a684331570cadfe8c6bd..0000000000000000000000000000000000000000 --- "a/2209040037/chapter_1/lc-1\345\245\207\346\225\260\345\255\220\351\233\206\346\261\202\345\222\214.cpp" +++ /dev/null @@ -1,15 +0,0 @@ -int sumOddLengthSubarrays(int* arr, int arrSize) -{ -? ? int sum=0; -? ? for(int i=0;i -#include -#include -bool prime1(long n) -{ - long i; - for(i=2;i twoSum(vector& nums, int target) { - unordered_mapun_map; - vectorres; - for (int i = 0; i < nums.size(); i++) { - if (un_map.find(target - nums[i]) != un_map.end()) { - res.push_back(i); - res.push_back(un_map[target - nums[i]]); - } - un_map[num[i]] = i; - } - return res; - } -}; \ No newline at end of file diff --git "a/2209040039/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.cpp" "b/2209040039/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.cpp" deleted file mode 100644 index 673796906ad69acb31270426c0edc6c47d50e31c..0000000000000000000000000000000000000000 --- "a/2209040039/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.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -int sumOddLengthSubarrays(int* arr, int arrSize) -{ - int sum = 0; - for (int i = 0; i < arrSize; i++) - { - int n = (arrSize - i) % 2 == 0 ? (arrSize - i) - 1 : arrSize - i; - for (int size = 1; size <= n; size += 2) - { - for (int j = 0; j < size; j++) - { - sum += arr[i + j]; - } - } - } - return sum; -} \ No newline at end of file diff --git "a/2209040040/chapter_1/lc_1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040040/chapter_1/lc_1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index d58d585d9abeedd27cd09ceee20661049b803ef5..0000000000000000000000000000000000000000 --- "a/2209040040/chapter_1/lc_1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,19 +0,0 @@ -int* twoSum(int* nums, int numsSize, int target, int* returnSize) -{ -int i=0,j=0; -int *a=(int*)malloc(sizeof(int)*2); -for(i=0;i -#include -#include - -long add1(long n) -{ - long i,sum=0; - for(i=1;i<=n;i++) - sum+=i; - return sum; -} -void AddTime1(long n) -{ - clock_t t; - long sum; - t=clock(); - sum=add1(n); - t=clock()-t; - printf("1:\n"); - printf(":1~%d֮:%ld\n",n,sum); - printf("ʱ:%lf\n",((float)t)/CLOCKS_PER_SEC); -} -int main() -{ - int n; - printf("n(1000000):"); - scanf("%d",&n); - if(n<1000000)return 0; - AddTime1(n); - return 1; -} - diff --git "a/2209040040/chapter_1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2209040040/chapter_1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index 31d8538b249bb96e283072379e75dd818182b5fb..0000000000000000000000000000000000000000 --- "a/2209040040/chapter_1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include -#include -bool prime1(long n) -{ - long i; - for(i=2;i -#include -#include -//求1+2+.....+n -long add1(long n) -{ - long i,sum=0; - for(i=1;i<=n;i++) - { - sum+=i; - return sum; - } -} -Viod AddTime1(long n) -{ - clock_t t; - long sum; - t=clock(); - sum=add1(n); - t=clock()-t; - printf("方法1:\n"); - printf(" 结果:1~%d之和:%d\n",n,sum); - printf(" 用时:%lf秒\n",((float)t)/CLOCKS_PER_SEC); -} -int main() -{ - int n; - print("n(大于1000000):"); - sacnf("%d",&n); - if(n<1000000)return 0; - AddTime1(n); - return 1; -} \ No newline at end of file diff --git "a/2209040042/chapter1/\350\257\276\345\240\202\344\275\234\344\270\232\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2209040042/chapter1/\350\257\276\345\240\202\344\275\234\344\270\232\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index e4da0fee98e7e4f7f7cf3bc39dfa7c944dbcb71b..0000000000000000000000000000000000000000 --- "a/2209040042/chapter1/\350\257\276\345\240\202\344\275\234\344\270\232\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,33 +0,0 @@ -#include -#include -#include -bool prime1(long n) -{ - long i; - for(i=2;i -#include -#include -bool prime1(long n) -{ - long i; - for(i=2;i -#include -#include - -long add1(long n) -{ - long i,sum=0; - for(i=1;i<=n;i++) - sum+=i; - return sum; -} -void AddTime1(long n) -{ - clock_t t; - long sum; - t=clock(); - sum=add1(n); - t=clock()-t; - printf("方法1:\n"); - printf("结果:1~%d之和:%ld\n",n,sum); - printf("用时:%lf秒\n",((float)t)/CLOCKS_PER_SEC); -} -int main() -{ - int n; - printf("n(大于1000000):"); - scanf("%d",&n); - if(n<1000000)return 0; - AddTime1(n); - return 1; -} \ No newline at end of file diff --git "a/2209040045/chap1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040045/chap1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 73e368cf5e6f501da1c9ee7893df46958e109728..0000000000000000000000000000000000000000 --- "a/2209040045/chap1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,15 +0,0 @@ -int* twoSum(int* nums, int numsSize, int target, int* returnSize) { - for (int i = 0; i < numsSize; ++i) { - for (int j = i + 1; j < numsSize; ++j) { - if (nums[i] + nums[j] == target) { - int* ret = malloc(sizeof(int) * 2); - ret[0] = i, ret[1] = j; - *returnSize = 2; - return ret; - } - } - } - *returnSize = 0; - return NULL; -} - diff --git "a/2209040045/chap1/\345\245\207\346\225\260\351\225\277\345\272\246\346\225\260\347\273\204\344\271\213\345\222\214.cpp" "b/2209040045/chap1/\345\245\207\346\225\260\351\225\277\345\272\246\346\225\260\347\273\204\344\271\213\345\222\214.cpp" deleted file mode 100644 index aa635214ddb43370f4f34318d53b9f59828188b4..0000000000000000000000000000000000000000 --- "a/2209040045/chap1/\345\245\207\346\225\260\351\225\277\345\272\246\346\225\260\347\273\204\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,13 +0,0 @@ -int sumOddLengthSubarrays(int* arr, int arrSize){ - int res = 0; - int lEven,lOdd,rEven,rOdd; - for(int i=0;i -#include -#include - -long add1(long n) -{ - long i,sum = 0; -for (i=1;i<=n:i++) -sum+=i; -return sum; -} -void AddTime1(long n) -{ -clock_t t; -long sum; -t=clock(); -sum=add1(n); -t=clock()-t; -printf("方法1:\n"); -printf(" 结果:1~%d之和:%ld\n",n,sum); -printf(" 用时:%lf秒\n",((float)t)/CLOCKS_PER_SEC); -} - -long add2(long n) -{ - return n*(n+1)/2; -} -void AddTime2(long n) -{ - clock_t t; - long sum; - t=clock(); - sum=add2(n); - t=clock()-t; - printf("方法2:\n"); - printf(" 结果:1~%d之和:%ld\n",n,sum); - printf(" 用时:%lf秒\n",((float)t)/CLOCKS_PER_SEC); -} -int main() -{ - int n; - printf("n(大于1000000):"); - scanf("%d",&n); - if(n<1000000) return 0; - AddTime1(n); - AddTime2(n); - return 1; -} \ No newline at end of file diff --git "a/2209040049/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040049/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 131900d8da99e66728761cf91c49c58a9793bcc5..0000000000000000000000000000000000000000 --- "a/2209040049/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,19 +0,0 @@ -class Solution { -public: - vector twoSum(vector& nums, int target) { - vector targetnum(2); - map temp_map; - for (int i = 0; i < nums.size(); i++) { - temp_map[nums[i]]= i; - } - for (int i = 0; i < nums.size(); i++) { - int complement = target - nums[i]; - if (temp_map.count(complement) && temp_map[complement] != i) { - targetnum[0]=i; - targetnum[1]=temp_map[complement]; - break;//这个很重要 - } - } - return targetnum; - } -}; diff --git "a/2209040049/chapter1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" "b/2209040049/chapter1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" deleted file mode 100644 index f0f1d019fb0ca12930c619b3d8481c98452b3c6b..0000000000000000000000000000000000000000 --- "a/2209040049/chapter1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { -public: - int sumOddLengthSubarrays(vector& arr) { - int result=0; - for(int step=0;step 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/2209040050/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.cpp" "b/2209040050/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.cpp" deleted file mode 100644 index 71613f189faeb792685230a908c4672b853813a3..0000000000000000000000000000000000000000 --- "a/2209040050/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.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - int sumOddLengthSubarrays(vector& arr) { - int sum = 0; - int n = arr.size(); - for (int start = 0; start < n; start++) { - for (int length = 1; start + length <= n; length += 2) { - int end = start + length - 1; - for (int i = start; i <= end; i++) { - sum += arr[i]; - } - } - } - return sum; - } -}; \ No newline at end of file diff --git "a/2209040059/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040059/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 32b29d6eb98cc26f4dd46d7f8b1a87bd4544065d..0000000000000000000000000000000000000000 --- "a/2209040059/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,17 +0,0 @@ -#include -int* twoSum(int* nums, int numsSize, int target, int* returnSize){ - int *a=(int*)malloc(sizeof(int)*2); - int i,j; - for(i=0;i -int* twoSum(int* nums, int numsSize, int target, int* returnSize) -{ - int *a=(int*)malloc(sizeof(int)*2); -for(int i=0;i -int sumOddLengthSubarrays(int* arr, int arrSize) -{ - int sum=0; - for(int i=0;i -int* twoSum(int* nums, int numsSize, int target, int* returnSize) -{ - int*ret=NULL; - for(int i=0;i -int arrSum(int* arr, int start, int end){ - int i, res = 0; - for(i = start; i < start + end; i++) - { - res += arr[i]; - } - - return res; -} -int sumOddLengthSubarrays(int* arr, int arrSize){ - int i = 0; - int j = 0; - int k = 0; - int ans = 0; - - for(i = 1; i <= arrSize; i += 2){ - for(j = 0; j < i; j += i){ - for(k = j; k < arrSize - i + 1;k++){ - ans += arrSum(arr, k ,i); - } - } - } - return ans; -} \ No newline at end of file diff --git "a/2209040076/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040076/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 242562e4291542715f9ac986821f7bdd9b5f84dd..0000000000000000000000000000000000000000 --- "a/2209040076/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - vector twoSum(vector& nums, int target) { - int i,j; - for(i=0;i& arr) { -int m=0; - for(int i=0;i 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/2209040077/chapter1/1.1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214" "b/2209040077/chapter1/1.1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214" deleted file mode 100644 index ffd8e00e007c5bfc913b87dff938b8749d15a45e..0000000000000000000000000000000000000000 --- "a/2209040077/chapter1/1.1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - int sumOddLengthSubarrays(vector& arr) { - int sum = 0; - int n = arr.size(); - for (int start = 0; start < n; start++) { - for (int length = 1; start + length <= n; length += 2) { - int end = start + length - 1; - for (int i = start; i <= end; i++) { - sum += arr[i]; - } - } - } - return sum; - } -}; \ No newline at end of file diff --git "a/2209040077/chapter1/1.1/\346\261\2021~n\344\270\255\346\211\200\346\234\211\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2209040077/chapter1/1.1/\346\261\2021~n\344\270\255\346\211\200\346\234\211\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index 20b485fbc4220f1580ffc10712ba8f9fa51b79e6..0000000000000000000000000000000000000000 --- "a/2209040077/chapter1/1.1/\346\261\2021~n\344\270\255\346\211\200\346\234\211\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,17 +0,0 @@ -#include -int main() -{ - int n,i,j; - int sum=0; - scanf("%d",&n); - for(i=2;i<=n;i++) - { - for(j=2;j<=i;j++) - if(i%j==0) - break; - if(i==j) - sum++; - } - printf("%d",sum); - return 0; -} \ No newline at end of file diff --git "a/2209040077/chapter1/1.1/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" "b/2209040077/chapter1/1.1/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" deleted file mode 100644 index 2547c3b1e8d3e2706a3b171b2ec4ef6b9dc39ae9..0000000000000000000000000000000000000000 --- "a/2209040077/chapter1/1.1/\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" +++ /dev/null @@ -1,11 +0,0 @@ -class Solution { -public: - int sumNums(int n) { -int i,sum=0; -for(i=1;i<=n;i++) -{ -sum+=i; -} -return sum; - } -}; \ No newline at end of file diff --git "a/2209040078/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040078/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 747e054f205c6c7a4500fbdd682220ca851ce490..0000000000000000000000000000000000000000 --- "a/2209040078/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) { - unordered_map hash; - for (int i = 0; i < nums.size(); ++i) - { - if (hash.find(target - nums[i]) != hash.end()) - return { hash[target - nums[i]],i }; - else - hash[nums[i]] = i; - } - return {}; -} -}; \ No newline at end of file diff --git "a/2209040078/chapter1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" "b/2209040078/chapter1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" deleted file mode 100644 index e3a789ead7920b3fd317769544283b934c41f382..0000000000000000000000000000000000000000 --- "a/2209040078/chapter1/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - int sumOddLengthSubarrays(vector& arr) { - int sum = 0; - int n = arr.size(); - for (int start = 0; start < n; start++) { - for (int length = 1; start + length <= n; length += 2) { - int end = start + length - 1; - for (int i = start; i <= end; i++) { - sum += arr[i]; - } - } - } - return sum; - } -}; \ No newline at end of file diff --git "a/2209040079/chapter 1/exp1-1\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" "b/2209040079/chapter 1/exp1-1\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" deleted file mode 100644 index 2547c3b1e8d3e2706a3b171b2ec4ef6b9dc39ae9..0000000000000000000000000000000000000000 --- "a/2209040079/chapter 1/exp1-1\346\261\2021~n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" +++ /dev/null @@ -1,11 +0,0 @@ -class Solution { -public: - int sumNums(int n) { -int i,sum=0; -for(i=1;i<=n;i++) -{ -sum+=i; -} -return sum; - } -}; \ No newline at end of file diff --git "a/2209040079/chapter 1/exp1-3\346\261\2021~n\344\270\255\347\232\204\346\211\200\346\234\211\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2209040079/chapter 1/exp1-3\346\261\2021~n\344\270\255\347\232\204\346\211\200\346\234\211\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index 2701b18d9e53d7febe4f34eebb46843133cfd53c..0000000000000000000000000000000000000000 --- "a/2209040079/chapter 1/exp1-3\346\261\2021~n\344\270\255\347\232\204\346\211\200\346\234\211\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,18 +0,0 @@ -#include -int main() -{ - int n,i,j; - int count=0; - scanf("%d",&n); - printf("素数个数:"); - for(i=2;i<=n;i++) - { - for(j=2;j<=i;j++) - if(i%j==0) - break; - if(i==j) - count++; - } - printf("%d",count); - return 0; -} \ No newline at end of file diff --git "a/2209040079/chapter 1/lc \344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2209040079/chapter 1/lc \344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 18918d6b5fe727cc5bc6f0e3e3fd371f5766974b..0000000000000000000000000000000000000000 --- "a/2209040079/chapter 1/lc \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) { - unordered_map hash; - for (int i = 0; i < nums.size(); ++i) - { - if (hash.find(target - nums[i]) != hash.end()) - return { hash[target - nums[i]],i }; - else - hash[nums[i]] = i; - } - return {}; -} -}; \ No newline at end of file diff --git "a/2209040079/chapter 1/lc\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214 .cpp" "b/2209040079/chapter 1/lc\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214 .cpp" deleted file mode 100644 index 0581723fa49945ed4babfaed3b0fe89cc9b855b5..0000000000000000000000000000000000000000 --- "a/2209040079/chapter 1/lc\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214 .cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - int sumOddLengthSubarrays(vector& arr) { -int m=0; - for(int i=0;i& nums1, int m, vector& nums2, int n) { - int i=m-1; - int j=n-1; - int k=m+n-1; - while(i>=0&&j>=0) - { - if(nums1[i]>nums2[j]) - { - nums1[k]=nums1[i]; - k--;i--; - } - else - { - nums1[k]=nums2[j]; - k--;j--; - } - } - while(j>=0) - { - nums1[k]=nums2[j]; - k--;j--; - } - } -}; diff --git "a/2209040081/chapter-2/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" "b/2209040081/chapter-2/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" deleted file mode 100644 index 71613f189faeb792685230a908c4672b853813a3..0000000000000000000000000000000000000000 --- "a/2209040081/chapter-2/\346\211\200\346\234\211\345\245\207\346\225\260\351\225\277\345\272\246\345\255\220\346\225\260\347\273\204\347\232\204\345\222\214.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - int sumOddLengthSubarrays(vector& arr) { - int sum = 0; - int n = arr.size(); - for (int start = 0; start < n; start++) { - for (int length = 1; start + length <= n; length += 2) { - int end = start + length - 1; - for (int i = start; i <= end; i++) { - sum += arr[i]; - } - } - } - return sum; - } -}; \ No newline at end of file diff --git "a/2209040081/chapter/\344\270\244\346\225\260\344\271\213\345\222\214" "b/2209040081/chapter/\344\270\244\346\225\260\344\271\213\345\222\214" deleted file mode 100644 index 39274acfc8c9d6a4e306ec5f89ba30a2e483dd1f..0000000000000000000000000000000000000000 --- "a/2209040081/chapter/\344\270\244\346\225\260\344\271\213\345\222\214" +++ /dev/null @@ -1,20 +0,0 @@ -//1.定义整数型数组和整数型数 -//2.用两个for循环遍历数组里面的值,使得两个值之和等于输入的整数型数 -//3.输出对应的数组中元素的下标 - - - -class Solution { -public: - vector twoSum(vector& nums, int target) { - for(int i=0; i=0;i--) - { - if(digits[i] != 9) - { - digits[i]++; - *returnSize = digitsSize; - return digits; - }else{ - digits[i]=0; - } - } - int* ret=malloc((digitsSize+1)*sizeof*ret); - ret[0]=1; - for(int n =1;n INT_MAX || n < INT_MIN ? 0 : n; -} \ No newline at end of file diff --git "a/2224020085/chapter1/leetcode\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2224020085/chapter1/leetcode\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 7cc9787cd7f103795eb8f08383ac28a0be5fa743..0000000000000000000000000000000000000000 --- "a/2224020085/chapter1/leetcode\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,36 +0,0 @@ -#include -int main() -{ - int* twoSum(int* nums, int numsSize, int target, int* returnSize); - int i,n,f; - int b[n]; - for(i=0;i -int main() -{ - int* twoSum(int* nums, int numsSize, int target, int* returnSize); - int i,n,m; - int b[n]; - for(i=0;i -#include -int amin() -{ - float log1(int n); - float sqrt1(int n1); - float yuan(int n2); - float log2(int n); - float pingf(int n); - float lif(int n); - float min(int n); - float jiec(int n); - int n,i; - scanf("%d\n",&n); - for(i=1;i<=n;i++) - { - printf("%f%f%f%f%f%f%f%f\n",log1(i),sqrt1(i),yuan(i),log2(i),pingf(i),lif(i),min(i),jiec(i)); - } - return 0; -} -float log1(int n) -{ - float nums1=log10(n)/log10(2); - return nums1; -} -float sqrt1(int n1) -{ - float nums2=sqrt(n1); - return nums2; -} -float yuan(int n2) -{ - return n2; -} -float log2(int n) -{ - float nums3=n*(log10(n)/log10(2)); - return nums3; -} -float pingf(int n) -{ - float nums4=n*n; - return nums4; -} -float lif(int n) -{ - float nums5=n*n*n; - return nums5; -} -float min(int n) -{ - float nums6=pow(2,n); - return nums6; -} -float jiec(int n) -{ - int i,nums7=0,num=1; - for(i=1;i<=n;i++) - { - num=num*i; - } - nums7=nums7+num; - return nums7; -} diff --git "a/2224020085/chapter1/\346\261\2021-n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" "b/2224020085/chapter1/\346\261\2021-n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" deleted file mode 100644 index e9a671d704dac5c9c87b06e5616b2898b36c7c19..0000000000000000000000000000000000000000 --- "a/2224020085/chapter1/\346\261\2021-n\347\232\204\350\277\236\347\273\255\346\225\264\346\225\260\345\222\214.cpp" +++ /dev/null @@ -1,24 +0,0 @@ -#include -int main() -{ - int add1(int n); - int add2(int n); - int num1; - scanf("%d\n",&num1); - printf("%d\n",add1(num1)); - printf("%d\n",add2(num1)); -} -int add1(int n) -{ - int i; - int sum=0; - for(i=1;i<=n;i++) - { - sum=sum+i; - } - return sum; -} -int add2(int n) -{ - return n*(n+1)/2; -} \ No newline at end of file diff --git "a/2224020085/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" "b/2224020085/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" deleted file mode 100644 index 41db4a5db496f2799ec2f23eaa84de79afc10b4b..0000000000000000000000000000000000000000 --- "a/2224020085/chapter1/\346\261\202\347\264\240\346\225\260\347\232\204\344\270\252\346\225\260.cpp" +++ /dev/null @@ -1,38 +0,0 @@ -#include -#include -int main() -{ - int way1(int n); - int way2(int n); - int num; - scanf("%d\n",&num); - printf("%d\n%d\n",way1(num),way2(num)); -} -int way2(int n) -{ - int n1,k,i,m=0; - for(n1=1;n1<=n;n=n+2) - { - k=sqrt(n1); - for(i=2;i<=k;i++) - if(n%i==0) break; - if(i>=k+1) - m=m+1; - } - return m; -} -int way1(int n) -{ - int count=0; - int i=0; - for(i=2;i -int main() -{ - int link(int n); - int num; - scanf("%d\n",&num); - printf("%d\n",link(num)); -} -int link(int n) -{ - int i,j,sum=0; - int num=1; - scanf("%d\n",&n); - for(i=1;i<=n;n++) - { - for(j=1;j<=i;j++) - { - num=num*j; - } - sum=sum+num; - } - return sum; -} \ No newline at end of file diff --git "a/2224020087/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2224020087/chapter1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index a46398c00c696f5160c2aa417447e99aa34ebe72..0000000000000000000000000000000000000000 --- "a/2224020087/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/2224020087/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.cpp" "b/2224020087/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.cpp" deleted file mode 100644 index 71613f189faeb792685230a908c4672b853813a3..0000000000000000000000000000000000000000 --- "a/2224020087/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.cpp" +++ /dev/null @@ -1,16 +0,0 @@ -class Solution { -public: - int sumOddLengthSubarrays(vector& arr) { - int sum = 0; - int n = arr.size(); - for (int start = 0; start < n; start++) { - for (int length = 1; start + length <= n; length += 2) { - int end = start + length - 1; - for (int i = start; i <= end; i++) { - sum += arr[i]; - } - } - } - return sum; - } -}; \ No newline at end of file diff --git "a/2224020099/chapter_1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2224020099/chapter_1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 155c76836367d79ec6b4e3449173f6c6d35af4c2..0000000000000000000000000000000000000000 --- "a/2224020099/chapter_1/\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,17 +0,0 @@ -int* twoSum(int* nums, int numsSize, int target, int* returnSize){ - int *a = (int *)malloc(sizeof(int) * 2); - int i, j; - for (i = 0; i < numsSize - 1; i++) { - for (j = i + 1; j < numsSize; j++) { - if (nums[i] + nums[j] == target) { - a[0] = i; - a[1] = j; - printf("%d---%d",i,j); - *returnSize = 2; - return a; - } - } - } - *returnSize = 0; - return a; - } \ No newline at end of file diff --git "a/2224020099/chapter_1/\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\351\233\206\345\220\210.cpp" "b/2224020099/chapter_1/\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\351\233\206\345\220\210.cpp" deleted file mode 100644 index bb7b0f70e948e0b533ff8952aae2923c1194af41..0000000000000000000000000000000000000000 --- "a/2224020099/chapter_1/\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\351\233\206\345\220\210.cpp" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - int sumOddLengthSubarrays(vector& arr) { - int sum = 0; - int n = arr.size(); - for (int start = 0; start < n; start++) { - for (int length = 1; start + length <= n; length += 2) { - int end = start + length - 1; - for (int i = start; i <= end; i++) { - sum += arr[i]; - } - } - } - return sum; - } - - }; \ No newline at end of file diff --git a/2224020106/chapter1/exp1-1.cpp b/2224020106/chapter1/exp1-1.cpp deleted file mode 100644 index 2d7f4d547e9ade20bffa0c6a6b1b4e7e08890f15..0000000000000000000000000000000000000000 --- a/2224020106/chapter1/exp1-1.cpp +++ /dev/null @@ -1,19 +0,0 @@ -#include -long add1(long n){ - long i,sum = 0; - for(i = 0;i<=n;i++) - sum += i; - return sum; -} -long add2(long n){ - return n * (n + 1)/2; -} -int main() -{ - int n; - printf("输入一个n"); - scanf("%d",&n); - printf("使用累加法:%ld\n",add1(n)); - printf("使用高斯法:%ld\n",add2(n)); - return 1; -} \ No newline at end of file diff --git a/2224020106/chapter1/exp1-3.cpp b/2224020106/chapter1/exp1-3.cpp deleted file mode 100644 index 077d2fc1276b9b58bcec37393159de3649cf4bbe..0000000000000000000000000000000000000000 --- a/2224020106/chapter1/exp1-3.cpp +++ /dev/null @@ -1,6 +0,0 @@ -#include - -int main(){ - long n; - scanf("输入一个%d,判断是否为素数",&n); -} \ No newline at end of file diff --git a/2224020106/chapter1/lc-1.cpp b/2224020106/chapter1/lc-1.cpp deleted file mode 100644 index 97ab3d711b32e0d4797a55deef256a7a6f9bfa82..0000000000000000000000000000000000000000 --- a/2224020106/chapter1/lc-1.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/2224020106/chapter1/lc-1588.cpp b/2224020106/chapter1/lc-1588.cpp deleted file mode 100644 index 7659ff3a76b687e6a53504a2f18061e00546a135..0000000000000000000000000000000000000000 --- a/2224020106/chapter1/lc-1588.cpp +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - int sumOddLengthSubarrays(vector& arr) { - int sum = 0; - int n = arr.size(); - for (int i = 0; i < n; i++) { - int temp = 0; - for(int j = i; j < n; j++) { - temp += arr[j]; - if((j - i)% 2 == 0) { - sum += temp; - } - } - } - return sum; - } -}; \ No newline at end of file diff --git "a/2225010064/chapter1/lc_1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" "b/2225010064/chapter1/lc_1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" deleted file mode 100644 index 2781a7955cd4be482a7592c919fd5a4f8d37f89e..0000000000000000000000000000000000000000 --- "a/2225010064/chapter1/lc_1\344\270\244\346\225\260\344\271\213\345\222\214.cpp" +++ /dev/null @@ -1,17 +0,0 @@ -class Solution { -public: - vector twoSum(vector& nums, int target) { - int i,j; - for(i=0;i - - -2. 在线编辑器注册 - -登录 [轻量级编辑器](https://lightly.teamcode.com/) 注册账号。 - -账号注册 - -账号注册 - -3. 写代码 - -页面中间点击“在线使用”; - -账号注册 - -使用微信或者其他方式登录: -账号注册 - -复制公开仓库项目到自己的仓库中: - -账号注册 - -账号注册 - -点击“导入项目”: - -账号注册 - -输入Git地址:比如:`https://github.com/OpenHUTB/data_structure.git` (注意换成自己仓库的地址),点击下一步: - -账号注册 - -选择C++工程,点击导入: - -账号注册 - - -打开项目: - -账号注册 - -账号注册 - - -4. 提交代码 - -将修改的代码添加到暂存区 - -账号注册 - - -填写提交信息,并将代码提交并推送到远端: - -账号注册 - -注意:如果自己项目的内容落后于开源湖工商项目的内容,需要拉取并合并修改 - -账号注册 - -账号注册 - -并从并同步到自己的项目 - -账号注册 - -账号注册 - -账号注册 - -5. 出现授权失败后,输入自己的用户名和Token - -账号注册 - -具体步骤参考[Gitee创建Token](https://lightly.teamcode.com/docs/version-control/git-authentication) : - -账号注册 - - -6. 提交pull request - -账号注册 - -创建pull request -账号注册 - -提交完成pull request 后会在开源湖工商项目页面看到此次的提交: -账号注册 - -7. 管理员进行审查: - -账号注册 - -账号注册 - -接收pull request - -账号注册 - -8. 最终在页面中显示代码合并到主分支 - -账号注册 - diff --git a/guide/accept_pr.png b/guide/accept_pr.png deleted file mode 100644 index 93bbe29c74c00977027ebe3021da720f48fefdf9..0000000000000000000000000000000000000000 Binary files a/guide/accept_pr.png and /dev/null differ diff --git a/guide/accepted_pr.png b/guide/accepted_pr.png deleted file mode 100644 index 43aa599a5e3a8cd06128eb15558de8953e49b3e4..0000000000000000000000000000000000000000 Binary files a/guide/accepted_pr.png and /dev/null differ diff --git a/guide/add_pr.png b/guide/add_pr.png deleted file mode 100644 index f89fa768327b762f158d8315d08c8722cfc67f53..0000000000000000000000000000000000000000 Binary files a/guide/add_pr.png and /dev/null differ diff --git a/guide/check_pr.png b/guide/check_pr.png deleted file mode 100644 index 4fb587423b9e9b11ad1cae97e1f33d20571735a6..0000000000000000000000000000000000000000 Binary files a/guide/check_pr.png and /dev/null differ diff --git a/guide/code_review.png b/guide/code_review.png deleted file mode 100644 index d762277578b071f84c95fe9a52c1ab48c3ec4f80..0000000000000000000000000000000000000000 Binary files a/guide/code_review.png and /dev/null differ diff --git a/guide/commit_push.png b/guide/commit_push.png deleted file mode 100644 index 24b681330b2b3654b41c5642524e4be2578ecb18..0000000000000000000000000000000000000000 Binary files a/guide/commit_push.png and /dev/null differ diff --git a/guide/create_fork.png b/guide/create_fork.png deleted file mode 100644 index 5f75b84d3136f90616c325f4ccdacea9d112ce53..0000000000000000000000000000000000000000 Binary files a/guide/create_fork.png and /dev/null differ diff --git a/guide/create_fork_2.png b/guide/create_fork_2.png deleted file mode 100644 index e94d9fcd02d5eb25d521f0afd8bdfeecf2dd9f26..0000000000000000000000000000000000000000 Binary files a/guide/create_fork_2.png and /dev/null differ diff --git a/guide/create_pr.png b/guide/create_pr.png deleted file mode 100644 index 40c95410f048700673bb7c95cf02a04963b868d6..0000000000000000000000000000000000000000 Binary files a/guide/create_pr.png and /dev/null differ diff --git a/guide/ensure_merge.png b/guide/ensure_merge.png deleted file mode 100644 index d0fcb7e00761a88c671f8842e6dc5ea2c955b963..0000000000000000000000000000000000000000 Binary files a/guide/ensure_merge.png and /dev/null differ diff --git a/guide/git_add.png b/guide/git_add.png deleted file mode 100644 index ae3e0e8a6bca9aa77dac63f1d42bdc33e0636198..0000000000000000000000000000000000000000 Binary files a/guide/git_add.png and /dev/null differ diff --git a/guide/gitee_right.png b/guide/gitee_right.png deleted file mode 100644 index 6e5eb57abe13188dca9a0da060c720fc79407b63..0000000000000000000000000000000000000000 Binary files a/guide/gitee_right.png and /dev/null differ diff --git a/guide/github_sign_up.png b/guide/github_sign_up.png deleted file mode 100644 index c5ab8f9afcaefe98f3fbe745e0536bf3ad69ee46..0000000000000000000000000000000000000000 Binary files a/guide/github_sign_up.png and /dev/null differ diff --git a/guide/img.png b/guide/img.png deleted file mode 100644 index d1069581ef8dfbf89e6825320c7ce609ed686419..0000000000000000000000000000000000000000 Binary files a/guide/img.png and /dev/null differ diff --git a/guide/import_project.png b/guide/import_project.png deleted file mode 100644 index 4ea09af1e0ced6aff3372a114d8f98033aeff843..0000000000000000000000000000000000000000 Binary files a/guide/import_project.png and /dev/null differ diff --git a/guide/input_git.png b/guide/input_git.png deleted file mode 100644 index ab19f8fb5b410a7cb476b7d4ae7c515383144675..0000000000000000000000000000000000000000 Binary files a/guide/input_git.png and /dev/null differ diff --git a/guide/input_user_info.png b/guide/input_user_info.png deleted file mode 100644 index 4979f3a954e78a2ac7a92f3b07dc1b84c729cc0b..0000000000000000000000000000000000000000 Binary files a/guide/input_user_info.png and /dev/null differ diff --git a/guide/lightly_login.png b/guide/lightly_login.png deleted file mode 100644 index 0b3d29426f7b6f1af7ed0d9a0fd1040566a89d28..0000000000000000000000000000000000000000 Binary files a/guide/lightly_login.png and /dev/null differ diff --git a/guide/login_with_github.png b/guide/login_with_github.png deleted file mode 100644 index ceb82272b3e321b73e7350905bc3a26bb4839b2c..0000000000000000000000000000000000000000 Binary files a/guide/login_with_github.png and /dev/null differ diff --git a/guide/merged_info.png b/guide/merged_info.png deleted file mode 100644 index 034d6ce21fd7a0016a8fbd41a398b7a0be267a53..0000000000000000000000000000000000000000 Binary files a/guide/merged_info.png and /dev/null differ diff --git a/guide/no_right.png b/guide/no_right.png deleted file mode 100644 index bbd9e69df54bd3cd00cc8e767a7c087d9e8f3f43..0000000000000000000000000000000000000000 Binary files a/guide/no_right.png and /dev/null differ diff --git a/guide/online_use.png b/guide/online_use.png deleted file mode 100644 index 2377532d57ad3d12f1d22732dde63f8ffa85ef61..0000000000000000000000000000000000000000 Binary files a/guide/online_use.png and /dev/null differ diff --git a/guide/open_project.png b/guide/open_project.png deleted file mode 100644 index 61bef51e8c51d87535e32a90d90b0877c28435b7..0000000000000000000000000000000000000000 Binary files a/guide/open_project.png and /dev/null differ diff --git a/guide/pr_page.png b/guide/pr_page.png deleted file mode 100644 index 6294d17f2da8463d95158072f767862da5505d9d..0000000000000000000000000000000000000000 Binary files a/guide/pr_page.png and /dev/null differ diff --git a/guide/pull_force.png b/guide/pull_force.png deleted file mode 100644 index cd9e1ee0f601df663a683260166e69a4f6105302..0000000000000000000000000000000000000000 Binary files a/guide/pull_force.png and /dev/null differ diff --git a/guide/pull_from_remote.png b/guide/pull_from_remote.png deleted file mode 100644 index 77d39cd216f75ce0d18c54f4f2aad5ce69feb0ab..0000000000000000000000000000000000000000 Binary files a/guide/pull_from_remote.png and /dev/null differ diff --git a/guide/select_cpp.png b/guide/select_cpp.png deleted file mode 100644 index d314353cf070f1f41edc82fcfe70da80b60c72da..0000000000000000000000000000000000000000 Binary files a/guide/select_cpp.png and /dev/null differ diff --git a/guide/webchat_login.png b/guide/webchat_login.png deleted file mode 100644 index 283964ef20d6aabcfd571c5a4cc6a8465e474e2d..0000000000000000000000000000000000000000 Binary files a/guide/webchat_login.png and /dev/null differ diff --git a/guide/wirte_code_windows.png b/guide/wirte_code_windows.png deleted file mode 100644 index 1dfe56ed0ca728814f2fbc36770875e5a39166ad..0000000000000000000000000000000000000000 Binary files a/guide/wirte_code_windows.png and /dev/null differ diff --git a/main.cpp b/main.cpp deleted file mode 100644 index 4562e17b782ed0a07d3274efd6e2cd31f395bce4..0000000000000000000000000000000000000000 --- a/main.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include - -int main(void) { - printf("Hello "); - - return 0; -} diff --git a/src/chap1/exp1-1.cpp b/src/chap1/exp1-1.cpp deleted file mode 100644 index 8e0fac9f5299b890d3f16c867982555e50d19994..0000000000000000000000000000000000000000 --- a/src/chap1/exp1-1.cpp +++ /dev/null @@ -1,53 +0,0 @@ -//文件名:exp1-1.cpp -#include -#include //clock_t, clock, CLOCKS_PER_SEC -#include - -//------方法1----------------------------------------------- -long add1(long n) //方法1:求1+2+...+n -{ - long i,sum=0; - for (i=1;i<=n;i++) - sum+=i; - return sum; -} - -void AddTime1(long n) //采用方法1的耗时统计 -{ - clock_t t; - long sum; - t=clock(); - sum=add1(n); - t=clock()-t; - printf("方法1:\n"); - printf(" 结果:1~%d之和:%ld\n",n,sum); - printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); -} - -//------方法2----------------------------------------------- -long add2(long n) //方法2:求1+2+...+n -{ - return n*(n+1)/2; -} -void AddTime2(long n) //采用方法2的耗时统计 -{ - clock_t t; - long sum; - t=clock(); - sum=add2(n); - t=clock()-t; - printf("方法2:\n"); - printf(" 结果:1~%d之和:%ld\n",n,sum); - printf(" 用时:%lf秒\n" ,((float)t)/CLOCKS_PER_SEC); -} -//------------------------------------------------------------ -int main() -{ - int n; - printf("n(大于1000000):"); - scanf("%d",&n); - if (n<1000000) return 0; - AddTime1(n); - AddTime2(n); - return 1; -} diff --git a/src/chap1/exp1-2.cpp b/src/chap1/exp1-2.cpp deleted file mode 100644 index cc6750048544030c906d526c37c491daf7e95831..0000000000000000000000000000000000000000 --- a/src/chap1/exp1-2.cpp +++ /dev/null @@ -1,43 +0,0 @@ -//文件名:exp1-2.cpp -#include -#include -double log2(double x) //求log2(x) -{ - return log10(x)/log10(2); -} -long exponent(int n) //求2^n -{ - long s=1; - for (int i=1;i<=n;i++) - s*=2; - return s; -} -long factorial(int n) //求n! -{ - long s=1; - for (int i=1;i<=n;i++) - s*=i; - return s; -} -void fun(int n) -{ - printf("log2(n) sqrt(n) n nlog2(n) n^2 n^3 2^n n!\n"); - printf("===========================================================================\n"); - for (int i=1;i<=n;i++) - { - printf("%5.2f\t",log2(double(i))); - printf("%5.2f\t",sqrt(i)); - printf("%2d\t",i); - printf("%7.2f\t",i*log2(double(i))); - printf("%5d\t",i*i); - printf("%7d\t",i*i*i); - printf("%8d\t",exponent(i)); - printf("%10d\n",factorial(i)); - } -} -int main() -{ - int n=10; - fun(n); - return 1; -} diff --git a/src/chap1/exp1-3.cpp b/src/chap1/exp1-3.cpp deleted file mode 100644 index d4f41ac889e3d03d92368c389e39e56330def977..0000000000000000000000000000000000000000 --- a/src/chap1/exp1-3.cpp +++ /dev/null @@ -1,62 +0,0 @@ -//文件名:exp1-3.cpp -#include -#include //clock_t, clock, CLOCKS_PER_SEC -#include - -//------方法1----------------------------------------------- -bool prime1(long n) //方法1:判断正整数n是否为素数 -{ - long i; - for (i=2;i -long Sum(int n) -{ - long sum=0,fact=1; - for (int i=1;i<=n;i++) - { - fact*=i; - sum+=fact; - } - return sum; -} -//------------------------------------------------------------ -int main() -{ - int n; - printf("n(3-20):"); - scanf("%d",&n); - if (n<3 || n>20) return 0; - printf("1!+2!+…+%d!=%ld\n",n,Sum(n)); - return 1; -} diff --git a/src/chap1/lc-1.cpp b/src/chap1/lc-1.cpp deleted file mode 100644 index f59cca343a52578807574ee513b0cf5fd947fcc3..0000000000000000000000000000000000000000 --- a/src/chap1/lc-1.cpp +++ /dev/null @@ -1,14 +0,0 @@ -class Solution { -public: - vector twoSum(vector& nums, int target) { - unordered_map m; - for (int i = 0; i < nums.size(); ++i) { - int t = target - nums[i]; - if (m.count(t)) return { m[t], i }; - m[nums[i]] = i; - } - return {}; - } - - -}; \ No newline at end of file diff --git a/src/chap10/exp10-1.cpp b/src/chap10/exp10-1.cpp deleted file mode 100644 index b2f967f5df9e876608eafca859f59f513e6cca41..0000000000000000000000000000000000000000 --- a/src/chap10/exp10-1.cpp +++ /dev/null @@ -1,29 +0,0 @@ -//ļ:exp10-1.cpp -#include "seqlist.cpp" //˳Ļ㷨 -void InsertSort(RecType R[],int n) //R[0..n-1]ֱӲ -{ int i, j; RecType tmp; - for (i=1;i=0 && R[j].key>tmp.key); - R[j+1]=tmp; //j+1R[i] - } - DispList(R,n); - } -} -int main() -{ int n=10; - KeyType a[]={9,8,7,6,5,4,3,2,1,0}; - RecType R[MAXL]; - CreateList(R,a,n); - printf("ǰ: "); DispList(R,n); - InsertSort(R,n); - printf(": "); DispList(R,n); - return 1; -} diff --git a/src/chap10/exp10-10.cpp b/src/chap10/exp10-10.cpp deleted file mode 100644 index f1a85206763a5262e2b6d5fa3ac5c5f78a116ea7..0000000000000000000000000000000000000000 --- a/src/chap10/exp10-10.cpp +++ /dev/null @@ -1,64 +0,0 @@ -//ļ:exp10-10.cpp -#include -#include -#define MaxL 100 //ַ -typedef struct node -{ - int start,length; -} RecType; //ָʾַλõļ¼ -int StringComp(char S[],RecType A[],int s1,RecType tmp) //ȽַĴС -{ - char str1[MaxL],str2[MaxL]; - int i,j; - for (j=0,i=A[s1].start;ii && StringComp(S,A,j,tmp)>0) j--; - A[i]=A[j]; - while (i -#include -#include -#define MaxLen 9 //ʵ󳤶 -#define Radix 27 //rdΪ27,ֱӦ' ','a','z' -typedef char String[MaxLen+1]; //StringΪַ -typedef struct node -{ String word; - struct node *next; -} LinkNode; //ڵ -void DispWord(String R[],int n) // -{ - int i; - printf(" "); - for (i=0;iword,R[i]); - p->next=NULL; - if (head[k]==NULL) - { head[k]=p; - tail[k]=p; - } - else - { tail[k]->next=p; - tail[k]=p; - } - } -} -void Collect(String R[],LinkNode *head[]) -//νǿնеĽڵռͷŸǿնенڵ -{ - int k=0,i; - LinkNode *pre,*p; - for (i=0;inext; - while (p!=NULL) - { - strcpy(R[k++],pre->word); - free(pre); - pre=p; - p=p->next; - } - strcpy(R[k++],pre->word); - free(pre); - } - } -} -void RadixSort(String R[],int n) //R[0..n-1]л -{ - LinkNode *head[Radix],*tail[Radix]; //Radix - int i,j; - for (i=MaxLen-1;i>=0;i--) //ӵλλd - { for (j=0;j -#include -#include -#define MAXR 10 -#define MaxSize 10 -typedef struct node -{ char xm[10]; // - char xb; //Ա m: f:Ů - char bh[6]; // - struct node *next; -} StudNode; -typedef struct -{ char xm[10]; // - char xb; //Ա m: f:Ů - char bh[6]; // -} StudType; -void CreateLink(StudNode *&p,StudType A[],int n) // -{ int i; - StudNode *s,*t; - p=NULL; - for (i=0;ixm,A[i].xm); - s->xb=A[i].xb; - strcpy(s->bh,A[i].bh); - if (p==NULL) - { p=s; - t=s; - } - else - { t->next=s; - t=s; - } - } - t->next=NULL; -} -void DispLink(StudNode *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) printf("\n"); - i++; - } - printf("\n"); -} -void DestroyLink(StudNode *p) //ٵ -{ - StudNode *pre=p,*q=pre->next; - while (q!=NULL) - { - free(pre); - pre=q; - q=q->next; - } - free(pre); -} -void RadixSort1(StudNode *&p,int r,int d) -//Ա:ֻһ.pΪָ,rΪ,dΪؼλ -{ StudNode *head[MAXR],*tail[MAXR],*t;//ӵβָ - int j,k; - printf("Ա\n"); - for (j=0;jxb=='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;jnext=head[j]; - t=tail[j]; - } - } - t->next=NULL; //һڵnextNULL - DispLink(p); -} -void RadixSort2(StudNode *&p,int r,int d) -//԰Ž:pΪָ,rΪ,dΪؼλ -{ StudNode *head[MAXR],*tail[MAXR],*t;//ӵβָ - int i,j,k; - printf("Ž\n"); - for (i=3;i>=2;i--) //ӵλλd - { for (j=0;jbh[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;jnext=head[j]; - t=tail[j]; - } - } - t->next=NULL; //һڵnextNULL - printf(" %d:\n",d-i+2);DispLink(p); - } -} -void Sort(StudType A[],int n) //źԱ -{ - StudNode *p; - CreateLink(p,A,n); - printf("ǰ:\n");DispLink(p); - RadixSort1(p,2,1); //Ա - RadixSort2(p,10,2); //԰ŵĺλ - printf(":\n");DispLink(p); - DestroyLink(p); -} -int main() -{ - int n=10; - StudType A[MaxSize]={{"",'m',"1003"},{"",'m',"1020"}, - {"",'f',"1022"},{"Ӣ",'f',"1003"}, - {"Ź",'m',"1021"},{"ǿ",'m',"1002"}, - {"",'f',"1002"},{"»",'m',"1001"}, - {"",'f',"1021"},{"ǿ",'m',"1022"}}; - Sort(A,n); - return 1; -} diff --git a/src/chap10/exp10-13.cpp b/src/chap10/exp10-13.cpp deleted file mode 100644 index 994ecc0c4c9ce0a52e88e9148f4863b38ee8277d..0000000000000000000000000000000000000000 --- a/src/chap10/exp10-13.cpp +++ /dev/null @@ -1,347 +0,0 @@ -//ļ:exp10-13.cpp -#include -#include -#include //clock_t, clock, CLOCKS_PER_SEC -#define MaxSize 50001 -typedef int KeyType; -//------------------------------------------- -void swap(KeyType &x,KeyType &y) //xy -{ - KeyType tmp=x; - x=y; y=tmp; -} -void initial(int R[],int low,int high) //R[low..highе -{ - int i; - srand((unsigned)time(NULL)); - for (i=low;iR[i+1]) - return false; - return true; -} -//-------ֱӲ---------------------------- -void InsertSort(KeyType R[],int n) -{ int i, j; KeyType tmp; - for (i=1;i=0 && R[j]>tmp); - R[j+1]=tmp; //j+1R[i] - } - } -} -void InsertSortTime(KeyType R[],int n) //ֱӲʱ -{ - clock_t t; - printf("ֱӲ\t"); - t=clock(); - InsertSort(R,n); - t=clock()-t; - printf ("%lf" ,((float)t)/CLOCKS_PER_SEC); - if (test(R,0,n-1)) - printf("\tȷ\n"); - else - printf("\t\n"); -} -//------۰----------------------------- -void BinInsertSort(KeyType R[],int n) -{ int i, j, low, high, mid; - KeyType tmp; - for (i=1;i=high+1;j--) //нԪغ - R[j+1]=R[j]; - R[high+1]=tmp; //tmp - } - } -} -void BinInsertSortTime(KeyType R[],int n) //۰ʱ -{ - clock_t t; - printf("۰\t"); - t=clock(); - BinInsertSort(R,n); - t=clock()-t; - printf ("%lf" ,((float)t)/CLOCKS_PER_SEC); - if (test(R,0,n-1)) - printf("\tȷ\n"); - else - printf("\t\n"); -} -//-----------ϣ㷨-------------------------------- -void ShellSort(KeyType R[],int n) //ϣ㷨 -{ int i,j,d; - KeyType tmp; - d=n/2; //óֵ - while (d>0) - { for (i=d;i=0 && tmpi;j--) //λR[i],ѭn-i-1 - if (R[j]i && R[j]>=tmp) - j--; //ɨ,һСtmpR[j] - if(j>i) - { R[i]=R[j]; //ҵR[j],R[i] - i++; - } - while (i=1;i--) //ѭʼ,sift㷨 n/2 - sift(R,i,n); - for (i=n;i>=2;i--) //n-1,ÿһ˶Ԫظ1 - { swap(R[1],R[i]); //һԪR[1] - sift(R,1,i-1); //R[1..i-1]ɸѡ,õi-1ڵĶ - } -} -void HeapSortTime(KeyType R[],int n) //㷨ʱ -{ - clock_t t; - printf(" \t"); - t=clock(); - HeapSort(R,n); - t=clock()-t; - printf ("%lf" ,((float)t)/CLOCKS_PER_SEC); - if (test(R,1,n)) - printf("\tȷ\n"); - else - printf("\t\n"); -} -//--------·鲢㷨----------------------------------- -void Merge(KeyType R[],int low,int mid,int high) //鲢R[low..high] -{ KeyType *R1; - int i=low,j=mid+1,k=0; //kR1±,ijֱΪ12ε± - R1=(KeyType *)malloc((high-low+1)*sizeof(KeyType)); //̬ռ - while (i<=mid && j<=high) //ڵ1κ͵2ξδɨʱѭ - if (R[i]<=R[j]) //1еԪطR1 - { R1[k]=R[i]; - i++;k++; - } - else //2еԪطR1 - { R1[k]=R[j]; - j++;k++; - } - while (i<=mid) //1²ָƵR1 - { R1[k]=R[i]; - i++;k++; - } - while (j<=high) //2²ָƵR1 - { R1[k]=R[j]; - j++;k++; - } - for (k=0,i=low;i<=high;k++,i++) //R1ƻR - R[i]=R1[k]; - free(R1); -} -void MergePass(KeyType R[],int length,int n) //нһ˹鲢 -{ int i; - for (i=0;i+2*length-1=high+1;j--) //нԪغ - R[j+1]=R[j]; - R[high+1]=tmp; //tmp - } - DispList(R,n); - } -} - -int main() -{ - int n=10; - RecType R[MAXL]; - KeyType a[]={9,8,7,6,5,4,3,2,1,0}; - CreateList(R,a,n); - printf("ǰ:"); DispList(R,n); - BinInsertSort(R,n); - printf(":"); DispList(R,n); - return 1; -} - - diff --git a/src/chap10/exp10-3.cpp b/src/chap10/exp10-3.cpp deleted file mode 100644 index 5d00eb066d56ef92f8c613b9d8bf0a6b3c477cc9..0000000000000000000000000000000000000000 --- a/src/chap10/exp10-3.cpp +++ /dev/null @@ -1,32 +0,0 @@ -//ļ:exp10-3.cpp -#include "seqlist.cpp" //˳Ļ㷨 - -void ShellSort(RecType R[],int n) //R[0..n-1]ϣ -{ int i,j,d; - RecType tmp; - d=n/2; //óֵ - while (d>0) - { for (i=d;i=0 && tmp.keyi;j--) //λR[i],ѭn-i-1 - if (R[j].keyi && R[j].key>=tmp.key) - j--; //ɨ,һСtmp.keyR[j] - if(j>i) - { R[i]=R[j]; //ҵR[j],R[i] - i++; - } - while (i=1;i--) //ѭʼ - Sift(R,i,n); - printf("ʼ:");DispHeap(R,1,n);printf("\n"); //ʼ - for (i=n;i>=2;i--) //n-1ѭ, - { - printf("%d:",count++); - printf(" %d%d,%d ",R[i].key,R[1].key,R[1].key); - swap(R[1],R[i]); //һԪͬǰR[1]Ի - printf(" :"); //ÿһ˵ - for (j=1;j<=n;j++) - printf("%2d",R[j].key); - printf("\n"); - Sift(R,1,i-1); //ɸѡR[1]ڵ,õi-1ڵĶ - printf("ɸѡõ:");DispHeap(R,1,i-1);printf("\n"); - } -} -int main() -{ - int n=10; - RecType R[MAXL]; - KeyType a[]={6,8,7,9,0,1,3,2,4,5}; - CreateList1(R,a,n); - printf("ǰ:"); DispList1(R,n); - HeapSort(R,n); - printf(":"); DispList1(R,n); - return 1; -} diff --git a/src/chap10/exp10-8.cpp b/src/chap10/exp10-8.cpp deleted file mode 100644 index 3e9f81c2f4f2f0e60becddcdce05b2cff44ac56a..0000000000000000000000000000000000000000 --- a/src/chap10/exp10-8.cpp +++ /dev/null @@ -1,68 +0,0 @@ -//ļ:exp10-8.cpp -#include "seqlist.cpp" //˳Ļ㷨 -#include - -void Merge(RecType R[],int low,int mid,int high) -//һι鲢R[low..mid]R[mid+1..high]鲢ΪһR[low..high] -{ - RecType *R1; - int i=low,j=mid+1,k=0; //kR1±,ijֱΪ12ε± - R1=(RecType *)malloc((high-low+1)*sizeof(RecType)); //̬ռ - while (i<=mid && j<=high) //ڵ1κ͵2ξδɨʱѭ - if (R[i].key<=R[j].key) //1еļ¼R1 - { - R1[k]=R[i]; - i++;k++; - } - else //2еļ¼R1 - { - R1[k]=R[j]; - j++;k++; - } - while (i<=mid) //1²ָƵR1 - { - R1[k]=R[i]; - i++;k++; - } - while (j<=high) //2²ָƵR1 - { - R1[k]=R[j]; - j++;k++; - } - for (k=0,i=low;i<=high;k++,i++) //R1ƻR - R[i]=R1[k]; -} -int count=1; //ȫֱ -void MergePass(RecType R[],int length,int n) //ʵһ˹鲢 -{ - int i; - printf("%d˹鲢:",count++); - for (i=0;i+2*length-1 -#include -#define MAXE 20 //ԱԪظ -#define MAXR 10 //ȡֵ -typedef struct node -{ - int key; //¼Ĺؼ - struct node *next; -} NodeType; - -void CreateLink(NodeType *&p,int a[],int n) //β巨 -{ - NodeType *s,*t; - for (int i=0;ikey=a[i]; - if (i==0) - { - p=s;t=s; - } - else - { - t->next=s;t=s; - } - } - t->next=NULL; -} -void DispLink(NodeType *p) // -{ - while (p!=NULL) - { - printf("%4d",p->key); - p=p->next; - } - printf("\n"); -} -void DestroyLink(NodeType *p) //ٵ -{ - NodeType *pre=p,*q=pre->next; - while (q!=NULL) - { - free(pre); - pre=q; - q=q->next; - } - free(pre); -} -int keyi(int s,int i) //ֵsӵ͵λȡiλ -{ - for (int j=0;jkey,i); //pڵؼֵĵiλk - if (head[k]==NULL) //pڵ䵽k - { - head[k]=p; - tail[k]=p; - } - else - { - tail[k]->next=p; - tail[k]=p; - } - p=p->next; //ɨһڵ - } - p=NULL; - for (j=0;jnext=head[j]; - t=tail[j]; - } - } - t->next=NULL; //һڵnextNULL - printf("%dλ:",i+1); DispLink(p); - } -} - -int main() -{ - int n=10; - NodeType *p; - int a[]={75,223,98,44,157,2,29,164,38,82}; - CreateLink(p,a,n); - printf(" ǰ:"); DispLink(p); - RadixSort(p,10,3); - printf(" :"); DispLink(p); - DestroyLink(p); - return 1; -} diff --git a/src/chap10/seqlist.cpp b/src/chap10/seqlist.cpp deleted file mode 100644 index 71926acd8ac228bddbbe70b12b27287981013226..0000000000000000000000000000000000000000 --- a/src/chap10/seqlist.cpp +++ /dev/null @@ -1,40 +0,0 @@ -//˳㷨 -#include -#define MAXL 100 //󳤶 -typedef int KeyType; //ؼΪint -typedef char InfoType; - -typedef struct -{ KeyType key; //ؼ - InfoType data; //ΪInfoType -} RecType; //Ԫص - -void swap(RecType &x,RecType &y) //xy -{ - RecType tmp=x; - x=y; y=tmp; -} - -void CreateList(RecType R[],KeyType keys[],int n) //˳ -{ - for (int i=0;i -#define K 5 //5·ƽ鲢 -#define MINKEY -32768 //Сؼֵ- -typedef int KeyType; -typedef KeyType LoserTree; //ΪLoserTree[K] -KeyType b[K]; //bдеļ¼(ؼ) -int count=1; //ȫֱ -void Adjust(LoserTree ls[K],int s) -//شҶӽڵb[s]ڵls[0]· -{ int i,t; - t=(s+K)/2; //ls[t]b[s]˫׽ڵ - while (t>0) - { - if(b[s]>b[ls[t]]) //˫׽ڵls[t]ȽС(ʤ) - { - i=s; //iָ() - s=ls[t]; //sָʾµʤ - ls[t]=i; //()˫׽ڵ - } - t=t/2; //ϵ - } - ls[0]=s; //ھڵС -} -void display(LoserTree ls[K]) //ls -{ int i; - printf(":"); - for (i=0;i=0;--i) //δb[K-1]b[K-2]b[0] - { - printf("(%d)b[%d](%d)е",count++,i,b[i]); - Adjust(ls,i); - display(ls); - } -} - -int main() -{ - LoserTree ls[K]; - int n=5; - KeyType a[]={17,5,10,29,15}; - printf("%d·鲢Ĺؼ:",K); - for (int i=0;i -#define MaxSize 100 -#define MAXKEY 32767 //ؼֵ -#define K 5 //ڴ湤ɵļ¼ -typedef int KeyType; //ؼ -typedef struct -{ KeyType recs[MaxSize]; //ļе - int length; //ļʵʼ¼ - int currec; //ŵǰλ -} FileType; //ļ -FileType Fi; //ļ,Ϊȫֱ -KeyType R[K+1]; //Ŵ -void initial() //ļʼ -{ int n=18,i; - KeyType a[]={15,4,97,64,17,32,108,44,76,9,39,82,56,31,80,73,255,68}; - for (i=0;i=1;i--) //ʼ - sift(i,K); - printf("ͷ%d¼Ĵ:",K); dispHeap(); - while (GetaRec(r)) //ļFiȡļ¼ - { - printf(" %d:",r); - if (r -#include -#include -#include -#define MaxSize 50 //ÿļ¼ -#define MAXKEY 32767 //ؼֵ -#define W 5 //ڴ湤ɵļ¼ -typedef int LoserTree; //ڵͣȫҲҶ,ɲ˳洢ṹ -typedef int InfoType; // -typedef int KeyType; //ؼΪ -typedef struct -{ KeyType key; //ؼ - InfoType otherinfo; //,ж -} RecType; //ļļ¼ -typedef struct -{ RecType recs[MaxSize]; //ļе - int length; //ļʵʼ¼ - int currec; //ŵǰλ -} FileType; //ļ -typedef struct -{ RecType rec; //ż¼ - int rnum; //鲢εĶκ -} WorkArea; //ڴ湤Ԫ,ΪW -FileType Fi; //ļ,Ϊȫֱ -FileType Fo; //ļ,Ϊȫֱ -void initial() //ļʼ -{ int n=19,i; - KeyType a[]={15,4,97,64,17,32,108,44,76,9,39,82,56,31,80,73,255,68,MAXKEY}; - for (i=0;i0;t=t/2,p=ls[t]) - if (wa[p].rnum=0;i--) - { - Fi.currec++; //ļһ¼ - wa[i].rec=Fi.recs[Fi.currec]; - wa[i].rnum=1; //κΪ1 - Select_MiniMax(ls,wa,i); // - } -} -void get_run(LoserTree ls[W],WorkArea wa[W],int rc,int &rmax) -//һʼ鲢 -{ - int q; - KeyType minimax; //ǰСؼ - while (wa[ls[0]].rnum==rc) //ѡõĵǰС¼ǰʱ - { - q=ls[0]; //qָʾǰС¼waеλ - minimax=wa[q].rec.key; - Fo.currec++; //ѡõĵǰС¼дļ - Fo.length++; - Fo.recs[Fo.currec]=wa[q].rec; - Fi.currec++; //ļһ¼ - wa[q].rec=Fi.recs[Fi.currec]; - if (Fi.currec>=Fi.length-1) //ļ,¼(rmax+1) - { - wa[q].rnum=rmax+1; - wa[q].rec.key=MAXKEY; - } - else //ļǿʱ - { - if(wa[q].rec.key -#define MaxSize 20 //ÿļ¼ -#define K 5 //5·ƽ鲢 -#define MAXKEY 32767 //ؼֵ -#define MINKEY -32768 //Сؼֵ- -typedef int InfoType; -typedef int KeyType; -typedef struct //¼ -{ - KeyType key; //ؼ - InfoType otherinfo; //,ж -} RecType; -typedef struct -{ - RecType recs[MaxSize]; - int currec; -} FileType; //ļ -typedef int LoserTree; //ΪLoserTree[K] -RecType b[K]; //bдŸȡĵǰ¼ -FileType F[K]; //ļ¼ -void initial() //ʼļ¼F -{ - int i; //5ʼļ,ǰ¼Ϊ-1 - F[0].recs[0].key=17; - F[0].recs[1].key=21; - F[0].recs[2].key=MAXKEY; - F[1].recs[0].key=5; - F[1].recs[1].key=44; - F[1].recs[2].key=MAXKEY; - F[2].recs[0].key=10; - F[2].recs[1].key=12; - F[2].recs[2].key=MAXKEY; - F[3].recs[0].key=29; - F[3].recs[1].key=32; - F[3].recs[2].key=MAXKEY; - F[4].recs[0].key=15; - F[4].recs[1].key=56; - F[4].recs[2].key=MAXKEY; - for (i=0;i0) - { - if(b[s].key>b[ls[t]].key) - { - i=s; - s=ls[t]; //sָʾµʤ - ls[t]=i; - } - t=t/2; - } - ls[0]=s; -} -void display(LoserTree ls[K]) //ls -{ - int i; - printf(":"); - for (i=0;i=0;--i) //δb[K-1]b[K-2]b[0] - Adjust(ls,i); -} -void K_Merge(LoserTree ls[K]) //ðlsK·鲢 -{ - int i,q; - for(i=0;i -#include -typedef int ElemType; -typedef struct DNode //定义双链表结点类型 -{ - ElemType data; - struct DNode *prior; //指向前驱结点 - struct DNode *next; //指向后继结点 -} DLinkNode; -void CreateListF(DLinkNode *&L,ElemType a[],int n) //头插法建立循环双链表 -{ - DLinkNode *s; - L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点 - L->next=NULL; - for (int i=0;idata=a[i]; - s->next=L->next; //将结点s插在原开始结点之前,头结点之后 - if (L->next!=NULL) L->next->prior=s; - L->next=s;s->prior=L; - } - s=L->next; - while (s->next!=NULL) //查找尾结点,由s指向它 - s=s->next; - s->next=L; //尾结点next域指向头结点 - L->prior=s; //头结点的prior域指向尾结点 - -} -void CreateListR(DLinkNode *&L,ElemType a[],int n) //尾插法建立循环双链表 -{ - DLinkNode *s,*r; - L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点 - L->next=NULL; - r=L; //r始终指向尾结点,开始时指向头结点 - for (int i=0;idata=a[i]; - r->next=s;s->prior=r; //将结点s插入结点r之后 - r=s; - } - r->next=L; //尾结点next域指向头结点 - L->prior=r; //头结点的prior域指向尾结点 -} -void InitList(DLinkNode *&L) //初始化线性表 -{ - L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点 - L->prior=L->next=L; -} -void DestroyList(DLinkNode *&L) //销毁线性表 -{ - DLinkNode *pre=L,*p=pre->next; - while (p!=L) - { - free(pre); - pre=p; //pre、p同步后移一个结点 - p=pre->next; - } - free(pre); //此时p=L,pre指向尾结点,释放它 -} -bool ListEmpty(DLinkNode *L) //判线性表是否为空表 -{ - return(L->next==L); -} -int ListLength(DLinkNode *L) //求线性表的长度 -{ - DLinkNode *p=L; - int i=0; - while (p->next!=L) - { - i++; - p=p->next; - } - return(i); //循环结束,p指向尾结点,其序号i为结点个数 -} -void DispList(DLinkNode *L) //输出线性表 -{ - DLinkNode *p=L->next; - while (p!=L) - { - printf("%c ",p->data); - p=p->next; - } - printf("\n"); -} -bool GetElem(DLinkNode *L,int i,ElemType &e) //求线性表中第i个元素值 -{ - int j=1; - DLinkNode *p=L->next; - if (i<=0 || L->next==L) - return false; //i错误或者L为空表返回假 - if (i==1) //i=1作为特殊情况处理 - { - e=L->next->data; - return true; - } - else //i不为1时 - { - while (j<=i-1 && p!=L) //查找第i个结点p - { - j++; - p=p->next; - } - if (p==L) //没有找到第i个节,返回假 - return false; - else //找到了第i个节,返回真 - { - e=p->data; - return true; - } - } -} -int LocateElem(DLinkNode *L,ElemType e) //查找第一个值域为e的元素序号 -{ - int i=1; - DLinkNode *p=L->next; - while (p!=NULL && p->data!=e) - { - i++; - p=p->next; - } - if (p==NULL) //不存在值为e的结点,返回0 - return(0); - else //存在值为e的结点,返回其逻辑序号i - return(i); -} -bool ListInsert(DLinkNode *&L,int i,ElemType e) //插入第i个元素 -{ - int j=1; - DLinkNode *p=L,*s; - if (i<=0) return false; //i错误返回假 - if (p->next==L) //原双链表为空表时 - { - s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点s - s->data=e; - p->next=s;s->next=p; - p->prior=s;s->prior=p; - return true; - } - else if (i==1) //L不为空,i=1作为特殊情况处理 - { - s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点s - s->data=e; - s->next=p->next;p->next=s; //将结点s插入到结点p之后 - s->next->prior=s;s->prior=p; - return true; - } - else //i不为1时 - { - p=L->next; - while (j<=i-2 && p!=L) //查找第i-1个结点p - { j++; - p=p->next; - } - if (p==L) //未找到第i-1个结点 - return false; - else //找到第i-1个结点*p - { - s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点s - s->data=e; - s->next=p->next; //将结点s插入到结点p之后 - if (p->next!=NULL) p->next->prior=s; - s->prior=p; - p->next=s; - return true; - } - } -} -bool ListDelete(DLinkNode *&L,int i,ElemType &e) //删除第i个元素 -{ - int j=1; - DLinkNode *p=L,*q; - if (i<=0 || L->next==L) - return false; //i错误或者为空表返回假 - if (i==1) //i==1作为特殊情况处理 - { - q=L->next; //删除第1个结点 - e=q->data; - L->next=q->next; - q->next->prior=L; - free(q); - return true; - } - else //i不为1时 - { - p=L->next; - while (j<=i-2 && p!=NULL) //查找到第i-1个结点p - { - j++; - p=p->next; - } - if (p==NULL) //未找到第i-1个结点 - return false; - else //找到第i-1个结点p - { - q=p->next; //q指向要删除的结点 - if (q==NULL) return 0; //不存在第i个结点 - e=q->data; - p->next=q->next; //从单链表中删除q结点 - if (p->next!=NULL) p->next->prior=p; - free(q); //释放q结点 - return true; - } - } -} diff --git a/src/chap2/clinklist.cpp b/src/chap2/clinklist.cpp deleted file mode 100644 index a6cd436d23efb2777cdcf428bc5af9aca5abcfd2..0000000000000000000000000000000000000000 --- a/src/chap2/clinklist.cpp +++ /dev/null @@ -1,190 +0,0 @@ -//循环单链表运算算法 -#include -#include -typedef int ElemType; -typedef struct LNode //定义单链表结点类型 -{ - ElemType data; - struct LNode *next; -} LinkNode; -void CreateListF(LinkNode *&L,ElemType a[],int n) //头插法建立循环单链表 -{ - LinkNode *s;int i; - L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点 - L->next=NULL; - for (i=0;idata=a[i]; - s->next=L->next; //将结点s插在原开始结点之前,头结点之后 - L->next=s; - } - s=L->next; - while (s->next!=NULL) //查找尾结点,由s指向它 - s=s->next; - s->next=L; //尾结点next域指向头结点 - -} -void CreateListR(LinkNode *&L,ElemType a[],int n) //尾插法建立循环单链表 -{ - LinkNode *s,*r;int i; - L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点 - L->next=NULL; - r=L; //r始终指向终端结点,开始时指向头结点 - for (i=0;idata=a[i]; - r->next=s; //将结点s插入结点r之后 - r=s; - } - r->next=L; //尾结点next域指向头结点 -} -void InitList(LinkNode *&L) //初始化线性表 -{ - L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点 - L->next=L; -} -void DestroyList(LinkNode *&L) //销毁线性表 -{ - LinkNode *pre=L,*p=pre->next; - while (p!=L) - { - free(pre); - pre=p; //pre、p同步后移一个结点 - p=pre->next; - } - free(pre); //此时p=L,pre指向尾结点,释放它 -} -bool ListEmpty(LinkNode *L) //判线性表是否为空表 -{ - return(L->next==L); -} -int ListLength(LinkNode *L) //求线性表的长度 -{ - LinkNode *p=L;int i=0; //p指向头结点,n置为0(即头结点的序号为0) - while (p->next!=L) - { - i++; - p=p->next; - } - return(i); //循环结束,p指向尾结点,其序号n为结点个数 -} -void DispList(LinkNode *L) //输出线性表 -{ - LinkNode *p=L->next; - while (p!=L) //p不为L,输出p结点的data域 - { - printf("%c ",p->data); - p=p->next; - } - printf("\n"); -} -bool GetElem(LinkNode *L,int i,ElemType &e) //求线性表中第i个元素值 -{ int j=1; - LinkNode *p=L->next; - if (i<=0 || L->next==L) //i错误或者空表返回假 - return false; - if (i==1) //求第1个结点值,作为特殊情况处理 - { - e=L->next->data; - return true; - } - else //i不为1时 - { - while (j<=i-1 && p!=L) //找第i个结点p - { - j++; - p=p->next; - } - if (p==L) //没有找到返回假 - return false; - else //找到了提取它的值并返回整 - { - e=p->data; - return true; - } - } -} - -int LocateElem(LinkNode *L,ElemType e) //查找第一个值域为e的元素序号 -{ - LinkNode *p=L->next; - int i=1; - while (p!=L && p->data!=e) //查找第一个值域为e的结点p - { - p=p->next; - i++; //i对应结点p的序号 - } - if (p==L) - return(0); //没有找到返回0 - else - return(i); //找到了返回其序号 -} -bool ListInsert(LinkNode *&L,int i,ElemType e) //插入第i个元素 -{ - int j=1; - LinkNode *p=L,*s; - if (i<=0) return false; //i错误返回假 - if (p->next==L || i==1) //原单链表为空表或i=1作为特殊情况处理 - { - s=(LinkNode *)malloc(sizeof(LinkNode)); //创建新结点s - s->data=e; - s->next=p->next; //将结点s插入到结点p之后 - p->next=s; - return true; - } - else - { - p=L->next; - while (j<=i-2 && p!=L) //找第i-1个结点p - { - j++; - p=p->next; - } - if (p==L) //未找到第i-1个结点 - return false; - else //找到第i-1个结点p - { - s=(LinkNode *)malloc(sizeof(LinkNode)); //创建新结点s - s->data=e; - s->next=p->next; //将结点s插入到结点p之后 - p->next=s; - return true; - } - } -} -bool ListDelete(LinkNode *&L,int i,ElemType &e) //删除第i个元素 -{ - int j=1; - LinkNode *p=L,*q; - if (i<=0 || L->next==L) - return false; //i错误或者空表返回假 - if (i==1) //i=1作为特殊情况处理 - { - q=L->next; //删除第1个结点 - e=q->data; - L->next=q->next; - free(q); - return true; - } - else //i不为1时 - { - p=L->next; - while (j<=i-2 && p!=L) //找第i-1个结点p - { - j++; - p=p->next; - } - if (p==L) //未找到第i-1个结点 - return false; - else //找到第i-1个结点p - { - q=p->next; //q指向要删除的结点 - e=q->data; - p->next=q->next; //从单链表中删除q结点 - free(q); //释放q结点 - return true; - } - } -} diff --git a/src/chap2/dlinklist.cpp b/src/chap2/dlinklist.cpp deleted file mode 100644 index a724181468c4f44f66cb49fde5e65e5dd5668ef7..0000000000000000000000000000000000000000 --- a/src/chap2/dlinklist.cpp +++ /dev/null @@ -1,158 +0,0 @@ -//双链表运算算法 -#include -#include -typedef int ElemType; -typedef struct DNode -{ - ElemType data; - struct DNode *prior; //指向前驱结点 - struct DNode *next; //指向后继结点 -} DLinkNode; //声明双链表结点类型 -void CreateListF(DLinkNode *&L,ElemType a[],int n) //头插法建双链表 -{ - DLinkNode *s; - L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点 - L->prior=L->next=NULL; - for (int i=0;idata=a[i]; - s->next=L->next; //将结点s插在原开始结点之前,头结点之后 - if (L->next!=NULL) L->next->prior=s; - L->next=s;s->prior=L; - } -} -void CreateListR(DLinkNode *&L,ElemType a[],int n) //尾插法建双链表 -{ - DLinkNode *s,*r; - L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点 - L->prior=L->next=NULL; - r=L; //r始终指向终端结点,开始时指向头结点 - for (int i=0;idata=a[i]; - r->next=s;s->prior=r; //将结点s插入结点r之后 - r=s; - } - r->next=NULL; //尾结点next域置为NULL -} -void InitList(DLinkNode *&L) //初始化线性表 -{ - L=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建头结点 - L->prior=L->next=NULL; -} -void DestroyList(DLinkNode *&L) //销毁线性表 -{ - DLinkNode *pre=L,*p=pre->next; - while (p!=NULL) - { - free(pre); - pre=p; //pre、p同步后移一个结点 - p=pre->next; - } - free(p); -} -bool ListEmpty(DLinkNode *L) //判线性表是否为空表 -{ - return(L->next==NULL); -} -int ListLength(DLinkNode *L) //求线性表的长度 -{ - DLinkNode *p=L; - int i=0; //p指向头结点,i设置为0 - while (p->next!=NULL) //找尾结点p - { - i++; //i对应结点p的序号 - p=p->next; - } - return(i); -} -void DispList(DLinkNode *L) //输出线性表 -{ - DLinkNode *p=L->next; - while (p!=NULL) - { - printf("%c ",p->data); - p=p->next; - } - printf("\n"); -} -bool GetElem(DLinkNode *L,int i,ElemType &e) //求线性表中第i个元素值 -{ - int j=0; - DLinkNode *p=L; - if (i<=0) return false; //i错误返回假 - while (jnext; - } - if (p==NULL) //没有找到返回假 - return false; - else //找到了提取值并返回真 - { - e=p->data; - return true; - } -} -int LocateElem(DLinkNode *L,ElemType e) //查找第一个值域为e的元素序号 -{ - int i=1; - DLinkNode *p=L->next; - while (p!=NULL && p->data!=e) //查找第一个值域为e的结点p - { - i++; //i对应结点p的序号 - p=p->next; - } - if (p==NULL) //没有找到返回0 - return(0); - else //找到了返回其序号 - return(i); -} -bool ListInsert(DLinkNode *&L,int i,ElemType e) //插入第i个元素 -{ - int j=0; - DLinkNode *p=L,*s; //p指向头结点,j设置为0 - if (i<=0) return false; //i错误返回假 - while (jnext; - } - if (p==NULL) //未找到第i-1个结点 - return false; - else //找到第i-1个结点p - { - s=(DLinkNode *)malloc(sizeof(DLinkNode)); //创建新结点s - s->data=e; - s->next=p->next; //将结点s插入到结点p之后 - if (p->next!=NULL) - p->next->prior=s; - s->prior=p; - p->next=s; - return true; - } -} -bool ListDelete(DLinkNode *&L,int i,ElemType &e) //删除第i个元素 -{ int j=0; - DLinkNode *p=L,*q; //p指向头结点,j设置为0 - if (i<=0) return false; //i错误返回假 - while (jnext; - } - if (p==NULL) //未找到第i-1个结点 - return false; - else //找到第i-1个节p - { q=p->next; //q指向第i个结点 - if (q==NULL) //当不存在第i个结点时返回false - return false; - e=q->data; - p->next=q->next; //从双链表中删除结点q - if (p->next!=NULL) //若p结点存在后继结点,修改其前驱指针 - p->next->prior=p; - free(q); //释放q结点 - return true; - } -} diff --git a/src/chap2/emp.dat b/src/chap2/emp.dat deleted file mode 100644 index b01ebf1c0368a0b74fdf71f9aea26439ef7a901b..0000000000000000000000000000000000000000 Binary files a/src/chap2/emp.dat and /dev/null differ diff --git a/src/chap2/exp2-1.cpp b/src/chap2/exp2-1.cpp deleted file mode 100644 index 1151e10f0e485a7751c479c4e0de9d0de7d70bc1..0000000000000000000000000000000000000000 --- a/src/chap2/exp2-1.cpp +++ /dev/null @@ -1,34 +0,0 @@ -//文件名:exp2-1.cpp -#include "sqlist.cpp" -int main() -{ - SqList *L; - ElemType e; - printf("顺序表的基本运算如下:\n"); - printf(" (1)初始化顺序表L\n"); - InitList(L); - printf(" (2)依次插入a,b,c,d,e元素\n"); - ListInsert(L,1,'a'); - ListInsert(L,2,'b'); - ListInsert(L,3,'c'); - ListInsert(L,4,'d'); - ListInsert(L,5,'e'); - printf(" (3)输出顺序表L:"); - DispList(L); - printf(" (4)顺序表L长度:%d\n",ListLength(L)); - printf(" (5)顺序表L为%s\n",(ListEmpty(L)?"空":"非空")); - GetElem(L,3,e); - printf(" (6)顺序表L的第3个元素:%c\n",e); - printf(" (7)元素a的位置:%d\n",LocateElem(L,'a')); - printf(" (8)在第4个元素位置上插入f元素\n"); - ListInsert(L,4,'f'); - printf(" (9)输出顺序表L:"); - DispList(L); - printf(" (10)删除L的第3个元素\n"); - ListDelete(L,3,e); - printf(" (11)输出顺序表L:"); - DispList(L); - printf(" (12)释放顺序表L\n"); - DestroyList(L); - return 1; -} diff --git a/src/chap2/exp2-10.cpp b/src/chap2/exp2-10.cpp deleted file mode 100644 index 11e2516b3020be086669077bb2c142ea1fa7ea4a..0000000000000000000000000000000000000000 --- a/src/chap2/exp2-10.cpp +++ /dev/null @@ -1,170 +0,0 @@ -//文件名:exp2-8.cpp -#include -#include -#define MAX 20 -typedef struct node -{ - double coef; //系数 - int exp; //指数 - struct node *next; -} PolyNode; //声明多项式单链表结点类型 -void CreatePolyR(PolyNode *&L,double a[],int b[],int n) //尾插法创建多项式单链表 -{ - PolyNode *s,*r;int i; - L=(PolyNode *)malloc(sizeof(PolyNode)); - L->next=NULL; - r=L; //r始终指向终端结点,开始时指向头结点 - for (i=0;icoef=a[i]; - s->exp=b[i]; - r->next=s; //将结点s插入结点r之后 - r=s; - } - r->next=NULL; //尾结点next域置为NULL -} -void DestroyPoly(PolyNode *&L) //销毁单链表 -{ - PolyNode *pre=L,*p=pre->next; - while (p!=NULL) - { - free(pre); - pre=p; - p=pre->next; - } - free(pre); -} -void DispPoly(PolyNode *L) //输出多项式单链表 -{ - bool first=true; //first为true表示是第一项 - PolyNode *p=L->next; - while (p!=NULL) - { - if (first) - first=false; - else if (p->coef>0) - printf("+"); - if (p->exp==0) - printf("%g",p->coef); - else if (p->exp==1) - printf("%gx",p->coef); - else - printf("%gx^%d",p->coef,p->exp); - p=p->next; - } - printf("\n"); -} -void Sort(PolyNode *&L) //将多项式单链表按指数递减排序 -{ - PolyNode *p=L->next,*pre,*q; - if (p!=NULL) //L有一个或以上的数据结点 - { - q=p->next; //q保存p结点的后继结点 - p->next=NULL; //构造只含一个数据结点的有序表 - p=q; - while (p!=NULL) //扫描原L中余下的数据结点 - { - q=p->next; //q保存p结点的后继结点 - pre=L; - while (pre->next!=NULL && pre->next->exp>p->exp) - pre=pre->next; //在有序表中找插入结点p的前驱结点pre - p->next=pre->next; //将结点p插入到结点pre之后 - pre->next=p; - p=q; //扫描原单链表余下的结点 - } - } -} -void Mult1(PolyNode *ha,PolyNode *hb,PolyNode *&hc) //ha和bh简单相乘得到hc -{ - PolyNode *pa=ha->next,*pb,*s,*tc; - hc=(PolyNode *)malloc(sizeof(PolyNode)); - tc=hc; - while (pa!=NULL) - { - pb=hb->next; - while (pb!=NULL) - { - s=(PolyNode *)malloc(sizeof(PolyNode)); - s->coef=pa->coef*pb->coef; - s->exp=pa->exp+pb->exp; - tc->next=s; - tc=s; - pb=pb->next; - } - pa=pa->next; - } - tc->next=NULL; -} -void Comb(PolyNode *&L) //合并指数相同的项 -{ - PolyNode *pre=L->next,*p; - if (pre==NULL) return; - p=pre->next; - while (p!=NULL) - { - while (p->exp==pre->exp) - { - pre->coef+=p->coef; - pre->next=p->next; - free(p); - p=pre->next; - } - pre=p; - p=p->next; - } -} -void DelZero(PolyNode *&L) //删除系数为0的项 -{ - PolyNode *pre=L,*p=pre->next; - while (p!=NULL) - { - if (p->coef==0.0) - { - pre->next=p->next; - free(p); - } - pre=p; - p=p->next; - } -} -void Mult(PolyNode *ha,PolyNode *hb,PolyNode *&hc) //ha和bh相乘得到最终的hc -{ - Mult1(ha,hb,hc); - printf("相乘结果: ");DispPoly(hc); - Sort(hc); - printf("按指数排序后: ");DispPoly(hc); - Comb(hc); - printf("合并重复指数项:");DispPoly(hc); - DelZero(hc); - printf("删除序数为0项: ");DispPoly(hc); -} -int main() -{ - PolyNode *Poly1,*Poly2,*Poly3; - double a[MAX]; - int b[MAX],n; - //----创建第1个多项单链表并排序----- - a[0]=2;b[0]=3; a[1]=1;b[1]=0; a[2]=3;b[2]=1; - n=3; - printf("第1个多项式:\n"); - CreatePolyR(Poly1,a,b,n); - printf(" 排序前多项式1:");DispPoly(Poly1); - Sort(Poly1); - printf(" 排序后多项式1:");DispPoly(Poly1); - //----创建第2个多项单链表并排序----- - printf("第2个多项式:\n"); - a[0]=2; b[0]=3; a[1]=-3;b[1]=2; - a[2]=5; b[2]=4; a[3]=-3;b[3]=0; - n=4; - CreatePolyR(Poly2,a,b,n); - printf(" 排序前多项式2:");DispPoly(Poly2); - Sort(Poly2); - printf(" 排序后多项式2:");DispPoly(Poly2); - Mult(Poly1,Poly2,Poly3); - printf("相乘后多项式3: ");DispPoly(Poly3); - DestroyPoly(Poly1); - DestroyPoly(Poly2); - DestroyPoly(Poly3); - return 1; -} diff --git a/src/chap2/exp2-11.cpp b/src/chap2/exp2-11.cpp deleted file mode 100644 index 8448cfaabc0c5ce82ffe823955ec54914169c9d6..0000000000000000000000000000000000000000 --- a/src/chap2/exp2-11.cpp +++ /dev/null @@ -1,244 +0,0 @@ -//文件名:exp2-11.cpp -#include -#include -typedef struct -{ - int no; //职工号 - char name[10]; //姓名 - int depno; //部门号 - float salary; //工资数 -} EmpType; //职工类型 -typedef struct node -{ - EmpType data; //存放职工信息 - struct node *next; //指向下一个结点的指针 -} EmpList; //职工单链表结点类型 -void DestroyEmp(EmpList *&L) //释放职工单链表L -{ - EmpList *pre=L,*p=pre->next; - while (p!=NULL) - { - free(pre); - pre=p; - p=p->next; - } - free(pre); -} -void DelAll(EmpList *&L) //删除职工文件中全部记录 -{ - FILE *fp; - if ((fp=fopen("emp.dat","wb"))==NULL) //重写清空emp.dat文件 - { - printf(" 提示:不能打开职工文件\n"); - return; - } - fclose(fp); - DestroyEmp(L); //释放职工单链表L - L=(EmpList *)malloc(sizeof(EmpList)); - L->next=NULL; //建立一个空的职工单链表L - printf(" 提示:职工数据清除完毕\n"); -} -void ReadFile(EmpList *&L) //读emp.dat文件建立职工单键表L -{ - FILE *fp; - EmpType emp; - EmpList *p,*r; - int n=0; - L=(EmpList *)malloc(sizeof(EmpList)); //建立头结点 - r=L; - if ((fp=fopen("emp.dat","rb"))==NULL) //不存在emp.dat文件 - { - if ((fp=fopen("emp.dat","wb"))==NULL) - printf(" 提示:不能创建emp.dat文件\n"); - } - else //若存在emp.dat文件 - { - while (fread(&emp,sizeof(EmpType),1,fp)==1) - { //采用尾插法建立单链表L - p=(EmpList *)malloc(sizeof(EmpList)); - p->data=emp; - r->next=p; - r=p; - n++; - } - } - r->next=NULL; - printf(" 提示:职工单键表L建立完毕,有%d个记录\n",n); - fclose(fp); -} -void SaveFile(EmpList *L) //将职工单链表数据存入数据文件 -{ - EmpList *p=L->next; - int n=0; - FILE *fp; - if ((fp=fopen("emp.dat","wb"))==NULL) - { - printf(" 提示:不能创建文件emp.dat\n"); - return; - } - while (p!=NULL) - { - fwrite(&p->data,sizeof(EmpType),1,fp); - p=p->next; - n++; - } - fclose(fp); - DestroyEmp(L); //释放职工单链表L - if (n>0) - printf(" 提示:%d个职工记录写入emp.dat文件\n",n); - else - printf(" 提示:没有任何职工记录写入emp.dat文件\n"); -} -void InputEmp(EmpList *&L) //添加一个职工记录 -{ - EmpType p; - EmpList *s; - printf(" >>输入职工号(-1返回):"); - scanf("%d",&p.no); - if (p.no==-1) return; - printf(" >>输入姓名 部门号 工资:"); - scanf("%s%d%f",&p.name,&p.depno,&p.salary); - s=(EmpList *)malloc(sizeof(EmpList)); - s->data=p; - s->next=L->next; //采用头插法插入结点s - L->next=s; - printf(" 提示:添加成功\n"); -} - -void DelEmp(EmpList *&L) //删除一个职工记录 -{ - EmpList *pre=L,*p=L->next; - int no; - printf(" >>输入职工号(-1返回):"); - scanf("%d",&no); - if (no==-1) return; - while (p!=NULL && p->data.no!=no) - { - pre=p; - p=p->next; - } - if (p==NULL) - printf(" 提示:指定的职工记录不存在\n"); - else - { - pre->next=p->next; - free(p); - printf(" 提示:删除成功\n"); - } -} -void Sortno(EmpList *&L) //采用直接插入法单链表L按no递增有序排序 -{ - EmpList *p,*pre,*q; - p=L->next->next; - if (p!=NULL) - { - L->next->next=NULL; - while (p!=NULL) - { - q=p->next; - pre=L; - while (pre->next!=NULL && pre->next->data.nodata.no) - pre=pre->next; - p->next=pre->next; - pre->next=p; - p=q; - } - } - printf(" 提示:按no递增排序完毕\n"); -} -void Sortdepno(EmpList *&L) //采用直接插入法单链表L按depno递增有序排序 -{ - EmpList *p,*pre,*q; - p=L->next->next; - if (p!=NULL) - { - L->next->next=NULL; - while (p!=NULL) - { - q=p->next; - pre=L; - while (pre->next!=NULL && pre->next->data.depnodata.depno) - pre=pre->next; - p->next=pre->next; - pre->next=p; - p=q; - } - } - printf(" 提示:按depno递增排序完毕\n"); -} -void Sortsalary(EmpList *&L) //采用直接插入法单链表L按salary递增有序排序 -{ - EmpList *p,*pre,*q; - p=L->next->next; - if (p!=NULL) - { - L->next->next=NULL; - while (p!=NULL) - { - q=p->next; - pre=L; - while (pre->next!=NULL && pre->next->data.salarydata.salary) - pre=pre->next; - p->next=pre->next; - pre->next=p; - p=q; - } - } - printf(" 提示:按salary递增排序完毕\n"); -} -void DispEmp(EmpList *L) //输出所有职工记录 -{ - EmpList *p=L->next; - if (p==NULL) - printf(" 提示:没有任何职工记录\n"); - else - { - printf(" 职工号 姓名 部门号 工资\n"); - printf(" ----------------------------------\n"); - while (p!=NULL) - { - printf(" %3d%10s %-8d%7.2f\n",p->data.no,p->data.name,p->data.depno,p->data.salary); - p=p->next; - } - printf(" ----------------------------------\n"); - } -} -int main() -{ - EmpList *L; - int sel; - printf("由emp.dat文件建立职工单键表L\n"); - ReadFile(L); - do - { - printf(">1:添加 2:显示 3:按职工号排序 4:按部门号排序 5:按工资数排序\n"); - printf(">6:删除 9:全删 0:退出 请选择:"); - scanf("%d",&sel); - switch(sel) - { - case 9: - DelAll(L); - break; - case 1: - InputEmp(L); - break; - case 2: - DispEmp(L); - break; - case 3: - Sortno(L); - break; - case 4: - Sortdepno(L); - break; - case 5: - Sortsalary(L); - break; - case 6: - DelEmp(L); - break; - } - } while (sel!=0); - SaveFile(L); - return 1; -} diff --git a/src/chap2/exp2-12.cpp b/src/chap2/exp2-12.cpp deleted file mode 100644 index a7a9aebfad807eee6ee05abbac9f1579a0628975..0000000000000000000000000000000000000000 --- a/src/chap2/exp2-12.cpp +++ /dev/null @@ -1,121 +0,0 @@ -//文件名:exp2-12.cpp -#include -#include -#include -#define MaxSize 50 -typedef struct node -{ - int data; - struct node *next; -} NodeType; -void CreateLink(NodeType *&h,char a[],int n) //创建整数单链表 -{ - NodeType *p,*r; - int i=0; - h=(NodeType *)malloc(sizeof(NodeType)); - r=h; - while (idata=a[n-i-1]-'0'; - r->next=p; r=p; - i++; - } - r->next=NULL; -} -void DestroyLink(NodeType *&h) //释放整数单链表 -{ - NodeType *pre=h,*p=pre->next; - while (p!=NULL) - { - free(pre); - pre=p; - p=p->next; - } - free(pre); -} -void DispLink(NodeType *h) //输出整数单链表 -{ - NodeType *p=h->next; - while (p!=NULL) - { - printf("%d ",p->data); - p=p->next; - } - printf("\n"); -} -void Add(NodeType *h1,NodeType *h2,NodeType *&h) //两整数值单链表h1和h2相加得到h -{ - NodeType *p1=h1->next,*p2=h2->next,*p,*r; - int carry=0; - h=(NodeType *)malloc(sizeof(NodeType)); - r=h; - while (p1!=NULL && p2!=NULL) - { - p=(NodeType *)malloc(sizeof(NodeType)); - p->data=(p1->data+p2->data+carry) % 10; - r->next=p; r=p; - carry=(p1->data+p2->data+carry) /10; - p1=p1->next; - p2=p2->next; - } - if (p1==NULL) p1=p2; - while (p1!=NULL) - { - - p=(NodeType *)malloc(sizeof(NodeType)); - p->data=(p1->data+carry) % 10; - r->next=p; r=p; - carry=(p1->data+carry) /10; - p1=p1->next; - } - if (carry>0) //最后carry不为0时,创建一个结点存放它 - { - p=(NodeType *)malloc(sizeof(NodeType)); - p->data=carry; - r->next=p; r=p; - } - r->next=NULL; -} -void Reverse(NodeType *&h) //逆置整数单链表h -{ - NodeType *p=h->next,*q; - h->next=NULL; - while (p!=NULL) - { - q=p->next; - p->next=h->next;h->next=p; - p=q; - } -} -int Mid(NodeType *h) //求整数单链表h的中间位 -{ - NodeType *slow=h,*quick=h; - while (quick!=NULL && quick->next!=NULL) - { - slow=slow->next; - quick=quick->next->next; - } - return slow->data; -} -int main() -{ - NodeType *h1,*h2,*h; - char s[MaxSize],t[MaxSize]; - printf("操作步骤:\n"); - printf(" (1)输入整数1: ");scanf("%s",s); - printf(" (2)输入整数2: ");scanf("%s",t); - CreateLink(h1,s,strlen(s)); - CreateLink(h2,t,strlen(t)); - printf(" (3)整数单链表1: "); DispLink(h1); - printf(" (4)整数单链表2: "); DispLink(h2); - Add(h1,h2,h); - printf(" (5)结果单链表: "); DispLink(h); - Reverse(h); - printf(" (6)对应的整数: "); DispLink(h); - printf(" (7)中间位:%d\n",Mid(h)); - DestroyLink(h); - DestroyLink(h1); - DestroyLink(h2); - return 1; -} diff --git a/src/chap2/exp2-2.cpp b/src/chap2/exp2-2.cpp deleted file mode 100644 index 285ea7a07797c7fa7ac9c86603035ba791c9c7eb..0000000000000000000000000000000000000000 --- a/src/chap2/exp2-2.cpp +++ /dev/null @@ -1,34 +0,0 @@ -//文件名:exp2-2.cpp -#include "linklist.cpp" -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); - return 1; -} diff --git a/src/chap2/exp2-3.cpp b/src/chap2/exp2-3.cpp deleted file mode 100644 index be4d05368d75a9ff3f5950b4127562192fa1bbfd..0000000000000000000000000000000000000000 --- a/src/chap2/exp2-3.cpp +++ /dev/null @@ -1,34 +0,0 @@ -//文件名:exp2-3.cpp -#include "dlinklist.cpp" -int main() -{ - DLinkNode *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); - return 1; -} diff --git a/src/chap2/exp2-4.cpp b/src/chap2/exp2-4.cpp deleted file mode 100644 index 446277b6de886d63dc7f33d11ed0a977cc48aa42..0000000000000000000000000000000000000000 --- a/src/chap2/exp2-4.cpp +++ /dev/null @@ -1,34 +0,0 @@ -//文件名:exp2-4.cpp -#include "clinklist.cpp" -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); - return 1; -} diff --git a/src/chap2/exp2-5.cpp b/src/chap2/exp2-5.cpp deleted file mode 100644 index 1e1a81c96485825e6d191c58f67ea2dd6b73f87e..0000000000000000000000000000000000000000 --- a/src/chap2/exp2-5.cpp +++ /dev/null @@ -1,34 +0,0 @@ -//文件名:exp2-5.cpp -#include "cdlinklist.cpp" -int main() -{ - DLinkNode *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); - return 1; -} diff --git a/src/chap2/exp2-6.cpp b/src/chap2/exp2-6.cpp deleted file mode 100644 index 8308214716484897bfa074d7c5678071b8aaf670..0000000000000000000000000000000000000000 --- a/src/chap2/exp2-6.cpp +++ /dev/null @@ -1,75 +0,0 @@ -//文件名:exp2-6.cpp -#include "linklist.cpp" //包含单链表的基本运算算法 -#include -void Split1(LinkNode *&L) //解法1:将L中所有结点按首结点值进行划分 -{ - LinkNode *pre,*p,*q; - if(L->next==NULL || L->next->next==NULL) - return; //单链表L为空或者只有一个结点时返回 - int x=L->next->data; //取首结点值x - pre=L->next; //pre指向首结点 - p=pre->next; //p指向pre结点的后继结点 - while(p!=NULL) - { - if(p->datanext=p->next; //删除结点p - p->next=L->next; //将结点p插入到表头 - L->next=p; - p=pre->next; //继续遍历 - } - else //结点p的值大于等于x时 - { - pre=p; //pre,p同步后移 - p=pre->next; - } - } -} -void Split2(LinkNode *&L) //解法2:将L中所有结点按首结点值进行划分 -{ - LinkNode *p=L->next,*r,*L1,*r1; - if(L->next==NULL || L->next->next==NULL) - return; //单链表L为空或者只有一个结点时返回 - int x=L->next->data; //取首结点值x - r=L; - L1=(LinkNode*)malloc(sizeof(LinkNode)); //建立大于等于x的单链表L1 - r1=L1; - while (p!=NULL) - { - if (p->datanext=p; r=p; - p=p->next; - } - else - { - r1->next=p; r1=p; - p=p->next; - } - } - r1->next=NULL; - r->next=L1->next; //L和L1连接 - free(L1); -} - -int main() -{ - LinkNode *L; - ElemType a[]="daxgdchaeb"; - int n=strlen(a); - printf("解法1\n"); - CreateListR(L,a,n); - printf(" L: "); DispList(L); - printf(" 以首结点值进行划分\n"); - Split1(L); - printf(" L: "); DispList(L); - DestroyList(L); - printf("解法2\n"); - CreateListR(L,a,n); - printf(" L: "); DispList(L); - printf(" 以首结点值进行划分\n"); - Split2(L); - printf(" L: "); DispList(L); - DestroyList(L); - return 1; -} diff --git a/src/chap2/exp2-7.cpp b/src/chap2/exp2-7.cpp deleted file mode 100644 index 97d92fb6c26cd90c0121554364fc6d143dcc4591..0000000000000000000000000000000000000000 --- a/src/chap2/exp2-7.cpp +++ /dev/null @@ -1,36 +0,0 @@ -//文件名:exp2-7.cpp -#include "linklist.cpp" //包含单链表的基本运算算法 -void Merge(LinkNode *L1,LinkNode *L2,LinkNode *&L3) //L1和L2合并产生L3 -{ - LinkNode *p=L1->next,*q=L2->next,*r; - L3=L1; - r=L3; //r指向新建单链表L3的尾结点 - free(L2); //释放L2的头结点 - while (p!=NULL && q!=NULL) - { - r->next=p; r=p; p=p->next; - r->next=q; r=q; q=q->next; - } - r->next=NULL; - if (q!=NULL) p=q; - r->next=p; -} - -int main() -{ - LinkNode *L1,*L2,*L3; - ElemType a[]="abcdefgh"; - int n=8; - CreateListR(L1,a,n); - printf("L1:"); DispList(L1); - - ElemType b[]="12345"; - n=5; - CreateListR(L2,b,n); - printf("L2:"); DispList(L2); - printf("L1和L2合并产生L3\n"); - Merge(L1,L2,L3); - printf("L3:"); DispList(L3); - DestroyList(L3); - return 1; -} diff --git a/src/chap2/exp2-8.cpp b/src/chap2/exp2-8.cpp deleted file mode 100644 index 3e0604875176b5215c68b1593c9a5934c1a152ab..0000000000000000000000000000000000000000 --- a/src/chap2/exp2-8.cpp +++ /dev/null @@ -1,124 +0,0 @@ -//文件名:exp2-8.cpp -#include "linklist.cpp" -void sort(LinkNode *&L) //单链表元素递增排序 -{ - LinkNode *p,*pre,*q; - p=L->next->next; //p指向L的第2个数据结点 - L->next->next=NULL; //构造只含一个数据结点的有序表 - while (p!=NULL) - { q=p->next; //q保存p结点的后继结点 - pre=L; //从有序表开头进行比较,pre指向插入结点p的前驱结点 - while (pre->next!=NULL && pre->next->datadata) - pre=pre->next; //在有序表中找pre结点 - p->next=pre->next; //将结点pre之后插入p结点 - pre->next=p; - p=q; //扫描原单链表余下的结点 - } -} - -void Union(LinkNode *ha,LinkNode *hb,LinkNode *&hc) //求两有序集合的并 -{ - LinkNode *pa=ha->next,*pb=hb->next,*s,*tc; - hc=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点 - tc=hc; - while (pa!=NULL && pb!=NULL) - { - if (pa->datadata) - { - s=(LinkNode *)malloc(sizeof(LinkNode)); //复制结点 - s->data=pa->data; - tc->next=s;tc=s; - pa=pa->next; - } - else if (pa->data>pb->data) - { - s=(LinkNode *)malloc(sizeof(LinkNode)); //复制结点 - s->data=pb->data; - tc->next=s;tc=s; - pb=pb->next; - } - else - { - s=(LinkNode *)malloc(sizeof(LinkNode)); //复制结点 - s->data=pa->data; - tc->next=s;tc=s; - pa=pa->next; //重复的元素只复制一个 - pb=pb->next; - } - } - if (pb!=NULL) pa=pb; //复制余下的结点 - while (pa!=NULL) - { - s=(LinkNode *)malloc(sizeof(LinkNode)); //复制结点 - s->data=pa->data; - tc->next=s;tc=s; - pa=pa->next; - } - tc->next=NULL; -} -void InterSect(LinkNode *ha,LinkNode *hb,LinkNode *&hc) //求两有序集合的交 -{ - LinkNode *pa=ha->next,*pb,*s,*tc; - hc=(LinkNode *)malloc(sizeof(LinkNode)); - tc=hc; - while (pa!=NULL) - { - pb=hb->next; - while (pb!=NULL && pb->datadata) - pb=pb->next; - if (pb!=NULL && pb->data==pa->data) //若pa结点值在B中 - { - s=(LinkNode *)malloc(sizeof(LinkNode)); //复制结点 - s->data=pa->data; - tc->next=s;tc=s; - } - pa=pa->next; - } - tc->next=NULL; -} -void Subs(LinkNode *ha,LinkNode *hb,LinkNode *&hc) //求两有序集合的差 -{ - LinkNode *pa=ha->next,*pb,*s,*tc; - hc=(LinkNode *)malloc(sizeof(LinkNode)); - tc=hc; - while (pa!=NULL) - { - pb=hb->next; - while (pb!=NULL && pb->datadata) - pb=pb->next; - if (!(pb!=NULL && pb->data==pa->data)) //若pa结点值不在B中 - { - s=(LinkNode *)malloc(sizeof(LinkNode)); //复制结点 - s->data=pa->data; - tc->next=s;tc=s; - } - pa=pa->next; - } - tc->next=NULL; -} -int main() -{ - LinkNode *ha,*hb,*hc; - ElemType a[]={'c','a','e','h'}; - ElemType b[]={'f','h','b','g','d','a'}; - printf("集合的运算如下:\n"); - CreateListR(ha,a,4); - CreateListR(hb,b,6); - printf(" 原 集 合A: ");DispList(ha); - printf(" 原 集 合B: ");DispList(hb); - sort(ha); - sort(hb); - printf(" 有序集合A: ");DispList(ha); - printf(" 有序集合B: ");DispList(hb); - Union(ha,hb,hc); - printf(" 集合的并C: ");DispList(hc); - InterSect(ha,hb,hc); - printf(" 集合的交C: ");DispList(hc); - Subs(ha,hb,hc); - printf(" 集合的差C: ");DispList(hc); - DestroyList(ha); - DestroyList(hb); - DestroyList(hc); - return 1; -} - diff --git a/src/chap2/exp2-9.cpp b/src/chap2/exp2-9.cpp deleted file mode 100644 index f5be1003ad0fb99b0bee8c856d0620e3a2077b94..0000000000000000000000000000000000000000 --- a/src/chap2/exp2-9.cpp +++ /dev/null @@ -1,149 +0,0 @@ -//文件名:exp2-9.cpp -#include -#include -#define MAX 100 //多项式最多项数 -typedef struct -{ - double coef; //系数 - int exp; //指数 -} PolyArray; //存放多项式的数组类型 -typedef struct pnode -{ - double coef; //系数 - int exp; //指数 - struct pnode *next; -} PolyNode; //声明多项式单链表结点类型 -void DispPoly(PolyNode *L) //输出多项式单链表 -{ - bool first=true; //first为true表示是第一项 - PolyNode *p=L->next; - while (p!=NULL) - { - if (first) - first=false; - else if (p->coef>0) - printf("+"); - if (p->exp==0) - printf("%g",p->coef); - else if (p->exp==1) - printf("%gx",p->coef); - else - printf("%gx^%d",p->coef,p->exp); - p=p->next; - } - printf("\n"); -} -void DestroyPoly(PolyNode *&L) //销毁多项式单链表 -{ - PolyNode *pre=L,*p=pre->next; - while (p!=NULL) - { - free(pre); - pre=p; - p=pre->next; - } - free(pre); -} -void CreatePolyR(PolyNode *&L,PolyArray a[],int n) //尾插法建表 -{ - PolyNode *s,*r;int i; - L=(PolyNode *)malloc(sizeof(PolyNode)); //创建头结点 - L->next=NULL; - r=L; //r始终指向尾结点,开始时指向头结点 - for (i=0;icoef=a[i].coef; - s->exp=a[i].exp; - r->next=s; //将结点s插入结点r之后 - r=s; - } - r->next=NULL; //尾结点next域置为NULL -} -void Sort(PolyNode *&L) //将多项式单链表按指数递减排序 -{ - PolyNode *p=L->next,*pre,*q; - if (p!=NULL) //L有一个或以上的数据结点 - { - q=p->next; //q保存p结点的后继结点 - p->next=NULL; //构造只含一个数据结点的有序表 - p=q; - while (p!=NULL) //扫描原L中余下的数据结点 - { - q=p->next; //q保存p结点的后继结点 - pre=L; - while (pre->next!=NULL && pre->next->exp>p->exp) - pre=pre->next; //在有序表中找插入结点p的前驱结点pre - p->next=pre->next; //将结点p插入到结点pre之后 - pre->next=p; - p=q; //扫描原单链表余下的结点 - } - } -} -void Add(PolyNode *ha,PolyNode *hb,PolyNode *&hc) //ha和bh相加得到hc -{ - PolyNode *pa=ha->next,*pb=hb->next,*s,*r; - double c; - hc=(PolyNode *)malloc(sizeof(PolyNode)); - r=hc; //r指向尾结点,初始时指向头结点 - while (pa!=NULL && pb!=NULL) //pa、pb均没有扫描完 - { - if (pa->exp>pb->exp) //将指数较大的pa结点复制到hc中 - { - s=(PolyNode *)malloc(sizeof(PolyNode)); - s->exp=pa->exp;s->coef=pa->coef; - r->next=s; r=s; - pa=pa->next; - } - else if (pa->expexp) //将指数较大的pb结点复制到hc中 - { - s=(PolyNode *)malloc(sizeof(PolyNode)); - s->exp=pb->exp;s->coef=pb->coef; - r->next=s; r=s; - pb=pb->next; - } - else //pa、pb结点的指数相等时 - { - c=pa->coef+pb->coef; //求两个结点的系数和c - if (c!=0) //若系数和不为0时创建新结点 - { - s=(PolyNode *)malloc(sizeof(PolyNode)); - s->exp=pa->exp;s->coef=c; - r->next=s; r=s; - } - pa=pa->next; //pa、pb均后移一个结点 - pb=pb->next; - } - } - if (pb!=NULL) pa=pb; //复制余下的结点 - while (pa!=NULL) - { - s=(PolyNode *)malloc(sizeof(PolyNode)); - s->exp=pa->exp; - s->coef=pa->coef; - r->next=s; r=s; - pa=pa->next; - } - r->next=NULL; //尾结点next设置为空 -} -int main() -{ - PolyNode *ha,*hb,*hc; - PolyArray a[]={{1.2,0},{2.5,1},{3.2,3},{-2.5,5}}; - PolyArray b[]={{-1.2,0},{2.5,1},{3.2,3},{2.5,5},{5.4,10}}; - CreatePolyR(ha,a,4); - CreatePolyR(hb,b,5); - printf("原多项式A: ");DispPoly(ha); - printf("原多项式B: ");DispPoly(hb); - Sort(ha); - Sort(hb); - printf("有序多项式A: ");DispPoly(ha); - printf("有序多项式B: ");DispPoly(hb); - Add(ha,hb,hc); - printf("多项式相加: ");DispPoly(hc); - DestroyPoly(ha); - DestroyPoly(hb); - DestroyPoly(hc); - return 1; -} - diff --git a/src/chap2/linklist.cpp b/src/chap2/linklist.cpp deleted file mode 100644 index 4d055819b7f5d1da946e5bd2711a3a10c42bc6ec..0000000000000000000000000000000000000000 --- a/src/chap2/linklist.cpp +++ /dev/null @@ -1,145 +0,0 @@ -//单链表运算算法 -#include -#include -typedef char ElemType; -typedef struct LNode -{ - ElemType data; - struct LNode *next; //指向后继结点 -} LinkNode; //单链表结点类型 -void CreateListF(LinkNode *&L,ElemType a[],int n) -//头插法建立单链表 -{ - LinkNode *s; - L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点 - L->next=NULL; - for (int i=0;idata=a[i]; - s->next=L->next; //将结点s插在原开始结点之前,头结点之后 - L->next=s; - } -} -void CreateListR(LinkNode *&L,ElemType a[],int n) -//尾插法建立单链表 -{ - LinkNode *s,*r; - L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点 - L->next=NULL; - r=L; //r始终指向尾结点,开始时指向头结点 - for (int i=0;idata=a[i]; - r->next=s; //将结点s插入r结点之后 - r=s; - } - r->next=NULL; //尾结点next域置为NULL -} -void InitList(LinkNode *&L) //初始化线性表 -{ - L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头结点 - L->next=NULL; //单链表置为空表 -} -void DestroyList(LinkNode *&L) //销毁线性表 -{ - LinkNode *pre=L,*p=pre->next; - while (p!=NULL) - { free(pre); - pre=p; //pre、p同步后移一个结点 - p=pre->next; - } - free(pre); //此时p为NULL,pre指向尾结点,释放它 -} -bool ListEmpty(LinkNode *L) //判线性表是否为空表 -{ - return(L->next==NULL); -} -int ListLength(LinkNode *L) //求线性表的长度 -{ int i=0; - LinkNode *p=L; //p指向头结点,n置为0(即头结点的序号为0) - while (p->next!=NULL) - { i++; - p=p->next; - } - return(i); //循环结束,p指向尾结点,其序号i为结点个数 -} - -void DispList(LinkNode *L) //输出线性表 -{ LinkNode *p=L->next; //p指向首结点 - while (p!=NULL) //p不为NULL,输出p结点的data域 - { printf("%c ",p->data); - p=p->next; //p移向下一个结点 - } - printf("\n"); -} - -bool GetElem(LinkNode *L,int i,ElemType &e) //求线性表中第i个元素值 -{ int j=0; - if (i<=0) return false; //i错误返回假 - LinkNode *p=L; //p指向头结点,j置为0(即头结点的序号为0) - while (jnext; - } - if (p==NULL) //不存在第i个数据结点,返回false - return false; - else //存在第i个数据结点,返回true - { e=p->data; - return true; - } -} - -int LocateElem(LinkNode *L,ElemType e) //查找第一个值域为e的元素序号 -{ int i=1; - LinkNode *p=L->next; //p指向首结点,i置为1(即首结点的序号为1) - while (p!=NULL && p->data!=e) //查找data值为e的结点,其序号为i - { p=p->next; - i++; - } - if (p==NULL) //不存在值为e的结点,返回0 - return(0); - else //存在值为e的结点,返回其逻辑序号i - return(i); -} - -bool ListInsert(LinkNode *&L,int i,ElemType e) //插入第i个元素 -{ int j=0; - if (i<=0) return false; //i错误返回假 - LinkNode *p=L,*s; //p指向头结点,j置为0(即头结点的序号为0) - while (jnext; - } - if (p==NULL) //未找到第i-1个结点,返回false - return false; - else //找到第i-1个结点p,插入新结点并返回true - { s=(LinkNode *)malloc(sizeof(LinkNode)); - s->data=e; //创建新结点s,其data域置为e - s->next=p->next; //将结点s插入到结点p之后 - p->next=s; - return true; - } -} - -bool ListDelete(LinkNode *&L,int i,ElemType &e) //删除第i个元素 -{ int j=0; - if (i<=0) return false; //i错误返回假 - LinkNode *p=L,*q; //p指向头结点,j置为0(即头结点的序号为0) - while (jnext; - } - if (p==NULL) //未找到第i-1个结点,返回false - return false; - else //找到第i-1个结点p - { q=p->next; //q指向第i个结点 - if (q==NULL) //若不存在第i个结点,返回false - return false; - e=q->data; - p->next=q->next; //从单链表中删除q结点 - free(q); //释放q结点 - return true; //返回true表示成功删除第i个结点 - } -} diff --git a/src/chap2/sqlist.cpp b/src/chap2/sqlist.cpp deleted file mode 100644 index 701b9c34429854ccd862030ad0aa40e72f44c8dc..0000000000000000000000000000000000000000 --- a/src/chap2/sqlist.cpp +++ /dev/null @@ -1,79 +0,0 @@ -//顺序表运算算法 -#include -#include -#define MaxSize 50 -typedef char ElemType; -typedef struct -{ ElemType data[MaxSize]; //存放顺序表元素 - int length; //存放顺序表的长度 -} SqList; //声明顺序表的类型 -void CreateList(SqList *&L,ElemType a[],int n) //整体建立顺序表 -{ - L=(SqList *)malloc(sizeof(SqList)); - for (int i=0;idata[i]=a[i]; - L->length=n; -} -void InitList(SqList *&L) //初始化线性表 -{ - L=(SqList *)malloc(sizeof(SqList)); //分配存放线性表的空间 - L->length=0; -} -void DestroyList(SqList *&L) //销毁线性表 -{ - free(L); -} -bool ListEmpty(SqList *L) //判线性表是否为空表 -{ - return(L->length==0); -} -int ListLength(SqList *L) //求线性表的长度 -{ - return(L->length); -} -void DispList(SqList *L) //输出线性表 -{ - for (int i=0;ilength;i++) - printf("%c ",L->data[i]); - printf("\n"); -} -bool GetElem(SqList *L,int i,ElemType &e) //求线性表中第i个元素值 -{ - if (i<1 || i>L->length) - return false; - e=L->data[i-1]; - return true; -} -int LocateElem(SqList *L, ElemType e) //查找第一个值域为e的元素序号 -{ - int i=0; - while (ilength && L->data[i]!=e) i++; - if (i>=L->length) - return 0; - else - return i+1; -} -bool ListInsert(SqList *&L,int i,ElemType e) //插入第i个元素 -{ - int j; - if (i<1 || i>L->length+1 || L->length==MaxSize) - return false; - i--; //将顺序表位序转化为elem下标 - for (j=L->length;j>i;j--) //将data[i]及后面元素后移一个位置 - L->data[j]=L->data[j-1]; - L->data[i]=e; - L->length++; //顺序表长度增1 - return true; -} -bool ListDelete(SqList *&L,int i,ElemType &e) //删除第i个元素 -{ - int j; - if (i<1 || i>L->length) - return false; - i--; //将顺序表位序转化为elem下标 - e=L->data[i]; - for (j=i;jlength-1;j++) //将data[i]之后的元素前移一个位置 - L->data[j]=L->data[j+1]; - L->length--; //顺序表长度减1 - return true; -} diff --git a/src/chap3/exp3-1.cpp b/src/chap3/exp3-1.cpp deleted file mode 100644 index c96440b40114899628e05c69f815c56249d27b32..0000000000000000000000000000000000000000 --- a/src/chap3/exp3-1.cpp +++ /dev/null @@ -1,29 +0,0 @@ -//文件名:exp3-1.cpp -#include "sqstack.cpp" //包含顺序栈的基本运算算法 -int main() -{ - ElemType e; - SqStack *s; - printf("顺序栈s的基本运算如下:\n"); - printf(" (1)初始化栈s\n"); - InitStack(s); - printf(" (2)栈为%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\n",(StackEmpty(s)?"空":"非空")); - printf(" (5)出栈序列:"); - while (!StackEmpty(s)) - { - Pop(s,e); - printf("%c ",e); - } - printf("\n"); - printf(" (6)栈为%s\n",(StackEmpty(s)?"空":"非空")); - printf(" (7)释放栈\n"); - DestroyStack(s); - return 1; -} diff --git a/src/chap3/exp3-2.cpp b/src/chap3/exp3-2.cpp deleted file mode 100644 index 41acba9f4a5f1aa218c91de16c1446bbb1e59921..0000000000000000000000000000000000000000 --- a/src/chap3/exp3-2.cpp +++ /dev/null @@ -1,29 +0,0 @@ -//文件名:exp3-2.cpp -#include "listack.cpp" //包含链栈的基本运算算法 -int main() -{ - ElemType e; - LinkStNode *s; - printf("链栈s的基本运算如下:\n"); - printf(" (1)初始化栈s\n"); - InitStack(s); - printf(" (2)栈为%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\n",(StackEmpty(s)?"空":"非空")); - printf(" (5)出栈序列:"); - while (!StackEmpty(s)) - { - Pop(s,e); - printf("%c ",e); - } - printf("\n"); - printf(" (6)栈为%s\n",(StackEmpty(s)?"空":"非空")); - printf(" (7)释放栈\n"); - DestroyStack(s); - return 1; -} diff --git a/src/chap3/exp3-3.cpp b/src/chap3/exp3-3.cpp deleted file mode 100644 index 4eaab3f3148d678c66cc7c5779b345c3e176efeb..0000000000000000000000000000000000000000 --- a/src/chap3/exp3-3.cpp +++ /dev/null @@ -1,32 +0,0 @@ -//文件名:exp3-3.cpp -#include "sqqueue.cpp" //包含环形队列的基本运算算法 -int main() -{ - ElemType e; - SqQueue *q; - printf("环形队列基本运算如下:\n"); - printf(" (1)初始化队列q\n"); - InitQueue(q); - printf(" (2)依次进队列元素a,b,c\n"); - if (!enQueue(q,'a')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'b')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'c')) printf("\t提示:队满,不能进队\n"); - printf(" (3)队列为%s\n",(QueueEmpty(q)?"空":"非空")); - if (deQueue(q,e)==0) - printf("队空,不能出队\n"); - else - printf(" (4)出队一个元素%c\n",e); - printf(" (5)依次进队列元素d,e,f\n"); - if (!enQueue(q,'d')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'e')) printf("\t提示:队满,不能进队\n"); - if (!enQueue(q,'f')) printf("\t提示:队满,不能进队\n"); - printf(" (6)出队列序列:"); - while (!QueueEmpty(q)) - { deQueue(q,e); - printf("%c ",e); - } - printf("\n"); - printf(" (7)释放队列\n"); - DestroyQueue(q); - return 1; -} diff --git a/src/chap3/exp3-4.cpp b/src/chap3/exp3-4.cpp deleted file mode 100644 index 3eb270a1509b8a2bc2988007062c616dc89ffdb4..0000000000000000000000000000000000000000 --- a/src/chap3/exp3-4.cpp +++ /dev/null @@ -1,33 +0,0 @@ -//文件名:exp3-4.cpp -#include "liqueue.cpp" //包含链队的基本运算算法 - -int main() -{ - ElemType e; - LinkQuNode *q; - printf("链队的基本运算如下:\n"); - printf(" (1)初始化链队q\n"); - InitQueue(q); - printf(" (2)依次进链队元素a,b,c\n"); - enQueue(q,'a'); - enQueue(q,'b'); - enQueue(q,'c'); - printf(" (3)链队为%s\n",(QueueEmpty(q)?"空":"非空")); - if (deQueue(q,e)==0) - printf("\t提示:队空,不能出队\n"); - else - printf(" (4)出队一个元素%c\n",e); - printf(" (5)依次进链队元素d,e,f\n"); - enQueue(q,'d'); - enQueue(q,'e'); - enQueue(q,'f'); - printf(" (6)出链队序列:"); - while (!QueueEmpty(q)) - { deQueue(q,e); - printf("%c ",e); - } - printf("\n"); - printf(" (7)释放链队\n"); - DestroyQueue(q); - return 1; -} diff --git a/src/chap3/exp3-5.cpp b/src/chap3/exp3-5.cpp deleted file mode 100644 index aa0d367313177f98b415fc6f7ab5a0b8e67772e2..0000000000000000000000000000000000000000 --- a/src/chap3/exp3-5.cpp +++ /dev/null @@ -1,95 +0,0 @@ -//文件名:exp3-5.cpp -#include -#define M 4 //行数 -#define N 4 //列数 -#define MaxSize 100 //栈最多元素个数 -int mg[M+2][N+2]={ //一个迷宫,其四周要加上均为1的外框 -{1,1,1,1,1,1}, -{1,0,0,0,1,1}, -{1,0,1,0,0,1}, -{1,0,0,0,1,1}, -{1,1,0,0,0,1}, -{1,1,1,1,1,1} -}; -struct -{ - int i,j; - int di; -} St[MaxSize],Path[MaxSize]; //定义栈和存放最短路径的数组 -int top=-1; //栈顶指针 -int count=1; //路径数计数 -int minlen=MaxSize; //最短路径长度 -void dispapath() //输出一条路径并求最短路径 -{ - int k; - printf("%5d: ",count++); //输出第count条路径 - for (k=0;k<=top;k++) - printf("(%d,%d) ",St[k].i,St[k].j); - printf("\n"); - if (top+1-1) //栈不空时循环 - { - i=St[top].i;j=St[top].j;di=St[top].di; - if (i==xe && j==ye) //找到了出口 - { - dispapath(); //输出一条路径 - mg[i][j]=0; //让出口变为其他路径可走方块 - top--; //出口退栈,即回退一个方块 - i=St[top].i;j=St[top].j; - di=St[top].di; //让栈顶方块变为当前方块 - } - find=false; //找下一个可走方块(i,1j1) - while (di<4 && !find) - { di++; - switch(di) - { - case 0:i1=i-1; j1=j; break; - case 1:i1=i; j1=j+1; break; - case 2:i1=i+1; j1=j; break; - case 3:i1=i, j1=j-1; break; - } - if (mg[i1][j1]==0) find=true; - } - if (find) //找到了下一个可走方块(i1,j1) - { St[top].di=di; //修改原栈顶元素的di值 - top++;St[top].i=i1;St[top].j=j1; - St[top].di=-1; //下一个可走方块(i1,j1)进栈 - mg[i1][j1]=-1; //避免重复走到该方块 - } - else //没有路径可走,则退栈(i,j)方块 - { - mg[i][j]=0; //让该位置变为其他路径可走方块 - top--; - } - } - dispminpath(); //输出最短路径 -} -int main() -{ - printf("迷宫所有路径如下:\n"); - mgpath(1,1,M,N); - return 1; -} diff --git a/src/chap3/exp3-6.cpp b/src/chap3/exp3-6.cpp deleted file mode 100644 index 1034cb7e0ff21a92b07456a6038710b20b17ff48..0000000000000000000000000000000000000000 --- a/src/chap3/exp3-6.cpp +++ /dev/null @@ -1,131 +0,0 @@ -//文件名:exp3-6.cpp -#include -#include -typedef struct qnode -{ - int data; //病历号 - struct qnode *next; //下一个节点指针 -} QNode; //链队节点类型 -typedef struct -{ - QNode *front,*rear; -} QuType; //声明链队类型 -void Destroyqueue(QuType *&qu) //释放链队 -{ - QNode *pre,*p; - pre=qu->front; - if (pre!=NULL) //若链队不空 - { - p=pre->next; - while (p!=NULL) //释放队中所有数据节点 - { - free(pre); - pre=p; - p=p->next; - } - free(pre); - } - free(qu); //释放链队节点 -} -bool exist(QuType *qu,int no) //队列中是否有no病历号的病人 -{ - bool find=false; - QNode *p=qu->front; - while (p!=NULL && !find) - { - if (p->data==no) - find=true; - else - p=p->next; - } - return find; -} -void SeeDoctor() //模拟病人看病的过程 -{ - int sel,no; - bool flag=true; - QuType *qu; - QNode *p; - qu=(QuType *)malloc(sizeof(QuType)); //创建空队 - qu->front=qu->rear=NULL; - while (flag) //循环执行 - { - printf(">1:排队 2:就诊 3:查看排队 4.不再排队,余下依次就诊 5:下班 请选择:"); - scanf("%d",&sel); - switch(sel) - { - case 1: printf(" 输入病历号:"); - while (true) - { - scanf("%d",&no); - if (exist(qu,no)) - printf(" 输入的病历号重复,重新输入:"); - else - break; - }; - p=(QNode *)malloc(sizeof(QNode)); //创建节点 - p->data=no;p->next=NULL; - if (qu->rear==NULL) //第一个病人排队 - qu->front=qu->rear=p; - else - { - qu->rear->next=p; qu->rear=p; //将p节点进队 - } - break; - case 2: if (qu->front==NULL) //队空 - printf(" 没有排队的病人!\n"); - else //队不空 - { - p=qu->front; - printf(" >>病人%d就诊\n",p->data); - if (qu->rear==p) //只有一个病人排队的情况 - qu->front=qu->rear=NULL; - else - qu->front=p->next; - free(p); - } - break; - case 3:if (qu->front==NULL) //队空 - printf(" 没有排列的病人!\n"); - else //队不空 - { - p=qu->front; - printf(" >>排队病人:"); - while (p!=NULL) - { - printf("%d ",p->data); - p=p->next; - } - printf("\n"); - } - break; - case 4:if (qu->front==NULL) //队空 - printf(" >>没有排列的病人!\n"); - else //队不空 - { - p=qu->front; - printf(" >>病人按以下顺序就诊:"); - while (p!=NULL) - { - printf("%d ",p->data); - p=p->next; - } - printf("\n"); - } - Destroyqueue(qu); //释放链队 - flag=false; //退出 - break; - case 5:if (qu->front!=NULL) //队不空 - printf(" 请排队的病人明天就医!\n"); - - flag=false; //退出 - Destroyqueue(qu); //释放链队 - break; - } - } -} -int main() -{ - SeeDoctor(); - return 1; -} diff --git a/src/chap3/exp3-7.cpp b/src/chap3/exp3-7.cpp deleted file mode 100644 index 91900826d07237590024d79068a53f7a49df5917..0000000000000000000000000000000000000000 --- a/src/chap3/exp3-7.cpp +++ /dev/null @@ -1,64 +0,0 @@ -//文件名:exp3-7.cpp -#include "sqstack.cpp" //包含顺序栈的基本运算算法 -void StackSort(SqStack *&st) //对栈st中元素排序 -{ - SqStack *tmpst; - InitStack(tmpst); - ElemType e,e1; - while (!StackEmpty(st)) //st栈不空循环 - { - Pop(st,e); //出栈元素e - printf(" st:出栈%c=> ",e); - while(!StackEmpty(tmpst)) - { - GetTop(tmpst,e1); - printf("tmpst:取栈顶元素%c ",e1); - if (e1>e) - { - printf("因%c>%c ",e1,e); - printf("tmpst:退栈%c ",e1); - Pop(tmpst,e1); - printf("s:进栈%c ",e1); - Push(st,e1); - } - else - { - printf("因%c<%c,退出循环 ",e1,e); - break; - } - } - Push(tmpst,e); - printf("tmpst:进栈%c\n",e); - } - while (!StackEmpty(tmpst)) - { - Pop(tmpst,e); - Push(st,e); - } - - DestroyStack(tmpst); -} - -int main() -{ - ElemType e; - SqStack *s; - InitStack(s); - printf("(1)依次进栈元素1,3,4,2\n"); - Push(s,'1'); - Push(s,'3'); - Push(s,'4'); - Push(s,'2'); - printf("(2)栈s排序过程:\n"); - StackSort(s); - printf("(3)栈s排序完毕\n"); - printf("(4)s的出栈序列:"); - while (!StackEmpty(s)) - { - Pop(s,e); - printf("%c ",e); - } - printf("\n"); - DestroyStack(s); - return 1; -} diff --git a/src/chap3/exp3-8.cpp b/src/chap3/exp3-8.cpp deleted file mode 100644 index c94112783babff70eafedef9f64e7d27d98f135f..0000000000000000000000000000000000000000 --- a/src/chap3/exp3-8.cpp +++ /dev/null @@ -1,65 +0,0 @@ -//文件名:exap3-8.cpp -#include -#include -#define MaxSize 100 -typedef struct -{ int col[MaxSize]; //col[i]存放第i个皇后的列号 - int top; //栈顶指针 -} StackType; //声明顺序栈类型 -void dispasolution(StackType St) //输出一个解 -{ static int count=0; //静态变量用于统计解个数 - printf(" 第%d个解:",++count); - for (int i=1;i<=St.top;i++) - printf("(%d,%d) ",i,St.col[i]); - printf("\n"); -} -bool place(StackType St,int k,int j) //测试(k,j)是否与1~k-1的皇后有冲突 -{ int i=1; - if (k==1) return true; //放第一个皇后时没有冲突 - while (i<=k-1) //测试与前面已放置的皇后是否有冲突 - { if ((St.col[i]==j) || (abs(j-St.col[i])==abs(i-k))) - return false; //有冲突时返回假 - i++; - } - return true; //没有冲突时返回真 -} -void queen(int n) //求解n皇后问题 -{ - int k; - bool find; - StackType St; //定义栈st - St.top=0; //初始化栈顶指针,为了让皇后从第1行开始,不用下标0 - St.top++; St.col[St.top]=0; //col[1]=0,表示从第1个皇后开始,初始列号为0 - while (St.top!=0) //栈不空时循环 - { k=St.top; //试探栈顶的第k个皇后 - find=false; //尚未找到第k个皇后的位置,find设置为假 - for (int j=St.col[k]+1;j<=n;j++)//为第k个皇后的找一个合适的列号 - if (place(St,k,j)) //在第k行找到一个放皇后的位置(k,j) - { St.col[St.top]=j; //修改第k个皇后的位置(新列号) - find=true; //找到第k个皇后的位置,find设置为真 - break; //找到后退出for循环 - } - if (find) //在第k行找到一个放皇后的位置(k,j) - { if (k==n) //若所有皇后均放好,输出一个解 - dispasolution(St); - else //还有皇后未放时,将第k+1个皇后进栈 - { St.top++; - St.col[St.top]=0; //新进栈的皇后从第0列开始试探起 - } - } - else //若第k个皇后没有合适位置,回溯 - St.top--; //即将第k个皇后退栈 - } -} -int main() -{ int n; //n存放实际皇后个数 - printf("皇后问题(n<20) n="); - scanf("%d",&n); - if (n>20) - printf("n值太大\n"); - else - { printf(" %d皇后问题求解如下:\n",n); - queen(n); - } - return 1; -} diff --git a/src/chap3/exp3-9.cpp b/src/chap3/exp3-9.cpp deleted file mode 100644 index ddbbc3e34bd6b0c43bae246b121af28964811060..0000000000000000000000000000000000000000 --- a/src/chap3/exp3-9.cpp +++ /dev/null @@ -1,196 +0,0 @@ -//文件名:exp3-9.cpp -#include -#include -#define N 3 //停车场内最多的停车数 -#define M 4 //候车场内最多的停车数 -#define Price 2 //每单位停车费用 -typedef struct -{ - int CarNo[N]; //车牌号 - int CarTime[N]; //进场时间 - int top; //栈指针 -} SqStack; //声明顺序栈类型 -typedef struct -{ - int CarNo[M]; //车牌号 - int front,rear; //队首和队尾指针 -} SqQueue; //声明环形队列类型 - -//以下为栈的运算算法 -void InitStack(SqStack *&s) //初始化栈 -{ - s=(SqStack *)malloc(sizeof(SqStack)); - s->top=-1; -} -bool StackEmpty(SqStack *s) //判断栈空 -{ - return(s->top==-1); -} -bool StackFull(SqStack *s) //判断栈满 -{ - return(s->top==N-1); -} -bool Push(SqStack *&s,int e1,int e2) //进栈 -{ - if (s->top==N-1) - return false; - s->top++; - s->CarNo[s->top]=e1; - s->CarTime[s->top]=e2; - return true; -} -bool Pop(SqStack *&s,int &e1,int &e2) //出栈 -{ - if (s->top==-1) - return false; - e1=s->CarNo[s->top]; - e2=s->CarTime[s->top]; - s->top--; - return true; -} -void DispStack(SqStack *s) //显示栈中元素 -{ - for (int i=s->top;i>=0;i--) - printf("%d ",s->CarNo[i]); - printf("\n"); -} - -//以下为队列的运算算法 -void InitQueue(SqQueue *&q) //初始化队 -{ - q=(SqQueue *)malloc (sizeof(SqQueue)); - q->front=q->rear=0; -} -bool QueueEmpty(SqQueue *q) //判断队空 -{ - return(q->front==q->rear); -} -bool QueueFull(SqQueue *q) //判断队满 -{ - return ((q->rear+1)%M==q->front); -} -bool enQueue(SqQueue *&q,int e) //进队 -{ - if ((q->rear+1)%M==q->front) //队满 - return false; - q->rear=(q->rear+1)%M; - q->CarNo[q->rear]=e; - return true; -} -bool deQueue(SqQueue *&q,int &e) //出队 -{ - if (q->front==q->rear) //队空的情况 - return false; - q->front=(q->front+1)%M; - e=q->CarNo[q->front]; - return true; -} -void DispQueue(SqQueue *q) //显示队中元素 -{ - int i=(q->front+1)%M; - printf("%d ",q->CarNo[i]); - while ((q->rear-i+M)%M>0) - { - i=(i+1)%M; - printf("%d ",q->CarNo[i]); - } - printf("\n"); -} -int main() -{ - int comm,i,j; - int no,e1,time,e2; - SqStack *St,*St1; - SqQueue *Qu; - InitStack(St); - InitStack(St1); - InitQueue(Qu); - do - { - printf(">输入指令(1:到达 2:离开 3:停车场 4:候车场 0:退出):"); - scanf("%d",&comm); - switch(comm) - { - case 1: //汽车到达 - printf(" 车号 到达时间:"); - scanf("%d%d",&no,&time); - if (!StackFull(St)) //停车场不满 - { - Push(St,no,time); - printf(" 停车场位置:%d\n",St->top+1); - } - else //停车场满 - { - if (!QueueFull(Qu)) //候车场不满 - { - enQueue(Qu,no); - printf(" 候车场位置:%d\n",Qu->rear); - } - else - printf(" 候车场已满,不能停车\n"); - } - break; - case 2: //汽车离开 - printf(" 车号 离开时间:"); - scanf("%d%d",&no,&time); - for (i=0;i<=St->top && St->CarNo[i]!=no;i++); - if (i>St->top) - printf(" 未找到该编号的汽车\n"); - else - { - for (j=i;j<=St->top;j++) - { - Pop(St,e1,e2); - Push(St1,e1,e2); //倒车到临时栈St1中 - } - Pop(St,e1,e2); //该汽车离开 - printf(" %d汽车停车费用:%d\n",no,(time-e2)*Price); - while (!StackEmpty(St1)) //将临时栈St1重新回到St中 - { - Pop(St1,e1,e2); - Push(St,e1,e2); - } - if (!QueueEmpty(Qu)) //队不空时,将队头进栈St - { - deQueue(Qu,e1); - Push(St,e1,time); //以当前时间开始计费 - } - } - break; - case 3: //显示停车场情况 - if (!StackEmpty(St)) - { - printf(" 停车场中的车辆:"); //输出停车场中的车辆 - DispStack(St); - } - else - printf(" 停车场中无车辆\n"); - break; - case 4: //显示候车场情况 - if (!QueueEmpty(Qu)) - { - printf(" 候车场中的车辆:"); //输出候车场中的车辆 - DispQueue(Qu); - } - else - printf(" 候车场中无车辆\n"); - break; - case 0: //结束 - if (!StackEmpty(St)) - { - printf(" 停车场中的车辆:"); //输出停车场中的车辆 - DispStack(St); - } - if (!QueueEmpty(Qu)) - { - printf(" 候车场中的车辆:"); //输出候车场中的车辆 - DispQueue(Qu); - } - break; - default: //其他情况 - printf(" 输入的命令错误\n"); - break; - } - } while(comm!=0); - return 1; -} diff --git a/src/chap3/liqueue.cpp b/src/chap3/liqueue.cpp deleted file mode 100644 index 61c8d3c74c32ea8149a90e854ba6a628e9c86ee3..0000000000000000000000000000000000000000 --- a/src/chap3/liqueue.cpp +++ /dev/null @@ -1,61 +0,0 @@ -//链队运算算法 -#include -#include -typedef char ElemType; -typedef struct DataNode -{ - ElemType data; - struct DataNode *next; -} DataNode; //链队数据节点类型定义 -typedef struct -{ - DataNode *front; - DataNode *rear; -} LinkQuNode; //链队类型定义 -void InitQueue(LinkQuNode *&q) //初始化队列q -{ - q=(LinkQuNode *)malloc(sizeof(LinkQuNode)); - q->front=q->rear=NULL; -} -void DestroyQueue(LinkQuNode *&q) //销毁队列q -{ - DataNode *p=q->front,*r;//p指向队头数据节点 - if (p!=NULL) //释放数据节点占用空间 - { r=p->next; - while (r!=NULL) - { free(p); - p=r;r=p->next; - } - } - free(p); - free(q); //释放链队节点占用空间 -} -bool QueueEmpty(LinkQuNode *q) //判断队q是否空 -{ - return(q->rear==NULL); -} -void enQueue(LinkQuNode *&q,ElemType e) //进队 -{ DataNode *p; - p=(DataNode *)malloc(sizeof(DataNode)); - p->data=e; - p->next=NULL; - if (q->rear==NULL) //若链队为空,则新节点是队首节点又是队尾节点 - q->front=q->rear=p; - else - { q->rear->next=p; //将p节点链到队尾,并将rear指向它 - q->rear=p; - } -} -bool deQueue(LinkQuNode *&q,ElemType &e) //出队 -{ DataNode *t; - if (q->rear==NULL) //队列为空 - return false; - t=q->front; //t指向第一个数据节点 - if (q->front==q->rear) //队列中只有一个节点时 - q->front=q->rear=NULL; - else //队列中有多个节点时 - q->front=q->front->next; - e=t->data; - free(t); - return true; -} diff --git a/src/chap3/listack.cpp b/src/chap3/listack.cpp deleted file mode 100644 index c70dde4d61b8955f67e6a4932009d4c016bf2c78..0000000000000000000000000000000000000000 --- a/src/chap3/listack.cpp +++ /dev/null @@ -1,52 +0,0 @@ -//链栈运算算法 -#include -#include -typedef char ElemType; -typedef struct linknode -{ - ElemType data; //数据域 - struct linknode *next; //指针域 -} LinkStNode; //链栈类型定义 -void InitStack(LinkStNode *&s) //初始化链栈 -{ - s=(LinkStNode *)malloc(sizeof(LinkStNode)); - s->next=NULL; -} -void DestroyStack(LinkStNode *&s) //销毁链栈 -{ - LinkStNode *p=s->next; - while (p!=NULL) - { - free(s); - s=p; - p=p->next; - } - free(s); //s指向尾节点,释放其空间 -} -bool StackEmpty(LinkStNode *s) //判断栈空否 -{ - return(s->next==NULL); -} -void Push(LinkStNode *&s,ElemType e) //进栈 -{ LinkStNode *p; - p=(LinkStNode *)malloc(sizeof(LinkStNode)); - p->data=e; //新建元素e对应的节点p - p->next=s->next; //插入p节点作为开始节点 - s->next=p; -} -bool Pop(LinkStNode *&s,ElemType &e) //出栈 -{ LinkStNode *p; - if (s->next==NULL) //栈空的情况 - return false; - p=s->next; //p指向开始节点 - e=p->data; - s->next=p->next; //删除p节点 - free(p); //释放p节点 - return true; -} -bool GetTop(LinkStNode *s,ElemType &e) //取栈顶元素 -{ if (s->next==NULL) //栈空的情况 - return false; - e=s->next->data; - return true; -} diff --git a/src/chap3/sqqueue.cpp b/src/chap3/sqqueue.cpp deleted file mode 100644 index 8ca45e71099eedc54361e8139393cc7437d51948..0000000000000000000000000000000000000000 --- a/src/chap3/sqqueue.cpp +++ /dev/null @@ -1,36 +0,0 @@ -//顺序队列(环形队列)运算算法 -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int front,rear; //队首和队尾指针 -} SqQueue; -void InitQueue(SqQueue *&q) //初始化队列q -{ q=(SqQueue *)malloc (sizeof(SqQueue)); - q->front=q->rear=0; -} -void DestroyQueue(SqQueue *&q) //销毁队列q -{ - free(q); -} -bool QueueEmpty(SqQueue *q) //判断队q是否空 -{ - return(q->front==q->rear); -} -bool enQueue(SqQueue *&q,ElemType e) //进队 -{ if ((q->rear+1)%MaxSize==q->front) //队满上溢出 - return false; - q->rear=(q->rear+1)%MaxSize; - q->data[q->rear]=e; - return true; -} -bool deQueue(SqQueue *&q,ElemType &e) //出队 -{ if (q->front==q->rear) //队空下溢出 - return false; - q->front=(q->front+1)%MaxSize; - e=q->data[q->front]; - return true; -} diff --git a/src/chap3/sqqueue1.cpp b/src/chap3/sqqueue1.cpp deleted file mode 100644 index d9c437ae7b198b9efb5a9c9be1a9fe8587254a2d..0000000000000000000000000000000000000000 --- a/src/chap3/sqqueue1.cpp +++ /dev/null @@ -1,36 +0,0 @@ -//顺序队列(非环形队列)运算算法 -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int front,rear; //队头和队尾指针 -} SqQueue; -void InitQueue(SqQueue *&q) -{ q=(SqQueue *)malloc (sizeof(SqQueue)); - q->front=q->rear=-1; -} -void DestroyQueue(SqQueue *&q) //销毁队列 -{ - free(q); -} -bool QueueEmpty(SqQueue *q) //判断队列是否为空 -{ - return(q->front==q->rear); -} -bool enQueue(SqQueue *&q,ElemType e) //进队 -{ if (q->rear==MaxSize-1) //队满上溢出 - return false; //返回假 - q->rear++; //队尾增1 - q->data[q->rear]=e; //rear位置插入元素e - return true; //返回真 -} -bool deQueue(SqQueue *&q,ElemType &e) //出队 -{ if (q->front==q->rear) //队空下溢出 - return false; - q->front++; - e=q->data[q->front]; - return true; -} diff --git a/src/chap3/sqstack.cpp b/src/chap3/sqstack.cpp deleted file mode 100644 index d8fc2b9d6f12b95ee8e74b8072f2b31384bf588e..0000000000000000000000000000000000000000 --- a/src/chap3/sqstack.cpp +++ /dev/null @@ -1,46 +0,0 @@ -//顺序栈运算算法 -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct -{ - ElemType data[MaxSize]; - int top; //栈指针 -} SqStack; //声明顺序栈类型 -void InitStack(SqStack *&s) //初始化顺序栈 -{ - s=(SqStack *)malloc(sizeof(SqStack)); - s->top=-1; -} -void DestroyStack(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]; - s->top--; - return true; -} -bool GetTop(SqStack *s,ElemType &e) //取栈顶元素 -{ - if (s->top==-1) //栈为空的情况,即栈下溢出 - return false; - e=s->data[s->top]; - return true; -} diff --git a/src/chap4/exp4-1.cpp b/src/chap4/exp4-1.cpp deleted file mode 100644 index b80f49b775730c2783158c164d5edb2d0eae378c..0000000000000000000000000000000000000000 --- a/src/chap4/exp4-1.cpp +++ /dev/null @@ -1,30 +0,0 @@ -//文件名:exp4-1.cpp -#include "sqstring.cpp" //包含顺序串基本运算算法 -int main() -{ - SqString s,s1,s2,s3,s4; - printf("顺序串的基本运算如下:\n"); - printf(" (1)建立串s和串s1\n"); - StrAssign(s,"abcdefghijklmn"); - StrAssign(s1,"123"); - printf(" (2)输出串s:");DispStr(s); - printf(" (3)串s的长度:%d\n",StrLength(s)); - printf(" (4)在串s的第9个字符位置插入串s1而产生串s2\n"); - s2=InsStr(s,9,s1); - printf(" (5)输出串s2:");DispStr(s2); - printf(" (6)删除串s第2个字符开始的5个字符而产生串s2\n"); - s2=DelStr(s,2,3); - printf(" (7)输出串s2:");DispStr(s2); - printf(" (8)将串s第2个字符开始的5个字符替换成串s1而产生串s2\n"); - s2=RepStr(s,2,5,s1); - printf(" (9)输出串s2:");DispStr(s2); - printf(" (10)提取串s的第2个字符开始的10个字符而产生串s3\n"); - s3=SubStr(s,2,10); - printf(" (11)输出串s3:");DispStr(s3); - printf(" (12)将串s1和串s2连接起来而产生串s4\n"); - s4=Concat(s1,s2); - printf(" (13)输出串s4:");DispStr(s4); - DestroyStr(s); DestroyStr(s1); DestroyStr(s2); - DestroyStr(s3); DestroyStr(s4); - return 1; -} \ No newline at end of file diff --git a/src/chap4/exp4-2.cpp b/src/chap4/exp4-2.cpp deleted file mode 100644 index 21917ea28f864b6cdf4b8a099567611f403e34a9..0000000000000000000000000000000000000000 --- a/src/chap4/exp4-2.cpp +++ /dev/null @@ -1,30 +0,0 @@ -//文件名:exp4-2.cpp -#include "listring.cpp" //包含链串基本运算算法 -int main() -{ - LinkStrNode *s,*s1,*s2,*s3,*s4; - printf("链串的基本运算如下:\n"); - printf(" (1)建立串s和串s1\n"); - StrAssign(s,"abcdefghijklmn"); - StrAssign(s1,"123"); - printf(" (2)输出串s:");DispStr(s); - printf(" (3)串s的长度:%d\n",StrLength(s)); - printf(" (4)在串s的第9个字符位置插入串s1而产生串s2\n"); - s2=InsStr(s,9,s1); - printf(" (5)输出串s2:");DispStr(s2); - printf(" (6)删除串s第2个字符开始的5个字符而产生串s2\n"); - s2=DelStr(s,2,3); - printf(" (7)输出串s2:");DispStr(s2); - printf(" (8)将串s第2个字符开始的5个字符替换成串s1而产生串s2\n"); - s2=RepStr(s,2,5,s1); - printf(" (9)输出串s2:");DispStr(s2); - printf(" (10)提取串s的第2个字符开始的10个字符而产生串s3\n"); - s3=SubStr(s,2,10); - printf(" (11)输出串s3:");DispStr(s3); - printf(" (12)将串s1和串s2连接起来而产生串s4\n"); - s4=Concat(s1,s2); - printf(" (13)输出串s4:");DispStr(s4); - DestroyStr(s); DestroyStr(s1); DestroyStr(s2); - DestroyStr(s3); DestroyStr(s4); - return 1; -} \ No newline at end of file diff --git a/src/chap4/exp4-3.cpp b/src/chap4/exp4-3.cpp deleted file mode 100644 index 5e25ddcebebd452a46bc58ba21ebac57adaaa6fa..0000000000000000000000000000000000000000 --- a/src/chap4/exp4-3.cpp +++ /dev/null @@ -1,117 +0,0 @@ -//文件名:exp4-3.cpp -#include "sqstring.cpp" //包含顺序串的基本运算算法 -int Index(SqString s,SqString t) //简单匹配算法 -{ - int i=0,j=0; - while (i=t.length) - return(i-t.length); //返回匹配的第一个字符的下标 - else - return(-1); //模式匹配不成功 -} -void GetNext(SqString t,int next[]) //由模式串t求出next值 -{ int j,k; - j=0;k=-1;next[0]=-1; - while (j=t.length) - return(i-t.length); //返回匹配模式串的首字符下标 - else - return(-1); //返回不匹配标志 -} -void GetNextval(SqString t,int nextval[]) //由模式串t求出nextval值 -{ - int j=0,k=-1; - nextval[0]=-1; - while (j=t.length) - return(i-t.length); - else - return(-1); -} - -int main() -{ - int j; - int next[MaxSize],nextval[MaxSize]; - SqString s,t; - StrAssign(s,"abcabcdabcdeabcdefabcdefg"); - StrAssign(t,"abcdeabcdefab"); - printf("串s:");DispStr(s); - printf("串t:");DispStr(t); - printf("简单匹配算法:\n"); - printf(" t在s中的位置=%d\n",Index(s,t)); - GetNext(t,next); //由模式串t求出next值 - GetNextval(t,nextval); //由模式串t求出nextval值 - printf(" j "); - for (j=0;j=p.length) //在A串中未找到p.data[i]字母 - q.data[i]=p.data[i]; - else //在A串中找到p.data[i]字母 - q.data[i]=B.data[j]; - i++; - } - q.length=p.length; - return q; -} -SqString UnEncrypt(SqString q) //返回解密串 -{ - int i=0,j; - SqString p; - while (i=q.length) //在B串中未找到q.data[i]字母 - p.data[i]=q.data[i]; - else //在B串中找到q.data[i]字母 - p.data[i]=A.data[j]; - i++; - } - p.length=q.length; - return p; -} -int main() -{ - SqString p,q; - int ok=1; - StrAssign(A,"abcdefghijklmnopqrstuvwxyz"); //建立A串 - StrAssign(B,"ngzqtcobmuhelkpdawxfyivrsj"); //建立B串 - char str[MaxSize]; - printf("\n"); - printf("输入原文串:"); - gets(str); //获取用户输入的原文串 - StrAssign(p,str); //建立p串 - printf("加密解密如下:\n"); - printf(" 原文串:");DispStr(p); - q=EnCrypt(p); //p串加密产生q串 - printf(" 加密串:");DispStr(q); - p=UnEncrypt(q); //q串解密产生p串 - printf(" 解密串:");DispStr(p); - printf("\n"); - DestroyStr(p); DestroyStr(q); - return 1; -} diff --git a/src/chap4/exp4-5.cpp b/src/chap4/exp4-5.cpp deleted file mode 100644 index 92add53fcd066b94fe47895f660ca040f34f6918..0000000000000000000000000000000000000000 --- a/src/chap4/exp4-5.cpp +++ /dev/null @@ -1,50 +0,0 @@ -//文件名:exp4-5.cpp -#include "sqstring.cpp" //包含顺序串的基本运算算法 -#include -SqString *MaxSubstr(SqString s) -{ - SqString *subs; - int index=0,length=0,length1,i=0,j,k; - while (ilength) //将较大长度者赋给index与length - { - index=i; - length=length1; - } - j+=length1; - } - else j++; - } - i++; //继续扫描第i字符之后的字符 - } - subs=(SqString *)malloc(sizeof(SqString)); - subs->length=length; - for (i=0;idata[i]=s.data[index+i]; - return subs; -} -int main() -{ - char str[MaxSize]; - SqString s,*subs; - printf("输入串:"); - gets(str); - StrAssign(s,str); //创建串s - subs=MaxSubstr(s); - printf("求最长重复子串:\n"); - printf(" 原串:"); - DispStr(s); - printf(" 最长重复子串:"); //输出最长重复子串 - DispStr(*subs); - DestroyStr(s); free(subs); - return 1; -} diff --git a/src/chap4/exp4-6.cpp b/src/chap4/exp4-6.cpp deleted file mode 100644 index 19a781637c524dffb30aa6a5b62c73fe906c4b97..0000000000000000000000000000000000000000 --- a/src/chap4/exp4-6.cpp +++ /dev/null @@ -1,76 +0,0 @@ -//文件名:exp4-6.cpp -#include "sqstring.cpp" //包含顺序串的基本运算算法 - -void GetNext(SqString t,int next[]) //由模式串t求出next值 -{ int j,k; - j=0;k=-1;next[0]=-1; - while (j -#include -typedef struct snode -{ - char data; - struct snode *next; -} LinkStrNode; //声明链串节点类型 -void StrAssign(LinkStrNode *&s,char cstr[]) //字符串常量cstr赋给串s -{ - LinkStrNode *r,*p; - s=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - r=s; //r始终指向尾节点 - for (int i=0;cstr[i]!='\0';i++) - { p=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - p->data=cstr[i]; - r->next=p;r=p; - } - r->next=NULL; -} -void DestroyStr(LinkStrNode *&s) //销毁串 -{ LinkStrNode *pre=s,*p=s->next; //pre指向节点p的前驱节点 - while (p!=NULL) //扫描链串s - { free(pre); //释放pre节点 - pre=p; //pre、p同步后移一个节点 - p=pre->next; - } - free(pre); //循环结束时,p为NULL,pre指向尾节点,释放它 -} -void StrCopy(LinkStrNode *&s,LinkStrNode *t) //串t复制给串s -{ - LinkStrNode *p=t->next,*q,*r; - s=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - r=s; //r始终指向尾节点 - while (p!=NULL) //将t的所有节点复制到s - { q=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - q->data=p->data; - r->next=q;r=q; - p=p->next; - } - r->next=NULL; -} -bool StrEqual(LinkStrNode *s,LinkStrNode *t) //判串相等 -{ - LinkStrNode *p=s->next,*q=t->next; - while (p!=NULL && q!=NULL && p->data==q->data) - { p=p->next; - q=q->next; - } - if (p==NULL && q==NULL) - return true; - else - return false; -} -int StrLength(LinkStrNode *s) //求串长 -{ - int i=0; - LinkStrNode *p=s->next; - while (p!=NULL) - { i++; - p=p->next; - } - return i; -} -LinkStrNode *Concat(LinkStrNode *s,LinkStrNode *t) //串连接 -{ - LinkStrNode *str,*p=s->next,*q,*r; - str=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - r=str; - while (p!=NULL) //将s的所有节点复制到str - { q=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - q->data=p->data; - r->next=q;r=q; - p=p->next; - } - p=t->next; - while (p!=NULL) //将t的所有节点复制到str - { q=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - q->data=p->data; - r->next=q;r=q; - p=p->next; - } - r->next=NULL; - return str; -} -LinkStrNode *SubStr(LinkStrNode *s,int i,int j) //求子串 -{ - int k; - LinkStrNode *str,*p=s->next,*q,*r; - str=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - str->next=NULL; - r=str; //r指向新建链表的尾节点 - if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s)) - return str; //参数不正确时返回空串 - for (k=0;knext; - for (k=1;k<=j;k++) //将s的第i个节点开始的j个节点复制到str - { q=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - q->data=p->data; - r->next=q;r=q; - p=p->next; - } - r->next=NULL; - return str; -} -LinkStrNode *InsStr(LinkStrNode *s,int i,LinkStrNode *t) //串插入 -{ - int k; - LinkStrNode *str,*p=s->next,*p1=t->next,*q,*r; - str=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - str->next=NULL; - r=str; //r指向新建链表的尾节点 - if (i<=0 || i>StrLength(s)+1) //参数不正确时返回空串 - return str; - for (k=1;kdata=p->data; - r->next=q;r=q; - p=p->next; - } - while (p1!=NULL) //将t的所有节点复制到str - { q=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - q->data=p1->data; - r->next=q;r=q; - p1=p1->next; - } - while (p!=NULL) //将节点p及其后的节点复制到str - { q=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - q->data=p->data; - r->next=q;r=q; - p=p->next; - } - r->next=NULL; - return str; -} -LinkStrNode *DelStr(LinkStrNode *s,int i,int j) //串删去 -{ - int k; - LinkStrNode *str,*p=s->next,*q,*r; - str=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - str->next=NULL; - r=str; //r指向新建链表的尾节点 - if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s)) - return str; //参数不正确时返回空串 - for (k=0;kdata=p->data; - r->next=q;r=q; - p=p->next; - } - for (k=0;knext; - while (p!=NULL) //将节点p及其后的节点复制到str - { q=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - q->data=p->data; - r->next=q;r=q; - p=p->next; - } - r->next=NULL; - return str; -} -LinkStrNode *RepStr(LinkStrNode *s,int i,int j,LinkStrNode *t) //串替换 -{ - int k; - LinkStrNode *str,*p=s->next,*p1=t->next,*q,*r; - str=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - str->next=NULL; - r=str; //r指向新建链表的尾节点 - if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s)) - return str; //参数不正确时返回空串 - for (k=0;kdata=p->data;q->next=NULL; - r->next=q;r=q; - p=p->next; - } - for (k=0;knext; - while (p1!=NULL) //将t的所有节点复制到str - { q=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - q->data=p1->data;q->next=NULL; - r->next=q;r=q; - p1=p1->next; - } - while (p!=NULL) //将节点p及其后的节点复制到str - { q=(LinkStrNode *)malloc(sizeof(LinkStrNode)); - q->data=p->data;q->next=NULL; - r->next=q;r=q; - p=p->next; - } - r->next=NULL; - return str; -} -void DispStr(LinkStrNode *s) //输出串 -{ - LinkStrNode *p=s->next; - while (p!=NULL) - { printf("%c",p->data); - p=p->next; - } - printf("\n"); -} diff --git a/src/chap4/sqstring.cpp b/src/chap4/sqstring.cpp deleted file mode 100644 index 7f6980cfa96bd2f91be06b0265773d8e351be477..0000000000000000000000000000000000000000 --- a/src/chap4/sqstring.cpp +++ /dev/null @@ -1,118 +0,0 @@ -//顺序串基本运算的算法 -#include -#define MaxSize 100 -typedef struct -{ - char data[MaxSize]; //串中字符 - int length; //串长 -} SqString; //声明顺序串类型 -void StrAssign(SqString &s,char cstr[]) //字符串常量赋给串s -{ - int i; - for (i=0;cstr[i]!='\0';i++) - s.data[i]=cstr[i]; - s.length=i; -} -void DestroyStr(SqString &s) //销毁串 -{ } - -void StrCopy(SqString &s,SqString t) //串复制 -{ - for (int i=0;is.length || j<0 || i+j-1>s.length) - return str; //参数不正确时返回空串 - for (k=i-1;ks1.length+1) //参数不正确时返回空串 - return str; - for (j=0;js.length || i+j>s.length+1) //参数不正确时返回空串 - return str; - for (k=0;ks.length || i+j-1>s.length) //参数不正确时返回空串 - return str; - for (k=0;k0) - { for (int i=0;i -#include -#define MaxSize 100 -//----递归算法------------------------------------------ -void Hanoi1(int n,char a,char b,char c) -{ - if (n==1) - printf("\t将第%d个盘片从%c移动到%c\n",n,a,c); - else - { - Hanoi1(n-1,a,c,b); - printf("\t将第%d个盘片从%c移动到%c\n",n,a,c); - Hanoi1(n-1,b,a,c); - } -} -//----非递归算法------------------------------------------ -typedef struct -{ int n; //盘片个数 - char x,y,z; //3个塔座 - bool flag; //可直接移动盘片时为true,否则为false -} ElemType; //顺序栈中元素类型 -typedef struct -{ ElemType data[MaxSize]; //存放元素 - int top; //栈顶指针 -} StackType; //声明顺序栈类型 - -//--求解Hanoi问题对应顺序栈的基本运算算法-------------- -void InitStack(StackType *&s) //初始化栈 -{ s=(StackType *)malloc(sizeof(StackType)); - s->top=-1; -} -void DestroyStack(StackType *&s) //销毁栈 -{ - free(s); -} -bool StackEmpty(StackType *s) //判断栈是否为空 -{ - return(s->top==-1); -} -bool Push(StackType *&s,ElemType e) //进栈 -{ if (s->top==MaxSize-1) - return false; - s->top++; - s->data[s->top]=e; - return true; -} -bool Pop(StackType *&s,ElemType &e) //出栈 -{ if (s->top==-1) - return false; - e=s->data[s->top]; - s->top--; - return true; -} -void Hanoi2(int n, char x, char y, char z) -{ StackType *st; //定义顺序栈指针 - ElemType e,e1,e2,e3; - if (n<=0) return; //参数错误时直接返回 - InitStack(st); //初始化栈 - e.n=n; e.x=x; e.y=y; e.z=z; e.flag=false; - Push(st,e); //元素e进栈 - while (!StackEmpty(st)) //栈不空循环 - { Pop(st,e); //出栈元素e - if (e.flag==false) //当不能直接移动盘片时 - { - e1.n=e.n-1; e1.x=e.y; e1.y=e.x; e1.z=e.z; - if (e1.n==1) //只有一个盘片时可直接移动 - e1.flag=true; - else //有一个以上盘片时不能直接移动 - e1.flag=false; - Push(st,e1); //处理Hanoi(n-1,y,x,z)步骤 - e2.n=e.n; e2.x=e.x; e2.y=e.y; e2.z=e.z; e2.flag=true; - Push(st,e2); //处理move(n,x,z)步骤 - e3.n=e.n-1; e3.x=e.x; e3.y=e.z; e3.z=e.y; - if (e3.n==1) //只有一个盘片时可直接移动 - e3.flag=true; - else - e3.flag=false; //有一个以上盘片时不能直接移动 - Push(st,e3); //处理Hanoi(n-1,x,z,y)步骤 - } - else //当可以直接移动时 - printf("\t将第%d个盘片从%c移动到%c\n",e.n,e.x,e.z); - } - DestroyStack(st); //销毁栈 -} -//---------------------------------------------------- -int main() -{ - int n=3; - printf("递归算法:%d个盘片移动过程:\n",n); - Hanoi1(n,'X','Y','Z'); - printf("非递归算法:%d个盘片移动过程:\n",n); - Hanoi2(n,'X','Y','Z'); - return 1; -} \ No newline at end of file diff --git a/src/chap5/exp5-2.cpp b/src/chap5/exp5-2.cpp deleted file mode 100644 index edc3bec552e186e01025a220e139eb4c3e7e5b11..0000000000000000000000000000000000000000 --- a/src/chap5/exp5-2.cpp +++ /dev/null @@ -1,43 +0,0 @@ -//文件名:exp5-2.cpp -#include -#define MaxSize 100 -int pathnum(int m,int n) //求解从(m,n)到(1,1)的路径条数 -{ - if (m<1 || n<1) return 0; - if (m==1 && n==1) return 1; - return pathnum(m-1,n)+pathnum(m,n-1); -} -typedef struct -{ - int i,j; -} PathType; //路径元素类型 -int count=0; //路径编号 -void disppath(int m,int n,PathType path[],int d) //输出从(m,n)到(1,1)的所有路径 -{ - if (m<1 || n<1) return; - if (m==1 && n==1) //找到目的地,输出一条路径 - { - d++; //将当前位置放入path中 - path[d].i=m; path[d].j=n; - printf("路径%d: ",++count); - for (int k=0;k<=d;k++) - printf("(%d,%d) ",path[k].i,path[k].j); - printf("\n"); - } - else - { - d++; //将当前位置放入path中 - path[d].i=m; path[d].j=n; - disppath(m-1,n,path,d); //向下走一步 - disppath(m,n-1,path,d); //退回来,向右走一步 - } -} -int main() -{ - int m=2,n=5; - printf("m=%d,n=%d的路径条数:%d\n",m,n,pathnum(m,n)); - PathType path[MaxSize]; - int d=-1; - disppath(m,n,path,d); - return 1; -} diff --git a/src/chap5/exp5-3.cpp b/src/chap5/exp5-3.cpp deleted file mode 100644 index 6ca6132048075662fc9b4c406865e0b17d0c4b07..0000000000000000000000000000000000000000 --- a/src/chap5/exp5-3.cpp +++ /dev/null @@ -1,47 +0,0 @@ -//文件名:exp5-3.cpp -#include -#define MaxSize 100 -typedef struct -{ char data[MaxSize]; - int length; -} IP; -void addch(IP &ip,char ch) //ip的末尾添加一个字符ch -{ ip.data[ip.length]=ch; - ip.length++; -} -IP adddot(IP ip) //ip的末尾添加一个'.',并返回结果 -{ addch(ip,'.'); - return ip; -} -void solveip(char s[],int n,int start,int step,IP ip) //恢复IP地址串 -{ - if (start<=n) - { - if (start==n && step==4) //找到一个合法解 - { - for (int i=0;i -double expx(double x,int n) -{ - if (n==1) - return x; - else if (n%2==0) //当n为大于1的偶数时 - return expx(x,n/2)*expx(x,n/2); - else //当n为大于1的奇数时 - return x*expx(x,(n-1)/2)*expx(x,(n-1)/2); -} -int main() -{ - double x; - int n; - printf("x:"); scanf("%lf",&x); - printf("n:"); scanf("%d",&n); - printf("%g的%d次方:%g\n",x,n,expx(x,n)); - return 1; -} diff --git a/src/chap5/exp5-5.cpp b/src/chap5/exp5-5.cpp deleted file mode 100644 index 67ae578773b5325c2a570d86e221ea834aa2b027..0000000000000000000000000000000000000000 --- a/src/chap5/exp5-5.cpp +++ /dev/null @@ -1,27 +0,0 @@ -//文件名:exp5-5.cpp -#include "linklist.cpp" //包含单链表的基本运算算法 -void Reverse(LinkNode *p,LinkNode *&L) -{ - if(p->next==NULL) //以p为首节点指针的单链表只有一个节点时 - { - L->next=p; //p节点变为尾节点 - return; - } - Reverse(p->next,L); //逆置后的尾节点是p->next - p->next->next=p; //将节点链接在尾节点之后 - p->next=NULL; //尾节点next域置为NULL -} - -int main() -{ - LinkNode *L; - char a[]="12345678"; - int n=8; - CreateListR(L,a,n); - printf("L:"); DispList(L); - printf("逆置L\n"); - Reverse(L->next,L); - printf("L:"); DispList(L); - DestroyList(L); - return 1; -} diff --git a/src/chap5/exp5-6.cpp b/src/chap5/exp5-6.cpp deleted file mode 100644 index 0628f84551b5e580b62ffdcf3a6290a86e85cd36..0000000000000000000000000000000000000000 --- a/src/chap5/exp5-6.cpp +++ /dev/null @@ -1,27 +0,0 @@ -//文件名:exp5-6.cpp -#include "linklist.cpp" //包含单链表的基本运算算法 -LinkNode *kthNode(LinkNode *L,int k,int &i) //求倒数第k个节点 -{ - LinkNode *p; - if(L==NULL) return NULL; //空表返回NULL - p=kthNode(L->next,k,i); - i++; - if (i==k) return L; - return p; -} - -int main() -{ - LinkNode *L,*p; - char a[]="12345678"; - int n=8,k=2,i=0; - CreateListR(L,a,n); - printf("L:"); DispList(L); - p=kthNode(L->next,k,i); - if (p!=NULL) - printf("倒数第%d个节点:%c\n",k,p->data); - else - printf("没有找到\n"); - DestroyList(L); - return 1; -} diff --git a/src/chap5/exp5-7.cpp b/src/chap5/exp5-7.cpp deleted file mode 100644 index a7d207a60ffab50371c305bb66a532a00ea85131..0000000000000000000000000000000000000000 --- a/src/chap5/exp5-7.cpp +++ /dev/null @@ -1,54 +0,0 @@ -//文件名:exp5-7.cpp -#include -#include -const int N=20; //最多皇后个数 -int q[N]; //存放各皇后所在的列号 -int count=0; //存放解个数 -void print(int n) //输出一个解 -{ - count++; - int i; - printf(" 第%d个解:",count); - for (i=1;i<=n;i++) - printf("(%d,%d) ",i,q[i]); - printf("\n"); -} -bool place(int k,int j) //测试(k,j)位置能否摆放皇后 -{ - int i=1; - while (in) - print(n); //所有皇后放置结束 - else - for (j=1;j<=n;j++) //在第k行上穷举每一个位置 - if (place(k,j)) //在第k行上找到一个合适位置(k,j) - { - q[k]=j; - queen(k+1,n); - } -} -int main() -{ - int n; //n存放实际皇后个数 - printf(" 皇后问题(n<20) n:"); - scanf("%d",&n); - if (n>20) - printf("n值太大,不能求解\n"); - else - { - printf(" %d皇后问题求解如下:\n",n); - queen(1,n); - printf("\n"); - } - return 1; -} diff --git a/src/chap5/exp5-8.cpp b/src/chap5/exp5-8.cpp deleted file mode 100644 index 6342daf40b2dbdc026edb589ed71feb542847b8f..0000000000000000000000000000000000000000 --- a/src/chap5/exp5-8.cpp +++ /dev/null @@ -1,43 +0,0 @@ -//文件名:exp5-8.cpp -#include -#define MAXN 20 //最多物品数 -int maxv; //存放最优解的总价值 -int maxw; //存放最优解的总重量 -int x[MAXN]; //存放最终解 -int W=7; //限制的总重量 -int n=4; //物品种数 -int w[]={5,3,2,1}; //物品重量 -int v[]={4,4,3,1}; //物品价值 - -void knap(int i,int tw,int tv,int op[]) //考虑第i个物品 -{ int j; - if (i>=n) //递归出口:所有物品都考虑过 - { if (tw<=W && tv>maxv) //找到一个满足条件的更优解,保存它 - { maxv=tv; - maxw=tw; - for (j=1;j<=n;j++) - x[j]=op[j]; - } - } - else //尚未找完所有物品 - { op[i]=1; //选取第i个物品 - knap(i+1,tw+w[i],tv+v[i],op); - op[i]=0; //不选取第i个物品,回溯 - knap(i+1,tw,tv,op); - } -} -void dispasolution(int x[],int n)//输出一个解 -{ int i; - printf("最佳装填方案是:\n"); - for (i=1;i<=n;i++) - if (x[i]==1) - printf(" 选取第%d个物品\n",i); - printf("总重量=%d,总价值=%d\n",maxw,maxv); -} -int main() -{ - int op[MAXN]; //存放临时解 - knap(0,0,0,op); - dispasolution(x,n); - return 1; -} diff --git a/src/chap5/linklist.cpp b/src/chap5/linklist.cpp deleted file mode 100644 index c0d77bbe39867e11d005161117c0801e8ae0d862..0000000000000000000000000000000000000000 --- a/src/chap5/linklist.cpp +++ /dev/null @@ -1,145 +0,0 @@ -//单链表运算算法 -#include -#include -typedef char ElemType; -typedef struct LNode -{ - ElemType data; - struct LNode *next; //指向后继节点 -} LinkNode; //单链表节点类型 -void CreateListF(LinkNode *&L,ElemType a[],int n) -//头插法建立单链表 -{ - LinkNode *s; - L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头节点 - L->next=NULL; - for (int i=0;idata=a[i]; - s->next=L->next; //将节点s插在原开始节点之前,头节点之后 - L->next=s; - } -} -void CreateListR(LinkNode *&L,ElemType a[],int n) -//尾插法建立单链表 -{ - LinkNode *s,*r; - L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头节点 - L->next=NULL; - r=L; //r始终指向尾节点,开始时指向头节点 - for (int i=0;idata=a[i]; - r->next=s; //将节点s插入r节点之后 - r=s; - } - r->next=NULL; //尾节点next域置为NULL -} -void InitList(LinkNode *&L) //初始化线性表 -{ - L=(LinkNode *)malloc(sizeof(LinkNode)); //创建头节点 - L->next=NULL; //单链表置为空表 -} -void DestroyList(LinkNode *&L) //销毁线性表 -{ - LinkNode *pre=L,*p=pre->next; - while (p!=NULL) - { free(pre); - pre=p; //pre、p同步后移一个节点 - p=pre->next; - } - free(pre); //此时p为NULL,pre指向尾节点,释放它 -} -bool ListEmpty(LinkNode *L) //判线性表是否为空表 -{ - return(L->next==NULL); -} -int ListLength(LinkNode *L) //求线性表的长度 -{ int i=0; - LinkNode *p=L; //p指向头节点,n置为0(即头节点的序号为0) - while (p->next!=NULL) - { i++; - p=p->next; - } - return(i); //循环结束,p指向尾节点,其序号i为节点个数 -} - -void DispList(LinkNode *L) //输出线性表 -{ LinkNode *p=L->next; //p指向首节点 - while (p!=NULL) //p不为NULL,输出p节点的data域 - { printf("%c ",p->data); - p=p->next; //p移向下一个节点 - } - printf("\n"); -} - -bool GetElem(LinkNode *L,int i,ElemType &e) //求线性表中第i个元素值 -{ int j=0; - if (i<=0) return false; //i错误返回假 - LinkNode *p=L; //p指向头节点,j置为0(即头节点的序号为0) - while (jnext; - } - if (p==NULL) //不存在第i个数据节点,返回false - return false; - else //存在第i个数据节点,返回true - { e=p->data; - return true; - } -} - -int LocateElem(LinkNode *L,ElemType e) //查找第一个值域为e的元素序号 -{ int i=1; - LinkNode *p=L->next; //p指向首节点,i置为1(即首节点的序号为1) - while (p!=NULL && p->data!=e) //查找data值为e的节点,其序号为i - { p=p->next; - i++; - } - if (p==NULL) //不存在值为e的节点,返回0 - return(0); - else //存在值为e的节点,返回其逻辑序号i - return(i); -} - -bool ListInsert(LinkNode *&L,int i,ElemType e) //插入第i个元素 -{ int j=0; - if (i<=0) return false; //i错误返回假 - LinkNode *p=L,*s; //p指向头节点,j置为0(即头节点的序号为0) - while (jnext; - } - if (p==NULL) //未找到第i-1个节点,返回false - return false; - else //找到第i-1个节点p,插入新节点并返回true - { s=(LinkNode *)malloc(sizeof(LinkNode)); - s->data=e; //创建新节点s,其data域置为e - s->next=p->next; //将节点s插入到节点p之后 - p->next=s; - return true; - } -} - -bool ListDelete(LinkNode *&L,int i,ElemType &e) //删除第i个元素 -{ int j=0; - if (i<=0) return false; //i错误返回假 - LinkNode *p=L,*q; //p指向头节点,j置为0(即头节点的序号为0) - while (jnext; - } - if (p==NULL) //未找到第i-1个节点,返回false - return false; - else //找到第i-1个节点p - { q=p->next; //q指向第i个节点 - if (q==NULL) //若不存在第i个节点,返回false - return false; - e=q->data; - p->next=q->next; //从单链表中删除q节点 - free(q); //释放q节点 - return true; //返回true表示成功删除第i个节点 - } -} diff --git a/src/chap6/exp6-1.cpp b/src/chap6/exp6-1.cpp deleted file mode 100644 index b81b9efcdb949d545593bfcf5d3ab2633d635144..0000000000000000000000000000000000000000 --- a/src/chap6/exp6-1.cpp +++ /dev/null @@ -1,168 +0,0 @@ -//文件名:exp6-1.cpp -#include -#define N 4 -typedef int ElemType; -#define MaxSize 100 //矩阵中非零元素最多个数 -typedef struct -{ int r; //行号 - int c; //列号 - ElemType d; //元素值 -} TupNode; //三元组定义 -typedef struct -{ int rows; //行数值 - int cols; //列数值 - int nums; //非零元素个数 - TupNode data[MaxSize]; -} TSMatrix; //三元组顺序表定义 -void CreatMat(TSMatrix &t,ElemType A[N][N]) //产生稀疏矩阵A的三元组表示t -{ - int i,j; - t.rows=N;t.cols=N;t.nums=0; - for (i=0;ib.data[j].c)//a元素的列号大于b元素的列号 - { - c.data[k].r=b.data[j].r; //将b元素添加到c中 - 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 -#include -typedef char ElemType; -typedef struct lnode -{ int tag; //节点类型标识 - union - { - ElemType data; - struct lnode *sublist; - } val; - struct lnode *link; //指向下一个元素 -} GLNode; //声明广义表节点类型 -GLNode *CreateGL(char *&s) //返回由括号表示法表示s的广义表链式存储结构 -{ GLNode *g; - char ch=*s++; //取一个字符 - if (ch!='\0') //串未结束判断 - { g=(GLNode *)malloc(sizeof(GLNode));//创建一个新节点 - if (ch=='(') //当前字符为左括号时 - { g->tag=1; //新节点作为表头节点 - g->val.sublist=CreateGL(s); //递归构造子表并链到表头节点 - } - else if (ch==')') - g=NULL; //遇到')'字符,g置为空 - else if (ch=='#') //遇到'#'字符,表示空表 - g->val.sublist=NULL; - else //为原子字符 - { g->tag=0; //新节点作为原子节点 - g->val.data=ch; - } - } - else //串结束,g置为空 - g=NULL; - ch=*s++; //取下一个字符 - if (g!=NULL) //串未结束,继续构造兄递节点 - if (ch==',') //当前字符为',' - g->link=CreateGL(s); //递归构造兄递节点 - else //没有兄弟了,将兄弟指针置为NULL - g->link=NULL; - return g; //返回广义表g -} -int GLLength(GLNode *g) //求广义表g的长度 -{ - int n=0; - g=g->val.sublist; //g指向广义表的第一个元素 - while (g!=NULL) - { - n++; - g=g->link; - } - return n; -} -int GLDepth(GLNode *g) //求广义表g的深度 -{ - int max=0,dep; - if (g->tag==0) - return 0; - g=g->val.sublist; //g指向第一个元素 - if (g==NULL) //为空表时返回1 - return 1; - while (g!=NULL) //遍历表中的每一个元素 - { - if (g->tag==1) //元素为子表的情况 - { - dep=GLDepth(g); //递归调用求出子表的深度 - if (dep>max) max=dep; //max为同一层所求过的子表中深度的最大值 - } - g=g->link; //使g指向下一个元素 - } - return(max+1); //返回表的深度 -} -void DispGL(GLNode *g) //输出广义表g -{ if (g!=NULL) //表不为空判断 - { //先输出g的元素 - if (g->tag==0) //g的元素为原子时 - printf("%c", g->val.data); //输出原子值 - else //g的元素为子表时 - { printf("("); //输出'(' - if (g->val.sublist==NULL) //为空表时 - printf("#"); - else //为非空子表时 - DispGL(g->val.sublist); //递归输出子表 - printf(")"); //输出')' - } - if (g->link!=NULL) - { printf(","); - DispGL(g->link); //递归输出g的兄弟 - } - } -} -ElemType maxatom(GLNode *g) //求广义表g中最大原子 -{ - ElemType max1,max2; - if (g!=NULL) - { - if (g->tag==0) - { - max1=maxatom(g->link); - return(g->val.data>max1?g->val.data:max1); - } - else - { - max1=maxatom(g->val.sublist); - max2=maxatom(g->link); - return(max1>max2?max1:max2); - } - } - else - return 0; -} -void DestroyGL(GLNode *&g) //销毁广义表g -{ GLNode *g1,*g2; - g1=g->val.sublist; //g1指向广义表的第一个元素 - while (g1!=NULL) //遍历所有元素 - { if (g1->tag==0) //若为原子节点 - { g2=g1->link; //g2临时保存兄弟节点 - free(g1); //释放g1所指原子节点 - g1=g2; //g1指向后继兄弟节点 - } - else //若为子表 - { g2=g1->link; //g2临时保存兄弟节点 - DestroyGL(g1); //递归释放g1所指子表的空间 - g1=g2; //g1指向后继兄弟节点 - } - } - free(g); //释放头节点空间 -} -int main() -{ - GLNode *g; - char *str="(b,(b,a,(#),d),((a,b),c,((#))))"; - g=CreateGL(str); - printf("广义表g:");DispGL(g);printf("\n"); - printf("广义表g的长度:%d\n",GLLength(g)); - printf("广义表g的深度:%d\n",GLDepth(g)); - printf("最大原子:%c\n",maxatom(g)); - DestroyGL(g); - return 1; -} - diff --git a/src/chap6/exp6-3.cpp b/src/chap6/exp6-3.cpp deleted file mode 100644 index 74a8bd70ede10bc97e56b977993128b2209c39c7..0000000000000000000000000000000000000000 --- a/src/chap6/exp6-3.cpp +++ /dev/null @@ -1,50 +0,0 @@ -//文件名:exp6-3.cpp -#include -#define MaxLen 10 -void fun(int a[MaxLen][MaxLen],int n) -{ - int i,j,k=0,m; - if (n%2==0) //m=én/2ù - m=n/2; - else - m=n/2+1; - for (i=0;i=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() -{ - int n,i,j; - int a[MaxLen][MaxLen]; - printf("输入n(n<10):"); - scanf("%d",&n); - fun(a,n); - printf("%d阶数字方阵如下:\n",n); - for (i=0;i -#define M 4 -#define N 4 -void MinMax(int A[M][N]) -{ int i,j; - bool have=false; - int min[M],max[N]; - for (i=0;imax[j]) - max[j]=A[i][j]; - } - for (i=0;i -#define N 4 -#define M 10 -int value(int a[],int i,int j) //返回压缩存储a中A[i][j]之值 -{ - if (i>=j) - return a[(i*(i-1))/2+j]; - else - return a[(j*(j-1))/2+i]; -} -void madd(int a[],int b[],int c[][N]) //求压缩存储a和b的和 -{ - int i,j; - for (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=NULL; - int top=-1,k,j=0; - 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->data=ch;p->lchild=p->rchild=NULL; - if (b==NULL) //*p为二叉树的根结点 - 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) //查找值为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) //求二叉树b的高度 -{ - int lchildh,rchildh; - if (b==NULL) return(0); //空树的高度为0 - else - { - lchildh=BTHeight(b->lchild); //求左子树的高度为lchildh - rchildh=BTHeight(b->rchild); //求右子树的高度为rchildh - 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(")"); //有孩子结点时才输出) - } - } -} diff --git a/src/chap7/exp4-3.cpp b/src/chap7/exp4-3.cpp deleted file mode 100644 index 5e25ddcebebd452a46bc58ba21ebac57adaaa6fa..0000000000000000000000000000000000000000 --- a/src/chap7/exp4-3.cpp +++ /dev/null @@ -1,117 +0,0 @@ -//文件名:exp4-3.cpp -#include "sqstring.cpp" //包含顺序串的基本运算算法 -int Index(SqString s,SqString t) //简单匹配算法 -{ - int i=0,j=0; - while (i=t.length) - return(i-t.length); //返回匹配的第一个字符的下标 - else - return(-1); //模式匹配不成功 -} -void GetNext(SqString t,int next[]) //由模式串t求出next值 -{ int j,k; - j=0;k=-1;next[0]=-1; - while (j=t.length) - return(i-t.length); //返回匹配模式串的首字符下标 - else - return(-1); //返回不匹配标志 -} -void GetNextval(SqString t,int nextval[]) //由模式串t求出nextval值 -{ - int j=0,k=-1; - nextval[0]=-1; - while (j=t.length) - return(i-t.length); - else - return(-1); -} - -int main() -{ - int j; - int next[MaxSize],nextval[MaxSize]; - SqString s,t; - StrAssign(s,"abcabcdabcdeabcdefabcdefg"); - StrAssign(t,"abcdeabcdefab"); - printf("串s:");DispStr(s); - printf("串t:");DispStr(t); - printf("简单匹配算法:\n"); - printf(" t在s中的位置=%d\n",Index(s,t)); - GetNext(t,next); //由模式串t求出next值 - GetNextval(t,nextval); //由模式串t求出nextval值 - printf(" j "); - for (j=0;jdata); - 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/src/chap7/exp7-10.cpp b/src/chap7/exp7-10.cpp deleted file mode 100644 index 9bdbf6fb0701424eaf54a8b3c0dafeefcd3ce587..0000000000000000000000000000000000000000 --- a/src/chap7/exp7-10.cpp +++ /dev/null @@ -1,194 +0,0 @@ -//文件名:exp7-10.cpp -#include -#include -#include -#include -#define MaxSize 100 //最多记录个数 -#define MaxSons 10 //最多下级单位数 -typedef struct -{ - char fname[20]; //单位名称 - char sname[20]; //下级单位名称或者人数 -} RecType; - -typedef struct node -{ char data[20]; //结点的值:单位名称或者人数 - struct node *sons[MaxSons]; //指向孩子结点 -} TSonNode; //声明孩子链存储结构结点类型 - -void ReadFile(RecType R[],int &n) //读abc.txt文件存入R数组中 -{ - FILE *fp; - n=0; - if ((fp=fopen("abc.txt","r"))==NULL) - { - printf("不能打开文件abc.txt"); - return; - } - while (!feof(fp)) - { - fscanf(fp,"%s",&R[n].fname); //读fname域数据 - fscanf(fp,"%s",&R[n].sname); //读sname域数据 - n++; - } - fclose(fp); -} -TSonNode *CreateTree(char root[],RecType R[],int n) //创建一棵树 -{ - int i,j,k; - TSonNode *t; - t=(TSonNode *)malloc(sizeof(TSonNode)); //创建根结点 - strcpy(t->data,root); - for (k=0;ksons[k]=NULL; - i=0; j=0; - while (isons[j]=CreateTree(R[i].sname,R,n); - j++; - } - i++; - } - return t; -} - -void DispTree(TSonNode *t) //输出孩子链存储结构 -{ - int i; - if (t!=NULL) - { printf("%s",t->data); - if (t->sons[0]!=NULL) //t结点至少有一个孩子 - { printf("("); //输出一个左括号 - for (i=0;isons[i]); - if (t->sons[i+1]!=NULL) //如果有下一个孩子 - printf(","); //输出一个',' - else //如果没有下一个孩子 - break; //退出循环 - } - printf(")"); //输出一个右括号 - } - } -} -void DestroyTree(TSonNode *&t) //销毁树t -{ - int i; - if (t!=NULL) - { for (i=0;isons[i]!=NULL) //有子树 - DestroyTree(t->sons[i]); //销毁该子树 - else //再没有子树 - break; //退出循环 - } - free(t); //释放根结点 - } -} -TSonNode *FindNode(TSonNode *t,char x[]) //求x结点的指针 -{ int i; - TSonNode *p; - if (t==NULL) - return NULL; - else if (strcmp(t->data,x)==0) //找到值为x的结点 - return t; - else - { for (i=0;isons[i]!=NULL) - { p=FindNode(t->sons[i],x); - if (p!=NULL) return p; - } - else break; - return NULL; - } -} -int ChildCount(TSonNode *p) //求p所指结点的孩子个数 -{ int i,num=0; - for (i=0;isons[i]!=NULL) - num++; - else - break; - return num; -} -int Sonnum(TSonNode *t,char x[]) //求x单位的下一级单位数 -{ - TSonNode *p; - p=FindNode(t,x); - if (p==NULL) - return 0; - else - return ChildCount(p); -} -int LeafCount(TSonNode *t) //求树中叶子结点个数 -{ - int i,num=0; - if (t==NULL) - return 0; - else - { if (t->sons[0]==NULL) //t为叶子结点 - num++; - else //t不为叶子结点 - { for (i=0;isons[i]!=NULL) - num+=LeafCount(t->sons[i]); - else break; - } - return num; - } -} -int Classnum(TSonNode *t,char x[]) //求x单位的班数 -{ - TSonNode *p; - p=FindNode(t,x); - if (p==NULL) - return 0; - else - return LeafCount(p); -} -int LeafSum(TSonNode *t) //求树中叶子结点的数值和 -{ - int i,sum=0; - if (t==NULL) - return 0; - else - { - if (t->sons[0]==NULL) //t为叶子结点 - return atoi(t->data); - else //t不为叶子结点 - { for (i=0;isons[i]!=NULL) - sum+=LeafSum(t->sons[i]); - else break; - } - return sum; - } -} -int Studnum(TSonNode *t,char x[]) //求x单位的总学生人数 -{ - TSonNode *p; - p=FindNode(t,x); - if (p==NULL) - return 0; - else - return LeafSum(p); -} -int main() -{ - TSonNode *t; - RecType R[MaxSize]; - int n; - printf("(1)从abc.txt文件读数据到R数组中\n"); - ReadFile(R,n); - if (n==0) return 1; //记录个数为0时直接返回 - printf("(2)由数组R创建树t的孩子链存储结构\n"); - t=CreateTree(R[0].fname,R,n); //创建一棵树 - printf("(3)输出树t:"); DispTree(t); printf("\n"); - printf("(4)计算机学院的专业数:%d\n",Sonnum(t,"计算机学院")); - printf("(5)计算机学院的班数:%d\n",Classnum(t,"计算机学院")); - printf("(6)电信学院的学生数:%d\n",Studnum(t,"电信学院")); - printf("(7)销毁树t\n"); - DestroyTree(t); - - return 1; -} diff --git a/src/chap7/exp7-11.cpp b/src/chap7/exp7-11.cpp deleted file mode 100644 index b50b16221d3625c22704e03dad22b2d913299517..0000000000000000000000000000000000000000 --- a/src/chap7/exp7-11.cpp +++ /dev/null @@ -1,21 +0,0 @@ -//文件名:exp7-11.cpp -#include "preseq.cpp" //包含序列化和反序列化算法 -int main() -{ - BTNode *b,*b1; - SqString str; - printf("(1)创建二叉链b\n"); - CreateBTree(b,"A(B(D,E(,G)),C(,F(H,I)))"); - printf("(2)二叉树b:");DispBTree(b);printf("\n"); - printf("(3)对b进行先序遍历,产生先序序列化序列str\n"); - str=PreOrderSeq(b); - printf("(4)str:"); DispStr(str); - printf("(5)由str构建二叉链b1\n"); - b1=CreatePreSeq(str); - printf("(6)二叉树b1:");DispBTree(b1);printf("\n"); - printf("(7)销毁b和b1\n"); - DestroyBTree(b); - DestroyBTree(b1); - return 1; -} - \ No newline at end of file diff --git a/src/chap7/exp7-12.cpp b/src/chap7/exp7-12.cpp deleted file mode 100644 index 820ad9db6742303462f029bcbcb5430aba8aa07f..0000000000000000000000000000000000000000 --- a/src/chap7/exp7-12.cpp +++ /dev/null @@ -1,54 +0,0 @@ -//文件名:exp7-12.cpp -#include "preseq.cpp" //包含序列化和反序列化算法 -void GetNext(SqString t,int next[]) //由模式串t求出next值 -{ int j,k; - j=0;k=-1;next[0]=-1; - while (j=t.length) - return(i-t.length); //返回匹配模式串的首字符下标 - else - return(-1); //返回不匹配标志 -} -bool isSubtree(BTNode *b1,BTNode *b2) //判断b2是否是b1的子树 -{ - SqString s1=PreOrderSeq(b1); //求b1的先序序列化序列s1 - SqString s2=PreOrderSeq(b2); //求b2的先序序列化序列s2 - if (KMPIndex(s1,s2)!=-1) //若s2是s1的子串,返回真 - return true; - else //若s2不是s1的子串,返回假 - return false; -} -int main() -{ - BTNode *b1,*b2; - CreateBTree(b1,"A(B(D,E(,G)),C(,F(H,I)))"); - printf("二叉树b1:");DispBTree(b1);printf("\n"); - CreateBTree(b2,"C(,F(H,I))"); - printf("二叉树b2:");DispBTree(b2);printf("\n"); - - if (isSubtree(b1,b2)) - printf("结果:b2是b1的子树\n"); - else - printf("结果:b2不是b1的子树\n"); - DestroyBTree(b1); DestroyBTree(b2); - return 1; -} - \ No newline at end of file diff --git a/src/chap7/exp7-13.cpp b/src/chap7/exp7-13.cpp deleted file mode 100644 index 57e23bfdc59de24225c08a7bc542d6274e2cacdd..0000000000000000000000000000000000000000 --- a/src/chap7/exp7-13.cpp +++ /dev/null @@ -1,69 +0,0 @@ -//文件名:exp7-13.cpp -#include "preseq.cpp" //包含序列化和反序列化算法 -void GetNext(SqString t,int next[]) //由模式串t求出next值 -{ int j,k; - j=0;k=-1;next[0]=-1; - while (j=t.length) - return(i-t.length); //返回匹配模式串的首字符下标 - else - return(-1); //返回不匹配标志 -} -SqString PreOrderSeq1(BTNode *b) //由二叉链b产生先序序列化序列str -{ - SqString str,str1,leftstr,rightstr; - if (b==NULL) - { - StrAssign(str,"#"); - return str; - } - str.data[0]='@'; str.length=1; //构造只有特殊字符'@'的字符串str - leftstr=PreOrderSeq1(b->lchild); - str1=Concat(str,leftstr); - rightstr=PreOrderSeq1(b->rchild); - str=Concat(str1,rightstr); - return str; -} -bool isSubtree1(BTNode *b1,BTNode *b2) //判断b1中是否有与b2树形结构相同的子树 -{ - SqString s1=PreOrderSeq1(b1); //求b1的先序序列化序列s1 - SqString s2=PreOrderSeq1(b2); //求b2的先序序列化序列s2 - if (KMPIndex(s1,s2)!=-1) //若s2是s1的子串,返回真 - return true; - else //若s2不是s1的子串,返回假 - return false; -} -int main() -{ - BTNode *b1,*b2; - CreateBTree(b1,"A(B(D,E(,G)),C(,F(H,I)))"); - printf("二叉树b1:");DispBTree(b1);printf("\n"); - CreateBTree(b2,"c(,f(h,i))"); - printf("二叉树b2:");DispBTree(b2);printf("\n"); - - if (isSubtree1(b1,b2)) - printf("结果:b1中有与b2树形结构相同的子树\n"); - else - printf("结果:b1中没有与b2树形结构相同的子树\n"); - DestroyBTree(b1); DestroyBTree(b2); - return 1; -} - \ No newline at end of file diff --git a/src/chap7/exp7-2.cpp b/src/chap7/exp7-2.cpp deleted file mode 100644 index c9f6aa105686d1f4648b6726d1483b723840766f..0000000000000000000000000000000000000000 --- a/src/chap7/exp7-2.cpp +++ /dev/null @@ -1,167 +0,0 @@ -//文件名:exp7-2.cpp -#include "btree.cpp" //包含二叉树的基本运算算法 -void PreOrder(BTNode *b) //先序遍历的递归算法 -{ - if (b!=NULL) - { - printf("%c ",b->data); //访问根结点 - PreOrder(b->lchild); //递归访问左子树 - PreOrder(b->rchild); //递归访问右子树 - } -} -void PreOrder1(BTNode *b) //先序非递归遍历算法 -{ - BTNode *St[MaxSize],*p; - int top=-1; - if (b!=NULL) - { - top++; //根结点进栈 - St[top]=b; - while (top>-1) //栈不为空时循环 - { - p=St[top]; //退栈并访问该结点 - top--; - printf("%c ",p->data); - if (p->rchild!=NULL) //有右孩子,将其进栈 - { - top++; - St[top]=p->rchild; - } - if (p->lchild!=NULL) //有左孩子,将其进栈 - { - top++; - St[top]=p->lchild; - } - } - printf("\n"); - } -} -void InOrder(BTNode *b) //中序遍历的递归算法 -{ - if (b!=NULL) - { - InOrder(b->lchild); //递归访问左子树 - printf("%c ",b->data); //访问根结点 - InOrder(b->rchild); //递归访问右子树 - } -} -void InOrder1(BTNode *b) //中序非递归遍历算法 -{ - BTNode *St[MaxSize],*p; - int top=-1; - if (b!=NULL) - { - p=b; - while (top>-1 || p!=NULL) - { - while (p!=NULL) //扫描结点p的所有左下结点并进栈 - { - top++; - St[top]=p; - p=p->lchild; - } - if (top>-1) - { - p=St[top]; //出栈结点p并访问 - top--; - printf("%c ",p->data); - p=p->rchild; - } - } - printf("\n"); - } -} -void PostOrder(BTNode *b) //后序遍历的递归算法 -{ - if (b!=NULL) - { - PostOrder(b->lchild); //递归访问左子树 - PostOrder(b->rchild); //递归访问右子树 - printf("%c ",b->data); //访问根结点 - } -} -void PostOrder1(BTNode *b) //后序非递归遍历算法 -{ - BTNode *St[MaxSize]; - BTNode *p; - int top=-1; //栈指针置初值 - bool flag; - if (b!=NULL) - { - do - { - while (b!=NULL) //将b结点的所有左下结点进栈 - { - top++; - St[top]=b; - b=b->lchild; - } - p=NULL; //p指向当前结点的前一个已访问的结点 - flag=true; //flag为真表示正在处理栈顶结点 - while (top!=-1 && flag) - { - b=St[top]; //取出当前的栈顶元素 - if (b->rchild==p) //右子树不存在或已被访问,访问之 - { - printf("%c ",b->data); //访问b结点 - top--; - p=b; //p指向则被访问的结点 - } - else - { - b=b->rchild; //b指向右子树 - flag=false; //表示当前不是处理栈顶结点 - } - } - } while (top!=-1); - printf("\n"); - } -} -void TravLevel(BTNode *b) //层次遍历 -{ - BTNode *Qu[MaxSize]; //定义环形队列 - int front,rear; //定义队首和队尾指针 - front=rear=0; //置队列为空队 - if (b!=NULL) - printf("%c ",b->data); - rear++; //根结点进队 - Qu[rear]=b; - while (rear!=front) //队列不为空 - { - front=(front+1)%MaxSize; - b=Qu[front]; //出队结点b - if (b->lchild!=NULL) //输出左孩子,并进队 - { - printf("%c ",b->lchild->data); - rear=(rear+1)%MaxSize; - Qu[rear]=b->lchild; - } - if (b->rchild!=NULL) //输出右孩子,并进队 - { - printf("%c ",b->rchild->data); - rear=(rear+1)%MaxSize; - Qu[rear]=b->rchild; - } - } - printf("\n"); -} -int main() -{ - BTNode *b; - CreateBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); - printf("二叉树b:");DispBTree(b);printf("\n"); - printf("层次遍历序列:"); - TravLevel(b); - printf("先序遍历序列:\n"); - printf(" 递归算法:");PreOrder(b);printf("\n"); - printf(" 非递归算法:");PreOrder1(b); - printf("中序遍历序列:\n"); - printf(" 递归算法:");InOrder(b);printf("\n"); - printf(" 非递归算法:");InOrder1(b); - printf("后序遍历序列:\n"); - printf(" 递归算法:");PostOrder(b);printf("\n"); - printf(" 非递归算法:");PostOrder1(b); - DestroyBTree(b); - return 1; -} - \ No newline at end of file diff --git a/src/chap7/exp7-3.cpp b/src/chap7/exp7-3.cpp deleted file mode 100644 index 80a43c2e28bb33105682e16278371bec95afc553..0000000000000000000000000000000000000000 --- a/src/chap7/exp7-3.cpp +++ /dev/null @@ -1,102 +0,0 @@ -//文件名:exp7-3.cpp -#include "btree.cpp" //包含二叉树的基本运算算法 -#define MaxWidth 40 -BTNode *CreateBT1(char *pre,char *in,int n) -{ BTNode *b; - char *p; - int k; - if (n<=0) return NULL; - b=(BTNode *)malloc(sizeof(BTNode)); //创建二叉树结点*b - b->data=*pre; - for (p=in;plchild=CreateBT1(pre+1,in,k); //递归构造左子树 - b->rchild=CreateBT1(pre+k+1,p+1,n-k-1); //递归构造右子树 - return b; -} -BTNode *CreateBT2(char *post,char *in,int n) -{ BTNode *b; - char r,*p; - int k; - if (n<=0) return NULL; - r=*(post+n-1); //根结点值 - b=(BTNode *)malloc(sizeof(BTNode)); //创建二叉树结点*b - b->data=r; - for (p=in;plchild=CreateBT2(post,in,k); //递归构造左子树 - b->rchild=CreateBT2(post+k,p+1,n-k-1); //递归构造右子树 - return b; -} - -void DispBTree1(BTNode *b) //以凹入表表示法输出一棵二叉树 -{ - BTNode *St[MaxSize],*p; - int level[MaxSize][2],top=-1,n,i,width=4; - char type; - if (b!=NULL) - { - top++; - St[top]=b; //根结点入栈 - level[top][0]=width; - level[top][1]=2; //2表示是根 - while (top>-1) - { - p=St[top]; //退栈并凹入显示该结点值 - n=level[top][0]; - switch(level[top][1]) - { - case 0:type='L';break; //左结点之后输出(L) - case 1:type='R';break; //右结点之后输出(R) - case 2:type='B';break; //根结点之后前输出(B) - } - for (i=1;i<=n;i++) //其中n为显示场宽,字符以右对齐显示 - printf(" "); - printf("%c(%c)",p->data,type); - for (i=n+1;i<=MaxWidth;i+=2) - printf("--"); - printf("\n"); - top--; - if (p->rchild!=NULL) - { //将右子树根结点入栈 - top++; - St[top]=p->rchild; - level[top][0]=n+width; //显示场宽增width - level[top][1]=1; //1表示是右子树 - } - if (p->lchild!=NULL) - { //将左子树根结点入栈 - top++; - St[top]=p->lchild; - level[top][0]=n+width; //显示场宽增width - level[top][1]=0; //0表示是左子树 - } - } - } -} - -int main() -{ - BTNode *b; - ElemType pre[]="ABDEHJKLMNCFGI"; - ElemType in[]="DBJHLKMNEAFCGI"; - ElemType post[]="DJLNMKHEBFIGCA"; - int n=14; - b=CreateBT1(pre,in,n); - printf("先序序列:%s\n",pre); - printf("中序序列:%s\n",in); - printf("构造一棵二叉树b:\n"); - printf(" 括号表示法:");DispBTree(b);printf("\n"); - printf(" 凹入表示法:\n");DispBTree1(b);printf("\n\n"); - printf("中序序列:%s\n",in); - printf("后序序列:%s\n",post); - b=CreateBT2(post,in,n); - printf("构造一棵二叉树b:\n"); - printf(" 括号表示法:");DispBTree(b);printf("\n"); - printf(" 凹入表示法:\n");DispBTree1(b);printf("\n"); - DestroyBTree(b); - return 1; -} diff --git a/src/chap7/exp7-4.cpp b/src/chap7/exp7-4.cpp deleted file mode 100644 index a9ef1a50b877ef98320a72b0093dd18de735c6d1..0000000000000000000000000000000000000000 --- a/src/chap7/exp7-4.cpp +++ /dev/null @@ -1,137 +0,0 @@ -//文件名:exp7-4.cpp -#include -#include -#define MaxSize 100 -typedef char ElemType; -typedef struct node -{ ElemType data; - int ltag,rtag; //增加的线索标记 - struct node *lchild; //左孩子指针 - struct node *rchild; //右孩子指针 -} TBTNode; -void CreateTBTree(TBTNode * &b,char *str) //由str串建立含空线索域的二叉链b -{ TBTNode *St[MaxSize],*p=NULL; - int top=-1,k,j=0; - 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=(TBTNode *)malloc(sizeof(TBTNode)); - p->data=ch;p->lchild=p->rchild=NULL; - if (b==NULL) //p为二叉树的根结点 - b=p; - else //已建立二叉树根结点 - { switch(k) - { - case 1:St[top]->lchild=p;break; - case 2:St[top]->rchild=p;break; - } - } - } - j++; - ch=str[j]; - } -} -void DispTBTree(TBTNode *b) //输出含空线索域的二叉树b -{ if (b!=NULL) - { printf("%c",b->data); - if (b->lchild!=NULL || b->rchild!=NULL) - { printf("("); - DispTBTree(b->lchild); - if (b->rchild!=NULL) printf(","); - DispTBTree(b->rchild); - printf(")"); - } - } -} -TBTNode *pre; //全局变量 -void Thread(TBTNode *&p) //中序线索化二叉树,被CreateThread调用 -{ if (p!=NULL) - { Thread(p->lchild); //左子树线索化 - if (p->lchild==NULL) //若p结点的左指针为空 - { p->lchild=pre; //建立当前结点的前驱线索 - p->ltag=1; - } - else p->ltag=0; - if (pre->rchild==NULL) //若p结点的右指针为空 - { pre->rchild=p; //建立前驱结点的后继线索 - pre->rtag=1; - } - else pre->rtag=0; - pre=p; - Thread(p->rchild); //右子树线索化 - } -} -TBTNode *CreateThread(TBTNode *b) //创建中序线索化二叉树 -{ TBTNode *root; - root=(TBTNode *)malloc(sizeof(TBTNode)); //创建根结点 - root->ltag=0;root->rtag=1; - root->rchild=b; - if (b==NULL) //空二叉树 - root->lchild=root; - else - { root->lchild=b; - pre=root; //pre结点是p结点的前驱结点,供加线索用 - Thread(b); //中序遍历线索二叉树 - pre->rchild=root; //最后处理,加入指向根结点的线索 - pre->rtag=1; - root->rchild=pre; //根结点右线索化 - } - return root; -} -void InOrder(TBTNode *tb) //被ThInOrder算法调用 -{ - if (tb->lchild!=NULL && tb->ltag==0) //有左孩子 - InOrder(tb->lchild); - printf("%c ",tb->data); - if (tb->rchild!=NULL && tb->rtag==0) //有右孩子 - InOrder(tb->rchild); -} -void ThInOrder(TBTNode *tb) //中序线索二叉树的中序遍历递归算法 -{ - InOrder(tb->lchild); -} -void ThInOrder1(TBTNode *tb) //中序线索二叉树的中序非递归算法 -{ TBTNode *p=tb->lchild; //指向根结点 - while (p!=tb) - { while (p->ltag==0) p=p->lchild; //找中序开始结点 - printf("%c ",p->data); - while (p->rtag==1 && p->rchild!=tb) //有右线索的情况 - { p=p->rchild; - printf("%c ",p->data); - } - p=p->rchild; //转向结点p的右子树 - } -} -void DestroyTBTree1(TBTNode *tb) //被DestroyTBTree算法调用 -{ - if (tb!=NULL) - { - if (tb->lchild!=NULL && tb->ltag==0) //有左孩子 - DestroyTBTree1(tb->lchild); - if (tb->rchild!=NULL && tb->rtag==0) //有右孩子 - DestroyTBTree1(tb->rchild); - free(tb); - } -} -void DestroyTBTree(TBTNode *tb) //释放中序线索二叉树的所有结点 -{ - DestroyTBTree1(tb->lchild); - free(tb); -} -int main() -{ TBTNode *b,*tb; - CreateTBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); - printf("二叉树:");DispTBTree(b);printf("\n"); - tb=CreateThread(b); - printf("线索中序序列:\n"); - printf(" 递归算法:");ThInOrder(tb);printf("\n"); - printf(" 非递归算法:");ThInOrder1(tb);printf("\n"); - DestroyTBTree(tb); - return 1; -} diff --git a/src/chap7/exp7-5.cpp b/src/chap7/exp7-5.cpp deleted file mode 100644 index 80ed06f02c14a48d8cb14345e7b06ff4385bf941..0000000000000000000000000000000000000000 --- a/src/chap7/exp7-5.cpp +++ /dev/null @@ -1,104 +0,0 @@ -//文件名:exp7-5.cpp -#include -#include -#define N 50 //叶子结点数 -#define M 2*N-1 //树中结点总数 -typedef struct -{ - char data[5]; //结点值 - int weight; //权重 - int parent; //双亲结点 - int lchild; //左孩子结点 - int rchild; //右孩子结点 -} HTNode; -typedef struct -{ - char cd[N]; //存放哈夫曼码 - int start; -} HCode; -void CreateHT(HTNode ht[],int n) //由ht的叶子结点构造完整的哈夫曼树 -{ - int i,k,lnode,rnode; - int min1,min2; - for (i=0;i<2*n-1;i++) //所有结点的相关域置初值-1 - ht[i].parent=ht[i].lchild=ht[i].rchild=-1; - for (i=n;i<2*n-1;i++) //构造哈夫曼树的分支结点 - { - min1=min2=32767; //lnode和rnode为最小权重的两个结点位置 - lnode=rnode=-1; - for (k=0;k<=i-1;k++) //查找最小和次小的结点 - if (ht[k].parent==-1) //只在尚未构造二叉树的结点中查找 - { - if (ht[k].weightlchild==NULL && b->rchild==NULL) - return 1; - else - { - num1=Nodes(b->lchild); - num2=Nodes(b->rchild); - return (num1+num2+1); - } -} -int LeafNodes(BTNode *b) //求二叉树b的叶子结点个数 -{ - int num1,num2; - if (b==NULL) - return 0; - else if (b->lchild==NULL && b->rchild==NULL) - return 1; - else - { - num1=LeafNodes(b->lchild); - num2=LeafNodes(b->rchild); - return (num1+num2); - } -} -int Level(BTNode *b,ElemType x,int h) //求二叉树b中结点值为x的结点的层次 -{ int l; - if (b==NULL) - return(0); - else if (b->data==x) - return(h); - else - { l=Level(b->lchild,x,h+1); //在左子树中查找 - if (l!=0) - return(l); - else //在左子树中未找到,再在右子树中查找 - return(Level(b->rchild,x,h+1)); - } -} - -int BTWidth(BTNode *b) //求二叉树b的宽度 -{ - struct - { - int lno; //结点的层次 - BTNode *p; //结点指针 - } Qu[MaxSize]; //定义非环形队列 - int front,rear; //定义队首和队尾指针 - int lnum,max,i,n; - front=rear=0; //置队列为空队 - if (b!=NULL) - { - rear++; - Qu[rear].p=b; //根结点进队 - Qu[rear].lno=1; //根结点的层次为1 - while (rear!=front) //队不空时循环 - { - front++; - b=Qu[front].p; //出队结点p - lnum=Qu[front].lno; - if (b->lchild!=NULL) //有左孩子,将其进队 - { - rear++; - Qu[rear].p=b->lchild; - Qu[rear].lno=lnum+1; - } - if (b->rchild!=NULL) //有右孩子,将其进队 - { - rear++; - Qu[rear].p=b->rchild; - Qu[rear].lno=lnum+1; - } - } - max=0;lnum=1;i=1; //max存放宽度 - while (i<=rear) - { - n=0; - while (i<=rear && Qu[i].lno==lnum) //i扫描队列中所有结点 - { - n++; //n累计一层中的结点个数 - i++; - } - lnum=Qu[i].lno; - if (n>max) max=n; - } - return max; - } - else return 0; -} -int main() -{ - ElemType x='K'; - BTNode *b,*p,*lp,*rp;; - CreateBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); - printf("输出二叉树b:");DispBTree(b);printf("\n"); - printf("二叉树b的结点个数:%d\n",Nodes(b)); - printf("二叉树b的叶子结点个数:%d\n",LeafNodes(b)); - printf("二叉树b中值为%c结点的层次:%d\n",x,Level(b,x,1)); - printf("二叉树b的宽度:%d\n",BTWidth(b)); - DestroyBTree(b); - return 1; -} - diff --git a/src/chap7/exp7-7.cpp b/src/chap7/exp7-7.cpp deleted file mode 100644 index fb38e66dfd5353012d99bbad4197af20d9204ff6..0000000000000000000000000000000000000000 --- a/src/chap7/exp7-7.cpp +++ /dev/null @@ -1,145 +0,0 @@ -//文件名:exp7-7.cpp -#include "btree.cpp" //包含二叉树的基本运算算法 -void AllPath1(BTNode *b,ElemType path[],int pathlen) -//采用先序遍历方法输出所有从叶子结点到根结点的逆路径 -{ - if (b!=NULL) - { - if (b->lchild==NULL && b->rchild==NULL) //b为叶子结点 - { - printf(" %c到根结点逆路径: %c->",b->data,b->data); - for (int i=pathlen-1;i>0;i--) - printf("%c->",path[i]); - printf("%c\n",path[0]); - } - else - { - path[pathlen]=b->data; //将当前结点放入路径中 - pathlen++; //路径长度增1 - AllPath1(b->lchild,path,pathlen); //递归扫描左子树 - AllPath1(b->rchild,path,pathlen); //递归扫描右子树 - } - } -} -void LongPath1(BTNode *b,ElemType path[],int pathlen,ElemType longpath[],int &longpathlen) -//采用先序遍历方法输出第一条最长的逆路径 -{ - if (b==NULL) - { - if (pathlen>longpathlen) //若当前路径更长,将路径保存在longpath中 - { - for (int i=pathlen-1;i>=0;i--) - longpath[i]=path[i]; - longpathlen=pathlen; - } - } - else - { - path[pathlen]=b->data; //将当前结点放入路径中 - pathlen++; //路径长度增1 - LongPath1(b->lchild,path,pathlen,longpath,longpathlen); //递归扫描左子树 - LongPath1(b->rchild,path,pathlen,longpath,longpathlen); //递归扫描右子树 - } -} -void AllPath2(BTNode *b) //采用后序非递归遍历方法输出所有从叶子结点到根结点的逆路径 -{ - BTNode *st[MaxSize]; //定义一个顺序栈st - int top=-1; //栈顶指针初始化 - BTNode *p,*r; - bool flag; - p=b; - 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); //栈不空循环 -} - -void AllPath3(BTNode *b) -//采用层次遍历方法输出所有从叶子结点到根结点的逆路径 -{ - struct snode - { - BTNode *node; //存放当前结点指针 - int parent; //存放双亲结点在队列中的位置 - } Qu[MaxSize]; //定义顺序队列 - int front,rear,p; //定义队头和队尾指针 - front=rear=-1; //置队列为空队列 - rear++; - Qu[rear].node=b; //根结点指针进入队列 - Qu[rear].parent=-1; //根结点没有双亲结点 - while (frontlchild==NULL && b->rchild==NULL) //*b为叶子结点 - { - printf(" %c到根结点逆路径:",b->data); - p=front; - while (Qu[p].parent!=-1) - { - printf("%c->",Qu[p].node->data); - p=Qu[p].parent; - } - printf("%c\n",Qu[p].node->data); - } - if (b->lchild!=NULL) //左孩子入队列 - { - rear++; - Qu[rear].node=b->lchild; - Qu[rear].parent=front; - } - if (b->rchild!=NULL) //右孩子入队列 - { - rear++; - Qu[rear].node=b->rchild; - Qu[rear].parent=front; - } - } -} - -int main() -{ - BTNode *b; - ElemType path[MaxSize],longpath[MaxSize]; - int i,longpathlen=0; - CreateBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); - printf("二叉树b:");DispBTree(b);printf("\n"); - printf("先序遍历方法:\n");AllPath1(b,path,0); - LongPath1(b,path,0,longpath,longpathlen); - printf(" 第一条最长逆路径长度:%d\n",longpathlen); - printf(" 第一条最长逆路径:"); - for (i=longpathlen-1;i>=0;i--) - printf("%c ",longpath[i]); - printf("\n"); - printf("后序非递归遍历方法:\n");AllPath2(b); - printf("层次遍历方法:\n");AllPath3(b); - DestroyBTree(b); - return 1; -} diff --git a/src/chap7/exp7-8.cpp b/src/chap7/exp7-8.cpp deleted file mode 100644 index 127b96c7ceee74289cbd8302d0656ba7696b9922..0000000000000000000000000000000000000000 --- a/src/chap7/exp7-8.cpp +++ /dev/null @@ -1,78 +0,0 @@ -//文件名:exp7-8.cpp -#include "btree.cpp" //包含二叉树的基本运算算法 -#include -#include -typedef char ElemType; -BTNode *CRTree(char s[],int i,int j) //建立简单算术表达式s[i..j]对应的二叉树 -{ - BTNode *p; - int k,plus=0,posi; //plus记录运算符的个数 - if (i==j) //处理i=j的情况,说明只有一个字符 - { - p=(BTNode *)malloc(sizeof(BTNode)); - p->data=s[i]; - p->lchild=NULL; - p->rchild=NULL; - return p; - } - //以下为i!=j的情况 - for (k=i;k<=j;k++) //首先查找+和-运算符 - if (s[k]=='+' || s[k]=='-') - { - plus++; //plus记录+或者-的个数 - posi=k; //posi记录最后一个+或-的位置 - } - if (plus==0) //没有+或-的情况 - for (k=i;k<=j;k++) - if (s[k]=='*' || s[k]=='/') - { - plus++; //plus记录*或者/的个数 - posi=k; //posi记录最后一个*或/的位置 - } - if (plus!=0) //有运算符的情况,创建一个存放它的结点 - { - p=(BTNode *)malloc(sizeof(BTNode)); - p->data=s[posi]; - p->lchild=CRTree(s,i,posi-1); //递归处理s[i..posi-1]构造左子树 - p->rchild=CRTree(s,posi+1,j); //递归处理s[posi+1..j]构造右子树 - return p; - } - else //若没有任何运算符,返回NULL - return NULL; -} -double Comp(BTNode *b) //计算二叉树对应表达式的值 -{ - double v1,v2; - if (b==NULL) return 0; - if (b->lchild==NULL && b->rchild==NULL) - return b->data-'0'; //叶子结点直接返回结点值 - v1=Comp(b->lchild); //递归求出左子树的值v1 - v2=Comp(b->rchild); //递归求出右子树的值v2 - switch(b->data) //根据b结点做相应运算 - { - case '+': - return v1+v2; - case '-': - return v1-v2; - case '*': - return v1*v2; - case '/': - if (v2!=0) - return v1/v2; - else - abort(); //除0异常退出 - - } -} -int main() -{ - BTNode *b; - char s[MaxSize]="1+2*3-4/5"; - printf("算术表达式%s\n",s); - b=CRTree(s,0,strlen(s)-1); - printf("对应二叉树:"); - DispBTree(b); - printf("\n算术表达式的值:%g\n",Comp(b)); - DestroyBTree(b); - return 1; -} diff --git a/src/chap7/exp7-9.cpp b/src/chap7/exp7-9.cpp deleted file mode 100644 index 76d377242a475156bd1fa45910e63a432cb3786c..0000000000000000000000000000000000000000 --- a/src/chap7/exp7-9.cpp +++ /dev/null @@ -1,310 +0,0 @@ -//文件名:exp7-9.cpp -#include -#include -#include -#define MaxSize 30 //栈的最大元素个数 -#define NAMEWIDTH 10 //姓名的最多字符个数 -typedef struct fnode -{ - char father[NAMEWIDTH]; //父 - char wife[NAMEWIDTH]; //母 - char son[NAMEWIDTH]; //子 -} FamType; //家谱文件的记录类型 -typedef struct tnode -{ - char name[NAMEWIDTH]; - struct tnode *lchild,*rchild; -} BTree; //家谱二叉树结点树类型 -int n; //家谱记录个数 -FamType fam[MaxSize]; //家谱记录数组 -//----家谱二叉树操作算法----------------------------------- -BTree *CreateBTree(char *root) //从fam(含n个记录)递归创建一棵二叉树 -{ - int i=0,j; - BTree *b,*p; - b=(BTree *)malloc(sizeof(BTree)); //创建父亲结点 - strcpy(b->name,root); - b->lchild=b->rchild=NULL; - while (ilchild=p->rchild=NULL; - strcpy(p->name,fam[i].wife); - b->lchild=p; - for (j=0;jrchild=CreateBTree(fam[j].son); - p=p->rchild; - } - } - return(b); -} -void DispTree(BTree *b) //以括号表示法输出二叉树 -{ - if (b!=NULL) - { - printf("%s",b->name); - if (b->lchild!=NULL || b->rchild!=NULL) - { - printf("("); - DispTree(b->lchild); - if (b->rchild!=NULL) - printf(","); - DispTree(b->rchild); - printf(")"); - } - } -} -BTree *FindNode(BTree *b,char xm[]) //采用先序递归算法找name为xm的结点 -{ - BTree *p; - if (b==NULL) - return(NULL); - else - { - if (strcmp(b->name,xm)==0) - return(b); - else - { - p=FindNode(b->lchild,xm); - if (p!=NULL) - return(p); - else - return(FindNode(b->rchild,xm)); - } - } -} -void FindSon(BTree *b) //输出某人的所有儿子 -{ - char xm[NAMEWIDTH]; - BTree *p; - printf(" >>父亲姓名:"); - scanf("%s",xm); - p=FindNode(b,xm); - if (p==NULL) - printf(" >>不存在%s的父亲!\n",xm); - else - { - p=p->lchild; - if (p==NULL) - printf(" >>%s没有妻子\n",xm); - else - { - p=p->rchild; - if (p==NULL) - printf(" >>%s没有儿子!\n",xm); - else - { - printf(" >>%s的儿子:",xm); - while (p!=NULL) - { - printf("%10s",p->name); - p=p->rchild; - } - printf("\n"); - } - } - } -} -int Path(BTree *b,BTree *s) //采用后序非递归遍历方法输出从根结点到s结点的路径 -{ - BTree *St[MaxSize]; - BTree *p; - int i,top=-1; //栈指针置初值 - bool flag; - do - { - while (b) //将b的所有左下结点进栈 - { - top++; - St[top]=b; - b=b->lchild; - } - p=NULL; //p指向当前结点的前一个已访问的结点 - flag=true; //flag为真表示正在处理栈顶结点 - while (top!=-1 && flag) - { - b=St[top]; //取出当前的栈顶元素 - if (b->rchild==p) //右子树不存在或已被访问,访问之 - { if (b==s) //当前访问的结点为要找的结点,输出路径 - { - printf(" >>所有祖先:"); - for (i=0;iname); - printf("\n"); - return 1; - } - else - { - top--; - p=b; //p指向则被访问的结点 - } - } - else - { - b=b->rchild; //b指向右子树 - flag=false; //表示当前不是处理栈顶结点 - } - } - } while (top!=-1); //栈不空时循环 - return 0; //其他情况时返回0 -} -void Ancestor(BTree *b) //输出某人的所有祖先 -{ - BTree *p; - char xm[NAMEWIDTH]; - printf(" >>输入姓名:"); - scanf("%s",xm); - p=FindNode(b,xm); - if (p!=NULL) - Path(b,p); - else - printf(" >>不存在%s\n",xm); -} -void DestroyBTree(BTree *b) //销毁家谱二叉树 -{ - if (b!=NULL) - { - DestroyBTree(b->lchild); - DestroyBTree(b->rchild); - free(b); - } -} - -//----家谱文件操作算法--------------------------------------------- -void DelAll() //清除家谱文件全部记录 -{ - FILE *fp; - if ((fp=fopen("fam.dat","wb"))==NULL) - { - printf(" >>不能打开家谱文件\n"); - return; - } - n=0; - fclose(fp); -} -void ReadFile() //读家谱文件存入fam数组中 -{ - FILE *fp; - long len; - int i; - if ((fp=fopen("fam.dat","rb"))==NULL) - { - n=0; - return; - } - fseek(fp,0,2); //家谱文件位置指针移到家谱文件尾 - len=ftell(fp); //len求出家谱文件长度 - rewind(fp); //家谱文件位置指针移到家谱文件首 - n=len/sizeof(FamType); //n求出家谱文件中的记录个数 - for (i=0;i>数据家谱文件不能打开\n"); - return; - } - for (i=0;i>输入父亲、母亲和儿子姓名:"); - scanf("%s%s%s",fam[n].father,fam[n].wife,fam[n].son); - n++; -} -void OutputFile() //输出家谱文件全部记录 -{ - int i; - if (n<=0) - { - printf(" >>没有任何记录\n"); - return; - } - printf(" 父亲 母亲 儿子\n"); - printf(" ------------------------------\n"); - for (i=0;i1:输入 2:输出 9:全清 0:存盘返回 请选择:"); - scanf("%d",&sel); - switch(sel) - { - case 9: - DelAll(); - break; - case 1: - InputFam(); - break; - case 2: - OutputFile(); - break; - case 0: - SaveFile(); - break; - } - } while (sel!=0); -} -void BTreeop() //家谱二叉树操作 -{ - BTree *b; - int sel; - if (n==0) return; //家谱记录为0时直接返回 - b=CreateBTree(fam[0].father); - do - { - printf(" >1:括号表示法 2.找某人所有儿子 3.找某人所有祖先 0:返回 请选择:"); - scanf("%d",&sel); - switch(sel) - { - case 1: - printf(" >>");DispTree(b);printf("\n"); - break; - case 2: - FindSon(b); - break; - case 3: - printf(" >>");Ancestor(b); - break; - } - } while (sel!=0); - DestroyBTree(b); //销毁家谱二叉树 -} -int main() -{ - BTree *b; - int sel; - ReadFile(); - do - { - printf("*1.文件操作 2:家谱操作 0:退出 请选择:"); - scanf("%d",&sel); - switch(sel) - { - case 1: - Fileop(); - break; - case 2: - BTreeop(); - break; - } - } while (sel!=0); - return 1; -} diff --git a/src/chap7/fam.dat b/src/chap7/fam.dat deleted file mode 100644 index d6bca66c2180fcf4b035f59dc7f61f204f3d7647..0000000000000000000000000000000000000000 Binary files a/src/chap7/fam.dat and /dev/null differ diff --git a/src/chap7/preseq.cpp b/src/chap7/preseq.cpp deleted file mode 100644 index e86fee0560b3120369186dfd2d3b8a0b7d84db53..0000000000000000000000000000000000000000 --- a/src/chap7/preseq.cpp +++ /dev/null @@ -1,34 +0,0 @@ -//文件名:exp7-11.cpp -#include "btree.cpp" //包含二叉树的基本运算算法 -#include "sqstring.cpp" //包含顺序串的基本运算算法 -int i=0; //全局变量 -SqString PreOrderSeq(BTNode *b) //由二叉链b产生先序序列化序列str -{ - SqString str,str1,leftstr,rightstr; - if (b==NULL) - { - StrAssign(str,"#"); - return str; - } - str.data[0]=b->data; str.length=1; //构造只有b->data字符的字符串str - leftstr=PreOrderSeq(b->lchild); - str1=Concat(str,leftstr); - rightstr=PreOrderSeq(b->rchild); - str=Concat(str1,rightstr); - return str; -} -BTNode *CreatePreSeq(SqString str) //由先序序列化序列str创建二叉链并返回根结点 -{ - BTNode *b; - char value; - if (i>=str.length) //i超界返回空 - return NULL; - value=str.data[i]; i++; //从str中取出一个字符value - if (value=='#') //若value为'#',返回空 - return NULL; - b=(BTNode *)malloc(sizeof(BTNode)); //创建根结点 - b->data=value; - b->lchild=CreatePreSeq(str); //递归构造左子树 - b->rchild=CreatePreSeq(str); //递归构造右子树 - return b; //返回根结点 -} diff --git a/src/chap7/sqstring.cpp b/src/chap7/sqstring.cpp deleted file mode 100644 index 7f6980cfa96bd2f91be06b0265773d8e351be477..0000000000000000000000000000000000000000 --- a/src/chap7/sqstring.cpp +++ /dev/null @@ -1,118 +0,0 @@ -//顺序串基本运算的算法 -#include -#define MaxSize 100 -typedef struct -{ - char data[MaxSize]; //串中字符 - int length; //串长 -} SqString; //声明顺序串类型 -void StrAssign(SqString &s,char cstr[]) //字符串常量赋给串s -{ - int i; - for (i=0;cstr[i]!='\0';i++) - s.data[i]=cstr[i]; - s.length=i; -} -void DestroyStr(SqString &s) //销毁串 -{ } - -void StrCopy(SqString &s,SqString t) //串复制 -{ - for (int i=0;is.length || j<0 || i+j-1>s.length) - return str; //参数不正确时返回空串 - for (k=i-1;ks1.length+1) //参数不正确时返回空串 - return str; - for (j=0;js.length || i+j>s.length+1) //参数不正确时返回空串 - return str; - for (k=0;ks.length || i+j-1>s.length) //参数不正确时返回空串 - return str; - for (k=0;k0) - { for (int i=0;ilchild,h+1,a); - levelnodes(b->lchild,h+1,a); - } -} -int BTWidth1(BTNode *b) -{ - int width=0,i; - int a[MaxSize]; - for (i=1;iwidth) - width=a[i]; - i++; - } - return width; -} - -int BTWidth2(BTNode *b) //求二叉树b的宽度 -{ - struct - { - int lno; //结点的层次 - BTNode *p; //结点指针 - } Qu[MaxSize]; //定义非环形队列 - int front,rear; //定义队头和队尾指针 - int lnum,width,i,n; - front=rear=0; //置队列为空队 - if (b!=NULL) - { - rear++; - Qu[rear].p=b; //根结点进队 - Qu[rear].lno=1; //根结点的层次为1 - while (rear!=front) //队不空时循环 - { - front++; - b=Qu[front].p; //出队结点p - lnum=Qu[front].lno; - if (b->lchild!=NULL) //有左孩子,将其进队 - { - rear++; - Qu[rear].p=b->lchild; - Qu[rear].lno=lnum+1; - } - if (b->rchild!=NULL) //有右孩子,将其进队 - { - rear++; - Qu[rear].p=b->rchild; - Qu[rear].lno=lnum+1; - } - } - width=0;lnum=1;i=1; //width存放宽度 - while (i<=rear) - { - n=0; - while (i<=rear && Qu[i].lno==lnum) //i扫描队列中所有结点 - { - n++; //n累计一层中的结点个数 - i++; - } - lnum=Qu[i].lno; - if (n>width) width=n; - } - return width; - } - else return 0; -} -int main() -{ - ElemType x='K'; - BTNode *b,*p,*lp,*rp;; - CreateBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))"); - printf("输出二叉树b:");DispBTree(b);printf("\n"); - printf("二叉树b的宽度:%d\n",BTWidth2(b)); - DestroyBTree(b); - return 1; -} - diff --git a/src/chap8/data14.txt b/src/chap8/data14.txt deleted file mode 100644 index 41986923ec3f6296c0295cc89bc62716a27104eb..0000000000000000000000000000000000000000 --- a/src/chap8/data14.txt +++ /dev/null @@ -1,6 +0,0 @@ -3 -0 990 692 -990 0 179 -692 179 0 -1 -1 2 \ No newline at end of file diff --git a/src/chap8/data15.txt b/src/chap8/data15.txt deleted file mode 100644 index 8e6338eb530e04d552efa55a7647a0b4e71d9320..0000000000000000000000000000000000000000 --- a/src/chap8/data15.txt +++ /dev/null @@ -1,7 +0,0 @@ -4 6 -1 2 10 -2 1 60 -1 3 20 -3 4 10 -2 4 5 -4 1 50 \ No newline at end of file diff --git a/src/chap8/data16.txt b/src/chap8/data16.txt deleted file mode 100644 index 9027639e4abf5b2f126bd4596d6ecfb441650ac6..0000000000000000000000000000000000000000 --- a/src/chap8/data16.txt +++ /dev/null @@ -1,4 +0,0 @@ -3 2 -1 2 5 6 -2 3 4 5 -1 3 \ No newline at end of file diff --git a/src/chap8/exp8-1.cpp b/src/chap8/exp8-1.cpp deleted file mode 100644 index 5bd98b7230cc3aadc8ce1a819bad3283ff9b4942..0000000000000000000000000000000000000000 --- a/src/chap8/exp8-1.cpp +++ /dev/null @@ -1,22 +0,0 @@ -//ļ:exp8-1.cpp -#include "graph.cpp" //ͼĴ洢ṹ㷨 -int main() -{ - MatGraph g; - AdjGraph *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,e=10; //ͼ8.1е - CreateMat(g,A,n,e); - printf("(1)ͼGڽӾ:\n"); DispMat(g); - CreateAdj(G,A,n,e); - printf("(2)ͼGڽӱ:\n"); DispAdj(G); - printf("(3)ͼGڽӱ\n"); - DestroyAdj(G); - return 1; -} \ No newline at end of file diff --git a/src/chap8/exp8-10.cpp b/src/chap8/exp8-10.cpp deleted file mode 100644 index 51dfdfd35fdcdd27c54536b92ecd56abfeb5c2dd..0000000000000000000000000000000000000000 --- a/src/chap8/exp8-10.cpp +++ /dev/null @@ -1,127 +0,0 @@ -//文件名:exp8-10.cpp -#include "graph.cpp" //包含图的存储结构及基本运算算法 -int visited[MAXV]; //全局数组 -void PathAll1(AdjGraph *G,int u,int v,int path[],int d) -//输出图G中从顶点u到v的所有简单路径 -{ - ArcNode *p; - int j,w; - d++; path[d]=u; //路径长度d增1,将当前顶点添加到路径中 - visited[u]=1; - if (u==v && d>0) //找到终点 - { - for (j=0;j<=d;j++) - printf("%3d",path[j]); - printf("\n"); - visited[u]=0; - return; - } - p=G->adjlist[u].firstarc; //p指向顶点u的第一个相邻点 - while (p!=NULL) - { - w=p->adjvex; //w为u的相邻点编号 - if (visited[w]==0 ) //若该顶点未标记访问,则递归访问之 - PathAll1(G,w,v,path,d); - p=p->nextarc; //找u的下一个相邻点 - } - visited[u]=0; -} -void PathAll2(AdjGraph *G,int u,int v,int l,int path[],int d) -//输出图G中从顶点u到v的长度为l的所有简单路径,d是到当前为止已走过的路径长度,调用时初值为-1 -{ - int w,i; - ArcNode *p; - visited[u]=1; - d++; path[d]=u; //路径长度d增1,将当前顶点添加到路径中 - if (u==v && d==l) //满足条件,输出一条路径 - { - for (i=0;i<=d;i++) - printf("%3d",path[i]); - printf("\n"); - visited[u]=0; - return; - } - p=G->adjlist[u].firstarc; //p指向顶点u的第一个相邻点 - while (p!=NULL) - { - w=p->adjvex; //w为顶点u的相邻点 - if (visited[w]==0) //若该顶点未标记访问,则递归访问之 - PathAll2(G,w,v,l,path,d); - p=p->nextarc; //找u的下一个相邻点 - } - visited[u]=0; //取消访问标记,以使该顶点可重新使用 -} -int ShortPath(AdjGraph *G,int u,int v,int path[]) -//求顶点u到顶点v(u≠v)的最短路径 -{ struct - { int vno; //当前顶点编号 - int level; //当前顶点的层次 - int parent; //当前顶点的当一个节点编号 - } qu[MAXV]; //定义顺序非循环队列 - int front=-1,rear=-1,k,lev,i,j; - ArcNode *p; - visited[u]=1; - rear++; //顶点u已访问,将其入队 - qu[rear].vno=u; - qu[rear].level=0; //根节点层次置为1 - qu[rear].parent=-1; - while (frontadjlist[k].firstarc; //p指向顶点k的第一个相邻点 - while (p!=NULL) //依次搜索k的相邻点 - { if (visited[p->adjvex]==0) //若未访问过 - { visited[p->adjvex]=1; - rear++; - qu[rear].vno=p->adjvex; //访问过的相邻点进队 - qu[rear].level=lev+1; - qu[rear].parent=front; - } - p=p->nextarc; //找顶点k的下一相邻点 - } - } - return -1; //如果未找到顶点v,返回一特殊值-1 -} -int main() -{ - int i,j; - int u=5,v=2,l=3; - int path[MAXV]; - AdjGraph *G; - int A[MAXV][MAXV]={ - {0,1,0,1,0,0}, - {0,0,1,0,0,0}, - {1,0,0,0,0,1}, - {0,0,1,0,0,1}, - {0,0,0,1,0,0}, - {1,1,0,1,1,0}}; - int n=6, e=10; - CreateAdj(G,A,n,e); //建立图8.21的邻接表 - printf("图G的邻接表:\n"); DispAdj(G); - printf("(1)从顶点%d到%d的所有路径:\n",u,v); - for (i=0;i",path[i]); - printf("%d\n",path[i]); - } - p=G->adjlist[vi].firstarc; //找vi的第一个邻接顶点 - while (p!=NULL) - { - v=p->adjvex; //v为vi的邻接顶点 - if (visited[v]==0) //若该顶点未标记访问,则递归访问之 - TravPath(G,v,vj,path,d); - p=p->nextarc; //找vi的下一个邻接顶点 - } - visited[vi]=0; //取消访问标记,以使该顶点可重新使用 - d--; -} -int main() -{ - int i,u,v; - int path[MAXV]; - AdjGraph *G; - int A[MAXV][MAXV]={ - {0,1,1,1,1,0,0,0,0,0,0,0,0,0,0}, - {1,0,0,0,0,0,1,0,1,0,0,0,0,0,0}, - {1,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, - {1,0,0,0,0,0,1,0,0,0,0,0,0,0,0}, - {1,0,0,0,0,0,0,1,0,0,0,0,0,0,0}, - {0,0,0,0,0,0,0,0,1,1,0,0,0,0,0}, - {0,1,0,1,0,0,0,1,0,1,0,0,0,0,0}, - {0,0,0,0,1,0,1,0,0,0,1,1,0,0,0}, - {0,1,0,0,0,1,0,0,0,0,0,0,1,0,0}, - {0,0,0,0,0,1,1,0,0,0,1,0,1,0,0}, - {0,0,0,0,0,0,0,1,0,1,0,0,0,1,0}, - {0,0,0,0,0,0,0,1,0,0,0,0,0,1,0}, - {0,0,0,0,0,0,0,0,1,1,0,0,0,0,1}, - {0,0,0,0,0,0,0,0,0,0,1,1,0,0,1}, - {0,0,0,0,0,0,0,0,0,0,0,0,1,1,0}}; - CreateAdj(G,A,15,21); - printf("图G的邻接表:\n"); DispAdj(G); - for (i=0;i -#include -#define MAXV 201 -//------图的邻接表定义--------- -typedef struct ANode -{ int no; //动物编号 - struct ANode *nextarc; //指向下一个可通信动物节点 -} ArcNode; -typedef struct Vnode -{ - ArcNode *firstarc; //指向第一个可通信动物节点 -} VNode; -typedef struct -{ int n; //动物个数,即顶点数 - int m; //可通信动物对数,即边数 - VNode adjlist[MAXV]; //表头节点数组 -} ALGraph; - -int BFS(ALGraph *G,int s,int e) //采用广度优先遍历查找s到e的最短路径长度 -{ - int visited[MAXV]; - struct - { int no; //动物顶点编号 - int level; //层次 - } qu[MAXV]; //环形队列 - int front=0,rear=0,i,w,l; - ArcNode *p; - if (s==e) return 0; - for (i=0;in;i++) visited[i]=0; - visited[s]=1; - rear=(rear+1) % MAXV; //起点s进队 - qu[rear].no=s; - qu[rear].level=0; //起点的层次设置为0 - while (front!=rear) //队不空循环 - { - front=(front+1) % MAXV; - w=qu[front].no; //出队顶点w - l=qu[front].level; //顶点w的层次为l - p=G->adjlist[w].firstarc; //找顶点w的第一个相邻点 - while (p!=NULL) - { - if (visited[p->no]==0) //若该顶点没有访问过 - { - if (p->no==e) //找到终点e,访问其层次 - return qu[rear].level; - visited[p->no]=1; //访问它 - rear=(rear+1) % MAXV; //将它进队 - qu[rear].no=p->no; - qu[rear].level=l+1; - } - p=p->nextarc; - } - } - return -1; -} - -void InitGraph(ALGraph *&G,int n) //初始化邻接表 -{ int i; - G=(ALGraph *)malloc(sizeof(ALGraph)); - for (i=0;iadjlist[i].firstarc=NULL; - G->n=n; - G->m=0; -} -void Add(ALGraph *&G,int a,int b) //图中添加一条边(a,b) -{ ArcNode *p; - p=(ArcNode *)malloc(sizeof(ArcNode)); - p->no=b; - p->nextarc=G->adjlist[a].firstarc; - G->adjlist[a].firstarc=p; - p=(ArcNode *)malloc(sizeof(ArcNode)); - p->no=a; - p->nextarc=G->adjlist[b].firstarc; - G->adjlist[b].firstarc=p; - G->m++; -} -void DestroyGraph(ALGraph *&G) //销毁图 -{ - ArcNode *pre,*p; - for (int i=0;in;i++) - { - pre=G->adjlist[i].firstarc; - if (pre!=NULL) - { - p=pre->nextarc; - while (p!=NULL) - { - free(pre); - pre=p; p=p->nextarc; - } - free(pre); - } - } - free(G); -} -void DispGraph(ALGraph *G) //输出图 -{ - int i; - ArcNode *p; - printf("n=%d,e=%d\n",G->n,G->m); - for (i=0;in;i++) - { printf("[%3d]:",i); - p=G->adjlist[i].firstarc; - while (p!=NULL) - { printf("→(%d)",p->no); - p=p->nextarc; - } - printf("→∧\n"); - } -} -int main() -{ - ALGraph *G; - int m,n,k,a,b,s,e,i; - FILE *fp; - fp=fopen("test.txt","r"); - if (fp==NULL) - { - printf("不能打开test.txt文件\n"); - return 0; - } - fscanf(fp,"%d%d",&n,&m); - InitGraph(G,n); - for (i=0;i=0;s--) //输出路径上的中间顶点 - printf("%d→",apath[s]); -} -int Mincycle(MatGraph g,int A[MAXV][MAXV],int &mini,int &minj) //在图g和A中的查找一个最小环 -{ - int i,j,min=INF; - for (i=0;iA[i][k]+A[k][j]) - { A[i][j]=A[i][k]+A[k][j]; //修改最短路径长度 - path[i][j]=path[k][j]; //修改最短路径 - } - } - min=Mincycle(g,A,mini,minj); - if (min!=INF) - { - printf(" 图中最小环:"); - Dispapath(path,mini,minj); //输出一条最短路径 - printf("%d, 长度:%d\n",mini,min); - } - else - printf(" 图中没有任何环\n"); - - -} -int main() -{ - MatGraph g; - int A[MAXV][MAXV]={ - {0,10,1,INF}, - {21,0,INF,6}, - {INF,1,0,INF}, - {5,INF,INF,0} }; - int n=4, e=6; - CreateMat(g,A,n,e); //建立图的邻接矩阵 - printf("有向图G的邻接矩阵:\n"); DispMat(g); - printf("求解结果:\n"); - Floyd(g); - return 1; -} \ No newline at end of file diff --git a/src/chap8/exp8-14.cpp b/src/chap8/exp8-14.cpp deleted file mode 100644 index 38c06191ca6b52da7a51958548b4f2c1ef77f1c4..0000000000000000000000000000000000000000 --- a/src/chap8/exp8-14.cpp +++ /dev/null @@ -1,128 +0,0 @@ -#include -#include -#include -#include -using namespace std; -#define INF 0x3f3f3f3f -#define MAXV 105 -int mat[MAXV][MAXV]; -int U[MAXV]; -int lowcost[MAXV]; -int n; - -int Prim() //解法1:Prim算法求顶点1出发的最小生成树的权值和 -{ memset(U,0,sizeof(U)); - memset(lowcost,0x3f,sizeof(lowcost)); - int ans=0; //存放结果 - lowcost[1]=0; - for(int i=1;i<=n;i++) - { int minc=INF,k=0; - for(int j=1;j<=n;j++) //在(V-U)中找出离U最近的顶点k - if(!U[j] && lowcost[j]mat[k][i]) - lowcost[i]=mat[k][i]; - } - return ans; -} -//----并查集基本运算算法 -int parent[MAXV]; //并查集存储结构 -int rnk[MAXV]; //存储结点的秩 -void Init(int n) //并查集初始化 -{ for (int i=1;i<=n;i++) //顶点编号1到n - { parent[i]=i; - rnk[i]=0; - } -} -int Find(int x) //并查集中查找x结点的根结点 -{ if (x!=parent[x]) - parent[x]=Find(parent[x]); //路径压缩 - return parent[x]; -} -void Union(int x,int y) //并查集中x和y的两个集合的合并 -{ int rx=Find(x); - int ry=Find(y); - if (rx==ry) //x和y属于同一棵树的情况 - return; - if (rnk[rx]u=u; - this->v=v; - this->w=w; - } - bool operator<(const Edge &s) const //重载<运算符 - { - return w E; //建立存放所有边的向量E - for (int i=1;i<=n;i++) //由图的邻接矩阵g产生边向量E - for (int j=1;j<=n;j++) - if (i -#define MAXV 105 -#define INF 0x3f3f3f3f -int N,M; -void Dijkstra(int g[MAXV][MAXV],int v,int dist[MAXV]) //Dijkstra算法 -{ - int S[MAXV]; //S[i]=1表示顶点i在S中, S[i]=0表示顶点i在U中 - int mindist,i,j,u; - for (i=1;i<=N;i++) - { dist[i]=g[v][i]; //距离初始化 - S[i]=0; //S[]置空 - } - S[v]=1; //源点编号v放入S中 - for (i=1;i:c - printf("<%d,%d>:%d\n",a,b,c); - A[a][b]=B[b][a]=c; - } - Dijkstra(A,1,dist1); - printf("求出dist1: ["); - for(int i=2;i<=N;i++) - printf("%4d",dist1[i]); - printf("]\n"); - Dijkstra(B,1,dist2); - printf("求出dist2: ["); - for(int i=2;i<=N;i++) - printf("%4d",dist2[i]); - printf("]\n"); - int ans=0; - for(int i=1;i<=N;i++) - ans+=(dist1[i]+dist2[i]); - return ans; -} - -int main() -{ - printf("ans=%d\n",solve()); - return 0; -} - diff --git a/src/chap8/exp8-16.cpp b/src/chap8/exp8-16.cpp deleted file mode 100644 index 1fad6808375867ef12f963a1da70a66a82141f47..0000000000000000000000000000000000000000 --- a/src/chap8/exp8-16.cpp +++ /dev/null @@ -1,75 +0,0 @@ -#include -using namespace std; -#define MAXV 1010 -#define INF 0x3f3f3f3f //定义∞ -int n,m; -int D[MAXV][MAXV],C[MAXV][MAXV]; -int s,t; -void Dijkstra() //狄克斯特拉算法 -{ int dist[MAXV]; - int cost[MAXV]; - int S[MAXV]; - int mindist,u; - for(int i=1;i<=n;i++) //dist、cost、S初始化 - { dist[i]=D[s][i]; - cost[i]=C[s][i]; - S[i]=0; - } - dist[s]=cost[s]=0; - S[s]=1; - for(int i=0;idist[j]) - { mindist=dist[j]; - u=j; - } - } - if (mindist==INF) break; //找不到连通的顶点 - S[u]=1; //将顶点u加入S集合 - for (int j=1;j<=n;j++) //考虑顶点u,求s到每个顶点j的最短路径长度和花费 - { int d=mindist+D[u][j]; //d记录经过顶点u的路径长度 - int c=cost[u]+C[u][j]; //c记录经过顶点u的花费 - if(S[j]==0 && dd) - { D[a][b]=D[b][a]=d; //无向图的边是对称的 - C[a][b]=C[b][a]=p; - } - } - scanf("%d%d",&s,&t); - printf(" 起点:%d 终点:%d\n",s,t); - printf("求解结果\n"); - Dijkstra(); - return 0; -} - diff --git a/src/chap8/exp8-2.cpp b/src/chap8/exp8-2.cpp deleted file mode 100644 index 01cc3f4cfb2be0605fd15a98c797c53e2453178a..0000000000000000000000000000000000000000 --- a/src/chap8/exp8-2.cpp +++ /dev/null @@ -1,24 +0,0 @@ -//文件名:exp8-2.cpp -#include "travsal.cpp" -int main() -{ - AdjGraph *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,e=10; //图8.1中的数据 - CreateAdj(G,A,n,e); - printf("图G的邻接表:\n"); DispAdj(G); - printf("从顶点0开始的DFS(递归算法):\n"); - DFS(G,0);printf("\n"); - printf("从顶点0开始的DFS(非递归算法):\n"); - DFS1(G,0); - printf("从顶点0开始的BFS:\n"); - BFS(G,0); - DestroyAdj(G); - return 1; -} diff --git a/src/chap8/exp8-3.cpp b/src/chap8/exp8-3.cpp deleted file mode 100644 index d3011ea1f4d2e9e329a7027689ba6cadfa72782b..0000000000000000000000000000000000000000 --- a/src/chap8/exp8-3.cpp +++ /dev/null @@ -1,48 +0,0 @@ -//文件名:exp8-3.cpp -#include "graph.cpp" //包含图的存储结构及基本运算算法 -int visited[MAXV]; -void DFSALL(AdjGraph *G,int v,int path[],int d) -{ - ArcNode *p; - visited[v]=1; //置已访问标记 - path[d]=v; - d++; - if (d==G->n) //如果已访问所有顶点,则输出访问序列 - { - for (int k=0;kadjlist[v].firstarc; //p指向顶点v的第一个相邻点 - while (p!=NULL) - { - if (visited[p->adjvex]==0) //若p->adjvex顶点未访问,递归访问它 - DFSALL(G,p->adjvex,path,d); - p=p->nextarc; //找顶点v的下一个相邻点 - } - visited[v]=0; -} -int main() -{ - AdjGraph *G; - int A[MAXV][MAXV]={ - {0,1,0,1,1}, - {1,0,1,1,0}, - {0,1,0,1,1}, - {1,1,1,0,1}, - {1,0,1,1,0}}; - int n=5,e=8; //《教程》中图8.1中的数据 - CreateAdj(G,A,n,e); - printf("图G的邻接表:\n"); DispAdj(G); - - int path[MAXV],v=1; - printf("从顶点%d出发的所有深度优先序列:\n",v); - DFSALL(G,v,path,0); - printf("\n"); - - DestroyAdj(G); - return 1; - -} diff --git a/src/chap8/exp8-4.cpp b/src/chap8/exp8-4.cpp deleted file mode 100644 index 7218c388fb3ca4510d8d3d74eb5149b27201fdad..0000000000000000000000000000000000000000 --- a/src/chap8/exp8-4.cpp +++ /dev/null @@ -1,77 +0,0 @@ -//文件名:exp8-4.cpp -#include "graph.cpp" //包含图的存储结构及基本运算算法 -#define MaxSize 100 -int visited[MAXV]={0}; -void DFSTree(AdjGraph *G,int v) //求图G从顶点v出发的深度优先生成树 -{ - ArcNode *p; - visited[v]=1; //置已访问标记 - p=G->adjlist[v].firstarc; //p指向顶点v的第一个相邻点 - while (p!=NULL) - { - if (visited[p->adjvex]==0) //若p->adjvex顶点未访问,递归访问它 - { - printf("(%d,%d) ",v,p->adjvex); - DFSTree(G,p->adjvex); - } - p=p->nextarc; //p指向顶点v的下一个相邻点 - } -} -void BFSTree(AdjGraph *G,int v) //求图G从顶点v出发的广度优先生成树 -{ - int w,i; - int qu[MAXV]; //定义环形队列 - int front=0,rear=0; - ArcNode *p; - int visited[MAXV]; //定义顶点访问标志数组 - for (i=0;in;i++) visited[i]=0; //访问标志数组初始化 - visited[v]=1; //置已访问标记 - rear++; //顶点v进队 - qu[rear]=v; - while (front!=rear) //队不空循环 - { - front=(front+1) % MAXV; //出队一个顶点w - w=qu[front]; - p=G->adjlist[w].firstarc; //p指向w的第一个相邻点 - while (p!=NULL) //查找w的所有相邻点 - { - if (visited[p->adjvex]==0) //若当前邻接点未被访问 - { - printf("(%d,%d) ",w,p->adjvex); - visited[p->adjvex]=1; //置已访问标记 - rear=(rear+1) % MAXV; //顶点p->adjvex进队 - qu[rear]=p->adjvex; - } - p=p->nextarc; //p指向顶点v的下一个相邻点 - } - } - printf("\n"); -} -int main() -{ - AdjGraph *G; - int A[MAXV][MAXV]; - int n=11, e=13; - for (int i=0;i=0 && temp.w=0;j--) //再输出其他顶点 - printf(",%d",apath[j]); - printf("\n"); - } - } -} - -void Dijkstra(MatGraph g,int v) //Dijkstra算法 -{ int dist[MAXV],path[MAXV]; - int S[MAXV]; //S[i]=1表示顶点i在S中, S[i]=0表示顶点i在U中 - int Mindis,i,j,u; - for (i=0;i=0;s--) //输出路径上的中间顶点 - printf(",%d",apath[s]); - printf(" \t路径长度为:%d\n",A[i][j]); - } - } -} - -void Floyd(MatGraph g) //Floyd算法 -{ int A[MAXV][MAXV],path[MAXV][MAXV]; - int i,j,k; - for (i=0;iA[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() -{ - 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, e=10; - CreateMat(g,A,n,e); //建立图8.1的邻接矩阵 - printf("有向图G的邻接矩阵:\n"); DispMat(g); - printf("弗洛伊德算法求解结果:\n"); - Floyd(g); - return 1; -} \ No newline at end of file diff --git a/src/chap8/exp8-9.cpp b/src/chap8/exp8-9.cpp deleted file mode 100644 index c73ff09cbe3fe9218af5dee5b652d696cf46764c..0000000000000000000000000000000000000000 --- a/src/chap8/exp8-9.cpp +++ /dev/null @@ -1,133 +0,0 @@ -//文件名:exp8-9.cpp -#include "graph.cpp" //包含图的存储结构及基本运算算法 -typedef struct -{ int ino; //起点 - int eno; //终点 -} KeyNode; //关键活动类型 - -bool TopSort(AdjGraph *G,int topseq[]) -//产生含有n个顶点编号的拓扑序列topseq -{ - int i,j,n=0; - int st[MAXV]; //定义一个顺序栈 - int top=-1; //栈顶指针为top - ArcNode *p; - for (i=0;in;i++) //所有顶点的入度置初值0 - G->adjlist[i].count=0; - for (i=0;in;i++) //求所有顶点的入度 - { p=G->adjlist[i].firstarc; - while (p!=NULL) - { G->adjlist[p->adjvex].count++; - p=p->nextarc; - } - } - for (i=0;in;i++) - if (G->adjlist[i].count==0) //入度为0的顶点进栈 - { top++; - st[top]=i; - } - while (top>-1) //栈不为空时循环 - { i=st[top];top--; //出栈 - topseq[n]=i; n++; - p=G->adjlist[i].firstarc; //找第一个邻接点 - while (p!=NULL) - { j=p->adjvex; - G->adjlist[j].count--; - if (G->adjlist[j].count==0) //入度为0的相邻顶点进栈 - { top++; - st[top]=j; - } - p=p->nextarc; //找下一个邻接点 - } - } - if (nn) //拓扑序列中不含所有顶点时 - return false; - else - { - printf("拓扑序列:"); - for (i=0;in-1]; //求出汇点 - int ve[MAXV]; //事件的最早开始时间 - int vl[MAXV]; //事件的最迟开始时间 - for (i=0;in;i++) ve[i]=0; //先将所有事件的ve置初值为0 - for (i=0;in;i++) //从左向右求所有事件的最早开始时间 - { p=G->adjlist[i].firstarc; - while (p!=NULL) //遍历每一条边即活动 - { w=p->adjvex; - if (ve[i]+p->weight>ve[w]) //求最大者 - ve[w]=ve[i]+p->weight; - p=p->nextarc; - } - } - for (i=0;in;i++) //先将所有事件的vl值置为最大值 - vl[i]=ve[enode]; - for (i=G->n-2;i>=0;i--) //从右向左求所有事件的最迟开始时间 - { p=G->adjlist[i].firstarc; - while (p!=NULL) - { w=p->adjvex; - if (vl[w]-p->weightweight; - p=p->nextarc; - } - } - d=-1; //d存放keynode中的关键活动下标,置初置为-1 - for (i=0;in;i++) //求关键活动 - { p=G->adjlist[i].firstarc; - while (p!=NULL) - { w=p->adjvex; - if (ve[i]==vl[w]-p->weight) //(i→w)是一个关键活动 - { - d++; keynode[d].ino=i; keynode[d].eno=w; - } - p=p->nextarc; - } - } - return true; -} -void DispKeynode(AdjGraph *G) //输出图G的关键活动 -{ - int inode,enode,d,i; - KeyNode keynode[MAXV]; - if (KeyPath(G,inode,enode,keynode,d)) - { - printf("从源点%c到汇点%c的关键活动:",char(inode='A'),char(enode+'A')); - for (i=0;i<=d;i++) - printf("(%c,%c) ",char(keynode[i].ino+'A'),char(keynode[i].eno+'A')); - printf("\n"); - } - else printf("不能求关键活动\n"); -} -int main() -{ - AdjGraph *G; - int n=9,e=11; - int A[MAXV][MAXV]={ - { 0, 6, 4, 5 ,INF,INF,INF,INF,INF}, - {INF, 0, INF,INF, 1 ,INF,INF,INF,INF}, - {INF,INF, 0 ,INF, 1 ,INF,INF,INF,INF}, - {INF,INF,INF, 0 ,INF,INF,INF, 2 ,INF}, - {INF,INF,INF,INF, 0 , 9 , 7 ,INF,INF}, - {INF,INF,INF,INF,INF, 0 ,INF,INF, 2 }, - {INF,INF,INF,INF,INF,INF, 0 ,INF, 4 }, - {INF,INF,INF,INF,INF,INF,INF, 0 , 4 }, - {INF,INF,INF,INF,INF,INF,INF,INF, 0 }}; - printf("建立图的邻接表:\n"); - CreateAdj(G,A,n,e); //创建图8.45的邻接表 - printf("图G的邻接表:\n"); DispAdj(G); - DispKeynode(G); //求构成关键路径的关键活动 - DestroyAdj(G); //销毁图 - return 1; -} \ No newline at end of file diff --git a/src/chap8/graph.cpp b/src/chap8/graph.cpp deleted file mode 100644 index 6732f39e16e4ad5ccd22339bfe73597303880342..0000000000000000000000000000000000000000 --- a/src/chap8/graph.cpp +++ /dev/null @@ -1,114 +0,0 @@ -//图的基本运算算法 -#include -#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,e; //顶点数,边数 - VertexType vexs[MAXV]; //存放顶点信息 -} MatGraph; //完整的图邻接矩阵类型 - -//以下定义邻接表类型 -typedef struct ANode -{ int adjvex; //该边的邻接点编号 - struct ANode *nextarc; //指向下一条边的指针 - int weight; //该边的相关信息,如权值(用整型表示) -} ArcNode; //边节点类型 -typedef struct Vnode -{ InfoType info; //顶点其他信息 - int count; //存放顶点入度,仅仅用于拓扑排序 - ArcNode *firstarc; //指向第一条边 -} VNode; //邻接表头节点类型 -typedef struct -{ VNode adjlist[MAXV]; //邻接表头节点数组 - int n,e; //图中顶点数n和边数e -} AdjGraph; //完整的图邻接表类型 - -//------------------------------------------------------------ -//----邻接矩阵的基本运算算法---------------------------------- -//------------------------------------------------------------ -void CreateMat(MatGraph &g,int A[MAXV][MAXV],int n,int e) //创建图的邻接矩阵 -{ - int i,j; - g.n=n; g.e=e; - for (i=0;iadjlist[i].firstarc=NULL; - for (i=0;i=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=n; -} -void DispAdj(AdjGraph *G) //输出邻接表G -{ - ArcNode *p; - for (int i=0;in;i++) - { - p=G->adjlist[i].firstarc; - printf("%3d: ",i); - while (p!=NULL) - { - printf("%3d[%d]→",p->adjvex,p->weight); - p=p->nextarc; - } - printf("∧\n"); - } -} -void DestroyAdj(AdjGraph *&G) //销毁图的邻接表 -{ - ArcNode *pre,*p; - for (int i=0;in;i++) //扫描所有的单链表 - { pre=G->adjlist[i].firstarc; //p指向第i个单链表的首节点 - if (pre!=NULL) - { p=pre->nextarc; - while (p!=NULL) //释放第i个单链表的所有边节点 - { free(pre); - pre=p; p=p->nextarc; - } - free(pre); - } - } - free(G); //释放头节点数组 -} -//------------------------------------------------------------ diff --git a/src/chap8/test.txt b/src/chap8/test.txt deleted file mode 100644 index c8b6cef5159655f5af319b8439b37dc0ed62ae98..0000000000000000000000000000000000000000 --- a/src/chap8/test.txt +++ /dev/null @@ -1,6 +0,0 @@ -3 2 -0 1 -1 2 -2 -0 0 -0 2 \ No newline at end of file diff --git a/src/chap8/travsal.cpp b/src/chap8/travsal.cpp deleted file mode 100644 index 0605909e054ec05b38082bdb9a102dc8b7cf5073..0000000000000000000000000000000000000000 --- a/src/chap8/travsal.cpp +++ /dev/null @@ -1,77 +0,0 @@ -//文件名:travsal.cpp -#include "graph.cpp" //包含图的存储结构及基本运算算法 -int visited[MAXV]; //全局数组 -void DFS(AdjGraph *G,int v) //递归深度优先遍历算法 -{ - ArcNode *p; - visited[v]=1; //置已访问标记 - printf("%3d",v); //输出被访问顶点的编号 - p=G->adjlist[v].firstarc; //p指向顶点v的第一条弧的弧头结点 - while (p!=NULL) - { - if (visited[p->adjvex]==0) //若p->adjvex顶点未访问,递归访问它 - DFS(G,p->adjvex); - p=p->nextarc; //p指向顶点v的下一条弧的弧头结点 - } -} -void DFS1(AdjGraph *G,int v) //非递归深度优先遍历算法 -{ - ArcNode *p; - int St[MAXV]; - int top=-1,w,x,i; - for (i=0;in;i++) - visited[i]=0; //顶点访问标志均置成0 - printf("%3d",v); //访问顶点v - visited[v]=1; //置顶点v已访问 - top++; St[top]=v; //将顶点v进栈 - while (top>-1) //栈不空循环 - { - x=St[top]; //取栈顶顶点x作为当前顶点 - p=G->adjlist[x].firstarc; //找顶点x的第一个相邻点 - while (p!=NULL) - { - w=p->adjvex; //x的相邻点为w - if (visited[w]==0) //若顶点w没有访问 - { - printf("%3d",w); //访问顶点w - visited[w]=1; //置顶点w已访问 - top++; //将顶点w进栈 - St[top]=w; - break; //退出循环,即再处理栈顶的顶点(体现后进先出) - } - p=p->nextarc; //找顶点x的下一个相邻点 - } - if (p==NULL) top--; //若顶点x再没有相邻点,将其退栈 - } - printf("\n"); -} -void BFS(AdjGraph *G,int v) //广度优先遍历算法 -{ - ArcNode *p; - int queue[MAXV],front=0,rear=0; //定义环形队列并初始化 - int visited[MAXV]; //定义存放顶点的访问标志的数组 - int w,i; - for (i=0;in;i++) visited[i]=0; //访问标志数组初始化 - printf("%3d",v); //输出被访问顶点的编号 - visited[v]=1; //置已访问标记 - rear=(rear+1)%MAXV; - queue[rear]=v; //v进队 - while (front!=rear) //若队列不空时循环 - { - front=(front+1)%MAXV; - w=queue[front]; //出队并赋给w - p=G->adjlist[w].firstarc; //找顶点w的第一个相邻点 - while (p!=NULL) - { - if (visited[p->adjvex]==0) //若相邻点未被访问 - { - printf("%3d",p->adjvex); //访问相邻点 - visited[p->adjvex]=1; //置该顶点已被访问的标志 - rear=(rear+1)%MAXV; //该顶点进队 - queue[rear]=p->adjvex; - } - p=p->nextarc; //找下一个相邻点 - } - } - printf("\n"); -} diff --git a/src/chap9/bst.cpp b/src/chap9/bst.cpp deleted file mode 100644 index 2ecda04e5704cafbe07a46708ccbbeb79dc927bc..0000000000000000000000000000000000000000 --- a/src/chap9/bst.cpp +++ /dev/null @@ -1,165 +0,0 @@ -//文件名:bst.cpp -#include -#include -#define MaxSize 100 -typedef int KeyType; //定义关键字类型 -typedef char InfoType; -typedef struct node //记录类型 -{ - KeyType key; //关键字项 - InfoType data; //其他数据域 - struct node *lchild,*rchild; //左右孩子指针 -} BSTNode; -void DispBST(BSTNode *b); //函数说明 -bool InsertBST(BSTNode *&bt,KeyType k) //在以bt为根节点的BST中插入一个关键字为k的节点 -{ - if (bt==NULL) //原树为空, 新插入的记录为根节点 - { - bt=(BSTNode *)malloc(sizeof(BSTNode)); - bt->key=k; - bt->lchild=bt->rchild=NULL; - return true; - } - else if (k==bt->key) - return false; - else if (kkey) - return InsertBST(bt->lchild,k); //插入到bt节点的左子树中 - else - return InsertBST(bt->rchild,k); //插入到bt节点的右子树中 -} -BSTNode *CreateBST(KeyType A[],int n) -//由数组A中的关键字建立一棵二叉排序树 -{ - BSTNode *bt=NULL; //初始时bt为空树 - int i=0; - while (irchild!=NULL) //递归找节点r的最右下节点 - Delete1(p,r->rchild); - else //找到了最右下节点r(它没有右子树) - { p->key=r->key; //将节点r的值存放到节点p中(节点值替代) - p->data=r->data; - q=r; //删除节点r - r=r->lchild; //即用节点r的左孩子替代它 - free(q); //释放节点r的空间 - } -} -void Delete(BSTNode *&p) //从二叉排序树中删除p节点 -{ - BSTNode *q; - if (p->rchild==NULL) //p节点没有右子树的情况 - { - q=p;p=p->lchild;free(q); - } - else if (p->lchild==NULL) //p节点没有左子树的情况 - { - q=p;p=p->rchild;free(q); - } - else Delete1(p,p->lchild); //p节点既有左子树又有右子树的情况 -} -bool DeleteBST(BSTNode *&bt,KeyType k) -//在bt中删除关键字为k的节点 -{ - if (bt==NULL) return false; //空树删除失败 - else - { - if (kkey) - return DeleteBST(bt->lchild,k); //递归在左子树中删除关键字为k的节点 - else if (k>bt->key) - return DeleteBST(bt->rchild,k); //递归在右子树中删除关键字为k的节点 - else //k=bt->key的情况 - { - Delete(bt); //调用Delete(bt)函数删除bt节点 - return true; - } - } -} -void SearchBST1(BSTNode *bt,KeyType k,KeyType path[],int i) -//以非递归方式输出从根节点到查找到的节点的路径 -{ - int j; - if (bt==NULL) - return; - else if (k==bt->key) //找到了节点 - { - path[i+1]=bt->key; //输出其路径 - for (j=0;j<=i+1;j++) - printf("%3d",path[j]); - printf("\n"); - } - else - { - path[i+1]=bt->key; - if (kkey) - SearchBST1(bt->lchild,k,path,i+1); //在左子树中递归查找 - else - SearchBST1(bt->rchild,k,path,i+1); //在右子树中递归查找 - } -} -int SearchBST2(BSTNode *bt,KeyType k) -//以递归方式输出从根节点到查找到的节点的路径 -{ - if (bt==NULL) - return 0; - else if (k==bt->key) - { - printf("%3d",bt->key); - return 1; - } - else if (kkey) - SearchBST2(bt->lchild,k); //在左子树中递归查找 - else - SearchBST2(bt->rchild,k); //在右子树中递归查找 - printf("%3d",bt->key); -} - -void DispBST(BSTNode *bt) -//以括号表示法输出二叉排序树bt -{ - if (bt!=NULL) - { - printf("%d",bt->key); - if (bt->lchild!=NULL || bt->rchild!=NULL) - { - printf("("); - DispBST(bt->lchild); - if (bt->rchild!=NULL) printf(","); - DispBST(bt->rchild); - printf(")"); - } - } -} -KeyType predt=-32767; //predt为全局变量,保存当前节点中序前驱的值,初值为-∞ -bool JudgeBST(BSTNode *bt) //判断bt是否为BST -{ - bool b1,b2; - if (bt==NULL) - return true; - else - { - b1=JudgeBST(bt->lchild); - if (b1==false || predt>=bt->key) - return false; - predt=bt->key; - b2=JudgeBST(bt->rchild); - return b2; - } -} -void DestroyBST(BSTNode *bt) //销毁一颗BST -{ - if (bt!=NULL) - { - DestroyBST(bt->lchild); - DestroyBST(bt->rchild); - free(bt); - } -} diff --git a/src/chap9/exp9-1.cpp b/src/chap9/exp9-1.cpp deleted file mode 100644 index ffbdfc9e4de2129bffb76a041897bf43c9514037..0000000000000000000000000000000000000000 --- a/src/chap9/exp9-1.cpp +++ /dev/null @@ -1,33 +0,0 @@ -//文件名:exp9-1.cpp -#include "seqlist.cpp" //包含顺序表基本运算算法 -int SeqSearch(RecType R[],int n,KeyType k) //顺序查找算法 -{ - int i=0; - while (i=n) - return 0; - else - { - printf("%d",R[i].key); - return i+1; - } -} -int main() -{ - RecType R[MAXL]; - int n=10,i; - KeyType k=5; - int a[]={3,6,2,10,1,8,5,7,4,9}; - CreateList(R,a,n); //建立顺序表 - printf("关键字序列:"); DispList(R,n); - printf("查找%d所比较的关键字:\n\t",k); - if ((i=SeqSearch(R,n,k))!=0) - printf("\n元素%d的位置是%d\n",k,i); - else - printf("\n元素%d不在表中\n",k); - return 1; -} diff --git a/src/chap9/exp9-10.cpp b/src/chap9/exp9-10.cpp deleted file mode 100644 index 226933de55466b34c8e6d60a33b6be9b24145ed3..0000000000000000000000000000000000000000 --- a/src/chap9/exp9-10.cpp +++ /dev/null @@ -1,47 +0,0 @@ -//文件名:exp9-10.cpp -#include "bst.cpp" //二叉排序树基本运算算法 - -void Succlength(BSTNode *bt,int &sumlen,int &m,int level) //求查找成功总的比较次数sumlen和情况数m -{ - if (bt==NULL) return; //空树直接返回 - m++; - sumlen+=level; - Succlength(bt->lchild,sumlen,m,level+1); - Succlength(bt->rchild,sumlen,m,level+1); -} -double ASLsucc(BSTNode *bt) //求查找成功情况下的平均查找长度 -{ - int sumlen=0,m=0; - Succlength(bt,sumlen,m,1); - return sumlen*1.0/m; -} -void Unsucclength(BSTNode *bt,int &sumlen,int &m,int level) //求查找失败总的比较次数sumlen和情况数m -{ - if (bt==NULL) //空指针对应外部节点 - { - m++; - sumlen+=level-1; - return; - } - Unsucclength(bt->lchild,sumlen,m,level+1); - Unsucclength(bt->rchild,sumlen,m,level+1); -} -double ASLunsucc(BSTNode *bt) //求查找失败情况下的平均查找长度 -{ - int sumlen=0,m=0; - Unsucclength(bt,sumlen,m,1); - return sumlen*1.0/m; -} -int main() -{ - BSTNode *bt; - int n=12; - KeyType a[]={25,18,46,2,53,39,32,4,74,67,60,11}; - printf("(1)创建BST\n"); //创建《教程》例9.3的一棵BST - bt=CreateBST(a,n); - printf("(2)BST:"); DispBST(bt); printf("\n"); - printf("(3)ASLsucc=%g\n",ASLsucc(bt)); - printf("(4)ASLunsucc=%g\n",ASLunsucc(bt)); - DestroyBST(bt); - return 1; -} diff --git a/src/chap9/exp9-11.cpp b/src/chap9/exp9-11.cpp deleted file mode 100644 index e0acacc69eb206ba58931718c16f0e13d6df4471..0000000000000000000000000000000000000000 --- a/src/chap9/exp9-11.cpp +++ /dev/null @@ -1,39 +0,0 @@ -//文件名:exp9-11.cpp -#include "bst.cpp" //二叉排序树基本运算算法 -bool Findseq(BSTNode *bt,int a[],int n) //判断a是否为bt中的一个合法查找序列 -{ BSTNode *p=bt; - int i=0; - while (ikey) //a查找完毕,返回true - return true; - if (p->key!=a[i]) //若不等,表示a不是查找序列 - return false; //返回false - i++; //查找序列指向下一个关键字 - if (a[i]key) p=p->lchild; //在左子树中查找 - else if (a[i]>p->key) p=p->rchild; //在右子树中查找 - } - return false; -} - - -int main() -{ - BSTNode *bt; - KeyType keys[]={5,2,3,4,1,6,8,7,9}; - int m=9; - printf("(1)构造二叉排序树bt\n"); - bt=CreateBST(keys,m); //创建二叉排序树 - printf("(2)输出BST:");DispBST(bt);printf("\n"); - int n=4; - KeyType a[]={5,6,8,9}; - printf("(3)关键字序列:"); - for (int i=0;ikey && ykey) - return LCA(bt->lchild,x,y); - else if (x>bt->key && y>bt->key) - return LCA(bt->rchild,x,y); - else - return bt; -} - - -int main() -{ - BSTNode *bt,*p; - KeyType x=1,y=4; - KeyType a[]={5,2,1,6,7,4,8,3,9},n=9; - printf("(1)构造二叉排序树bt\n"); - bt=CreateBST(a,n); //创建一棵二叉排序树 - printf("(2)输出BST:");DispBST(bt);printf("\n"); - printf("(3)查找%d和%d节点的LCA\n",x,y); - if (p=LCA(bt,x,y)) - printf(" LCA是:%d\n",p->key); - else - printf(" 指定的关键字不存在\n"); - printf("(4)销毁bt"); DestroyBST(bt); printf("\n"); - return 1; -} diff --git a/src/chap9/exp9-13.cpp b/src/chap9/exp9-13.cpp deleted file mode 100644 index abb288e8312ce07b79326a194d4ef6e642bde64b..0000000000000000000000000000000000000000 --- a/src/chap9/exp9-13.cpp +++ /dev/null @@ -1,85 +0,0 @@ -//文件名:exp9-13.cpp -#include -#define MAXL 100 //定义表中最多记录个数 -typedef int KeyType; //定义关键字类型为int -typedef char InfoType; -typedef struct -{ KeyType key; //关键字项 - InfoType data; //其他数据项,类型为InfoType -} RecType; //查找元素的类型 -int FindElem(RecType R[],int n,KeyType k) //(1)小题对应的算法 -{ - int i=4,j; - if (kR[4*n].key) - return 0; //不在范围内返回0 - while(i<=4*n) - { - if (R[i].key==k) - return i; //查找成功返回 - else if (R[i].keyR[4*n].key) - return 0; //不在范围内返回0 - low=4; high=4*n; - while (low<=high) //二分查找 - { - mid=(low+high)/2; - if (kR[mid].key) - low=mid+4; - else - return mid; - } //查找失败时刚好有k>R[high].key并且k<=R[high+4].key - i=high+4; - j=high+1; - while (j=1) - printf(" 结果:R[%d]=%d\n",i,k); - else - printf(" 未找到%d\n",k); - k=20; - printf("用算法(2)查找关键字%d:\n",k); - i=ImproveFindElem(R,n,k); - if (i>=1) - printf(" 结果:R[%d]=%d\n",i,k); - else - printf(" 未找到%d\n",k); - return 1; -} diff --git a/src/chap9/exp9-14.cpp b/src/chap9/exp9-14.cpp deleted file mode 100644 index 1664264548e23768d941e0aa8976ed74118e4552..0000000000000000000000000000000000000000 --- a/src/chap9/exp9-14.cpp +++ /dev/null @@ -1,93 +0,0 @@ -//文件名:exp9-14.cpp -#include -#include -#include -typedef struct node -{ - long index; //当前节点对应的记录下标 - int level; //当前节点的层次 - struct node *lchild,*rchild; //左右孩子指针 -} DecNode; //判定树节点类型 -void CreateDectree1(DecNode *&b,long low,long high,int h) //由CreateDectree调用以建立判定树 -{ - int mid; - if (low<=high) - { - mid=(low+high)/2; - b=(DecNode *)malloc(sizeof(DecNode)); - b->index=mid; - b->level=h; - CreateDectree1(b->lchild,low,mid-1,h+1); - CreateDectree1(b->rchild,mid+1,high,h+1); - } - else - b=NULL; -} -void CreateDectree(DecNode *&b,long n) //建立判定树b -{ - CreateDectree1(b,0,n-1,1); -} - -void DispDectree(DecNode *b) //以括号表示法输出二叉树b -{ - if (b!=NULL) - { - printf("%d[%d]",b->index,b->level); - if (b->lchild!=NULL || b->rchild!=NULL) - { - printf("("); - DispDectree(b->lchild); - if (b->rchild!=NULL) printf(","); - DispDectree(b->rchild); - printf(")"); - } - } -} -void DestroyDectree(DecNode *&b) //销毁判定树b -{ - if (b!=NULL) - { - DestroyDectree(b->lchild); - DestroyDectree(b->rchild); - free(b); - } -} -int Sum(DecNode *b) //求判定树b中比较的总次数 -{ - if (b!=NULL) - { - if (b->lchild==NULL && b->rchild==NULL) - return b->level; - else - return Sum(b->lchild)+Sum(b->rchild)+b->level; - } - else - return 0; -} -double ASLsucc(DecNode *b,long n) //求成功情况下的平均查找长度 -{ - return 1.0*Sum(b)/n; -} -int main() -{ - DecNode *b; - long n=11; - double d,asl1,asl2; - CreateDectree(b,n); - printf("R[0..%d]判定树:\n\t",n-1); - DispDectree(b); - printf("\n\tASL=%g\n",ASLsucc(b,n)); - DestroyDectree(b); - printf("成功平均查找长度分析:\n"); - printf("\tn\t\tASL1\t\tASL2\t\t差值\n"); - for (n=10;n<=1000000;n*=10) - { - CreateDectree(b,n); - asl1=ASLsucc(b,n); - asl2=log(n+1)-1; - d=asl1-asl2; - printf(" %10d\t\t%g\t\t%g\t\t%g\n",n,asl1,asl2,d); - DestroyDectree(b); - } - return 1; -} diff --git a/src/chap9/exp9-15.cpp b/src/chap9/exp9-15.cpp deleted file mode 100644 index 6bbfef4c58b58fc86b3ec688f79ac896d7490a5b..0000000000000000000000000000000000000000 --- a/src/chap9/exp9-15.cpp +++ /dev/null @@ -1,163 +0,0 @@ -//文件名:exp9-15.cpp -//AVL算法 -#include -#include -#define max(x,y) ((x)>(y)?(x):(y)) -typedef struct node -{ - int key; //关键字项 - int ht; //当前结点的子树高度 - struct node *lchild,*rchild; //左右孩子指针 -} AVLNode; //AVL树结点类型 -void DestroyAVL(AVLNode* r) //释放所有的结点空间 -{ - if (r!=NULL) - { - DestroyAVL(r->lchild); //递归释放左子树 - DestroyAVL(r->rchild); //递归释放右子树 - delete r; //释放根结点 - } -} -int getht(AVLNode* p) //返回结点p的子树高度 -{ if (p==NULL) - return 0; - return p->ht; - } -AVLNode* right_rotate(AVLNode* A) //以结点A为根做右旋转 -{ AVLNode* B=A->lchild; - A->lchild=B->rchild; - B->rchild=A; - A->ht=max(getht(A->rchild),getht(A->lchild))+1; - B->ht=max(getht(B->rchild),getht(B->lchild))+1; - return B; -} -AVLNode* left_rotate(AVLNode* A) //以结点A为根做左旋转 -{ AVLNode* B=A->rchild; - A->rchild=B->lchild; - B->lchild=A; - A->ht=max(getht(A->rchild),getht(A->lchild))+1; - B->ht=max(getht(B->rchild),getht(B->lchild))+1; - return B; -} -AVLNode* LL(AVLNode* A) //LL型调整 -{ - return right_rotate(A); -} -AVLNode* RR(AVLNode* A) //RR型调整 -{ - return left_rotate(A); -} -AVLNode* LR(AVLNode* A) //LR型调整 -{ AVLNode* B=A->lchild; - A->lchild=left_rotate(B); //结点b左旋 - return right_rotate(A); //结点a右旋 -} -AVLNode* RL(AVLNode* A) //RL型调整 -{ AVLNode* B=A->rchild; - A->rchild=right_rotate(B); //结点b右旋 - return left_rotate(A); //结点a左旋 -} -AVLNode* InsertAVL(AVLNode* r,int k) //在AVL树r中插入关键字k -{ if (r==NULL) //空树时创建根结点 - { AVLNode* p=(AVLNode*)malloc(sizeof(AVLNode)); - p->key=k; p->ht=1; - p->lchild=p->rchild=NULL; - return p; - } - else if (k==r->key) //找到重复关键字时返回 - return r; - else if (kkey) //kkey的情况 - { r->lchild=InsertAVL(r->lchild,k); //将k插入到r的左子树中 - if (getht(r->lchild)-getht(r->rchild)>=2) //找到失衡结点r - { if (klchild->key) //k插入在r的左孩子的左子树中 - r=LL(r); //采用LL型调整 - else //k插入在r的左孩子的右子树中 - r=LR(r); //采用LR型调整 - } - } - else //k>r->key的情况 - { r->rchild=InsertAVL(r->rchild,k); //将k插入到r的右子树中 - if (getht(r->rchild)-getht(r->lchild)>=2) //找到失衡结点r - { if (k>r->rchild->key) //k插入在r的右孩子的右子树中 - r=RR(r); //采用RR型调整 - else //k插入在r的右孩子的左子树中 - r=RL(r); //采用RL型调整 - } - } - r->ht=max(getht(r->lchild),getht(r->rchild))+1; //更新结点r的高度 - return r; -} -AVLNode* Deletemin(AVLNode* r,int &mine) //删除AVL树r中的最小结点 -{ - AVLNode *f=NULL,*p=r; - while(p->lchild!=NULL) //找根结点r的最左下结点p - { - f=p; - p=p->lchild; - } - if(f==NULL) //结点p是根结点 - r=r->rchild; - else //结点p不是根结点 - f->lchild=p->rchild; - mine=p->key; - free(p); - return r; -} -AVLNode* Deletemax(AVLNode* r,int &maxe) //删除AVL树r中的最大结点 -{ - AVLNode *f=NULL,*p=r; - while(p->rchild!=NULL) //找根结点r的最右下结点p - { - f=p; - p=p->rchild; - } - if(f==NULL) //结点p是根结点 - r=r->lchild; - else //结点p不是根结点 - f->rchild=p->lchild; - maxe=p->key; - free(p); - return r; -} -void inorder(AVLNode* r) //中序遍历所有结点值 -{ - if (r!=NULL) - { inorder(r->lchild); - printf("%d ",r->key); - inorder(r->rchild); - } -} -void solve(int a[],int n) -{ - AVLNode *b=NULL; - int mine,maxe; - for(int i=0;i -#define MaxSize 1005 -int ht[MaxSize]; //哈希表 -int a[MaxSize]; -int n; -//-----哈希表操作算法开始------- -void insertht(int x,int no) //将插入到哈希表ht中 -{ - ht[x]=no; -} -void deleteht(int x) //从哈希表ht中删除 -{ - ht[x]=-1; -} -//-----哈希表操作算法结束------- -void dispa() //输出序列 -{ - printf("a: "); - for(int i=0;i插入到哈希表ht中 - n++; -} -void deletea(int x) //删除x -{ - int no=ht[x]; //求整数x的序号no - deleteht(x); //从哈希表ht中删除x - a[no]=a[n-1]; //将z中末尾元素移到no位置 - deleteht(a[n-1]); //从哈希表ht中删除a[n-1] - insertht(a[n-1],no); //将重新插入到哈希表ht中 - n--; -} -int search(int no) //返回序号为no的整数 -{ - return a[no-1]; -} -int main() -{ - int op[]={1,5,1,4,1,3,2,4,1,7,1,6,3,2,2,5,3,1,3,2}; - int m=sizeof(op)/sizeof(op[0]); //操作次数为m/2 - for(int i=0;ik) //继续在R[low..mid-1]中查找 - high=mid-1; - else - low=mid+1; //继续在R[mid+1..high]中查找 - } - return 0; -} -int main() -{ - RecType R[MAXL]; - KeyType k=9; - int a[]={1,2,3,4,5,6,7,8,9,10},i,n=10; - CreateList(R,a,n); //建立顺序表 - printf("关键字序列:"); DispList(R,n); - printf("查找%d的比较过程如下:\n",k); - if ((i=BinSearch(R,n,k))!=0) - printf("元素%d的位置是%d\n",k,i); - else - printf("元素%d不在表中\n",k); - return 1; -} diff --git a/src/chap9/exp9-3.cpp b/src/chap9/exp9-3.cpp deleted file mode 100644 index bb3faa06ecb7fc5cf966406c442e9d66fa1ed248..0000000000000000000000000000000000000000 --- a/src/chap9/exp9-3.cpp +++ /dev/null @@ -1,71 +0,0 @@ -//文件名:exp9-3.cpp -#include "seqlist.cpp" //包含顺序表基本运算算法 -#define MAXI 20 //定义索引表的最大长度 -typedef struct -{ - KeyType key; //KeyType为关键字的类型 - int link; //指向分块的起始下标 -} IdxType; //索引表元素类型 - -int IdxSearch(IdxType I[],int b,RecType R[],int n,KeyType k) //分块查找 -{ - int s=(n+b-1)/b; //s为每块的元素个数,应为n/b取上界 - int count1=0,count2=0; - int low=0,high=b-1,mid,i; - printf("(1)在索引表中折半查找\n"); - while (low<=high) //在索引表中进行折半查找,找到的位置为high+1 - { - mid=(low+high)/2; - printf(" 第%d次比较:在[%d,%d]中比较元素R[%d]:%d\n",count1+1,low,high,mid,I[mid].key); - if (I[mid].key>=k) - high=mid-1; - else - low=mid+1; - count1++; //count1累计在索引表中的比较次数 - } - printf("比较%d次,在第%d块中查找元素%d\n",count1,low,k); - //应在索引表的high+1块中,再在主数据表中进行顺序查找 - i=I[high+1].link; //找到对应的块 - printf("(2)在对应块中顺序查找:\n "); - while (i<=I[high+1].link+s-1) - { printf("%d ",R[i].key); - count2++; //count2累计在顺序表对应块中的比较次数 - if (R[i].key==k) break; - i++; - } - printf("比较%d次,在顺序表中查找元素%d\n",count2,k); - if (i<=I[high+1].link+s-1) - return i+1; //查找成功,返回该元素的逻辑序号 - else - return 0; //查找失败,返回0 -} - -int main() -{ - RecType R[MAXL]; - IdxType I[MAXI]; - int n=25, i; - int a[]={8,14,6,9,10,22,34,18,19,31,40,38,54,66,46,71,78,68,80,85,100,94,88,96,87}; - CreateList(R,a,n); //建立顺序表 - I[0].key=14;I[0].link=0; //建立索引表 - I[1].key=34;I[1].link=5; - I[2].key=66;I[2].link=10; - I[3].key=85;I[3].link=15; - I[4].key=100;I[4].link=20; - printf("关键字序列:"); - for (i=0;i -#define MaxSize 100 //定义最大哈希表长度 -#define NULLKEY -1 //定义空关键字值 -typedef int KeyType; //关键字类型 -typedef char InfoType; //其他数据类型 -typedef struct -{ - KeyType key; //关键字域 - InfoType data; //其他数据域 - int count; //探测次数域 -} HashTable; //哈希表元素类型 -void InsertHT(HashTable ha[],int &n,int m,int p,KeyType k) //将关键字k插入到哈希表中 -{ - int cnt,adr; - adr=k % p; //计算哈希函数值 - if (ha[adr].key==NULLKEY) //k可以直接放在哈希表中 - { - ha[adr].key=k; - ha[adr].count=1; - } - else //发生冲突时采用线性探测法解决冲突 - { - cnt=1; //cnt记录k发生冲突的次数 - do - { - adr=(adr+1) % m; //线性探测 - cnt++; - } while (ha[adr].key!=NULLKEY); - ha[adr].key=k; //在adr处放置k - ha[adr].count=cnt; //设置探测次数 - } - n++; //总关键字个数增1 -} -void CreateHT(HashTable ha[],int &n,int m,int p,KeyType keys[],int total) //由keys中total个关键字创建哈希表 -{ - for (int i=0;i -#define MAXL 100 //定义表中最多记录个数 -typedef int KeyType; //定义关键字类型为int -typedef char InfoType; -typedef struct -{ KeyType key; //关键字项 - InfoType data; //其他数据项,类型为InfoType -} RecType; //查找元素的类型 -int lowerbound(RecType R[],int n,KeyType k) //求关键字为k记录的下界 -{ - int low=0, high=n, mid; - while (lowR[mid].key) - low=mid+1; - else - high=mid; - } - return low; -} -int upperbound(RecType R[],int n,KeyType k) //求关键字为k记录的上界 -{ - int low=0, high=n, mid; - while (low=R[mid].key) - low=mid+1; - else - high=mid; - } - return low; -} -void SearchRange(RecType R[],int n,KeyType k) //输出关键字为k的记录的区间 -{ - int lower=lowerbound(R,n,k); - int upper=upperbound(R,n,k); - if (lower==n || lower>=upper) - printf("不存在\n"); - else - printf("区间[%d,%d]\n",lower,upper-1); -} -int main() -{ - RecType R[MAXL]; - KeyType k=9; - int a[]={1,2,3,3,3,3,3,8,9,10},i,n=10; - for (i=0;i -#define MAXL 100 //定义表中最多记录个数 -typedef int KeyType; //定义关键字类型为int -typedef char InfoType; -typedef struct -{ KeyType key; //关键字项 - InfoType data; //其他数据项,类型为InfoType -} RecType; //查找元素的类型 - -KeyType M_Search(RecType A[],RecType B[],int n) //求A、B的中位数 -{ - int start1,end1,mid1,start2,end2,mid2; - start1=0; end1=n-1; - start2=0; end2=n-1; - while(start1!=end1 || start2!=end2) - { - mid1=(start1+end1)/2; - mid2 = (start2+end2)/2; - if(A[mid1].key==B[mid2].key) - return A[mid1].key; - if(A[mid1].keykey=a[mid]; - bt->lchild=CreateBST1(a,start,mid-1); - bt->rchild=CreateBST1(a,mid+1,end); - return bt; -} -int main() -{ - BSTNode *bt; - int n=9; - KeyType a[]={1,2,3,4,5,6,7,8,9}; - bt=CreateBST1(a,0,n-1); - printf("BST:"); DispBST(bt); printf("\n"); - DestroyBST(bt); - return 1; -} diff --git a/src/chap9/exp9-9.cpp b/src/chap9/exp9-9.cpp deleted file mode 100644 index 17510100c14fa6bd2dcc6d3bf7a9e792fc194f16..0000000000000000000000000000000000000000 --- a/src/chap9/exp9-9.cpp +++ /dev/null @@ -1,62 +0,0 @@ -//文件名:exp9-9.cpp -#include -#include -#include -#define MAXWORD 100 -typedef struct tnode -{ - char ch; //字符 - int count; //出现次数 - struct tnode *lchild,*rchild; -} BSTNode; -void CreateBST(BSTNode *&bt,char c) //采用递归方式构造一棵二叉排序树bt -{ - if (bt==NULL) //bt为NULL,则建立一个新节点 - { - bt=(BSTNode *)malloc(sizeof(BSTNode)); - bt->ch=c; - bt->count=1; - bt->lchild=bt->rchild=NULL; - } - else if (c==bt->ch) - bt->count++; - else if (cch) - CreateBST(bt->lchild,c); - else - CreateBST(bt->rchild,c); -} -void InOrder(BSTNode *bt) //中序遍历二叉排序树bt -{ - if (bt!=NULL) - { - InOrder(bt->lchild); //中序遍历左子树 - printf(" %c(%d)\n",bt->ch,bt->count);//访问根节点 - InOrder(bt->rchild); //中序遍历右子树 - } -} -void DestroyBST(BSTNode *bt) //销毁二叉排序树bt -{ - if (bt!=NULL) - { - DestroyBST(bt->lchild); - DestroyBST(bt->rchild); - free(bt); - } -} -int main() -{ - BSTNode *bt=NULL; - int i=0; - char str[MAXWORD]; - printf("输入字符串:"); - gets(str); - while (str[i]!='\0') - { - CreateBST(bt,str[i]); - i++; - } - printf("字符及出现次数:\n"); - InOrder(bt); - DestroyBST(bt); - return 1; -} diff --git a/src/chap9/seqlist.cpp b/src/chap9/seqlist.cpp deleted file mode 100644 index bebb9e7d734ee8c22bd49d52a529ee9cee7736ab..0000000000000000000000000000000000000000 --- a/src/chap9/seqlist.cpp +++ /dev/null @@ -1,23 +0,0 @@ -//顺序表基本运算算法 -#include -#include -#define MAXL 100 //最大长度 -typedef int KeyType; //定义关键字类型为int -typedef char InfoType; - -typedef struct -{ KeyType key; //关键字项 - InfoType data; //其他数据项,类型为InfoType -} RecType; //查找元素的类型 - -void CreateList(RecType R[],KeyType keys[],int n) //创建顺序表 -{ - for (int i=0;i -#include -#define N 5 -int grid[N][N]; -int dx[]={-2,-1,1,2,2,1,-1,-2}; //x方向的偏移量 -int dy[]={1,2,2,1,-1,-2,-2,-1}; //y方向的偏移量 -int cnt=0; -void DFS(int i,int j,int step) //从(i,j)格子开始游历 -{ - int ni,nj; - if(step>=N*N) //找到一条路径 - { cnt++; - if(cnt<=3) - { printf("第%d个解\n",cnt); - for(int i=0;i=N || nj>=N) - continue; //如果是边界则不可走 - if(grid[ni][nj]==0) //如果这个方位可走 - { - grid[ni][nj]=step+1; //从(i,j)走到(ni,nj) - DFS(ni,nj,step+1); //递归调用,继续走下去 - grid[ni][nj]=0; //从(ni,nj)回退 - } - } -} -int main() -{ - int sx, sy; - memset(grid,0,sizeof(grid)); //初始时grid所有元素为0 - printf("输入起始点:"); - scanf("%d %d", &sx, &sy); - if(sx<0 || sx>=N || sy<0 || sy>=N) - { - printf(" 起始位置错误!\n"); - return 0; - } - printf("\n从(%d,%d)出发的游历\n",sx,sy); - grid[sx][sy]=1; //(sx,sy)格子是第1步 - DFS(sx, sy,1); - printf(".\n\.\n"); - printf("共%d个解\n",cnt); - return 1; -} - diff --git a/src/chap_app/app2.cpp b/src/chap_app/app2.cpp deleted file mode 100644 index d84904d4279e7ebf6845c1cc0ce48a657b6a4b3b..0000000000000000000000000000000000000000 --- a/src/chap_app/app2.cpp +++ /dev/null @@ -1,61 +0,0 @@ -#include -#include -#define N 8 -int grid[N][N]; -int dx[]={-2,-1,1,2,2,1,-1,-2}; //x方向的偏移量 -int dy[]={1,2,2,1,-1,-2,-2,-1}; //y方向的偏移量 -bool DFS(int i,int j,int x,int y,int step) //搜索(i,j)->(x,y)的路径 -{ - int ni,nj; - if(i==x && j==y) //找到一条路径 - return true; - for(int di=0;di<8;di++) - { - ni=i+dx[di]; //di方位的相邻格子是(ni,nj) - nj=j+dy[di]; - if(ni<0 || nj<0 || ni>=N || nj>=N) - continue; //如果是边界则不可走 - if(grid[ni][nj]==0) //如果这个方位可走 - { - grid[ni][nj]=step+1; //从(i,j)走到(ni,nj) - if (DFS(ni,nj,x,y,step+1)) //递归调用 - return true; - grid[ni][nj]=0; //从(ni,nj)回退 - } - } - return false; -} -int main() -{ - int sx,sy,tx,ty; - memset(grid,0,sizeof(grid)); //初始时grid所有元素为0 - printf("输入起始点:"); - scanf("%d %d", &sx, &sy); - if(sx<0 || sx>=N || sy<0 || sy>=N) - { - printf(" 起始位置错误!\n"); - return 0; - } - printf("输入终止点:"); - scanf("%d %d", &tx, &ty); - if(tx<0 || tx>=N || ty<0 || ty>=N) - { - printf(" 终止位置错误!\n"); - return 0; - } - printf("\n求(%d,%d)->(%d,%d)的路径\n",sx,sy,tx,ty); - grid[sx][sy]=1; //(sx,sy)格子是第1步 - if (DFS(sx,sy,tx,ty,1)) - { printf("找到一条路径如下\n"); - for(int i=0;i -#include -#define N 8 -int grid[N][N]; -int dx[8]={-2,-1,1,2,2,1,-1,-2}; //x方向的偏移量 -int dy[8]={1,2,2,1,-1,-2,-2,-1}; //y方向的偏移量 -bool DFS(int i,int j,int step) //从(i,j)格子开始游历 -{ - if(step>=N*N) return true; - int exists[8]={0}; //记录每个方位出口的出路数 - int nexti[8]={0}; //记录每个方位出口的格子 - int nextj[8]={0}; - int ni,nj; - int count, mindi; - for(int di=0;di<8;di++) //初始化每个方位出口的出路数为0 - exists[di]=0; - int total=0; //(i,j)格子的出口数为total,初始为0 - for(int di=0;di<8;di++) //试探(i,j)格子的8个方位 - { - ni=i+dx[di]; //di方位的相邻格子是(ni,nj) - nj=j+dy[di]; - if(ni<0 || nj<0 || ni>=N || nj>=N) - continue; //如果是边界则不可走 - if(grid[ni][nj]==0) //如果这个方位可走,记录下来 - { nexti[total]=ni; //(ni,nj)是(i,j)的第total个相邻可走格子 - nextj[total]=nj; - total++; //(i,j)格子的出口数增1 - } - } - if(total==0) //如果出口数为0则返回 - return false; - else if(total==1) //如果只有一个出口,mindi表示该出口 - mindi=0; - else - { - for(int c=0;c=N || nj>=N) - continue; - if(grid[ni][nj]==0) //(ni,nj)格子是可走的 - exists[c]++; - } - int tmp=exists[0]; - mindi=0; - for(int c=1;c=N || sy<0 || sy>=N) - { - printf(" 起始位置错误!\n"); - return 0; - } - grid[sx][sy]=1; //(sx,sy)格子是第1步 - if(DFS(sx, sy,1)) - { - printf("从(%d,%d)出发的游历完成,游历如下:\n",sx,sy); - for(int i=0;i