更多课程 选择中心

C/C++培训
达内IT学院

400-111-8989

C++_数据结构_线性表_普通线性表

  • 发布:wang_huizhang
  • 来源:果核里的图灵
  • 时间:2017-11-08 15:39

代码开始前的闲谈

数据结构部分现在开始了,所用的书是 《数据结构》科技出版社 作者 吴陈 教授。

作者是我校的老师。感谢老师一学期的辛勤工作。由于课本是用 V C 6.0 写的。所以我这里用 vs 2015.。

包含的主要知识点:

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

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

3.线性表的普通写法。

线性表的一般写法

先来张图证明一下我的代码没问题。。。(总是有眼神不好用的说我代码出问题,结果是他自己抄错了一些细节。)

“ 头文件:

#pragma once

#define MaxSize 100

#include<ostream>

#include<iostream>

using namespace std;

template<class T>

class LinearList;

template<class T>

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

template<typename T>

void PrintList2(LinearList<T> & a);

template<class T>

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<T>& a);//重载输出线性表

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

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

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

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

};

template<typename T>

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

{

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

{

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

}

os << '\n';

return os;

}

template<typename T>

void PrintList2(LinearList<T> & 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<typename T>

LinearList<T>::LinearList()

{

Length = 0;

}

template<typename T>

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

{

this->Length = n;

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

{

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

}

}

template<typename T>

LinearList<T>::~LinearList()

{

Length = 0;

}

template<typename T>

int LinearList<T>::GetLength()

{

return this->Length;

}

template<typename T>

T LinearList<T>::GetPos(int pos)

{

return this->Data[pos];

}

template<typename T>

void LinearList<T>::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<typename T>

T LinearList<T>::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<typename T>

int LinearList<T>::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<typename T>

void LinearList<T>::PrintList1()

{

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

{

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

}

std::cout << endl;

}

template<typename T>

T LinearList<T>::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<typename T>

T * LinearList<T>::ToFirstAdd()

{

return this->Data;

}

template<typename T>

void LinearList<T>::SetLength(int len)

{

this->Length = len;

} “ 主函数:

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

//

#include "stdafx.h"

#include"LinearList.cpp"

#include<iostream>

using namespace std;

int main()

{

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

LinearList<int> 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;

}

本文内容转载自网络,本着分享与传播的原则,版权归原作者所有,如有侵权请联系我们进行删除!

预约申请免费试听课

填写下面表单即可预约申请免费试听!怕钱不够?可就业挣钱后再付学费! 怕学不会?助教全程陪读,随时解惑!担心就业?一地学习,可全国推荐就业!

上一篇:C++_数据结构_线性表_间接寻址方式储存
下一篇:方法 | 减少C++代码编译时间

超全的C语言标识符知识

C指针——指针类型转换

C指针——指针和结构类型的关系

C指针——数组和指针的关系

  • 扫码领取资料

    回复关键字:视频资料

    免费领取 达内课程视频学习资料

  • 搜索抖音号

    搜索抖音号:1821685962

    免费领取达内课程视频学习资料

Copyright © 2021 Tedu.cn All Rights Reserved 京ICP备08000853号-56 京公网安备 11010802029508号 达内时代科技集团有限公司 版权所有

选择城市和中心
黑龙江省

吉林省

河北省

湖南省

贵州省

云南省

广西省

海南省