我遇到了一個問題,我嘗試在鏈表上使用while循環。 我有兩個鏈表,分別爲temp
和。我正在使用while循環來執行任務while(temp != NULL)
。要繼續在每個循環中,我將分配temp = temp->link
。但是,這段代碼不能編譯。我意識到遞歸函數可能是一個解決方案,但函數實際上更復雜,我不認爲遞歸會是一個好主意。順便說一句,已經是一個建立鏈接列表。提前致謝!C使用while循環鏈表
P.S.這是作業的一部分。
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.
}
編輯:
堆棧和圖表是鏈表的兩個陣列:
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的定義。如果你添加編譯器錯誤,它會有所幫助。 – dwalter
'但是,這段代碼不能編譯。 「我不明白這個說法。你沒有任何的麻煩問題。 – banuj