簡體   English   中英

數據結構中的鏈表

[英]Linked List In Data Structures

假設temp是指向node的指針。 temp->nextNULL 那么temp->next->next的值是多少?

簡而言之, NULL->next的值是什么? 是否因為我在ubuntu和代碼塊(windows)中看到了不同的結果而依賴於編譯器?

下面的程序輸出是什么?

 struct node
 {
  int data;
   struct node *next;
 };

 main()
 {
   struct node *temp,*p;
   int c=0;
   temp=(struct node *)malloc(sizeof(struct node));
   temp->data=50;
   temp->next=NULL;
   p=temp;
   if(p->next->next==NULL)//will it enter the if loop?
      c++;
   printf("%d",c);
  }

如果temp->next為NULL,則對其進行解引用以獲取temp->next->nextundefined behavior 可能會發生崩潰,但是可能會發生其他事情。 原則上, 任何事情都可能發生。

不要取消引用空指針。

NULL-> next必須給您一個段錯誤。

您可能想擁有類似的東西:

if(p->next != NULL && p->next->next==NULL)

要么

if(p->next == NULL || p->next->next==NULL)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM