《C算法》——基本數據結構及其算法

 數組
-----------------------------------------------------------------------------------------------------
厄拉多塞素數篩選法
求出小於N的所有素數(若i爲素數,則設置a[i]爲1;不是素數則設爲0)

#define N 1000

main()
{
    
int
 i, j, a[N];
    
for(i = 2; i < N; i++) a[i] = 1
;
    
for(i = 2; i < N; i++
)
    
{
        
if
(a[i])
        
{
            
for(j = i; j < N/i; j++
)
            
{
                a[i
*j] = 0
;
            }

        }

    }


    
for(i = 2; i< N; i++)
    
{
        
if(a[i]) printf("%d"
, i);
    }

    printf(
" ");
}


-----------------------------------------------------------------------------------------------------

鏈表
-----------------------------------------------------------------------------------------------------
定義:

typedef struct node* link;
struct
 node
{
    Item item;
    link next;
}

-----------------------------------------------------------------------------------------------------
約瑟夫問題
N個人排成一個圓周,沿着圓周每次數M個人,就排除對應者,問最後剩下的人是誰。

#include <stdlib.h>
typedef 
struct node* link;
struct
 node
{
    Item item;
    link next;
}

main(
int argc, char *argv[])
{
    
int
 i, N, M;
    N 
= atoi(argv[1
]);
    M 
= atoi(argv[2
]);
    link t 
= (node*)malloc(sizeof
(node));
    link x 
=
 t;
    t
->item = 1
;
    t
->next =
 t;
    
for(i = 2; i <= N; i++//構造環形鏈表

    {
        x
->next = (node*)malloc(sizeof
(node));
        x 
= x->
next;
        x
->item =
 i;
        x
->next =
 t;
    }

    
while(x != x->next)  //判斷是否只剩下一個元素
    {
        
for(i = 1; i < M; i++)     x = x->
next;
        x
->next = x->next->next; //刪除第M個節點

    }

    printf(
"%d ", x->item);
}

-----------------------------------------------------------------------------------------------------
倒置鏈表

link reverse(link x)
{
    link t, y 
= x, r =
 NULL;
    
while(y !=
 NULL)
    
{
        t 
= y->
next;
        y
->next =
 r;
        r 
=
 y;
        y 
=
 t;
    }

    
return r;
}

-----------------------------------------------------------------------------------------------------


-----------------------------------------------------------------------------------------------------
串指變長字符數組,由一個起點以及一個標記其結尾的串終止符來定義
-----------------------------------------------------------------------------------------------------
基本串處理運算

計算串長度 strlen(a)
for(int i = 0; a[i] != '/0'; i++) return i;

複製 strcpy(a, b)
for(int i = 0; (a[i] = b[i]) != '/0'; i++)

比較 strcmp(a, b)
for(int i = 0; a[i] == b[i]; i++);
if(a[i] == 0) return 0;
return a[i] - b[i];

追加 strcat(a, b)
strcpy(a+strlen(a), b)

-----------------------------------------------------------------------------------------------------
串搜索

#include <stdio.h>
#define N 10000
main(
int argc, char *argv[])
{
    
int
 i, j, t;
    
char a[N], *= argv[1
];
    
for(i = 0; i < N-1; a[i] = t, i++
)
    
{
        
if((t = getchar()) == EOF) break
;
    }

    a[i] 
= 0;
    
for(i = 0; a[i] != 0; i++
)
    
{
         
for(j = 0; p[j] != 0; j++
)
         
{
             
if(a[i+j] != p[j]) break
;
         }

         
if(p[j] == 0) printf("%d", i);
    }

}

 

複合數據結構
-----------------------------------------------------------------------------------------------------
矩陣相乘

-----------------------------------------------------------------------------------------------------

for(int i = 0; i < N; i++)
    
for(int j = 0; j < N; j++
)
        
for(int k = 0; k < N; k++
)
            c[i][j] 
+= a[i][k] *
 b[k][j];

-----------------------------------------------------------------------------------------------------
二維數組分配

int **malloc2d(int r, int c)
{
    
int **= (int *)malloc(r * sizeof(int *
));
    
for(int i = 0; i < c; i++
)
         t[i] 
= (int)malloc(c * sizeof(int
));
    
return
 t;
}

-----------------------------------------------------------------------------------------------------
棧(LIFO)
-----------------------------------------------------------------------------------------------------
後綴表達式求值

int Func(char *exp)
{
    
char *=
 exp;
    
int i, N = 0
;

    
while(a[i++
])
    
{
        N
++
;
    }

    stack s;
    STACKinit(s, N);
    
for(i = 0; i< N; i++)
    
{
          
if(a[i] == '+'
)
             STACKpush(s, STACKpop(s) 
+
 STACKpop(s));

         
if(a[i] == '*'
)
              STACKpush(s, STACKpop(s) 
*
 STACKpop(s));

        
if((a[i] >= '0'&& (a[i] <= '9'
))
              STACKpush(s, 
0
);

        
while((a[i] >= '0'&& (a[i] <= '9'
))
               STACKpush(s, 
10 * STACKpop(s) + (a[i++- '0'
));
    }

    
return STACKpop(s);
}


-----------------------------------------------------------------------------------------------------
中綴表達式轉後綴表達式

char* Func(char *exp)
{
    
int N = 0, i = 0, j = 0
;
    
while(exp[i++
])
    
{
        N
++
;
    }

    
char* result = (char*)malloc(N * sizeof(char));
    stack s;
    STACKinit(s, N);
    
for(int i = 0; i < N; i++
)
    
{
        
if(exp[i] == ')'
)
            result[j
++=
 STACKpop(s);
        
if((exp[i] == '+'|| (exp[i] == '*'
))
            STACKpush(s, exp[i]);
        
if((exp[i] >= '0'&& (exp[i] <= '9'
))
             result[j
++=
 exp[i];
    }

    result[j] 
= 0;
    
return
 result;
}
發佈了24 篇原創文章 · 獲贊 5 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章