Programa lista 7 Daniel Prado Garcia
Una lista enlazada es una de las estructuras de datos fundamentales, y puede ser usada para implementar otras estructuras de datos. Consiste en una secuencia de nodos, en los que se guardan campos de datos arbitrarios y una o dos referencias (punteros) al nodo anterior o posterior. El principal beneficio de las listas enlazadas respecto a los array convencionales es que el orden de los elementos enlazados puede ser diferente al orden de almacenamiento en la memoria o el disco, permitiendo que el orden de recorrido de la lista sea diferente al de almacenamiento.
#include <conio.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
struct lista
{
int clave;
char descrip[25];
struct lista *sigptr;
};
typedef struct lista LISTA;
typedef LISTA *LSTPTR;
//Funcion para imprimir lista
void imprime(LSTPTR auxptr)
{
system("cls");
if (auxptr==NULL)
printf("\n\n La lista esta vacia\n\n");
else
{
printf("\n\n El contenido de la lista es: ");
printf("\n\n\n ---- Principio de la lista ----\n");
while(auxptr!=NULL)//mientras sigptr que esta siendo apuntado por auxptr sea diferente de nulo
{
printf("\n\n %d - %s",auxptr->clave, auxptr->descrip);
auxptr=auxptr->sigptr;
}
printf("\n\n\n ---- Fin de la lista ----\n");
}
system("pause");
}
void extraer(LSTPTR *iniptr,LSTPTR *finptr,int cla)
{
system("cls");
LSTPTR auxptr,ultptr,buscaptr;
auxptr=*iniptr;
ultptr=*finptr;
if(*iniptr==NULL)//si el contenido de "iniptr" es nulo entonces no hay lista
{
printf("\n\n No hay nodos en la lista \n\n");
system("pause");
}
else
{
if(cla<auxptr->clave || cla>ultptr->clave)
{
printf("\n\n No existe ese nodo en la lista \n\n");
system("pause");
}
else
{
if(cla==auxptr->clave)
{
*iniptr=auxptr->sigptr;
free(auxptr);
}
else
{
if(cla==ultptr->clave) //si "cla" es igual al miembro "clave" que esta siendo apuntada por "ultptr"
{
while(auxptr->sigptr!=ultptr)
{
auxptr=auxptr->sigptr;
}
auxptr->sigptr=NULL;
free(ultptr);
*finptr=auxptr;
}
else
{
while(cla!=auxptr->clave)
{
buscaptr=auxptr;
auxptr=auxptr->sigptr;
}
buscaptr->sigptr=auxptr->sigptr;
free(auxptr);
}
}
}
}
}
}
void enlista(LSTPTR *iniptr, LSTPTR *finptr, int cla, char descr[25])
{
LSTPTR nvoptr, actptr, otroptr; //crear 3 apuntadores
nvoptr = (LSTPTR)malloc(sizeof(LISTA));
if (nvoptr != NULL)
{
nvoptr->clave = cla;
strcpy(nvoptr->descrip, descr);
nvoptr->sigptr = NULL;
if (*iniptr == NULL)
{
*iniptr = nvoptr;
*finptr = nvoptr;
}
else
{
actptr = *iniptr;
if (cla < (*iniptr)->clave)
{
nvoptr->sigptr = *iniptr;
*iniptr = nvoptr;
}
else
{
if (cla > (*finptr)->clave)
{
(*finptr)->sigptr = nvoptr;
*finptr = nvoptr;
}
else
{
otroptr = actptr->sigptr;
while (cla > otroptr->clave)
{
actptr= actptr->sigptr;
otroptr= actptr->sigptr;
}
actptr->sigptr = nvoptr;
nvoptr->sigptr = otroptr;
}
}
}
}
else
printf("%d no fue insertado. No hay memoria n\n", cla);
}
void menu(void)
{
system("cls");
printf("\n\n\n\n"
" OPERACIONES DISPONIBLES EN LA LISTA ORDENADA: \n\n\n\n"
" 1.- AGREGAR UN ELEMENTO A LA LISTA\n"
"2.- IMPRIMIR LA LISTA\n"
" 3.- EXTRAER UN ELEMENTO DE LA LISTA\n"
" 4 SALIR DEL PROGRAMA\n");
}
main()
{
LSTPTR iniptr = NULL, finptr = NULL;
char opcion, descri[25];
int clav;
menu();
printf("\n Elige opcion: ");
scanf("%c", &opcion);
while (opcion != '4')
{
switch(opcion)
{
case '1':
system("cls");
printf("\n Clave del articulo (entero): ");
scanf("%d", &clav);
fflush(stdin);
printf("\n Descripcion del articulo : ");
gets(descri);
enlista(&iniptr, &finptr, clav, descri);
imprime(iniptr);
break;
case '2':
imprime(iniptr);
break;
case '3':
printf("\n Clave del articulo a extraer (entero): ");
scanf("%d", &clav);
extraer(&iniptr,&finptr,clav);
imprime(iniptr);
break;
default :
printf("\n\n Opcion no valida.....");
printf("\n\n Enter para continuar...");
getch();
break;
}
menu();
fflush(stdin);
printf("\n Elige opcion: ");
scanf("%c", &opcion);
}
printf("\n Fin del programa.\n\n");
printf(" Enter para terminar...");
system("pause");
}
No hay comentarios:
Publicar un comentario