一、交换两个变量的值,不使用第三个变量
1、算术运算
a = a + b; b = a - b; a = a - b;
注:缺点只能用于数字型的,不能用于字符串之类的
2、位运算
a = a ^ b; b = a ^ b; a = a ^ b;
注:运用异或运算的特性,一个数与任意一个数连续异或两次,值不变
二、实现atoi和itoa
#include <stdio.h> #include <ctype.h> int atoi(char s[]) { int i, n, sign; for (i=0; isspace(s[i]); i++) { // do nothing } sign = (s[0]=='-')? -1 : 1; for (n=0; isdigit(s[i]); i++) { n = 10*n + (s[i]-'0'); } return n*sign; } void reverse(char s[]) { char tmp; int i, j; for (i=0,j=strlen(s)-1; i<j; i++,j--) { tmp = s[i]; s[i] = s[j]; s[j] = tmp; } } void itoa(int n, char s[]) { int sign = 0; if (n < 0) { sign = 1; } int i = 0; do { s[i++] = n%10 + '0'; n = n / 10; } while ((n/=10) > 0); if (sign == 1) { s[i] = '-'; } reverse(s); } int main(int argc, char **argv) { char s[] = "23"; int rs = atoi(s); printf("rs: %d\n", rs); rs = 11; itoa(12, s); printf("s: %s\n", s); }
三、链表翻转
#include <time.h> #include <stdio.h> // 单向链表结构 struct list { int num; struct list *next; }; // 生成一个链表 struct list *init(int count) { srand((unsigned)time(NULL)); struct list *pHead; pHead = malloc(sizeof(struct list)); pHead->num = rand()/100; struct list *pList = pHead; while(--count) { pList->next = malloc(sizeof(struct list)); pList->num = rand()%100; pList = pList->next; } pList->next = NULL; return pHead; } // 删除链表 void release(struct list *pHead) { struct list *pList = pHead; struct list *pNext; while (pList != NULL) { pNext = pList->next; free(pList); pList = pNext; } } // 打印链表 void show(struct list *pHead) { struct list *pList = pHead; while (pList != NULL) { printf("%d->", pList->num); pList = pList->next; } printf("\n"); } // 用两个指针配合使节点翻转,再用一个指针指下剩下链表 struct list *reverse1(struct list *pHead) { struct list *p, *q, *r; p = pHead; q = pHead->next; pHead->next = NULL; while (q != NULL) { r = q->next; q->next = p; p = q; q = r; } return p; } // 第二个到N结点依次插入第一个结点后面,然后再把第一个结点放到链表尾 struct list *reverse2(struct list *pHead) { if (pHead->next == NULL) return pHead; struct list *pList = pHead->next; struct list *pNext; while (pList->next != NULL) { pNext = pList->next; pList->next = pNext->next; pNext->next = pHead->next; pHead->next = pNext; } pList->next = pHead; pHead = pList->next->next; pList->next->next = NULL; return pHead; } // 测试 int main(int argc, char **argv) { struct list *pHead = init(10); show(pHead); pHead = reverse1(pHead); show(pHead); pHead = reverse2(pHead); show(pHead); release(pHead); return 0; }
四、排序
#include <stdio.h>void insertSort(int a[], int length) { int i, tmp, j; for (i=1; i<length; i++) { if (a[i] < a[i-1]) { tmp = a[i]; for (j=i-1; j>=0&&a[j]>tmp; j--) { a[j+1] = a[j]; a[j] = tmp; } } } } void shellSort(int a[], int length) { int i, j, tmp; int inc = length / 2; while (inc > 0) { for (i=inc; i<length; i++) { tmp = a[i]; for (j=i-inc; j>=0&&a[j]>tmp; j=j-inc) { a[j+inc] = a[j]; } a[j+inc] = tmp; } inc /= 2; } } void bubbleSort(int a[], int length) { int i, j, tmp; for (i=0; i<length-1; i++) { for (j=0; j<length-1-i; j++) { if (a[j]>a[j+1]) { tmp = a[j]; a[j] = a[j+1]; a[j+1] = tmp; } } } } void quickSort(int a[], int low, int hight) { if (low >= hight) return; int first = low; int last = hight; int key = a[first]; while (first < last) { while (first<last && a[last]>key) last--; a[first] = a[last]; while (first<last && a[first]<key) first++; a[last] = a[first]; } a[first] = key; quickSort(a, low, first-1); quickSort(a, last+1, hight); } void selectSort(int a[], int length) { int i, j, tmp, minIdx; for (i=0; i<length; i++) { minIdx = i; for (j=i+1; j<length; j++) { if (a[i] > a[j]) minIdx = j; } if (i != minIdx) { tmp = a[i]; a[i] = a[minIdx]; a[minIdx] = tmp; } } } void headJust(int a[], int i, int length) { int child, tmp; for ( ; i*2+1<length; i=child) { child = i*2+1; if (child<length && a[child+1]>a[child]) child++; if (a[i]<a[child]) { tmp = a[i]; a[i] = a[child]; a[child] = tmp; } else { break; } } } void headSort(int a[], int length) { int i, tmp; for (i=length/2-1; i>=0; i--) { headJust(a, i, length); } for (i=length-1; i>0; i--) { tmp = a[i]; a[i] =a[0]; a[0] = tmp; headJust(a, 0, i); } } void merge(int source[], int target[], int start, int mid, int end) { int i, j, k; for (i=mid+1,j=start; start<=mid&&i<=end; j++) { if (source[start]<source[i]) { target[j] = source[start++]; } else { target[j] = source[i++]; } } if (start<=mid) { for (k=0; k<=mid-start; k++) target[j+k] = source[start+k]; } if (i<=end) { for (k=0; k<=end-i; k++) target[j+k] = source[i+k]; } } void mergeSort(int source[], int target[], int start, int end) { int mid; int tmp[100]; if (start == end) { target[start] = source[start]; } else { mid = (start+end)/2; mergeSort(source, tmp, start, mid); mergeSort(source, tmp, mid+1, end); merge(tmp, target, start, mid, end); } } void show(int a[], int length) { int i; for (i=0; i<length; i++) { printf("%d ", a[i]); } printf("\n"); } int main(int argc, char **argv) { int a[] = {98,76,109,34,67,190,80,12,14,89,1}; int length = sizeof(a)/sizeof(a[0]); show(a, length); // insertSort(a, length); // shellSort(a, length); // bubbleSort(a, length); // quickSort(a, 0, length-1); // selectSort(a, length); // headSort(a, length); int b[11]; mergeSort(a, b, 0, 11); show(a, length); show(b, length); }