int update_glist(struct gdev_info *ginfo, int sum_num) { int pre, mid, tai, ret; pre = 0; tai = sum_num - 1; if(0 == sum_num) { insert_glist(ginfo, 0, 0); return 1; } struct gdev_info *tmp_info; while(pre <= tai)//等號的做用,在於循環結束時pre的位置即爲要插入的位置(tail >= 0時) { //mid = (pre + tai)/2; mid = pre + (tai - pre)>>2;//防止溢出 tmp_info = &g_infolist[mid]; ret = memcmp(ginfo->mac + 6, tmp_info->mac + 6, 6); if(ret < 0) { tai = mid - 1; } else if(ret > 0) { pre = mid + 1;//maybe do'not need to add 1 } else { //成功找到 return 0; } //printf("mid:%d\n",mid); } if(G_INFO_MAX_NUM == sum_num ) { return -1; } ret = insert_glist(ginfo, pre, sum_num); return (ret); }