1.1 算法设计步骤
a.输入输出,由此确定算法的参数和返回值。
b.使用断言,检查输入参数的合法性,防止非法输入。
c.考虑边界,全面考虑可能出现的所有情况。
d.出错处理,goto error方式。
1.2 字符串
1: char* strstr(char* s1,char* s2){
2: char *p=s1,*r=s2;
3: while(*p!=‘\0‘){
4: while(*p++==*r++);
5: if(*r==‘\0‘)
6: return p;
7: else{
8: r=s2;
9: p=s1++;
10: }
11: }
12: return NULL;
13: }
1: char* strcpy(char *strDst,char *strSrc){
2: if(strDst==strSrc)
3: return strDst;
4: char *pDst=strDst;
5: char *pSrc=strSrc;
6: while((*pDst++=*pSrc++)!=‘\0‘);
7: *pDst=‘\0‘;
8: return pDst;
9: }
1: char* deletechar(char *str,char chr[],int n){
2: char tmp[256]={0};
3: for(int i=0;i<n;i++)
4: tmp[chr[i]]=1;
5: int iDst=0,iSrc=0;
6: while(str[iSrc++]!=‘\0‘){
7: if(!tmp[str[iSrc]])
8: str[iDst++]=str[iSrc];
9: }
10: }
1: int wordnum(char *str){
2: int num=0;
3: char *pchar=str;
4: while(*pchar!=‘\0‘){
5: while(*pchar!=‘ ‘ && *pchar++!=‘\0‘);
6: num++;
7: pchar++;
8: }
9: }
1: void reverse(char *str){
2: int n=strlen(str);
3: char c;
4: for(int i=0;i<n/2;i++){
5: c=str[i];
6: str[i]=str[n-i];
7: str[n-i]=c;
8: }
9: }
1: void memcpy(char *strDst,char *strSrc,int size){
2: char *pSrc=(char*)strSrc+size-1;
3: char *pDst=(char*)strDst+size-1;
4: while(size--)
5: *pDst--=*pSrc--;
6: }
1: void sortstring(char *str,int n){
2: char *tmp=NULL;
3: for(int i=0;i<n-1;i++){
4: for(int j=n-1;j>i;j--){
5: if(strcmp(str[j],str[j-1])<0){
6: tmp=str[j];
7: str[j]=str[j-1];
8: str[j-1]=tmp;
9: }
10: }
11: }
12: }
1.3 链表
1: typedef struct _node{
2: int data;
3: struct _node *next;
4: }node,*list;
1: void reverselist(node **head){
2: node *p,*q,*r;
3: p=*head;
4: q=p->next;
5: while(!q=NULL){
6: r=q->next;
7: q->next=p;
8: p=q;
9: q=r;
10: }
11: (*head)->next=NULL;
12: *head=p;
13: }
1: void sortlist(node *head){
2: node *p,*q,*s;
3: int t;
4: p=head;
5: while(p){
6: s=p;
7: q=p->next;
8: while(q){
9: if(q->value < s->value)
10: s=q;
11: q=q->next;
12: }
13: if(s!=p){
14: t=s->value;
15: s->value=p->value;
16: p->value=t;
17: }
18: p=p->next;
19: }
20: }
1.4 树
1: typedef struct _tree{
2: int data;
3: struct _tree *left;
4: struct _tree *right;
5: }tree,*ptree;
1: void preorder(tree *t){
2: if(t){
3: printf("%d",t->data);
4: preorder(t->left);
5: preorder(t->right);
6: }
7: }
1: void inorder(tree *t){
2: if(t){
3: inorder(t->left);
4: printf("%d",t->data);
5: inorder(t->right);
6: }
7: }
1: void postorder(tree *t){
2: if(t){
3: postorder(t->left);
4: postorder(t->right);
5: printf("%d‘,t->data);
6: }
7: }
1: void broadorder(tree *t){
2: Queue q;
3: tree *pt=NULL;
4: Enque(q,t);
5: while(!IsQueEmpty(q)){
6: pt=Deque(q);
7: printf("%d",pt->data);
8: if(pt->left!=NULL)
9: Enque(q,pt->left);
10: if(pt->right!=NULL)
11: Enque(q,pt->right);
12: }
13: }
二叉排序树:
1: void insert(tree *b,tree *s){
2: if(b==NULL)
3: b=s;
4: else if(s->data==b->data)
5: break;
6: else if(s->data<b->data)
7: insert(b->left,s);
8: else if(s->data>b->data)
9: insert(b->right,s);
10: }
1: void create(tree *b){
2: int x;
3: tree *s;
4: do{
5: scanf("%d",&x);
6: s=(node*)malloc(sizeof(node));
7: s->data=x;
8: s->left=NULL;
9: s->right=NULL;
10: insert(b,s);
11: }while(x!=-1);
12: }
1.5 数
1: int gys(int i,int j){
2: if(i%j==0)
3: return j;
4: else
5: return gys(j,i%j);
6: }
1: int atoi(char *s){
2: int i,n,sign;
3: for(i=0;s[i]==‘ ‘;i++);
4: sign=(s[i]==‘-‘)?-1:1;
5: if(s[i]==‘+‘ || s[i]==‘-‘)
6: i++;
7: for(n=0;‘0‘<=s[i]<=‘9‘;i++)
8: n=10*n+(s[i]-‘0‘);
9: return sign*n;
10: }
1.6 数组
1: void findminmax(int a[],int n){
2: int i,tmp,min,max;
3: for(i=0;i<n-2;i+=2){
4: if(a[i]<a[i+1]){
5: tmp=a[i];
6: a[i]=a[i+1];
7: a[i+1]=tmp;
8: }
9: }
10: max=a[0];
11: min=a[1];
12: for(i=0;i<n-2;i+=2){
13: if(min>a[i])
14: min=a[i];
15: }
16: for(i=1;i<n-2;i+=2){
17: if(max<a[i])
18: min=a[i];
19: }
20: printf("min:%d,max:%d\n",min,max);
21: }
1: void findrepeated(int a[],int n){
2: int tmp[MAX]={0};
3: for(int i=0;i<n;i++)
4: tmp[a[i]]++;
5: for(i=0;i<MAX;i++)
6: if(tmp[i]>1)
7: printf("%d",i);
8: }
1.7 排序
1: void insertsortint a[],int n){
2: int i,j,tmp;
3: for(i=0;i<n;i++){
4: tmp=a[i];
5: j=i-1;
6: while(tmp<a[j]){
7: a[j+1]=a[j];
8: j--;
9: }
10: a[j+1]=tmp;
11: }
12: }
1: void shellsort(int a[],int n){
2: int i,j,gap,tmp;
3: gap=n/2;
4: while(gap>0){
5: for(i=gap+1;i<=n;i++){
6: j=i-gap;
7: while(j>=0){
8: if(a[j]>a[j+gap]){
9: tmp=a[j];
10: a[j]=a[j+gap];
11: a[j+gap]=tmp;
12: }else
13: j=0;
14: }
15: }
16: gap/=2;
17: }
18: }
1: void selectsort(int a[],int n){
2: int i,j,k,tmp;
3: for(i=0;i<n-1;i++){
4: k=i;
5: for(j=i+1;j<n;j++){
6: if(a[j]<a[k])
7: k=j;
8: }
9: if(k!=i){
10: tmp=a[i];
11: a[i]=a[k];
12: a[k]=tmp;
13: }
14: }
15: }
1: void bubblesort(int a[],int n){
2: int i,j,tmp;
3: for(i=0;i<n-1;i++){
4: for(j=n;j>i;j--){
5: if(a[j]<a[j-1]){
6: tmp=a[j];
7: a[j]=a[j-1];
8: a[j-1]=tmp;
9: }
10: }
11: }
12: }
1: void sift(int d[], int ind, int len)
2: {
3: //#置i为要筛选的节点#%
4: int i = ind;
5: //#c中保存i节点的左孩子#%
6: int c = i * 2 + 1; //#+1的目的就是为了解决节点从0开始而他的左孩子一直为0的问题#%
7: while(c < len)//#未筛选到叶子节点#%
8: {
9: //#如果要筛选的节点既有左孩子又有右孩子并且左孩子值小于右孩子#%
10: //#从二者中选出较大的并记录#%
11: if(c + 1 < len && d[c] < d[c + 1])
12: c++;
13: //#如果要筛选的节点中的值大于左右孩子的较大者则退出#%
14: if(d[i] > d[c]) break;
15: else
16: {
17: //#交换#%
18: int t = d[c];
19: d[c] = d[i];
20: d[i] = t;
21: //#重置要筛选的节点和要筛选的左孩子#%
22: i = c;
23: c = 2 * i + 1;
24: }
25: }
26: return;
27: }
28: void heap_sort(int d[], int n)
29: {
30: //#初始化建堆, i从最后一个非叶子节点开始#%
31: for(int i = (n - 2) / 2; i >= 0; i--)
32: sift(d, i, n);
33: for(int j = 0; j < n; j++)
34: {
35: //#交换#%
36: int t = d[0];
37: d[0] = d[n - j - 1];
38: d[n - j - 1] = t;
39: //#筛选编号为0 #%
40: sift(d, 0, n - j - 1);
41: }
42: }
1: //将有二个有序数列a[first...mid]和a[mid...last]合并。
2: void mergearray(int a[], int first, int mid, int last, int temp[])
3: {
4: int i = first, j = mid + 1;
5: int m = mid, n = last;
6: int k = 0;
7:
8: while (i <= m && j <= n)
9: {
10: if (a[i] <= a[j])
11: temp[k++] = a[i++];
12: else
13: temp[k++] = a[j++];
14: }
15:
16: while (i <= m)
17: temp[k++] = a[i++];
18:
19: while (j <= n)
20: temp[k++] = a[j++];
21:
22: for (i = 0; i < k; i++)
23: a[first + i] = temp[i];
24: }
25: void mergesort(int a[], int first, int last, int temp[])
26: {
27: if (first < last)
28: {
29: int mid = (first + last) / 2;
30: mergesort(a, first, mid, temp); //左边有序
31: mergesort(a, mid + 1, last, temp); //右边有序
32: mergearray(a, first, mid, last, temp); //再将二个有序数列合并
33: }
34: }
1.8 查找
1: int binsearch(int a[],int n,int k){
2: int low,high,mid ,find,i;
3: find=0;
4: low=1;
5: high=n;
6: while(low<=high && !find){
7: mid=(low+high)/2;
8: if(a[mid]<k)
9: low=mid+1;
10: else if(a[mid]>k)
11: high=mid-1;
12: else{
13: i=mid;
14: find=1;
15: }
16: }
17: if(!find)
18: i=0;
19: retun i;
20: }
1: tree* search(tree *b,int x){
2: if(b==NULL)
3: return NULL;
4: else if(b->data==x)
5: return b;
6: else if(x<b->data)
7: return search(b->left);
8: else
9: return search(b->right);
10: }
1: char getFirstNonRepeated(char *str){
2: int hash[256]={0};
3: int i;
4: char *pstr=str;
5: while(*pstr!=‘\0‘){
6: hash[*pstr]++;
7: pstr++;
8: }
9: while(*pstr!=‘\0‘){
10: if(hash[*pstr]==1)
11: return *pstr;
12: pstr++;
13: }
14: return NULL;
15: }