已知顺序表la和lb中的元素依值非递减有序排列,现将la和lb归并到新的顺序表lc中,中元素也依值非递减有序
排列。例如la=(3,6,9,11)lb=(4,6,8,11,13,17,20)则lc=(3,4,6,6,8,9,11,11,13,17,20)我不要算法,要从开头到最后...
排列。例如 la=(3,6,9,11) lb=(4,6,8,11,13,17,20) 则lc=(3,4,6,6,8,9,11,11,13,17,20)我不要算法,要从开头到最后的完整的程序,包括怎么定义.建表等等。谢谢了。有好的会有追加
另外请大概解释一下每一步的的意思,加点注释 展开
另外请大概解释一下每一步的的意思,加点注释 展开
展开全部
#include<stdio.h>
#include<malloc.h>
/*声明库函数*/
struct Node
{
int data;
Node *next;
};/*定义结点结构*/
void Create(Node **root) /*生成单链表*/
{
Node *p,*q;
int count;
if( NULL == *root)
return;
p = *root;
printf("请输入节点的个数:");
scanf("%d",&count);
for(int i = 0; i<count; i++)
{
q = (Node*)malloc(sizeof(Node));
printf("请输入节点数据:");
scanf("%d",&(q->data));
p->next = q;
p = q;
p ->next =NULL;
}
}
Node * Combine(Node *root1, Node *root2) /*合并两个单链表,并返回新单链表的表头指针*/
{
Node *root3 = (Node*)malloc(sizeof(Node));
Node *p1,*p2,*p3;
p1 = root1 ->next;
p2 = root2 ->next;
p3 = root3;
while(p1 != NULL&&p2 != NULL)
{
if(p1 ->data < p2 ->data)
{
p3 ->next = p1;
p3 = p3 ->next;
p1 = p1 ->next;
}
else
{
p3 ->next = p2;
p3 = p3 ->next;
p2 = p2 ->next;
}
}
while(p1 == NULL && p2 != NULL)
{
p3 ->next = p2;
p3 = p3 ->next;
p2 = p2 ->next;
}
while(p2 == NULL && p1 != NULL)
{
p3 ->next = p1;
p3 = p3 ->next;
p1 = p1 ->next;
}
return root3;
}
/*下面为主方法*/
int main()
{
Node *root1,*root2,*Combine_Root;
Node *p,*q;
root1 = (Node*)malloc(sizeof(Node));
root2 = (Node*)malloc(sizeof(Node));
Create(&root1); /*生成单链表1*/
Create(&root2);/*生成单链表2*/
Combine_Root = Combine(root1,root2); /*合并单链表*/
for(p = Combine_Root ->next; p != NULL; p = p ->next)
printf("%d",p ->data);
free(root1);
free(root2);
for(p = Combine_Root; p != NULL; )
{
q = p;
p = p ->next;
free(q);
}
return 0;
}
#include<malloc.h>
/*声明库函数*/
struct Node
{
int data;
Node *next;
};/*定义结点结构*/
void Create(Node **root) /*生成单链表*/
{
Node *p,*q;
int count;
if( NULL == *root)
return;
p = *root;
printf("请输入节点的个数:");
scanf("%d",&count);
for(int i = 0; i<count; i++)
{
q = (Node*)malloc(sizeof(Node));
printf("请输入节点数据:");
scanf("%d",&(q->data));
p->next = q;
p = q;
p ->next =NULL;
}
}
Node * Combine(Node *root1, Node *root2) /*合并两个单链表,并返回新单链表的表头指针*/
{
Node *root3 = (Node*)malloc(sizeof(Node));
Node *p1,*p2,*p3;
p1 = root1 ->next;
p2 = root2 ->next;
p3 = root3;
while(p1 != NULL&&p2 != NULL)
{
if(p1 ->data < p2 ->data)
{
p3 ->next = p1;
p3 = p3 ->next;
p1 = p1 ->next;
}
else
{
p3 ->next = p2;
p3 = p3 ->next;
p2 = p2 ->next;
}
}
while(p1 == NULL && p2 != NULL)
{
p3 ->next = p2;
p3 = p3 ->next;
p2 = p2 ->next;
}
while(p2 == NULL && p1 != NULL)
{
p3 ->next = p1;
p3 = p3 ->next;
p1 = p1 ->next;
}
return root3;
}
/*下面为主方法*/
int main()
{
Node *root1,*root2,*Combine_Root;
Node *p,*q;
root1 = (Node*)malloc(sizeof(Node));
root2 = (Node*)malloc(sizeof(Node));
Create(&root1); /*生成单链表1*/
Create(&root2);/*生成单链表2*/
Combine_Root = Combine(root1,root2); /*合并单链表*/
for(p = Combine_Root ->next; p != NULL; p = p ->next)
printf("%d",p ->data);
free(root1);
free(root2);
for(p = Combine_Root; p != NULL; )
{
q = p;
p = p ->next;
free(q);
}
return 0;
}
展开全部
#include <iostream>
#include <map>
int main()
{
int la[] = { 3,6,9,11 };
int lb[] = { 4,6,8,11,13,17,20 };
int lc[] = { 3,4,6,6,8,9,11,11,13,17,20 };
//球长度
int la_len = sizeof( la )/sizeof( int );
int lb_len = sizeof( lb )/sizeof( int );
int lc_len = sizeof( lc )/sizeof( int );
//构造lc到map表中
std::multimap< int, bool > oAll;
for ( int i = 0; i < lc_len; ++i )
{
oAll.insert( make_pair( lc[ i ], true ) );
}
//塞a进去
for ( int i = 0; i < la_len; ++i )
{
oAll.insert( make_pair( true, la[ i ] ) );
}
//塞b进去
for ( int i = 0; i < lb_len; ++i )
{
oAll.insert( make_pair( lb[ i ], true ) );
}
//打印输出
for (
std::multimap< int, bool >::iterator iter = oAll.begin();
iter != oAll.end();
++iter )
{
std::cout << ( iter->first ) << endl;
}
system( "pause" );
return 0;
}
#include <map>
int main()
{
int la[] = { 3,6,9,11 };
int lb[] = { 4,6,8,11,13,17,20 };
int lc[] = { 3,4,6,6,8,9,11,11,13,17,20 };
//球长度
int la_len = sizeof( la )/sizeof( int );
int lb_len = sizeof( lb )/sizeof( int );
int lc_len = sizeof( lc )/sizeof( int );
//构造lc到map表中
std::multimap< int, bool > oAll;
for ( int i = 0; i < lc_len; ++i )
{
oAll.insert( make_pair( lc[ i ], true ) );
}
//塞a进去
for ( int i = 0; i < la_len; ++i )
{
oAll.insert( make_pair( true, la[ i ] ) );
}
//塞b进去
for ( int i = 0; i < lb_len; ++i )
{
oAll.insert( make_pair( lb[ i ], true ) );
}
//打印输出
for (
std::multimap< int, bool >::iterator iter = oAll.begin();
iter != oAll.end();
++iter )
{
std::cout << ( iter->first ) << endl;
}
system( "pause" );
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream>
using namespace std;
int main()
{
const int lengtha = 4;
const int lengthb = 7;
int a[lengtha] = {3,6,9,11};
int b[lengthb] = {4,6,8,11,13,17,20};
int c[lengtha+lengthb];
//如果数组A和数组B中元素都未选完,则每次选出较小的放入C中;
//如果已经有数组选完了,那么剩下的都是有序的,无需再进行比较
int pta = 0, ptb = 0, ptc = 0;
while((pta < lengtha)&&(ptb < lengthb))
{
if(a[pta] <= b[ptb])
{
c[ptc] = a[pta];
ptc++;
pta++;
}
else
{
c[ptc] = b[ptb];
ptc++;
ptb++;
}
}
while(pta < lengtha)//将A中剩余元素放入到C中
{
c[ptc] = a[pta];
ptc++;
pta++;
}
while(ptb < lengthb)//将B中剩余元素放入到C中
{
c[ptc] = b[ptb];
ptc++;
ptb++;
}
for(int i = 0; i < lengtha+lengthb; i++)
{
cout << c[i] << ' ';
}
cout << endl;
return 0;
}
using namespace std;
int main()
{
const int lengtha = 4;
const int lengthb = 7;
int a[lengtha] = {3,6,9,11};
int b[lengthb] = {4,6,8,11,13,17,20};
int c[lengtha+lengthb];
//如果数组A和数组B中元素都未选完,则每次选出较小的放入C中;
//如果已经有数组选完了,那么剩下的都是有序的,无需再进行比较
int pta = 0, ptb = 0, ptc = 0;
while((pta < lengtha)&&(ptb < lengthb))
{
if(a[pta] <= b[ptb])
{
c[ptc] = a[pta];
ptc++;
pta++;
}
else
{
c[ptc] = b[ptb];
ptc++;
ptb++;
}
}
while(pta < lengtha)//将A中剩余元素放入到C中
{
c[ptc] = a[pta];
ptc++;
pta++;
}
while(ptb < lengthb)//将B中剩余元素放入到C中
{
c[ptc] = b[ptb];
ptc++;
ptb++;
}
for(int i = 0; i < lengtha+lengthb; i++)
{
cout << c[i] << ' ';
}
cout << endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询