Data structure class notes

template < class T>
 void SeqList <T> :: reSize ( int newsize) 
{ 
    if (newSize! = maxSize) 
    { 
        T * newarray = new T [newSize];
        / * 
        Open up the memory space of size newSize The 
        data type is T         
        * / 
       if (newarray == NULL) 
       { 
        cerr << " Storage allocation failed! " << endl; 
        exit ( 1 ); 
       } 
    } 
    int n = last + 1 ; 
    T * scrptr =data;
    T *destptr=newarray;
    while(n--) *destptr++=*srcptr++;
    delete []data;
    data=newarray;
    maxSize=newSize; 
}


template<class T>
int SeqList<T>::search(T &x) const
{
    for(int i=0;i<=last;i++)
    {
        if(data[i]==x) return i+1;
    }
}

/*
在第i个位置插入x 
插入前:a1,....,ai-1,ai,...,an
After inserting: a1, ..., ai-1, x, ai, ..., an 
The logic between ai-1 and ai has changed 
Table full: last> = MaxSize-1 
last is under the last element array subscript 
1 <= i <= last + 2 
moves, the less the higher the efficiency of the insertion element 
number of the mobile element and the length of the table is not only related to the insertion and location of the 
best case (i = n + 1) basis statement is executed zero , The time complexity O (1) 
* / 

/ * 
the realization of the sequence table-delete 
each move forward is equivalent to overwriting the 
first to determine whether the delete position is legal 
1 <= i <= last + 1 
* / 
template < class T>
 bool SeqList <T> :: Remove ( int i, T & x) 
{ 
    if (last ==- 1 || i < 1 || i> last + 1 ) return  false ; 
    x = data [i- 1 ];// The index of the i-th element array is i-1
    for ( int j = i; j <= last; j ++ ) 
    { 
        data [j - 1 ] = data [j]; // back cover front 
    } 
    last- ; // table length-1 
} 


// input of sequence table Algorithm 
template < class T>
 void SeqList <T> :: input () 
{ 
    cout << " Number of input elements " ;
     while ( 1 ) 
    { 
        cin >> last;
         if (last <= maxSize- 1 ) break ;
        cout << "The number is incorrect " ; 
    } 
    for ( int i = 0 ; i <= last; i ++ ) 
    { 
        cin > data [i]; 
        cout << i + 1 << endl; 
    } 
} 
 
// The sequence table Output algorithm 
template < class T>
 void SeqList <T> :: output () 
{ 
    cout << " The last position of the current element is " << last << endl;
     for ( int i = 0;i<=last;i++)
    {
        cout<<"#"<<i+1<<":"<<data[i]<<endl;
    }
}

//顺序表的应用

//并运算 
void Union(SeqList<int>&A,SeqList<int>& B)
{
    int n=A.length(),x;
    int m=B.length();
    for(int i=1;i<=m;i++)
    {
        B.getData(i,x);Fetch an element in B//
        intk = A.Search (x); // Search it in A 
        if (k == 0 )      // If the last result inserted into A is not found, put the merged result into A (If you reopen the space, it will be wasted) 
        { A.Insert (n, x); n ++ ;} 
    } 
} 

// Intersection operation 
void Intersection (SeqList < int > & A, SeqList < int > & B) 
{ 
    int n = A.Length ();
     int m = B.Length (); int i = 1 , x;
     while (i <= n) 
    { 
        A.getData (i, x); 
        int k = B.search (x);
         if (k == 0) A.Remove (i, x), n-- ;
         else i ++ ; 
    } 
} 

// Advantage of sequence table: 
/ * 
No need to add extra storage space to express the logical relationship between elements in the table. 
Random access can be fast To access the elements at any position in the 
table. The disadvantages of the sequence table: 
(1) Insert and delete operations need to move a large number of elements. 
(2) The capacity of the table is difficult to determine and the capacity of the table is difficult to expand. 
(3) The fragmented 

linked list of the storage space is very Suitable for frequent insertion or deletion, and storage space requirements vary greatly 
* /  


/ * 
Single linked list is the simplest linked list, also called linear linked list. It uses pointers to represent the logical relationship between nodes. 
Node has a 
linear structure of data fields and pointer fields . first pointer to the head 
node can not be stored contiguously continuous 99.99% discontinuous storage 
logical order and the order of the nodes can be different physical 
tables expand easily 
* /  

/ * 
the sequence table is a fixed capacity 
single chain unfixed   
* /  

/ * 
number of classes expression of a concept (single chain) 
list node (ListNode) class 
list (List) class
* / 

// 1. Compound class 1 
class List; // Linked list class definition (composite method) 
class LinkNode 
{ 
    friend class List; // Linked list class is its friend class 
    private :
         int data; 
        LinkNode * link; 
}; 
class List 
{ 
    private : 
        LinNode * first; 
}; 
// Friend class does not have symmetry? ?
// Compound class 2: Define linkNode class 
struct LinkNode 
    with struct 
{ int data: 
    LinkNode * link; 
};
class List 
{ 
    private : 
        LinkNode * first;
     public : 
}; 
/ * 
Although the structure makes List lose its encapsulation, 
all LinkNode nodes belonging to LIst objects can only be accessed with first. 
* /  
// 2. Nested class 
class List 
{ 
    
}   
 
 // 3. Inheritance   
class LinkNode 
{ 
     protected :
          int data; 
         LinkNode * link; 
}; 
class List 
{ 
    
} 
// If you ca n’t finish writing, please go to the PPT.
1

 

Guess you like

Origin www.cnblogs.com/zzyh/p/12761342.html
Recommended