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