云计算百科
云计算领域专业知识百科平台

链表就这么简单:结构、操作、C语言实现全图解

一、链表是什么?

链表是一种线性数据结构,由一个个节点组成。每个节点包含两部分:

  • 数据域:存放实际数据
  • 指针域:存放下一个节点的地址

就像一列火车:每节车厢装数据,车厢之间用挂钩连接。

节点逻辑结构:

#mermaid-svg-iBSpy9KYusc29RVH{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-iBSpy9KYusc29RVH .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-iBSpy9KYusc29RVH .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-iBSpy9KYusc29RVH .error-icon{fill:#552222;}#mermaid-svg-iBSpy9KYusc29RVH .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-iBSpy9KYusc29RVH .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-iBSpy9KYusc29RVH .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-iBSpy9KYusc29RVH .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-iBSpy9KYusc29RVH .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-iBSpy9KYusc29RVH .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-iBSpy9KYusc29RVH .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-iBSpy9KYusc29RVH .marker{fill:#333333;stroke:#333333;}#mermaid-svg-iBSpy9KYusc29RVH .marker.cross{stroke:#333333;}#mermaid-svg-iBSpy9KYusc29RVH svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-iBSpy9KYusc29RVH p{margin:0;}#mermaid-svg-iBSpy9KYusc29RVH .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-iBSpy9KYusc29RVH .cluster-label text{fill:#333;}#mermaid-svg-iBSpy9KYusc29RVH .cluster-label span{color:#333;}#mermaid-svg-iBSpy9KYusc29RVH .cluster-label span p{background-color:transparent;}#mermaid-svg-iBSpy9KYusc29RVH .label text,#mermaid-svg-iBSpy9KYusc29RVH span{fill:#333;color:#333;}#mermaid-svg-iBSpy9KYusc29RVH .node rect,#mermaid-svg-iBSpy9KYusc29RVH .node circle,#mermaid-svg-iBSpy9KYusc29RVH .node ellipse,#mermaid-svg-iBSpy9KYusc29RVH .node polygon,#mermaid-svg-iBSpy9KYusc29RVH .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-iBSpy9KYusc29RVH .rough-node .label text,#mermaid-svg-iBSpy9KYusc29RVH .node .label text,#mermaid-svg-iBSpy9KYusc29RVH .image-shape .label,#mermaid-svg-iBSpy9KYusc29RVH .icon-shape .label{text-anchor:middle;}#mermaid-svg-iBSpy9KYusc29RVH .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-iBSpy9KYusc29RVH .rough-node .label,#mermaid-svg-iBSpy9KYusc29RVH .node .label,#mermaid-svg-iBSpy9KYusc29RVH .image-shape .label,#mermaid-svg-iBSpy9KYusc29RVH .icon-shape .label{text-align:center;}#mermaid-svg-iBSpy9KYusc29RVH .node.clickable{cursor:pointer;}#mermaid-svg-iBSpy9KYusc29RVH .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-iBSpy9KYusc29RVH .arrowheadPath{fill:#333333;}#mermaid-svg-iBSpy9KYusc29RVH .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-iBSpy9KYusc29RVH .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-iBSpy9KYusc29RVH .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-iBSpy9KYusc29RVH .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-iBSpy9KYusc29RVH .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-iBSpy9KYusc29RVH .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-iBSpy9KYusc29RVH .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-iBSpy9KYusc29RVH .cluster text{fill:#333;}#mermaid-svg-iBSpy9KYusc29RVH .cluster span{color:#333;}#mermaid-svg-iBSpy9KYusc29RVH div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-iBSpy9KYusc29RVH .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-iBSpy9KYusc29RVH rect.text{fill:none;stroke-width:0;}#mermaid-svg-iBSpy9KYusc29RVH .icon-shape,#mermaid-svg-iBSpy9KYusc29RVH .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-iBSpy9KYusc29RVH .icon-shape p,#mermaid-svg-iBSpy9KYusc29RVH .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-iBSpy9KYusc29RVH .icon-shape .label rect,#mermaid-svg-iBSpy9KYusc29RVH .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-iBSpy9KYusc29RVH .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-iBSpy9KYusc29RVH .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-iBSpy9KYusc29RVH :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

数据域 data

指针域 next

单链表整体结构:

