首先为了更好地实现静态链表以及相应的操作,我们要知道静态链表的数据域和各个位置处游标的含义:
1).下标0的元素的游标是指向第一个备用链表的下标;
2).下一位置如果元素数据为空则这个位置的游标记为;0
3).最后一个元素的游标记为第一个插入元素的下标;
1、首先是头文件部分:
#ifndef StaticLinkList_H
#define StaticLinkList_H
#include <iostream>
#define MAXSIZE 100
using namespace std;
template <class ElemType>
class StaticLinkList
{
public:
typedef struct
{
ElemType data;
int cur;
}Node;
public:
StaticLinkList();
~StaticLinkList();
int getSize(); //获得链表的元素数
int malloc(); //创建一个新的node
void free(int k); //回收下标为k的node
bool isEmpty() const; //判断链表是否为空
void insertElem(int i,const ElemType &e); //在下标为i的位置处插入元素e
void deleteElem(int i); //删除下标为i的位置处的结点元素
void showVal() const; //打印整个链表
Node node[MAXSIZE]; //存放链表的数组
int m_size; //链表的元素
};
#endif
2、其次是核心实现部分(函数定义)
#include "StaticLinkList.h"
/**********************************************
*成员函数的定义
**********************************************/
//构造函数
template <class ElemType>
StaticLinkList<ElemType>::StaticLinkList() {
for (int i = 0; i < MAXSIZE - 1; ++i) {
node[i].cur = i + 1;
}
node[MAXSIZE - 1].cur = 0;
m_size = 0;
}
//析构函数
template <class ElemType>
StaticLinkList<ElemType>::~StaticLinkList() {
}
//获取链表的长度
template<class ElemType>
int StaticLinkList<ElemType>::getSize()
{
int p = node[0].cur;
while (!node[p].cur) {
p = node[p].cur;
++m_size;
cout << "获得大小程序运行至此" << endl;
}
return m_size;
}
//找到一个可以插入的位置
template<class ElemType>
int StaticLinkList<ElemType>::malloc()
{
int k = node[0].cur;
if (node[0].cur) {
node[0].cur = node[k].cur;
}
return k;
}
//判断链表是否为空表
template <class ElemType>
bool StaticLinkList<ElemType>::isEmpty() const {
if (m_size) {
return false;
}
else
return true;
}
//在指定的位置插入元素e
template<class ElemType>
void StaticLinkList<ElemType>::insertElem(int i, const ElemType &e){
if (i < 1 || i > MAXSIZE + 1) {
return;
}
int k = malloc();
int j = MAXSIZE - 1;
if (k){
node[k].data = e;
for (int index = 1; index <= i - 1; ++index){
j = node[j].cur;
}
node[k].cur = node[j].cur;
node[j].cur = k;
}
++m_size;
}
//删除指定的元素
template<class ElemType>
void StaticLinkList<ElemType>::deleteElem(int k){
if (k < 1 || k > MAXSIZE + 1)
return;
int i;
int j = MAXSIZE - 1;
for (int index = 1; index <= k - 1; ++index){
j = node[j].cur;
}
i = node[j].cur;
node[j].cur = node[i].cur; //此时i是第一个元素的下标
free(i);
--m_size;
}
//回收要删除的链表结点
template <class ElemType>
void StaticLinkList<ElemType>::free(int k) {
node[k].cur = node[0].cur;
node[0].cur = k; //备用链表要变成下标k
}
//打印链表
template<class ElemType>
void StaticLinkList<ElemType>::showVal () const{
if (!m_size) {
cout << "链表为空表" << endl;
return;
}else {
int oneIndex = node[MAXSIZE - 1].cur;
for (int index = 1; index <= m_size; ++index) {
cout << node[oneIndex].data << " ";
oneIndex = node[oneIndex].cur; //这样的删除操作才是正确的
}
cout << endl;
}
}
3、最后是实现部分(main函数)
#include "StaticLinkList.h"
#include "StaticLinkList.cpp"
int main()
{
StaticLinkList<int> sll;
cout << "空表的大小:" << sll.m_size << endl;
cout << "链表是否为空表 :"
<< boolalpha << sll.isEmpty() << endl;
for (int i = 1; i <= 10; ++i) {
sll.insertElem(i,i);
}
cout << sll.getSize();
cout << "插入元素后的大小" << sll.m_size << endl;
cout << "链表是否为空表 :"
<< boolalpha <<sll.isEmpty() << endl;
sll.showVal();
unsigned i;
cout << "请输入要删除的元素位置 : " ;
cin >> i;
sll.deleteElem(i);
sll.showVal();
system("pause");
return 0;
}