// TAD Vetor
// definição de tipos de dados (typedef e struct)
typedef int TipoChave;
struct T_Vetor{
int tamanho;
int capacidade;
TipoChave a[100];
};
typedef struct T_Vetor TipoVetor;
// definição de operações (cabeçalho)
// criar o vetor
void criarVetor(TipoVetor *v, int capacidade);
// insere um elemento no vetor
int inserirEmVetor(TipoVetor *v, TipoChave a);
// obter tamanho
int obterTamanho(TipoVetor v);
int obterPosicaoDe(TipoVetor v, TipoChave chave);
int obterElementoEm(TipoVetor v, int pos);
void imprimirVetor(TipoVetor v);
int estahCheio(TipoVetor v);
void retirarElementoEm(TipoVetor *v, int pos);
// implementação das operações (funções)
void criarVetor(TipoVetor *v, int capacidade){
if (capacidade < 1) {
printf("ERRO: Capacidade deve ser maior que zero");
return;
}
v->capacidade = capacidade;
v->tamanho = 0;
}
int inserirEmVetor(TipoVetor *v, TipoChave a){
if (estahCheio(*v)){
printf("ERRO: Vetor estah cheio!!")
return -1;
}
int pos = v->tamanho;
v->a[pos] = a;
v->tamanho++;
return pos;
}
int obterTamanho(TipoVetor v){
return v.tamanho;
}
int obterPosicaoDe(TipoVetor v, TipoChave chave){
int c;
for (c = 0; c < v.tamanho; c++){
if (v.a[c] == chave){
return c;
}
}
return -1;
}
int obterElementoEm(TipoVetor v, int pos){
if (pos >= v.tamanho){
printf("ERRO: posição vazia ou inexistente");
return -1;
}
return v.a[pos];
}
void imprimirVetor(TipoVetor v){
int c;
printf("Tamanho: %d\n", v.tamanho);
printf("Capacidade: %d\n", v.capacidade);
for (c = 0; c < v.tamanho; c++){
printf("%d ", v.a[c]);
}
printf("\n");
}
int estahCheio(TipoVetor v){
// se for negativo, nao estah cheio
// se for zero, estah cheio
return v.tamanho - v.capacidade;
}
void retirarElementoEm(TipoVetor *v, int pos){
}
int main(){
TipoVetor v;
TipoChave c;
//void criarVetor(TipoVetor *v, int capacidade){
criarVetor(&v, 10);
imprimirVetor(v);
//int inserirEmVetor(TipoVetor *v, TipoChave a){
c = 12;
inserirEmVetor(&v, c);
c = 10;
inserirEmVetor(&v, c);
//int obterPosicaoDe(TipoVetor v, TipoChave chave){
c = 12;
int pos = obterPosicaoDe(v, c);
//int obterElementoEm(TipoVetor v, int pos){
c = obterElementoEm(v, 0);
imprimirVetor(v);
// capacidade: 10
// tamanho : 2
// Valores : [ 12, 10]
}
Nenhum comentário:
Postar um comentário