第八周项目2-链串算法库

main.cpp

#include<bits/stdc++.h>
#include"lianchuan.h"
using namespace std;
int main()
{
    int num,b;
    strnode *s,*s1,*s2,*s3,*s4,*s5,*s6;
    char a[100];
    gets(a);
    jianchuan(s,a);
    display(s);
    num=chuanchang(s);
    cout<<num<<endl;
    s1=zichuan(s,1,5);
    display(s1);
    chuanfuzhi(s2,s1);
    display(s2);
    b=chuandeng(s1,s2);
    cout<<b<<endl;
    s3=lianjie(s1,s2);
    display(s3);
    s4=tihuan(s,1,5,s3);
    display(s4);
    s5=shanchu(s3,3,6);
    display(s5);
    s6=charu(s5,2,s);
    display(s6);
    return 0;
}

lianchuan.h

#ifndef LIANCHUAN_H_INCLUDED
#define LIANCHUAN_H_INCLUDED
typedef struct node
{
    char data;
    struct node *next;
}strnode;
void jianchuan(strnode *&s,char cstr[]);
void xiaohui(strnode *&s);
void chuanfuzhi(strnode *&s,strnode *t);
int chuandeng(strnode *s,strnode *t);
int chuanchang(strnode *s);
strnode * lianjie(strnode *s,strnode *t);
strnode * zichuan(strnode *s,int i,int j);
void display(strnode *s);
strnode * charu(strnode *s,int i,strnode *t);
strnode * shanchu(strnode *s,int i,int j);
strnode * tihuan(strnode *s,int i,int j,strnode *t);
#endif // LIANCHUAN_H_INCLUDED


lianchuan.cpp


#include<bits/stdc++.h>
#include"lianchuan.h"
using namespace std;
void jianchuan(strnode *&s,char cstr[])///建串
{
    int i;
    strnode *r,*p;
    s=(struct node *)malloc(sizeof(strnode));
    r=s;
    for(i=0;cstr[i]!='\0';i++)
    {
        p=(struct node *)malloc(sizeof(strnode));
        p->data=cstr[i];
        r->next=p;
        r=p;
    }
    r->next=NULL;
}
void xiaohui(strnode *&s)///销毁串
{
    strnode *p,*r;
    p=s;
    r=s->next;
    while(r!=NULL)
    {
        free(p);
        p=r;
        r=p->next;
    }
    free(p);
}
void chuanfuzhi(strnode *&s,strnode *t)///串的复制,其实就是新建了一个串
{
    strnode *p=t->next,*q,*r;
    s=(strnode *)malloc(sizeof(struct node));
    r=s;
    while(p!=NULL)
    {
        q=(strnode *)malloc(sizeof(strnode));
        q->data=p->data;
        r->next=q;
        r=q;
        p=p->next;
    }
    r->next=NULL;
}
int chuandeng(strnode *s,strnode *t)///判断两个串是否相等
{
    strnode *p=s->next,*q=t->next;
    while(p!=NULL&&q!=NULL&&p->data==q->data)
    {
        p=p->next;
        q=q->next;
    }
    if(p==NULL&&q==NULL)
        return 1;
    else
        return 0;
}
int chuanchang(strnode *s)///求串长
{
    int k=0;
    while(s!=NULL)
    {
        k++;
        s=s->next;
    }
    return k-1;
}
strnode * lianjie(strnode *s,strnode *t)///连接两个子串
{
    strnode *p=s->next,*str,*q,*r;
    str=(strnode *)malloc(sizeof(strnode));
    r=str;
    while(p!=NULL)
    {
        q=(strnode *)malloc(sizeof(strnode));
        q->data=p->data;
        r->next=q;
        r=q;
        p=p->next;
    }
    p=t->next;
    while(p!=NULL)
    {
        q=(strnode *)malloc(sizeof(strnode));
        q->data=p->data;
        r->next=q;
        r=q;
        p=p->next;
    }
    r->next=NULL;
    return str;
}
strnode * zichuan(strnode *s,int i,int j)///求子串
{
    int k;
    strnode *str,*p=s->next,*q,*r;
    str=(strnode *)malloc(sizeof(strnode));
    str->next=NULL;
    r=str;
    if(i<=0||j<0||i+j-1>chuanchang(s)||i>chuanchang(s))
        return str;
    for(k=1;k<i;k++)
        p=p->next;
    for(k=1;k<=j;k++)
    {
        q=(strnode *)malloc(sizeof(strnode));
        q->data=p->data;
        r->next=q;
        r=q;
        p=p->next;
    }
    r->next=NULL;
    return str;
}
void display(strnode *s)///输出
{
    strnode *p=s->next;
    while(p!=NULL)
    {
        cout<<p->data;
        p=p->next;
    }
    cout<<endl;
}
strnode * charu(strnode *s,int i,strnode *t)///串插入
{
    int k;
    strnode *str,*p=s->next,*p1=t->next,*q,*r;
    str=(strnode *)malloc(sizeof(strnode));
    str->next=NULL;
    r=str;
    if(i<=0||i>chuanchang(s)+1)
        return str;
    for(k=1;k<i;k++)
    {
        q=(strnode *)malloc(sizeof(strnode));
        q->data=p->data;
        r->next=q;
        r=q;
        p=p->next;
    }
    while(p1!=NULL)
    {
        q=(strnode *)malloc(sizeof(strnode));
        q->data=p1->data;
        r->next=q;
        r=q;
        p1=p1->next;
    }
    while(p!=NULL)
    {
        q=(strnode *)malloc(sizeof(strnode));
        q->data=p->data;
        r->next=q;
        r=q;
        p=p->next;
    }
    r->next=NULL;
    return str;
}
strnode * shanchu(strnode *s,int i,int j)///串删除
{
    int k;
    strnode *str,*p=s->next,*q,*r;
    str=(strnode *)malloc(sizeof(strnode));
    str->next=NULL;
    r=str;
    if(i<=0||j<0||i+j-1>chuanchang(s)||i>chuanchang(s))
        return str;
    for(k=1;k<i;k++)
    {
        q=(strnode *)malloc(sizeof(strnode));
        q->data=p->data;
        r->next=q;
        r=q;
        p=p->next;
    }
    for(k=0;k<j;k++)
        p=p->next;
    while(p!=NULL)
    {
        q=(strnode *)malloc(sizeof(strnode));
        q->data=p->data;
        r->next=q;
        r=q;
        p=p->next;
    }
    r->next=NULL;
    return str;
}
strnode * tihuan(strnode *s,int i,int j,strnode *t)///串替换
{
    int k;
    strnode *str,*p=s->next,*p1=t->next,*q,*r;
    str=(strnode *)malloc(sizeof(strnode));
    str->next=NULL;
    r=str;
    if(i<=0||j<0||i+j-1>chuanchang(s)||i>chuanchang(s))
        return str;
    for(k=0;k<i-1;k++)
    {
        q=(strnode *)malloc(sizeof(strnode));
        q->data=p->data;///q->next=NULL;
        r->next=q;
        r=q;
        p=p->next;
    }
    for(k=0;k<j;k++)
        p=p->next;
    while(p1!=NULL)
    {
        q=(strnode *)malloc(sizeof(strnode));
        q->data=p1->data;
        r->next=q;
        r=q;
        p1=p1->next;
    }
    while(p!=NULL)
    {
        q=(strnode *)malloc(sizeof(strnode));
        q->data=p->data;
        r->next=q;
        r=q;
        p=p->next;
    }
    r->next=NULL;
    return str;
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值