#mermaid-svg-yVuJMzSQ6qTiiR12{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-yVuJMzSQ6qTiiR12 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yVuJMzSQ6qTiiR12 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yVuJMzSQ6qTiiR12 .error-icon{fill:#552222;}#mermaid-svg-yVuJMzSQ6qTiiR12 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yVuJMzSQ6qTiiR12 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yVuJMzSQ6qTiiR12 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yVuJMzSQ6qTiiR12 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yVuJMzSQ6qTiiR12 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yVuJMzSQ6qTiiR12 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yVuJMzSQ6qTiiR12 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yVuJMzSQ6qTiiR12 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yVuJMzSQ6qTiiR12 .marker.cross{stroke:#333333;}#mermaid-svg-yVuJMzSQ6qTiiR12 svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yVuJMzSQ6qTiiR12 p{margin:0;}#mermaid-svg-yVuJMzSQ6qTiiR12 .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-yVuJMzSQ6qTiiR12 .cluster-label text{fill:#333;}#mermaid-svg-yVuJMzSQ6qTiiR12 .cluster-label span{color:#333;}#mermaid-svg-yVuJMzSQ6qTiiR12 .cluster-label span p{background-color:transparent;}#mermaid-svg-yVuJMzSQ6qTiiR12 .label text,#mermaid-svg-yVuJMzSQ6qTiiR12 span{fill:#333;color:#333;}#mermaid-svg-yVuJMzSQ6qTiiR12 .node rect,#mermaid-svg-yVuJMzSQ6qTiiR12 .node circle,#mermaid-svg-yVuJMzSQ6qTiiR12 .node ellipse,#mermaid-svg-yVuJMzSQ6qTiiR12 .node polygon,#mermaid-svg-yVuJMzSQ6qTiiR12 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-yVuJMzSQ6qTiiR12 .rough-node .label text,#mermaid-svg-yVuJMzSQ6qTiiR12 .node .label text,#mermaid-svg-yVuJMzSQ6qTiiR12 .image-shape .label,#mermaid-svg-yVuJMzSQ6qTiiR12 .icon-shape .label{text-anchor:middle;}#mermaid-svg-yVuJMzSQ6qTiiR12 .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-yVuJMzSQ6qTiiR12 .rough-node .label,#mermaid-svg-yVuJMzSQ6qTiiR12 .node .label,#mermaid-svg-yVuJMzSQ6qTiiR12 .image-shape .label,#mermaid-svg-yVuJMzSQ6qTiiR12 .icon-shape .label{text-align:center;}#mermaid-svg-yVuJMzSQ6qTiiR12 .node.clickable{cursor:pointer;}#mermaid-svg-yVuJMzSQ6qTiiR12 .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-yVuJMzSQ6qTiiR12 .arrowheadPath{fill:#333333;}#mermaid-svg-yVuJMzSQ6qTiiR12 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-yVuJMzSQ6qTiiR12 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-yVuJMzSQ6qTiiR12 .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yVuJMzSQ6qTiiR12 .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-yVuJMzSQ6qTiiR12 .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yVuJMzSQ6qTiiR12 .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-yVuJMzSQ6qTiiR12 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-yVuJMzSQ6qTiiR12 .cluster text{fill:#333;}#mermaid-svg-yVuJMzSQ6qTiiR12 .cluster span{color:#333;}#mermaid-svg-yVuJMzSQ6qTiiR12 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-yVuJMzSQ6qTiiR12 .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-yVuJMzSQ6qTiiR12 rect.text{fill:none;stroke-width:0;}#mermaid-svg-yVuJMzSQ6qTiiR12 .icon-shape,#mermaid-svg-yVuJMzSQ6qTiiR12 .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yVuJMzSQ6qTiiR12 .icon-shape p,#mermaid-svg-yVuJMzSQ6qTiiR12 .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-yVuJMzSQ6qTiiR12 .icon-shape .label rect,#mermaid-svg-yVuJMzSQ6qTiiR12 .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yVuJMzSQ6qTiiR12 .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-yVuJMzSQ6qTiiR12 .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-yVuJMzSQ6qTiiR12 :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

head

1 | next

2 | next

3 | next

NULL

  • head:头指针,指向第一个节点
  • 最后一个节点的 next 指向 NULL,表示链表结束

二、为什么需要链表?

特性数组链表
随机访问 O(1) O(n)
插入/删除(已知位置) O(n) O(1)
内存分配 需要连续空间 分散,动态分配
额外开销 指针占用空间

结论:频繁插入/删除、数据量动态变化时,链表更有优势;需要快速按下标访问时,数组更好。

三、单链表核心操作图解

1. 插入节点

在节点 p 后插入新节点 s。

插入前:

