Sequence Table of Study Notes of Data Structure (Yin Ren Yi Edition)

Source: Internet
Author: User

FalseCodeI sorted it out:

/*************************************** ************************************

* File name: linearlist. h
* Created: 2011/11/01
* Description:
* Author: Gavin Dai xlx
* Update:

**************************************** ************************************/
# Ifndef _ linearlist_h
# DEFINE _ linearlist_h

/*
This is a pure virtual class, which is equivalent to an interface
*/
Template <class T>
Class linearlist
{

Public:
 
Linearlist (){};

~ Linearlist (){};

Virtual int size () const = 0; // calculate the table capacity

Virtual int length () const = 0; // calculate the number of elements in the current table

Virtual int search (T & X) const = 0; // search for X in the table

Virtual int locate (int I) const = 0; // locate the position of the I-th element in the table

Virtual bool getdata (int I, T & X) const = 0; // obtain the value of the I element in the table

Virtual void setdata (int I, T & X) const = 0; // you can specify the value of X for the I element in the table.

Virtual bool insert (int I, T & X) = 0; // insert X after entry I of the table

Virtual bool remove (int I, T & X) = 0; // Delete the entry in the I table, return through X

Virtual bool isempty () const = 0;

Virtual bool isfull () const = 0;

// Virtual void sort () = 0; // sort

Virtual void input () = 0; // Input

Virtual void output () = 0; // output

// Virtual linearlist <t> operator = (linearlist <t> & L) = 0;

};

# Endif

 

/*************************************** ************************************

* File name: seqlist. h
* Created: 2011/11/01
* Description:
* Author: Gavin Dai xlx
* Update:

**************************************** ************************************/
# Ifndef _ seqlist_h
# DEFINE _ seqlist_h

# Include "linearlist. H"
# Include <iostream>
# Include <cstdlib>
Using namespace STD;

Const int defaultsize = 100;

Template <class T>
Class seqlist: Public linearlist <t>
{

Public:
 
Seqlist (int sz = defaultsize );
 
Seqlist (seqlist <t> & L );

~ Seqlist () {Delete [] data ;}

Int size () const {return maxsize ;}

Int length () const {return last + 1 ;}

Int search (T & X) const;

Int locate (int I) const;

Bool getdata (int I, T & X) const
{
If (I> 0 & I <= last + 1)
{
X = data [I-1];
Return true;
} Else
{
Return false;
}
}

Void setdata (int I, T & X) const
{
If (I> 0 & I <= last + 1)
{
Data [I-1] = X;
}
}

Bool insert (int I, T & X );

Bool remove (int I, T & X );

Bool isempty () const
{
Return (last =-1 )? True: false;
}

Bool isfull () const
{
Return (last = maxsize-1 )? True: false;
}

Void input ();

Void output ();

Seqlist <t> & operator = (seqlist <t> & L );

Protected:
 
T * data;
Int maxsize;
Int last;
Void resize (INT newsize );
};

# Endif

 

/*************************************** ************************************

* File name: seqlist. cpp
* Created: 2011/11/01
* Description:
* Author: Gavin Dai xlx
* Update:

**************************************** ************************************/
# Include "seqlist. H"

Template <class T>
Seqlist <t>: seqlist (INT sz/* = defaultsize */)
{
If (SZ> 0)
{
Maxsize = SZ;
Last =-1;
Data = new T [maxsize];
If (Data = NULL)
{
Cerr <"Storage Allocation Error" <Endl;
Exit (1 );
}
}
}

Template <class T>
Seqlist <t >:: seqlist (seqlist <t> & L)
{
Maxsize = L. Size ();
Last = L. Length ()-1;
T value;
Data = new T [maxsize];
For (INT I = 1; I <= last + 1; I ++)
{
Value = getdata (I, value );
Data [I-1] = value;
}
}

template
void seqlist : resize (INT newsize)
{< br> If (newsize <= 0)
{< br> cerr <"invalid array size"
return;
}< br> If (newsize! = Maxsize)
{< br> T * newarray = new T [newsize];
If (null = newarray)
{< br> cerr <"Storage Allocation Error"
exit (1 );
}< br> int n = last + 1;
T * srcptr = data;
T * dstptr = newarray;
while (n --)
{< br> * dstptr ++ = * srcptr ++;
}< br> Delete [] data;
DATA = newarray;
maxsize = newsize;
}< BR >}

Template <class T>
Int seqlist <t >:: search (T & X) const
{
For (INT I = 0; I <= last; I ++)
{
If (data [I] = X)
{
Return I + 1;
}
}
Return 0;
}

Template <class T>
Int seqlist <t>: locate (int I) const
{
If (I> = 1 & I <= last + 1)
{
Return I;
}
Return 0;
}

Template <class T>
Bool seqlist <t>: insert (int I, T & X)
{
If (last = maxsize-1)
{
Return false;
}
If (I <0 | I> last + 1)
{
Return false;
}
For (Int J = last; j> = I; j --)
{
Data [J + 1] = data [J];
}
Data [I] = X;
Last ++;
Return true;
}

Template <class T>
Bool seqlist <t>: Remove (int I, T & X)
{
If (last =-1)
{
Return false;
}
If (I <1 | I> last + 1)
{
Return false;
}
X = data [I-1];
For (Int J = I; j <= last; j ++)
{
Data [J-1] = data [J];
}
Last --;
Return true;
}

Template <class T>
Void seqlist <t>: input ()
{
Cout <"to create an order table, enter the number of elements in the table:" <Endl;
While (1)
{
Cin> last;
If (last <= maxsize-1)
{
Break;
}
Cout <"the number of elements in the table is incorrect. The value range cannot exceed" <maxsize-1 <";";
}
For (INT I = 0; I <= last; I ++)
{
Cin> data [I];
Cout <I + 1 <Endl;
}
}

Template <class T>
Void seqlist <t>: output ()
{
Cout <"the last position of the current element in the sequence table is:" <last <Endl;
For (INT I = 0; I <= last; I ++)
{
Cout <"#" <I + 1 <":" <data [I] <Endl;
}
}

Template <class T>
Seqlist <t> & seqlist <t>: Operator = (seqlist <t> & L)
{
Maxsize = L. Size ();
Last = L. Length ()-1;
T value;
Data = new T [maxsize];
For (INT I = 1; I <= last + 1; I ++)
{
Value = getdata (I, value );
Data [I-1] = value;
}
Return * this;
}

/*************************************** ************************************

* File name: Main. cpp
* Created: 2011/11/01
* Description:
* Author: Gavin Dai xlx
* Update:

**************************************** ************************************/

# Include "seqlist. cpp"

Int main ()
{
Seqlist <int> L (100 );
Seqlist <int> q;
L. Input ();
L. Output ();
Cout <L. Length ();
Int I = 22;
L. insert (1, I );
L. Output ();
Int idata = 89;
L. setdata (1, idata );
L. Output ();
L. Remove (3, idata );
Cout <"idata =" <idata;
Q = L;
Cout <"elements of table Q :";
Q. Output ();
System ("pause ");
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.