条件:()
算法思想:首先找到你需要的子串的第一个位置i,找到了之后再用while循环确定子串的长度(len)。最后将最后一个结点的指针区间置为NULL;
程序:
#include <stdio.h>
#include <stdlib.h>
typedef char datatype;
typedef struct node{
datatype data;
struct node *next;
}seqstring;
// 字符串的链式存储的创建
seqstring * creat()
{
seqstring *s,*p=NULL,*head=NULL;
datatype ch;
while((ch=getchar())!='\n')
{
s=(seqstring *)malloc(sizeof(seqstring));
s->data=ch;
if(head==NULL)
head=p=s;
else
{
p->next=s;
p=s;
}
p->next=NULL;
}
return head;
}
// 字符串的遍历
void display(seqstring *head)
{
seqstring *p=head;
while(p)
{
printf("%c",p->data);
p=p->next;
}
printf("\n");
}
//
seqstring * subsititute(seqstring *head,int i,int len)
{
seqstring *p=head,*r,*q,*s;
int k=1;
while(p && k!=i)
{
p=p->next;
k++;
}
if(!p)
{
printf("ERROR \n");
return 0;
}
else
{
k=1;
r=(seqstring *)malloc(sizeof(seqstring));
r->data=p->data;
r->next=NULL;
q=r;
while(p->next && k<len)
{
p=p->next;
s=(seqstring *)malloc(sizeof(seqstring));
s->data=p->data;
k++;
q->next=s;
q=s;
}
if(k<len)
{
printf("ERROR 2\n");
return 0;
}
else
{
q->next=NULL;
return r;
}
}
}
int main()
{
int i,len;
seqstring *head,*str2;
head=creat();
display(head);
scanf("%d",&i);
scanf("%d",&len);
str2=subsititute(head,i,len);
display(str2);
return 0;
}