串 链式实现

#ifndef STR_H_INCLUDED
#define STR_H_INCLUDED


struct node
{
    char data;
    struct node*next;
};
typedef struct node* str;


str Initial(str t)              //初始化
{
    return NULL;
}


str Createstr(str t)            //创建串并返回
{
    printf("input the string\n");


    char ch;
    str tmp=NULL;
    str p;
    while((ch=getchar())!='\n')
    {
        tmp=(str)malloc(sizeof(struct node));
        tmp->data = ch;
        tmp->next = NULL;
        if(t==NULL) {t=tmp;p=t;continue;}
        p->next=tmp;
        p=p->next;
    }
    return t;
}


void Print(str t)           //输出串
{
    if(t==NULL) {printf("Empty\n");exit(0);}
    str p;
    for(p=t;p;p=p->next) printf("%c",p->data);
    printf("\n");
}


int length(str t)
{
    int len=0;
    str tmp;
    for(tmp=t;tmp;tmp=tmp->next) len++;


    return len;
}


str clear(str t)
{
    str tmp;
    while(t!=NULL)
    {
        tmp=t;
        t=t->next;
        free(tmp);
    }
    return t;
}




str Concat(str T,str A,str B)
{
    if(T) T=clear(T);
    int i,len;
    str tmp,p,q;
    len = length(A);
    p=A;
    for(i=0;i<len;i++)
    {
        tmp=(str)malloc(sizeof(struct node));//建立新结点
        tmp->data=p->data;
        tmp->next=NULL;


        p=p->next;                           //取值点后移


        if(!T){T=tmp;q=T;continue;}           //第一次赋值
        q->next = tmp;                        //作为尾结点插入
        q=q->next;                            //q总是记下最后一个位置
    }


    p=B;
    len = length(B);
    for(i=0;i<len;i++)
    {
        tmp=(str)malloc(sizeof(struct node));
        tmp->data=p->data;
        tmp->next=NULL;
        p=p->next;


        q->next = tmp;  //作为尾结点插入
        q=q->next;      //q总是记下最后一个位置
    }
    return T;
}


str substr(str dst,str s,int pos,int len)
{
    if(pos+len>length(s)+1){printf("illegal\n");exit(0);}
    int i;
    str p,tmp;
    for(i=1;i<pos;i++) s=s->next;
    for(i=0;i<len;i++)
    {
        tmp=(str)malloc(sizeof(struct node));
        tmp->data = s->data;
        tmp->next = NULL;
        s=s->next;
        if(!dst)
        {
            dst=tmp;
            p=dst;
            continue;
        }
        p->next=tmp;
        p=p->next;
    }
    return dst;


}


#endif // STR_H_INCLUDED
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值