遍歷連結串列
阿新 • • 發佈:2021-02-19
1.如果連結串列中的資料不連續,只需要把連結串列中的頭節點拿出來(point=head,定義指標指向連結串列的頭),利用指標,可以遍歷整個連結串列。
2.程式碼實現
#include <stdio.h>
struct Test
{
int data;//定義結構體的資料
struct Test *next;//定義結構體的指標
};
void printlink(struct Test *head)
{
struct Test *point;//對連結串列進行索引
point=head;//讓point等於連結串列的頭
while(1)
{
if(point != NULL)
{
printf("%d ",point->data);
point=point->next;
//讓指標往後走,讓指標遍歷整個連結串列
}
else
{
putchar('\n');
break;
}
}
}
int main( )
{
struct Test t1={1,NULL};
struct Test t2={2,NULL};
struct Test t3={3,NULL};
struct Test t4={4,NULL};
t1.next=&t2;/*t1的下一個存放下一個資料的地址*/
t2.next=&t3;
t3.next=&t4;
printlink(&t1);
return 0;
}
執行結果
——@上官可程式設計