การท่องผ่านและการค้นหา
ไล่ดูรายการ
การท่องผ่านและการค้นหา เป็นบทเรียน C Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน C Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส C Academy มีบทเรียนทั้งหมด 4 บทเรียน
การเดินไปตามรายการ
การท่องรายการหมายถึงการเยี่ยมชมโหนดแต่ละโหนดตามลำดับ เริ่มจากหัวรายการและติดตามตัวชี้ next จนถึง NULL
อัลกอริทึมเกือบทุกอย่างของรายการสร้างขึ้นจากการเดินแบบง่าย ๆ นี้
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int main(void) {
struct Node *head = make(10);
head->next = make(20);
for (struct Node *p = head; p != NULL; p = p->next)
printf("%d ", p->value);
printf("\n");
return 0;
}รูปแบบการท่องรายการ
ลูปมาตรฐานใช้ตัวชี้ที่เลื่อนไปเรื่อย ๆ คือ p: กำหนดค่าเริ่มต้นเป็น head ทำต่อขณะที่ p ไม่ใช่ NULL และเลื่อนไปข้างหน้าด้วย p = p->next
อย่าแก้ไข head โดยตรงขณะเดินรายการ มิฉะนั้นจะสูญเสียจุดเริ่มต้นของรายการ
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int main(void) {
struct Node *head = make(1);
head->next = make(2);
struct Node *p = head;
while (p) { printf("%d ", p->value); p = p->next; }
printf("\n");
return 0;
}การนับโหนด
หากต้องการหาความยาว ให้เดินไปตามรายการและเพิ่มค่าตัวนับสำหรับโหนดแต่ละโหนด
การทำงานนี้ใช้เวลา O(n) เนื่องจากไม่ได้จัดเก็บจำนวนไว้ที่ใด
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int length(struct Node *head) {
int n = 0;
for (struct Node *p = head; p; p = p->next) n++;
return n;
}
int main(void) {
struct Node *head = make(1);
head->next = make(2);
head->next->next = make(3);
printf("length = %d\n", length(head));
return 0;
}การหาผลรวมค่า
การท่องรายการช่วยให้คุณรวมข้อมูลได้ ในที่นี้เราบวกค่าจำนวนเต็มทั้งหมดในรายการ
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int main(void) {
struct Node *head = make(5);
head->next = make(10);
int sum = 0;
for (struct Node *p = head; p; p = p->next) sum += p->value;
printf("sum = %d\n", sum);
return 0;
}การค้นหาค่า
หากต้องการค้นหาค่า ให้เดินไปตามรายการและเปรียบเทียบโหนดแต่ละโหนด เมื่อพบค่าที่ตรงกัน ให้ส่งคืนโหนดหรือตำแหน่งของโหนด หรือแจ้งว่าค้นหาไม่สำเร็จหากไปถึงจุดสิ้นสุด
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
struct Node *find(struct Node *head, int v) {
for (struct Node *p = head; p; p = p->next)
if (p->value == v) return p;
return NULL;
}
int main(void) {
struct Node *head = make(1);
head->next = make(2);
printf("found 2: %d\n", find(head, 2) != NULL);
printf("found 9: %d\n", find(head, 9) != NULL);
return 0;
}การค้นหาตำแหน่ง
บางครั้งคุณอาจต้องการดัชนีของค่าที่ตรงกันแทนโหนด ให้ใช้ตัวนับขณะเดินรายการ และส่งคืนค่าตัวนับเมื่อพบค่าที่ต้องการ
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int index_of(struct Node *head, int v) {
int i = 0;
for (struct Node *p = head; p; p = p->next, i++)
if (p->value == v) return i;
return -1;
}
int main(void) {
struct Node *head = make(7);
head->next = make(8);
printf("%d\n", index_of(head, 8));
return 0;
}การเข้าถึงโหนดลำดับที่ n
รายการเชื่อมโยงไม่มีการเข้าถึงด้วยดัชนีโดยตรง หากต้องการไปยังตำแหน่ง n คุณต้องเลื่อนจากหัวรายการไป n ครั้ง
ด้วยเหตุนี้ การเข้าถึงแบบสุ่มจึงใช้เวลา O(n) เมื่อเทียบกับ O(1) ของอาร์เรย์
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
struct Node *at(struct Node *head, int n) {
struct Node *p = head;
for (int i = 0; i < n && p; i++) p = p->next;
return p;
}
int main(void) {
struct Node *head = make(10);
head->next = make(20);
head->next->next = make(30);
printf("%d\n", at(head, 2)->value);
return 0;
}การค้นหาโหนดสุดท้าย
หากต้องการหาท้ายรายการ ให้เดินไปจนกว่า p->next จะเป็น NULL โหนดนั้นคือโหนดสุดท้าย
โปรดระวังกรณีรายการว่าง ซึ่ง head เองจะเป็น NULL
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int main(void) {
struct Node *head = make(1);
head->next = make(2);
head->next->next = make(3);
struct Node *p = head;
while (p->next) p = p->next;
printf("last = %d\n", p->value);
return 0;
}การหาค่าสูงสุด
เมื่อผสานการค้นหาเข้ากับการรวมค่า คุณสามารถหาค่าที่มากที่สุดได้โดยติดตามค่าที่ดีที่สุดขณะท่องรายการ
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
int main(void) {
struct Node *head = make(3);
head->next = make(9);
head->next->next = make(5);
int best = head->value;
for (struct Node *p = head->next; p; p = p->next)
if (p->value > best) best = p->value;
printf("max = %d\n", best);
return 0;
}การท่องรายการแบบเวียนเกิด
รายการสามารถท่องแบบเวียนเกิดได้เช่นกัน โดยประมวลผลโหนดปัจจุบัน แล้วเรียกการทำงานซ้ำกับ next
วิธีนี้ดูสวยงาม แต่ใช้พื้นที่สแตกตามสัดส่วนของความยาวรายการ ดังนั้นการวนซ้ำจึงปลอดภัยกว่าสำหรับรายการที่ยาวมาก
#include <stdio.h>
#include <stdlib.h>
struct Node { int value; struct Node *next; };
struct Node *make(int v){struct Node*n=malloc(sizeof*n);n->value=v;n->next=NULL;return n;}
void print_rec(struct Node *p) {
if (!p) { printf("\n"); return; }
printf("%d ", p->value);
print_rec(p->next);
}
int main(void) {
struct Node *head = make(1);
head->next = make(2);
print_rec(head);
return 0;
}รองรับรายการว่าง
ฟังก์ชันสำหรับท่องรายการทุกฟังก์ชันควรจัดการรายการว่าง (head == NULL) ได้อย่างเหมาะสม
ลูปมาตรฐานรองรับกรณีนี้อยู่แล้ว: เงื่อนไข p != NULL จะเป็นเท็จทันที ทำให้ส่วนคำสั่งภายในไม่ทำงาน
#include <stdio.h>
struct Node { int value; struct Node *next; };
int length(struct Node *head) {
int n = 0;
for (struct Node *p = head; p; p = p->next) n++;
return n;
}
int main(void) {
struct Node *head = NULL;
printf("empty length = %d\n", length(head));
return 0;
}ตรวจสอบความเข้าใจอย่างรวดเร็ว
ทดสอบความเข้าใจเกี่ยวกับต้นทุนของการท่องรายการ
สรุปทบทวน
คุณได้เรียนรู้การท่องและค้นหารายการแล้ว:
- รูปแบบการเดินรายการ: เริ่มที่
headวนซ้ำขณะที่ไม่ใช่NULLและเลื่อนไปด้วยp = p->next - การนับ การหาผลรวม และการหาค่าสูงสุด ล้วนสร้างขึ้นจากการท่องรายการ
- การค้นหาเปรียบเทียบโหนดแต่ละโหนด ส่วนการเข้าถึงด้วยดัชนีใช้เวลา O(n)
- การท่องรายการทำแบบเวียนเกิดได้ แต่การวนซ้ำปลอดภัยกว่าสำหรับรายการยาว และต้องจัดการกรณีรายการว่างเสมอ
เรียนรู้ C ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 39
- บทเรียน
- 144
คำถามที่พบบ่อย
บทเรียน “การท่องผ่านและการค้นหา” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การท่องผ่านและการค้นหา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส C Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส C Academy มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การท่องผ่านและการค้นหา”
ไล่ดูรายการ คุณปฏิบัติ C Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน C Academy หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน C Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การท่องผ่านและการค้นหา” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน C Academy นี้ได้ไหม
ได้ บทเรียน C Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- ลิงก์ลิสต์แบบทางเดียว
- การแทรกและการลบ
- การท่องผ่านและการค้นหา
- ลิงก์ลิสต์แบบสองทาง