-2
void main()
{
struct node
{
int num;
struct node *ptr;
};
typedef struct node NODE;
NODE *head, *first, *temp = 0;
int count = 0;
int choice = 1;
first = 0;
while (choice)
{
head = (NODE *)malloc(sizeof(NODE));
printf("Enter the data item\n");
scanf("%d", &head-> num);
if (first != 0) // what the
{ // heck is
temp->ptr = head; // this
temp = head; // piece
} // doing
else // right
{ // here ?
first = temp = head;
}
printf("Do you want to continue(Type 0 or 1)?\n");
scanf("%d", &choice);
}
temp->ptr = 0;
/* reset temp to the beginning */
temp = first;
printf("\n status of the linked list is\n");
while (temp != 0)
{
printf("%d=>", temp->num);
count++;
temp = temp -> ptr;
}
printf("NULL\n");
printf("No. of nodes in the list = %d\n", count);
}
爲什麼第一個被初始化爲0? if else語句在做什麼? 如果我們評論一部分代碼,爲什麼會出現分段錯誤?這段代碼在這裏做什麼?
新用鏈表,所以任何幫助,將不勝感激
你真的在'main()'裏面有結構定義嗎? –
'first = 0;'是初始化空指針常量的糟糕嘗試,所以後面的'if(first!= 0)'由於未初始化的值不應該是UB。 –
關於_poor_部分,它應該是'first = NULL;'並且同樣... –