本文共 4228 字,大约阅读时间需要 14 分钟。
又有一段时间没有更新博客了。在家久了变的异常敏感,什么都会去想,想多了又会自闭。不管咋样,生活还在继续,无法逃避。该面对的还是要直面,就希望安稳毕业,顺利入职。
开始数据结构与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并且要在函数名处也要定义参数;
有关线性链表的顺序存储结构知识点,就不再赘述。我是对照《大话数据结构》学习的,目前为止感觉讲的很不错,浅入深出。此外,实现方法肯定有更加简单或者需要优化的地方,欢迎批评指正。
线性链表的顺序存储结构头文件
#pragma once#ifndef LINEARLIST_H_#define LINEARLIST_H_#includeusing namespace std;#define MaxSize 100template 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 LinearList ::LinearList(){ length = 0;}template LinearList ::LinearList(T *s){ int size = strlen(s); for (int i = 0; i < size; i++) data[i] = s[i]; length = size;}template LinearList ::LinearList(const LinearList & lt){ for (int i = 0; i < lt.length; i++) data[i] = lt.data[i]; length = lt.length;}template bool LinearList ::ListEmpty() const{ return length == 0;}template bool LinearList ::ListFull() const{ return length == MaxSize;}template T LinearList ::GetElem(int i){ return data[i];}template bool LinearList ::LocateElem(T e, int &l) const{ for (int i = 0; i < length; i++) if (data[i] == e) { l = i; return true; } return false;}template bool LinearList ::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 bool LinearList ::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 const int LinearList ::ListLength(){ return length;}template void LinearList ::show() const{ for (int i = 0; i < length; i++) cout << data[i]; cout << endl;}template LinearList & LinearList ::operator=(const LinearList & lt){ for (int i = 0; i < lt.length; i++) data[i] = lt.data[i]; length = lt.length; return *this;}template LinearList &LinearList ::operator+(const LinearList & 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#include"linearlist.h"#include #include using namespace std;void main(){ srand(time(0)); LinearList data1; LinearList 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 data3(data2); cout << "data3 = "; data3.show(); LinearList data4; data4 = data3; cout << "data4 = "; data4.show(); LinearList 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(); }}
转载地址:http://osyci.baihongyu.com/