#include <stdio.h> #include <stdlib.h> #include <string.h> #include "search.h" int Search_Seq(SSTable L, KeyType key) {/*在静态查找表L中采用顺序查找法,查找其关键字等于key的记录,若找到返回该记录在表中位置,否则返回0*/ // 请在这里补充代码,完成本关任务 /********** Begin *********/ /********** End **********/ } void SSTableInput(SSTable &L) /*输入若干记录的关键字,存放到静态查找表L中*/ { int i=1; KeyType x; scanf("%d",&x); while(x!=-1) { L.r[i++].key=x; scanf("%d",&x); } L.length=i-1; } void SSTableOutput(SSTable L) /*输出静态查找表L中各记录的关键字*/ { int i; for(i=1;i<=L.length;i++) printf("%d ",L.r[i].key); printf("\n"); }
时间: 2023-07-23 15:56:41 浏览: 85
这段代码是实现了一个静态查找表,其中包括了以下函数:
1. `Search_Seq(SSTable L, KeyType key)`:在静态查找表L中采用顺序查找法,查找其关键字等于key的记录,若找到返回该记录在表中位置,否则返回0。
2. `SSTableInput(SSTable &L)`:输入若干记录的关键字,存放到静态查找表L中。
3. `SSTableOutput(SSTable L)`:输出静态查找表L中各记录的关键字。
其中,`SSTable` 是一个结构体,包含了以下成员变量:
- `r`:记录数组,存放静态查找表中的记录。
- `length`:记录数组中记录的个数。
另外,该段代码中包含了头文件 `search.h`,但没有提供该头文件的内容,因此无法确定其中定义了哪些类型和函数。
相关问题
#include <stdio.h> #include <stdlib.h> #include <string.h> #include "search.h" int Search_Seq(SSTable L, KeyType key) {/*在静态查找表L中采用顺序查找法,查找其关键字等于key的记录,若找到返回该记录在表中位置,否则返回0*/ // 请在这里补充代码,完成本关任务 /********** Begin *********/ /********** End ***
以下是完整的代码实现,包括注释:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "search.h"
int Search_Seq(SSTable L, KeyType key) {
/*在静态查找表L中采用顺序查找法,查找其关键字等于key的记录,若找到返回该记录在表中位置,否则返回0*/
int i;
for (i = 1; i <= L.length; i++) { // 依次遍历表中的每个元素
if (EQ(key, L.elem[i].key)) { // 如果找到了与key相等的元素
return i; // 返回该元素在表中的位置
}
}
return 0; // 如果遍历完整个表都没有找到与key相等的元素,则返回0
}
```
仔细观察代码,可以发现:
- 函数的返回值是int类型,表示该元素在表中的位置,如果找不到则返回0。
- 函数的参数有两个,分别是SSTable类型的静态查找表L和KeyType类型的关键字key。
- 函数中使用了for循环遍历表中的每个元素,如果找到了与key相等的元素,则返回该元素在表中的位置。
- 判断元素是否相等的方法是调用了EQ宏定义,EQ的定义可以在search.h头文件中找到。
需要注意的是,这个顺序查找算法只适用于静态查找表,即表中的数据不发生变化,否则需要重新建立查找表。
PACKET_SHADOW_ENTRY *packetShadowEntryFind(PACKET_SHADOW_ENTRY_QUEUE *head, SDDM_LPP_PACKET_KEY* pPacketKey) { PACKET_SHADOW_ENTRY *cur = NULL; PACKET_SHADOW_ENTRY *next = NULL; if((NULL == head) || (NULL == pPacketKey)) { return NULL; } STAILQ_FOREACH_SAFE(cur, head, link_next, next) { if(cur->packetKey.pkt_seq == pPacketKey->pkt_seq) { return cur; } } return NULL; } 用c语言写一个ft测试用例,覆盖所有分支
以下是一个可能的测试用例:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stddef.h>
#include "ft.h"
// Mock data types and functions
typedef struct {
int pkt_seq;
} SDDM_LPP_PACKET_KEY;
typedef struct {
SDDM_LPP_PACKET_KEY packetKey;
int payload;
STAILQ_ENTRY(PACKET_SHADOW_ENTRY) link_next;
} PACKET_SHADOW_ENTRY;
typedef STAILQ_HEAD(PACKET_SHADOW_ENTRY_QUEUE, PACKET_SHADOW_ENTRY) PACKET_SHADOW_ENTRY_QUEUE;
PACKET_SHADOW_ENTRY_QUEUE* mockPacketShadowEntryQueueCreate() {
PACKET_SHADOW_ENTRY_QUEUE* queue = malloc(sizeof(PACKET_SHADOW_ENTRY_QUEUE));
STAILQ_INIT(queue);
return queue;
}
PACKET_SHADOW_ENTRY* mockPacketShadowEntryCreate(int pkt_seq, int payload) {
PACKET_SHADOW_ENTRY* entry = malloc(sizeof(PACKET_SHADOW_ENTRY));
entry->packetKey.pkt_seq = pkt_seq;
entry->payload = payload;
return entry;
}
void mockPacketShadowEntryQueueDestroy(PACKET_SHADOW_ENTRY_QUEUE* queue) {
PACKET_SHADOW_ENTRY *cur, *next;
for (cur = STAILQ_FIRST(queue); cur != NULL; cur = next) {
next = STAILQ_NEXT(cur, link_next);
free(cur);
}
free(queue);
}
// Test cases
void test_packetShadowEntryFind_given_null_queue_should_return_null() {
PACKET_SHADOW_ENTRY_QUEUE* head = NULL;
SDDM_LPP_PACKET_KEY packetKey = { .pkt_seq = 1 };
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, &packetKey);
if (result != NULL) {
printf("test_packetShadowEntryFind_given_null_queue_should_return_null: Fail (result is not NULL)\n");
}
}
void test_packetShadowEntryFind_given_null_packetKey_should_return_null() {
PACKET_SHADOW_ENTRY_QUEUE* head = mockPacketShadowEntryQueueCreate();
SDDM_LPP_PACKET_KEY* packetKey = NULL;
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, packetKey);
mockPacketShadowEntryQueueDestroy(head);
if (result != NULL) {
printf("test_packetShadowEntryFind_given_null_packetKey_should_return_null: Fail (result is not NULL)\n");
}
}
void test_packetShadowEntryFind_given_empty_queue_should_return_null() {
PACKET_SHADOW_ENTRY_QUEUE* head = mockPacketShadowEntryQueueCreate();
SDDM_LPP_PACKET_KEY packetKey = { .pkt_seq = 1 };
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, &packetKey);
mockPacketShadowEntryQueueDestroy(head);
if (result != NULL) {
printf("test_packetShadowEntryFind_given_empty_queue_should_return_null: Fail (result is not NULL)\n");
}
}
void test_packetShadowEntryFind_given_queue_with_one_element_matching_packetKey_should_return_element() {
PACKET_SHADOW_ENTRY_QUEUE* head = mockPacketShadowEntryQueueCreate();
SDDM_LPP_PACKET_KEY packetKey = { .pkt_seq = 1 };
PACKET_SHADOW_ENTRY* element = mockPacketShadowEntryCreate(1, 10);
STAILQ_INSERT_TAIL(head, element, link_next);
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, &packetKey);
mockPacketShadowEntryQueueDestroy(head);
if (result != element) {
printf("test_packetShadowEntryFind_given_queue_with_one_element_matching_packetKey_should_return_element: Fail (result is not expected element)\n");
}
}
void test_packetShadowEntryFind_given_queue_with_one_element_not_matching_packetKey_should_return_null() {
PACKET_SHADOW_ENTRY_QUEUE* head = mockPacketShadowEntryQueueCreate();
SDDM_LPP_PACKET_KEY packetKey = { .pkt_seq = 1 };
PACKET_SHADOW_ENTRY* element = mockPacketShadowEntryCreate(2, 10);
STAILQ_INSERT_TAIL(head, element, link_next);
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, &packetKey);
mockPacketShadowEntryQueueDestroy(head);
if (result != NULL) {
printf("test_packetShadowEntryFind_given_queue_with_one_element_not_matching_packetKey_should_return_null: Fail (result is not NULL)\n");
}
}
void test_packetShadowEntryFind_given_queue_with_multiple_elements_matching_packetKey_should_return_first_matching_element() {
PACKET_SHADOW_ENTRY_QUEUE* head = mockPacketShadowEntryQueueCreate();
SDDM_LPP_PACKET_KEY packetKey = { .pkt_seq = 1 };
PACKET_SHADOW_ENTRY* element1 = mockPacketShadowEntryCreate(1, 10);
PACKET_SHADOW_ENTRY* element2 = mockPacketShadowEntryCreate(1, 20);
PACKET_SHADOW_ENTRY* element3 = mockPacketShadowEntryCreate(2, 30);
STAILQ_INSERT_TAIL(head, element1, link_next);
STAILQ_INSERT_TAIL(head, element2, link_next);
STAILQ_INSERT_TAIL(head, element3, link_next);
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, &packetKey);
mockPacketShadowEntryQueueDestroy(head);
if (result != element1) {
printf("test_packetShadowEntryFind_given_queue_with_multiple_elements_matching_packetKey_should_return_first_matching_element: Fail (result is not expected element)\n");
}
}
void test_packetShadowEntryFind_given_queue_with_multiple_elements_not_matching_packetKey_should_return_null() {
PACKET_SHADOW_ENTRY_QUEUE* head = mockPacketShadowEntryQueueCreate();
SDDM_LPP_PACKET_KEY packetKey = { .pkt_seq = 1 };
PACKET_SHADOW_ENTRY* element1 = mockPacketShadowEntryCreate(2, 10);
PACKET_SHADOW_ENTRY* element2 = mockPacketShadowEntryCreate(3, 20);
PACKET_SHADOW_ENTRY* element3 = mockPacketShadowEntryCreate(4, 30);
STAILQ_INSERT_TAIL(head, element1, link_next);
STAILQ_INSERT_TAIL(head, element2, link_next);
STAILQ_INSERT_TAIL(head, element3, link_next);
PACKET_SHADOW_ENTRY* result = packetShadowEntryFind(head, &packetKey);
mockPacketShadowEntryQueueDestroy(head);
if (result != NULL) {
printf("test_packetShadowEntryFind_given_queue_with_multiple_elements_not_matching_packetKey_should_return_null: Fail (result is not NULL)\n");
}
}
int main() {
test_packetShadowEntryFind_given_null_queue_should_return_null();
test_packetShadowEntryFind_given_null_packetKey_should_return_null();
test_packetShadowEntryFind_given_empty_queue_should_return_null();
test_packetShadowEntryFind_given_queue_with_one_element_matching_packetKey_should_return_element();
test_packetShadowEntryFind_given_queue_with_one_element_not_matching_packetKey_should_return_null();
test_packetShadowEntryFind_given_queue_with_multiple_elements_matching_packetKey_should_return_first_matching_element();
test_packetShadowEntryFind_given_queue_with_multiple_elements_not_matching_packetKey_should_return_null();
return 0;
}
```
阅读全文