StaticLinkedList.h
#ifndef STATICLINKEDLIST_H
#define STATICLINKEDLIST_H
#include<iostream>
const int max_size = 1000;
//list node
template<typename T>
struct list_node{
T data;
int cur;//cursor
};
//StaticLinkedList
template<typename T>
class StaticLinkedList{
public:
typedef T value_type;
typedef T* pointer;
StaticLinkedList();//constructor
~StaticLinkedList();//destructor
int get_length();//get length
bool is_empty(); // whether StaticLinkedList is empty or not
void traverse();//traverse StaticLinkedList
void clear();//clear StaticLinkedList
bool get_elem(int i, pointer value);//get the ith data
bool insert(int i, value_type value);//insert data in the ith place
bool remove(int i, pointer value);//remove the ith data
private:
int get_list_node();//get the index of a free list node
void free_list_node(int i);//free list node
private:
list_node<T> space[max_size];
};
//constructor
template<typename T>
StaticLinkedList<T>::StaticLinkedList(){
for (int i = 0; i < max_size - 1; i++)
space[i].cur = i + 1;
space[max_size - 1].cur = 0;
}
//destructor
template<typename T>
StaticLinkedList<T>::~StaticLinkedList(){}
//get length
template<typename T>
int StaticLinkedList<T>::get_length(){
int count = 0;
int i = space[max_size - 1].cur;
while (i){
i = space[i].cur;
count++;
}
return count;
}
// whether StaticLinkedList is empty or not
template<typename T>
bool StaticLinkedList<T>::is_empty(){
return space[max_size - 1].cur == 0 ? true : false;
}
//traverse StaticLinkedList
template<typename T>
void StaticLinkedList<T>::traverse(){
int i = space[max_size - 1].cur;
while (i){
std::cout << space[i].data << " ";
i = space[i].cur;
}
std::cout << std::endl;
}
//clear StaticLinkedList
template<typename T>
void StaticLinkedList<T>::clear(){
for (int i = 0; i < max_size - 1; i++)
space[i].cur = i + 1;
space[max_size - 1].cur = 0;
}
//get the ith data
template<typename T>
bool StaticLinkedList<T>::get_elem(int i, pointer value){
if (i<1 || i>get_length())
return false;
int k = max_size - 1;
for (int j = 0; j < i; j++){
k = space[k].cur;
}
*value = space[k].data;
return true;
}
//insert data in the ith place
template<typename T>
bool StaticLinkedList<T>::insert(int i, value_type value){
if (i<1 || i>get_length() + 1)
return false;
int k = max_size - 1;
int j = get_list_node();
if (j){
space[j].data = value;
for (int l = 1; l < i; l++)
k = space[k].cur;
space[j].cur = space[k].cur;
space[k].cur = j;
return true;
}
return false;
}
//remove the ith data
template<typename T>
bool StaticLinkedList<T>::remove(int i, pointer value){
if (i<1 || i>get_length())
return false;
int k = max_size - 1;
for (int j = 0; j < i-1; j++){
k = space[k].cur;
}
int j = space[k].cur;
space[k].cur = space[j].cur;
*value = space[j].data;
free_list_node(j);
return true;
}
//get the index of a free list node
template<typename T>
int StaticLinkedList<T>::get_list_node(){
int i = space[0].cur;
if (0<space[0].cur&&space[0].cur<max_size-1){
space[0].cur = space[i].cur;
return i;
}
return 0;//no free list node
}
//free list node
template<typename T>
void StaticLinkedList<T>::free_list_node(int i){
space[i].cur = space[0].cur;
space[0].cur = i;
}
#endif
main.cpp
#include"StaticLinkedList.h"
using namespace std;
int main(){
StaticLinkedList<int> int_list;
cout << int_list.get_length() << endl; //0
cout << boolalpha << int_list.is_empty() << endl;//true
for (int i = 0; i < 10; i++){
int_list.insert(i, i);
}
int_list.traverse();//1 2 3 4 5 6 7 8 9
int value;
if (int_list.get_elem(5, &value))
cout << "get element succeed,value is " << value << endl;//get element succeed,value is 5
else
cout << "get element fail" << endl;
if (int_list.insert(7, 20))
cout << "insert succeed" << endl;//insert succeed
else
cout << "insert fail" << endl;
if (int_list.remove(8, &value))
cout << "remove succeed,remove value is " << value << endl;//remove succeed,remove value is 7
else
cout << "remove fail" << endl;
int_list.traverse();//1 2 3 4 5 6 20 8 9
cout << int_list.get_length() << endl;//9
int_list.clear();
cout << int_list.get_length() << endl;//0
return 0;
}