链表的建立与操作

本文介绍了一个使用类模板实现的简单链表数据结构。该链表支持基本操作如插入、删除、遍历等,并展示了如何创建链表及进行元素的插入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

采用类模板 完成链表

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
template<class ElemType>
struct Node
{
    ElemType data;
    Node<ElemType>*next;

    Node();
    Node(ElemType item,Node<ElemType>*link);
};
template<class ElemType>
Node<ElemType>::Node()
{
    next=NULL;
}

template<class ElemType>
Node<ElemType>::Node(ElemType item,Node<ElemType> *link)
{
    data=item;
    next=link;
}

template<class ElemType>
class SimpleLinkList
{
protected:
    Node<ElemType> *head;
    Node<ElemType> *GetElemPtr(int position) const;
public:
    SimpleLinkList();
    ~SimpleLinkList();
    int Length() const;
    bool Empty() const;
    void Clear();
    void Traverse() const;
    bool GetElem(int position,ElemType &e) const;
    bool SetElem(int position,const ElemType &e);
    bool Delete(int position,ElemType &e);
    bool Insert(int position,const ElemType &e);
};

template<class ElemType>
Node<ElemType>* SimpleLinkList<ElemType>::GetElemPtr(int position) const
{
    Node<ElemType> *tmpPtr=head;
    int pos=0;
    while(tmpPtr!=NULL&&pos<position)
    {
        tmpPtr=tmpPtr->next;
        pos++;
    }

    /*
    if(tmpPtr!=NULL&&pos==position)
    {
        return tmpPtr;
    }
    else
    {
        return NULL;
    }
    */
    return tmpPtr;
}

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

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

template<class ElemType>
int SimpleLinkList<ElemType>::Length() const
{
    int cnt=0;
    for(Node<ElemType> *tmpPtr=head->next;tmpPtr!=NULL;tmpPtr=tmpPtr->next)
    {
        cnt++;
    }
    return cnt;
}

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

template<class ElemType>
void SimpleLinkList<ElemType>::Clear()
{
    ElemType tmpElem;
    while(!Empty())
    {
        Delete(1,tmpElem);
    }
}

template<class ElemType>
void SimpleLinkList<ElemType>::Traverse() const
{
    Node<ElemType> *tmpPtr=head->next;
    while(tmpPtr!=NULL)
    {
        printf("%d ",tmpPtr->data);
        tmpPtr=tmpPtr->next;
    }
    printf("\n");
    return;
}

template<class ElemType>
bool SimpleLinkList<ElemType>::GetElem(int position,ElemType &e) const
{
    if(position<1||position>Length())
    {
        return false;
    }
    else
    {
        Node<ElemType>* tmpPtr;
        tmpPtr=GetElemPtr(position);
        e=tmpPtr->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> *tmpPtr;
        tmpPtr=GetElemPtr(position);
        tmpPtr->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> *tmpPtr;
        tmpPtr=GetElemPtr(position-1);
        Node<ElemType> *newPtr;
        newPtr=new Node<ElemType>(e,tmpPtr->next);
        tmpPtr->next=newPtr;
        return true;
    }
}

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

int main()
{
    SimpleLinkList<int>s;
    int a=5;
    s.Insert(1,a);
    s.Traverse();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值