c语言实现两个顺序表的合并
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c语言实现两个顺序表的合并相关的知识,希望对你有一定的参考价值。
将共同拥有的元素只存其一
一个算法给你(假如是升序,并且不重复)
while(表1不结束 && 表2不结束)
if (表1结束 || 表1.当前值>表2.当前值) 表2.当前值插入新表;表2.当前值向后移动
else if (表2结束 || 表1.当前值<表2.当前值) 表1.当前值插入新表;表1.当前值向后移动
else if (表1.当前值=表2.当前值) 表1.当前值插入新表;表1.当前值和表2.当前值向后移动
#include<stdio.h>#include<malloc.h>
#include<stdlib.h>
struct student
int num;
struct student *next;
;
void print(struct student *head)
struct student *p;
p=head;
char s=' ';
if(head==NULL)
printf("该链表为空");
if(head!=NULL)
do
printf("%c%c%d",s,s,p->num);
p=p->next;
while(p!=NULL);
printf("\\n");
struct student *creatb()
struct student *head;
struct student *p1,*p2;
int n=0;
p1=p2=(struct student*)malloc(sizeof(struct student));
scanf("%d",&p1->num);
head=NULL;
while(p1->num!=0)
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student*)malloc(sizeof(struct student));
scanf("%d",&p1->num);
p2->next=NULL;
return head;
int main()
struct student *head1,*head2,*head3;
head1=NULL;
head2=NULL;
head3=NULL;
printf("请输入单链表La,输入0表示输入结束:\\n");
head1=creatb();
printf("输入的链表为:");
print(head1);
printf("请输入单链表Lb,输入0表示输入结束:\\n");
head2=creatb();
printf("输入的链表为:");
print(head2);
struct student *a,*b,*c, *tmpNode;
a=head1->next;
b=head2;//
head3=c=head1;
while(a != NULL || b != NULL)
if(b == NULL || (a != NULL && a->num < b->num))
c->next=a;
c=a;
a=a->next;
else if(a == NULL || (b != NULL && a->num > b->num))
c->next=b;
c=b;
b=b->next;
else if (a != NULL && b != NULL)
c->next=a;
c=a;
a=a->next;
tmpNode = b;
b = b->next;
free(tmpNode);
c->next=NULL;
printf("合并后的有序链表为:");
print(head3);
c = head3;
while(c)
tmpNode = c;
c = c->next;
free(tmpNode);
return 0;
追问
不能假设他不重复啊,要考虑他有重复的情况的啊
追答我假设的是各自里面没有重复
追问这个是顺序表吗?好像是链表啊?
参考技术A 思路,先合并在排序,合并时候注意一个不变,你一个插入,检查有,就pass,没有再插入,在排序,排序方法很多自己选。追问这个思路我也知道啊,要求代码
追答。。。。最好有个具体的描述,顺序表存的是数字吗?
追问可以是数字啊,元素类型可以是多种的啊
参考技术B 两层for循环,第一层表示第一个集合,第二层表示第二个集合,用一个变量flag标记有没有出现过,如果出现了不管,如果没有出现就增加到第一个集合中。最后输出第一个集合的数就好了..追问要的是代码诶。思路我也是这样的
参考技术C 这是链表傻X 参考技术D 你可以好好去看一下《数据结构》中链表这一章,介绍的比较详细,合并、翻转、增加、删除、更新等都有课程设计|C++实现两个链表的合并
目录
前言
Hello!
非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出~
自我介绍 ଘ(੭ˊᵕˋ)੭
昵称:海轰
标签:程序猿|C++选手|学生
简介:因C语言结识编程,随后转入计算机专业,获得过国家奖学金,有幸在竞赛中拿过一些国奖、省奖…已保研。
学习经验:扎实基础 + 多做笔记 + 多敲代码 + 多思考 + 学好英语!
唯有努力💪
知其然 知其所以然!
1. 实现两个链表的合并
基本功能与要求
(1)建立两个链表A和B,链表元素个数分别为m和n个。
(2)假设元素分别为(x1,x2,…,xm),和(y1,y2,…,yn)。把它们合并成一个线性表C:
当m>=n时,C=x1,y1,x2,y2,…,xn,yn,…,xm
当n>m时, C=y1,x1,y2,x2,…,ym,xm,…,yn;
(3)输出线性表C。
(4)用直接插入排序法对C进行升序排序,生成链表D,并输出链表D。
测试数据
(1) A表(30,41,15,12,56,80)
B表(23,56,78,23,12,33,79,90,55)
(2) A表(30,41,15,12,56,80,23,12,34)
B表(23,56,78,23,12)
代码
#include <iostream>
using namespace std;
template <class t>
class node
public:
t data;
node<t> *next;
node(node<t> *ptr = NULL) next = ptr;
node(t a, node<t> *ptr = NULL)
data = a;
next = ptr;
;
template <class t>
class linklist
public:
void merge(linklist<t> &l1, linklist<t> &l2)
int m = l1.length();
int n = l2.length();
node<t> *r;
node<t> *s;
r = first;
node<t> *p = l1.first->next;
cout << p->data << endl;
node<t> *q = l2.first->next;
if (m >= n)
while (p != NULL && q != NULL)
s = new node<t>(p->data);
r->next = s;
r = s;
s = new node<t>(q->data);
r->next = s;
r = s;
p = p->next;
q = q->next;
r->next = p;
else
while (p != NULL && q != NULL)
s = new node<t>(q->data);
r->next = s;
r = s;
s = new node<t>(p->data);
r->next = s;
r = s;
p = p->next;
q = q->next;
r->next = q;
void sort()
node<t> *L = first->next;
node<t> *p, *q, *pre;
p = L->next->next;
L->next->next = NULL;
while (p)
q = p->next;
pre = L;
while (pre->next != NULL && pre->next->data < p->data)
pre = pre->next;
p->next = pre->next;
pre->next = p;
p = q;
// first = L
// node<t> *r;
// node<t> *p = l.first->next;
// r = first;
// node<t> *newNode = new node<t>(p->data);
// r->next = newNode;
// r = newNode;
// p = p->next;
// while(p != NULL)
// t num = r->data;
// newNode = new node<t>(p->data);
// if(p->data > num)
// // newNode = new node<t>(p->data);
// r->next = newNode;
// r = newNode;
// else
//
// p = p->next;
//
void rel()
node<t> *p;
node<t> *s;
p = first->next;
s = p->next;
first->next = NULL;
while (p != NULL)
p->next = first->next;
first->next = p;
p = s;
// cout<<"1"<<endl;
if (p != NULL)
s = p->next;
linklist() first = new node<t>;
linklist(t x) first = new node<t>(x);
void creat(t end)
node<t> *newnode;
t val;
cin >> val;
while (val != end)
newnode = new node<t>(val);
newnode->next = first->next;
first->next = newnode;
cin >> val;
void creat1(t end)
node<t> *r;
node<t> *s;
r = first;
t val;
cin >> val;
while (val != end)
s = new node<t>(val);
r->next = s;
r = s;
cin >> val;
// first=new node<t>
r->next = NULL;
void print()
node<t> *p = first->next;
while (p != NULL)
cout << p->data << "--->>";
p = p->next;
cout << "NULL" << endl;
int length()
node<t> *p;
p = first->next;
int count = 0;
while (p != NULL)
p = p->next;
count++;
return count;
t get(int i)
node<t> *p;
int count = 1;
p = first->next;
while (p != NULL && count < i)
p = p->next;
count++;
if (p == NULL)
throw "位置";
else
return p->data;
void insert(t x, int i)
node<t> *p;
node<t> *s;
p = first;
int count = 0;
while (p != NULL && count < i - 1)
p = p->next;
count++;
if (p == NULL)
throw "位置";
else
s = new node<t>(x);
s->next = p->next;
p->next = s;
int locate(t x)
node<t> *p;
p = first->next;
int count = 1;
while (p != NULL)
if (p->data == x)
return count;
p = p->next;
count++;
return 0;
void del(int i)
node<t> *p;
node<t> *q;
p = first;
int count = 0;
while (p != NULL && count < i - 1)
p = p->next;
count++;
if (p == NULL || p->next == NULL)
throw "位置";
else
q = p->next;
p->next = q->next;
delete q;
~linklist()
node<t> *p;
while (first != NULL)
p = first;
first = first->next;
delete p;
// private:
node<t> *first;
;
linklist<int> merge(linklist<int> &l1, linklist<int> &l2)
int m = l1.length();
int n = l2.length();
cout << "m =" << m << endl;
linklist<int> ans;
// linklist<int> ans;
// cout << "((" << endl;
node<int> *cur = ans.first;
cout << "((" << endl;
// cout << ans.first->data;
node<int> *p = l1.first;
node<int> *q = l2.first;
if (m >= n)
while (p != NULL && q != NULL)
cur->next = p;
cur = cur->next;
cur->next = q;
cur = cur->next;
p = p->next;
q = q->next;
cur->next = p;
else
while (p != NULL && q != NULL)
cur->next = q;
cur = cur->next;
cur->next = p;
cur = cur->next;
p = p->next;
q = q->next;
cur->next = q;
return ans;
int main()
linklist<int> L;
linklist<int> A;
linklist<int> B;
linklist<int> C;
linklist<int> D;
int key = 1;
int choice;
int choice1;
int choice2;
while (key == 1)
cout << "+++++++++++++++++++++++++++++" << endl;
cout << "+ 单链表 +" << endl;
cout << "+ 1.创建单链表(头部插入) +" << endl;
cout << "+ 2. 创建单链表(尾部插入)+" << endl;
cout << "+ 3.查找第i个结点的值 +" << endl;
cout << "+ 4.查找元素x的结点位置 +" << endl;
cout << "+ 5.在第i个位置插入元素x +" << endl;
cout << "+ 6.删除第i个位置的值 +" << endl;
cout << "+ 7.输出单链表各元素 +" << endl;
cout << "+ 8.旋转链表 +" << endl;
cout << "+ 9.新建两个链表 +" << endl;
cout << "+ 10.直接插入排序 +" << endl;
cout << "输入你的选择:(1---10)" << endl;
cin >> choice;
switch (choice)
case 1:
cout << "输入需要存储的数据 以0结尾 例如 1 2 3 4 0" << endl;
L.creat(0);
cout << "单链表创建完成!" << endl;
break;
case 2:
cout << "输入需要存储的数据 以0结尾 例如 1 2 3 4 0" << endl;
L.creat1(0);
cout << "单链表创建完成!" << endl;
break;
case 3:
cout << "输入查找元素的位置i(第i个结点):";
cin >> choice1;
cout << "第" << choice1 << "个结点的值是:";
cout << L.get(choice1) << endl;
break;
case 4:
cout << "输入查找元素的值:";
cin >> choice1;
cout << "该元素在第" << L.locate(choice1) << "个位置!" << endl;
break;
case 5:
cout << "输入插入元素的位置(第i个) :";
cin >> choice1;
cout << "输入插入元素的值:";
cin >> choice2;
L.insert(choice2, choice1);
cout << "插入完成!" << endl;
break;
case 6:
cout << "输入删除元素的位置(第i个):" << endl;
cin >> choice1;
L.del(choice1);
cout << "删除完成!" << endl;
break;
case 7:
cout << "该链表元素的值是:" << endl;
L.print();
break;
case 8:
L.rel();
// cout<<"1"<<endl;
case 9:
cout << "输入需要存储的数据 以0结尾 例如 1 2 3 4 0" << endl;
A.creat1(0);
cout << "单链表创建完成!" << endl;
cout << "输入需要存储的数据 以0结尾 例如 1 2 3 4 0" << endl;
B.creat1(0);
cout << "单链表创建完成!" << endl;
C.merge(A, B);
C.print();
// break;
break;
case 10:
C.sort();
C.print();
break;
cout << "是否继续?(1:继续 0:退出)" << endl;
cin >> key;
system("cls");
return 0;
实验截图
菜单
合并两个链表
直接插入排序
结语
文章仅作为个人学习笔记记录,记录从0到1的一个过程
希望对您有一点点帮助,如有错误欢迎小伙伴指正
以上是关于c语言实现两个顺序表的合并的主要内容,如果未能解决你的问题,请参考以下文章