Download presentation
Presentation is loading. Please wait.
1
Chapter 3 Linear List (Sequential List)
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 Sequential List
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( const Array<Type>& x ); // Constructor (copy) Prof. Q.Wang
6
~Array( ) { delete [ ]elements;} //Deconstructor
Array <Type> & operator = // Array duplication ( const Array <Type> & A ); Type& operator [ ] ( int i ); // Get the element of the array Type* operator ( ) const // Conversion of the pointers { return elements; } int Length ( ) const // Get the length of array { return ArraySize; } void ReSize ( int sz ); // Enlarge the size of the array } Prof. Q.Wang
7
Implementation of Methods for 1D Array Memory allocation
template <class Type> void Array <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 Array Constructor
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 ( const Array<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 Array operator []
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 Array Resize
template <class Type> void Array<Type>::Resize (int sz) { 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
Type *srcptr = elements; Type *destptr = newarray;
while ( n-- ) * destptr++ = * srcptr++; delete [ ] elements; elements = newarray; ArraySize = sz; } 2D Array 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 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 Sequential List 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) where a1 is the item or element of list , n is 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> class SeqList { private: Type *data; // Array to store the sequential list int MaxSize; // Maximize the size of list int last; // Length of the list public: SeqList ( int MaxSize = defaultSize ); ~SeqList ( ) { delete [ ] data; } int Length ( ) const { return last+1; } int Find ( Type & x ) const; Prof. Q.Wang
20
int Insert ( Type & x, int i ); int Remove ( Type & x );
int IsIn ( Type & x ); int Insert ( Type & x, int i ); int Remove ( Type & x ); int Next ( Type & x ) ; int Prior ( Type & x ) ; int IsEmpty ( ) { return last ==-1; } int IsFull ( ) { return last == MaxSize-1; } Type Get ( int i ) { return i < 0 || i > last?NULL : data[i]; } Prof. Q.Wang
21
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; } Find Prof. Q.Wang
22
Implementation of Methods for Sequential List
template <class Type> int SeqList<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
Complexity Analysis of Searching
If success Average Comparison Number (ACN) is If fail to search x, Actual comparison number is n Insert element Prof. Q.Wang
25
Insert an item into the List
Average Move Number (AMN) is Prof. Q.Wang
26
Insert an item into the List
template <class Type> int SeqList<Type>::Insert ( Type & x, int i ){ // 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 } Remove element Prof. Q.Wang
27
Remove an item from the List
Average Move Number (AMN) is Prof. Q.Wang
28
Remove an item from the List
template <class Type> int SeqList<Type>::Remove ( Type & x ) { // Remove existed item x from the list int i = 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 Application Prof. Q.Wang
29
Application of Sequential List (1)
Union of two sets a2 b2 a1 b1 an bm ai bi a1, a2, …,ai… an, b1, b2, …, bi… bm, N Insert Get an item bi Get another item Y Prof. Q.Wang
30
Union of two sets template <class Type>
void Union ( 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
{ LC.Insert (k, x); i++; k++;} // Insert x into LC else
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) has n+1 items。
Coefficients: a0, a1, a2, …, an Exponentials: 0, 1, 2, …, n。 ascending Prof. Q.Wang
37
Polynomial ( ); //Constructor int operator ! ( ); //Is zero-polynomial
ADT of Polynomial class Polynomial { public: Polynomial ( ); //Constructor int operator ! ( ); //Is zero-polynomial float Coef ( int e); int LeadExp ( ); //return max-exp Polynomial Add (Polynomial poly); Polynomial Mult (Polynomial poly); float Eval ( float x); //compute the value of the PN } Prof. Q.Wang
38
To computer the power of x, (Power Class)
#include <iostream.h> class power { double x; int e; double mul; //The value of ex public: power (double val, int exp); //constructor double get_power ( ) { return mul; } //Get ex }; Prof. Q.Wang
39
power::power (double val, int exp) { //Computer the power of val xe
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
Polynomial::Polynomial (int sz) { degree = sz;
2nd method: private: int degree; float * coef; Polynomial::Polynomial (int sz) { degree = sz; coef = new float [degree + 1]; } The 1st and 2nd storages are NOT suitable for the following case P101(x) = 3 + 5x x101 Prof. Q.Wang
42
class term { //item definition
3rd method: class Polynomial; 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 free space // term Polynomial::termArray[MaxTerms]; // int Polynomial::free = 0; int start, finish; //start and finish pos of the items of //Polynomial } Prof. Q.Wang
44
A(x) = 2.0x1000+1.8 Two polynomials are stored in termArray
Examples: Two polynomials are stored in termArray A(x) = 2.0x B(x) = x x101 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 exps are 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;
int a = start; int b = 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
for ( ; b <= B.finish; b++ ) //B has remains
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 for ( ; b <= B.finish; b++ ) //B has remains C.finish = free-1; return C; Prof. Q.Wang
48
Add a new item in the polynomial
void Polynomial::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
Points of Chapter 3 Array Sequential List Polynomial ADT of array
Methods Sequential List ADT Applications Polynomial ADT and Representation Addition STL Prof. Q.Wang
50
Standard Template Library
STL Overview Standard Template Library Dr. Qing Wang
51
Introduction to the Standard Template Library
STL Overview Containers Iterators Algorithms STL Overview Prof. Q.Wang
52
STL Overview The library is organized into three main abstractions
Containers Iterators Algorithms Prof. Q.Wang
53
Detail of containers The containers include strings, vectors, lists, sets, stacks, and the like. The containers are organized as a collection of independent C++ classes. All the STL containers classes are template classes. Prof. Q.Wang
54
Containers Prof. Q.Wang
55
Iterators Iterators provide a uniform interface between containers and algorithms in the STL. Iterators are modeled after plain C++ pointers. In particular, operators operator*, operator++ and so forth are properly overloaded, so that the use of iterators is very similar to the use of pointers. Prof. Q.Wang
56
Iterators Prof. Q.Wang
57
Algorithms The core of the STL is its extensive collection of algorithms. Insertion Removal Retrieval Modification Traversal Replacement Sorting …… Prof. Q.Wang
58
Example of Algorithms Prof. Q.Wang
59
A Tour of the Standard Library
C++reference C++ Reference A Tour of the Standard Library C++ glossary Sequential List using STL Prof. Q.Wang
60
Implementation of Sequential List using STL
Dr. Qing Wang
61
Contents String Array STL sequence containers Vector Deque
Prof. Q.Wang
62
Using the Standard string Class
C-style Strings The standard string class provides support for character strings. String class provides ease of use, convenience, and safety that C-style strings lack. Prof. Q.Wang
63
Using the Standard string Class
Prof. Q.Wang
64
Using the Standard string Class
Advanced String Operations Prof. Q.Wang
65
Using the Standard string Class
In C++, the string data type provides the necessary abstraction to allow C++ programmers to work with character strings. C++ string Ref Prof. Q.Wang
66
Array C++ provides basic support for a sequence of homogeneous data objects through arrays. Prof. Q.Wang
67
Example Prof. Q.Wang
68
Using the Standard vector Class
Vector as an Array Class More safer than array Templates supported Prof. Q.Wang
69
Using the Standard vector Class
declare a vector of vector objects an implementation for a two-dimensional data structure such as a matrix. Prof. Q.Wang
70
Using the Standard vector Class
Constructor Prof. Q.Wang
71
Using the Standard vector Class
Element access Other member functions Prof. Q.Wang
72
Using the Standard vector Class
Vector as an STL Container As an STL container, class vector provides access to its elements via iterators. Iterators allow interoperability of vector objects and the STL algorithms. Prof. Q.Wang
73
Using the Standard vector Class
Iterators return iterators to the invoking vector. begin returns an iterator to the first element in the vector end returns an iterator positioned beyond the last element in the vector Reverse iterators Prof. Q.Wang
74
Using the Standard vector Class
Insert and erase functions Take vector iterators as parameters. These iterators indicate the location to insert, or the elements to erase. Prof. Q.Wang
75
Using the Standard vector Class
C++ Vectors Vectors contain contiguous elements stored as an array. Accessing members of a vector or appending elements can be done in constant time, whereas locating a specific value or inserting elements into the vector takes linear time. C++ vector Ref Prof. Q.Wang
76
Using the STL deque Container
Double-ended Queue Interface can store and provide access to a linear sequence of elements C++ deque Ref Prof. Q.Wang
77
STL deque push_front and pop_front Prof. Q.Wang
78
STL deque count and count_if functions:
Counting the number of items that possess certain properties Prof. Q.Wang
79
Difference between vector and deque
Storage strategies Vectors reserve memory only at the rear of stored elements Deques reserve memory locations at both the front and rear of their stored elements Prof. Q.Wang
80
Difference between vector and deque
Operations The same ones push_back pop_back For deque only push_front pop_front For vector Alternative method: insert or erase the first element Prof. Q.Wang
81
Summary Containers Iterators Algorithms Sequences Vector Deque
Prof. Q.Wang
82
Quiz Quiz1 Prof. Q.Wang
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.