c++实现的一个链式串

链式串类LinkString源码:

#pragma once

class StringNode
{
public:
    char element;
    StringNode *next;  
};

class LinkString
{
public:

    LinkString(void)
    {
        top = new StringNode();
    }

    LinkString(char * s2)
    {
        top = new StringNode();
        *this = s2;    //运算符=重载,用char串赋值
    }

    //用char串赋值
    void operator=(char * s2)
    {
        StringNode * temp;
        temp = top;

        for(int i =0;s2[i]!='';i++)
        {
            if(temp->next==NULL)
                temp->next = new StringNode();

            temp->next->element = s2[i];
            temp = temp->next;
        }
    }

    //串的比较
    int Compare(LinkString *list2)
    {
        StringNode *temp1 = top->next;
        StringNode *temp2 = list2->GetTopElement();
        while(temp1!=NULL&&temp2!=NULL)
        {
            if(temp1->element!=temp2->element)
                break;
            else
            {
                temp1 = temp1->next;
                temp2 = temp2->next;
            }
        }

        if(temp1==NULL&&temp2!=NULL)
            return -1;
        else if(temp1!=NULL&&temp2==NULL)
            return 1;
        else if(temp1==NULL&&temp2==NULL)
            return 0;
        else if(temp1->element>temp2->element)
            return 1;
        else if(temp1->element<temp2->element)
            return -1;
    }

    //根据下标和长度截取子串
    LinkString * SubStr(int index,int length)
    {
        if(index+length>GetLength())
            return NULL;

        StringNode *temp = top;
        for(int i=0;i<=index;i++)
            temp = temp->next;

        char * newlist = new char[length+1];

        for(int i=0;i<length;i++)
        {
            newlist[i] = temp->element;
            temp = temp->next;
        }
        newlist[length] = '';

        return new LinkString(newlist);
    }

    //查询子串在主串中的下标
    int IndexOf(LinkString *list2)
    {
        if(GetLength()<list2->GetLength())
            return -1;

        StringNode *temp = top->next;
        char t = list2->GetTopElement()->element;
        int i = 0;

        for(i=0;i<=GetLength()-list2->GetLength();temp=temp->next,i++)
        {
            if(temp->element==t)
                if(list2->Compare(this->SubStr(i,list2->GetLength()))==0)
                    return i;
        }

        return -1;
    }

    //串的拼接,将list2拼接到本串尾部
    void Concat(LinkString *list2)
    {
        StringNode *temp = top;
        while(temp->next!=NULL)
            temp = temp->next;
        temp->next = list2->GetTopElement();
    }

    //插入串
    void Insert(int index,LinkString *list2)
    {
        if(index>GetLength()||index<0)
            return;

        StringNode *temp = top;
        for(int i=0;i<index;i++)
            temp = temp->next;

        StringNode *t = temp->next;   //temp为插入位置前一个节点,temp->next为插入位置的节点

        StringNode * temp2 = list2->GetTopElement();
        while(temp2!=NULL)
        {
            StringNode *t2 = new StringNode();
            t2->element = temp2->element;
            temp->next = t2;
            temp = temp->next;
            temp2 = temp2->next;
        }

        temp->next = t;
    }

    //删除串
    void Delete(int index,int length)
    {
        if(index>GetLength()-1||index<0||index+length>GetLength())
            return;

        StringNode *temp = getNode(index-1); //temp记录下了要删除位置的前一个节点
        StringNode *t = temp->next;

        for(int i =0;i<length;i++)
            t = t->next;

        temp->next = t;
    }

    //获取链串的长度
    int GetLength()
    {
        StringNode *temp = top;
        int i=0 ;
        while(temp->next!=NULL)
        {
            temp = temp->next;
            i++;
        }
        return i;
    }

    //获取链串的第一个元素
    StringNode * GetTopElement()
    {
        return top->next;
    }

    //返回char数组
    char * ToString()
    {
        StringNode *temp = top->next;
        int len = GetLength();
        char * charlist = new char[len+1];

        for(int i =0;i<len;i++)
        {
            charlist[i] = temp->element;
            temp = temp->next;
        }
        charlist[len] = '';
        return charlist;
    }

    //根据下标获取元素
    char get(int index)
    {
        return getNode(index)->element;
    }

    ~LinkString(void)
    {
    }

private:
    StringNode *top;

    StringNode * getNode(int index)
    {
        StringNode *temp = top;
        for(int i=0;i<=index;i++)
            temp = temp->next;
        return temp;
    }
};
View Code

测试代码:

//串的链式存储结构
    LinkString *str = new LinkString("abcdefg");
    LinkString *str2 = new LinkString("cde");

    //std::cout<<str->GetLength()<<std::endl;  //测试获取链串长度

    //str->Concat(str2);  //测试拼接
    //std::cout<<str->ToString()<<std::endl;

    //for(int i=0;i<str->GetLength();i++)
    //{ 
    //    std::cout<<str->get(i) ;   //测试get()遍历
    //}   std::cout<<std::endl;

    //std::cout<<str->Compare(str2)<<std::endl; //测试字符串的比较

    //std::cout<<str->SubStr(3,3)->ToString()<<std::endl; //测试取子串

    //std::cout<<str->IndexOf(str2)<<std::endl;  //测试子串在主串中的下标

    //str->Insert(0,str2);
    //std::cout<<str->ToString()<<std::endl;   //测试插入串

    //str->Delete(2,5);
    //std::cout<<str->ToString()<<std::endl;   //测试插入串
原文地址:https://www.cnblogs.com/xiayangqiushi/p/3342662.html