首页 > 解决方案 > 用给定的标识符替换标识符列表顺序中的第 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;
  }

标签: clistidentifier

解决方案


推荐阅读