[英]List inside struct in c++
我在C ++的结构中有一个列表; 我只想像往常一样将元素插入此列表。
我的结构是:
// A structure to represent an adjacency list node
struct AdjListNode
{
int dest;
int weight;
std::list<int> adjacents;
struct AdjListNode* next;
};
// A structure to represent an adjacency list
struct AdjList
{
int pos;
struct AdjListNode *head; // pointer to head node of list
};
// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
struct AdjList* array;
};
struct Graph* createGraph(int V)
{
struct Graph* graph = (struct Graph*) malloc(sizeof(struct Graph));
graph->V = V;
// Create an array of adjacency lists. Size of array will be V
graph->array = (struct AdjList*) malloc(V * sizeof(struct AdjList));
// Initialize each adjacency list as empty by making head as NULL
for (int i = 0; i < V; ++i) {
graph->array[i].head = NULL;
}
return graph;
}
当我尝试让步时:
graph->array[position].head->adjacents->push_back(number);
它只是提示我:
退出代码为139的过程结束(信号11:SIGSEGV中断)
抱歉,我不知道此错误。
分割错误来自
graph->array[position].head->adjacents.push_back(number);
与
graph->array[position].head = NULL;
我想您的代码中具有隐式结构不变性,因为您有两个可能要连接的列表:链表从AdjList::head
开始,并遍历AdjNode::next
和列表AdjNode::adjacent
。
为了保持连接,您可以添加(C样式)函数,在两个列表中添加一个元素。
void
addAdjacent(AdjList& list, int adjacent) {
// struct AdjListNode* newNode = (struct AdjListNode*) malloc(sizeof(struct AdjListNode));
struct AdjListNode* newNode = new AdjListNode;
newNode->next = list.head;
list.head = newNode;
newNode->dest = 0;
newNode->weight = 0;
newNode->adjacents = std::list<int>(); // undefined behavior with malloc
newNode->adjacents.push_back(adjacent);
}
请注意,将C样式(malloc / free)与C ++样式(尤其是与标准模板库的容器)混合使用是一个坏主意。 由于std::list
的字段未填充0,因此代码的注释部分会产生分段错误。
最后,即使有很多内存泄漏,以下main
功能也可以工作(请参见valgrind工具)
int main(int argc, char** argv) {
struct Graph* graph = createGraph(2);
addAdjacent(graph->array[0], 1);
addAdjacent(graph->array[1], 2);
free(graph);
return 0;
}
一个C ++-98解决方案(没有任何内存泄漏)可能是:
// A structure to represent an adjacency list node
struct AdjListNode
{
int dest;
int weight;
std::list<int> adjacents;
struct AdjListNode* next;
AdjListNode() : dest(0), weight(0), next(NULL) {}
};
// A structure to represent an adjacency list
struct AdjList
{
int pos;
struct AdjListNode *head; // pointer to head node of list
// Initialize each adjacency list as empty by making head as NULL
AdjList() : pos(0), head(NULL) {}
~AdjList()
{ while (head) {
struct AdjListNode* temp = head;
head = head->next;
delete temp;
}
}
void addAdjacent(int adjacent)
{ struct AdjListNode* newNode = new AdjListNode;
newNode->next = head;
head = newNode;
newNode->adjacents.push_back(adjacent);
}
};
// A structure to represent a graph. A graph is an array of adjacency lists.
// Size of array will be V (number of vertices in graph)
struct Graph
{
int V;
struct AdjList* array;
// Create an array of adjacency lists. Size of array will be V
Graph(int v) : V(v), array(NULL)
{ if (v >= 0 && v <= 1000)
array = new struct AdjList[v];
else
throw std::bad_alloc();
}
~Graph()
{ delete [] array; }
};
int main() {
struct Graph* graph = new Graph(2);
graph->array[0].addAdjacent(1);
graph->array[1].addAdjacent(1);
delete graph;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.