各个子函数都已经测试通过,均是单链表常用的操作
想使用哪个直接在主函数调用就行
#include<stdio.h>
#include<stdlib.h>
typedef struct Node{
int data;
struct Node *next;
}LinkNode,*LinkList;
void Create_T(LinkList head);
void trans(LinkList head);
void Del_x1(LinkList head,int x);
void Del_x2(LinkList head,int x);
void reverse(LinkList head);
void min_value(LinkList head);
void sort(LinkList L);
void del(LinkList L,int a,int b);
LinkList break_list(LinkList L);
LinkList common(LinkList La,LinkList Lb);
int length(LinkList L);
void del_re(LinkList L);
void merge(LinkList La,LinkList Lb);
void common_A(LinkList La,LinkList Lb );
int panduan(LinkList La,LinkList Lb);
void main(){
LinkList head,Lb,longList,shortList;
int x,a,b,dis,L1,L2;
//int flag=1;
head= (LinkList)malloc(sizeof(LinkNode));//head指向新生成的头结点
head->next=NULL;//初始化一个空的单链表
printf("请输入各个结点的值:\n");
Create_T(head);
trans(head);
Lb=(LinkList)malloc(sizeof(LinkNode));
Lb->next=NULL;
printf("请输入单链表B各个结点的值:\n");
Create_T(Lb);
trans(Lb);
//longList=common(head,Lb);
//printf("公共结点为:\n");
//trans(longList);
//printf("合并之后的链表为:\n");
//merge(head,Lb);
//trans(head);
//del_re(head);
//trans(head);
//common_A(head,Lb);
//trans(head);
if(panduan(head,Lb)){
printf("Yes\n");
}
else
printf("NO\n");
#if(0)
Lb=break_list(head);
printf("分解的Lb:\n");
trans(Lb);
L1=length(head);
Lb=(LinkList)malloc(sizeof(LinkNode));
Lb->next=NULL;
printf("请输入单链表B各个结点的值:\n");
Create_T(Lb);
trans(Lb);
L2=length(Lb);
if(L1<L2){
dis=L2-L1;
longList=Lb->next;
shortList=head->next;
}
else{
dis=L1-L2;
longList=head->next;
shortList=Lb->next;
}
printf("dis的值为%d\n",dis);
while(dis--){
longList=longList->next;
}
while(longList){
if(longList->data==shortList->data){
break;
}
else{
longList=longList->next;
shortList=shortList->next;
}
}
trans(longList);
#endif
//printf("请输出要删除的值:");
//scanf("%d",&x);
//Del_x(head,x);
//Del_x2(head,x);
//reverse(head);
//min_value(head);
//sort(head);
//trans(head);
//scanf("%d%d",&a,&b);
//del(head,a,b);
//trans(head);
}
//尾插法创建单链表
void Create_T(LinkList head){
LinkList tail,p;
int num;
tail=head;
while(scanf("%d",&num),num!=-1){
p=(LinkList)malloc(sizeof(LinkNode));
p->data=num;
tail->next=p;
tail=p;
}
tail->next=NULL;
}
void trans(LinkList head){
LinkList p=head->next;
while(p){
printf("%d ",p->data);
p=p->next;
}
printf("\n");
}
//法1:删除链表中值为X的结点
void Del_x1(LinkList head,int x){
LinkList p,pre,q;
pre=head;
p=pre->next;
while(p){
if(p->data==x){
q=p;
p=p->next;
pre->next=q->next;
free(q);
}
else{
pre=p;
p=p->next;
}
}
}
//法2:删除链表中值为X的结点
void Del_x2(LinkList head,int x){
LinkList p,rear,q;
rear=head;
p=head->next;
while(p){
if(p->data!=x){
rear->next=p;
rear=p;
p=p->next;
}
else{
q=p;
p=p->next;
free(q);
}
}
rear->next=NULL;
}
//链表进行逆置--头插法
void reverse(LinkList head){
LinkList p,q;
p=head->next;
head->next=NULL; //先将头结点的next域置为NULL
while(p){
q=p;//保存后继结点
p=p->next;
q->next=head->next;
head->next=q;
}
}
//删除最小值结点
void min_value(LinkList head){
LinkList mpre,min,pre,p;
pre=head;
p=pre->next;
mpre=pre;
while(p){
if( p->data < mpre->next->data){
mpre=pre;
}
pre=p;
p=p->next;
}
min=mpre->next;
mpre->next=min->next;
free(min);
}
//直接插入排序链表实现方法
void sort(LinkList L){
LinkList p,q,pre,s;
p=L->next;
q=p->next;
p->next=NULL;
while(q){
s=q->next;//先保存后继
pre=L;
while(pre->next && pre->next->data<q->data) //进行比较,若关键字值大,则指针后移
pre=pre->next;
q->next=pre->next;
pre->next=q;
q=s;
}
}
//删除值为a—b的结点
void del(LinkList L,int a,int b){
LinkList pre,p,q;
pre=L;
p=pre->next;
while(p){
if(p->data>a && p->data<b){
q=p;
p=p->next;
pre->next=p;
free(q);
}
else{
pre=p;
p=p->next;
}
}
}
//计算单链表的长度
int length(LinkList L){
int len;
LinkList p=L;
len=0;
while(p->next){
len++;
p=p->next;
}
return len;
}
//链表进行拆分,奇数序号存储在A中,偶数序号存储在B中
LinkList break_list(LinkList L){
LinkList Lb,p,tail1,q;
Lb=(LinkList)malloc(sizeof(LinkNode));
Lb->next=NULL;
tail1=L;
//tail2=Lb;
p=L->next;
while(p){
tail1->next=p;
tail1=p;
p=p->next;
if(p){ //采用头插法
q=p->next;
p->next=Lb->next;
Lb->next=p;
p=q;
}
}
tail1->next=NULL;
return Lb;
}
//递增有序的单链表实现去重处理
void del_re(LinkList L){
LinkList p,q,s;
p=L->next;
q=p->next;
while(q){
if(q->data==p->data){
s=q;
q=q->next;
p->next=s->next;
free(s);
}
else{
p=q;
q=q->next;
}
}
}
//链表归并,两个链表初始递增,归并后递减顺序排列---运用头插法
void merge(LinkList La,LinkList Lb){
LinkList pa,pb,p;
pa=La->next;
pb=Lb->next;
La->next=NULL;//很关键,La作为结果链表的头指针,先将结果链表初始化为空
while(pa && pb){
if(pa->data < pb->data){
p=pa->next;
pa->next=La->next;
La->next=pa;
pa=p;
}
else{
p=pb->next;
pb->next=Lb->next;
Lb->next=pb;
pb=p;
}
}
if(pb){
pa=pb;
}
while(pa){
p=pa->next;
pa->next=La->next;
La->next=pa;
pa=p;
}
free(Lb);
}
//两个链表的公共元素存储在单链表C中
LinkList common(LinkList La,LinkList Lb){
LinkList Lc,pa,pb,s,tail;
Lc=(LinkList)malloc(sizeof(LinkNode));
//Lc->next=NULL;
tail=Lc;
pa=La->next;
pb=Lb->next;
while(pa && pb){
if(pa->data< pb->data){
pa=pa->next;
}
else if(pa->data > pb->data)
pb=pb->next;
else{
s=(LinkList)malloc(sizeof(LinkNode));
s->data=pa->data;
tail->next=s;
tail=s;
pb=pb->next;
pa=pa->next;
}
}
tail->next=NULL;
return Lc;
}
//两个链表的公共元素存储在A中
void common_A(LinkList La,LinkList Lb ){
LinkList pa,pb,tail,u;
pa=La->next;
pb=Lb->next;
La->next=NULL;//很关键,将结果链表值为空
tail=La;
while(pa && pb){
if(pa->data < pb->data){
u=pa;
pa=pa->next;
free(u);
}
else if(pb->data< pa->data){
u=pb;
pb=pb->next;
free(u);
}
else{
tail->next=pa;
tail=pa;
u=pb;
pa=pa->next;
pb=pb->next;
free(u);
}
}
while(pa){
u=pa;
pa=pa->next;
free(u);
}
while(pb){
u=pb;
pb=pb->next;
free(u);
}
tail->next=NULL;
free(Lb);
}
//判断B是不是A的连续子序列
int panduan(LinkList La,LinkList Lb){
LinkList pa,pb,p;
pa=La->next;
pb=Lb->next;
p=pa;
while(pa&&pb){
if(pa->data==pb->data){
pa=pa->next;
pb=pb->next;
}
else{
p=p->next;
pa=p;
pb=Lb->next;
}
}
if(pb==NULL){
return 1;
}
else
return 0;
}