(仅供参考)王道2020算法题代码2.3(P38-P41)
部分题目有多种解法或次优解
#include<stdio.h>
#include<malloc.h>
#define Elem int
typedef struct LNode
{
Elem data;
struct LNode* next;
}LNode;
LNode* HeadInsert(void)
{
LNode* L = NULL;
L = (LNode*)malloc(sizeof(LNode));
L->next = NULL;
int x;
printf("Please enter the Value you want to creat in LNode: ");
scanf("%d", &x);
LNode* S;
while(x != 9999)
{
S = (LNode*)malloc(sizeof(LNode));
S->data = x;
S->next = L->next;
L->next = S;
printf("Please enter the Value you want to creat in LNode: ");
scanf("%d", &x);
}
return L;
}
LNode* TailInsert(void)
{
LNode* L = (LNode*)malloc(sizeof(LNode));
L->next = NULL;
LNode* r = L;
LNode* S;
int x;
printf("Please enter the Value you want to creat in LNode: ");
scanf("%d", &x);
while(x != 9999)
{
S = (LNode*)malloc(sizeof(LNode));
S->data = x;
r->next = S;
r = S;
printf("Please enter the Value you want to creat in LNode: ");
scanf("%d", &x);
}
r->next = NULL;
return L;
}
LNode* GetLNode(LNode* L, int i)
{
if(i < 0)
return NULL;
else if(i == 0)
return L;
else
{
LNode* S = L->next;
for(int j = 2; j <= i && S; j++)
S = S->next;
return S;
}
}
LNode* LocateElem(LNode* L, Elem e)
{
LNode* S = L->next;
while(S->data != e && S)
S = S->next;
return S;
}
typedef struct DNode
{
struct DNode* prior;
Elem data;
struct DNode* next;
} DNode;
void deletex(LNode* &L, Elem x)
{
if(L)
{
if(L->data == x)
{
LNode* p = L;
L = L->next;
free(p);
deletex(L, x);
}
else
deletex(L->next, x);
}
}
void deletenextx(LNode* L, Elem x)
{
if(L->next)
{
deletenextx(L->next, x);
if(L->next->data == x)
{
LNode* p = L->next;
L->next = p->next;
free(p);
}
}
}
void deletex_1(LNode* &L, Elem x)
{
deletenextx(L, x);
if(L->data == x)
{
LNode* p = L;
L = L->next;
free(p);
}
}
void deletexhead(LNode* &L, Elem x)
{
LNode* l, * r, * q;
l = L;
r = L->next;
while(r)
{
if(r->data == x)
{
q = r;
r = r->next;
l->next = r;
free(q);
}
else
{
r = r->next;
l = l->next;
}
}
}
void deletexhead_2(LNode* &L, Elem x)
{
LNode* l = L, * r = L->next, *q;
while(r)
{
if(r->data != x)
{
l->next = r;
l = r;
r = r->next;
}
else
{
q = r;
r = r->next;
free(q);
}
}
}
void R_printfvalue(LNode* L)
{
while(L)
{
R_printfvalue(L->next);
printf("%d\n", L->data);
}
}
void deletemin(LNode* &L)
{
LNode* r = L->next;
LNode* l = L;
LNode* pmin = r;
LNode* pminp = l;
while(r)
{
if(pmin->data > r->data)
{
pmin = r;
pminp = l;
}
r = r->next;
l = l->next;
}
pminp->next = pmin->next;
free(pmin);
}
void Reverse_link(LNode* &L)
{
LNode* p = L->next, *temp;
L->next = NULL;
while(p)
{
temp = p->next;
p->next = L->next;
L->next = p;
p = temp;
}
}
void Reverse_link1(LNode* &L)
{
LNode* l = L->next;
LNode* m = l->next;
LNode* r = m->next;
l->next = NULL;
while(r)
{
m->next = l;
l = m;
m = r;
r = r->next;
}
m->next = l;
L->next = m;
}
void sort_link(LNode* &L)
{
LNode* l;
LNode* p = L->next->next;
LNode* r = p->next;
L->next->next = NULL;
while(p)
{
l = L;
while(l->next && l->next->data < p->data)
l = l->next;
r = p->next;
p->next = l->next;
l->next = p;
p = r;
}
}
void deleterange_link(LNode* &L, Elem min, Elem max)
{
LNode* l = L;
LNode* p = l->next;
LNode* r = p->next;
while(p)
{
if(p->data > min && p->data < max)
{
l->next = p->next;
free(p);
p = l->next;
}
else
{
l = l->next;
p = p->next;
}
}
}
int Length(LNode* L)
{
int count = 0;
L = L->next;
while(L)
{
count++;
L = L->next;
}
return count;
}
LNode* Search_common(LNode* L1, LNode* L2)
{
int len1 = Length(L1);
int len2 = Length(L2);
L1 = L1->next;
L2 = L2->next;
int t;
LNode* temp;
if(len1 > len2)
{
temp = L1;
L1 = L2;
L2 = temp;
t = len2;
len2 = len1;
len1 = t;
}
t = len2 - len1;
while(t--)
{
L2 = L2->next;
}
while(L1 && L1 != L2)
{
L1 = L1->next;
L2 = L2->next;
}
if(L1)
return NULL;
else
return L1;
}
void printfgrow(LNode* &L)
{
while(L->next)
{
LNode* l = L;
LNode* p = L->next;
LNode* minl = l;
LNode* min = p;
while(p)
{
if(min->data > p->data)
{
min = p;
minl = l;
}
p = p->next;
l = l->next;
}
printf("%d\n", min->data);
minl->next = min->next;
free(min);
}
free(L);
}
void demerge_link(LNode* &L, LNode* &L1, LNode* &L2)
{
int count = 1;
L1 = L2 = (LNode*)malloc(sizeof(LNode));
L1->next = L2->next =NULL;
LNode* r1 = L1;
LNode* r2 = L2;
LNode* p = L->next;
while(p)
{
if(count%2 != 0)
{
r1->next = p;
r1 = p;
}
else
{
r2->next = p;
r2 = p;
}
p = p->next;
}
r1->next = r2->next = NULL;
free(L);
}
void demerge_link1(LNode* L, LNode* L1, LNode* L2)
{
L1 = L2 = (LNode*)malloc(sizeof(LNode));
L1->next = L2->next = NULL;
LNode* r1 = L1;
LNode* q2;
LNode* p = L->next;
while(p)
{
r1->next = p;
r1 = p;
p = p->next;
q2 = p->next;
p->next = L1->next;
L1->next = p;
p = p->next;
}
r1->next = NULL;
free(L);
}
void deletesame_link(LNode* &L)
{
if(L->next == NULL)
return;
LNode* l = L->next;
LNode* p = l->next;
while(p)
{
if(l->data == p->data)
{
l->next = p->next;
free(p);
p = l->next;
}
else
{
l = p;
p = p->next;
}
}
}
LNode* Merge_link(LNode* L1, LNode* L2)
{
LNode* p1, * p2, * q;
p1 = L1->next;
p2 = L2->next;
L1->next = NULL;
while(p1 && p2)
{
if(p1->data > p2->data)
{
q = p2->next;
p2->next = L1->next;
L1->next = p2;
p2 = q;
}
else
{
q = p1->next;
p1->next = L1->next;
L1->next = p1;
p1 = q;
}
}
while(p2)
{
q = p2->next;
p2->next = L1->next;
L1->next = p2;
p2 = q;
}
while(p1)
{
q = p1->next;
p1->next = L1->next;
L1->next = p1;
p1 = q;
}
free(L2);
return L1;
}
LNode* samecreat(LNode* L1, LNode* L2)
{
LNode* L = (LNode*)malloc(sizeof(LNode));
L->next = NULL;
L1 = L1->next;
L2 = L2->next;
LNode* r = L;
while(L1 && L2)
{
if(L1->data = L2->data)
{
r->next = (LNode*)malloc(sizeof(LNode));
r->next->data = L1->data;
r = r->next;
L1 = L1->next;
L2 = L2->next;
}
else if(L1->data < L2->data)
L1 = L1->next;
else
L2 = L2->next;
}
r->next = NULL;
return L;
}
void Union(LNode* &la, LNode* &lb)
{
LNode* pa, * pb, * prep, * temp;
pa = la->next;
pb = lb->next;
prep = la;
while(pa && pb)
{
if(pa->data == pb->data)
{
temp = pb;
pb = pb->next;
free(temp);
prep = pa;
pa = pa->next;
}
else if(pa->data < pb->data)
{
prep->next = pa->next;
temp = pa;
pa = pa->next;
free(temp);
}
else
{
temp = pb;
pb = pb->next;
free(temp);
}
}
if(pb)
pa = pb;
while(pa)
{
temp = pa;
pa = pa->next;
free(temp);
}
prep->next = NULL;
free(lb);
}
bool pattern(LNode* LA, LNode* LB)
{
LNode* pa = LA->next;
LNode* pb = LB->next;
LA = LA->next;
LB = LB->next;
while(LA)
{
if(LA->data = LB->data)
{
pa = LA->next;
pb = LB->next;
while(pa && pb && pa->data == pb->data)
{
pa = pa->next;
pb = pb->next;
}
if(!pb)
return true;
if(!pa)
return false;
}
LA = LA->next;
}
}
bool symmetry(DNode* L)
{
DNode* l = L->next, * r = L->prior;
while(l != r && l->next != r)
{
if(l->data != r->data)
return false;
else
{
l = l->next;
r = r->prior;
}
}
return true;
}
void Link(LNode* &h1, LNode* &h2)
{
LNode* p1 = h1;
LNode* p2 = h2;
while(p1->next != h1)
p1 = p1->next;
while(p2->next != h2)
p2 = p2->next;
p1->next = h2->next;
free(h2);
p2->next = p1;
}
void printflinkmin(LNode* &L)
{
Elem min;
LNode* minpre, * p;
while(L->next != L)
{
min = L->next->data;
minpre = L;
p = L->next;
while(p->next != L)
{
if(p->next->data < min)
{
minpre = p;
min = p->next->data;
}
p = p->next;
}
printf("%d\n", min);
p = minpre->next;
minpre->next = p->next;
free(p);
}
free(L);
}
typedef struct freqDNode
{
int freq;
struct freqDNode* pred;
Elem data;
struct freqDNode* next;
}freqDNode;
freqDNode* Locate1x(freqDNode* &L, Elem x)
{
freqDNode* p = L->next;
freqDNode* q = L->next;
while(p && p->data != x)
p = p->next;
if(!p)
return NULL;
p->freq ++;
p->pred->next = p->next;
p->next->pred = p->pred;
while(q && q->freq > p->freq)
q = q->next;
p->next = q;
p->pred = q->pred;
q->pred->next = p;
q->pred = p;
return p;
}
int SearchR_k(LNode* list,int k)
{
LNode* p = list->next;
LNode* q = p;
k = k-1;
while(k-- && p)
p = p->next;
if(!p)
return 0;
while(p->next)
{
p = p->next;
q = q->next;
}
printf("%d", q->data);
return 1;
}
void deleteabssame_link(LNode* &head)
{
int n = 10;
int a[n+1] = {0};
LNode* p = head->next;
LNode* l = head;
int m;
while(p)
{
if(p->data < 0)
m = -p->data;
else
m = p->data;
if(a[m] == 0)
{
a[m]++;
l = l->next;
p = p->next;
}
else
{
l->next = p->next;
free(p);
p = l->next;
}
}
}