Data structure C # language version chapter 2nd linear Table (3)

Source: Internet
Author: User

Previous: http://www.bkjia.com/kf/201201/116965.html

C ++ implements an ordered table


[Cpp] // Algri. h
# Include "stdafx. h"
# Include <iostream>
# Include <vector>
 
Using std: cout;
Using std: vector;
 
Namespace Jeffery
{
Template <class T>
Class SeqList
{
Public:
Int Maxsize;
Private:
Vector <T> data;
Int Last;
Public:
T & operator [] (size_t index ){
Return data [index];
}
 
SeqList (int size ){
Data = vector <T> (size );
Maxsize = size;
Last =-1;
}
 
Void Append (T item ){
If (IsFull ())
Cout <"List is full ";
Else
Data [++ Last] = item;
}
 
Void Clear (){
Last =-1;
}
 
T Delete (int I ){
T tmp;
If (IsEmpty ())
Cout <"List is empty ";
Else if (I <0 | I> Last)
Cout <"Position is error! ";
Else if (I = Last)
Tmp = data [Last --];
Else {
Tmp = data [I];
For (int j = I + 1; j <= Last; ++ j ){
Data [J-1] = data [j];
}
-- Last;
}
Return tmp;
}
 
T GetItem (int I ){
If (IsEmpty () | I <0 | I> Last ){
Cout <"List is empty or position is error! ";
Return T ();
}
Else
Return data [I];
}
 
Int GetLength (){
Return Last + 1;
}
 
Void Insert (T item, int I ){
If (IsFull ())
Cout <"List is full ";
Else if (I <0 | I> = Last + 2)
Cout <"Position is error! ";
Else if (I = Last + 1)
Data [++ Last] = item;
Else {
For (int j = Last ++; j> = I; -- j ){
Data [j + 1] = data [j];
}
Data [I] = item;
}
}
Bool IsEmpty (){
If (Last =-1)
Return true;
Else
Return false;
}
Bool IsFull (){
If (Last = Maxsize-1)
Return true;
Else
Return false;
}
 
Int Locate (T value ){
If (IsEmpty ()){
Cout <"List is Empty! ";
Return-1;
}
For (int I = 0; I <= Last; ++ I ){
If (value = data [I])
Return I;
}
Return-1;
}
};
}
// Algri. h
# Include "stdafx. h"
# Include <iostream>
# Include <vector>

Using std: cout;
Using std: vector;

Namespace Jeffery
{
Template <class T>
Class SeqList
{
Public:
Int Maxsize;
Private:
Vector <T> data;
Int Last;
Public:
T & operator [] (size_t index ){
Return data [index];
}

SeqList (int size ){
Data = vector <T> (size );
Maxsize = size;
Last =-1;
}

Void Append (T item ){
If (IsFull ())
Cout <"List is full ";
Else
Data [++ Last] = item;
}

Void Clear (){
Last =-1;
}

T Delete (int I ){
T tmp;
If (IsEmpty ())
Cout <"List is empty ";
Else if (I <0 | I> Last)
Cout <"Position is error! ";
Else if (I = Last)
Tmp = data [Last --];
Else {
Tmp = data [I];
For (int j = I + 1; j <= Last; ++ j ){
Data [J-1] = data [j];
}
-- Last;
}
Return tmp;
}

T GetItem (int I ){
If (IsEmpty () | I <0 | I> Last ){
Cout <"List is empty or position is error! ";
Return T ();
}
Else
Return data [I];
}

Int GetLength (){
Return Last + 1;
}

Void Insert (T item, int I ){
If (IsFull ())
Cout <"List is full ";
Else if (I <0 | I> = Last + 2)
Cout <"Position is error! ";
Else if (I = Last + 1)
Data [++ Last] = item;
Else {
For (int j = Last ++; j> = I; -- j ){
Data [j + 1] = data [j];
}
Data [I] = item;
}
}
Bool IsEmpty (){
If (Last =-1)
Return true;
Else
Return false;
}
Bool IsFull (){
If (Last = Maxsize-1)
Return true;
Else
Return false;
}

Int Locate (T value ){
If (IsEmpty ()){
Cout <"List is Empty! ";
Return-1;
}
For (int I = 0; I <= Last; ++ I ){
If (value = data [I])
Return I;
}
Return-1;
}
};
} [Cpp] # include "stdafx. h"
# Include "Algri. h"
# Include <iostream>
 
Using namespace std;
Using namespace Jeffery;
 
Int _ tmain (int argc, _ TCHAR * argv [])
{
SeqList <int> li = SeqList <int> (10 );
For (int I = 0; I <8; I ++)
{
Li. Append (I );
}
 
For (int I = 0; I <li. GetLength (); I ++)
{
Cout <li [I] <",";
}
Cout <endl;
 
Li. Insert (100,5 );
For (int I = 0; I <li. GetLength (); I ++)
{
Cout <li [I] <",";
}
Cout <endl;
 
Li. Insert (1000,9 );
For (int I = 0; I <li. GetLength (); I ++)
{
Cout <li [I] <",";
}
Cout <endl;
 
 
Li. Delete (2 );
For (int I = 0; I <li. GetLength (); I ++)
{
Cout <li [I] <",";
}
Cout <endl;
 
Cout <li. GetItem (7) <endl;
Cout <li. Locate (4) <endl;
Return 0;
}
# Include "stdafx. h"
# Include "Algri. h"
# Include <iostream>

Using namespace std;
Using namespace Jeffery;

Int _ tmain (int argc, _ TCHAR * argv [])
{
SeqList <int> li = SeqList <int> (10 );
For (int I = 0; I <8; I ++)
{
Li. Append (I );
}

For (int I = 0; I <li. GetLength (); I ++)
{
Cout <li [I] <",";
}
Cout <endl;

Li. Insert (100,5 );
For (int I = 0; I <li. GetLength (); I ++)
{
Cout <li [I] <",";
}
Cout <endl;

Li. Insert (1000,9 );
For (int I = 0; I <li. GetLength (); I ++)
{
Cout <li [I] <",";
}
Cout <endl;


Li. Delete (2 );
For (int I = 0; I <li. GetLength (); I ++)
{
Cout <li [I] <",";
}
Cout <endl;

Cout <li. GetItem (7) <endl;
Cout <li. Locate (4) <endl;
Return 0;
}

From xufei96's column
 

Related Article

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.