首页 > 编程语言 > 详细

C++ 静态链表基本算法实现

时间:2018-11-24 20:34:17      阅读:185      评论:0      收藏:0      [点我收藏+]

C++ 静态链表基本算法实现

#ifndef StaticLinkList_h
#define StaticLinkList_h
const int MAXSIZE = 100;
template <class T>
struct StaticNode{
    T data;
    int next;
};
template <class T>
class StaticLinkList{
public:
    StaticLinkList();
    StaticLinkList(T a[], int n);
    void Insert(int i, T a);
    T Delete(int i);
    int Get(int n);
    int Locate(T x);
    int NewNode();
    void DeleteNode(int i);
private:
    int front;
    int tail;
    StaticNode<T> SArray[MAXSIZE];
}
template <class T>
StaticLinkList<T>:: StaticLinkList(){
    for(int i = 0;i<MAXSIZE-1;i++){
        SArray[i].next = i+1;
    }
    SArray[MAXSIZE-1].next = -1;
    front = -1;
    tail = 0;
}
template <class T>
StaticLinkList<T>:: StaticLinkList(T a[], int n){
    if(n>MAXSIZE) throw "溢出";
    for(int i=0;i<MAXSIZE-1;i++){
        SArray[i].next = i+1;
    }
    SArray[MAXSIZE-1].next = -1;
    for(int i = 0;i<n-1;i++){
        SArray[i] = a[i];
    }
    front = 0;
    tail = SArray[n-1].next;
    SArray[n-1].next = -1;
}
template <class T>
int StaticLinkList<T>::NewNode(){
    if(-1 == tail) throw"空间不足";
    int pos = tail;
    tail = SArray[tail].next;
    return pos;
}
template <class T>
T StaticLinkList<T>::Delete(int i){
    if(i<0 || i>MAXSIZE -1){ throw "释放空间错误";}
    if(front == i) front = SArray[i].next;
    SArray[i].next = tail;
    tail = i;
}
#endif /* StaticLinkList_h */

 

C++ 静态链表基本算法实现

原文:https://www.cnblogs.com/ycbeginner/p/10006373.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!