首先爲了更好地實現靜態鏈表以及相應的操作,我們要知道靜態鏈表的數據域和各個位置處遊標的含義:
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;
}