我正在尝试打印链接列表的所有成员。我正在遍历列表并计算列表中整数的重复副本(如果有)。但是当我再次遍历列表以检查重复副本时,我的 ipNext 指向 null 终止我之前的遍历循环。
插入数据函数:
void insertIP(bstNode *head, char user[], int ip){
if(head != NULL){
bstNode* startList = head;
while ((startList) && (strcmp(startList->data, user) != 0) ){
if(strcmp(user, startList->data)<0)
{
startList=startList->left;
}
else if(strcmp(user, startList->data)>0)
{
startList=startList->left;
}
}
if (startList != NULL){
IP* new = (IP*)malloc(sizeof(IP));
new->ip = ip;
//new->count = (new->count + 1);
new->ipNext=NULL;
IP* temp = startList->ipHead;
startList->ipHead = new;
new->ipNext = temp;
}
}
}
迭代函数查找特定的数据条目并计算它在链表中的出现次数(如果有)。
bstNode* search(char* key, bstNode* root)
{
int res;
bstNode *leaf = root;
if( leaf != NULL ) {
res = strcmp(key, leaf->data);
if( res < 0)
search( key, leaf->left);
else if( res > 0)
search( key, leaf->right);
else
{
printf("\n'%s' found!\n", key);
//int count = 0;
bstNode *temp = leaf;
while (temp->ipHead != NULL) {
int tempip = temp->ipHead->ip;
int ipcount = 0;
uint32_t ip = tempip;
struct in_addr ip_addr;
ip_addr.s_addr = ip;
bstNode *cpy = leaf;
ipcount = count(&cpy, tempip);
//temp = leaf;
printf("The IP address is %s\n C:%d\n", inet_ntoa(ip_addr), ipcount);
temp->ipHead = temp->ipHead->ipNext;
}
}
}
else printf("\nNot in tree\n");
return leaf;
}
支持函数(这将 ipNext 值设置为 null,从而终止搜索循环。即使我传递了指针的副本,我认为这是我的问题)。
int count(bstNode** start, int item)
{
bstNode* current = *start;
int count = 0;
while (current->ipHead->ipNext != NULL)
{
if (current->ipHead->ip == item)
{
count++;
}
current->ipHead = current->ipHead->ipNext;
}
return count;
}
数据结构减速:
typedef struct ip{
int ip;
struct ip *ipNext;
}IP;
typedef struct bstNode
{
char data[32];
struct bstNode* left;
struct bstNode* right;
IP *ipHead;
}bstNode;
BST插入函数:
bstNode *insert(bstNode *root, char *word, int ip)
{
bstNode *node = root;
if(node==NULL){
node= malloc(sizeof(bstNode));
//IP* ipNode=malloc(sizeof(IP));
strcpy(node->data, word);
node->left=NULL;
node->right=NULL;
insertIP(node, word, ip);
}
else{
if(strcmp(word, node->data)<0)
node->left=insert(node->left, word, ip);
else if(strcmp(word, node->data)>0)
node->right=insert(node->right, word,ip);
else if(strcmp(word, node->data) == 0) {
insertIP(node, word, ip);
}
}
return node;
}
感谢大家的帮助!
最佳答案
正如评论中指出的,这是您的问题:
while ((startList) && (strcmp(startList->data, user) != 0) ){
if(strcmp(user, startList->data)<0)
{
startList=startList->left;
}
else if(strcmp(user, startList->data)>0)
{
startList=startList->left;
}
}
在这两种情况下您都走左边的路。我不会将其作为答案发布(因为它已经在 Pras 的评论中得到了回答,但是当我看到这种未优化的代码时,它让我烦恼:您可能会使用相同的内容多次调用 strcmp() 函数数据和结果总是相同的。如果比较长字符串,strcmp() 可能是一个代价高昂的操作,而且您是在树内部执行此操作,因此此操作可能会执行很多次。所以为什么不呢您第一次将结果分配给变量,然后测试结果,而不是再次调用 strcmp()。例如:
int result;
while (startList && (result = strcmp(startList->data, user)) ) {
if (result < 0)
{
startList = startList->left;
}
else if (result > 0)
{
startList = startList->right;
}
}
实际上不需要第二个if,你可以使用一个简单的else,因为零的测试是在while语句的条件下完成的,所以显然如果结果不< 0,它肯定会> 0。
关于c - C中无法迭代链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49441853/