堆串的插入

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node
{char *a;
int b;
 }node;//堆串
node fuzhi(node s,char c[])
{int i=0,j;
j=strlen(c);
s.a=(char*)malloc(j);
while(i<40&&c[i]!='\0')
{s.a[i]=c[i];
i++;}
s.a[i]='\0';
s.b=i;
return s;}//fuzhi 
node charu(node s,int pos,node p)
{if(pos<=0||pos>s.b)
{printf("插入未知错误");
exit(1);
}
int i;
char *temp;
temp=(char*)malloc(s.b+p.b);
for(i=0;i<pos-1;i++)
temp[i]=s.a[i];
for(i=0;i<p.b;i++)
temp[i+pos-1]=p.a[i];
for(i=pos-1;i<s.b;i++)
temp[i+p.b]=s.a[i];
temp[s.b+p.b]='\0';
free(s.a);
s.a=temp;
s.b+=p.b;
return s;
}//插入
int main()
{char c[40];
int pos,weizhi;
node s,p;
scanf("%s",c);
s=fuzhi(s,c);
p=fuzhi(p,c);
printf("请输入p串插入s的位置\n");
scanf("%d",&pos);
s=charu(s,pos,p);
printf("%s",s.a);
 } 
在这里插入代码片

猜你喜欢

转载自blog.csdn.net/feiqipengcheng/article/details/83214212
今日推荐