#mermaid-svg-yOaJRBWV8WB7AVZU{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-yOaJRBWV8WB7AVZU .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-yOaJRBWV8WB7AVZU .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-yOaJRBWV8WB7AVZU .error-icon{fill:#552222;}#mermaid-svg-yOaJRBWV8WB7AVZU .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-yOaJRBWV8WB7AVZU .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-yOaJRBWV8WB7AVZU .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-yOaJRBWV8WB7AVZU .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-yOaJRBWV8WB7AVZU .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-yOaJRBWV8WB7AVZU .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-yOaJRBWV8WB7AVZU .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-yOaJRBWV8WB7AVZU .marker{fill:#333333;stroke:#333333;}#mermaid-svg-yOaJRBWV8WB7AVZU .marker.cross{stroke:#333333;}#mermaid-svg-yOaJRBWV8WB7AVZU svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-yOaJRBWV8WB7AVZU p{margin:0;}#mermaid-svg-yOaJRBWV8WB7AVZU .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-yOaJRBWV8WB7AVZU .cluster-label text{fill:#333;}#mermaid-svg-yOaJRBWV8WB7AVZU .cluster-label span{color:#333;}#mermaid-svg-yOaJRBWV8WB7AVZU .cluster-label span p{background-color:transparent;}#mermaid-svg-yOaJRBWV8WB7AVZU .label text,#mermaid-svg-yOaJRBWV8WB7AVZU span{fill:#333;color:#333;}#mermaid-svg-yOaJRBWV8WB7AVZU .node rect,#mermaid-svg-yOaJRBWV8WB7AVZU .node circle,#mermaid-svg-yOaJRBWV8WB7AVZU .node ellipse,#mermaid-svg-yOaJRBWV8WB7AVZU .node polygon,#mermaid-svg-yOaJRBWV8WB7AVZU .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-yOaJRBWV8WB7AVZU .rough-node .label text,#mermaid-svg-yOaJRBWV8WB7AVZU .node .label text,#mermaid-svg-yOaJRBWV8WB7AVZU .image-shape .label,#mermaid-svg-yOaJRBWV8WB7AVZU .icon-shape .label{text-anchor:middle;}#mermaid-svg-yOaJRBWV8WB7AVZU .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-yOaJRBWV8WB7AVZU .rough-node .label,#mermaid-svg-yOaJRBWV8WB7AVZU .node .label,#mermaid-svg-yOaJRBWV8WB7AVZU .image-shape .label,#mermaid-svg-yOaJRBWV8WB7AVZU .icon-shape .label{text-align:center;}#mermaid-svg-yOaJRBWV8WB7AVZU .node.clickable{cursor:pointer;}#mermaid-svg-yOaJRBWV8WB7AVZU .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-yOaJRBWV8WB7AVZU .arrowheadPath{fill:#333333;}#mermaid-svg-yOaJRBWV8WB7AVZU .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-yOaJRBWV8WB7AVZU .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-yOaJRBWV8WB7AVZU .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yOaJRBWV8WB7AVZU .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-yOaJRBWV8WB7AVZU .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yOaJRBWV8WB7AVZU .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-yOaJRBWV8WB7AVZU .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-yOaJRBWV8WB7AVZU .cluster text{fill:#333;}#mermaid-svg-yOaJRBWV8WB7AVZU .cluster span{color:#333;}#mermaid-svg-yOaJRBWV8WB7AVZU div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-yOaJRBWV8WB7AVZU .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-yOaJRBWV8WB7AVZU rect.text{fill:none;stroke-width:0;}#mermaid-svg-yOaJRBWV8WB7AVZU .icon-shape,#mermaid-svg-yOaJRBWV8WB7AVZU .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-yOaJRBWV8WB7AVZU .icon-shape p,#mermaid-svg-yOaJRBWV8WB7AVZU .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-yOaJRBWV8WB7AVZU .icon-shape .label rect,#mermaid-svg-yOaJRBWV8WB7AVZU .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-yOaJRBWV8WB7AVZU .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-yOaJRBWV8WB7AVZU .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-yOaJRBWV8WB7AVZU :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

p

q

插入后:

