第6章线性表——链式描述2.template<class T>T& class chain :: setSize(int theSize){if(theSize<0)cout<<”链表长度值非法!”<<endl;else{inti = 0;//遍历链表chainNode<T>* currentNode = firstNode;while(currentNode != NULL){currentNode = currentNode->next;i++;}if(i>=theSize){currentNode = firstNode;for(int j=0;j<theSize;j++)currentNode = currentNode->next;listSize = theSize;currentNode->next = NULL;}}}3.template<class T>void chain<T>set(int theIndex,const T&theElement){if(theIndex<0 || theIndex>listSize){ostringstream s;s<<”index = ”<<theIndex<<”size = “<<listSize;throwilleagaiIndex(s.str());}chainNode<T>* deleateNode;if(theIndex == 0){deleateNode = firstNode;firstNode = new chainNode<T>(theElement,firstNode);}else{chianNode<T>* p = firstNode;for(int i = 0;i<theIndex-1;i++)p = p->next;deleateNode = p->next;p->next = new chainNode<T>(theElement,p->next);}deleatedeleateNode;}4.template<class T>void chain<T>::removeRange(fromIndex,toIndex){if(fromIndex<0 || fromIndex<toIndex || toIndex>listSize){ostringstream s;s<<”fromIndex= ”<<fromIndex<<”toIndex = ”<<toIndex<<”size = “<<listSize;throwilleagaiIndex(s.str());}chainNode<T> * p = firstNode,q;if(fromIndex == toIndex){chianNode<T>* deleateNode;if(fromIndex == 0){deleateNode = firstNode;firstNode = firstNode->next;}else{for(int i = 0;i<fromIndex-1;i++)p = p->next;deleateNode = p->next;p->next = p->next->next;}listSize--;deleatedeleateNode;}else{inti;for(i = 0;i<fromSize-1;i++)p = p->next;while(i<toSize){chainNode<T>* deleateNode = p;p = p->next;i++;listSize--;deleatedeleateNode;}}}第7章数组和矩阵14.1)#include<iostream>using namespace std;int main(){int **a = new int*[3];//原矩阵int **b = new int*[6];//转置矩阵for (int i = 0; i< 3; i++)a[i] = new int[6];for (int i = 0; i< 6; i++)b[i] = new int[3];int element[18] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17 };int k = 0;for (int i = 0; i< 6; i++){for (int j = 0; j < 3; j++){a[j][i] = element[k];k++;}}cout<< "The old matrix is:" <<endl;for (int i = 0; i< 3; i++){for (int j = 0; j < 6; j++){cout<< a[i][j] << " ";}cout<<endl;}for (int i = 0; i< 6; i++){for (int j = 0; j < 3; j++){b[i][j] = a[j][i];}}cout<< "The new matrix is:" <<endl;for (int i = 0; i< 6; i++){for (int j = 0; j < 3; j++){cout<< b[i][j] << " ";}cout<<endl;}}2)#include<iostream>using namespace std;int main(){int **a = new int*[3];//原矩阵int **b = new int*[6];//转置矩阵for (int i = 0; i< 3; i++)a[i] = new int[6];for (int i = 0; i< 6; i++)b[i] = new int[3];int element[18] = { 0, 3, 6, 9, 12, 15, 1, 4, 7, 10, 13, 16, 2, 5, 8, 11, 14, 17 };int k = 0;for (int i = 0; i< 3; i++){for (int j = 0; j < 6; j++){a[i][j] = element[k];k++;}}cout<< "原矩阵是:" <<endl;for (int i = 0; i< 3; i++){for (int j = 0; j < 6; j++){cout<< a[i][j] << " ";}cout<<endl;}for (int i = 0; i< 6; i++){for (int j = 0; j < 3; j++){b[i][j] = a[j][i];}}cout<< "转置矩阵是:" <<endl;for (int i = 0; i< 6; i++){for (int j = 0; j < 3; j++){cout<< b[i][j] << " ";}cout<<endl;}intct = 0, cm = 0, cw = 0;intcelement[9];for (int i = 1; i<= 3; i++){for (int j = 1; j <= 3; j++){int sum = element[ct] * element[cm]; for (int m = 2; m <= 6; m++){ ct++; cm += 3;sum += element[ct] * element[cm]; }celement[cw++] = sum; ct -= 5; cm = j; }ct += 6; cm = 0; }cout<< "原矩阵与其转置矩阵的积为:" <<endl; for (int i = 0; i< 3; i++){ for (int j = 0; j < 3; j++)cout<<celement[3*i+j]<< " "; cout<<endl; } }18.1)解:由题意得,该矩阵如下所示:1000101001111000001000101111000000000000001001011很明显该矩阵不是对称的,是上三角矩阵,不是下三角矩阵。
2)此小题中矩阵其实就是第一小题中矩阵的转置矩阵1000000000000011001000000000011110110111000011101显然,该矩阵不是对称矩阵,不是上三角矩阵,是下三角矩阵。
39.1)500×500的二维整形数组需要1000000个byte 的空间,而使用sparseMatrix 需要24000个byte 的空间。
2).要有m +n 3个非零元素,才使sparseMatrix 的储存所需要的空间超过m ×n 二维数组所需要的空间。
第八章 栈7、1)i.template<class T>voidarrayStack<T>::Input(){//输入栈T *elements[arrayLength];intft = 0,i=0;cout<<”请输入要输入到栈中的数据,输入-1结束,最多输入”<<arrayLength<<”个:”<<endl;while(ft != -1 &&!(i>LarrayLength)){elements[i] = ft;i++;}if(ft != 1)cout<<”输入元素过多!已强行终止。