Ⅰ 使用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;
}