【线性表】静态链表

来源:互联网 发布:mac上的iphoto在哪儿 编辑:程序博客网 时间:2024/05/22 09:45

StaticLinkedList.h

#ifndef STATICLINKEDLIST_H#define STATICLINKEDLIST_H#include<iostream>const int max_size = 1000;//list nodetemplate<typename T>struct list_node{T data;int cur;//cursor};//StaticLinkedListtemplate<typename T>class StaticLinkedList{public:typedef T value_type;typedef T* pointer;StaticLinkedList();//constructor~StaticLinkedList();//destructorint get_length();//get lengthbool is_empty(); // whether StaticLinkedList is empty or notvoid traverse();//traverse StaticLinkedListvoid clear();//clear StaticLinkedListbool get_elem(int i, pointer value);//get the ith databool insert(int i, value_type value);//insert data in the ith placebool remove(int i, pointer value);//remove the ith dataprivate:int get_list_node();//get the index of a free list nodevoid free_list_node(int i);//free list nodeprivate:list_node<T> space[max_size];};//constructortemplate<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;}//destructortemplate<typename T>StaticLinkedList<T>::~StaticLinkedList(){}//get lengthtemplate<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 nottemplate<typename T>bool StaticLinkedList<T>::is_empty(){return space[max_size - 1].cur == 0 ? true : false;}//traverse StaticLinkedListtemplate<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 StaticLinkedListtemplate<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 datatemplate<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 placetemplate<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 datatemplate<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 nodetemplate<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 nodetemplate<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; //0cout << boolalpha << int_list.is_empty() << endl;//truefor (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 5elsecout << "get element fail" << endl;if (int_list.insert(7, 20))cout << "insert succeed" << endl;//insert succeedelsecout << "insert fail" << endl;if (int_list.remove(8, &value))cout << "remove succeed,remove value is " << value << endl;//remove succeed,remove value is 7elsecout << "remove fail" << endl;int_list.traverse();//1 2 3 4 5 6 20 8 9 cout << int_list.get_length() << endl;//9int_list.clear();cout << int_list.get_length() << endl;//0return 0;}

0 0