- 带头结点的单链表中,删除所有值为x的节点,释放其空间
void Del_x_1(LinkList &L,ElemType x)
{
LNode *p=L->next,*pre=L,*q;
while(p!=NULL)
{
if(p->data=x)
{
q=p;
p=p->next;
pre->next=p;
free(q);
}
else
{
pre=p;
p=p->next;
}
}
}
void Del_x_2(LinkList &L,ElemType x)
{
LNode *p=L->next,*r=L,*q;
while(p!=NULL)
{
if(p->data!=x)
{
r->next=p;
r=p;
p=p->next;
}
else
{
q=p;
p=p->next;
free(q);
}
}
}
void R_print(LinKList L)
{
if(L->next!=NULL)
R_print(L->next);
printf(L->data);
}
LinkList Del_Min(LinkList &L)
{
LNode *pre=L,*p=pre->next;
LNode *minpre=pre, *min=p;
while(p!=NULL)
{
if(p->data<minp->data)
{
minp=p;
minpre=pre;
}
pre=p;
p=p->next;
}
minpre->next=minp->next;
free(minp);
return L;
}
LinkList R_1(LinkList &L)
{
p=L->next;
L->next=NULL;
while(p!=NULL)
{
r=p->next;
p->next=L->next;
L->next=p;
p=r;
}
return L;
}
LinkList R_2(LinkList &L)
{
LinkList *pre,*p=L->next;*r=p->next;
p->next=NULL;
while(r!=NULL)
{
pre=p;
p=r;
r=r->next;
p->next=pre;
}
L->next=p;
return L;
}
void Sort(LinkList &L)
{
LNode *p=L->next;*pre;
Lnode *r=p->next;
p->next=NULL;
p=r;
while(p!=NULL)
{
r=p->next;
pre=L;
while(pre->next!=NULL && pre->next->data<p->data)
pre=pre->next;
p->next=pre->next;
pre->next=p;
p=r;
}
}
LinkList Search_List_Common(LinkList L1,LinkList L2)
{
int len1=Length(L1),len2=Length(L2);
LiNKList longlist,shortlist;
if(len1>len2)
{
longlist=L1->next;
shortlist=L2->next;
dist=len1-len2
}
else
{
longlist=L2->next;
shortlist=L1->next;
dist=len2-len1;
}
while(dist--)
longlist=longlist->next;
while(longlist!=NULL)
{
if(longlist=shortlist)
return longlist;
else
{
longlist=longlist->next;
shortlist=shortlist->next;
}
}
return NULL;
}
void MergeList(LinkList &La,LinkList &Lb)
{
LNode *r,*pa=La->next,*pb=Lb->next;
La->next=NULL;
while(pa&&pb)
{
if(pa->data<=pb->data)
{
r=pa->next;
pa->next=La->next;
La->next=pa;
pa=r;
}
else
{
r=pa->next;
pb->next=La->next;
La->next=pn;
pb=r;
}
}
if(pa)
pa=pb;
while(pb)
{
r=pb->next;
pb->next=La->next;
La->next=pb;
pb=r;
}
free(Lb);
}
int Pattern(LinkList A,LinkList B)
{
LNode *p=A;
LNode *pre=p;
LNode *q=B;
while(p&&q)
if(p->data==q->data)
{
p=p->next;
q=q->next;
}
else
{
pre=pre->next;
p=pre;
q=B;
}
if(q==NULL)
return 1;
else
return 0;
}