« 2012年1月的文章归档

centos 下为php添加mongodb扩展

export PHP_AUTOCONF=/usr/bin/autoconf
export PHP_AUTOHEADER=/usr/bin/autoheader
wget http://pecl.php.net/get/mongo-1.2.7.tgz
tar zxvf mongo-1.2.7.tgz
cd mongo-1.2.7
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config
make && make install

//最后在php.ini中加入以下行并重启webserver
extension=mongo.so

查看phpinfo,如出现mongo字样,说明成功了。

KMP算法精髓之预算NEXT值

void next_index(char *str, int next[]) {h
    int i = 0, j = -1, str_length = strlen(str);
    next[0] = -1;
    while(i < str_length) {
        if(j == -1 || str[i] == str[j] ) {
            ++i;
            ++j;
            next[i] = j;
        } else {
            j = next[j];
        }
    }
}

顺序表的初始、插入、删除实现

#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
#include<stdlib.h>
#include<stdio.h>
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef struct {
    ElemType *elem;
    int length;
    int listsize;
} SqList;

Status InitList_Sq(SqList *L) {
    L->elem = (ElemType *)malloc(LIST_INIT_SIZE * sizeof(ElemType));
    if (!L->elem) exit(OVERFLOW);
    L->length = 0;
    L->listsize = LIST_INIT_SIZE;
    return OK;
}

Status ListInsert_Sq(SqList *L, int i, ElemType e) {
    ElemType *newbase, *q, *p;
    if (i < 1 || i > L->length + 1) return ERROR;
    if (L->length >= L->listsize) {
        newbase = (ElemType *)realloc(L->elem, (L->listsize + LISTINCREMENT) * sizeof(ElemType));
        if (!newbase) exit(OVERFLOW);
        L->elem = newbase;
        L->listsize += LISTINCREMENT;
    }
    q = &(L->elem[i-1]);
    for (p = &(L->elem[L->length - 1]); p >= q; --p) *(p + 1) = *p;
    *q = e;
    ++L->length;
    return OK;
}

Status ListDelete_Sq(SqList *L, int i, ElemType *e) {
    ElemType *p, *q;
    if(i < 1 || i > L->length) return ERROR;
    p = &(L->elem[i - 1]);
    *e = *p;
    q = L->elem + L->length - 1;
    for(++p; p length;
    return OK;
}