【发布时间】:2023-03-30 23:11:01
【问题描述】:
我遇到了一个问题,我尝试在链表上使用 while 循环。
我有两个链表,即temp 和graph。我正在使用 while 循环来执行任务while(temp != NULL)。为了在每个循环中继续,我分配了temp = temp->link。但是,此代码无法编译。我意识到递归函数可能是一个解决方案,但该函数实际上要复杂得多,我认为递归不是一个好主意。顺便说一句,graph 已经是一个内置的链表。提前致谢!
附:这是家庭作业的一部分。
temp = graph->link;
while(temp!=NULL){
if(stack->link == NULL){
stack->link = (node_pointer)malloc(sizeof(graph));
stack->link->weight = temp->weight;
stack->link->vertex = temp->vertex;
}
temp = temp->link; //Here is the problem.
}
编辑:
stack和graph都是链表的数组:
typedef struct node *node_pointer;
struct node{
int vertex;
int weight;
int visited;
struct node *link;
};
node_pointer graph[50];
node_pointer stack[50];
node_pointer temp;
【问题讨论】:
-
错误信息是什么?
-
您能否至少向我们展示一下 temp、stack 和 graph 的定义。如果您添加编译器错误会有所帮助。
-
However, this code does not compile.我不明白这个说法。您没有任何 sintatic 问题。 -
Unhandled exception at 0x00d716c2 in graph.exe: 0xC0000005: Access violation reading location 0xcdcdcdd9. -
@meany 这不是编译器问题。代码编译得很好
标签: c list while-loop linked-list