第八周项目2

  1. #include<bits/stdc++.h>  
  2. #include"lianchuan.h"  
  3. using namespace std;  
  4. int main()  
  5. {  
  6.     int num,b;  
  7.     strnode *s,*s1,*s2,*s3,*s4,*s5,*s6;  
  8.     char a[100];  
  9.     gets(a);  
  10.     jianchuan(s,a);  
  11.     display(s);  
  12.     num=chuanchang(s);  
  13.     cout<<num<<endl;  
  14.     s1=zichuan(s,1,5);  
  15.     display(s1);  
  16.     chuanfuzhi(s2,s1);  
  17.     display(s2);  
  18.     b=chuandeng(s1,s2);  
  19.     cout<<b<<endl;  
  20.     s3=lianjie(s1,s2);  
  21.     display(s3);  
  22.     s4=tihuan(s,1,5,s3);  
  23.     display(s4);  
  24.     s5=shanchu(s3,3,6);  
  25.     display(s5);  
  26.     s6=charu(s5,2,s);  
  27.     display(s6);  
  28.     return 0;  
  29. }  

lianchuan.h

[cpp]  view plain  copy
  1. #ifndef LIANCHUAN_H_INCLUDED  
  2. #define LIANCHUAN_H_INCLUDED  
  3. typedef struct node  
  4. {  
  5.     char data;  
  6.     struct node *next;  
  7. }strnode;  
  8. void jianchuan(strnode *&s,char cstr[]);  
  9. void xiaohui(strnode *&s);  
  10. void chuanfuzhi(strnode *&s,strnode *t);  
  11. int chuandeng(strnode *s,strnode *t);  
  12. int chuanchang(strnode *s);  
  13. strnode * lianjie(strnode *s,strnode *t);  
  14. strnode * zichuan(strnode *s,int i,int j);  
  15. void display(strnode *s);  
  16. strnode * charu(strnode *s,int i,strnode *t);  
  17. strnode * shanchu(strnode *s,int i,int j);  
  18. strnode * tihuan(strnode *s,int i,int j,strnode *t);  
  19. #endif // LIANCHUAN_H_INCLUDED  


lianchuan.cpp


