链表合并

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

/*问题描述:实现两个链表的合并
基本要求:1) 建立两个链表A和B,链表元素个数分别为m和n个;
2) 假设元素分别为(x1,x2,…xm),和(y1,y2, …yn);
3)把它们合并成一个顺序表C,使得:当m>=n时,C=x1,y1,x2,y2,...xn,yn, (x)
当n>m时,C=y1,x1,y2,x2,…ym,xm,…,yn
4)输出顺序表C
5) 用直接插入排序法对顺序表C进行升序排序,生成链表D,并输出链表D。

6)能删除链表D中指定位置和指定值的元素。

提示:可考虑使用链表、顺序表等数据结构
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct linknode
{ char data;
struct linknode *next;
}node;
node *head=NULL,*head1=NULL,*head2=NULL;
int n;
void Merge()
{
int n1=0,n2=0;
node *p,*q,*r;
if(head!=NULL);
else if(head1==NULL)
head=head2;
else if(head2==NULL)
head=head1;
else
{
p=head1->next;
while(p!=NULL)
{
n1++;
p=p->next;
}
p=head2->next;
while(p!=NULL)
{
n2++;
p=p->next;
}
if(n1>=n2)
{
head=head1;
p=head1->next;
q=head2->next;
while(q!=NULL)
{
r=q->next;
q->next=p->next;
p->next=q;
p=p->next->next;
q=r;
}
}
else
{
head=head2;
p=head2->next;
q=head1->next;
while(q!=NULL)
{
r=q->next;
q->next=p->next;
p->next=q;
p=p->next->next;
q=r;
}
}
head1=NULL;
head2=NULL;
}
printf("\t\t");
p=head->next;
while(p!=NULL)
{
printf("%c ",p->data);
p=p->next;
}
printf("\n");
}
void CreateList() /*尾插法建立单链表*/
{
node *p,*s;
char x;
n=0;
head1=(node*)malloc(sizeof(node));
p=head1;
printf("\n\t\t建立单链表A:");
printf("\n\t\t说明:请逐个输入字符,结束标记为#\n");
printf("\t\t输入:");
scanf("%c",&x);
while(x!='#')
{
s=(node*)malloc(sizeof(node)); /*申请一个新结点*/
n++;
s->data=x; /*对新结点的data域赋值*/
s->next=NULL; /*对新结点的next域赋空值*/
p->next=s; /*将新结点连接到表的末尾*/
p=s; /*P指向最后结点*/
scanf("%c",&x);
}
head2=(node*)malloc(sizeof(node));
p=head2;
printf("\n\t\t建立单链表B:");
printf("\n\t\t说明:请逐个输入字符,结束标记为#\n");
printf("\t\t输入:");
getchar();
scanf("%c",&x);
while(x!='#')
{
s=(node*)malloc(sizeof(node)); /*申请一个新结点*/
n++;
s->data=x; /*对新结点的data域赋值*/
s->next=NULL; /*对新结点的next域赋空值*/
p->next=s; /*将新结点连接到表的末尾*/
p=s; /*P指向最后结点*/
scanf("%c",&x);
}
}
void frees()
{
node *p,*q;
if(head!=NULL)
{
p=head;
head=NULL;
while(p!=NULL)
{
q=p->next;
free(p);
p=q;
}
}
if(head1!=NULL)
{
p=head1;
head1=NULL;
while(p!=NULL)
{
q=p->next;
free(p);
p=q;
}
}
if(head2!=NULL)
{
p=head2;
head2=NULL;
while(p!=NULL)
{
q=p->next;
free(p);
p=q;
}
}
}
void InsList(int i,char x) /*在单链表第i个位置插入元素*/ {
node *s,*p;
int j;
if(i<1) /*若i<1, 则插入位置错误*/ {
printf("\n\t\t\t插入位置不合法!\n");
return;
}
else
{
p=head;j=0;
while(p!=NULL&&j<i-1) /*寻找插入位置*/
{
j++;
p=p->next;
}
if(p!=NULL) /*插入*/
{
s=(node*)malloc(sizeof(node));
s->data=x;
s->next=p->next;
p->next=s;
n++;
}
else
{
printf("\n\t\t\t未找到插入位置!\n");
return;
}
}
}
void DelList(int i) /*删除单链表中第i个元素*/
{
node *p,*q;
int j=0;
if(head->next==NULL)
{
printf("\n\t\t\t线性表已经为空!\n");
return;
}
if(i<1)
{
printf("\n\t\t\t位置不合法!\n");
return;
}
p=head;
while(p->next!=NULL && j<i-1) /*循环直到p指向第i-1个结点*/ {
p=p->next;
j++;
}
if(p->next==NULL || j>i-1)
{
printf("\n\t\t\t没找到要删除的位置!\n");
return;
}
else
{
q=p->next; p->next=q->next; /*删除第i个结点*/
n--;
free(q);
}
}
void ShowList() /*显示单链表所有元素*/
{
node *p=head,*q,*r,*s;
int i;
q=p->next;
r=q->next;
q->next=NULL;
while(r!=NULL)
{
i=1;
p=head;
q=p->next;
s=r->next;
while(q!=NULL)
{
if(q->data>r->data)
{
r->next=q;
p->next=r;
i=0;
break;
}
p=q;
q=q->next;
}
if(i)
{
p->next=r;
r->next=NULL;
}
r=s;
}
p=head;
printf("\n\t\t");
while(p->next!=NULL)
{
printf("%c ",p->next->data);
p=p->next;
}
}
void SearchList(char x)
{
node *p;
p=head->next;
while(p!=NULL)
{
if(p->data==x)printf("\t\t%c ",p->data);
p=p->next;
}
}
void main()
{
int choice,i,j=1;
char x;
head=NULL;
while(j!=0)
{
printf("\n\n\n\n");
printf("\t\t\t 线性表子系统\n");
printf("\n\t\t***************************************");
printf("\n\t\t* 1-------建表*");
printf("\n\t\t* 2-------合并*");
printf("\n\t\t* 3-------插入*");
printf("\n\t\t* 4-------删除*");
printf("\n\t\t* 5-------排序*");
printf("\n\t\t* 6-------显示*");
printf("\n\t\t* 7-------查找*");
printf("\n\t\t* 0-------返回*");
printf("\n\t\t***************************************\n"); printf("\t\t请选择菜单号(0--6):");
scanf("%d",&choice);getchar();
if(choice==1)
{
if(head==NULL && head1==NULL && head2==NULL) CreateList();
else
{
frees();
CreateList();
}
}
else if (choice==2)
{
Merge();
}
else if (choice==3)
{ printf("\n\t\t请输入的位置i和数值x(输入格式:i,x):");
scanf("%d,%c",&i,&x);
InsList(i,x);
}
else if (choice==4)
{ printf("\n\t\t请输入要删除元素的位置:");
scanf("%d",&i);
DelList(i);
}
else if (choice==5)
{ if(head==NULL)
printf("\n\t\t请先建立线性表!");
else
ShowList();
}
else if (choice==6)
{
node *p;
if(head==NULL)
{
printf("\t\t请合并或建立线性表");
}
else
{
p=head->next;
printf("\n\t\t表长为%d\n\t\t",n);
printf("线性表数据为:\n\t\t\t");
while(p!=NULL)
{
printf("%c ",p->data);
p=p->next;
}
printf("\n");
}
}
else if (choice==7)
{
if(head==NULL)printf("\t\t请合并或建立线性表");
else
{
printf("\n\t\t请输入要查找的元素:");
scanf("%c",&x);
SearchList(x);
}
}
else if (choice==0)
j=0;
else
printf("\n\t\t输入错误!请重新输入!\n");
}
}。

相关文档
最新文档