数据结构、算法与应用(c++描述)答案

       答案纯自己手撕,很多地方做的不到位的欢迎私信或者留言。希望正在啃这本书的同学也能把答案分享在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>&
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值