上下文是L链表之一.我假设L在开头不是0,并且每个链表都以一个节点作为下一个字段结束.
void g(node*, int, char);
void g(node* L, int k, char y) {
node* current = L;
if (current->info == y) k--;
while (current->next) {
if (current->next->info == y) {
if (k > 0) k--;
else {
node* very_next = current->next->next;
delete current->next;
current->next = very_next;
}
}
current = current->next;
}
}
Run Code Online (Sandbox Code Playgroud)
我不断收到BAD_ACCESS警告while(current->next).怎么了?我正在那里访问一个正确的节点,因为测试(!current->next)失败了.那有什么不对?
我正在测试的链表是
node* n = new node('a',new node('b', new node('a', new node('c', new node('a', 0)))));
Run Code Online (Sandbox Code Playgroud)
使用此结构:
struct node {
char info;
node* next;
node(char a = 0, nodo* b = 0) {
info = a;
next = b;
}
};
Run Code Online (Sandbox Code Playgroud)
小智 5
如果current->next->next == very_next = NULL,也不会分配当前的电流NULL,从而使以后访问当前(通过current->next)invalid(current = current->next = very_next)?
| 归档时间: |
|
| 查看次数: |
117 次 |
| 最近记录: |