链表有一系列节点组成,一个节点有两个域:数据域,指针域(保存下一个节点地址),
链表在内存中是非连续的。
可以依据指针随机插入、删除元素。
image.png

静态链表

  1. struct LinkNode{
  2. int data;
  3. struct LinkNode *next;
  4. };
  5. void test(){
  6. struct LinkNode node1 = {1, NULL};
  7. struct LinkNode node2 = { 2, NULL};
  8. struct LinkNode node3 = { 3, null};
  9. struct LinkNode node4 = { 4, null};
  10. node1.next = &node2;
  11. node2.next = &node3;
  12. node3.next = &node4;
  13. // 遍历链表
  14. struct LinkNode *Pcurrent = &node1;
  15. while(Pcurrent != NULL){
  16. printf("%d ", Pcurrent->data);
  17. Pcurrent = Pcurrent->next;
  18. }
  19. }
  20. int main(){
  21. test();
  22. return 0;
  23. }