一个堆串的基本操作问题
这个基本操作的算法本身没有问题,但是在main函数中输入字符串的时候编译器提示无法运行,源代码如下#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *str;
int length;
}Hstring;
void insert(Hstring *s,Hstring *n,int pos)
{
int i;
char *temp;
temp=(char *)malloc(s->length+n->length);
for(i=0;i<pos;i++)
temp=s->str;
for(i=0;i<n->length;i++)
temp=n->str;
for(i=pos;i<s->length;i++)
temp=s->str;
s->length=s->length+n->length;
s->str=temp;
}
void del(Hstring *s,int pos,int len)
{
int i;
char *temp;
temp=(char *)malloc(s->length-len);
for(i=0;i<pos;i++)
temp=s->str;
for(i=pos;i<s->length-len;i++)
temp=s->str;
s->length=s->length-len;
s->str=temp;
}
int main()
{
int pos,len;
Hstring *s,*n;
s=n=(Hstring *)malloc(sizeof(Hstring));
printf("请输入字符串:");
scanf("%s",s->str);
printf("请输入长度:");
scanf("%d",&s->length);
printf("请输入字符串:");
scanf("%s",n->str);
printf("请输入长度:");
scanf("%d",&n->length);
printf("请输入插入位置:");
scanf("%d",&pos);
insert(s,n,pos);
printf("%s \n",s->str);
printf("请输入要删除的位置:");
scanf("%d",&pos);
printf("请输入想删除的长度:");
scanf("%d",&len);
del(s,pos,len);
printf("%s \n",s->str);
} #include <stdio.h>
#include <stdlib.h>
typedef struct
{
char *str;
int length;
}Hstring;
void insert(Hstring *s, Hstring *n, int pos)
{
int i;
char *temp;
temp = (char *)malloc(s->length + n->length);
for (i = 0; i<pos; i++)
temp = s->str;
for (i = 0; i<n->length; i++)
temp = n->str;
for (i = pos; i<s->length; i++)
temp = s->str;
s->length = s->length + n->length;
s->str = temp;
}
void del(Hstring *s, int pos, int len)
{
int i;
char *temp;
temp = (char *)malloc(s->length - len);
for (i = 0; i<pos; i++)
temp = s->str;
for (i = pos; i<s->length - len; i++)
temp = s->str;
s->length = s->length - len;
s->str = temp;
}
int main()
{
int pos, len;
Hstring *s, *n;
s = n = (Hstring *)malloc(sizeof(Hstring));
printf("请输入字符串:");
scanf("%s", &s->str);
printf("请输入长度:");
scanf("%d", &s->length);
printf("请输入字符串:");
scanf("%s", &n->str);
printf("请输入长度:");
scanf("%d", &n->length);
printf("请输入插入位置:");
scanf("%d", &pos);
insert(&s, &n, pos);
printf("%s \n", &s->str);
printf("请输入要删除的位置:");
scanf("%d", &pos);
printf("请输入想删除的长度:");
scanf("%d", &len);
del(&s, &pos, len);
printf("%s \n", &s->str);
}
这样就行,不过好像什么都不会打印。
页:
[1]