Ⅰ 使用java设计算法,完成将两个有序递增的单链表合并为一个有序递增的单链表,重复的元素只出现一次。
type
point=^node;
node=record
data:integer;
next:point;
end;
var h1,h2,h:point;
procere prt(p:point); //打印链表
begin
p:=p^.next;
while p<>nil do
begin
write(p^.data,' ');
p:=p^.next;
end;
writeln;
end;
procere creat(var h:point); //建立链表
var x:integer; p,q:^node;
begin
writeln('请输入升序的数,负数结束:');
new(h);
p:=h;
read(x);
while(x>=0)do
begin
new(q);
q^.data:=x;
p^.next:=q;
p:=q;
read(x);
end;
p^.next:=nil;
end;
function merge_link(var p,q:point):point; //升序合并二个升序链表
var h,w:^node;
begin
w:=p; p:=p^.next; dispose(w); //回收一个头结点,p指向首个数据结点
w:=q; h:=q; q:=q^.next; //h:合并后的头结点,q指向首个数据结点
while (p<>nil)and(q<>nil) do //当二个链表都不空时
if(p^.data<q^.data) then //选一个小的结点
begin
w^.next:=p; //把小结点链入
p:=p^.next; //跳过此结点
w:=w^.next; //w指向当前合并后链表的尾结点
end
else
begin //下面三行作用同上
w^.next:=q;
q:=q^.next;
w:=w^.next;
end;
if p<>nil then w^.next:=p; //将未完的链表接入
if q<>nil then w^.next:=q; //将未完的链表接入
merge_link:=h; //返回合并后的链表头指针
end;
begin
creat(h1);
creat(h2);
h:=merge_link(h1,h2);
writeln('合并后的链表:');
prt(h);
Ⅱ 实现两个链表的合并
#include <stdio.h>
#include <malloc.h>
int m, n;
int count = 1;
struct Node
{
int data;
struct Node *next;
}*A,*B,*C,*D;
//打印AList列表
void printList(struct Node *AList)
{
struct Node *post;
post = AList->next;
switch(count)
{
case 1:
printf("\nListA: ");
break;
case 2:
printf("\nListB: ");
break;
case 3:
printf("\nListC: ");
break;
case 4:
printf("\nListD: ");
break;
default:
printf("\nList: ");
break;
}
while (post)
{
printf("%d ",post->data);
post = post->next;
}
count ++;
}
//初始化表头,列表含有表头
void init()
{
A = (struct Node*)malloc(sizeof(struct Node));
A->data = 0;
A->next = 0;
B = (struct Node*)malloc(sizeof(struct Node));
B->data = 0;
B->next = 0;
C = (struct Node*)malloc(sizeof(struct Node));
C->data = 0;
C->next = 0;
D = (struct Node*)malloc(sizeof(struct Node));
D->data = 0;
D->next = 0;
}
//读取列表A和B
void ReadListAB()
{
int i;
struct Node *post;
struct Node *pre;
// 输入列表长度
printf("m="); scanf("%d",&m);
printf("n="); scanf("%d",&n);
//读取列表A
pre = A;
for(i=1; i<=m; i++)
{
post = (struct Node*)malloc(sizeof(struct Node));
printf("A%d = ", i);
scanf("%d", &post->data);
post->next = 0;
pre->next = post;
pre = post;
}
//读取列表B
pre = B;
for(i=1; i<=n; i++)
{
post = (struct Node*)malloc(sizeof(struct Node));
printf("B%d = ", i);
scanf("%d", &post->data);
post->next = 0;
pre->next = post;
pre = post;
}
}
//合并列表A和B
void MergeABToC()
{
int i;
struct Node *pre, *postA, *postB, *postC;
pre = C;
postA = A->next;
postB = B->next;
if(m >= n)
{
for(i=1; i<=n; i++)
{
postC = (struct Node*)malloc(sizeof(struct Node));
postC->data = postA->data;
postC->next = 0;
pre->next = postC;
pre = postC;
postC = (struct Node*)malloc(sizeof(struct Node));
postC->data = postB->data;
postC->next = 0;
pre->next = postC;
pre = postC;
postA = postA->next;
postB = postB->next;
}
for(i=n+1; i<=m; i++)
{
postC = (struct Node*)malloc(sizeof(struct Node));
postC->data = postA->data;
postC->next = 0;
pre->next = postC;
pre = postC;
postA = postA->next;
}
}
else
{
for(i=1; i<=m; i++)
{
postC = (struct Node*)malloc(sizeof(struct Node));
postC->data = postB->data;
postC->next = 0;
pre->next = postC;
pre = postC;
postC = (struct Node*)malloc(sizeof(struct Node));
postC->data = postA->data;
postC->next = 0;
pre->next = postC;
pre = postC;
postA = postA->next;
postB = postB->next;
}
for(i=m+1; i<=n; i++)
{
postC = (struct Node*)malloc(sizeof(struct Node));
postC->data = postB->data;
postC->next = 0;
pre->next = postC;
pre = postC;
postB = postB->next;
}
}
}
//使用直接插入法,将C排序输出D
void SortCToD()
{
struct Node *pre, *postC, *postD, *lopD;
int len;
//表示总的长度
len = m + n;
pre = D; //指向D有序数列的尾指针
postC = C->next; //指向C列表
//列表D第一个节点加入
postD = (struct Node*)malloc(sizeof(struct Node));
postD->data = postC->data;
postD->next = 0;
pre->next = postD;
pre = postD;
postC = postC->next;
while (postC)
{
//pre为指向插入的前一个节点,lopD是指向插入的后一个节点
pre = D;
lopD = D->next;
while (lopD)
{
if (lopD->data > postC->data)
break;
else
{
pre = lopD;
lopD = lopD->next;
}
}
//将节点插入
postD = (struct Node*)malloc(sizeof(struct Node));
postD->data = postC->data;
postD->next = 0;
pre->next = postD;
postD->next = lopD;
//循环条件
postC = postC->next;
}
}
void main(void)
{
init();
ReadListAB();
MergeABToC();
SortCToD();
printList(A);
printList(B);
printList(C);
printList(D);
}
以上代码可以运行,并且结果正确。
Ⅲ 实现两个链表的合并,使用java语言编写一程序,将A表和B表归并成一个新的递增有序的单链表C,值
importjava.util.Collections;
importjava.util.LinkedList;
importjava.util.List;
publicclassTestMouse1{
publicstaticvoidmain(String[]agse){
List<Integer>list1=newLinkedList<Integer>();
Collections.addAll(list1,30,41,15,12,56,80);
List<Integer>list2=newLinkedList<Integer>();
Collections.addAll(list2,23,56,78,23,12,33,79,90,55);
test1(list1,list2);
}
publicstaticvoidtest1(List<Integer>list1,List<Integer>list2){
list1.removeAll(list2);//list1中删除和list2中交集的元素
list2.addAll(list1);//合并
Collections.sort(list2);
for(Integerinteger:list2){
System.out.print(integer+"");
}
}
}
请采纳
Ⅳ 链表的合并
#include "stdio.h"
struct student
{
int number;
int score;
struct student *next;
struct student *before; /*由于要排序,所以要建立双向链表 */
}*Head_A,*Rear_A,*Head_B,*Rear_B,*p,*p1;
void main()
{
int i,j,Length_A,Length_B,Length_Z;
printf("此程序是把a,b两个链表合并并按学号升序排列.\n");
printf("a,b两个链表中的结点包括学号、成绩。\n");
printf("先分别输入两个链表的长度.\n");
printf("请输入a链表的长度:"); /*输入链表a*/
scanf("%d",&Length_A);
printf("请输入a链表结点中的学号和成绩\n");
printf(" 学号 成绩\n");
for(i=1;i<=Length_A;i++)
{
p=(struct student*)malloc(sizeof(struct student));
printf("请输入第%d个同学的学号和成绩:",i);
scanf("%d%d",&p->number,&p->score);
p->next=NULL;
if(i==1) {Head_A=p;Rear_A=p;p->before=NULL;}
else {p->before=Rear_A;Rear_A->next=p;Rear_A=p;}
}
printf("现在将输出a链表.\n"); /*输出链表a*/
printf(" 学号 成绩\n");
p=Head_A;
for(i=1;i<=Length_A;i++)
{
printf("第%d个同学:%d %d\n",i,p->number,p->score);
p=p->next;
}
printf("请输入b链表的长度:"); /*输入链表b*/
scanf("%d",&Length_B);
printf("请输入b链表结点中的学号和成绩\n");
printf(" 学号 成绩\n");
for(i=1;i<=Length_B;i++)
{
printf("第%d个同学的学号和成绩:",i);
scanf("%d%d",&p->number,&p->score);
p->next=NULL;
if(i==1) {Head_B=p;Rear_B=p;}
else {p->before=Rear_B;Rear_B->next=p;Rear_B=p;}
}
printf("现在将输出b链表.\n"); /*输出链表b*/
printf(" 学号 成绩\n");
p=Head_A;
for(i=1;i<=Length_B;i++)
{
printf("第%d位同学的学号和成绩:%d %d\n",i,p->number,p->score);
p=p->next;
}
printf("现在将a,b链表进行合并\n"); /*合并两链表*/
Rear_A->next=Head_B; /*将b链表的头指针地址赋值给a链表的尾部*/
Rear_A=Head_B;
printf("现在输出合并后的新链表\n"); /*输出新链表*/
Length_Z=Length_A+Length_B; /*长度变为两链表之和*/
p=Head_A;
printf(" 学号 成绩\n");
for(i=1;i<=Length_Z;i++)
{
printf("第%d位同学的学号和成绩:%d %d\n",i,p->number,p->score);
p=p->next;
}
p1=(struct student*)malloc(sizeof(struct student));
printf("现在进行排序工作\n");
for(i=1;i<Length_Z;i++)
{ p=Head_A;
p->before=NULL;
p1=p->next;
for(j=1;j<=Length_Z-i;j++)
{
if(p->number>p->next->number)
{
p1=p->next;
p->next=p1->next;
p1->next->before=p1->before;
p1->next=p1->before;
if(j==1) {p->before=p1;Head_A=p1;p1->before=NULL;}
else {p1->before=p->before;p1->before->next=p1;}
p1=p->next;
}
else {p=p1;p1=p1->next;}
}
}
/*经过上面的程序,已经把顺序排好*/
printf("现在将输出已按升序排列的新链表.\n");
printf(" 学号 成绩\n");
for(i=1;i<=Length_Z;i++)
{
printf("第%d位同学的学号和成绩:%d %d\n",i,p->number,p->score);
p=p->next;
}
}
Ⅳ java List合并相同的项的问题
单纯用sql语句有点难度
似乎需要用到存储过程
这个我不会
但是如果拿到程序里面要方便许多
没条数据做处理就好了
用两个哈希表和一个链表处理
问题不大
Ⅵ 将两个单循环的链表合并 的问题
看不到题,说不清楚
如果m和n是题目给出来的,那么把长的放到后面速度快一些,因为遍历到短链表尾节点所需时间少
如果不是,那么就在同一个循环里遍历,哪个链表先到尾节点,就把另外一个放在它后面,实际上的循环次数还是短链表的长度,长链表没有遍历完
Ⅶ 如何用Java合并两个升序单链表
比如a b是2个有序升链表,设c为集合链表
while(a.next()!=null||b.next()!=null)
{
if(a.getdata()>b.getdata()){c.next()=b;c=c.next();b=b.next();}
else if(a.getdata()<b.getdata()){c.next()=a;c=c.next();a=a.next();}
else {c.next()=a;c=c.next();a=a.next();b=b.next();}
}
Ⅷ 链表的合并
#include <stdio.h>
#include <stdlib.h>
//定义单链表结构类型
typedef struct linknode
{
int data;
struct linknode *next;
}LNode,*LinkList;
//单链表初始化(不带头节点)
int Init_LL(LinkList *phead)
{
*phead=NULL;
return 1;
}
//求单链表长度
int Length_LL(LinkList phead)
{
int count=0;//初始化计数器
while (phead !=NULL)
{
phead=phead->next;
count ++;
}
return (count);//返回表长
}
//打印单链表
void Printf_LL(LinkList phead)
{
while(phead !=NULL)
{
printf("%d",phead->data);
phead=phead->next;
if(phead!=NULL)
printf(",");
}
printf("\n");
}
//释放资源
void Free_LL(LinkList *phead)
{
LinkList p;
p=*phead;
while(p !=NULL)
{
*phead=(*phead)->next;//表头后移一个
free(p);//释放
p=*phead;//指向表头,释放下一个
}
}
//插入新节点:尾插法
//i为插入位置,在其后进行插入操作。x为插入值
int Insert_LL(LinkList *phead,int i,int x)
{
LinkList s,p;//s是新节点指针
int j;
int count;//链表长度
//如果插入位置出现负数,中断
if(i <= 0)
{
printf("插入 %d 位置非法\n",i);
return 0;
}
//如果插入位置超过链表总长,在表尾插入。
count = Length_LL(*phead);
if(i > count+1)
i=count+1;
//生成一个新节点
s=(LinkList)malloc(sizeof(LNode));
if(s==NULL)
{
printf("申请内存出错\n");
return 0;
}
s->data=x;
//如果i=1,将s所指节点插入到表头
if(i==1)
{
//如果表头为空,直接将s设置表头.否则在表头后插入
if (*phead == NULL)
{
s->next=NULL;
*phead = s;
}
else
{
s->next=(*phead)->next;
(*phead)->next = s;
}
}
else
{
p=*phead;
j=1;//用来记录节点个数
//在单链表查找第i-1个节点,由p指向
while (p !=NULL && j<i-1)
{
j++;
p=p->next;
}
if (p !=NULL)//找到插入位置,把新节点插入其后
{
s->next=p->next;//新节点后续指向p后续
p->next = s; //p后续指向新节点
}
else
{
printf("没有找到节点为%d对应位置!\n",i);
return 0;
}
}
return 1;
}
//创建链表
int Creat_LL(LinkList *phead)
{
int i=1;//循环变量
int n; //元素
printf("输入-1代表停止:\n");
scanf("%d",&n);
while(n!=-1)
{
if(Insert_LL(phead,i,n)!=1)
return 0;
scanf("%d",&n);
i++;
}
return 1;
}
//单链表合并
LinkList Merger_LL(LinkList head1,LinkList head2)
{
LinkList head;//合并后的链表第一节点
LinkList p1,p2;
if(head1==NULL&&head2==NULL)
{
printf("2个链表都为空,不能合并\n");
return NULL;
}
else if(head1==NULL)//如果第一个链表为空,直接返回第二个链表
{
return head2;
}
else if(head2==NULL)//如果第二个链表为空,直接返回第一个链表
{
return head1;
}
p1=head1;
p2=head2;
if(Length_LL(head1) >= Length_LL(head2))
{
head=head1;
while(p1->next!=NULL)
p1=p1->next;
p1->next=p2;
}
else
{
head=head2;
while(p2->next!=NULL)
p2=p2->next;
p2->next=p1;
}
return head;
}
int main(void)
{
//单链表初始化(不带头节点)
LinkList head,head1,head2;
Init_LL(&head1);
Init_LL(&head2);
//创建链表1
printf("输入链表1,");
if(Creat_LL(&head1)!=1)
return 1;
//打印单链表
Printf_LL(head1);
//创建链表2
printf("输入链表2,");
if(Creat_LL(&head2)!=1)
return 1;
//打印单链表
Printf_LL(head2);
//单链表合并
head=Merger_LL(head1,head2);
if(head==NULL)
return 1;
printf("合并后链表:\n");
//打印单链表
Printf_LL(head);
//释放资源
Free_LL(&head);
return 0;
}