前言
hello,大家好。这期文章博主和大家分享一下合并两个有序线性表的方法,希望对大家有所帮助。
1. 题目
2. 代码
2.1 LinkList.h
#ifndef LINKLIST_H
#define LINKLIST_H
#include<iostream>
#include<vector>
using namespace std;
template <class T>
struct Node
{
T data;
Node<T> *next;
};
template <class T>
class LinkList
{
public:
LinkList();
LinkList(T a[], int n);
~LinkList();
friend void LinkListMerge(LinkList<T> &L1, LinkList<T> &L2)
{
Node<T>*p1 = L1.head->next;
Node<T>*p2 = L2.head->next;
Node<T>*p3 = L1.head;
vector<int>l;
while ((p1 != NULL) && (p2 != NULL))
{
if ((p1->data)<(p2->data))
{
l.push_back(p1->data);
p3->next = p1;
p1 = p1->next;
p3 = p3->next;
}
else
{
l.push_back(p2->data);
p3->next = p2;
p2 = p2->next;
p3 = p3->next;
}
}
if (p1 != NULL)
p3->next = p1;
if (p2 != NULL)
p3-> next = p2;
p3 = p3->next;
while (p3 != NULL)
{
l.push_back(p3->data);
p3=p3->next;
}
for (auto e : l)
{
cout << e << " ";
}
cout << endl;
}
private:
Node<T> *head;
};
#endif
2.2 LinkList.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"LinkList.h"
template <class T>
LinkList<T>::LinkList()
{
head = new Node<T>;
head->next = NULL;
}
template <class T>
LinkList<T>::LinkList(T a[], int n)
{
Node<T>* rear, *s;
head = new Node<T>;
rear = head;
for (int i = 0; i < n; i++)
{
s = new Node<T>;
s->data = a[i];
rear->next = s;
rear = s;
}
rear->next = NULL;
}
template <class T>
LinkList<T>:: ~LinkList()
{
Node<T>* p, *q;
p = head;
while (p)
{
q = p;
p = p->next;
delete q;
}
head = NULL;
}
2.3 SeqList.h
#ifndef SEQLIST_H
#define SEQLIST_H
#include<vector>
#include<iostream>
using namespace std;
template <class T, int MaxSize>
class SeqList
{
T data[MaxSize];
int length;
public:
SeqList();
SeqList(T a[], int n);
friend void SeqListMerge(SeqList<T, MaxSize> &S1, SeqList<T, MaxSize>&S2)
{
vector<int>s;
int i = 0;
int j = 0;
while (i < S1.length&&j < S2.length)
{
if (S1.data[i] < S2.data[j])
{
s.push_back(S1.data[i]);
i++;
}
else
{
s.push_back(S2.data[j]);
j++;
}
}
if (i < S1.length)
{
for (int k = i; k < S1.length; k++)
s.push_back(S1.data[k]);
}
else
{
for (int k = j; k < S2.length; k++)
s.push_back(S2.data[k]);
}
for (auto e : s)
{
cout << e << " ";
}
cout << endl;
}
};
#endif
2.4 SeqList.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"SeqList.h"
#include<iostream>
template <class T, int MaxSize>
SeqList<T, MaxSize >::SeqList()
{
length = 0;
}
template <class T, int MaxSize>
SeqList<T, MaxSize>::SeqList(T a[], int n)
{
if (n>MaxSize)
{
cerr << "参数非法"; exit(1);
}
for (int i = 0; i<n; i++)
data[i] = a[i];
length = n;
}
2.5 test.cpp
#define _CRT_SECURE_NO_WARNINGS 1
#include"LinkList.cpp"
#include"SeqList.cpp"
#include<iostream>
using namespace std;
void menu()
{
cout << "|--------------------------------------------|" << endl;
cout << "|欢迎来到两个有序线性表的合并,请选择实现方式|" << endl;
cout << "|****************** 0.退出 **************|" << endl;
cout << "|****************** 1.顺序表 **************|" << endl;
cout << "|****************** 2.链表 **************|" << endl;
cout << "|--------------------------------------------|" << endl;
}
int main()
{
cout << "请输入第一个有序线性表的长度和内容\n" << endl;
int *a;
int n;
cin >> n;
a = new int[n];
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
cout << "请输入第二个有序线性表的长度和内容\n" << endl;
int *b;
int m;
cin >> m;
b = new int[m];
for (int i = 0; i < m; i++)
{
cin >> b[i];
}
SeqList<int, 20>Seq1(a, n);
SeqList<int, 20>Seq2(b, m);
LinkList<int> Link1(a, n);
LinkList<int>Link2(b, m);
int input = 0;
do
{
menu();
cin >> input;
switch (input)
{
case 1:
SeqListMerge(Seq1,Seq2);
break;
case 2:
LinkListMerge(Link1, Link2);
break;
case 0:
cout << "感谢您的使用,程序退出" << endl;
break;
default:
cout << "输入错误,请重新输入,或者,您可以选择0来终止程序" << endl;
}
} while (input);
return 0;
}
|