c - 用给定的标识符替换标识符列表顺序中的第 K 个值
问题描述
给出了标识符列表。每个标识符的长度不超过 8 个字符。列表中的标识符按字典顺序指示。为以下操作编写一个函数:将标识符列表中的第 k 个值替换为给定的标识符。我无法让它工作,在我输入它应该替换的元素后,它只是跳过 pech_sp 并转到它下面的行。这是代码:
struct el_sp
{ char id [MAXDL];
struct el_sp *sled;
};
int replace(struct el_sp *p, int k, char *str)
{
struct el_sp *pt;
int i;
pt = p;
for (i = 0; i < k; i++) {
if (pt->sled != NULL)
pt = pt->sled;
else {
printf("...");
break;
}
}
strncpy_s(pt->id, str, MAXDL);
return (0);
}
void vkl(struct el_sp **p, char t_id[] )
{ struct el_sp * pt,
*k,*j = NULL;
pt = (struct el_sp *)malloc(sizeof(struct el_sp));
strcpy_s(pt->id,t_id);
if (*p==NULL || strcmp(pt->id,(*p)->id)<0)
{
pt->sled=*p; *p=pt;
}
else
{k=*p;
while (k!=NULL && strcmp(pt->id,k->id)>=0)
{ j=k; k=k->sled;
}
j->sled=pt; pt->sled=k;
}
}
void pech_sp(struct el_sp *p )
{ struct el_sp *i;
printf("\Result:\n");
for( i=p; i!=NULL; i=i->sled )
puts (i->id);
}
int main(){
//setlocale(LC_ALL, "RUS");
struct el_sp *p;
unsigned n;
unsigned i;
unsigned k;
char *r;
char t_id[MAXDL];
printf("\nEnter the amount of identifiers\n n=");
scanf_s("%u",&n);
getchar();
p=NULL;
printf("Enter the identifier ");
printf("(after each identifier press <Enter> )\n");
for ( i=1; i<=n; i++ )
{ gets_s(t_id);
vkl (&p,t_id);
}
pech_sp(p);
printf("Which identifier should be replaced?:\n");
scanf_s("%d", &k);
printf("\nWhat should it be replaced with?:\n");
scanf_s("%d", &r);
replace(p, k, r);
pech_sp(p);
printf("\n\nTo finish, press any key\n");
getchar();
return 0;
}
解决方案
推荐阅读
- mysql - 复杂的mysqldump
- amazon-web-services - 以编程方式收集清单和服务器日志
- ember.js - Emberjs,在控制器内部调用 this.transitionToRoute('index');
- asp.net-core - 文件上传一直显示“未选择文件”
- oracle-fusion-middleware - 如何修改企业绩效管理系统Workspace Fusion版界面
- ios - 如何根据条件在 SwiftUI 中隐藏视图?
- git - 在 gerrit 中使用 -f push 后很少有提交丢失
- laravel - 多台服务器上的 Laravel 队列
- docker - 导出包含使用 compose 创建的多个图像的容器
- python - 如何强制 SQLAlchemy ORM 在子查询中包含表?