C言语作为一种历史长久且功能富强的编程言语,在数据构造与算法的进修跟利用中扮演侧重要角色。数据构造是构造跟管理数据的方法,而算法则是处理成绩的明白指令。本文将深刻探究C言语中的数据构造与算法利用,帮助读者更好地懂得跟控制这一范畴。
数组是一种基本的数据构造,用于存储存在雷同数据范例的元素序列。在C言语中,数组经由过程持续的内存空间来存储数据。
int arr[5] = {1, 2, 3, 4, 5};
可能经由过程索引来拜访跟修改数组中的元素。
int value = arr[2]; // 读取第3个元素的值
arr[3] = 10; // 修改第4个元素的值为10
数组支撑查找、拔出跟删除操纵,但拔出跟删除操纵可能涉及大年夜量元素的挪动。
// 查找元素
int index = -1;
for (int i = 0; i < sizeof(arr) / sizeof(arr[0]); i++) {
if (arr[i] == target) {
index = i;
break;
}
}
// 拔出元素
for (int i = sizeof(arr) / sizeof(arr[0]); i > index; i--) {
arr[i] = arr[i - 1];
}
arr[index] = newValue;
// 删除元素
for (int i = index; i < sizeof(arr) / sizeof(arr[0]) - 1; i++) {
arr[i] = arr[i + 1];
}
链表是一种静态的数据构造,由一系列节点构成,每个节点包含数据跟指向下一个节点的指针。
单链表是最简单的链心情势,每个节点只包含数据跟指向下一个节点的指针。
struct Node {
int data;
struct Node* next;
};
void insertAtHead(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
*head = newNode;
}
双链表是单链表的扩大年夜,每个节点包含数据跟指向下一个及前一个节点的指针。
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
void insertAtHead(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
newNode->prev = NULL;
if (*head != NULL) {
(*head)->prev = newNode;
}
*head = newNode;
}
轮回链表是链表的另一种情势,最后一个节点的指针指向链表的第一个节点。
struct Node {
int data;
struct Node* next;
};
void insertAtHead(struct Node** head, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = *head;
if (*head != NULL) {
(*head)->prev = newNode;
}
*head = newNode;
newNode->next = newNode; // 轮回链表
}
栈是一种掉落队先出(LIFO)的数据构造,常用于保存常设变量或函数挪用的情况。
typedef struct Stack {
int top;
int capacity;
int* array;
} Stack;
void push(Stack* stack, int item) {
if (stack->top == stack->capacity - 1) {
return;
}
stack->array[++stack->top] = item;
}
int pop(Stack* stack) {
if (stack->top == -1) {
return -1;
}
return stack->array[stack->top--];
}
行列是一种进步先出(FIFO)的数据构造,常用于任务调理跟缓冲处理。
typedef struct Queue {
int front;
int rear;
int capacity;
int* array;
} Queue;
void enqueue(Queue* queue, int item) {
if (queue->rear == queue->capacity - 1) {
return;
}
queue->array[++queue->rear] = item;
}
int dequeue(Queue* queue) {
if (queue->front == queue->rear) {
return -1;
}
return queue->array[queue->front++];
}
二叉树是一种罕见的树形数据构造,每个节点最多有两个子节点。
struct Node {
int data;
struct Node* left;
struct Node* right;
};
void insert(struct Node** root, int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
if (*root == NULL) {
*root = newNode;
} else {
struct Node* current = *root;
struct Node* parent = NULL;
while (current != NULL) {
parent = current;
if (data < current->data) {
current = current->left;
} else {
current = current->right;
}
}
if (data < parent->data) {
parent->left = newNode;
} else {
parent->right = newNode;
}
}
}
均衡二叉树是一种特其余二叉树,其阁下子树的高度差不超越1。
struct Node {
int data;
struct Node* left;
struct Node* right;
int height;
};
int height(struct Node* N) {
if (N == NULL) {
return 0;
}
return N->height;
}
struct Node* newNode(int data) {
struct Node* node = (struct Node*)malloc(sizeof(struct Node));
node->data = data;
node->height = 1; // 新节点的高度为1
node->left = node->right = NULL;
return node;
}
int max(int a, int b) {
return (a > b) ? a : b;
}
struct Node* rightRotate(struct Node* y) {
struct Node* x = y->left;
struct Node* T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
return x;
}
struct Node* leftRotate(struct Node* x) {
struct Node* y = x->right;
struct Node* T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
return y;
}
int getBalance(struct Node* N) {
if (N == NULL) {
return 0;
}
return height(N->left) - height(N->right);
}
struct Node* insert(struct Node* node, int data) {
if (node == NULL) {
return(newNode(data));
}
if (data < node->data) {
node->left = insert(node->left, data);
} else if (data > node->data) {
node->right = insert(node->right, data);
} else {
return node;
}
node->height = 1 + max(height(node->left), height(node->right));
int balance = getBalance(node);
if (balance > 1 && data < node->left->data) {
return rightRotate(node);
}
if (balance < -1 && data > node->right->data) {
return leftRotate(node);
}
if (balance > 1 && data > node->left->data) {
node->left = leftRotate(node->left);
return rightRotate(node);
}
if (balance < -1 && data < node->right->data) {
node->right = rightRotate(node->right);
return leftRotate(node);
}
return node;
}
堆是一种特其余完全二叉树,用于实现优先行列等利用。
struct MinHeap {
int size;
int capacity;
int* array;
};
void swap(int* a, int* b) {
int t = *a;
*a = *b;
*b = t;
}
void minHeapify(struct MinHeap* minHeap, int idx) {
int smallest = idx;
int left = 2 * idx + 1;
int right = 2 * idx + 2;
if (left < minHeap->size && minHeap->array[left] < minHeap->array[smallest]) {
smallest = left;
}
if (right < minHeap->size && minHeap->array[right] < minHeap->array[smallest]) {
smallest = right;
}
if (smallest != idx) {
swap(&minHeap->array[idx], &minHeap->array[smallest]);
minHeapify(minHeap, smallest);
}
}
void buildMinHeap(struct MinHeap* minHeap) {
int n = minHeap->size - 1;
int i;
for (i = (n - 1) / 2; i >= 0; i--) {
minHeapify(minHeap, i);
}
}
void insertMinHeap(struct MinHeap* minHeap, int item) {
minHeap->array[minHeap->size] = item;
minHeap->size++;
int i = minHeap->size - 1;
while (i && minHeap->array[(i - 1) / 2] > minHeap->array[i]) {
swap(&minHeap->array[i], &minHeap->array[(i - 1) / 2]);
i = (i - 1) / 2;
}
}
void deleteMin(struct MinHeap* minHeap) {
if (minHeap->size <= 0) {
return;
}
if (minHeap->size == 1) {
minHeap->array[0] = 0;
minHeap->size--;
return;
}
minHeap->array[0] = minHeap->array[minHeap->size - 1];
minHeap->size--;
buildMinHeap(minHeap);
}
图是一种用于表示东西及其之间关联的抽象数据构造。
#define MAX_VERTICES 100
typedef struct Graph {
int numVertices;
int** adjMatrix;
} Graph;
Graph* createGraph(int numVertices) {
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;
}
}
return graph;
}
void addEdge(Graph* graph, int src, int dest) {
graph->adjMatrix[src][dest] = 1;
graph->adjMatrix[dest][src] = 1; // 无向图
}
图的遍历算法包含深度优先遍历(DFS)跟广度优先遍历(BFS)。
void DFS(Graph* graph, int vertex) {
// 利用递归或栈实现
}
void BFS(Graph* graph, int startVertex) {
// 利用行列实现
}
冒泡排序是一种简单的排序算法,经由过程比较相邻元素并交换它们的次序来对数组停止排序。
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
swap(&arr[j], &arr[j + 1]);
}
}
}
}
拔出排序是一种简单直不雅的排序算法,它的任务道理是经由过程构建有序序列,对未排序数据,在已排序序列中从后向前扫描,找到响应地位并拔出。
void insertionSort(int arr[], int n) {
int i, key, j;
for (i = 1; i < n; i++) {
key = arr[i];
j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j - 1;
}
arr[j + 1] = key;
}
}
抉择排序是一种简单直不雅的排序算法,它的任务道理是从待排序的数据当选出最小(或最大年夜)元素,存放到序列的肇端地位,然后,再从剩余未排序元素中持续寻觅最小(或最大年夜)元素,然后放到已排序序列的末端。
void selectionSort(int arr[], int n) {
int i, j, min_idx;
for (i = 0; i < n - 1; i++) {
min_idx = i;
for (j = i + 1; j < n; j++)
if (arr[j] < arr[min_idx])
min_idx = j;
swap(&arr[min_idx], &arr[i]);
}
}
疾速排序是一种分而治之的排序算法,其基本头脑是抉择一个基准元素,然后将数组分为两部分,一部分比基准元素小,另一部分比基准元素大年夜,然后递归地对这两部分停止疾速排序。
int partition(int arr[], int low, int high) {
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
次序查找是一种简单直不雅的查找算法,它的任务道理是从数组的第一个元素开端,一一元素地与要查找的元素停止比较,直到找到为止。
int sequentialSearch(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}