首页 > 解决方案 > 如何从结构中删除元素 - C

问题描述

我希望你能帮我完成我们从老师那里得到的家庭作业。我的问题如下:你能帮我纠正我的ddelete方法吗?哦,请不要将其标记为重复,因为不要认为我的问题有解决方案。(我这么说是因为昨晚我为此做了很多研究)

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
typedef struct{
    long long unsigned num;
    char name[20];
}Telbook;
using namespace std;
int be(Telbook*);
void ki(Telbook[]);
void search (Telbook[]);
void ddelete(Telbook[]);
int count(Telbook[]);

int main(){


setlocale(LC_ALL,"");
printf("\t\t\t  Struktura feladat 1.  \n\n\n");
Telbook tomb[50];
int db;
db=be(tomb);            
ki(tomb);
search(tomb);
ddelete(tomb);
ki(tomb);
system("pause");
}
int be(Telbook *n){
    int i=0;
    printf("Enter phone # and names until the phone # you entered is 0\n");

    /*printf("Kérek egy nevet: ");
    scanf("%s",n->name);*/
    printf("Enter a Phone #: ");
    scanf("%llu",&n->num);

    while(n->num){
        printf("Enter a name: ");
        scanf("%s",n->name);
        i++;
        n++;
        printf("Enter a phone #: ");
        scanf("%llu",&n->num);

    }
    return i;
}
void ki(Telbook n[]){
    int i=0;
    while(n[i].num){

    printf("Name: %s, Phone #: %llu\n",n[i].name,n[i].num);
    i++;
    //n++;
    }

}
void search(Telbook n[]){
    int i=0;
    int dbb;

    char nev[20];
    dbb=count(n);
    printf("Enter the name you're  searching for: ");
    scanf("%s",nev);
    for(i=0;i<dbb;i++){
        if(strcmp(n[i].name,nev)==0)break;

        //n++;
    }
    if(i==dbb){
    printf("The name doesn't exist.\n");
    }
    else{
        printf("The name you have searhed for is: %s it's on the %d. index.\n",nev,i+1);
    }
}
int count(Telbook n[]) {
    int i = 0;
    while (n[i].num) {

        i++;
    }
    return i;
}

void ddelete(Telbook n[]){


    int szam,db=count(n),i=0,adat;

        printf("Enter a number you want to delete: ");scanf("%d",&szam);    
        for(i = 0; i < db; i++){
            if( szam == n[i].num){
                for (i = 0; i <  db - 1; i++)
            {
            n[i] = n[i + 1];


           } 
       }        
    }       

}

这是我的代码。我写得尽可能容易理解。**我的问题是它不会从结构中删除元素。**

标签: c++struct

解决方案


当您使用一个数组时,Telbook您无法删除。这可能是您通过阅读发现的。这里提到的删除类型是删除元素的空间,以便删除。这在使用数组时是不可能的。在 C++ 中,数组是固定大小的,因为你的硬盘是固定大小的(你不能删除你买的空间)。

现在,删除可能是指用户的观点,意味着如果应用程序将数据呈现给用户,就好像可以删除某些数据一样,这样的删除是编码信息的问题(您可以删除文件,但这不是物理空间删除,只是逻辑删除,数据永远不会再次呈现给您,仅此而已)。

然后在您的情况下,您要么更改所用内存的管理方式(例如,通过使用动态分配 - 新建/删除 - 在正在运行的程序中分配和删除空间),要么使用某种编码来表示一些事实固定大小数组中的条目与某些内容不对应,将被视为“免费”。

在您的情况下,该功能ki可以指导您:

void ki(Telbook n[]){
    int i=0;
    while(n[i].num){    
      printf("Name: %s, Phone #: %llu\n",n[i].name,n[i].num);
      i++;
    } 
}

正如它所写的那样,数组中的有效条目是那些从开始索引到字段num为 0 的条目。所以存储背后的逻辑是:任何等于 0 的TelBook元素都是“空闲的”或者是存储在大于等于零num的元素的索引处。num

然后是这样的:

void ddelete(Telbook n[]){
    int szam,db=count(n),i=0,adat;
    printf("Enter a number you want to delete: ");
    scanf("%d",&szam);    
    for(i = 0; i < db; i++) {
       if (szam == n[i].num){
         for (j = i; j <  db - 2; j++) { // copy elements at the end, one pos to the left 
            n[j] = n[j + 1];
         }
         n[db-1].num = 0; // ensure the last element is effectively a free one
         return; // stop here, no need to continue 
       }       
    }       
}

但这也假设所有TelBook条目都已正确初始化,但情况并非如此,更改为(至少):

Telbook tomb[50];
tomb[0].num = 0;

可能还存在其他一些问题,但是您现在必须自己做一点工作。


推荐阅读