#mermaid-svg-NZdaAZ5hlJyZj6PF{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-NZdaAZ5hlJyZj6PF .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-NZdaAZ5hlJyZj6PF .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-NZdaAZ5hlJyZj6PF .error-icon{fill:#552222;}#mermaid-svg-NZdaAZ5hlJyZj6PF .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-NZdaAZ5hlJyZj6PF .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-NZdaAZ5hlJyZj6PF .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-NZdaAZ5hlJyZj6PF .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-NZdaAZ5hlJyZj6PF .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-NZdaAZ5hlJyZj6PF .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-NZdaAZ5hlJyZj6PF .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-NZdaAZ5hlJyZj6PF .marker{fill:#333333;stroke:#333333;}#mermaid-svg-NZdaAZ5hlJyZj6PF .marker.cross{stroke:#333333;}#mermaid-svg-NZdaAZ5hlJyZj6PF svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-NZdaAZ5hlJyZj6PF p{margin:0;}#mermaid-svg-NZdaAZ5hlJyZj6PF .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-NZdaAZ5hlJyZj6PF .cluster-label text{fill:#333;}#mermaid-svg-NZdaAZ5hlJyZj6PF .cluster-label span{color:#333;}#mermaid-svg-NZdaAZ5hlJyZj6PF .cluster-label span p{background-color:transparent;}#mermaid-svg-NZdaAZ5hlJyZj6PF .label text,#mermaid-svg-NZdaAZ5hlJyZj6PF span{fill:#333;color:#333;}#mermaid-svg-NZdaAZ5hlJyZj6PF .node rect,#mermaid-svg-NZdaAZ5hlJyZj6PF .node circle,#mermaid-svg-NZdaAZ5hlJyZj6PF .node ellipse,#mermaid-svg-NZdaAZ5hlJyZj6PF .node polygon,#mermaid-svg-NZdaAZ5hlJyZj6PF .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-NZdaAZ5hlJyZj6PF .rough-node .label text,#mermaid-svg-NZdaAZ5hlJyZj6PF .node .label text,#mermaid-svg-NZdaAZ5hlJyZj6PF .image-shape .label,#mermaid-svg-NZdaAZ5hlJyZj6PF .icon-shape .label{text-anchor:middle;}#mermaid-svg-NZdaAZ5hlJyZj6PF .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-NZdaAZ5hlJyZj6PF .rough-node .label,#mermaid-svg-NZdaAZ5hlJyZj6PF .node .label,#mermaid-svg-NZdaAZ5hlJyZj6PF .image-shape .label,#mermaid-svg-NZdaAZ5hlJyZj6PF .icon-shape .label{text-align:center;}#mermaid-svg-NZdaAZ5hlJyZj6PF .node.clickable{cursor:pointer;}#mermaid-svg-NZdaAZ5hlJyZj6PF .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-NZdaAZ5hlJyZj6PF .arrowheadPath{fill:#333333;}#mermaid-svg-NZdaAZ5hlJyZj6PF .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-NZdaAZ5hlJyZj6PF .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-NZdaAZ5hlJyZj6PF .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-NZdaAZ5hlJyZj6PF .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-NZdaAZ5hlJyZj6PF .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-NZdaAZ5hlJyZj6PF .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-NZdaAZ5hlJyZj6PF .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-NZdaAZ5hlJyZj6PF .cluster text{fill:#333;}#mermaid-svg-NZdaAZ5hlJyZj6PF .cluster span{color:#333;}#mermaid-svg-NZdaAZ5hlJyZj6PF div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-NZdaAZ5hlJyZj6PF .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-NZdaAZ5hlJyZj6PF rect.text{fill:none;stroke-width:0;}#mermaid-svg-NZdaAZ5hlJyZj6PF .icon-shape,#mermaid-svg-NZdaAZ5hlJyZj6PF .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-NZdaAZ5hlJyZj6PF .icon-shape p,#mermaid-svg-NZdaAZ5hlJyZj6PF .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-NZdaAZ5hlJyZj6PF .icon-shape .label rect,#mermaid-svg-NZdaAZ5hlJyZj6PF .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-NZdaAZ5hlJyZj6PF .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-NZdaAZ5hlJyZj6PF .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-NZdaAZ5hlJyZj6PF :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

p

s

q

关键步骤(C语言):

s->next = p->next; // 先让 s 指向 q
p->next = s; // 再让 p 指向 s

顺序不能反!如果先 p->next = s,就会丢失 q 的地址。

2. 删除节点

删除节点 p 后面的节点 q。

删除前:

