2.数组
- 格式:pdf
- 大小:575.37 KB
- 文档页数:21
作为抽象数据类型的数组顺序表(Sequential List)多项式抽象数据类型(Polynomial ADT)稀疏矩阵(Sparse Matrix)字符串(String)1作为抽象数据类型的数组2一维数组的特点连续存储的线性聚集(别名除第一个元素外,其他每一个元素有一个且仅有一个直接前驱。
除最后除最后一个元素外,其他每一个元素有一个且仅有一个直接后继。
3数组的定义和初始化#include <iostream.h> class szcl{int e;public:szcl( ) { eszcl ( int valueint get_value}szcl a1[3] =for ( int i=0,cout<< a//打印静态数组elem= &a1for(=0,for ( int i0,cout << elem//打印动态数组elem++;}return 0;}#include<iostream.h>#include<stdlib.h> template <class Type>class Array {Type*elementsint ArraySizevoid getArraypublic:Array(int Size=DefaultSizeArray(const~Array( )Array<Type>( constType& operatoArray<Type> operator Type *{{ returnint Length{ returnvoid ReSize}template <class Type>void Array<Type>:://私有函数:创建数组存储空间elements =if(l一维数组公共操作的实现if ( elements ==cerr<< "Memory Allocation Error"<<endl;}template <class Type>void Array<Type>:://构造函数if ( sz <=0 )cerr << "Invalid Array Size" <<return;return;}ArraySize = szgetArray( )}template <class Type>Array( const Array//复制构造函数int n = ArraySize = x.ArraySizeelements= new Typeif ( elements ==(cerr<< "Memory Allocation Error"<< endl;Type *srcptr = x.elementsType*destptr = elementswhile( n--}template <class Type>Type & Array//按数组名及下标if ( i < 0||cerr << "Index out of Range" << returnreturn element}使用该函数于赋值语句Position[i] = void Array<Type>::if ( sz>= 0 &&Type * newarray =if ( newarray ==cerr<< "Memory Allocation Error" << int n = ( sz <= ArraySizeType*srcptr = elementsType *destptr = newarraywhile( n--)* destptr++ = * srcptr++delete[ ]elementselements = newarray}}行向量下标i 页向量下标i 列向量下标j 行向量下标j列向量下标k数组的连续存储方式一维数组(i LOC LOC ( i ) = LOC ( i -1 ) + l =α+ i*l14•二维数组⎜⎜⎜⎜⎜⎛=]][[]][[]][[020100a a a a a a a #⎜⎝−[]][[01n a n a 行优先LOC n 维数组各维元素个数为下标为i 1, i 储地址:n j k j i m m i i i LOC ⎜⎜⎝⎛∗=∗∗+∑∏−==113221,,("顺序表(Sequential List 顺序表的定义和特点 定义n(a i是表项,特点遍历逐项访问从前向后顺序表(SeqList template <class Type> Type *dataint MaxSize;int last;public:SeqList ( int~SeqList( )int Lengthint Find (int Insert (int Removeint Next ( Typeint Prior (int IsEmptyp yint IsFull( )Type& Getreturn i <}}顺序表部分公共操作的实现template <class Type> SeqList<Type>:://构造函数if>0)if (sz > 0 )MaxSize = szdata = new Type}}template <class Type>int SeqList <Type>::{//搜索函数:在表中从前向后顺序查找int i = 0;(i <=while ( i i++;if ( i > last else return }顺序搜索图示x = 48 x = 5022搜索成功若搜索概率相等,则ACN=1n 搜索不成功1=n n ACN i ∗=∑= 表项的插入1)(1 0)1(11)(11=0n n n i n n AMN ni +=++++=−+∑="int SeqList<Type>:://在表中第i个位置插入新元素if( i < 0||i > lastreturn0else {last++;for (intdata[i]= xreturn1}}表项的删除∑−=−=−=−−111)(11)(1=ninnninnAMNint SeqList<Type>:://在表中删除已有元素int i = Find (if (i >= 0) {last--;(for ( int j=idata[j]return1;}return0; }顺序表的应用:template <class Type> void Union(int n = LA.Length int m = LB.Length for ( int i=Type x = LB.Getint k = LA.Findif ( k =={ LA.Insert}}template <class Type>void Intersection int n = LA.Length int m = LB.Length hil (顺序表的应用:while ( i < n Type x = LA.Get int k = LB.Find if ( k ==else i++; }}ni n a x P ∑=== )(多项式(Polynomialn 阶多项式 系数 指数class Polynomial public:Polynomial int operator 多项式(Polynomial)Coefficient Coef Exponent LeadExp Polynomial Polynomial float Eval }#include <iostream.h>class power {double x ;int e ;创建power 类,计算double mul ;public:power (double double get_power };power::power//按val值计算乘幂x=val; eif(exp ==for( ;exp>0}main( ) {power pwrcout << pwr}多项式的存储表示第一种:private:(静态数int组表示)floatP xn(pl.degreepl.coef i34第二种:private:(动态数组表示)Polynomial}以上两种存储表示适用于指数连续排列的多项式。
但对于指数不全的多项式如P101(x) = 3 + 5第三种:class Polynomialclass termfriend Polynomialprivate:fl tfloat coefint exp;};36class Polynomial public:……private:static term termArray static intstatic int free//term Polynomial // int Polynomialint start, finish}A(x) = 2.0x1000B(x) = 1.2 + 51.3两个多项式存放在termArray中两个多项式的相加结果多项式另存扫描两个相加多项式,若都未检测完: 若当前被检测项指数相等,系数相加。
若未变成加若未变成多项式。
若当前被检测项指数不等,将指数小者加到结果多项式。
若有一个多项式已检测完,将另一个多项式剩余部分复制到结果多项式。
Polynomial Cint a = start;float c;while ( a <= finish Switch( comparetermArraycase ‘=’ :c = termArrayif ( c) NewTerma++;b++NewTermtermArray b++;break; case '<': NewTermtermArray a++;}for(;a<=finish NewTermtermArrayfor(;b<=B.finishNewTermtermArray C.finish = free return C;}在多项式中加入新的项void Polynomial//把一个新的项加到多项式if ( free >= maxTermscout << "Too many terms in polynomials”<<endl;return;}termArray[freetermArray[freefree++;}⎜⎜⎜⎛11稀疏矩阵(Sparse Matrix)⎜⎜⎜⎜⎜⎝=×91A76行数m= 6, 列数template <class Type>class SparseMatrix int Rows, Cols, Terms Trituple <Type>public: S M t i SparseMatrix SparseMatrix SparseMatrix Add ( SparseMatrix SparseMatrix Multiply }template <class Type> class template <class Type> class friend class SparseMatrix private:i t l int row, col ;Type value ; }r a w ⎜⎜⎜⎜⎜⎜⎜⎜⎝=×091000A 76稀疏矩阵⎜⎜⎜⎜⎜⎜⎜⎜⎜⎝⎛=×150022000B67转置矩阵用三元组表表示的稀疏矩阵及其转置行(r o w ) 列(c o l ) (v [0] 0 3 [1] 0 6 [2]11[3] 1 5 [4] 2 3 [5] 3 5 [6] 4 0 [7] 5 2稀疏矩阵转置算法思想设矩阵列数为描Cols次。