答案纯自己手撕,很多地方做的不到位的欢迎私信或者留言。希望正在啃这本书的同学也能把答案分享在gitee上面,方便大家一起共同学习,共同进步。目前啃到了第七章的40题,后续会持续的更新,这是我的gitee链接:https://gitee.com/luo_jinchuan/Data-Structures-Algorithm-and-Application.git。
#pragma once
#pragma once
#include"myExceptions.h"
using namespace std;
// 反对角矩阵,从第一列依次存储,第七章
template<class T>
class antidiagonalMatrix
{
friend ostream& operator<<(ostream&, const antidiagonalMatrix<T>&);
public:
antidiagonalMatrix(int theN = 10);
~antidiagonalMatrix() { delete[] element; }
T get(int i, int j) const;
void set(int i, int j, const T& newValue);
void init(T a[], int len);
private:
int n;
T *element;
};
template<class T>
inline antidiagonalMatrix<T>::antidiagonalMatrix(int theN)
{
if (theN < 1)
throw illegalParameterValue("Matrix size must be > 0");
n = theN;
element = new T[n];
}
template<class T>
inline T antidiagonalMatrix<T>::get(int i, int j) const
{
if (i < 1 || j < 1 || i > n || j > n)
throw matrixIndexOutOfBounds();
int len = j - 1;
if (i + len == n)
{
return element[len];
}
return 0;
}
template<class T>
inline void antidiagonalMatrix<T>::set(int i, int j, const T & newValue)
{
if (i < 1 || j < 1 || i > n || j > n)
throw matrixIndexOutOfBounds();
int len = j - 1;
if (i + len == n)
{
element[len] = newValue;
}
throw illegalParameterValue("elements in C matrix must be zero");
}
template<class T>
inline void antidiagonalMatrix<T>::init(T a[], int len)
{
std::copy(a, a + len, element);
}
template<class T>
ostream& operator<<(ostream& out, const antidiagonalMatrix<T>& m)
{
for (int i = 1; i <= m.n; ++i)
{
for (int j = 1; j <= m.n; ++j)
{
T tmp = m.get(i, j);
out << tmp << " ";
}
out << endl;
}
return out;
}
ostream& operator<<(ostream& out, const antidiagonalMatrix<int>& m)
{
for (int i = 1; i <= m.n; ++i)
{
for (int j = 1; j <= m.n; ++j)
{
int tmp = m.get(i, j);
out << tmp << " ";
}
out << endl;
}
return out;
}
// 第六章,链表
#ifndef chain_
#define chain_
#include <iostream>
#include <sstream>
#include <string>
#include "linearList.h"
#include "chainNode.h"
#include "myExceptions.h"
#include"arrayList.hpp"
#include<map>
using namespace std;
class linkedDigraph;
template <class T> class linkedWDigraph;
template<class T>
class chain : public linearList<T>
{
friend linkedDigraph;
friend linkedWDigraph<int>;
friend linkedWDigraph<float>;
friend linkedWDigraph<double>;
template<class T>
friend bool operator==(const chain<T>& x1, const chain<T>& x2);
public:
chain(int initialCapacity = 10);
chain(const chain<T>&);
~chain();
bool empty() const {return listSize == 0;}
int size() const {return listSize;}
T& get(int theIndex) const;
int indexOf(const T& theElement) const;
void erase(int theIndex);
void insert(int theIndex, const T& theElement);
void output(ostream& out) const;
//
void setSize(int theSize);
void set(int theIndex,T theElement);
void removeRange(int fromIndex, int toIndex);
int lastIndexOf(const T& theElement);
T& operator[](int index);
const T& operator[](int index) const;
bool operator!=(const chain<T>& theList) const;
bool operator>(const chain<T>& theList) const;
bool operator<(const chain<T>& theList) const;
bool operator>=(const chain<T>& theList) const;
bool operator<=(const chain<T>& theList) const;
void swap(chain<T>& theChain);
void leftShift(int count);
void reverse();
void meld(chain<T>& lhs, chain<T>& rhs);
void merge(chain<T>& lhs, chain<T>&