#mermaid-svg-Mf89NrFIpCPux0aN{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-Mf89NrFIpCPux0aN .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-Mf89NrFIpCPux0aN .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-Mf89NrFIpCPux0aN .error-icon{fill:#552222;}#mermaid-svg-Mf89NrFIpCPux0aN .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-Mf89NrFIpCPux0aN .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-Mf89NrFIpCPux0aN .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-Mf89NrFIpCPux0aN .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-Mf89NrFIpCPux0aN .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-Mf89NrFIpCPux0aN .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-Mf89NrFIpCPux0aN .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-Mf89NrFIpCPux0aN .marker{fill:#333333;stroke:#333333;}#mermaid-svg-Mf89NrFIpCPux0aN .marker.cross{stroke:#333333;}#mermaid-svg-Mf89NrFIpCPux0aN svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-Mf89NrFIpCPux0aN p{margin:0;}#mermaid-svg-Mf89NrFIpCPux0aN .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-Mf89NrFIpCPux0aN .cluster-label text{fill:#333;}#mermaid-svg-Mf89NrFIpCPux0aN .cluster-label span{color:#333;}#mermaid-svg-Mf89NrFIpCPux0aN .cluster-label span p{background-color:transparent;}#mermaid-svg-Mf89NrFIpCPux0aN .label text,#mermaid-svg-Mf89NrFIpCPux0aN span{fill:#333;color:#333;}#mermaid-svg-Mf89NrFIpCPux0aN .node rect,#mermaid-svg-Mf89NrFIpCPux0aN .node circle,#mermaid-svg-Mf89NrFIpCPux0aN .node ellipse,#mermaid-svg-Mf89NrFIpCPux0aN .node polygon,#mermaid-svg-Mf89NrFIpCPux0aN .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-Mf89NrFIpCPux0aN .rough-node .label text,#mermaid-svg-Mf89NrFIpCPux0aN .node .label text,#mermaid-svg-Mf89NrFIpCPux0aN .image-shape .label,#mermaid-svg-Mf89NrFIpCPux0aN .icon-shape .label{text-anchor:middle;}#mermaid-svg-Mf89NrFIpCPux0aN .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-Mf89NrFIpCPux0aN .rough-node .label,#mermaid-svg-Mf89NrFIpCPux0aN .node .label,#mermaid-svg-Mf89NrFIpCPux0aN .image-shape .label,#mermaid-svg-Mf89NrFIpCPux0aN .icon-shape .label{text-align:center;}#mermaid-svg-Mf89NrFIpCPux0aN .node.clickable{cursor:pointer;}#mermaid-svg-Mf89NrFIpCPux0aN .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-Mf89NrFIpCPux0aN .arrowheadPath{fill:#333333;}#mermaid-svg-Mf89NrFIpCPux0aN .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-Mf89NrFIpCPux0aN .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-Mf89NrFIpCPux0aN .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Mf89NrFIpCPux0aN .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-Mf89NrFIpCPux0aN .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Mf89NrFIpCPux0aN .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-Mf89NrFIpCPux0aN .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-Mf89NrFIpCPux0aN .cluster text{fill:#333;}#mermaid-svg-Mf89NrFIpCPux0aN .cluster span{color:#333;}#mermaid-svg-Mf89NrFIpCPux0aN div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-Mf89NrFIpCPux0aN .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-Mf89NrFIpCPux0aN rect.text{fill:none;stroke-width:0;}#mermaid-svg-Mf89NrFIpCPux0aN .icon-shape,#mermaid-svg-Mf89NrFIpCPux0aN .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-Mf89NrFIpCPux0aN .icon-shape p,#mermaid-svg-Mf89NrFIpCPux0aN .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-Mf89NrFIpCPux0aN .icon-shape .label rect,#mermaid-svg-Mf89NrFIpCPux0aN .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-Mf89NrFIpCPux0aN .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-Mf89NrFIpCPux0aN .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-Mf89NrFIpCPux0aN :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

p

q

r

删除后:

