Você está na página 1de 4

Lista com arranjos em C++ (Estrutura de Dados Básica)

Por: Emerson Shigueo Sugimoto – UTFPR – BSI

Utilizei para o exemplo o Visual Studio C++ 2008.

Figura 1 – Projeto em Visual C++ - Win32 Console Application


Figura 2 – Configurações do Projeto

Arquivo listaarranjo.h:

#ifndef LISTAARRANJO_H
#define LISTAARRANJO_H

class ListaArranjo{
public:
typedef struct nItem{ char *s; };
nItem *item;
ListaArranjo(int tamMax);

char *pesquisa(char *s);


bool vazia();
void insere(char *s);
char *retira(char *s);
char *retiraPrimeiro();
char *getPrimeiro();
private:
int primeiro, ultimo, pos, tamMAX;
};

#endif
Arquivo listaarranjo.cpp (obs: stdafx.h é um arq. de conf. padrão no softw. VC++):

#include "stdafx.h"
#include "listaarranjo.h"

ListaArranjo::ListaArranjo(int tamMax){ // Cria uma Lista vazia


this->tamMAX = tamMax;
this->item = (nItem *)malloc(sizeof(nItem)*tamMax);
this->pos = -1;
this->primeiro = this->ultimo = 0;

int i;
for (i = 0; i < tamMax; i++){
(this->item+i)->s = (char *)malloc(sizeof(char)*3); // * 3 ???
}
}

char * ListaArranjo::pesquisa(char *s){


if( this->vazia() || s == NULL ) return NULL;
int p;
for (p = 0; p < this->ultimo; p++) {
//printf("%i, %s, %i \n", p, (this->item+p)->s, strcmp( (this-
>item+p)->s, s)); //debug
if ( strcmp( (this->item+p)->s, s) == 0 ) { //0 é igual.
return (this->item+p)->s;
}
}

return NULL;
}

void ListaArranjo::insere(char *s) {


if (this->ultimo >= this->tamMAX) {
printf("%s", "Erro: A lista esta cheia");
} else {
(this->item+(this->ultimo))->s = s;
//this->ultimo = this->ultimo+1;
this->ultimo++;
}
}

char * ListaArranjo::retira(char *s){


if( this->vazia() || s == NULL ) return NULL;
int p = 0;
while ( (p < this->ultimo) && (strcmp( (this->item+p)->s, s) != 0) ) { p+
+; }
if (p >= this->ultimo ) return NULL; //Chave não encontrada

//this->ultimo = this->ultimo-1;
this->ultimo--;
int aux;
char *item = (this->item+p)->s;
for (aux = p; aux < this->ultimo; aux++) { //"empurra" para frente
(this->item+aux)->s = (this->item+(aux + 1))->s;
}
return item;
}
char * ListaArranjo::retiraPrimeiro(){
if (this->vazia()) { return NULL; }
char *item = (this->item+0)->s;
//this->ultimo = this->ultimo − 1;
this->ultimo--;
int aux;
for (aux = 0; aux < this->ultimo; aux++) { //"empurra" para frente
(this->item+aux)->s = (this->item+(aux + 1))->s;
}
return item;
}

char * ListaArranjo::getPrimeiro(){
if (this->vazia()) { return NULL; }
return (this->item+this->primeiro)->s;
}

bool ListaArranjo::vazia ( ) {
return (this->primeiro == this->ultimo);
}

Arquivo que contém o main:

#include "stdafx.h"
#include "listaarranjo.h"

//using namespace std; //cout e endl

int _tmain(int argc, _TCHAR* argv[])


{
ListaArranjo listaA(8);
printf("Vazia: %s\n",(int)listaA.vazia() == 1 ? "sim" : "nao");

listaA.insere("Novo item");
listaA.insere("Novo item 2");
listaA.insere("Novo item 3");

printf("getPrimeiro(): %s\n", listaA.getPrimeiro()); //Novo item

printf("Esta na lista: %s\n", listaA.pesquisa("Novo item"));


printf("Remover item: %s\n", listaA.retira("Novo item"));
printf("Esta na lista: %s\n", listaA.pesquisa("Novo item"));
printf("retiraPrimeiro(): %s\n", listaA.retiraPrimeiro()); //Novo item 2
printf("retiraPrimeiro(): %s\n", listaA.retiraPrimeiro()); //Novo item 3
printf("retiraPrimeiro(): %s\n", listaA.retiraPrimeiro()); //null

system("PAUSE"); // pausa o programa


return EXIT_SUCCESS;
}

Você também pode gostar