要求
- 使用题目 链表实现 中实现的链表类、迭代器类完成本题
- 不得使用与题目实现相关的STL
描述
给定两组整数序列,你需要分别创建两个有序链表,使用链表迭代器实现链表的合并,并分别输出这三个有序链表的索引与元素的异或和。
Note: 给定序列是无序的,你需要首先得到一个有序的链表
格式
输入
第一行两个整数 N 和 M。
第二行 N 个整数,代表第一组整数序列。
第三行 M 个整数,代表第二组整数序列。
输出
三行整数。分别代表第一组数、第二组数对应的有序链表与合并后有序链表的索引与元素的异或和。
链表异或和的计算方式如下:
f(chain)=i=0∑n−1i⊕chain[i],n=len(chain)
样例 1
输入
3 0
3 1 2
输出
5
0
5
#include <iostream>
using namespace std;
// 定义节点
template<class T>
struct chainNode{
// 数据成员
T element; // 节点中数据
chainNode<T> *next; // 节点的指针
// 方法
// 构造函数
chainNode(){}
chainNode(const T& element){this->element = element;} //
chainNode(const T& element, chainNode<T>* next){
this->element = element;
this->next = next;
}
};
// 链表类
template<class T>
class chain{
public:
// friend class iterator;
// chain<T>::iterator;
// 构造函数,复制构造函数和析构函数
chain(int initialCapacity = 10);
chain(const chain<T>&);
~chain();
// 抽象数据类型ADT的方法
bool empty() const {return listSize == 0;}
int size() const{return listSize;}
// int indexOf(const T& theElement)const; // 查询
int erase(T& theElement); // 删除
void insert(int theIndex, const T& theElement); // 插入
int output(); // 输出
int get();
// void merge(); // 链表置换
// void Binsort(); // 箱子排序
void Bubblesort(); // 冒泡排序
void merge(chain<T> &a, chain<T>& b); // 合并
// 迭代器
class iterator{
public:
iterator(chainNode<T>* theNode = NULL){node = theNode;}
// 重载引用操作符
T& operator*() const{return node->element;}
T& operator->()const{return &node->element;}
// T& operator()
// 迭代器加法操作
iterator& operator++(){ // 前加
node = node->next;
return *this;
}
iterator operator++(int){ // 后加
iterator old = *this;
node = node->next;
return old;
}
// 相等检验
bool operator != (const iterator right) const{return node != right.node;}
bool operator == (const iterator right) const{return node == right.node;}
protected:
chainNode<T>* node;
};
iterator begin(){return iterator(firstNode);}
iterator end(){return iterator(NULL);}
protected:
void checkIndex(int theIndex)const; //如果索引无效,抛出异常
chainNode<T>* firstNode; // 指向链表第一个节点的指针
int listSize; // 线性表的元素个数
};
// 构造函数
template<class T>
chain<T>::chain(int initialCapacity){
//构造函数
firstNode = NULL;
listSize = 0;
}
template <class T>
chain<T>::~chain(){
while (firstNode!=NULL){
chainNode<T> *nextnode=firstNode->next;
delete firstNode;
firstNode=nextnode;
}
}
// 输出
template<class T>
int chain<T>::output(){
int j = 0;
int a = 0;
for(chain<T>::iterator i(firstNode); i != this->end(); j++, i++){
a = a + ((*i) ^ j);
}
return a;
}
// template<class T>
// int chain<T>::get(){
// return element;
// }
// 插入元素theElement并使其索引为theIndex(插入)
template<class T>
void chain<T>::insert(int theIndex, const T& theElement){
if(theIndex == 0){
// 表头插入
firstNode = new chainNode<T>(theElement, firstNode);
}
else{
// 寻找新元素的前驱
chainNode<T>* p = firstNode;
for(int i = 0; i < theIndex - 1; i++){
p = p->next;
}
// 在p之后插入
p->next = new chainNode<T>(theElement, p->next);
}
listSize++;
}
// 排序
// template<class T>
// void chain<T>::Binsort(){
// // 箱子初始化
// chainNode<T>* bin;
// bin = new chain<T>[listSize + 1];
// for(int i = 0; i < listSize; i++){
// for()
// }
// }
// 排序
template<class T>
void chain<T>::Bubblesort(){
chainNode<T>* p1, *p2;
for(int i = 0; i < listSize-1; i++){
p1 = firstNode;
p2 = p1->next;
T x;
for(int j = 0; j < listSize -1; j++){
if(p1->element > p2->element){
x = p1->element;
p1->element = p2->element;
p2->element = x;
}
p1 = p2;
p2 = p2->next;
}
}
}
// 合并
template<class T>
void chain<T>::merge(chain<T> &a, chain<T> &b){
// chain<T> *l = new chain<T>();
// l.firstNode = a.firstNode;
chain<T>::iterator i(a.firstNode);
chain<T>::iterator j(b.firstNode);
int x = 0;
// chain<T>::iterator w(l->firstNode);
while(j != NULL && i != NULL){
if(*i > *j){
// w = j;
insert(x, *j);
j++;
x++;
}
else{
insert(x, *i);
i++;
x++;
}
}
while(i != NULL){
insert(x, *i);
i++;
x++;
}
while(j != NULL){
insert(x, *j);
j++;
x++;
}
}
// 置换
// template<class T>
// void chain<T>::zhihuan(){
// chainNode<T>* p1, *p2;
// p1 = firstNode;
// p2 = p1->next;
// while(p2 != NULL){
// p1->next = p2->next;
// p2->next = firstNode;
// firstNode = p2;
// p2 = p1->next;
// }
// // p2 = firstNode;
// // p2->next = p3;
// // p1 = p1->next;
// // p1->next = NULL;
// }
int main(){
int N, M;
cin >> N >> M;
chain<int> chain1(N), chain2(M), chain3(M + N);
for(int i = 0; i < N; i++){
int c = 0;
cin >> c;
chain1.insert(i, c);
}
for(int j = 0; j < M; j++){
int d = 0;
cin >> d;
chain2.insert(j, d);
}
int aa[3];
chain1.Bubblesort();
chain2.Bubblesort();
aa[0] = chain1.output();
aa[1] = chain2.output();
chain3.merge(chain1, chain2);
aa[2] = chain3.output();
for(int i = 0; i < 3; i++){
cout << aa[i] << endl;
}
return 0;
}