#mermaid-svg-bs2xBJldF6AEMHkS{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-bs2xBJldF6AEMHkS .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-bs2xBJldF6AEMHkS .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-bs2xBJldF6AEMHkS .error-icon{fill:#552222;}#mermaid-svg-bs2xBJldF6AEMHkS .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-bs2xBJldF6AEMHkS .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-bs2xBJldF6AEMHkS .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-bs2xBJldF6AEMHkS .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-bs2xBJldF6AEMHkS .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-bs2xBJldF6AEMHkS .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-bs2xBJldF6AEMHkS .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-bs2xBJldF6AEMHkS .marker{fill:#333333;stroke:#333333;}#mermaid-svg-bs2xBJldF6AEMHkS .marker.cross{stroke:#333333;}#mermaid-svg-bs2xBJldF6AEMHkS svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-bs2xBJldF6AEMHkS p{margin:0;}#mermaid-svg-bs2xBJldF6AEMHkS .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-bs2xBJldF6AEMHkS .cluster-label text{fill:#333;}#mermaid-svg-bs2xBJldF6AEMHkS .cluster-label span{color:#333;}#mermaid-svg-bs2xBJldF6AEMHkS .cluster-label span p{background-color:transparent;}#mermaid-svg-bs2xBJldF6AEMHkS .label text,#mermaid-svg-bs2xBJldF6AEMHkS span{fill:#333;color:#333;}#mermaid-svg-bs2xBJldF6AEMHkS .node rect,#mermaid-svg-bs2xBJldF6AEMHkS .node circle,#mermaid-svg-bs2xBJldF6AEMHkS .node ellipse,#mermaid-svg-bs2xBJldF6AEMHkS .node polygon,#mermaid-svg-bs2xBJldF6AEMHkS .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-bs2xBJldF6AEMHkS .rough-node .label text,#mermaid-svg-bs2xBJldF6AEMHkS .node .label text,#mermaid-svg-bs2xBJldF6AEMHkS .image-shape .label,#mermaid-svg-bs2xBJldF6AEMHkS .icon-shape .label{text-anchor:middle;}#mermaid-svg-bs2xBJldF6AEMHkS .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-bs2xBJldF6AEMHkS .rough-node .label,#mermaid-svg-bs2xBJldF6AEMHkS .node .label,#mermaid-svg-bs2xBJldF6AEMHkS .image-shape .label,#mermaid-svg-bs2xBJldF6AEMHkS .icon-shape .label{text-align:center;}#mermaid-svg-bs2xBJldF6AEMHkS .node.clickable{cursor:pointer;}#mermaid-svg-bs2xBJldF6AEMHkS .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-bs2xBJldF6AEMHkS .arrowheadPath{fill:#333333;}#mermaid-svg-bs2xBJldF6AEMHkS .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-bs2xBJldF6AEMHkS .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-bs2xBJldF6AEMHkS .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-bs2xBJldF6AEMHkS .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-bs2xBJldF6AEMHkS .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-bs2xBJldF6AEMHkS .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-bs2xBJldF6AEMHkS .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-bs2xBJldF6AEMHkS .cluster text{fill:#333;}#mermaid-svg-bs2xBJldF6AEMHkS .cluster span{color:#333;}#mermaid-svg-bs2xBJldF6AEMHkS div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-bs2xBJldF6AEMHkS .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-bs2xBJldF6AEMHkS rect.text{fill:none;stroke-width:0;}#mermaid-svg-bs2xBJldF6AEMHkS .icon-shape,#mermaid-svg-bs2xBJldF6AEMHkS .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-bs2xBJldF6AEMHkS .icon-shape p,#mermaid-svg-bs2xBJldF6AEMHkS .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-bs2xBJldF6AEMHkS .icon-shape .label rect,#mermaid-svg-bs2xBJldF6AEMHkS .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-bs2xBJldF6AEMHkS .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-bs2xBJldF6AEMHkS .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-bs2xBJldF6AEMHkS :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

p

r

关键步骤(C语言):

p->next = q->next; // 让 p 直接指向 r
free(q); // 释放 q 的内存

3. 查找节点

只能从头指针开始,沿着 next 依次遍历,直到找到目标或遇到 NULL。时间复杂度 O(n)。

四、常见链表变种

双向链表

每个节点有两个指针:prev 指向前一个节点,next 指向后一个节点。

#mermaid-svg-CqMUBWFX9SSfezjV{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-CqMUBWFX9SSfezjV .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-CqMUBWFX9SSfezjV .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-CqMUBWFX9SSfezjV .error-icon{fill:#552222;}#mermaid-svg-CqMUBWFX9SSfezjV .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-CqMUBWFX9SSfezjV .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-CqMUBWFX9SSfezjV .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-CqMUBWFX9SSfezjV .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-CqMUBWFX9SSfezjV .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-CqMUBWFX9SSfezjV .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-CqMUBWFX9SSfezjV .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-CqMUBWFX9SSfezjV .marker{fill:#333333;stroke:#333333;}#mermaid-svg-CqMUBWFX9SSfezjV .marker.cross{stroke:#333333;}#mermaid-svg-CqMUBWFX9SSfezjV svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-CqMUBWFX9SSfezjV p{margin:0;}#mermaid-svg-CqMUBWFX9SSfezjV .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-CqMUBWFX9SSfezjV .cluster-label text{fill:#333;}#mermaid-svg-CqMUBWFX9SSfezjV .cluster-label span{color:#333;}#mermaid-svg-CqMUBWFX9SSfezjV .cluster-label span p{background-color:transparent;}#mermaid-svg-CqMUBWFX9SSfezjV .label text,#mermaid-svg-CqMUBWFX9SSfezjV span{fill:#333;color:#333;}#mermaid-svg-CqMUBWFX9SSfezjV .node rect,#mermaid-svg-CqMUBWFX9SSfezjV .node circle,#mermaid-svg-CqMUBWFX9SSfezjV .node ellipse,#mermaid-svg-CqMUBWFX9SSfezjV .node polygon,#mermaid-svg-CqMUBWFX9SSfezjV .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-CqMUBWFX9SSfezjV .rough-node .label text,#mermaid-svg-CqMUBWFX9SSfezjV .node .label text,#mermaid-svg-CqMUBWFX9SSfezjV .image-shape .label,#mermaid-svg-CqMUBWFX9SSfezjV .icon-shape .label{text-anchor:middle;}#mermaid-svg-CqMUBWFX9SSfezjV .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-CqMUBWFX9SSfezjV .rough-node .label,#mermaid-svg-CqMUBWFX9SSfezjV .node .label,#mermaid-svg-CqMUBWFX9SSfezjV .image-shape .label,#mermaid-svg-CqMUBWFX9SSfezjV .icon-shape .label{text-align:center;}#mermaid-svg-CqMUBWFX9SSfezjV .node.clickable{cursor:pointer;}#mermaid-svg-CqMUBWFX9SSfezjV .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-CqMUBWFX9SSfezjV .arrowheadPath{fill:#333333;}#mermaid-svg-CqMUBWFX9SSfezjV .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-CqMUBWFX9SSfezjV .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-CqMUBWFX9SSfezjV .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-CqMUBWFX9SSfezjV .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-CqMUBWFX9SSfezjV .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-CqMUBWFX9SSfezjV .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-CqMUBWFX9SSfezjV .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-CqMUBWFX9SSfezjV .cluster text{fill:#333;}#mermaid-svg-CqMUBWFX9SSfezjV .cluster span{color:#333;}#mermaid-svg-CqMUBWFX9SSfezjV div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-CqMUBWFX9SSfezjV .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-CqMUBWFX9SSfezjV rect.text{fill:none;stroke-width:0;}#mermaid-svg-CqMUBWFX9SSfezjV .icon-shape,#mermaid-svg-CqMUBWFX9SSfezjV .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-CqMUBWFX9SSfezjV .icon-shape p,#mermaid-svg-CqMUBWFX9SSfezjV .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-CqMUBWFX9SSfezjV .icon-shape .label rect,#mermaid-svg-CqMUBWFX9SSfezjV .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-CqMUBWFX9SSfezjV .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-CqMUBWFX9SSfezjV .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-CqMUBWFX9SSfezjV :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

