C++_008_數據結構_線性表_普通線性表

代碼開始前的閒談

    計算機協會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;
}


發佈了74 篇原創文章 · 獲贊 34 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章