 Download Download Presentation Prof. Qing Wang

# Prof. Qing Wang

Download Presentation ## Prof. Qing Wang

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. Lecture-03 Lecture Notes: Data Structures and Algorithms Chapter 3 Linear List (Sequential List) Prof. Qing Wang

2. Table of Contents • Arrays and ADTs of Array • 1D array • ADT of array • 2D and high dimensional arrays • Sequential List • ADT of sequential list • Applications • Polynomial ADT and Representation • Polynomial ADT • Sequential Representation of PolyN • Addition of Polynomial Prof. Q.Wang

3. 3.1 Arrays and ADTs • One dimensional array • An example Prof. Q.Wang

4. Arrays and ADTs • Characteristics of one dimensional array • In contiguous storage, also called as Vector • Except the first element of an array, other elements have and only have one predecessor • Except the last element of the array, other elements have and only have one successor First element Last element Prof. Q.Wang

5. ADT of 1D Array (Class Definition) #include <iostream.h> #include <stdlib.h> template <class Type> class Array { private: Type*elements;//Space to store array int ArraySize;// Length of array void getArray ( );// Initialization of storage for array public: Array( int Size=DefaultSize );// Constructor Array( constArray<Type>& x );// Constructor (copy) Prof. Q.Wang

6. ~Array( ) { delete [ ]elements;} //Deconstructor Array <Type> &operator= // Array duplication ( constArray <Type> & A); Type& operator [ ] ( inti ); // Get the element of the array Type* operator ( )const// Conversion of thepointers { return elements; } intLength ( ) const// Get the length of array { return ArraySize; } voidReSize ( intsz ); // Enlarge the size of the array } Prof. Q.Wang

