-4
我想從列表中使用尾部poninter鏈接列表的末尾添加數字,但我不明白爲什麼我的尾巴永遠不會改變。我想從鏈表中添加數字從列表中結束
struct node
{
int data;
struct node *next;
}*head , *tail;
typedef struct node NOD;
//I addd the first node
void addfirst(int num)
{
NOD *temp;//This is the new node
temp = (NOD*)malloc(sizeof(NOD));
temp->data = num;
temp->next = NULL;
head = tail = temp;
}
//I add at the end of the list
void add(int num)
{
NOD *temp;
temp = (NOD*)malloc(sizeof(NOD));
temp->data = num;
temp->next = NULL;
tail->next = temp;
tail = temp;
}
int main()
{
int n , num, i;
freopen("intrare.txt" , "r" , stdin);
scanf("%d" , &n);
for(i = 0 ; i < n ; i++)
{
scanf("%d" , &num);
if(i==1)
addfirst(num);
else
add(num);
}
return 0;
}
'if(i == 1)' - >'if(i == 0)' – BLUEPIXY
這看起來像一個答案 – Radinator