又有一段时间没有更新博客了。在家久了变的异常敏感,什么都会去想,想多了又会自闭。不管咋样,生活还在继续,无法逃避。该面对的还是要直面,就希望安稳毕业,顺利入职。
开始数据结构与c++的混合训练模式。看别人的代码写代码,好像啥都懂。拿开自己写,好像啥都不知道。先从最基本的线性链表的顺序存储结构学起吧。先上代码(包含如下函数功能):
//使用数组存储数据,建立了LinearList类,并用类模板实现
LinearList(); //默认构造函数
LinearList(T *s); //传入数组作为初始化值
LinearList(const LinearList & lt); //复制构造函数
bool ListEmpty() const; //判断列表是否为空
bool ListFull() const; //判断列表是否装满
T GetElem(int i); //得到下标为i的元素
bool LocateElem( T e, int &l) const; //列表中是否有元素e,如果有返回其下标l
bool ListInsert(int i, T e); //在下标i处插入元素e,并返回是否插入成功
bool ListDelete(int i, T &e); //删除下标i处的元素,并返回是否删除成功
const int ListLength(); //返回列表的长度
void show() const; //展示列表的值
LinearList &operator=(const LinearList & lt); //赋值函数
LinearList &operator+(const LinearList & lt1); //加法运算符重载
实现过程中遇到的几个问题需要注意:
1、类模板必须与特定的模板实例化请求一起使用,即要放在一个文件下,不能声明和定义分别放在两个文件里;
2、数组类型不能数组间直接赋值,需要循环赋值;
3、模板函数在定义时都需要之前加上template<typename type>并且要在函数名处也要定义参数;
有关线性链表的顺序存储结构知识点,就不再赘述。我是对照《大话数据结构》学习的,目前为止感觉讲的很不错,浅入深出。此外,实现方法肯定有更加简单或者需要优化的地方,欢迎批评指正。
线性链表的顺序存储结构头文件
#pragma once
#ifndef LINEARLIST_H_
#define LINEARLIST_H_
#include<string>
using namespace std;
#define MaxSize 100
template<typename T>
class LinearList
{
private:
T data[MaxSize];
int length;
public:
LinearList();
LinearList(T *s);
LinearList(const LinearList & lt);
bool ListEmpty() const;
bool ListFull() const;
T GetElem(int i);
bool LocateElem( T e, int &l) const;
bool ListInsert(int i, T e);
bool ListDelete(int i, T &e);
const int ListLength();
void show() const;
LinearList &operator=(const LinearList & lt);
LinearList &operator+(const LinearList & lt1);
};
template<typename T>
LinearList<T>::LinearList()
{
length = 0;
}
template<typename T>
LinearList<T>::LinearList(T *s)
{
int size = strlen(s);
for (int i = 0; i < size; i++)
data[i] = s[i];
length = size;
}
template<typename T>
LinearList<T>::LinearList(const LinearList & lt)
{
for (int i = 0; i < lt.length; i++)
data[i] = lt.data[i];
length = lt.length;
}
template<typename T>
bool LinearList<T>::ListEmpty() const
{
return length == 0;
}
template<typename T>
bool LinearList<T>::ListFull() const
{
return length == MaxSize;
}
template<typename T>
T LinearList<T>::GetElem(int i)
{
return data[i];
}
template<typename T>
bool LinearList<T>::LocateElem(T e, int &l) const
{
for (int i = 0; i < length; i++)
if (data[i] == e) {
l = i;
return true;
}
return false;
}
template<typename T>
bool LinearList<T>::ListInsert(int i, T e)
{
if (ListFull() || i<0 || i>length)
return false;
else
for (int j = length - 1; j >= i; j--)
data[j] = data[j - 1];
data[i] = e;
length += 1;
return true;
}
template<typename T>
bool LinearList<T>::ListDelete(int i, T &e)
{
if (ListEmpty() || i<0 || i>length - 1)
return false;
else {
e = data[i];
for (int j = i; j < length - 1; j++)
data[j] = data[j + 1];
}
length -= 1;
}
template<typename T>
const int LinearList<T>::ListLength()
{
return length;
}
template<typename T>
void LinearList<T>::show() const
{
for (int i = 0; i < length; i++)
cout << data[i];
cout << endl;
}
template<typename T>
LinearList<T> & LinearList<T>::operator=(const LinearList<T> & lt)
{
for (int i = 0; i < lt.length; i++)
data[i] = lt.data[i];
length = lt.length;
return *this;
}
template<typename T>
LinearList<T> &LinearList<T>::operator+(const LinearList<T> & lt1)
{
int j,k;
for (j = length,k=0; j < length + lt1.length; j++,k++)
data[j] = lt1.data[k];
length = length + lt1.length;
return *this;
}
#endif // !LINEARLIST
线性链表的顺序存储结构使用示例
#include<iostream>
#include"linearlist.h"
#include<random>
#include<ctime>
using namespace std;
void main()
{
srand(time(0));
LinearList<int> data1;
LinearList<char> data2("This is a test about linearlist!");
for (int i = 0; i < 11; i++) {
if (data1.ListInsert(i, rand() % 10))
cout << "Insert sucessed! \n";
else
cout << "Insert falied! \n";
cout << "data1 = ";
data1.show();
}
int lac;
if (data2.LocateElem('s', lac)) {
cout << "Find at " << lac << " !\n";
}
else
cout << "No find!" << endl;
LinearList<char> data3(data2);
cout << "data3 = ";
data3.show();
LinearList<char> data4;
data4 = data3;
cout << "data4 = ";
data4.show();
LinearList<char> data5;
data5 = data3 + data4;
data5.show();
while(!data2.ListEmpty()) {
char temp;
if (data2.ListDelete(rand() % data2.ListLength(), temp)) {
cout << "Delete sucessed! \n";
cout << "Delete " << temp << " ";
}else
cout << "Delete falied! \n";
cout << "data2 = ";
data2.show();
}
}