双链表插入项算法有缺陷
Double-linked-list insert item algorithm flawed
我写了一个双链表:
class doubled {
private:
class sublink {
private:
char data[30];
sublink *next_ptr;
sublink *previous_ptr;
friend doubled;
};
public:
sublink *first_ptr;
doubled(){first_ptr = NULL;};
void add_item(char *item);
void rm_item(char *item);
};
问题在于向列表中添加项目的函数:
void doubled::add_item(char *item){
sublink *new_data;
sublink *n_insert;
sublink *p_insert;
new_data = new sublink;
n_insert = new sublink;
p_insert = new sublink;
if(first_ptr == NULL){
strcpy(new_data->data, item);
new_data->previous_ptr = NULL;
new_data->next_ptr = first_ptr;
first_ptr = new_data;
} else {
strcpy(new_data->data, item);
n_insert = first_ptr;
while(1){
n_insert = n_insert->next_ptr;
if(n_insert == NULL)
break;
if(strcmp(n_insert->data, new_data->data) >= 0){
new_data->next_ptr = n_insert;
n_insert->previous_ptr = new_data;
}
}
p_insert = first_ptr;
while(1){
p_insert = p_insert->next_ptr;
if(p_insert == NULL)
break;
if((strcmp(p_insert->data, new_data->data)) < 0){
new_data->previous_ptr = p_insert;
p_insert->next_ptr = new_data;
}
}
}
std::cout << first_ptr->data << 'n';
std::cout << new_data->data << 'n';
if(new_data->next_ptr != NULL)
std::cout << new_data->next_ptr->data << 'n';
}
上面的代码按字母顺序将给定的项插入到列表中。
程序输出first_ptr->data
和new_data->data
,但不输出new_data->next_ptr->data
,也不输出first_ptr->next_ptr->data
。因此,语句if(new_data->next_ptr != NULL)
总是为真,而不应该为真。
有人看到这个程序的问题了吗?
您可能忘记将前一个或下一个ptr设置为NULL。
在这里我修复了一点逻辑,并摆脱了段错误(我试图尽可能地评论每个被检查的情况):
#include <iostream>
#include <string.h>
class doubled
{
private:
class sublink
{
private:
char data[30];
sublink *next_ptr;
sublink *previous_ptr;
friend doubled;
};
public:
sublink *first_ptr;
doubled(){first_ptr = NULL;};
void add_item(char *item);
void rm_item(char *item);
};
void doubled::add_item(char *item)
{
sublink *new_data;
new_data = new sublink;
strcpy(new_data->data, item);
// empty list case
if(first_ptr == NULL)
{
// Only item in the list, I have no next or previous element
new_data->previous_ptr = NULL;
new_data->next_ptr = NULL;
// Make the list point to this element
first_ptr = new_data;
}
else
{
sublink* iter;
iter = first_ptr;
// 1 element list
if(iter->next_ptr == NULL)
{
// I'm after the first and only node
if(strcmp(iter->data, new_data->data) <= 0)
{
iter->next_ptr = new_data;
new_data->previous_ptr = iter;
new_data->next_ptr = NULL;
}
// I'm before the first and only node and thefore I become the new first
else
{
iter->previous_ptr = new_data;
new_data->next_ptr = iter;
first_ptr = iter;
}
}
// 2+ element list
else
{
// this is never null the first time because empty list case is take care of above
while(iter != NULL)
{
// Should I be inserted before the current node?
if(strcmp(iter->data, new_data->data) >= 0)
{
// first node case
if(iter->previous_ptr == NULL)
{
new_data->previous_ptr = NULL;
new_data->next_ptr = iter;
iter->previous_ptr = new_data;
first_ptr = new_data;
}
// intermediate node case
else if(iter->next_ptr != NULL)
{
iter->previous_ptr->next_ptr = new_data;
new_data->previous_ptr = iter->previous_ptr;
new_data->next_ptr = iter;
iter->previous_ptr = new_data;
}
// last node case
else
{
iter->next_ptr = new_data;
new_data->previous_ptr = iter;
new_data->next_ptr = NULL;
}
break;
}
// Move to next node
iter = iter->next_ptr;
}
}
}
// Print the list
std::cout << "List: " << std::endl;
sublink* printer = first_ptr;
while(printer != NULL)
{
std::cout << 't' << printer->data << std::endl;
printer = printer->next_ptr;
}
std::cout << std::endl;
}
int main(int argc, char* argv[])
{
doubled d;
char item[30] = "bla bla bla ";
char item2[30] = "meh ";
char item3[30] = "ahhhhhhhh ";
char item4[30] = "dayummmmm ";
d.add_item(item);
d.add_item(item2);
d.add_item(item3);
d.add_item(item4);
std::cin.get();
return 0;
}
你可以在这里看到结果:http://ideone.com/EAzsPZ
相关文章:
- 在这个函数中是有缺陷的,因为取消引用 null 是无效的,所以我想更改代码
- 这种用于查找连续子数组中最大和的递归算法有什么优势吗?
- 我的C++语言蛮力算法有问题
- 当子类需要在 c++ 中相互包含时,继承有缺陷
- 无法打印完整的二叉搜索树,因为我从最低节点向后迭代的逻辑有缺陷
- 这种快速排序算法有什么问题?
- 尝试算法:这个算法有相反的吗?
- 我的记忆动态规划算法有什么问题?
- 关于模板参数推理的看似有缺陷的段落?
- 为什么我的递归快速排序算法有如此不平衡的分区
- 尝试在C++中"blur"矩阵;有缺陷的算法或代码?
- 链接列表,我的逻辑有缺陷
- 在C++中,这个Eigen::张量的声明是安全的,还是有缺陷的?我应该为它提交一个问题吗
- 这种递归算法有什么问题?
- 否则字符逻辑有缺陷
- Qt自定义树模型显示正确,但有缺陷且速度慢
- C++中的合并函数算法有问题
- 有缺陷的 DLL(使用外部函数)
- 3D 'shooter'跳跃算法 - C++低级缺陷。两个相同的双打在应该相等的时候并不相等。4.0 != 4.0 显然
- 双链表插入项算法有缺陷