代码开始前的闲谈
计算机协会C++培训的数据结构部分现在开始了,所用的书是 《数据结构》科技出版社 作者 吴陈 教授。
作者也就是我的老师。感谢老师一学期的辛勤工作。虽然老师可能还不认识我。。。由于课本是用 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; }