6 10 二分查詢 20分

2021-10-08 23:18:32 字數 1186 閱讀 8907

position binarysearch

( list l, elementtype x )

;

其中list結構定義如下:

typedef

int position;

typedef

struct lnode *list;

struct lnode

;

l是使用者傳入的乙個線性表,其中elementtype元素可以通過》、==、《進行比較,並且題目保證傳入的資料是遞增有序的。函式binarysearch要查詢x在data中的位置,即陣列下標(注意:元素從下標1開始儲存)。找到則返回下標,否則返回乙個特殊的失敗標記notfound。

#include

#include

#define maxsize 10

#define notfound 0

typedef

int elementtype;

typedef

int position;

typedef

struct lnode *list;

struct lnode

;list readinput()

;/* 裁判實現,細節不表。元素從下標1開始儲存 */

position binarysearch

( list l, elementtype x )

;int

main()

/* 你的**將被嵌在這裡 */

5

1231

5589

10131

2
3

2678

23331

0
position binarysearch

( list l, elementtype x )

else

if(l->data[mid]

>x)

else

if(l->data[mid]

}return notfound;

}

6 10 二分查詢(20 分)

本題要求實現二分查詢演算法。函式介面定義 position binarysearch list l,elementtype x 其中list結構定義如下 typedef int position typedef struct lnode list struct lnode l是使用者傳入的乙個線性表...

6 10 二分查詢 20分

position binarysearch list l,elementtype x 其中list結構定義如下 typedef int position typedef struct lnode list struct lnode l是使用者傳入的乙個線性表,其中elementtype元素可以通過 ...

6 10 二分查詢 20 分

本題要求實現二分查詢演算法。函式介面定義 position binarysearch list l,elementtype x 其中list結構定義如下 typedef int position typedef struct lnode list struct lnode l是使用者傳入的乙個線性表...