
求数据结构的题目答案……
1:已知带头结点的单链表L中的结点按整数值递增排列,试写一算法,将值为X的结点插入表L中,使得L仍然有序,并分析算法的时间复杂度。2:已知两个单链表A和B分别表示两个集合...
1:已知带头结点的单链表L中的结点按整数值递增排列,试写一算法,将值为X的结点插入表L中,使得L仍然有序,并分析算法的时间复杂度。
2:已知两个单链表A和B分别表示两个集合,其元素递增有序,编写一个函数求出A和B得交集C,要求C同样以元素递增的单链表形式储存。 展开
2:已知两个单链表A和B分别表示两个集合,其元素递增有序,编写一个函数求出A和B得交集C,要求C同样以元素递增的单链表形式储存。 展开
1个回答
展开全部
if(!head.next) head.next=x;
else
{
if(x.d<=head.next.d) {x.next=head.next;head.next=x;}
else
{
p=head;
while(p.next && p.next.d>x.d)
{
p=p.next;
}
x.next=p.next;
p.next=x;
}
}
时间复杂度为O(n)
2.
假设链表带头结点
createList(C);
pA=A.head.next;
pB=B.head.next;
pC=C.head;
while(pA && pB)
{
temp=new node;
temp.next=NULL;
if(pA.d<=pB.d)
{temp.d=pA.d;pA=pA.next;}
else
{temp.d=pB.d;pB=pB.next;}
pC.next=temp;
pC=pC.next;
}
while(pA)
{
temp=new node;
temp.d=pA.d;
temp.next=NULL;
pC.next=temp;
pC=pC.next;
}
while(pB)
{
temp=new node;
temp.d=pB.d;
temp.next=NULL;
pC.next=temp;
pC=pC.next;
}
else
{
if(x.d<=head.next.d) {x.next=head.next;head.next=x;}
else
{
p=head;
while(p.next && p.next.d>x.d)
{
p=p.next;
}
x.next=p.next;
p.next=x;
}
}
时间复杂度为O(n)
2.
假设链表带头结点
createList(C);
pA=A.head.next;
pB=B.head.next;
pC=C.head;
while(pA && pB)
{
temp=new node;
temp.next=NULL;
if(pA.d<=pB.d)
{temp.d=pA.d;pA=pA.next;}
else
{temp.d=pB.d;pB=pB.next;}
pC.next=temp;
pC=pC.next;
}
while(pA)
{
temp=new node;
temp.d=pA.d;
temp.next=NULL;
pC.next=temp;
pC=pC.next;
}
while(pB)
{
temp=new node;
temp.d=pB.d;
temp.next=NULL;
pC.next=temp;
pC=pC.next;
}

2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件...
点击进入详情页
本回答由光点科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询