提交 62d270c6 创建 作者: 宋海霞's avatar 宋海霞

modify

上级 f3f1d752
...@@ -3,24 +3,24 @@ ...@@ -3,24 +3,24 @@
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
SeqTable ST; // 线性表 SeqTable ST; // Linear table
int i, pos1, pos2; int i, pos1, pos2;
// //
// 初始化线性表 // Initialize the linear table
// //
InitSeqTable(&ST); InitSeqTable(&ST);
// //
// 折半查找 // Binary search
// //
pos1 = BinarySearch(&ST, 60); pos1 = BinarySearch(&ST, 60);
pos2 = BinarySearch(&ST, 30); // 查找失败 pos2 = BinarySearch(&ST, 30); // To find the failure
printf("%d %d\n", pos1, pos2); printf("%d %d\n", pos1, pos2);
// //
// 销毁线性表 // Destroy linear table
// //
DeleteSeqTable(&ST); DeleteSeqTable(&ST);
...@@ -28,16 +28,16 @@ int main(int argc, char* argv[]) ...@@ -28,16 +28,16 @@ int main(int argc, char* argv[])
} }
/* /*
功能: function:
折半查找。 Binary search.
参数: parameter:
pST -- 线性表指针 pST -- Linear table pointer
key -- 查找关键字 key -- search key
返回值: returned value:
如果查找成功返回关键字在表中的位置 Returns the position of the keyword in the table if the lookup is successful
如果查找失败返回 0 Returns 0 if the lookup fails
*/ */
int BinarySearch(SeqTable* pST, KeyType key) int BinarySearch(SeqTable* pST, KeyType key)
{ {
...@@ -46,38 +46,38 @@ int BinarySearch(SeqTable* pST, KeyType key) ...@@ -46,38 +46,38 @@ int BinarySearch(SeqTable* pST, KeyType key)
int mid; int mid;
// //
// TODO: 在此添加代码 // TODO: Add the code here
// //
return 0; return 0;
} }
/* /*
功能: function:
初始化线性表。 Initialize the linear table.
参数: parameter:
pST -- 线性表指针 pST -- Linear table pointer
*/ */
// 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 // 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
int InitArray[] = { 2, 4, 11, 18, 25, 32, 39, 46, 53, 60, 67, 74 }; // 顺序必须为从小到大 int InitArray[] = { 2, 4, 11, 18, 25, 32, 39, 46, 53, 60, 67, 74 }; // The order must be from smallest to largest
void InitSeqTable(SeqTable* pST) void InitSeqTable(SeqTable* pST)
{ {
int i; int i;
pST->length = sizeof(InitArray) / sizeof(InitArray[0]); pST->length = sizeof(InitArray) / sizeof(InitArray[0]);
pST->elem = (ElemType*)malloc((pST->length + 1) * sizeof(ElemType)); // 因为单元 0 留空,所以长度必须加 1 pST->elem = (ElemType*)malloc((pST->length + 1) * sizeof(ElemType)); // Because the 0 is left blank, the length must be increased by 1
for(i=1; i<=pST->length; i++) for(i=1; i<=pST->length; i++)
pST->elem[i].key = InitArray[i-1]; pST->elem[i].key = InitArray[i-1];
} }
/* /*
功能: function:
销毁线性表。 Destroy linear table.
参数: parameter:
pST -- 线性表指针 pST -- Linear table pointer
*/ */
void DeleteSeqTable(SeqTable* pST) void DeleteSeqTable(SeqTable* pST)
{ {
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论