A

B

C

优点:可以双向遍历,删除时不需要额外找前驱。

循环链表

尾节点的 next 指向头节点,形成一个环。

#mermaid-svg-08YjmQMuLcGfiwlF{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-08YjmQMuLcGfiwlF .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-08YjmQMuLcGfiwlF .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-08YjmQMuLcGfiwlF .error-icon{fill:#552222;}#mermaid-svg-08YjmQMuLcGfiwlF .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-08YjmQMuLcGfiwlF .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-08YjmQMuLcGfiwlF .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-08YjmQMuLcGfiwlF .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-08YjmQMuLcGfiwlF .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-08YjmQMuLcGfiwlF .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-08YjmQMuLcGfiwlF .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-08YjmQMuLcGfiwlF .marker{fill:#333333;stroke:#333333;}#mermaid-svg-08YjmQMuLcGfiwlF .marker.cross{stroke:#333333;}#mermaid-svg-08YjmQMuLcGfiwlF svg{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-08YjmQMuLcGfiwlF p{margin:0;}#mermaid-svg-08YjmQMuLcGfiwlF .label{font-family:\”trebuchet ms\”,verdana,arial,sans-serif;color:#333;}#mermaid-svg-08YjmQMuLcGfiwlF .cluster-label text{fill:#333;}#mermaid-svg-08YjmQMuLcGfiwlF .cluster-label span{color:#333;}#mermaid-svg-08YjmQMuLcGfiwlF .cluster-label span p{background-color:transparent;}#mermaid-svg-08YjmQMuLcGfiwlF .label text,#mermaid-svg-08YjmQMuLcGfiwlF span{fill:#333;color:#333;}#mermaid-svg-08YjmQMuLcGfiwlF .node rect,#mermaid-svg-08YjmQMuLcGfiwlF .node circle,#mermaid-svg-08YjmQMuLcGfiwlF .node ellipse,#mermaid-svg-08YjmQMuLcGfiwlF .node polygon,#mermaid-svg-08YjmQMuLcGfiwlF .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-08YjmQMuLcGfiwlF .rough-node .label text,#mermaid-svg-08YjmQMuLcGfiwlF .node .label text,#mermaid-svg-08YjmQMuLcGfiwlF .image-shape .label,#mermaid-svg-08YjmQMuLcGfiwlF .icon-shape .label{text-anchor:middle;}#mermaid-svg-08YjmQMuLcGfiwlF .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-08YjmQMuLcGfiwlF .rough-node .label,#mermaid-svg-08YjmQMuLcGfiwlF .node .label,#mermaid-svg-08YjmQMuLcGfiwlF .image-shape .label,#mermaid-svg-08YjmQMuLcGfiwlF .icon-shape .label{text-align:center;}#mermaid-svg-08YjmQMuLcGfiwlF .node.clickable{cursor:pointer;}#mermaid-svg-08YjmQMuLcGfiwlF .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-08YjmQMuLcGfiwlF .arrowheadPath{fill:#333333;}#mermaid-svg-08YjmQMuLcGfiwlF .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-08YjmQMuLcGfiwlF .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-08YjmQMuLcGfiwlF .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-08YjmQMuLcGfiwlF .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-08YjmQMuLcGfiwlF .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-08YjmQMuLcGfiwlF .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-08YjmQMuLcGfiwlF .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-08YjmQMuLcGfiwlF .cluster text{fill:#333;}#mermaid-svg-08YjmQMuLcGfiwlF .cluster span{color:#333;}#mermaid-svg-08YjmQMuLcGfiwlF div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\”trebuchet ms\”,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-08YjmQMuLcGfiwlF .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-08YjmQMuLcGfiwlF rect.text{fill:none;stroke-width:0;}#mermaid-svg-08YjmQMuLcGfiwlF .icon-shape,#mermaid-svg-08YjmQMuLcGfiwlF .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-08YjmQMuLcGfiwlF .icon-shape p,#mermaid-svg-08YjmQMuLcGfiwlF .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-08YjmQMuLcGfiwlF .icon-shape .label rect,#mermaid-svg-08YjmQMuLcGfiwlF .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-08YjmQMuLcGfiwlF .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-08YjmQMuLcGfiwlF .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-08YjmQMuLcGfiwlF :root{–mermaid-font-family:\”trebuchet ms\”,verdana,arial,sans-serif;}