7. Implementation of Methods for 1D ArrayMemory allocation template <class Type> voidArray <Type>::getArray ( ) { // Private Function: Allocate the space for the array elements = new Type[ArraySize]; if ( elements == 0 ) { arraySize = 0; cerr << "Memory Allocation Error" << endl; return; } } Prof. Q.Wang

8. Implementation of Methods for 1D ArrayConstructor template <class Type> Array <Type>::Array ( int sz ) { // Constructor Function of Array ADT if ( sz <= 0 ) { arraySize = 0; cerr << “Invalid size of array!” <<endl; return; } ArraySize = sz; getArray ( ); } Prof. Q.Wang

9. Implementation of Methods for 1D Array Copy Constructor template <class Type> Array<Type>:: Array ( constArray<Type>& x ) { // Constructor Function (with copy) of Array ADT int n = ArraySize = x.ArraySize; elements = new Type[n]; if ( elements == 0 ) { arraySize = 0; cerr<< “Error of Memory Allocation” << endl; return; } Type *srcptr = x.elements; Type*destptr = elements; while( n-- ) * destptr++ = * srcptr++; } Prof. Q.Wang

10. Implementation of Methods for 1D Arrayoperator [] template <class Type> Type & Array<Type>::operator [ ] ( int i ) { // Get the i-th element of array of Array ADT if ( i < 0|| i > ArraySize-1 ) { cerr << “The i is exceed the bound of the array” <<endl; return NULL; } return element[i]; } Prof. Q.Wang

11. Implementation of Methods for 1D ArrayResize template <class Type> voidArray<Type>::Resize (intsz) { if ( sz >= 0 &&sz != ArraySize ) { Type * newarray =new Type[sz]; if ( newarray == 0 ) { cerr<< “Error of Memory Allocation” <<endl; return; } int n = ( sz <= ArraySize ) ? sz : ArraySize; Prof. Q.Wang

12. 2D Array Type*srcptr = elements; Type *destptr = newarray; while ( n-- ) * destptr++ = * srcptr++; delete [ ] elements; elements = newarray; ArraySize = sz; } } Prof. Q.Wang

13. 2D Array Row subscript i, Column subscript j Prof. Q.Wang

14. 3D Array Page subscript i, Row subscript j, Column subscript k Prof. Q.Wang

15. Sequential Storage of Arrays • 1D array LOC ( i ) = LOC ( i -1 ) + l =α+ i*l Prof. Q.Wang

16. Sequential Storage of Arrays • 2D array Row-first: LOC ( j, k ) = a + ( j * m + k ) * l Prof. Q.Wang

17. Sequential List Sequential Storage of Arrays • N-D array • The dimensions are m1, m2, m3, …, mn • The element with subscripts (i1, i2, i3, …, in) is in the space： LOC ( i1, i2, …, in ) = a + ( i1*m2*m3*…*mn + i2*m3*m4*…*mn+ + ……+ in-1*mn + in) * l Prof. Q.Wang

18. 3.2 Sequential List (Sequence) • Definition and Property of Sequential List • Definition: A list is a finite, ordered sequence of data items （a1, a2, …, an） wherea1 is the item or element of list , nis the length of the list • Property: sequential access (put and get) • Important concept: List element has a position. • Traversal: • from the first to the last • from the last to the first • from the intermediate position to the head or the end Prof. Q.Wang

19. ADT of Sequential List (Class Definition) template <class Type> classSeqList { private: Type *data; // Array to store the sequential list int MaxSize; // Maximize the size of list intlast; // Length of the list public: SeqList ( int MaxSize = defaultSize ); ~SeqList ( ){ delete [ ] data; } int Length ( )const{ returnlast+1;} int Find ( Type& x ) const; Prof. Q.Wang

20. int IsIn ( Type& x ); intInsert ( Type & x, inti ); intRemove ( Type& x ); int Next ( Type& x ) ; int Prior ( Type& x ) ; int IsEmpty ( ){ returnlast ==-1;} intIsFull ( ){ return last == MaxSize-1;} TypeGet (int i ) { returni < 0|| i > last？NULL:data[i]; } } Prof. Q.Wang

21. Find Implementation of Methods for Sequential List template <class Type> SeqList<Type> :: SeqList ( int sz ) { // Constructor Function if ( sz > 0 ){ MaxSize = sz; last = -1; data = new Type[MaxSize]; if ( data == NULL ) { MaxSize = 0; last = -1; return; } } } Prof. Q.Wang

22. Implementation of Methods for Sequential List template <class Type> intSeqList<Type>::Find ( Type& x ) const { // Searching Function: try to find out the position of x int i = 0; while( i <= last && data[i]!= x ) i++; if ( i > last ) return-1; else return i; } Prof. Q.Wang

23. Details of Searching in the List Prof. Q.Wang

24. Insert element Complexity Analysis of Searching • If success Average Comparison Number (ACN) is • If fail to search x, Actual comparison number is n Prof. Q.Wang

25. Insert an item into the List • Average Move Number (AMN) is Prof. Q.Wang

26. Remove element Insert an item into the List template <class Type> intSeqList<Type>::Insert (Type& x,inti ){ // Insert a new item with (x) before pos i in the list if ( i < 0||i > last+1||last == MaxSize-1 ) return 0; // Fail to insert else { last++; for (int j = last; j > i; j--) // Move elements data[j] = data[j -1]; data[i] = x; return 1; // Success to insert } } Prof. Q.Wang

27. Remove an item from the List • Average Move Number (AMN) is Prof. Q.Wang

28. Application Remove an item from the List template <class Type> intSeqList<Type>::Remove (Type& x ) { // Remove existed item x from the list inti = Find (x); // Search x in the list if ( i >= 0) { last--; for ( int j = i; j <= last; j++ ) data[j] = data[j+1]; // Move elements return 1; // Success to remove x } return 0; // No removal if no item x } Prof. Q.Wang

29. Application of Sequential List (1) • Union of two sets b2 a2 b1 a1 bm an bi ai a1, a2, …,ai… an, b1, b2, …, bi… bm, Insert N Get an item bi Get another item Y Prof. Q.Wang

30. Union of two sets template <class Type> voidUnion ( SeqList<Type>& LA, SeqList<Type>& LB ) { int n = LA.Length ( ); int m = LB.Length ( ); for ( int i = 1; i <= m; i++ ) { Type x = LB.Get(i); // Get an item x from Set LB int k = LA.Find (x); // Search x in Set LA if ( k ==-1 ) // if not found, insert x into LA { LA.Insert (x, n+1); n++; } } } Prof. Q.Wang

31. Application of Sequential List (2) • Intersection of two sets b2 a2 b1 a1 bm an bi ai Remove from A a1, a2, …,ai… an, Y Get an item ai Get another item N Prof. Q.Wang

32. Intersection of two sets template <class Type> void Intersection ( SeqList<Type>& LA, SeqList<Type>& LB ) { int n = LA.Length ( ); int m = LB.Length ( ); int i = 0; while ( i < n ) { Type x = LA.Get (i); // Get an item x from LA int k = LB.Find (x); // Search x in Set LB if ( k ==-1 ) { LA.Remove (i); n--; } else i++; // if not found, remove x from LA } } Prof. Q.Wang

33. Application of Sequential List (3) • Merge two sorted lists into a new list and the new one is also sorted as before. i LA= (3, 5, 8, 11) LB= (2, 6, 8, 9, 11, 15, 20) j LC= (2, 3, 5, 6, 8, 8, 9, 11, 11, 15, 20) Merge k Prof. Q.Wang

34. Application of Sequential List (3) • Implementation template <class Type> SeqList &Merge_List ( SeqList <Type>& LA, SeqList <Type>& LB ) { int n = LA.Length ( ); int m = LB.Length ( ); SeqList LC(m+n); int i=j=k=0; while ( i < n && j<m) { Type x = LA.Get (i); // Get an item x from LA Type y = LB.Get (j); // Get an item y from LB Prof. Q.Wang

35. if (x <= y ) { LC.Insert (k, x); i++; k++;}// Insert x into LC else { LC.Insert (k, y); j++; k++;} } while ( i < n) { // Insert the remains of LA into LC Type x = LA.Get (i); LC.Insert (k, x); i++; k++; } while (j<m) { // Insert the remains of LB into LC Type y = LB.Get (j); LC.Insert (k, y); j++; k++; } return LC; } Prof. Q.Wang

36. 3.3 Polynomial • N-order polynomial Pn(x)hasn+1items。 • Coefficients: a0, a1, a2, …, an • Exponentials: 0, 1, 2, …, n。 ascending Prof. Q.Wang

37. ADT of Polynomial class Polynomial{ public: Polynomial ( ); //Constructor int operator !( ); //Is zero-polynomial float Coef ( int e); intLeadExp ( ); //return max-exp PolynomialAdd (Polynomialpoly); PolynomialMult (Polynomial poly); float Eval ( floatx); //compute the valueof the PN } Prof. Q.Wang

38. To computer the power of x, (Power Class) #include <iostream.h> classpower { doublex; inte; doublemul;//The value ofex public: power (doubleval, int exp);//constructor doubleget_power ( ) { return mul; } //Getex }; Prof. Q.Wang

39. power::power (doubleval, intexp) { //Computer the power of valxe x = val; e = exp; mul = 1.0; if ( exp == 0 ) return; for ( ;exp>0;exp--) mul = mul * x; } main ( ) { power pwr ( 1.5, 2 ); cout << pwr.get_power ( ) << “\n”; } Prof. Q.Wang

40. Representation of Polynomial (storage) 1st method： private: int degree; float coef [maxDegree+1]; Pn(x):pl.degree = n pl.coef[i] = ai, 0 in Prof. Q.Wang

41. 2nd method： private: intdegree; float * coef; Polynomial::Polynomial (intsz) { degree = sz; coef = new float [degree + 1]; } The 1st and 2nd storages are NOT suitable for the following case P101(x) = 3 + 5x50 - 14x101 Prof. Q.Wang

42. 3rd method: classPolynomial; class term {//item definition friend Polynomial; //PN class is the friend class of item class private: float coef; //coefficient int exp; //exponential }; Prof. Q.Wang

43. class Polynomial { //Polynomial class public: …… private: static term termArray[MaxTerms]; //items static int free; //pos of current freespace // termPolynomial::termArray[MaxTerms]; // int Polynomial::free = 0; int start, finish; //start and finish pos of the items of //Polynomial } Prof. Q.Wang

44. Examples: Two polynomials are stored in termArray A(x) = 2.0x1000+1.8 B(x) = 1.2 + 51.3x50 + 3.7x101 Prof. Q.Wang

45. Addition of Polynomials • Requirement • The summarization polynomial is an new one • Method • To traverse two polynomials (A and B) until one of them has been traversed; • If the expsare equal, add two coefs. • If the addition of coefs are not equal to 0, new a item and append it into C, otherwise continue to traverse. • If the exps are not equal, add the item whose exp is lower into C. • If one of A and B has been traversed completely, it is easy to duplicate the remains of another one into C Prof. Q.Wang

46. Polynomial Polynomial::Add (Polynomial B) { Polynomial C; inta = start;intb = B.start;C.start = free; float c; while ( a <= finish && b <= B.finish ) Switch( compare ( termArray[a].exp, termArray[b].exp) ) {//compare case ‘=’ : //exps are equal c = termArray[a].coef + //coef termArray[b].coef; if ( c ) NewTerm ( c, termArray[a].exp ); a++; b++;break; Prof. Q.Wang

47. case ‘>’ :// new item with item b in C NewTerm ( termArray[b].coef, termArray[b].exp ); b++;break; case '<':// new item with item a in C NewTerm ( termArray[a].coef, termArray[a].exp ); a++; } for ( ; a <= finish; a++ ) //A has remains NewTerm ( termArray[a].coef, termArray[a].exp ); for ( ; b <= B.finish; b++ ) //B has remains NewTerm ( termArray[b].coef, termArray[b].exp ); C.finish = free-1; returnC; } Prof. Q.Wang

48. Add a new item in the polynomial voidPolynomial::NewTerm ( float c,int e ) { // Add a new item into polynomial if ( free >= maxTerms ) { cout << "Too many terms in polynomials” <<endl; return; } termArray[free].coef = c; termArray[free].exp = e; free++; } Prof. Q.Wang

49. STL Points of Chapter 3 • Array • ADT of array • Methods • Sequential List • ADT • Methods • Applications • Polynomial • ADT and Representation • Addition Prof. Q.Wang

50. STL Overview Standard Template Library Dr. Qing Wang