引言
C言語作為一種歷史長久且功能富強的編程言語,在數據構造與算法的進修跟利用中扮演着重要角色。數據構造是構造跟管理數據的方法,而算法則是處理成績的明白指令。本文將深刻探究C言語中的數據構造與算法利用,幫助讀者更好地懂得跟控制這一範疇。
數據構造基本
2.1 數組
2.1.1 定義跟初始化數組
數組是一種基本的數據構造,用於存儲存在雷同數據範例的元素序列。在C言語中,數組經由過程持續的內存空間來存儲數據。
int arr[5] = {1, 2, 3, 4, 5};
2.1.2 數組的讀取跟修改
可能經由過程索引來拜訪跟修改數組中的元素。
int value = arr[2]; // 讀取第3個元素的值
arr[3] = 10; // 修改第4個元素的值為10
2.1.3 數組的罕見操縱:查找、拔出、刪除
數組支撐查找、拔出跟刪除操縱,但拔出跟刪除操縱可能涉及大年夜量元素的挪動。
// 查找元素
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];
}
2.2 鏈表
鏈表是一種靜態的數據構造,由一系列節點構成,每個節點包含數據跟指向下一個節點的指針。
2.2.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;
}
2.2.2 雙鏈表
雙鏈表是單鏈表的擴大年夜,每個節點包含數據跟指向下一個及前一個節點的指針。
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;
}
2.2.3 輪回鏈表
輪回鏈表是鏈表的另一種情勢,最後一個節點的指針指向鏈表的第一個節點。
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; // 輪回鏈表
}
2.3 棧跟行列
2.3.1 棧的實現跟利用
棧是一種掉落隊先出(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--];
}
2.3.2 行列的實現跟利用
行列是一種進步先出(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++];
}
2.4 樹
2.4.1 二叉樹
二叉樹是一種罕見的樹形數據構造,每個節點最多有兩個子節點。
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;
}
}
}
2.4.2 均衡二叉樹
均衡二叉樹是一種特其余二叉樹,其閣下子樹的高度差不超越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;
}
2.4.3 堆
堆是一種特其余完全二叉樹,用於實現優先行列等利用。
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);
}
2.5 圖
2.5.1 圖的表示方法
圖是一種用於表示東西及其之間關係的抽象數據構造。
#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; // 無向圖
}
2.5.2 圖的遍歷算法
圖的遍歷算法包含深度優先遍歷(DFS)跟廣度優先遍歷(BFS)。
void DFS(Graph* graph, int vertex) {
// 利用遞歸或棧實現
}
void BFS(Graph* graph, int startVertex) {
// 利用行列實現
}
算法計劃與分析
3.1 排序算法
3.1.1 冒泡排序
冒泡排序是一種簡單的排序算法,經由過程比較相鄰元素並交換它們的次序來對數組停止排序。
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]);
}
}
}
}
3.1.2 拔出排序
拔出排序是一種簡單直不雅的排序算法,它的任務道理是經由過程構建有序序列,對未排序數據,在已排序序列中從後向前掃描,找到響應地位並拔出。
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;
}
}
3.1.3 抉擇排序
抉擇排序是一種簡單直不雅的排序算法,它的任務道理是從待排序的數據當選出最小(或最大年夜)元素,存放到序列的肇端地位,然後,再從剩餘未排序元素中持續尋覓最小(或最大年夜)元素,然後放到已排序序列的末端。
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]);
}
}
3.1.4 疾速排序
疾速排序是一種分而治之的排序算法,其基本頭腦是抉擇一個基準元素,然後將數組分為兩部分,一部分比基準元素小,另一部分比基準元素大年夜,然後遞歸地對這兩部分停止疾速排序。
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);
}
}
3.2 查找算法
3.2.1 次序查找
次序查找是一種簡單直不雅的查找算法,它的任務道理是從數組的第一個元素開端,壹壹元素地與要查找的元素停止比較,直到找到為止。
int sequentialSearch(int arr[], int n, int x) {
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
return i;
}
}
return -1;
}