所以我使用通過引用在我的鏈表上的代碼,但問題是它不是印刷洙我怎麼才能真正解決這個問題?使用通過鏈接列表的引用傳遞
我的代碼:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int x;
struct node *next;
};
void add(struct node **root, int x)
{
struct node *conductor;
if(root==NULL)
{
(*root)=malloc(sizeof(struct node));
(*root)->x=x;
(*root)->next=NULL ;
}
else
{
conductor = *root;
while(conductor->next!=NULL)
{
conductor = conductor -> next;
}
conductor->next=malloc(sizeof(struct node));
conductor->next->x=x;
conductor->next->next=NULL;
}
}
void display(struct node *root)
{
struct node *conductor;
conductor=root;
while(conductor!=NULL)
{
printf("%d",conductor->x);
conductor = conductor ->next;
}
}
int main()
{
struct node *root;
root=NULL;
add(&root,5);
add(&root,4);
display(root);
free(root);
system("pause");
}
在更好的形式 http://codepad.org/CPdUvK0x
是不是在我的程序的所有節點都聯繫?
請接受您的上一個問題的答案http://stackoverflow.com/questions/5433967/how-to-remove-any-node-in-a-singly-linked-請點擊旁邊的複選標記。 – 2011-03-26 12:59:58
「通過引用傳遞」在C中不存在。但有值傳遞的值是指針。 – BenjaminB 2011-03-26 13:24:06