《单链表》

#include<bits/stdc++.h>
using namespace std;
template <class ElemType>
struct Node
{
    ElemType data;
    Node<ElemType> *next;
    
    Node(const ElemType &e);   
}; 

//构造一个数据成分为e和指针成分为空的节点

template <class ElemType>
Node<ElemType>::Node(const ElemType &e)
{
    data =e;
    next = NULL; 
}

template <class ElemType>
class SimpleLinkList
{
public:
    Node<ElemType> *head;
    Node<ElemType> *GetElemPtr(int position) const; //返回指向第position个节点的指针 
    
    SimpleLinkList();
    virtual ~SimpleLinkList();
    int Length() const;
    bool Empty() const;
    void Clear();
    void Traverse(void (*visit)(ElemType)) ;
    bool GetElem(int position,ElemType &e) const; //求指定位置元素 
    bool SetElem(int position,const ElemType &e); //设置指定位置元素值 
    bool Delete(int position,ElemType &e);
    bool Delete(int position);
    bool Insert(int position,const ElemType &e);
    void display();
    
};

//返回指向第position个节点位置的指针

template<class ElemType>
Node<ElemType>* SimpleLinkList<ElemType>::GetElemPtr(int position) const
{
    Node<ElemType> *temPtr = head;
    int temPos = 0; //设置头结点值为0 
    while(temPtr!=NULL&&temPos<position)
    {
        temPtr = temPtr->next;
        temPos++;
    }
    if(temPtr!=NULL&&temPos==position)
    {
        return temPtr;
    }
    else
    {
        return NULL;
    }
}

//构造函数

template <class ElemType>
SimpleLinkList<ElemType>::SimpleLinkList()
{
    head = new Node<ElemType>(0);
}

//析构函数

template <class ElemType>
SimpleLinkList<ElemType>::~SimpleLinkList()
{
    Clear();
    delete head;
}

//返回链表长度

template <class ElemType>
int SimpleLinkList<ElemType>::Length() const //注意:链表长度不包括头结点 
{
    int count =0;
    for(Node<ElemType>*temPtr = head->next;temPtr!=NULL;temPtr =temPtr->next)
    {
        count++;
    }
    return count;
}

//判断是否为空

template <class ElemType>
bool SimpleLinkList<ElemType>::Empty() const
{
    return head->next==NULL;
}

//清空链表

template <class ElemType>
void SimpleLinkList<ElemType>::Clear()
{
    Node<ElemType>* temPtr = head;
    while(temPtr!=NULL){
        int h;
        Delete(1,h);//从第一个位置开始清除
    }
}

//遍历链表

template <class ElemType>
void SimpleLinkList<ElemType>::Traverse(void (*visit)(ElemType))
{
    for(Node<ElemType> * temPtr = head->next;temPtr!=NULL;temPtr =temPtr->next)
    {
        (*visit)(temPtr->data);
    }
}

//求指定位置元素

template <class ElemType>
bool SimpleLinkList<ElemType>::GetElem(int position,ElemType &e)const
{
    if(position<1||position>Length())
    {
        return false;
    }
    else
    {
        Node<ElemType>*temPtr = GetElemPtr(position);
        e = temPtr->data;
        return true;
    }
}

//设置指定位置元素值

template <class ElemType>
bool SimpleLinkList<ElemType>::SetElem(int position,const ElemType &e)
{
    if(position <1 || position >Length())
    {
        return false;
    }
    else
    {
        Node<ElemType>* temPtr=GetElemPtr(position);
        temPtr->data=e;
        return true;
    }
}

//插入元素

template <class ElemType>
bool SimpleLinkList<ElemType>::Insert(int position,const ElemType &e)
{
    if(position<1||position>Length()+1)
    {
        return false;
    }
    else
    {
        Node<ElemType>* temPtr = GetElemPtr(position -1);
        Node<ElemType>* newPtr = new Node<ElemType>(e);
        newPtr->next = temPtr->next;
        temPtr->next = newPtr;
        return true;
    }
}

//删除第position个位置的元素

template <class ElemType>
bool SimpleLinkList<ElemType>::Delete(int position,ElemType &e)
{
    if(position<1||position>Length())
    {
        return false;
    }
    else
    {
        Node<ElemType>* temPtr = GetElemPtr(position - 1);
        Node<ElemType>* nextPtr = temPtr->next;
        e = nextPtr->data;
        temPtr->next = nextPtr->next;
        
        delete nextPtr;
        nextPtr = NULL;
        return true;
    }
}

//展示链表

template <class ElemType>
void SimpleLinkList<ElemType>::display() 
{
    Node<ElemType>* temPtr = head;
    while (temPtr != NULL) 
    {
        std::cout << temPtr->data << " ";
        temPtr = temPtr->next;
    }
    std::cout << std::endl;
}

//主函数:

int main() {
    SimpleLinkList<int> linklist;
    for (int i = 1; i <= 10; ++i) 
    {
        linklist.Insert(i,i);
    }
    linklist.display();
    cout << endl;
    int h;
    cout<<"请输入要删除的元素位置:"; 
    cin>>h;
    linklist.Delete(h,h);
    linklist.display();
    cout << endl;

    int pot;
    linklist.GetElem(6, pot);
    cout << "位置6在链表中的下标索引为:" << pot << endl;

    linklist.SetElem(6, 1);
    linklist.display();
    cout << endl;
    int pos;
    linklist.GetElem(6, pos);
    cout << "位置6在链表中的下标索引为:" << pos << endl;

    int target;
    linklist.GetElem(5,target);
    cout << "5号位置的元素为:" << target << endl;

    cout << "链表的长度为:" << linklist.Length() << endl;
    
    int t;
    cout<<"请输入要插入的元素位置:"; 
    cin>>t;
    linklist.Insert(t,t);
    linklist.display();
    
    linklist.GetElem(5,target);
    cout << "5号位置的元素为:" << target << endl;
    
    cout << "链表的长度为:" << linklist.Length() << endl;
    
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值