1.头插法创建两个升序单链表2.使用两个单链表来合并两个单链表并按从大到小顺序排列3.结果打印4.在合并链表中查找指定的值5.求表长6.删除指定值7.指定位置插入值
#include<stdio.h>
#include<stdlib.h>
typedef struct LNode
{
int data
;
struct LNode
*next
;
}LNode
,*LinkList
;
LinkList
initLinkList(){
LNode
* head
=(LNode
*)malloc(sizeof(LNode
));
LinkList p
=head
;
int elem
;
printf("输入:");
scanf("%d",&elem
);
int n
=5;
while(elem
!=9999){
LNode
* L
=(LNode
*)malloc(sizeof(LNode
));
L
->data
=elem
;
L
->next
=NULL;
p
->next
=L
;
p
=p
->next
;
printf("输入:");
scanf("%d",&elem
);
}
return head
;
}
void display(LinkList p
){
LinkList pp
=p
->next
;
while(pp
){
int data
=pp
->data
;
printf("%d ",data
);
pp
=pp
->next
;
}
}
void reverseMergeList(LinkList
&la
,LinkList
&lb
,LinkList
&resultList
){
LNode
*pa
,*pb
,*now
,*flag
;
pa
=la
->next
;
pb
=lb
->next
;
now
=NULL;
while(pa
!=NULL||pb
!=NULL){
if(pa
!=NULL&&(pb
==NULL||(pa
->data
< pb
->data
))){
flag
=pa
;
pa
=pa
->next
;
flag
->next
=now
;
now
=flag
;
}else{
flag
=pb
;
pb
=pb
->next
;
flag
->next
=now
;
now
=flag
;
}
}
resultList
->next
=now
;
LNode
*head
=(LNode
*)malloc(sizeof(LNode
));
head
->next
=resultList
;
}
LNode
*findByValue(LinkList pp
,int elem
){
LNode
*p
=pp
->next
;
while(p
!=NULL&&elem
!=p
->data
){
p
=p
->next
;
}
if(!p
){
printf("未找到该值");
}else{
printf("查找成功");
}
return p
;
}
void deleteData(LinkList
&pp
,int index
){
LNode
*p
=pp
->next
;
int indexCount
=1;
while(p
!=NULL&&(indexCount
<index
-1)){
p
=p
->next
;
indexCount
++;
}
if(!p
){
printf("删除位置超出表长");
}else{
LNode
*q
;
q
=p
->next
;
p
->next
=q
->next
;
free(q
);
printf("删除成功");
}
}
void insertData(LinkList
&pp
,int index
,int elem
){
LNode
*p
=pp
->next
;
int indexCount
=0;
while(p
!=NULL&&(indexCount
<index
-1)){
p
=p
->next
;
indexCount
++;
}
if(!p
){
printf("插入位置超出表长");
}else{
LNode
*newNode
=(LNode
*)malloc(sizeof(LNode
));
newNode
->data
=elem
;
newNode
->next
=p
->next
;
p
->next
=newNode
;
printf("插入成功");
}
}
int getListLength(LinkList pp
){
LNode
*p
=pp
->next
;
int length
=0;
while(p
!=NULL){
p
=p
->next
;
length
++;
}
return length
;
}
int main(){
printf("初始化链表1:\n");
LinkList LL1
=initLinkList();
printf("\n打印链表1:\n");
display(LL1
);
printf("\n初始化链表2:\n");
LinkList LL2
=initLinkList();
printf("\n打印链表2:\n");
display(LL2
);
printf("\n两表合并且逆序");
LNode
*head
=(LNode
*)malloc(sizeof(LNode
));
LinkList resultList
=head
;
reverseMergeList(LL1
,LL2
,resultList
);
printf("\n合并结果:");
display(resultList
);
while(1){
printf("\n2.按值查找元素结点\n3.求表长\n4.删除指定值\n5.指定位置插入值\n6.打印结果\n");
int op
;
scanf("%d",&op
);
LNode
* p
=(LNode
*)malloc(sizeof(LNode
));
switch(op
%10){
case 1:
break;
case 2:
printf("\n按值查找元素结点\n");
int elem
;
printf("输入查找元素的值:");
scanf("%d",&elem
);
p
=findByValue(resultList
,elem
);
if(p
!=NULL){
printf("\n查找结果:%d",p
->data
);
}
break;
case 3:
printf("\n求表长\n");
int length
;
length
=getListLength(resultList
);
printf("表长为:%d",length
);
break;
case 4:
printf("\n删除指定值\n");
int delIndex
;
printf("\n输入删除位置下标:");
scanf("%d",&delIndex
);
deleteData(resultList
,delIndex
);
printf("\n删除后结果:");
display(resultList
);
break;
case 5:
printf("\n指定位置插入值\n");
int insetIndex
,insetElem
;
printf("\n输入插入位置下标:");
scanf("%d",&insetIndex
);
printf("\n输入插入位置元素:");
scanf("%d",&insetElem
);
insertData(resultList
,insetIndex
,insetElem
);
printf("\n插入后结果:");
display(resultList
);
break;
case 6:
display(resultList
);
break;
default:
printf("error!\n");
break;
}
}
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-1412.html