串的链式结构的简单操作

    xiaoxiao2021-04-15  65

    #include<bits/stdc++.h> #define max_size 200 using namespace std; char p[max_size]; typedef struct strnode{ char ch; strnode *next; }strnode,*pstr; void strassign(pstr &s,char *p)//初始化赋值 { pstr q; s=(strnode *)malloc(sizeof(strnode));//头结点 q=s; q->next=NULL; for(int i=0;p[i]!='\0';++i){ q->next=(strnode*)malloc(sizeof(strnode)); q=q->next; q->ch=p[i]; q->next=NULL; } } void print(pstr s) { while(s->next) { cout<<s->next->ch; s=s->next; } cout<<endl; } strnode *sub_string(pstr s,int start,int len) { pstr s1,p,q; p=s; s1=(strnode *)malloc(sizeof(strnode)); q=s1; q->next=NULL; while(start--) { p=p->next; } while(len--) { q->next=(strnode*)malloc(sizeof(strnode)); q=q->next; q->ch=p->ch; q->next=NULL; p=p->next; } return s1; } int main() { pstr head,s1; cin>>p; strassign(head,p); print(head); s1=sub_string(head,3,5);//从第三位字符开始取五个字符 print(s1); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-670854.html

    最新回复(0)