提交 7e57b542 创建 作者: 宋海霞's avatar 宋海霞

modify

上级 bc6a75fa
#ifndef SEQTABLE_H_ #ifndef SEQTABLE_H_
#define SEQTABLE_H_ #define SEQTABLE_H_
// //
// Include the C standard library header file here // Include the C standard library header file here
// //
#include <stdio.h> #include <stdio.h>
// //
// Other header files are included here // Other header files are included here
// //
// //
// Define the data structure here // Define the data structure here
// //
// Elements in a linear table // Elements in a linear table
typedef int KeyType; typedef int KeyType;
typedef struct ElemType { typedef struct ElemType
{
KeyType key; // Primary key KeyType key; // Primary key
// Omit other data and no longer define // Omit other data and no longer define
}ElemType; }ElemType;
// The sequential storage structure of a linear table // The sequential storage structure of a linear table
typedef struct SeqTable { typedef struct SeqTable
ElemType* elem; // Data element storage space base address, allocated according to the actual length when the table is built, {
ElemType* elem; // Data element storage space base address,
// allocated according to the actual length when the table is built,
// Unit 0 is left blank and is used from unit 1. // Unit 0 is left blank and is used from unit 1.
int length; // The length of the table int length; // The length of the table
}SeqTable; }SeqTable;
...@@ -43,12 +41,8 @@ int BinarySearch(SeqTable* pST, KeyType key); ...@@ -43,12 +41,8 @@ int BinarySearch(SeqTable* pST, KeyType key);
void InitSeqTable(SeqTable* pST); void InitSeqTable(SeqTable* pST);
void DeleteSeqTable(SeqTable* pST); void DeleteSeqTable(SeqTable* pST);
// //
// Declare global variables here // Declare global variables here
// //
#endif /* SEQTABLE_H_ */ #endif /* SEQTABLE_H_ */
#include "SeqTable.h" #include "SeqTable.h"
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
SeqTable ST; // Linear table SeqTable ST; // Linear table
...@@ -59,17 +58,22 @@ function: ...@@ -59,17 +58,22 @@ function:
parameter: parameter:
pST -- Linear table pointer 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 }; // The order must be from smallest to largest // The order must be from smallest to largest
int InitArray[] = { 2, 4, 11, 18, 25, 32, 39, 46, 53, 60, 67, 74 };
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)); // Because the 0 is left blank, the length must be increased by 1 // Because the 0 is left blank, the length must be increased by 1
pST->elem = (ElemType*)malloc((pST->length + 1) * sizeof(ElemType));
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];
}
} }
/* /*
......
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论