Mi_List链表实现:
#include "stdafx.h"
#include <iostream>
//约定:Mi_List下标开始是0。Mi_Stack的升级版。
using namespace std;
template<class Ltype>
class Mi_List {
public:
Mi_List() {
size = 0;
eFrist = 0;
eEnd = 0;
}
//删除指定位置下表
void DeleteWithIndex(int);
//插入数据到指定位置
void InsertWithIndex(int, Ltype);
//删除链表尾
void DeleteEnd();
//删除链表首
void DeleteStart();
//插入数据到链表尾
void InsertEnd(Ltype);
//插入数据到链表首
void InsertStart(Ltype);
//输出链表数据
void PrintList();
//输出链表地址
void PrintAddress();
//清空链表
void Clean();
//获取指定位置数据
Ltype GetElementWithIndex(int);
//覆盖指定位置数据
void ResiveDataWithIndex(int, Ltype);
//获取链表首数据
inline Ltype GetFristElement() {
return GetElementWithIndex(0);
}
//获取链表尾数据
inline Ltype GetEndElement() {
return GetElementWithIndex(size - 1);
}
//获取链表长度
inline int GetSize() {
return size;
}
private:
int size;
class Element {
public:
Ltype data;
Element* pFront;
Element* pRear;
};
Element* eFrist;
Element* eEnd;
};
//在指定位置插入数据
template<class Ltype>
void Mi_List<Ltype>::InsertWithIndex(int _index, Ltype _data) {
if (_index>size || 0>_index) {
cout << "警告:队列插入失败,预插位置错误。" << endl;
return;
}
if (0 == _index) {
InsertStart(_data);
return;
}
else if (_index == size - 1) {
InsertEnd(_data);
return;
}
else {
Element* n = new Element();
Element* e = eFrist;
for (int i = 0;i<_index;i++) {
e = e->pRear;
}
n->data = _data;
n->pFront = e->pFront;
n->pRear