無序連結串列合併為一個有序連結串列,且排序後連結串列中無重複元素
阿新 • • 發佈:2019-01-02
1.
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
void listNum(struct Node *head,int count)//氣泡排序
{
int i,temp;
struct Node *p;
for (int i=0;i<count;++i)
{
for (p=head;p->next!=NULL;p=p->next)
{
if (p->data>p->next->data)
{
temp=p->data;
p->data=p->next->data;
p->next->data=temp;
}
}
}
printf("\n連結串列排序完後:\n");
p=head;
while(p)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
}
void merge(struct Node *head1,struct Node *head2)
{
struct Node *head3=(struct Node *)malloc(sizeof(struct Node *));
head3=NULL;
struct Node *p=(struct Node *)malloc(sizeof(struct Node *));
struct Node *q=(struct Node *)malloc(sizeof(struct Node *));
while(head1!=NULL&&head2!=NULL)
{
if (head1->data<head2->data)
{
p=head1->next;
head1->next=head3;
head3=head1;
head1=p;
}else if (head1->data>head2->data)
{
q=head2->next;
head2->next=head3;
head3=head2;
head2=q;
}else if(head1->data==head2->data)
{
p=head1->next;
head1->next=head3;
head3=head1;
head1=p;
head2=head2->next;
}
}
if (head1!=NULL)
{
p=head1;
while(p!=NULL)
{
p=head1->next;
head1->next=head3;
head3=head1;
head1=p;
}
}
if (head2!=NULL)
{
q=head2;
while(q!=NULL)
{
q=head2->next;
head2->next=head3;
head3=head2;
head2=q;
}
}
//printf("兩連結串列合併後(為遞減輸出):\n");
//while(head3)
//{
//printf("%d",head3->data);
//head3=head3->next;
//}
//將連結串列倒置,原來是由大到小,倒置成由小到大
q=head3->next;
head3->next=NULL;
while(q!=NULL)
{
p=q->next;
q->next=head3;
head3=q;
q=p;
}
printf("\n兩連結串列合併後(為遞增輸出):\n");
while(head3)
{
printf(" %d",head3->data);
head3=head3->next;
}
}
void main()
{
struct Node *head1,*head2;
struct Node *p,*q;
int count1=1,count2=1;
//head3=(struct Node *)malloc(sizeof(struct Node *));
//head3=NULL;
head2=(struct Node *)malloc(sizeof(struct Node *));
head2=NULL;
head1=(struct Node *)malloc(sizeof(struct Node *));
head1=NULL;
p=(struct Node *)malloc(sizeof(struct Node *));
q=(struct Node *)malloc(sizeof(struct Node *));
printf("輸入連結串列1資料:\n");
scanf("%d",&p->data);
while (p->data!=0)
{
count1++;
p->next=head1;
head1=p;
p=(struct Node *)malloc(sizeof(struct Node *));
scanf("%d",&p->data);
}
listNum(head1,count1);
printf("輸入連結串列2資料:\n");
scanf("%d",&q->data);
while (q->data!=0)
{
count2++;
q->next=head2;
head2=q;
q=(struct Node *)malloc(sizeof(struct Node *));
scanf("%d",&q->data);
}
listNum(head2,count2);//排序連結串列
merge(head1,head2);//合併兩個連結串列
system("pause");
}
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node* next;
};
void listNum(struct Node *head,int count)//氣泡排序
{
int i,temp;
struct Node *p;
for (int i=0;i<count;++i)
{
for (p=head;p->next!=NULL;p=p->next)
{
if (p->data>p->next->data)
{
temp=p->data;
p->data=p->next->data;
p->next->data=temp;
}
}
}
printf("\n連結串列排序完後:\n");
p=head;
while(p)
{
printf("%d",p->data);
p=p->next;
}
printf("\n");
}
void merge(struct Node *head1,struct Node *head2)
{
struct Node *head3=(struct Node *)malloc(sizeof(struct Node *));
head3=NULL;
struct Node *p=(struct Node *)malloc(sizeof(struct Node *));
struct Node *q=(struct Node *)malloc(sizeof(struct Node *));
while(head1!=NULL&&head2!=NULL)
{
if (head1->data<head2->data)
{
p=head1->next;
head1->next=head3;
head3=head1;
head1=p;
}else if (head1->data>head2->data)
{
q=head2->next;
head2->next=head3;
head3=head2;
head2=q;
}else if(head1->data==head2->data)
{
p=head1->next;
head1->next=head3;
head3=head1;
head1=p;
head2=head2->next;
}
}
if (head1!=NULL)
{
p=head1;
while(p!=NULL)
{
p=head1->next;
head1->next=head3;
head3=head1;
head1=p;
}
}
if (head2!=NULL)
{
q=head2;
while(q!=NULL)
{
q=head2->next;
head2->next=head3;
head3=head2;
head2=q;
}
}
//printf("兩連結串列合併後(為遞減輸出):\n");
//while(head3)
//{
//printf("%d",head3->data);
//head3=head3->next;
//}
//將連結串列倒置,原來是由大到小,倒置成由小到大
q=head3->next;
head3->next=NULL;
while(q!=NULL)
{
p=q->next;
q->next=head3;
head3=q;
q=p;
}
printf("\n兩連結串列合併後(為遞增輸出):\n");
while(head3)
{
printf(" %d",head3->data);
head3=head3->next;
}
}
void main()
{
struct Node *head1,*head2;
struct Node *p,*q;
int count1=1,count2=1;
//head3=(struct Node *)malloc(sizeof(struct Node *));
//head3=NULL;
head2=(struct Node *)malloc(sizeof(struct Node *));
head2=NULL;
head1=(struct Node *)malloc(sizeof(struct Node *));
head1=NULL;
p=(struct Node *)malloc(sizeof(struct Node *));
q=(struct Node *)malloc(sizeof(struct Node *));
printf("輸入連結串列1資料:\n");
scanf("%d",&p->data);
while (p->data!=0)
{
count1++;
p->next=head1;
head1=p;
p=(struct Node *)malloc(sizeof(struct Node *));
scanf("%d",&p->data);
}
listNum(head1,count1);
printf("輸入連結串列2資料:\n");
scanf("%d",&q->data);
while (q->data!=0)
{
count2++;
q->next=head2;
head2=q;
q=(struct Node *)malloc(sizeof(struct Node *));
scanf("%d",&q->data);
}
listNum(head2,count2);//排序連結串列
merge(head1,head2);//合併兩個連結串列
system("pause");
}