掌握C语言遍历技巧,轻松应对复杂数据处理

在C语言编程中,遍历是处理复杂数据结构的基础技能。无论是数组、链表、树还是图,遍历都是对这些数据结构进行操作的前提。熟练掌握C语言的遍历技巧,能够帮助我们更高效地处理数据,解决实际问题。
一、数组遍历
数组是C语言中最基本的数据结构之一,其遍历通常使用for循环实现。
#include

int main() {
int array[] = {1, 2, 3, 4, 5};
int length = sizeof(array) / sizeof(array[0]);

for (int i = 0; i < length; i++) { printf("%d ", array[i]); } printf("\n"); return 0; } 二、链表遍历 链表是一种灵活的数据结构,遍历链表需要使用指针。 #include
#include

typedef struct Node {
int data;
struct Node* next;
} Node;

void traverseList(Node* head) {
Node* current = head;
while (current != NULL) {
printf(“%d “, current->data);
current = current->next;
}
printf(“\n”);
}

int main() {
Node* head = (Node*)malloc(sizeof(Node));
head->data = 1;
head->next = (Node*)malloc(sizeof(Node));
head->next->data = 2;
head->next->next = (Node*)malloc(sizeof(Node));
head->next->next->data = 3;

traverseList(head);

// 释放内存
free(head->next->next);
free(head->next);
free(head);

return 0;
}
三、树遍历
树是一种层次结构,遍历树通常有三种方法:前序遍历、中序遍历和后序遍历。
#include
#include

typedef struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
} TreeNode;

void preorder(TreeNode* node) {
if (node == NULL) {
return;
}
printf(“%d “, node->data);
preorder(node->left);
preorder(node->right);
}

void inorder(TreeNode* node) {
if (node == NULL) {
return;
}
inorder(node->left);
printf(“%d “, node->data);
inorder(node->right);
}

void postorder(TreeNode* node) {
if (node == NULL) {
return;
}
postorder(node->left);
postorder(node->right);
printf(“%d “, node->data);
}

int main() {
TreeNode* root = (TreeNode*)malloc(sizeof(TreeNode));
root->data = 1;
root->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->data = 2;
root->right = (TreeNode*)malloc(sizeof(TreeNode));
root->right->data = 3;

root->left->left = (TreeNode*)malloc(sizeof(TreeNode));
root->left->left->data = 4;
root->left->right = (TreeNode*)malloc(sizeof(TreeNode));
root->left->right->data = 5;

printf(“Preorder traversal: “);
preorder(root);
printf(“\n”);

printf(“Inorder traversal: “);
inorder(root);
printf(“\n”);

printf(“Postorder traversal: “);
postorder(root);
printf(“\n”);

// 释放内存
free(root->left->right);
free(root->left->left);
free(root->left);
free(root->right);
free(root);

return 0;
}
四、图遍历
图是一种复杂的数据结构,遍历图通常有深度优先遍历(DFS)和广度优先遍历(BFS)两种方法。
#include
#include

typedef struct Graph {
int numVertices;
int** adjMatrix;
} Graph;

void dfs(Graph* graph, int vertex, int visited[]) {
visited[vertex] = 1;
printf(“%d “, vertex);

for (int i = 0; i < graph->numVertices; i++) {
if (graph->adjMatrix[vertex][i] && !visited[i]) {
dfs(graph, i, visited);
}
}
}

void bfs(Graph* graph, int startVertex) {
int visited[graph->numVertices] = {0};
int queue[graph->numVertices];
int front = 0, rear = -1;

visited[startVertex] = 1;
queue[++rear] = startVertex;

while (front <= rear) { int currentVertex = queue[front++]; printf("%d ", currentVertex); for (int i = 0; i < graph->numVertices; i++) {
if (graph->adjMatrix[currentVertex][i] && !visited[i]) {
visited[i] = 1;
queue[++rear] = i;
}
}
}
}

int main() {
int numVertices = 4;
Graph* graph = (Graph*)malloc(sizeof(Graph));
graph->numVertices = numVertices;
graph->adjMatrix = (int**)malloc(numVertices * sizeof(int*));

for (int i = 0; i < numVertices; i++) { graph->adjMatrix[i] = (int*)malloc(numVertices * sizeof(int));
for (int j = 0; j < numVertices; j++) { graph->adjMatrix[i][j] = 0;
}
}

// 构建图
graph->adjMatrix[0][1] = 1;
graph->adjMatrix[0][2] = 1;
graph->adjMatrix[1][2] = 1;
graph->adjMatrix[2][3] = 1;

printf(“DFS traversal: “);
int visited[numVertices] = {0};
dfs(graph, 0, visited);
printf(“\n”);

printf(“BFS traversal: “);
bfs(graph, 0);
printf(“\n”);

// 释放内存
for (int i = 0; i < numVertices; i++) { free(graph->adjMatrix[i]);
}
free(graph->adjMatrix);
free(graph);

return 0;
}
通过以上示例,我们可以看到C语言在遍历各种数据结构方面的强大能力。熟练掌握这些遍历技巧,将有助于我们更好地处理复杂数据,解决实际问题。