当前位置:文档之家› 数据结构—矩阵课后题

数据结构—矩阵课后题

P-219-29Ttemplate<class T>T** LowerMatrix<T>::operator*(const LowerMatrix<T>& m) const{ if(n!=m.n) throw SizeMismatch();T** w = new T *[n];for(int i=0;i<n;i++){w[i] = new int[n];}int ct=0,cm=0;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){T sum = t[ct]*m.t[cm];for(int k=j;k<i;k++){ct++;cm++;sum += t[ct]*m.t[cm];}w[i-1][j-1]=sum;if(i<j){w[i-1][j-1] = 0;}ct = i*(i-1)/2+j-1;cm = j;}ct = i*(i+1)/2;cm = 0;}return w;}函数时间复杂性为O(n^3);P-219-30Ttemplate<class T>T** UpperMatrix<T>::operator*(const LowerMatrix<T>& m) const{ int front=0;if(n!=m.n) throw SizeMismatch();T** c = new T *[n];for(int i=0;i<n;i++){c[i] = new int[n];}int ct=0,cm=0;for(int i=0;i<n;i++){for(int j=0;j<n;j++){c[i][j]=0;if(i<=j)front=j;else front=i;for(int k=front;k<n;k++){ct = i*(2*n-i)/2+k-i;cm = k*(k+1)/2+j;c[i][j] += t[ct]*m.t[cm];}}}return c;}函数时间复杂性为O(n^3);P-237-36Ttemplate<class T>SparseMatrix<T>& SparseMatrix<T>::Store(const int& x,int i,int j){ if(i<1 || j<1 || i>rows || j>cols ) throw OutOfBounds();if(terms = = 0){if(x ==0 )return *this;a[0].row = i;a[0].col = j;a[0].value = x;terms++;return *this;}int location = a[0].row*cols + a[0].col;int other = i*cols + j;int k = 0;while(k<terms && other>location){k++;if(k!=terms)location = a[k].row*cols + a[k].col;}if(k == terms){if(terms = = MaxTerms) throw OutOfBounds();a[k].row = i;a[k].col = j;a[k].value = x;terms++;}if(other = = location){a[k].row = i;a[k].col = j;a[k].value = x;}else{if(terms = = MaxTerms) throw OutOfBounds();for(int l=k;l<terms;l++)a[l+1] = a[l];a[k].row = i;a[k].col = j;a[k].value = x;terms++;}return *this;}template<class T>T SparseMatrix<T>::Retrieve(int i,int j) const{if(i<1 || j<1 || i>rows || j>cols) throw OutOfBounds();int location = a[0].row*cols + a[0].col;int other = i*cols + j;int k = 0;while(k<terms && other>location){k++;if(k!=terms)location = a[k].row*cols + a[k].col;}if(other == location)return a[k].value;elsereturn 0;}Store函数和Retrieve 函数的时间复杂性均为O(terms)P-237-43template<class T>SparseMatrix<T>& SparseMatrix<T>::Multiply(SparseMatrix<T>& m,SparseMatrix<T>& n){if(cols!=m.rows) throw SizeMismatch();n.rows = rows;n.cols = m.cols;n.terms = 0;int *RowNext,RowSize;RowSize = new int[m.rows+1];for(int i=1;i<=m.rows;i++){RowSize[i] = 0;}for(int i=0;i<=m.rows;i++)RowSize[m.a[i].row]++;RowNext[1]=0;for(int i=2;i<=m.rows;i++)RowNext[i] = RowNext[i-1] + RowSize[i-1];int **t = new int[rows];for(int i=0;i<rows;i++){t[i] = new int[m.cols];}for(int i=0;i<rows;i++){for(int j=0;j<m.cols;j++){t[i][j] = 0;}}for(int i=0;i<terms;i++){for(int j=RowNext[a[i].col];j<RowNext[a[i].col+1];j++) t[a[i].row][a[j].col] += a[i].value * m.a[j].value;}for(int i=0;i<rows;i++){for(int j=0;j<m.cols;j++){if(t[i][j]){Term<T> b;b.row = i;b.col = j;b.value = t[i][j];Append(b);}}}}P-247-1T(1)template<class T>T Stack<T>::Length()const{return top+1;}(2)template<class T>void Stack<T>::Input(){cout<<"Please Enter the length of the stack "<<endl;int len;cin>>len;if(len<0 || len-1>MaxTop) throw BadInitializers();for(int i=0;i<len;i++){cin>>stack[i];top++;}}(3)template<class T>void Stack<T>::Output() const{for(int i=0;i<=top;i++){cout<<stack[i]<<" ";}cout<<endl;}P-247-2T(1)template<class T>void Stack<T>::Split(Stack<T>& a,Stack<T>& b){if(top/2 > a.MaxTop) throw NoMem();if(top/2-1 > b.MaxTop) throw NoMem();for(int i=0; i<(top+2)/2; i++)a.stack[i] = stack[i];a.top =(top+2)/2-1;for(int i=0; i<top/2; i++)b.stack[i] = stack[(top+2)/2+i];b.top = top/2-1;}(2)template<class T>void Stack<T>::Merge(Stack<T>& a){if(a.top+top+1>MaxTop) throw OutOfBounds();for(int i=0;i<=top;i++)a.stack[a.top+i+1] = stack[i];a.top += top+1;top = -1;}P-290-1T (1)int Length() const{if(IsEmpty()) throw OutOfBounds();return (rear+MaxSize-front)%MaxSize; }P-291-4T#include"except.h"template<class T>class Queue{public:Queue(int MaxQueueSize = 10);~Queue(){delete []queue;}bool IsEmpty() const {return front==rear&&LastOp==0;} bool IsFull() const {return rear==front&&LastOp==1);}int Length() const;T First() const;T Last() const;Queue<T>& Add(const T& x);Queue<T>& Delete(T& x);private:int front;int rear;int LastOp;int MaxSize;T *queue;}template<class T>Queue<T>::Queue(int MaxQueueSize){MaxSize = MaxQueueSize;queue = new T[MaxSize];front = rear = 0;int LastOp = 0;//0为空,1为满}template<class T>int Queue<T>::Length() const{int a = rear+MaxSize-front;if(rear==front){if(lastop==0)return MaxSize;elsereturn 0;}return a % maxsize;}template<class T>T Queue<T>::First() const{if (IsEmpty()) throw OutOfBounds();return queue[front];}template<class T>T Queue<T>::Last() const{if (IsEmpty()) throw OutOfBounds();return queue[rear];}template<class T>Queue<T>& Queue<T>::Add(const T& x){ if(IsFull()) throw NoMem();rear = (rear+1)%MaxSize;queue[rear] = x;LastOp = 1;return *this;}template<class T>bool Queue<T>::Delete(T& x){if(IsEmpty()) throw BadInitializers();front = (front+1) % MaxSize;x = queue[front];LastOp = 0;return true;}P-297-7T#include"except.h"template<class T>class Node{friend LinkedQueue<T>;private:T data;Node<T> *link;};template<class T>class LinkedQueue{public:LinkedQueue(){front = rear =0;}~LinkedQueue(){};bool IsEmpty() const{return ((front)? false:true;) }bool IsFull()const;T First() const;T Last() const;int Length() const;void Input();void Output();LinkedQueue<T>& Add(const T& x);LinkedQueue<T>& Delete(T& x); private:Node<T>*front;Node<T>*rear;};template<class T>LinkedQueue<T>::~LinkedQueue(){ Node<T> *next;while (front){next = front->link;delete front;front = next;}}template<class T>bool LinkedQueue<T>::IsFull() const{ Node<T> *p;try {p = new Node<T>;delete p;return false;}catch (NoMem){return true;}}template<class T>int LinkedQueue<T>::Length() const{ Node<T> *p = front;int i = 0;for(;p;){i++;p = p->link;}return i;}template<class T>void LinkedQueue::Input(){Node<T> *p;int len;cout<<"Please enter the length of the Queue: "<<endl;cin>>len;front = rear = 0;for (int i=0;i<len;i++){p = new Node<T>;cin>>p->data;p->link = 0;if(i)rear->link = p;elsefront = p;rear = p;}}template<class T>void LinkedQueue<T>::Output(){Node<T> *p = front;for(;p;){cout<<p->data<<" ";p = p->link;}cout<<endl;}template<class T>T LinkedQueue<T>::First() const{if (IsEmpty) throw OutOfBounds();return front->data;}template<class T>T LinkedQueue<T>::Last() const{if (IsEmpty) throw OutOfBounds();return rear->data;}template<class T>LinkedQueue<T>& LinkedQueue<T>::Add(const T& x){Node<T> *p = new Node<T>;p->data = x;p->link = 0;if(front) rear->link = p;else front = p;rear = p;return *this;}template<class T>LinkedQueue<T>& LinkedQueue<T>::Delete(T& x){if (IsEmpty) throw OutOfBounds();x = front->data;Node<T> *p = front;front = front->link;delete p;return *this;}P-324-17T可以把堆栈换成队列,但换成队列后效率会降低,时间复杂性增高。

相关主题