C ++ linear table

The main points include:

1.C ++ writing class friend function template.

2.C ++ template class overrides the wording of cout <<.

3. longhand linear form.

The general pattern of the linear form

Without further ado, directly on the code
header file:

#pragma once

#define MaxSize 100

#include

#include

using namespace std;

template

class LinearList;

template

ostream& operator<<(ostream & os, LinearList & a);

template

void PrintList2(LinearList & a);

template

class LinearList

{

public:

LinearList (); // no-argument constructor.

LinearList (T a [], int n); // constructor n pieces of data.

~ LinearList (); // destructor.

private:

T Data[MaxSize];

int Length;

public:

int GetLength (); // Get the number of the linear data storage table.

T GetPos (int pos); // returns linear list of data pos.

void InsertObjInPos (T Obj, int pos); // Obj inserted at the position pos

T DeletePos (int pos); // delete the data on pos positions.

int Locate (T Obj); // find data Obj position. No -1 is returned.

void PrintList1();

friend ostream & operator << <> (ostream & os, LinearList & a); // overloaded output linear table

friend void PrintList2 <> (LinearList & a); // friend function output sequence table.

T SetPosToObj (int pos, T Obj); // data into the second position pos Obj

T * ToFirstAdd (); // returns the address of the first linear table

void SetLength (int len); // Set the linearization table length.

};

template

ostream& operator<<(ostream & os, LinearList & a)

{

for (int i = 0; i < a.Length;i++)

{

os << a.Data[i]<<" ";

}

os << ‘\n’;

the return;

}

template

void PrintList2(LinearList & a)

{

for (int i = 0; i < a.Length;i++)

{

std::cout << a.Data[i]<<" ";

}

std::cout << ‘\n’;

} "Cpp file:

#include “stdafx.h”

#include “LinearList.h”

template

LinearList::LinearList()

{

Length = 0;

}

template

LinearList::LinearList(T a[], int n)

{

this->Length = n;

for (int i = 0; i < n; i++)

{

this->Data[i] = a[i];

}

}

template

LinearList::~LinearList()

{

Length = 0;

}

template

int LinearList::GetLength()

{

return this->Length;

}

template

T LinearList::GetPos(int pos)

{

return this->Data[pos];

}

template

void LinearList::InsertObjInPos(T Obj, int pos)

{

if (pos > Length + 1||pos<1)

{

throw “InsertObjInPos error! And mostly the position is too long or too short”;

return;

}

this->Length++;

for (int i = Length - 1; i >= pos; i–)

{

this->Data[i] = this->Data[i - 1];

}

this->Data[pos - 1] = Obj;

}

template

T LinearList::DeletePos(int pos)

{

if (pos<1 || pos>this ->Length)

{

throw “DeletePos error and mostly the position is wrong”;

}

T temp = this->Data[pos - 1];

for (int i = pos - 1; i < Length-1; i++)

{

this->Data[i] = this->Data[i + 1];

}

Length–;

return temp;

}

template

int LinearList::Locate(T Obj)

{

int pos = -1;

for (int i = 0; i < Length; i++)

{

if (this->Data[i] == Obj)

{

//return i+1;

pos = i + 1;

return pos;

}

}

return pos;

}

template

void LinearList::PrintList1()

{

for (int i = 0; i < this->Length; i++)

{

std::cout << this->Data[i]<<’ ';

}

std::cout << endl;

}

template

T LinearList::SetPosToObj(int pos, T Obj)

{

if (pos<1 || pos>this - Length+1)

{

throw “DeletePos error and mostly the position is wrong”;

}

if (pos == Length + 1)

{

Length++;

this->Data[pos - 1] = Obj;

}

this->Data[pos - 1] = Obj;

return T();

}

template

T * LinearList::ToFirstAdd()

{

return this->Data;

}

template

void LinearList::SetLength(int len)

{

this->Length = len;

} "Main function:

// linear table _ Normal Edition .cpp: defining the entry point console application.

//

#include “stdafx.h”

#include"LinearList.cpp"

#include

using namespace std;

int main ()

{

int test[10] = { 2,4,6,8,10,12,14,16,18,20 };

LinearList a(test,10);

std :: cout << "order table after the constructor:" << endl;

a.PrintList1 (); // output the first method.

std :: cout << "inserted in the first position 99" << endl;

a.InsertObjInPos(99, 1);

PrintList2 (a); // second output method.

std :: cout << "88 inserted in position 12" << endl;

a.InsertObjInPos(88, 12);

cout << a; // overloaded output.

std :: cout << "Find Data 3 positions:" << a.Locate (3) << endl;

std :: cout << "to find and delete the data output 4:";

a.DeletePos(a.Locate(4));

cout << a; // again overloaded output. In fact, there are other heavy-duty output wording. I use the <> to write. The next chapter I will realize overloaded with other wording.

cout << "offset table array element output order two address:";

std::cout << a.ToFirstAdd()[2]<<endl;

getchar();

return 0;

}

Published 261 original articles · won praise 4 · Views 4261

Guess you like

Origin blog.csdn.net/it_xiangqiang/article/details/105206322