C++线性表

包含的主要知识点:

1.C++模板类 友元函数的写法。

2.C++模板类 重载cout<< 的写法。

3.线性表的普通写法。

线性表的一般写法

废话少说,直接上代码
头文件:

#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();//无参数构造函数。

LinearList(T a[],int n);//n个数据的 构造函数。

~LinearList();//析构函数。

private:

T Data[MaxSize];

int Length;

public:

int GetLength();//获取线性表储存数据的个数。

T GetPos(int pos);//返回线性表的 第 pos 个数据。

void InsertObjInPos(T Obj, int pos);//在第 pos 个位置 插入Obj

T DeletePos(int pos);//删除第 pos 个位置的数据。

int Locate(T Obj);//查找 数据Obj 的位置。没有则返回 -1。

void PrintList1();

friend ostream& operator<< <>(ostream & os,LinearList& a);//重载输出线性表

friend void PrintList2<>(LinearList & a);//友元函数输出顺序表。

T SetPosToObj(int pos, T Obj);//把第 pos 个位置的数据改成 Obj

T *ToFirstAdd();//返回线性表首地址

void SetLength(int len);//设置线性表长度。

};

template

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

{

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

{

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

}

os << ‘\n’;

return os;

}

template

void PrintList2(LinearList & a)

{

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

{

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

}

std::cout << ‘\n’;

} “ cpp文件:

#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;

} “ 主函数:

// 线性表_普通版.cpp : 定义控制台应用程序的入口点。

//

#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 << “构造函数后顺序表为:” << endl;

a.PrintList1();//第一种方法输出。

std::cout << “在第1个位置插入99” << endl;

a.InsertObjInPos(99, 1);

PrintList2(a);//第二种方法输出。

std::cout << “在第12个位置插入88” << endl;

a.InsertObjInPos(88, 12);

cout << a;//重载输出。

std::cout << “查找 数据 3 的位置:” << a.Locate(3) << endl;

std::cout << “查找到数据 4 并删除后输出:”;

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

cout << a;//再来一个重载输出。其实重载输出还有其他的写法。我这里用了 <> 来写。下一章我会用其他的写法实现重载。

cout << “输出顺序表数组偏移两个地址的元素:”;

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

getchar();

return 0;

}

发布了261 篇原创文章 · 获赞 4 · 访问量 4261

猜你喜欢

转载自blog.csdn.net/it_xiangqiang/article/details/105206322
今日推荐