[cpp]  view plain  copy
  1. #include<bits/stdc++.h>  
  2. #include"lianchuan.h"  
  3. using namespace std;  
  4. void jianchuan(strnode *&s,char cstr[])///建串  
  5. {  
  6.     int i;  
  7.     strnode *r,*p;  
  8.     s=(struct node *)malloc(sizeof(strnode));  
  9.     r=s;  
  10.     for(i=0;cstr[i]!='\0';i++)  
  11.     {  
  12.         p=(struct node *)malloc(sizeof(strnode));  
  13.         p->data=cstr[i];  
  14.         r->next=p;  
  15.         r=p;  
  16.     }  
  17.     r->next=NULL;  
  18. }  
  19. void xiaohui(strnode *&s)///销毁串  
  20. {  
  21.     strnode *p,*r;  
  22.     p=s;  
  23.     r=s->next;  
  24.     while(r!=NULL)  
  25.     {  
  26.         free(p);  
  27.         p=r;  
  28.         r=p->next;  
  29.     }  
  30.     free(p);  
  31. }  
  32. void chuanfuzhi(strnode *&s,strnode *t)///串的复制,其实就是新建了一个串  
  33. {  
  34.     strnode *p=t->next,*q,*r;  
  35.     s=(strnode *)malloc(sizeof(struct node));  
  36.     r=s;  
  37.     while(p!=NULL)  
  38.     {  
  39.         q=(strnode *)malloc(sizeof(strnode));  
  40.         q->data=p->data;  
  41.         r->next=q;  
  42.         r=q;  
  43.         p=p->next;  
  44.     }  
  45.     r->next=NULL;  
  46. }  
  47. int chuandeng(strnode *s,strnode *t)///判断两个串是否相等  
  48. {  
  49.     strnode *p=s->next,*q=t->next;  
  50.     while(p!=NULL&&q!=NULL&&p->data==q->data)  
  51.     {  
  52.         p=p->next;  
  53.         q=q->next;  
  54.     }  
  55.     if(p==NULL&&q==NULL)  
  56.         return 1;  
  57.     else  
  58.         return 0;  
  59. }  
  60. int chuanchang(strnode *s)///求串长  
  61. {  
  62.     int k=0;  
  63.     while(s!=NULL)  
  64.     {  
  65.         k++;  
  66.         s=s->next;  
  67.     }  
  68.     return k-1;  
  69. }  
  70. strnode * lianjie(strnode *s,strnode *t)///连接两个子串  
  71. {  
  72.     strnode *p=s->next,*str,*q,*r;  
  73.     str=(strnode *)malloc(sizeof(strnode));  
  74.     r=str;  
  75.     while(p!=NULL)  
  76.     {  
  77.         q=(strnode *)malloc(sizeof(strnode));  
  78.         q->data=p->data;  
  79.         r->next=q;  
  80.         r=q;  
  81.         p=p->next;  
  82.     }  
  83.     p=t->next;  
  84.     while(p!=NULL)  
  85.     {  
  86.         q=(strnode *)malloc(sizeof(strnode));  
  87.         q->data=p->data;  
  88.         r->next=q;  
  89.         r=q;  
  90.         p=p->next;  
  91.     }  
  92.     r->next=NULL;  
  93.     return str;  
  94. }  
  95. strnode * zichuan(strnode *s,int i,int j)///求子串  
  96. {  
  97.     int k;  
  98.     strnode *str,*p=s->next,*q,*r;  
  99.     str=(strnode *)malloc(sizeof(strnode));  
  100.     str->next=NULL;  
  101.     r=str;  
  102.     if(i<=0||j<0||i+j-1>chuanchang(s)||i>chuanchang(s))  
  103.         return str;  
  104.     for(k=1;k<i;k++)  
  105.         p=p->next;  
  106.     for(k=1;k<=j;k++)  
  107.     {  
  108.         q=(strnode *)malloc(sizeof(strnode));  
  109.         q->data=p->data;  
  110.         r->next=q;  
  111.         r=q;  
  112.         p=p->next;  
  113.     }  
  114.     r->next=NULL;  
  115.     return str;  
  116. }  
  117. void display(strnode *s)///输出  
  118. {  
  119.     strnode *p=s->next;  
  120.     while(p!=NULL)  
  121.     {  
  122.         cout<<p->data;  
  123.         p=p->next;  
  124.     }  
  125.     cout<<endl;  
  126. }  
  127. strnode * charu(strnode *s,int i,strnode *t)///串插入  
  128. {  
  129.     int k;  
  130.     strnode *str,*p=s->next,*p1=t->next,*q,*r;  
  131.     str=(strnode *)malloc(sizeof(strnode));  
  132.     str->next=NULL;  
  133.     r=str;  
  134.     if(i<=0||i>chuanchang(s)+1)  
  135.         return str;  
  136.     for(k=1;k<i;k++)  
  137.     {  
  138.         q=(strnode *)malloc(sizeof(strnode));  
  139.         q->data=p->data;  
  140.         r->next=q;  
  141.         r=q;  
  142.         p=p->next;  
  143.     }  
  144.     while(p1!=NULL)  
  145.     {  
  146.         q=(strnode *)malloc(sizeof(strnode));  
  147.         q->data=p1->data;  
  148.         r->next=q;  
  149.         r=q;  
  150.         p1=p1->next;  
  151.     }  
  152.     while(p!=NULL)  
  153.     {  
  154.         q=(strnode *)malloc(sizeof(strnode));  
  155.         q->data=p->data;  
  156.         r->next=q;  
  157.         r=q;  
  158.         p=p->next;  
  159.     }  
  160.     r->next=NULL;  
  161.     return str;  
  162. }  
  163. strnode * shanchu(strnode *s,int i,int j)///串删除  
  164. {  
  165.     int k;  
  166.     strnode *str,*p=s->next,*q,*r;  
  167.     str=(strnode *)malloc(sizeof(strnode));  
  168.     str->next=NULL;  
  169.     r=str;  
  170.     if(i<=0||j<0||i+j-1>chuanchang(s)||i>chuanchang(s))  
  171.         return str;  
  172.     for(k=1;k<i;k++)  
  173.     {  
  174.         q=(strnode *)malloc(sizeof(strnode));  
  175.         q->data=p->data;  
  176.         r->next=q;  
  177.         r=q;  
  178.         p=p->next;  
  179.     }  
  180.     for(k=0;k<j;k++)  
  181.         p=p->next;  
  182.     while(p!=NULL)  
  183.     {  
  184.         q=(strnode *)malloc(sizeof(strnode));  
  185.         q->data=p->data;  
  186.         r->next=q;  
  187.         r=q;  
  188.         p=p->next;  
  189.     }  
  190.     r->next=NULL;  
  191.     return str;  
  192. }  
  193. strnode * tihuan(strnode *s,int i,int j,strnode *t)///串替换  
  194. {  
  195.     int k;  
  196.     strnode *str,*p=s->next,*p1=t->next,*q,*r;  
  197.     str=(strnode *)malloc(sizeof(strnode));  
  198.     str->next=NULL;  
  199.     r=str;  
  200.     if(i<=0||j<0||i+j-1>chuanchang(s)||i>chuanchang(s))  
  201.         return str;  
  202.     for(k=0;k<i-1;k++)  
  203.     {  
  204.         q=(strnode *)malloc(sizeof(strnode));  
  205.         q->data=p->data;///q->next=NULL;  
  206.         r->next=q;  
  207.         r=q;  
  208.         p=p->next;  
  209.     }  
  210.     for(k=0;k<j;k++)  
  211.         p=p->next;  
  212.     while(p1!=NULL)  
  213.     {  
  214.         q=(strnode *)malloc(sizeof(strnode));  
  215.         q->data=p1->data;  
  216.         r->next=q;  
  217.         r=q;  
  218.         p1=p1->next;  
  219.     }  
  220.     while(p!=NULL)  
  221.     {  
  222.         q=(strnode *)malloc(sizeof(strnode));  
  223.         q->data=p->data;  
  224.         r->next=q;  
  225.         r=q;  
  226.         p=p->next;  
  227.     }  
  228.     r->next=NULL;  
  229.     return str;  
  230. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值