miércoles, 21 de septiembre de 2011

Listas simples






ListaChar.h

#pragma once

class ListaChar
{
private:
int cab;
int lib;
public:
int capacidad;

struct structLista
{
wchar_t dato;
int ptr;
}*lista;

public:
ListaChar(int);
bool agregar(wchar_t);
int buscar(wchar_t,int);
bool eliminar(wchar_t);
bool editar(wchar_t,wchar_t);
int length();
wchar_t *getLista();
};


ListaChar.cpp


#include "StdAfx.h"
#include "ListaChar.h"

ListaChar::ListaChar(int size)
{
lista = new structLista[size];
capacidad=size;

cab=999;
lib=0;
for(int k=0;k<size;k++)
{
lista[k].dato=' ';
lista[k].ptr=k+1;
}
lista[size-1].ptr=999;
}

bool ListaChar::agregar(wchar_t xdato)
{
if(length()!=capacidad)
{
lista[lib].dato=xdato;
if(cab==999)
{
cab=lib;
lib=lista[lib].ptr;
lista[cab].ptr=999;
}
else
{
int xptr=cab;

while(lista[xptr].ptr!=999)
xptr=lista[xptr].ptr;

int xlib=lib;
lista[xptr].ptr=lib;
lib=lista[xlib].ptr;
lista[xlib].ptr=999;
}
return true;
}
else
return false;
}

int ListaChar::buscar(wchar_t xdato,int xtipo)
{
int xptr=cab;
int xant=xptr;
int xExiste=0;

while(xptr!=999 && !xExiste)
{
if(lista[xptr].dato==xdato)
xExiste=1;
else
{
xant=xptr;
xptr=lista[xptr].ptr;
}
}

if(xExiste)
{
if(xtipo==0)
return xant;
else
return xptr;
}
else
return 999;
}

bool ListaChar::eliminar(wchar_t xdato)
{
int xptr=buscar(xdato,1);

if(length()>0)
{
if(xptr!=999)
{
int xant=buscar(xdato,0);
if(xant==xptr)
{
cab=lista[cab].ptr;
lista[xptr].ptr=lib;
lib=xptr;
}
else
{
lista[xant].ptr=lista[xptr].ptr;
lista[xptr].ptr=lib;
lib=xptr;
}

return true;
}
else
return false;
}
else
return false;
}

bool ListaChar::editar(wchar_t xdato,wchar_t ndato)
{
int xptr=buscar(xdato,1);

if(xptr!=999)
{
lista[xptr].dato=ndato;
return true;
}
else
return false;
}

int ListaChar::length()
{
int index=0;

int xptr=cab;

while(xptr!=999)
{
index++;
xptr=lista[xptr].ptr;
}

return index;

}

wchar_t *ListaChar::getLista()
{
wchar_t *listado = new wchar_t[length()];

int index=0;

int xptr=cab;

while(xptr!=999)
{
listado[index]=lista[xptr].dato;
index++;

xptr=lista[xptr].ptr;
}

return listado;
}

Descargar la solución completa de Listas

No hay comentarios:

Publicar un comentario