


Preview text:
Hàm Tìm Kiếm Node* p = l->pHead;
Node* search(List* l, int x) { do {
if (l->pHead == NULL) return NULL; // Danh sách n++; rỗng p = p->pNext; Node* p = l->pHead; } while (p != l->pHead); do {
if (p->data == x) return p; // Tìm thấy giá trị // Tìm kiếm nhị phân p = p->pNext; int low = 0, high = n - 1;
} while (p != l->pHead); // Duyệt toàn bộ vòng while (low <= high) {
return NULL; // Không tìm thấy int mid = (low + high) / 2; }
Xóa Sau Một Node
// Duyệt đến node ở vị trí mid
int removeAfter(List* l, Node* q) { p = l->pHead;
if (q == NULL || q->pNext == NULL) return 0; //
for (int i = 0; i < mid; i++) p = p->pNext; Không hợp lệ Node* p = q->pNext;
if (p->data == x) return p; if (p->data < x) if (p == q) { low = mid + 1;
// Danh sách chỉ có một phần tử else
l->pHead = l->pTail = NULL; high = mid - 1; } else { } q->pNext = p->pNext; if (p == l->pTail) {
return NULL; // Không tìm thấy
l->pTail = q; // Cập nhật lại node cuối } } Sắp xếp }
1. Hàm hoán đổi dữ liệu giữa hai node free(p); void swap(Node* a, Node* b) { return 1; int temp = a->data; } a->data = b->data;
Tìm kiếm tuyến tính b->data = temp;
Node* linearSearch(List* l, int x) { }
if (l->pHead == NULL) return NULL; // Danh sách
Interchange Sort rỗng
void interchangeSort(List* l) {
if (l->pHead == NULL) return; Node* p = l->pHead; do { Node* p = l->pHead;
if (p->data == x) return p; // Tìm thấy giá trị do { p = p->pNext; Node* q = p->pNext; } while (p != l->pHead); while (q != l->pHead) {
if (p->data > q->data) swap(p, q);
return NULL; // Không tìm thấy q = q->pNext; } }
Tìm kiếm nhị phân p = p->pNext;
Node* binarySearch(List* l, int x) { } while (p != l->pHead);
if (l->pHead == NULL) return NULL; } Bubble Sort // Đếm số lượng node void bubbleSort(List* l) { int n = 0;
if (l->pHead == NULL) return;
while (temp->pNext != NULL && temp->pNext- int swapped;
>data < current->data) { do { temp = temp->pNext; swapped = 0; } Node* p = l->pHead;
current->pNext = temp->pNext; do { temp->pNext = current;
if (p->data > p->pNext->data) { } swap(p, p->pNext); swapped = 1; current = next; }
} while (current != l->pHead); p = p->pNext;
} while (p->pNext != l->pHead); l->pHead = sorted; } while (swapped); } }
kiểm tra số nguyên tố Selection Sort int isPrime(int n) { void selectionSort(List* l) { if (n < 2) return 0;
if (l->pHead == NULL) return;
for (int i = 2; i * i <= n; i++) { if (n % i == 0) return 0; Node* p = l->pHead; } do { return 1; Node* min = p; } Node* q = p->pNext;
kiểm tra số chính phương while (q != l->pHead) { int isPerfectSquare(int n) {
if (q->data < min->data) min = q; int root = sqrt(n); q = q->pNext; return root * root == n; } } if (min != p) swap(p, min);
kiểm tra số đảo p = p->pNext; int isReverse(int n) { } while (p != l->pHead);
int original = n, reversed = 0; } while (n > 0) { Insertion Sort
reversed = reversed * 10 + n % 10; void insertionSort(List* l) { n /= 10;
if (l->pHead == NULL) return; } return original == reversed;
Node* sorted = NULL; // Danh sách đã sắp xếp } Node* current = l->pHead;
Tìm và xuất các số nguyên tố void printPrimes(List* l) { do {
if (l->pHead == NULL) return;
Node* next = current->pNext; Node* p = l->pHead;
// Chèn node hiện tại vào danh sách đã sắp xếp do {
if (sorted == NULL || sorted->data >= current- if (isPrime(p->data)) { >data) { printf("%d ", p->data); current->pNext = sorted; } sorted = current; p = p->pNext; } else { } while (p != l->pHead); Node* temp = sorted; printf("\n"); }
Tìm và xuất các số chính phương
void printPerfectSquares(List* l) {
if (l->pHead == NULL) return; Node* p = l->pHead; do {
if (isPerfectSquare(p->data)) { printf("%d ", p->data); } p = p->pNext; } while (p != l->pHead); printf("\n"); }
Tìm và xuất các số đảo
void printReverses(List* l) {
if (l->pHead == NULL) return; Node* p = l->pHead; do { if (isReverse(p->data)) { printf("%d ", p->data); } p = p->pNext; } while (p != l->pHead); printf("\n"); }
kiểm tra số chẵn int isEven(int n) { return n % 2 == 0; }
Tìm và xuất các số chẵn
void printEvens(List* l) { if (l->pHead == NULL) {
printf("Danh sách rỗng!\n"); return; } Node* p = l->pHead;
printf("Các số chẵn trong danh sách: "); do { if (isEven(p->data)) { printf("%d ", p->data); } p = p->pNext; } while (p != l->pHead); printf("\n"); }