单链表

基础知识:

(1)链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的
(2)链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成
(3)每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域
(4)但是链表失去了数组随机读取的优点,同时链表由于增加了结点的指针域,空间开销比较大
(5)链表有很多种不同的类型:单向链表,双向链表以及循环链表,其中有分带头节点和不带头节点的链表,头结点不存放数据信息,头指针总是指向头结点;

带头节点的单链表基本实现:
 #ifndef GUARD_NODE_H
#define GUARD_NODE_H
template<class T>
struct Node{
    T data;
    Node<T> *next;
    Node();
    Node(T e,Node<T> *p=NULL);
};
template<class T>
Node<T>::Node(){
    next=NULL;
}

template<class T>
Node<T>::Node(T e,Node<T> *p){
    next=p;
    data=e;
}
#endif


#ifndef GUARD_SLIST_H
#define GUARD_SLIST_H
#include<iostream>
#include"Node.h"
using namespace std;

template<class T>
class S_List{
private:
    int length;
    Node<T>* head;
public:
    S_List();
    S_List(T v[],int n);
    virtual ~S_List();
    int GetLength()const;
    bool IsEmpty()const;
    void Clear();
    void Traverse(void(*Vist)(const T&))const;//元素遍历
    int LocateElem(const T&)const;//元素定位
    T GetElem(int i)const;//求指定位置的元素
    bool SetElem(int i,const T& e);//修改指定位置的元素
    bool DeleteElem(int i,T& e);//删除指定位置的元素
    bool Inset(int i,const T&e);
    bool InsertElem(const T& e);//在表尾插入元素
    S_List(const S_List<T>& x);//复制构造函数
    S_List<T>& operator=(const S_List<T>& e);//重载赋值运算
};

//链表的长度不包括头节点,第一个元素是在头结点之后
template<class T>
S_List<T>::S_List(){
    length=0;
    head=new Node<T>;
}

template<class T>
S_List<T>::S_List(T v[],int n){
    Node<T>* p;
    p=head=new Node<T>;
    for(int i=0;i<n;i++){
        p->next=new Node<T>(v[i],NULL);
        p=p->next;
    }
    length=n;
}

//释放所有节点,包括头结点
template<class T>
S_List<T>::~S_List(){
    Clear();
    delete head;
}

template<class T>
int S_List<T>::GetLength()const{
    return length;
}

template<class T>
bool S_List<T>::IsEmpty()const{
    return length!=0?true:false
}

//依次删除单链表的每一个节点,只保留头结点,并把当前的长度变为0,注意头指针始终指向头结点
template<class T>
void S_List<T>::Clear(){
    Node<T>* p=head->next;
    while(p!=NULL){
        head->next=p->next;
        delete p;
        p=head->next;
    }
    length=0;
}

//链表的第一个元素不包括头节点,找不到返回0;
template<class T>
int S_List<T>::LocateElem(const T& e)const{
    Node<T>* p=head->next;
    int count=1;
    while(p!=NULL&&p->data!=e){
        p=p->next;
        count++;
    }
    if(p!=NULL)
        return count;
    else
        return 0;
}

//取指定位置的值
template<class T>
T S_List<T>::GetElem(int i)const{
    if(i<1||i>length){
        cout<<"RANG_OVER"<<endl;
        return -1;}
    else{
        Node<T>* p=head->next;
        int count=1;
        for(count;count!=i;count++)
            p=p->next;
        return p->data;
    }
}

template<class T>
bool S_List<T>::SetElem(int i,const T& e){
    if(i<1||i>length)
        cout<<"RANGE_OVER"<<endl;
    else{
        Node<T>* p=head->next;
        int count=1;
        for(count;count!=1;cout++)
            p=p->next;
        p->data=e;
        return true;
    }
}

//删除指定位置的元素,删除后将原长度减1;
template<class T>
bool S_List<T>::DeleteElem(int i,T& e){
    if(i<1||i>length)
        cout<<"RANGE_OVER"<<endl;
    else{
        Node<T>*p=head,*q;
        int count=1;
        for(count;count!=i;count++)
            p=p->next;
        q=p->next;
        p->next=q->next;
        e=q->data;
        delete q;
        length--
        return true;
    }
}

//在第I个位置前插入元素,注意头结点
template<class T>
bool S_List<T>::Inset(int i,const T&e){
    if(i<1||i>length)
        cout<<"RANG_OVER"<<endl;
    else{
        Node<T>*p=head,*q;
        int count;
        for(count=1;count!=i;count++)
            p=p->next;
        q=new Node<T>(e,p->next);
        p->next=q;
        length++;
        return true;
    }
}
//在表尾插入元素,即插入的元素是最后一个
template<class T>
bool S_List<T>::InsertElem(const T& e){
    Node<T> *p,*q;
    q=new Node<T>(e,NULL);
    for(p=head;p->next!=NULL;p=p->next);
    p->next=q;
    length++;
    return true;
}
template<class T>
S_List<T>::S_List(const S_List<T>& x){
    length=x.length;
    head=x.head;
    Node<T> *p;
    p=head=new Node<T>;
    for(int i=0;i<length;i++){
        p->next=new Node<T>(x.GetElem(i+1),NULL);
        p=p->next;
    }
}
#endif


#include"S_List.h"
#include<iostream>
using namespace std;
int main(){
    int a[20];
    for(int j=0;j!=20;j++)
        a[j]=j;
    S_List<int> List(a,30);
    S_List<int> List1(List);
    cout<<List.GetElem(10)<<endl;
    cout<<List.GetLength ()<<endl;
    cout<<List1.GetElem(10)<<endl;
    cout<<List1.GetLength ();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值