List (static) operations c++

Asked

Viewed 1,518 times

1

Good afternoon guys, I’m having a hard time understanding the operations in Statist lists, I know create and display, but how do I remove and search within the list, the sorting algorithms I’ve already understood some, can help me please? I’m a little desperate.

#include <iostream>
#include <cstring> //lib pra poder usar o strcpy
#define maximo 2
using namespace std;

struct pessoa {
  char nome [20];
  int idade;
  float altura;
  float peso;
};

void imprime (pessoa alguem[]){
  for (int i = 0; i < maximo; i++) {
    cout <<"Nome: "<< alguem[i].nome << " - ";
    cout <<"Idade: "<<alguem[i].idade << " - ";
    cout <<"Altura: " << alguem[i].altura << " - ";
    cout <<"Peso: "<< alguem[i].peso<< "\n";

  }
}
int main() {

pessoa alguem [maximo];

for (int i = 0; i < maximo; i++) {
  cout << "Insira o nome: " << '\n';
  cin >> alguem[i].nome;
  cout << "Insira a idade: " << '\n';
  cin >> alguem[i].idade;
  cout << "Insira a altura: " << '\n';
  cin >> alguem[i].altura;
  cout << "Insira o peso: " << '\n';
  cin >> alguem[i].peso;
}
  imprime (alguem);
}
  • Is there any reason to use C in a code that’s supposed to be C++? The search is almost the same as the print, only it makes a comparison to see if the item is what you’re looking for. The removal is much more complicated, starting by searching for what you want to remove, unless you know its position. Then you will have to pull each item to the previous one since the item that was removed, and.

  • You could improve your question. What do you want to know exactly? Your code loops to a fixed size to add within an array of instances of the person struct, and then uses an equivalent loop to print what has been inserted. Now you want to search for a specific element of the array, and also remove a specific element?

  • @rafaelB. My problem is that I need to somehow delete an instance of the struct that’s already been put on the list, only I’m not able to develop the logic for such a.

  • Use Std::vector or Std::deck with both you can delete an element as long as you know its position, the position of all next elements will be automatically readjusted.

1 answer

0


The most practical suggestion to implement is the one @Nex' and @Maniero talked about: use the ready-made C structures++ (vector, list, etc). They already come with their own search, insertion and removal methods already optimized.

If you really need to use array, you can try the following approach:

Using a pointer vector

I find it easier to find the elements already removed/end of the list.

pessoa* alguem[maximo];

for (int i = 0; i < maximo; i++) {
    alguem[i] = new pessoa;
    //...
}

Choose a field to search

To do a search, you have to designate some field to find the element (index, name, etc).

void busca(pessoa* alguem[], int idade) {
for (int i = 0; i < maximo; i++) {
    if (alguem[i] != NULL && alguem[i]->idade == idade) {
        //Achou e pára a busca
    }
}
//Não achou
}

Choose a field to search and remove

Here you can follow the same search logic to find the element to be removed. Then you can either simply point the vector index to null (simplest), resize the vector or create a new vector with size n-1, where n is the current size of the vector. Could something like:

    //Removendo o elemento fazendo com que o índice aponte para nulo
void remove(pessoa* alguem[], int idade) {
    for (int i = 0; i < maximo; i++) {
        if (alguem[i] != NULL && alguem[i]->idade == idade) {
            alguem[i] = NULL;
            return;
        }
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.