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

来源:互联网 发布:淘宝用户服务协议 编辑:程序博客网 时间:2024/06/06 08:35

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_INCLUDEDtypedef 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;}



原创粉丝点击