无法通过一个单向链表数组创建邻接列表
Failed to create adjacency list via one array of singly linked list
我正在尝试通过一个单向链表数组创建邻接列表。
请参阅我的代码。
#include<iostream>
#include<cstdlib>
using namespace std;
typedef struct city
{
int id;
struct city *next;
}city;
int main()
{
int num_city, index = 0, length;
cin >> num_city;
length = num_city;
city **adj_list = new city*[num_city]; // here it's the header node
for(int index = 0 ; index < length ; index++)
adj_list[index] = new city;
city **temp = adj_list;
while( num_city -- )
{
int a,b;
cin >> a;
cin >> b;
a--;
b--;
city *t1 = new city;
t1 -> id = a;
t1 -> next = NULL;
city *t2 = new city;
t2 -> id = b;
t2 -> next = NULL;
temp[a] -> next = t2;
temp[b] -> next = t1;
temp[a] = temp[a] -> next;
temp[b] = temp[b] -> next;
}
for ( int index = 0; index < length ; index ++)
delete [] adj_list[index];
delete [] adj_list;
adj_list = NULL;
exit(0);
}
当我尝试逐个访问单向链表时,它的输出是 NULL
.
GDB
这段代码后,我发现:启动循环,city
创建成功,adj_list[index]
也可以指向正确的内存位置。一旦进入下一个循环,adj_list[index]
等于出乎意料地NULL
。
怎么了?
有两个问题。您正在使用temp
来跟踪每个列表中的最后一项。但是temp
指向原始adj_list
(它是adj_list
而不是副本),因此结果是adj_list
本身仅保留您在每个列表中添加的最后一项。如果将初始adj_list
复制到 temp
中,则更新temp
不会影响 adj_list
中的现有项目。
第二个问题是,邻居的对数可能多于城市的数量。以 5 个城市为例:(2,3)、(2,4)、(2,5)、(2,1)、(1,5)、(4,5)、(3,4)。所以你不能循环num_city
.相反,继续循环,直到用户输入城市-1
。
我添加了一个构造函数city( int id, city* next )
,以便更轻松地初始化每个新城市。 temp
被重命名为tail
,以更清楚地说明其目的。该功能分为 3 个部分,构建、打印和删除。
typedef struct city
{
int id;
struct city *next;
city(int id, city *next) { this->id = id; this->next = next; }
} city;
// pass `adj_list` in as a reference (city **&) so you keep the changes to it when you return
int adj_list_build(city **& adj_list)
{
int num_city, index = 0;
cin >> num_city;
// you need to keep track of the "tail" (end) of each list so you know where to add next item
city **tail = new city*[num_city];
adj_list = new city*[num_city];
for (int index = 0; index < num_city; index++)
{
adj_list[index] = new city( index, NULL );
tail[index] = adj_list[index]; // the tail starts off pointing to the first item in each list
}
while (true)
{
int a, b;
cin >> a; // enter `-1` to stop
if (a == -1 ) break;
cin >> b;
a--; // convert to 0-index
b--;
city *t1 = new city( a, NULL );
city *t2 = new city( b, NULL );
tail[a]->next = t2;
tail[b]->next = t1;
tail[a] = t2; // or `tail[a] = tail[a]->next;` - they have same effect
tail[b] = t1;
}
return num_city;
}
void adj_list_print(city ** adj_list, int length)
{
for (int index = 0; index < length; index++)
{
cout << index << ": ";
city * item = adj_list[index];
while (item)
{
cout << item->id << " " << item->next << " ";
item = item->next;
}
cout << endl;
}
}
void adj_list_delete(city ** adj_list, int length)
{
for (int index = 0; index < length; index++)
delete[] adj_list[index];
delete[] adj_list;
adj_list = NULL;
}
void main()
{
city ** adj_list;
int len = adj_list_build( adj_list );
adj_list_print(adj_list, len);
adj_list_delete(adj_list, len);
}
相关文章:
- C++,指针数组,指向双链表中的条目
- 实现链表数组
- 将链表转换为指针数组时出错
- 将数组的元素添加到链表中
- 将链表的内容放在数组引用中
- 你能在链表中有一个动态数组和它的内存分配吗
- 递归解决方案,用于显示线性链表数组
- 链表数组(5 个队列)
- 有没有办法将最初作为字符串输入的数字字符数组复制到双向链表中
- C++ 在给定像素数据数组的表单上绘制矩形
- 从链表数组访问节点数据
- 单个或单个头用于链表的动态数组
- 如果链表比数组具有许多功能优势,那么数组与链表相比有什么优势?
- 警告:数组"字母表"由括号字符串文本初始化
- 使用数组创建表
- 追加在 C++ 中的数组链表中不起作用
- 定义一个非常大的数组(查找表)的最佳方式是什么
- const extern c数组链接器多重定义
- 牌组(链表)(节点)洗牌
- 在数组链表中使用一个嵌套循环从一个位置迭代到另一个位置