我想创建一个类似下图的结构,为此我创建了两个结构。一个用于所有有序顶点,另一个用于具有相邻顶点。
struct verticeslist{ //list with all ordered vertices
int n;
struct adjacentlist *pointer; //pointer to the list with adjacent vertices
struct verticeslist *next;
};
struct adjacentlist{ //list with the adjacent vertices
int n;
struct adjacentlist *next;
};
然后使用这些结构,我想用来自用户的特定输入初始化它们。
int main(){
int i, vertices, links, start;
struct verticeslist *lv;
struct adjacentlist *lva;
lv = (struct verticeslist *)malloc(sizeof(struct verticeslist));
scanf("%d %d\n%d", &vertices, &links, &start); //save the values to create the list
for (i=1; i<=vertices; i++){ //create and initialize the list
lv->n = i;
lv = lv->next;
}
while(lv != NULL){ //print the vertices list
printf("%d ", lv->n);
lv = lv->next;
}
return 0;
}
我试过运行这个程序,我得到了seg错误。为什么呢?
编辑:
我改变了我的最后一个代码并理解了什么是错的,但现在我正在尝试构建结构的第二部分而我不知道如何跟踪第一个元素因为我分配了lv->pointer = lva
在一个循环中。程序末尾的lv->pointer
将指向另一个结构的最后一个数字(相邻列表)。
scanf("%d ", &input);
while (input != EOF){
lv = firstvert;
while (lv != NULL){
if(lv->n == input){
scanf("%d\n", &adjacentnum);
lva = (struct adjacentlist *)malloc(sizeof(struct adjacentlist));
lva->n = adjacentnum;
lva->next = NULL;
lv->pointer = lva;
}
lv = lv->next;
}
scanf("%d ", &input);
}
free(firstvert);
free(lv);
free(lva);
return 0;
答案 0 :(得分:1)
for (i=1; i<=vertices; i++){ //create and initialize the list
lv->n = i;
lv = lv->next; // This has to be changed to create verticallist
}
在这里,改变
lv = lv->next;
要
lv->next = (struct verticeslist *)malloc(sizeof(struct verticeslist));
lv = lv->next
并且不要忘记为您的垂直列表创建一个开头,当您打印列表值时(在while循环中),它将用于遍历列表。
答案 1 :(得分:1)
您只在列表中分配了一个节点,您需要为您使用的每个节点分配内存。像这样的东西
for (i=1; i<=vertices; i++){ //create and initialize the list
lv->n = i;
lv->next = (struct verticeslist *)malloc(sizeof(struct verticeslist));
lv = lv->next;
lv->next = 0;
}
编辑以在null旁边添加设置lv-&gt;。否则,当您稍后尝试阅读时,您仍会遇到段错误。
答案 2 :(得分:1)
以下是我认为您应该做的事情,对评论中的每个步骤进行解释。
#include <stdio.h>
#include <stdlib.h>
struct adjacentlist { //list with the adjacent vertices
int n;
struct adjacentlist *next;
};
struct verticeslist { //list with all ordered vertices
int n;
struct adjacentlist *pointer; //pointer to the list with adjacent vertices
struct verticeslist *next;
};
int main(){
int i, vertices, links, start;
struct verticeslist *lv, *lfirstPtr;
struct adjacentlist *lva;
// Note that you aren't using links and start yet, what is the use of them?
scanf("%d %d %d", &vertices, &links, &start);
// Use print to debug when needed or give some feedback on what you did
printf("\nvertices=%d links=%d start=%d", vertices, links, start);
// Always initialize your fields when doing malloc, avoiding trash (non-value assigned)
// being held on your variables. Later they can be used through some if to know if it is
// already initialized ()
// Vertice List - Initialize the first element with 0 pos
lv = (struct verticeslist*)malloc(sizeof( struct verticeslist));
lv->pointer = NULL;
lv->next = NULL;
lv->n = 0;
// Pointer to the first element (never change it, only if you want to remove your first element)
lfirstPtr = lv;
for (i=1; i <= vertices; i++){ //create and initialize the list
// Updates the last element attaching a new allocated space
lv->next = (struct verticeslist*)malloc(sizeof( struct verticeslist));
// Makes lv point to this space
lv = lv->next;
// Fill it
lv->pointer = NULL;
lv->next = NULL;
lv->n = i;
}
// Make it points to the first element and proceed to the loop
// It's good to remember that the last element is pointed through its parent
lv = lfirstPtr;
// note the NULL comparison
while(lv != NULL){ //print the vertices list
printf("\n%d ", lv->n);
// Fill here when you have your adjacency list allocated (similar to what you've seen)
// while (itAdj != NULL) {...}
// Go to the next Vertice position in the list
lv = lv->next;
}
return 0;
}