两种存储结构,两种处理方式。
顺序表:
#include<iostream>
#define MAX 100
using namespace std;
enum status {
success, failed, underflow, overflow
};
template<class T>
class SqList {
public:
SqList();
int length()const;
status get_element(const int i, T& x)const;
int locate(const T x)const;
status push(const T x);
status insert(const int i, const T x);
status delete_element(const int i);
void sort();
void traverse();
private:
T data[MAX];
int count;
};
template<class T>
SqList<T>::SqList() {
count = 0;
}
template<class T>
int SqList<T>::length()const {
return count;
}
template<class T>
int SqList<T>::locate(const T x)const {
for (int i = 0; i < length(); i++) {
if (data[i] == x) return i + 1;
}
return 0;
}
template<class T>
status SqList<T>::push(const T x) {
if (length() == MAX) {
cout << "溢出" << endl;
exit(0);
}
else {
data[count] = x;
count++;
return success;
}
}
template<class T>
status SqList<T>::get_element(const int i, T& x)const {
if (i<1 || i>length()) {
cout << "输入错误,抛出" << endl;
exit(0);
}
else {
x = data[i - 1];
return success;
}
}
template<class T>
status SqList<T>::delete_element(const int i) {
if (length() == 0) return underflow;
if (i<1 || i>length()) {
cout << "输入错误,抛出" << endl;
exit(0);
}
for (int j = i - 1; j < length() - 1; j++) {
data[j] = data[j + 1];
}
count--;
return success;
}
template<class T>
status SqList<T>::insert(const int i, const T x) {
if (count == MAX) return overflow;
if (i<1 || i>length()) return failed;
for (int j = i; j < length() + 1; j++) {
data[j] = data[j - 1];
}
data[i - 1] = x;
count++;
return success;
}
template<class T>
void SqList<T>::sort()
{
for (int i = 0; i < length()-1; ++i)
{
int end = i;
int tem = data[end + 1];
while (end >= 0)
{
if (tem < data[end])
{
data[end + 1] = data[end];
end--;
}
else
{
break;
}
}
data[end + 1] = tem;
}
}
template<class T>
void SqList<T>::traverse() {
for (int i = 0; i < length(); i++) {
cout << data[i] << " ";
}
cout << endl;
}
先合并再排序:
template<class T>
void merge_to_sort(SqList<T> a, SqList<T> b, SqList<T>& c) {
T x;
for (int i = 1; i <= a.length(); i++) {
a.get_element(i, x);
c.push(x);
}
for (int i = 1; i <= b.length(); i++) {
b.get_element(i, x);
c.push(x);
}
c.sort();
}
先排序再合并:
template<class T>
void sort_to_merge(SqList<T> a,SqList<T> b, SqList<T>& c) {
a.sort();
b.sort();
int len1 = a.length();
int len2 = b.length();
int i = 1; T elem1;
int j = 1; T elem2;
while (i <= len1 && j <= len2) {
if (i <= len1) {
a.get_element(i, elem1);
}
if (j <= len2) {
b.get_element(j, elem2);
}
while (i <= len1 && j <= len2 && elem1 <= elem2) {
c.push(elem1);
++i;
if (i <= len1) {
a.get_element(i, elem1);
}
}
while (i <= len1 && j <= len2 && elem2 < elem1) {
c.push(elem2);
++j;
if (j <= len2) {
b.get_element(j, elem2);
}
}
}
while (i <= len1) {
if (i <= len1) {
a.get_element(i, elem1);
}
c.push(elem1);
++i;
}
while (j <= len2) {
if (j <= len2) {
b.get_element(j, elem2);
}
c.push(elem2);
++j;
}
}
链表:
#include<iostream>
using namespace std;
enum status {
success, failed, underflow, overflow
};
template<class T>
struct node {
T data;
node<T>* next;
};
template<class T>
class LinkedList {
public:
LinkedList();
~LinkedList();
int length()const;
status get_element(const int i, T& x);
node<T>* locate(const T x) const;
void push(const T x);
status insert(const int i, const T x);
status delete_element(const int i);
void traverse();
void sort();
private:
int count;
node<T>* head;
};
template<class T>
LinkedList<T>::LinkedList() {
head = new node<T>;
head->next = nullptr;
count = 0;
}
template<class T>
LinkedList<T>::~LinkedList() {
}
template<class T>
int LinkedList<T>::length()const {
return count;
}
template<class T>
node<T>* LinkedList<T>::locate(const T x)const {
node<T>* pos = head->next;
while (pos) {
if (pos->data == x) return pos;
else pos = pos->next;
}
return nullptr;
}
template<class T>
void LinkedList<T>::push(const T x) {
node<T>* t = new node<T>;
t->data = x;
t->next = nullptr;
if (head->next == nullptr) {
head->next = t; // 空链表,直接将新节点作为头节点的下一个节点
count++;
}
else {
node<T>* cur = head;
while (cur->next) {
cur = cur->next;
}
cur->next = t; // 将新节点插入到链表的末尾
count++;
}
}
template<class T>
status LinkedList<T>::insert(const int i, const T x) {
if (i<1 || i>length()) return failed;
node<T>* t = new node<T>;
t->data = x;
node<T>* cur = head;
int j = 0;
for (; j < i - 1; j++) {
if (!cur) return failed;
else cur = cur->next;
}
t->next = cur->next;
cur->next = t;
count++;
return success;
}
template<class T>
status LinkedList<T>::get_element(const int i, T& x) {
node<T>* cur = head->next;
int j = 1;
while(j < i) {
if (!cur) return failed;
else {
cur = cur->next;
++j;
}
}
x = cur->data;
return success;
}
template<class T>
status LinkedList<T>::delete_element(const int i) {
if (i<1 || i>length()) {
cout << "输入有误,抛出" << endl;
exit(0);
}
node<T>* cur = head;
int j = 0;
for (; j < i - 1; j++) {
if (!cur) return failed;
else cur = cur->next;
}
node<T>* pos = cur->next;
cur->next = pos->next;
delete pos;
count--;
return success;
}
template<class T>
void LinkedList<T>::traverse() {
node<T>* cur = head->next;
while (cur) {
cout << cur->data << " ";
cur = cur->next;
}
cout << endl;
}
template<class T>
void LinkedList<T>::sort() {
if (!head || !head->next) return;
node<T>* dummy = new node<T>;
dummy->next = head;
node<T>* pre = head; // 当前节点的前驱
node<T>* cur = head->next;
node<T>* tmp = dummy; //tmp用于记录上一次的插入位置
while (cur) {
if (cur->data < tmp->data) tmp = dummy; //如果当前值比上一次插入点的值要小,就只能从头查找插入点,否则,插入点的查找可以从上一个插入点之后开始查找
if (pre->data >= cur->data) { //需要进行插入
//tmp开始寻找插入位置
while (tmp->next->data < cur->data) tmp = tmp->next; // cur应该插入在tmp后面
pre->next = cur->next; //断开节点cur
cur->next = tmp->next; //插入
tmp->next = cur;
cur = pre->next; //继续处理下一个节点
}
else { //无需插入
pre = pre->next;
cur = cur->next;
}
}
}
先排序再合并:
template<class T>
void sort_to_merge(LinkedList<T> a, LinkedList<T> b, LinkedList<T>& c) {
a.sort();
b.sort();
int len1 = a.length();
int len2 = b.length();
int i = 1; T elem1;
int j = 1; T elem2;
while (i <= len1 && j <= len2) {
if (i <= len1) {
a.get_element(i, elem1);
}
if (j <= len2) {
b.get_element(j, elem2);
}
while (i <= len1 && j <= len2 && elem1 <= elem2) {
c.push(elem1);
++i;
if (i <= len1) {
a.get_element(i, elem1);
}
}
while (i <= len1 && j <= len2 && elem2 < elem1) {
c.push(elem2);
++j;
if (j <= len2) {
b.get_element(j, elem2);
}
}
}
while (i <= len1) {
if (i <= len1) {
a.get_element(i, elem1);
}
c.push(elem1);
++i;
}
while (j <= len2) {
if (j <= len2) {
b.get_element(j, elem2);
}
c.push(elem2);
++j;
}
}
先合并再排序:
template<class T>
void merge_to_sort(LinkedList<T> a, LinkedList<T> b, LinkedList<T>& c) {
T x;
for (int i = 1; i <= a.length(); i++) {
a.get_element(i,x);
c.push(x);
}
for (int i = 1; i <= b.length(); i++) {
b.get_element(i, x);
c.push(x);
}
c.sort();
}