Activity Lists in C

Asked

Viewed 66 times

1

I received the following activity in the topic of Lists in physical contiguity:

"Complete the attributes of the contact list structure in the file list_contacts.h. Create a contact list file. c and implement the initialize, insert, print_list and functions destroy, without modifying the interface (the functions defined in list of contacts. h).

After that, modify the insert function so that instead of failing when not has space, proceed as follows: Allocate a vector of items with capacity of 150% of the current; Update the new capacity; Copy the old vector items to the new vector and use the new one from then on; Release the old vector; Insert the new item normally. Feel free to add new functions if you feel the need, however, do not change the files . h"

My doubt is on what I should use to implement these requested functions (as we are learning lists in physical contiguity, I believe it is something like this, but I do not know how), and how I can do it, since, I should not change the interface.

I’ll put in here all the codes that came from the activity:

//lista_contatos.h//
#ifndef LISTA_H
#define LISTA_H

#include "contato.h"

typedef struct lista_contatos {
//CompleteAqui//
} lista_contatos;

/** Inicializa uma lista vazia de contatos  */
void inicializar(lista_contatos *l, int capacidade);

/** Destroi a lista de contatos, liberando o espaço ocupado */
void destruir(lista_contatos *l);

/** Mostra na tela a lista de contatos */
void print_lista(lista_contatos* l);

/**
* Adiciona um elemento na lista de contatos. Retorna 1 se
* a insercao foi bem sucedida e 0 caso contrario
*/
int inserir(lista_contatos *l, contato novo);

#endif // LISTA_H

and

//contato.h//
#ifndef CONTATO_H
#define CONTATO_H

typedef struct contato {
char nome[140];
char telefone[20];
int ano, mes, dia;
} contato;

#endif // CONTATO_H

and finally the main:

#include <stdio.h>
#include <stdlib.h>

#include "contato.h"
#include "lista_contatos.h"

int main()
{
lista_contatos amigos;
inicializar(&amigos, 3);

contato a = {"neil tyson", "+1(699)193265", 1958,10,5};
contato b = {"serena williams", "+1(369)654963", 1981,9,26};
contato c = {"edson nascimento", "+55 (11) 35693696", 1940,10,23};
contato d = {"miguel sapateiro", "+49 (0151) 9876589", 1969,1,3};

inserir(&amigos, a);
inserir(&amigos, b);
inserir(&amigos, c);
inserir(&amigos, d);

print_lista(&amigos);

destruir(&amigos);

return 0;
}
  • Search by array. Apparently each element of the array will be a structure with 5 data (as described in contact).

No answers

Browser other questions tagged

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