A

B

C

五、C语言代码实现

1. 节点结构体定义

#include <stdio.h>
#include <stdlib.h>

// 节点结构体
typedef struct Node {
int data; // 数据域
struct Node* next; // 指针域
} Node;

2. 创建新节点

// 创建新节点
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("内存分配失败\\n");
exit(1);
}
newNode->data = data;
newNode->next = NULL;
return newNode;
}

3. 尾部添加节点

// 在链表尾部添加节点
void append(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
return;
}
Node* cur = *head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = newNode;
}

4. 在指定节点后插入

// 在指定节点 prev 后插入新节点
void insertAfter(Node* prev, int data) {
if (prev == NULL) {
printf("前驱节点不能为空\\n");
return;
}
Node* newNode = createNode(data);
newNode->next = prev->next;
prev->next = newNode;
}

5. 删除第一个值为 key 的节点

// 删除第一个值为 key 的节点
void deleteNode(Node** head, int key) {
Node* cur = *head;
Node* prev = NULL;

// 如果头节点就是要删除的节点
if (cur != NULL && cur->data == key) {
*head = cur->next;
free(cur);
return;
}

// 查找要删除的节点
while (cur != NULL && cur->data != key) {
prev = cur;
cur = cur->next;
}

// 如果没找到
if (cur == NULL) {
printf("未找到值为 %d 的节点\\n", key);
return;
}

// 删除节点
prev->next = cur->next;
free(cur);
}

6. 打印链表

// 打印链表
void printList(Node* head) {
Node* cur = head;
while (cur != NULL) {
printf("%d -> ", cur->data);
cur = cur->next;
}
printf("NULL\\n");
}

7. 释放链表内存

// 释放整个链表
void freeList(Node** head) {
Node* cur = *head;
Node* temp;
while (cur != NULL) {
temp = cur;
cur = cur->next;
free(temp);
}
*head = NULL;
}

8. 测试代码

int main() {
Node* head = NULL;

append(&head, 1);
append(&head, 2);
append(&head, 3);
printList(head); // 1 -> 2 -> 3 -> NULL

insertAfter(head, 99);
printList(head); // 1 -> 99 -> 2 -> 3 -> NULL

deleteNode(&head, 2);
printList(head); // 1 -> 99 -> 3 -> NULL

freeList(&head);
return 0;
}

运行结果

1 -> 2 -> 3 -> NULL
1 -> 99 -> 2 -> 3 -> NULL
1 -> 99 -> 3 -> NULL

六、总结

  • 链表 = 节点 + 指针,节点包含数据和指向下一个节点的指针
  • 插入/删除快(O(1),已知位置),查找慢(O(n))
  • 适合频繁增删、数据量动态变化的场景
  • C语言实现需手动管理内存(malloc / free)
  • 常见变种:双向链表、循环链表

掌握链表是学习更复杂数据结构(栈、队列、图、哈希表等)的基础。希望这篇图解能帮你快速理解!


如果你觉得有帮助,欢迎点赞、收藏、评论,让更多人看到!

赞(0)
未经允许不得转载:网硕互联帮助中心 » 链表就这么简单:结构、操作、C语言实现全图解
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!