void MergeList(int *A, int *B, int *C, int ALen, int BLen, int CLen){
int i = 0, j = 0, k = 0;
while (i < ALen && j < BLen){
if (A[i] <= B[j])
C[k++] = A[i++];
else
C[k++] = B[j++];
}
while (i < ALen) C[k++] = A[i++];
while (j < BLen) C[k++] = B[j++];
}
void Reverse(int *A, int Alen){
int mid = Alen/2;
for (int i = 0; i < mid; ++i) {
int temp = A[i];
A[i] = A[Alen - i - 1];
A[Alen - i - 1] = temp;
}
}
void Adjust(int *L, int Llen, int temp){
int help[Llen];
int low = 0;
int high = Llen - 1;
for (int i = 0; i < Llen; ++i)
if (L[i] < temp)
help[low++] = L[i];
else
help[high--] = L[i];
for (int j = 0; j < Llen; ++j)
L[j] = help[j];
}
int MainElement(int *A, int Len){
int *count = (int *)malloc(sizeof(int) * Len);
memset(count, 0, sizeof(int) * Len);
for (int i = 0; i < Len; ++i)
count[A[i]]++;
for (int j = 0; j < Len; ++j)
if (count[j] > (int)(Len/2))
return j;
else return -1;
}
void RotateLeft(int *A, int p, int Len){
int remove = p % Len;
int *copy = (int *)malloc(sizeof(int) * Len);
for (int i = 0; i < Len; ++i)
copy[i] = A[i];
int count = 0;
for (int j = remove; j < Len; ++j)
A[count++] = copy[j];
for (int k = 0; k < remove; ++k)
A[count++] = copy[k];
free(copy);
}
void InitLinkList(struct ListNode *L){
struct ListNode *ptr = L;
ptr->val = INT_MAX;
ptr->next = NULL;
}
void HeadCreatList(struct ListNode *L, int *elemt, int elemtSize){
for (int i = 0; i < elemtSize; ++i) {
struct ListNode *new = (struct ListNode *)malloc(sizeof(struct ListNode));
new->val = elemt[i];
new->next = L->next;
L->next = new;
}
}
void TailCreatList(struct ListNode *L, int *elemt, int elemtSize){
for (int i = 0; i < elemtSize; ++i) {
struct ListNode *new = (struct ListNode *)malloc(sizeof(struct ListNode));
new->val = elemt[i];
new->next = NULL;
L->next = new;
L = L->next;
}
}
void ReverseLinkList(struct ListNode *L){
struct ListNode *ptr, *r;
ptr = L->next;
L->next = NULL;
while (ptr){
r = ptr->next;
ptr->next = L->next;
L->next = ptr;
ptr = r;
}
}
void PrintIncrease(struct ListNode *L){
struct ListNode *cur = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *pre = (struct ListNode *)malloc(sizeof(struct ListNode));
while (L->next != NULL){
cur = L->next;
pre = L->next;
int temp = INT_MAX;
while (cur->next != NULL){
cur = cur->next;
if (cur->val < temp)
temp = cur->val;
}
if (L->next->next == NULL){
printf("%d", L->next->val);
struct ListNode *re = (struct ListNode *)malloc(sizeof(struct ListNode));
L->next = NULL;
free(re);
free(pre);
free(cur);
}
while (pre->next->val != temp)
pre = pre->next;
printf("%d ", temp);
struct ListNode *s = (struct ListNode *)malloc(sizeof(struct ListNode));
s = pre->next;
pre->next = s->next;
free(s);
}
}
void DeleteKeyNode(struct ListNode *L, int data){
struct ListNode *cur = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *pre = (struct ListNode *)malloc(sizeof(struct ListNode));
_Bool isChange = 0;
while (!isChange) {
cur = L->next;
pre = L;
while (cur != NULL)
if (cur->val == data) {
isChange = 1;
break;
}else{
cur = cur->next;
}
if (!isChange)
return;
while (pre->next->val != data)
pre = pre->next;
struct ListNode *s = (struct ListNode *) malloc(sizeof(struct ListNode));
s = pre->next;
pre->next = s->next;
free(s);
isChange = 0;
}
}
void RemoveDuplicates(struct ListNode *L) {
struct ListNode *cur = (struct ListNode *) malloc(sizeof(struct ListNode));
struct ListNode *pre = (struct ListNode *) malloc(sizeof(struct ListNode));
cur = L->next->next;
pre = L->next;
if (cur == NULL)
return;
while (cur) {
if (cur->val == pre->val) {
struct ListNode *dis = (struct ListNode *) malloc(sizeof(struct ListNode));
dis = cur;
cur = cur->next;
pre->next = cur;
free(dis);
} else {
cur = cur->next;
pre = pre->next;
}
}
}
void DeleteInterValue(struct ListNode *L, int left, int right){
struct ListNode *pre = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *cur = (struct ListNode *)malloc(sizeof(struct ListNode));
pre = L;
cur = pre->next;
while (cur){
if (cur->val > left && cur->val < right){
struct ListNode *dis = (struct ListNode *)malloc(sizeof(struct ListNode));
dis = cur;
cur = cur->next;
pre->next = cur;
free(dis);
} else{
cur = cur->next;
pre = pre->next;
}
}
}
void SplitLinkList(struct ListNode *A, struct ListNode *B, struct ListNode *C){
struct ListNode *cur1 = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *cur2 = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *cur3 = (struct ListNode *)malloc(sizeof(struct ListNode));
cur1 = A->next;
cur2 = B;
cur3 = C;
int count = 1;
while (cur1){
struct ListNode *node = (struct ListNode *)malloc(sizeof(struct ListNode));
node->next = NULL;
node->val = cur1->val;
if (count % 2){
cur2->next = node;
cur2 = cur2->next;
} else{
cur3->next = node;
cur3 = cur3->next;
}
cur1 = cur1->next;
count++;
}
}
struct ListNode* IncreaseAndDecrease(struct ListNode *A, struct ListNode *B){
struct ListNode *cur = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *curA = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *curB = (struct ListNode *)malloc(sizeof(struct ListNode));
curA = A->next;
curB = B->next;
A->next = NULL;
while (curA && curB){
if (curA->val <= curB->val){
cur = curA->next;
curA->next = A->next;
A->next = curA;
curA = cur;
} else{
cur = curB->next;
curB->next = A->next;
A->next = curB;
curB = cur;
}
}
if (curA)
curB = curA;
while (curB){
cur = curB->next;
curB->next = A->next;
A->next = curB;
curB = cur;
}
free(B);
}
void PublicBuildList(struct ListNode *A, struct ListNode *B, struct ListNode *C, int ALen, int BLen){
struct ListNode *curTemp = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *curOther = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *curRes = (struct ListNode *)malloc(sizeof(struct ListNode));
curRes = C;
if (ALen <= BLen){
curTemp = A->next;
curOther = B->next;
}else{
curTemp = B->next;
curOther = A->next;
}
while (curTemp && curOther){
if (curTemp->val < curOther->val)
curTemp = curTemp->next;
else if(curTemp->val > curOther->val)
curOther = curOther->next;
else{
struct ListNode *res = (struct ListNode *)malloc(sizeof(struct ListNode));
res->val = curTemp->val;
res->next = NULL;
curRes->next = res;
curRes = curRes->next;
curTemp = curTemp->next;
curOther = curOther->next;
}
}
}
int IsSub(struct ListNode *A, struct ListNode *B, int ALen, int BLen){
struct ListNode *curOri = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *curSub = (struct ListNode *)malloc(sizeof(struct ListNode));
if (ALen <= BLen){
curOri = B->next;
curSub = A->next;
} else{
curOri = A->next;
curSub = B->next;
}
while (curOri){
if (curSub->val != curOri->val){
curOri = curOri->next;
if (curOri == NULL)
return 0;
} else{
while (curOri && curSub){
if (curSub->val == curOri->val){
curSub = curSub->next;
curOri = curOri->next;
if (curSub == NULL)
return 1;
}else
return 0;
}
}
}
}
void RemoveAbsEqual(struct ListNode *L, int n){
struct ListNode *ptr = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *dis = (struct ListNode *)malloc(sizeof(struct ListNode));
ptr = L;
int *help, m;
help = (int *)malloc(sizeof(int) * (n + 1));
for (int i = 0; i < n + 1; ++i)
*(help + i) = 0;
while (ptr->next){
m = ptr->next->val > 0 ? ptr->next->val : -ptr->next->val;
if (*(help + m) == 0){
*(help + m) = 1;
ptr = ptr->next;
} else{
dis = ptr->next;
ptr->next = dis->next;
free(dis);
}
}
free(help);
}
struct ListNode* PublicSuffix(struct ListNode *A, struct ListNode *B, int ALen, int BLen){
struct ListNode *curLong = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *curShort = (struct ListNode *)malloc(sizeof(struct ListNode));
if (ALen <= BLen){
curShort = A->next;
curLong = B->next;
} else{
curShort = B->next;
curLong = A->next;
}
int mid = abs(ALen - BLen);
while (mid){
curLong = curLong->next;
mid--;
}
while (curLong != curShort){
curLong = curLong->next;
curShort = curShort->next;
}
return curLong;
}
int KthNodeFromTheBottom(struct ListNode *L, int k, int Len){
struct ListNode *cur = (struct ListNode *)malloc(sizeof(struct ListNode));
cur = L->next;
int mid = Len - k;
while (mid){
cur = cur->next;
mid--;
}
return cur->val;
}
struct ListNode* isCircle(struct ListNode *L){
struct ListNode *slow = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *fast = (struct ListNode *)malloc(sizeof(struct ListNode));
slow = L;
fast = L;
while (fast->next->next){
slow = slow->next;
fast = fast->next->next;
if (slow == fast)
return slow;
}
return NULL;
}
void RefactorList(struct ListNode *L, int Len){
int *A = (int *)malloc(sizeof(int) * Len);
int *B = (int *)malloc(sizeof(int) * Len);
struct ListNode *res= (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *cur = (struct ListNode *)malloc(sizeof(struct ListNode));
res = L;
cur = L->next;
int cou = 0;
while (cur){
A[cou] = cur->val;
cou++;
cur = cur->next;
}
int count = 0;
for (int i = 0, j = Len - 1; i <= j; ++i, --j) {
B[count++] = A[i];
B[count++] = A[j]; }
for (int k = 0; k < Len; ++k) {
struct ListNode *dis = (struct ListNode *)malloc(sizeof(struct ListNode));
dis->val = B[k];
dis->next = NULL;
res->next = dis;
res = res->next;
}
}
void insertsort(struct ListNode *L)
{
struct ListNode *p,*q,*pre;
p = L->next->next;
L->next->next = NULL;
while(p)
{
q = p->next;
pre = L;
while(pre->next != NULL && pre->next->val < p->val)
pre = pre->next;
p->next = pre->next;
pre->next = p;
p = q;
}
}
void DeleteMinElement(struct ListNode *L){
struct ListNode *cur = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *pre = (struct ListNode *)malloc(sizeof(struct ListNode));
cur = L->next;
pre = L;
int temp = INT_MAX;
while (cur){
if (cur->val < temp)
temp = cur->val;
cur = cur->next;
}
while (pre->next->val != temp)
pre = pre->next;
struct ListNode *dis = (struct ListNode *)malloc(sizeof(struct ListNode));
dis = pre->next;
pre->next = pre->next->next;
free(dis);
}
_Bool IsItSymmetrical(struct ListNode *L, int n){
int *list = (int *)malloc(sizeof(int) * n);
memset(list, 0, sizeof(int) * n);
struct ListNode *cur = (struct ListNode *)malloc(sizeof(struct ListNode));
cur = L->next;
int count = n;
while (count && cur){
list[n - count] = cur->val;
cur = cur->next;
count--;
}
for (int i = 0, j = n-1; i <= j; ++i, --j)
if (list[i] != list[j])
return 0;
return 1;
}
void SetDifference(struct ListNode *A, struct ListNode *B, struct ListNode *C){
struct ListNode *curA = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *curB = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *curC = (struct ListNode *)malloc(sizeof(struct ListNode));
curA = A->next;
curB = B->next;
curC = C;
while (curA && curB){
if (curA->val < curB->val){
struct ListNode *dis = (struct ListNode *)malloc(sizeof(struct ListNode));
dis->val = curA->val;
dis->next = NULL;
curC->next = dis;
curC = curC->next;
curA = curA->next;
} else if (curA->val == curB->val){
curA = curA->next;
curB = curB->next;
}else
curB = curB->next;
}
if (!curA)
return;
if (!curB){
while (curA){
struct ListNode *dis = (struct ListNode *)malloc(sizeof(struct ListNode));
dis->val = curA->val;
dis->next = NULL;
curC->next = dis;
curC = curC->next;
curA = curA->next;
}
}
}
_Bool SpecialLinkedList_01(struct ListNode *L){
struct ListNode *cur = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *pre = (struct ListNode *)malloc(sizeof(struct ListNode));
pre = L->next;
cur = L->next->next;
int index = 2;
while (cur){
if (cur->val == (index * index - pre->val)){
cur = cur->next;
pre = pre->next;
index++;
} else return 0;
}
return 1;
}
void SpecialLinkedList_02(struct ListNode *L){
struct ListNode *cur = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *pre = (struct ListNode *)malloc(sizeof(struct ListNode));
pre = L->next;
cur = L->next;
int temp = INT_MAX;
while (cur){
if (cur->val < temp)
temp = cur->val;
cur = cur->next;
}
printf("Min Node: %d\n", temp);
while (pre->val != temp)
pre = pre->next;
if (temp % 2){
if (pre->next){
int te = pre->val;
pre->val = pre->next->val;
pre->next->val = te;
}
} else{
struct ListNode *dis = (struct ListNode *)malloc(sizeof(struct ListNode));
dis = pre->next;
pre->next = pre->next->next;
free(dis);
}
}
void SpecialLinkedList_03(struct ListNode *L, int Len, int x){
struct ListNode *cur = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *res = (struct ListNode *)malloc(sizeof(struct ListNode));
res = L;
cur = L->next;
int *list = (int *)malloc(sizeof(int ) * Len);
memset(list, 0, sizeof(int) * Len);
int count = 0;
int index = 0;
int left = -1;
int right = Len;
while (cur){
list[index] = cur->val;
if (cur->val < x){
left++;
if (cur->val != cur->next->val)
count++;
}
if (cur->val > x)
right--;
index++;
cur = cur->next;
}
printf("The count of small than x: %d\n", count);
for(int i = 0, j = left; i <= j; ++i, --j){
int temp = list[i];
list[i] = list[j];
list[j] = temp;
}
int oddInd = 0;
int *odd = (int *)malloc(sizeof(int ) * (Len - right));
memset(odd, 0, sizeof(int) * (Len - right));
for (int k = right; k < Len; ++k)
if (list[k] % 2)
odd[oddInd++] = list[k];
for (int l = 0; l < right; ++l) {
struct ListNode *dis = (struct ListNode *)malloc(sizeof(struct ListNode));
dis->val = list[l];
dis->next = NULL;
res->next = dis;
res = res->next;
}
for (int m = right, n = 0; m < Len, n < oddInd; ++m, ++n) {
struct ListNode *dis = (struct ListNode *)malloc(sizeof(struct ListNode));
dis->val = odd[n];
dis->next = NULL;
res->next = dis;
res = res->next;
}
}
void BulidListPublic(struct ListNode *L1, struct ListNode *L2){
struct ListNode *cur1 = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *cur2 = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *pre = (struct ListNode *)malloc(sizeof(struct ListNode));
pre = L1;
cur1 = L1->next;
cur2 = L2->next;
while (cur1 && cur2){
if (cur1->val < cur2->val){
struct ListNode *dis = (struct ListNode *)malloc(sizeof(struct ListNode));
dis = cur1;
pre->next = cur1->next;
cur1 = cur1->next;
pre = pre->next;
free(dis);
}else if (cur1->val > cur2->val){
cur2 = cur2->next;
} else{
cur1 = cur1->next;
cur2 = cur2->next;
}
}
if (cur1){
struct ListNode *dis = (struct ListNode *)malloc(sizeof(struct ListNode));
dis = cur1;
pre->next = cur1->next;
cur1 = cur1->next;
free(dis);
}
}
void SpecialLinkedList_04(struct ListNode *A, struct ListNode *B, struct ListNode *C){
BulidListPublic(A, B);
BulidListPublic(A, C);
}
void SpecialLinkedList_05(struct ListNode *A, struct ListNode *B, int start, int len, int insert){
struct ListNode *cur1 = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *cur2 = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *pre = (struct ListNode *)malloc(sizeof(struct ListNode));
int startIndex = 1;
int removeCount = 0;
int insertIndex = 1;
cur1 = A->next;
cur2 = B->next;
pre = A;
while (cur1){
while (startIndex != start){
cur1 = cur1->next;
pre = pre->next;
startIndex++;
}
while (removeCount != len){
struct ListNode *dis = (struct ListNode *)malloc(sizeof(struct ListNode));
dis = cur1;
pre->next = cur1->next;
cur1 = cur1->next;
free(dis);
removeCount++;
}
break;
}
while (cur1->next)
cur1 = cur1->next;
while (cur2){
while (insertIndex != (insert - 1)){
cur2 = cur2->next;
insertIndex++;
}
break;
}
struct ListNode *r = (struct ListNode *)malloc(sizeof(struct ListNode));
r = cur2->next;
cur2->next = A->next;
cur1->next = r;
}
void ExchangeNode(struct ListNode *L, struct ListNode *p){
struct ListNode *pre = (struct ListNode *)malloc(sizeof(struct ListNode));
struct ListNode *post = (struct ListNode *)malloc(sizeof(struct ListNode));
pre = L;
post = p->next->next;
while (pre->next != p)
pre = pre->next;
pre->next = p->next;
p->next->next = p;
p->next = post;
}
void CreatCirList(struct ListNode *L, int *list, int len){
struct ListNode *tail, *head;
L->next = NULL;
head = L;
for (int i = 0; i < len; ++i) {
struct ListNode *new = (struct ListNode *)malloc(sizeof(struct ListNode));
new->val = list[i];
L->next = new;
L = new;
}
tail = L;
tail->next = head->next;
}
void RemoveNodePre(struct ListNode *L, struct ListNode *s){
struct ListNode *pre;
pre = s;
while (pre->next->next != s)
pre = pre->next;
struct ListNode *dis = pre->next;
pre->next = s;
free(dis);
}
void MergeCirList(struct ListNode *A, struct ListNode *B, int ALen, int BLen){
struct ListNode *tailA = A;
struct ListNode *tailB = B;
while (ALen){
tailA = tailA->next;
ALen--;
}
while (BLen){
tailB = tailB->next;
BLen--;
}
tailA->next = B->next;
tailB->next = A->next;
}
void RemoveMinCirList(struct ListNode *L, int Len){
struct ListNode *cur, *curPre, *min, *minPre;
min = L->next;
minPre = L;
int temp = Len;
while (Len){
cur = L->next;
curPre = L;
while (temp){
if (cur->val < min->val){
min = cur;
minPre = curPre;
}
curPre = cur;
cur = cur->next;
temp--;
}
struct ListNode *dis = min;
min = min->next;
minPre = minPre->next;
printf("Remove Node %d\n", dis->val);
free(dis);
Len--;
}
free(L);
printf("Remove Head Node");
}
void CreatDulList(struct DulListNode *L, int *list, int Len){
struct DulListNode *tail;
L->prior = NULL;
L->next = NULL;
tail = L;
for (int i = 0; i < Len; ++i) {
struct DulListNode *new = (struct DulListNode *)malloc(sizeof(struct DulListNode));
new->val = list[i];
tail->next = new;
new->prior = tail;
tail = new;
}
tail->next = NULL;
}
void InsertDulList(struct DulListNode *L, int index, int element){
struct DulListNode *cur = L->next;
while (index - 1){
cur = cur->next;
index--;
}
struct DulListNode *new = (struct DulListNode *)malloc(sizeof(struct DulListNode));
new->val = element;
new->next = cur->next;
cur->next->prior = new;
new->prior = cur;
cur->next = new;
}
void RemoveDulNode(struct DulListNode *L, int index){
struct DulListNode *cur = L->next;
struct DulListNode *pre = L;
while (index - 1){
cur = cur->next;
pre = pre->next;
index--;
}
struct DulListNode *dis = cur;
pre->next = cur->next;
cur->next->prior = pre;
free(dis);
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· winform 绘制太阳,地球,月球 运作规律
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· AI 智能体引爆开源社区「GitHub 热点速览」
· 写一个简单的SQL生成工具