嵌软面试准备必背代码总结(持续更新中)
目录
目录
字符串相关:
gets
puts
strlen
strcpy
strcat
strcmp
memcpy
atoi
排序类
选择
冒泡
插入
快排(递归)
快排(非递归)*
并归*
二分查找
递归类常见算法
斐波那契数列(前n项和)
斐波那契数列(第n项)
n的阶乘
数据结构
顺序表
链表(单向)
链表(逆序)
链表(循环)
链表(双向循环)
链表(双向)
队列
栈
树
判断链表是否为循环链表(快慢指针法)
大小端判断
联合体法
指针法(1)
指针法(2)
扩展类
线段树
字符串相关:
gets
char * my_gets(char * s)
{
char * ret = s;
while((* s++ = getchar()) != '\n') ;
*--s = '\n';
return ret;
}
puts
int my_puts(const char * s)
{
while(*s != '\0')
{
putchar(*s);
s++;
}
putchar('\n');
return 0;
}
strlen
size_t my_strlen(const char * str)
{
size_t len = 0;
while(*str != '\0')
{
++str;
++len;
}
return len;
}
strcpy
char * my_strcpy(char * dest , const char * src)
{
char * rent = dest;
while((*dest++ = *src++) != '\0') ;
retrun rent;
}
strcat
char * my_strcat(char * dest , const char * src)
{
char * rent = dest;
while (*dest++ != '\0');
dest--;
while ((*dest++ = *src++) != '\0') ;
return rent;
}
strcmp
size_t my_strcmp(const char * str1 , const char * str2)
{
while(*str1 == *str2 && str1 != '\0')
{
str1++;
str2++;
}
return *str1 - *str2;
}
memcpy
void * my_memcpy(void * dest ,const void *src,size_t n)
{
char *q = dest;
const char *p = src;
while (n)
{
*q++ = *p++;
--n;
}
}
atoi
int my_atoi(const char * str)
{
int result = 0;
int sign = 1;
if(*str =='-')
{
sign = -1;
str++;
}
else if(*str == '+')
{
str++;
}
while(*str >= '0' && *str <='9')
{
result = result * 10 + (*str - '0');
str++;
}
return sign* result;
}
排序类
选择
int i,j;
for (i = 0 ; i < n-1 ; i++)
{
for (j = i+1 ; j < n ; j++)
{
if (x[i] > x[j])
swap (&x[i] ,&x[j]);
}
}
冒泡
int i,j;
for (i = 0 ; i < n-1 ; i++)
{
for(j = 0 ; j < n -i -1;j++)
{
if( x[j] > x[j+1])
{
swap(x[j] ,x[j+1]);
}
}
}
插入
for (i = 1 ; i < n ; i++)
{
k=x[i];
j=i-1;
while( j >= 0 && x[j] > k)
{
x[j+1]=x[j];
j=j-1;
}
x[j+1]=k;
}
快排(递归)
void quicksort(int *begin , int *end)
{
int *p = begin;
int *q = end;
int *k = begin;
if(begin >= end)
return ;
while(begin < end)
{
while(begin < end && *end >= *k)
end--;
while(begin < end && *begin <= *k)
begin++;
swap (begin , end);
}
swap (k , begin);
quicksort (p , end-1);
quicksort (begin+1 , q);
}
快排(非递归)*
#include <stdio.h>
#define MAX_STACK_SIZE 100
// 交换两个元素
void swap(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
}
// 分割数组并返回分割点的下标
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // 选择最后一个元素作为基准
int i = (low - 1); // i 记录小于基准的元素的最右位置
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) {
// 创建一个栈用于存储待排序区间的左右边界
int stack[MAX_STACK_SIZE];
int top = -1;
// 将初始区间的左右边界压入栈
stack[++top] = low;
stack[++top] = high;
// 循环直到栈为空
while (top >= 0) {
// 弹出当前待排序区间的左右边界
high = stack[top--];
low = stack[top--];
// 分割数组,并获取分割点的下标
int pivotIndex = partition(arr, low, high);
// 如果分割点的左侧还有元素,将左侧区间的左右边界压入栈
if (pivotIndex - 1 > low) {
stack[++top] = low;
stack[++top] = pivotIndex - 1;
}
// 如果分割点的右侧还有元素,将右侧区间的左右边界压入栈
if (pivotIndex + 1 < high) {
stack[++top] = pivotIndex + 1;
stack[++top] = high;
}
}
}
// 输出数组
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = { 7, 2, 1, 6, 8, 5, 3, 4 };
int size = sizeof(arr) / sizeof(arr[0]);
printf("原始数组:");
printArray(arr, size);
quickSort(arr, 0, size - 1);
printf("排序后数组:");
printArray(arr, size);
return 0;
}
并归*
#include <stdio.h>
void merge(int arr[], int left, int mid, int right) {
int i, j, k;
int n1 = mid - left + 1;
int n2 = right - mid;
// 创建临时数组
int L[n1], R[n2];
// 将数据复制到临时数组
for (i = 0; i < n1; i++) {
L[i] = arr[left + i];
}
for (j = 0; j < n2; j++) {
R[j] = arr[mid + 1 + j];
}
// 归并临时数组到原数组
i = 0;
j = 0;
k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
// 将剩余的元素复制到原数组
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
// 分割数组
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
// 合并数组
merge(arr, left, mid, right);
}
}
void printArray(int arr[], int size) {
int i;
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int size = sizeof(arr) / sizeof(arr[0]);
printf("待排序数组:\n");
printArray(arr, size);
mergeSort(arr, 0, size - 1);
printf("排序后的数组:\n");
printArray(arr, size);
return 0;
}
二分查找
int * binary_search(int * begin,int *end,int y)
{
int *left = begin;
int *right = end;
int *mid = NULL;
while(left <= right)
{
mid = left + (right - left)/2;
if(*mid > y)
right = mid - 1;
else if(*mid < y)
left = mid + 1;
else break;
}
if(left < right)
mid = NULL;
return mid;
}
递归类常见算法
斐波那契数列(前n项和)
int fibonacci_sum(int n) {
int sum = 0;
int a = 0, b = 1;
for (int i = 0; i < n; i++) {
sum += a;
int temp = a + b;
a = b;
b = temp;
}
return sum;
}
斐波那契数列(第n项)
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n-1) + fibonacci(n-2);
}
n的阶乘
int factorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
return n * factorial(n - 1);
}
}
数据结构
顺序表
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int length;
} SeqList;
// 初始化顺序表
void init(SeqList *list) {
list->length = 0;
}
// 插入元素
void insert(SeqList *list, int value, int pos) {
if (pos < 0 || pos > list->length) {
printf("Invalid position!\n");
return;
}
if (list->length >= MAX_SIZE) {
printf("Overflow!\n");
return;
}
for (int i = list->length - 1; i >= pos; i--) {
list->data[i + 1] = list->data[i];
}
list->data[pos] = value;
list->length++;
}
// 删除元素
void remove(SeqList *list, int pos) {
if (pos < 0 || pos >= list->length) {
printf("Invalid position!\n");
return;
}
for (int i = pos + 1; i < list->length; i++) {
list->data[i - 1] = list->data[i];
}
list->length--;
}
// 打印顺序表
void print(SeqList *list) {
if (list->length == 0) {
printf("Empty list!\n");
return;
}
for (int i = 0; i < list->length; i++) {
printf("%d ", list->data[i]);
}
printf("\n");
}
int main() {
SeqList list;
init(&list);
insert(&list, 1, 0);
insert(&list, 3, 1);
insert(&list, 2, 1);
print(&list);
remove(&list, 0);
print(&list);
return 0;
}
链表(单向)
typedef struct node {
DATATYPE data;
struct node *next;
}SLinkNode;
//链表节点//
typedef struct list {
SLinkNode *head;
int clen;
}SLinkList;
//链表//
SLinkList * CreateSLinkList()
{
SLinkList* sll = (SLinkList*) malloc(sizeof(SLinkList));
if(NULL == sll)
{
perror("CreateSLinkList malloc");
return NULL;
}
sll->head = NULL;
sll->clen = 0 ;
return sll;
}
//创建链表//
int IsEmptySLinkList(SLinkList*sll)
{
return 0 == sll->clen ;
}
//判空//
int InsertHeadSLinkList(SLinkList* sll,DATATYPE *data)
{
SLinkNode* newnode = (SLinkNode*)malloc(sizeof(SLinkNode));
if(NULL == sll)
{
perror("inserthead malloc");
return 1;
}
memcpy(&newnode->data ,data,sizeof(DATATYPE));
newnode->next = NULL;
if(IsEmptySLinkList(sll))
{
sll->head = newnode;
}
else
{
newnode->next = sll->head;
sll->head = newnode;
}
sll->clen++;
return 0;
}
//头插//
int ShowSLinkList(SLinkList*sll)
{
SLinkNode* tmp = sll->head ;
while(tmp)
{
printf("name:%s \n",tmp->data.name );
tmp= tmp->next ;//i++
}
return 0;
}
//打印//
SLinkNode * FindSlinkList(SLinkList*sll,FUN fun,void* arg)
{
SLinkNode* tmp = sll->head;
int len = GetSizeSLinkList(sll);
int i = 0;
for(i=0;i<len;i++)
{
//if(0==strcmp(tmp->data.name,name))
if(fun(&tmp->data,arg))
{
// return tmp;
printf("find it; Dname:%s name:%s line: %d path:%s\n",tmp->data.Dname,tmp->data.name,tmp->data.line,tmp->data.path);
}
tmp = tmp->next;
}
//printf("can't find %s\n",want_Dname);
return NULL;
}
//查找//
int GetSizeSLinkList(SLinkList*sll)
{
return sll->clen;
}
//获取长度//
int ModifySlinkList(SLinkList*sll,FUN fun,void* arg,DATATYPE*newdata)
{
SLinkNode* tmp= FindSlinkList(sll,fun, arg);
if(NULL == tmp)
{
fprintf(stderr,"can find\n");
return 1;
}
else
{
memcpy(&tmp->data,newdata,sizeof(DATATYPE));
}
return 0;
}
//替换//
int DeleteSLinkList(SLinkList*sll,FUN fun,void* arg)
{
//待删除节点的前一个指针
SLinkNode*prev = NULL;
SLinkNode*tmp = sll->head;
int len = GetSizeSLinkList(sll);
if(len == 1)
{
free(sll->head);
sll->head = NULL;
sll->clen = 0 ;
return 0;
}
//至少2个节点
while(1)
{
if(fun(&tmp->data,arg))
{
if(prev)
{
prev->next = tmp->next;
}
else
{
sll->head = tmp->next;
}
free(tmp);
sll->clen--;
return 0;
}
prev = tmp;
tmp = tmp->next;
if(NULL == tmp)
{
//删除失败
//return 1;
break;
}
}
return 1;
}
//删除//
int DestroySLinkList(SLinkList *sll) {
SLinkNode *tmp = sll->head;
while (1) {
tmp = sll->head;
if (!tmp)
break;
sll->head = sll->head->next;
free(tmp);
}
free(sll);
return 0;
}
//销毁//
int InserTailSlinkList(SLinkList* sll,DATATYPE *data)
{
SLinkNode* newnode = (SLinkNode*)malloc(sizeof(SLinkNode));
SLinkNode* tmp=sll->head;
if(NULL == sll)
{
perror("inserthead malloc");
return 1;
}
memcpy(&newnode->data ,data,sizeof(DATATYPE));
newnode->next = NULL;
if(IsEmptySLinkList(sll))
{
sll->head = newnode;
}
else
{
while(tmp->next)
tmp=tmp->next;
tmp->next=newnode;
}
sll->clen++;
return 0;
}
//尾插//
int InserPosSlinkList(SLinkList* sll,DATATYPE *data,int pos)
{
int len = GetSizeSLinkList(sll);
if(pos<0 || pos > len)
{
return 1;
}
if(0 == pos)
{
return InsertHeadSLinkList(sll,data);
}
else if(pos == len)
{
return InserTailSlinkList(sll,data);
} else // mid
{
int i = 0;
SLinkNode *prev = sll->head;
for (i = 0; i < pos - 1; i++) {
prev = prev->next;
}
SLinkNode *newnode = malloc(sizeof(SLinkNode));
if (NULL == newnode) {
perror("insert pos malloc");
return 1;
}
memcpy(&newnode->data, data, sizeof(DATATYPE));
newnode->next = NULL;
newnode->next = prev->next;
prev->next = newnode;
}
sll->clen++;
return 0;
}
//位插//
链表(逆序)
// 单向链表逆序
Node* reverseList(Node *head) {
Node *prev = NULL, *cur = head, *next = NULL;
while (cur != NULL) {
next = cur->next;
cur->next = prev;
prev = cur;
cur = next;
}
return prev;
}
链表(循环)
#include <stdio.h>
#include <stdlib.h>
// 定义链表节点结构
typedef struct Node {
int data;
struct Node* next;
} Node;
// 创建新的链表节点
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
// 在链表末尾插入节点
void insertAtEnd(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
newNode->next = *head;
} else {
Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
}
}
// 在链表中间插入节点
void insertAtMid(Node** head, int data, int position) {
Node* newNode = createNode(data);
Node* temp = *head;
for (int i = 0; i < position - 1; i++) {
temp = temp->next;
}
newNode->next = temp->next;
temp->next = newNode;
}
// 在链表头部插入节点
void insertAtBegin(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
newNode->next = *head;
} else {
Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
*head = newNode;
}
}
// 删除链表中指定位置的节点
void deleteAtPosition(Node** head, int position) {
Node* temp = *head;
if (*head == NULL) {
printf("链表为空,无法删除节点。\n");
return;
}
if (position == 1) {
while (temp->next != *head) {
temp = temp->next;
}
if (*head == temp) {
*head = NULL;
free(temp);
return;
}
temp->next = (*head)->next;
free(*head);
*head = temp->next;
} else {
Node* prev;
for (int i = 0; i < position - 1; i++) {
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
free(temp);
}
}
// 打印循环链表
void printList(Node* head) {
Node* temp = head;
if (head != NULL) {
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
}
printf("\n");
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
insertAtEnd(&head, 4);
insertAtBegin(&head, 5);
insertAtMid(&head, 6, 2);
printf("循环链表中的元素:");
printList(head);
deleteAtPosition(&head, 3);
printf("删除节点后的循环链表:");
printList(head);
return 0;
}
链表(双向循环)
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
void insertAtBeginning(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
(*head)->prev = *head;
(*head)->next = *head;
} else {
newNode->next = *head;
newNode->prev = (*head)->prev;
(*head)->prev->next = newNode;
(*head)->prev = newNode;
*head = newNode;
}
}
void insertAtEnd(struct Node** head, int data) {
struct Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
(*head)->prev = *head;
(*head)->next = *head;
} else {
newNode->prev = (*head)->prev;
newNode->next = *head;
(*head)->prev->next = newNode;
(*head)->prev = newNode;
}
}
void deleteFromBeginning(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
if ((*head)->prev == *head) {
free(*head);
*head = NULL;
} else {
struct Node* temp = *head;
(*head)->prev->next = (*head)->next;
(*head)->next->prev = (*head)->prev;
*head = (*head)->next;
free(temp);
}
}
void deleteFromEnd(struct Node** head) {
if (*head == NULL) {
printf("List is empty\n");
return;
}
if ((*head)->prev == *head) {
free(*head);
*head = NULL;
} else {
struct Node* temp = (*head)->prev;
(*head)->prev = (*head)->prev->prev;
(*head)->prev->next = *head;
free(temp);
}
}
void display(struct Node* head) {
if (head == NULL) {
printf("List is empty\n");
return;
}
struct Node* curr = head;
do {
printf("%d ", curr->data);
curr = curr->next;
} while (curr != head);
printf("\n");
}
int main() {
struct Node* head = NULL;
insertAtBeginning(&head, 1);
insertAtBeginning(&head, 2);
insertAtEnd(&head, 3);
insertAtEnd(&head, 4);
printf("List: ");
display(head);
deleteFromBeginning(&head);
printf("List after deleting from beginning: ");
display(head);
deleteFromEnd(&head);
printf("List after deleting from end: ");
display(head);
return 0;
}
链表(双向)
#include <stdio.h>
#include <stdlib.h>
// 双向链表节点结构
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
// 创建一个新节点
Node* newNode(int data) {
Node* node = (Node*)malloc(sizeof(Node));
node->data = data;
node->prev = NULL;
node->next = NULL;
return node;
}
// 在链表尾部插入一个新节点
void append(Node** head, int data) {
Node* node = newNode(data);
if (*head == NULL) {
*head = node;
} else {
Node* curr = *head;
while (curr->next != NULL) {
curr = curr->next;
}
curr->next = node;
node->prev = curr;
}
}
// 在链表头部插入一个新节点
void prepend(Node** head, int data) {
Node* node = newNode(data);
if (*head == NULL) {
*head = node;
} else {
node->next = *head;
(*head)->prev = node;
*head = node;
}
}
// 删除链表中的指定节点
void deleteNode(Node** head, Node* node) {
if (*head == NULL || node == NULL) {
return;
}
if (*head == node) {
*head = node->next;
}
if (node->next != NULL) {
node->next->prev = node->prev;
}
if (node->prev != NULL) {
node->prev->next = node->next;
}
free(node);
}
// 在链表中查找指定数据的节点
Node* search(Node* head, int data) {
Node* curr = head;
while (curr != NULL) {
if (curr->data == data) {
return curr;
}
curr = curr->next;
}
return NULL;
}
// 遍历链表并打印所有节点的值
void printList(Node* head) {
Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
int main() {
Node* head = NULL;
// 在链表尾部插入节点
append(&head, 1);
append(&head, 2);
append(&head, 3);
printList(head); // 输出: 1 2 3
// 在链表头部插入节点
prepend(&head, 0);
printList(head); // 输出: 0 1 2 3
// 删除链表中的节点
Node* node = search(head, 2);
deleteNode(&head, node);
printList(head); // 输出: 0 1 3
return 0;
}
队列
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
typedef struct {
int data[MAX_SIZE];
int front;
int rear;
} Queue;
// 初始化队列
void init(Queue *queue) {
queue->front = 0;
queue->rear = 0;
}
// 入队操作
void enqueue(Queue *queue, int value) {
if ((queue->rear + 1) % MAX_SIZE == queue->front) {
printf("Queue is full!\n");
return;
}
queue->data[queue->rear] = value;
queue->rear = (queue->rear + 1) % MAX_SIZE;
}
// 出队操作
int dequeue(Queue *queue) {
if (queue->rear == queue->front) {
printf("Queue is empty!\n");
return -1; // 返回一个特殊值表示队列为空
}
int value = queue->data[queue->front];
queue->front = (queue->front + 1) % MAX_SIZE;
return value;
}
// 打印队列
void print(Queue *queue) {
if (queue->rear == queue->front) {
printf("Empty queue!\n");
return;
}
printf("Queue: ");
int i = queue->front;
while (i != queue->rear) {
printf("%d ", queue->data[i]);
i = (i + 1) % MAX_SIZE;
}
printf("\n");
}
int main() {
Queue queue;
init(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
print(&queue);
int value = dequeue(&queue);
printf("Dequeued value: %d\n", value);
print(&queue);
return 0;
}
栈
#include <stdio.h>
#define MAX_SIZE 100
int stack[MAX_SIZE];
int top = -1; // 栈顶指针
// 入栈
void push(int data) {
if (top >= MAX_SIZE-1) {
printf("Stack Overflow\n");
return;
}
stack[++top] = data;
}
// 出栈
int pop() {
if (top < 0) {
printf("Stack Underflow\n");
return -1;
}
return stack[top--];
}
// 获取栈顶元素
int peek() {
if (top < 0) {
printf("Stack is empty\n");
return -1;
}
return stack[top];
}
// 判断栈是否为空
int is_empty() {
return (top < 0);
}
int main() {
push(1);
push(2);
push(3);
printf("Top element: %d\n", peek());
printf("Popped element: %d\n", pop());
printf("Is stack empty? %s\n", is_empty() ? "Yes" : "No");
return 0;
}
树
#include <stdio.h>
#include <stdlib.h>
// 定义树的节点
struct TreeNode {
int data;
struct TreeNode* left;
struct TreeNode* right;
};
// 创建新节点
struct TreeNode* createNode(int data) {
struct TreeNode* newNode = (struct TreeNode*)malloc(sizeof(struct TreeNode));
newNode->data = data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
int main() {
// 创建树的节点
struct TreeNode* root = createNode(1);
root->left = createNode(2);
root->right = createNode(3);
root->left->left = createNode(4);
root->left->right = createNode(5);
// 对树进行操作,比如遍历等等
return 0;
}
判断链表是否为循环链表(快慢指针法)
#include <stdio.h>
#include <stdbool.h>
typedef struct ListNode {
int val;
struct ListNode* next;
} ListNode;
bool isCircular(ListNode* head) {
if (head == NULL || head->next == NULL) {
return false;
}
ListNode* slow = head;
ListNode* fast = head->next;
while (fast != NULL && fast->next != NULL) {
if (slow == fast) {
return true;
}
slow = slow->next;
fast = fast->next->next;
}
return false;
}
int main() {
ListNode* head = malloc(sizeof(ListNode));
head->val = 1;
ListNode* node1 = malloc(sizeof(ListNode));
node1->val = 2;
head->next = node1;
ListNode* node2 = malloc(sizeof(ListNode));
node2->val = 3;
node1->next = node2;
ListNode* node3 = malloc(sizeof(ListNode));
node3->val = 4;
node2->next = node3;
// 循环链表
node3->next = head;
bool result = isCircular(head);
printf("Is circular: %s\n", result ? "true" : "false");
return 0;
}
大小端判断
联合体法
#include <stdio.h>
union EndianChecker {
int i;
char c[sizeof(int)];
};
int main() {
union EndianChecker ec;
ec.i = 1;
if (ec.c[0] == 1) {
printf("Little Endian\n");
} else {
printf("Big Endian\n");
}
return 0;
}
指针法(1)
int checkEndianness() {
short int number = 0x1;
char *byte = (char*)&number;
// 如果小端,byte的第一个字节存储低位数据
// 如果大端,byte的第一个字节存储高位数据
if (*byte == 0x1) {
return 1; // 小端
} else {
return 0; // 大端
}
}
指针法(2)
void judge_bigend_littleend()
{
int i = 48;
int *p = &i;
char c = 0;
c = *((char*)p);
if(c == '0')
printf(" 小端\n");
else
printf(" 大端\n");
}
扩展类
线段树
#include <stdio.h>
#include <stdlib.h>
// 定义线段树结点
struct SegmentTreeNode {
int start; // 起始位置
int end; // 结束位置
int max; // 区间内的最大值
struct SegmentTreeNode* left; // 左子节点
struct SegmentTreeNode* right; // 右子节点
};
// 创建一个新的线段树结点
struct SegmentTreeNode* createNode(int start, int end) {
struct SegmentTreeNode* node = (struct SegmentTreeNode*)malloc(sizeof(struct SegmentTreeNode));
node->start = start;
node->end = end;
node->max = 0;
node->left = NULL;
node->right = NULL;
return node;
}
// 构建线段树
struct SegmentTreeNode* buildSegmentTree(int* nums, int start, int end) {
if (start > end) {
return NULL;
}
struct SegmentTreeNode* root = createNode(start, end);
if (start == end) {
root->max = nums[start];
} else {
int mid = start + (end - start) / 2;
root->left = buildSegmentTree(nums, start, mid);
root->right = buildSegmentTree(nums, mid + 1, end);
root->max = (root->left->max > root->right->max) ? root->left->max : root->right->max;
}
return root;
}
// 计算区间的最大值
int query(struct SegmentTreeNode* root, int start, int end) {
if (root == NULL || start > end) {
return -1;
}
if (start == root->start && end == root->end) {
return root->max;
}
int mid = root->start + (root->end - root->start) / 2;
if (end <= mid) {
return query(root->left, start, end);
} else if (start >= mid + 1) {
return query(root->right, start, end);
} else {
int leftMax = query(root->left, start, mid);
int rightMax = query(root->right, mid + 1, end);
return (leftMax > rightMax) ? leftMax : rightMax;
}
}
// 更新结点的值
void update(struct SegmentTreeNode* root, int index, int value) {
if (root == NULL || index < root->start || index > root->end) {
return;
}
if (root->start == index && root->end == index) {
root->max = value;
return;
}
int mid = root->start + (root->end - root->start) / 2;
if (index <= mid) {
update(root->left, index, value);
} else {
update(root->right, index, value);
}
root->max = (root->left->max > root->right->max) ? root->left->max : root->right->max;
}
int main() {
int nums[] = {1, 3, 5, 7, 9, 11};
int size = sizeof(nums) / sizeof(nums[0]);
struct SegmentTreeNode* root = buildSegmentTree(nums, 0, size - 1);
printf("区间[0, 5]的最大值为:%d\n", query(root, 0, 5));
update(root, 3, 8);
printf("更新后的区间[0, 5]的最大值为:%d\n", query(root, 0, 5));
return 0;
}
.....