c++ - 感觉函数逻辑写复杂了 ,大家看看能否优化下

浏览:55日期:2023-05-08

问题描述

这个函数功能类似于单链表插入

/*这是节点结构*/ struct list_node {int number;list_node *next; }*p;//函数要判断p指向的链表中是否存在相同元素,如果存在什么都不做;如果不存在在链表尾部添加该元素void addConnection(int x, int y){auto *head =p;if (p !=nullptr){ auto *head = adjacency_listx->vlist[x].list; while (p != nullptr) {if (p->number == y){ return;}if (p->next != nullptr){ p = p->next;}else{ p->next = new list_node; p->next->next = nullptr; p->next->number = y; p = head; break;} }}else{ p = new list_node; p->next = nullptr; p->number = y;}}

问题解答

回答1:

list_node **pp;for (pp = &p; *pp != NULL && (*pp)->number != y; pp = &(*pp)->next);if (*pp == NULL) { *pp = new list_node(y);}

相关文章: