即時嘗試添加節點在鏈接列表的開始,但在我的代碼中,它只顯示最後一個元素,我只輸入最後一個元素。問題的原因是什麼顯示功能鏈接列表代碼不工作
#include <stdio.h>
//node structure
struct node
{
int data;
struct node *next;
};
//結構
//datatype declaration
typedef struct node node ;
// head pointer which will indicate starting point of link list
node *head;
//create fuction that will insert values into note and its next pointer field
void create(int num);
//display function will display the link list
void display();
main()
{
int num,i,n;
printf("enter the nno of node to create : ");
scanf("%d",&n);
for(i=0;i<n;++i)
{
printf("enter data for node %d= ",i+1);
scanf("%d",&num);
create(num);
}
display();
//顯示呼叫
}
void create(int num)
{
head=NULL;
node *temp;
temp=(node*)malloc(sizeof(node));
temp->data=num;
temp->next=head;
head=temp;
return;
}
//函數創建()端
void display()
{
node *temp1;
temp1=head;
while(temp1!=NULL)
{
printf("data : %d-> ",temp1->data);
temp1=temp1->next;
}
return;
}
//溫控功能顯示()端