Estructura de datos
Inicio
Estructuras
Ejemplo 1 Estructuras
Ejemplo 2 Estructuras
Listas
Tipos de listas
Ejemplo Lista
Funciones
Funciones Ejemplo
Contacto
 

Ejemplo Lista

* EJEMPLO *

//EN ESTE CODIGO SE MUESTRAN LAS OPERACIONES BASICAS DE UNA LISTA SENCILLA A TRAVES DE FUNCIONES//

//Lista sencilla
#include<stdio.h>
#include<conio.h>
#include<iostream.h>
#include<alloc.h>

struct nodo{
int info; //EN ESTAS 2 LINEAS SE RENOMBRAN LOS NODOS
struct nodo *sig;
};

struct nodo *CAB=NULL, *AUX=NULL, *P=NULL;//SE RENOMBRAN LOS NODOS
int valor;
void insertar(void);
void mostrar(void);
void buscar(void);
void elimina(void);

void main(){
clrscr();
char op;
do{
clrscr();
gotoxy(31,4);printf("--MENU PRINCIPAL--");
gotoxy(30,6);printf("1.Insertar ");
gotoxy(30,7);printf("2.Eliminar ");
gotoxy(30,8);printf("3.Buscar elemento");
gotoxy(30,9);printf("4.Mostrar");
gotoxy(30,10);printf("0.Salir");
gotoxy(30,14);printf("Escoja una opcion ");
op=getch();
clrscr();
switch(op){
case '1':insertar(); break;
case '2':elimina(); break;
case '3':buscar(); break;
case '4':mostrar(); break;
}
}while(op!='0');
gotoxy(29,9);printf("!!GRACIAS USUARIO!!");
gotoxy(30,11);printf("-Cod 2722071047-");
getch();
}//fin del main

void insertar(void){
AUX=new(nodo); //ESTA SENTENCIA INSERTA UN NODO
clrscr();
gotoxy(30,5); printf("Numero a insertar ") ;
scanf("%d", &AUX->info);
clrscr();
AUX->sig=NULL;
if(CAB==NULL){
CAB=AUX;
}
else{
P=CAB;
while(P->sig!=NULL){
P=P->sig;
}
P->sig=AUX;
}
}//fin de insertar

 

void elimina(void){
int x, sw=1;
if(CAB==NULL)
{
gotoxy(28,5);printf("¡¡Lista vacia!!...");
getch();
}
else
{
gotoxy(30,5),printf("Eliminar el elemento...");
scanf("%d", &x);
P=CAB;
while(((P->info)!=x)&&(sw==1)){
if((P->sig)!=NULL){
AUX=P;
P=P->sig;
}
else{
sw=0;
}
}
if(sw==0){
gotoxy(28,7);printf("¡¡%d no esta en la lista!!...", x);
getch();
}
else{
if(P==CAB){
CAB=CAB->sig;
}
else{
AUX->sig=P->sig;
}
free(P);
gotoxy(30,7);printf("%d ha sido eliminado", x);
getch();
}
}
} //fin de elimina

 

void buscar(void){
int x;
if(CAB==NULL) {
gotoxy(28,5);printf("¡¡Lista vacia!!...");
getch();
}
else{
AUX=CAB;
gotoxy(38,5);printf("--BUSQUEDA--");
gotoxy(30,7);printf("Digite el elememto a buscar...");
scanf("%d", &x);
while((AUX->info!=x)&&(AUX!=NULL)){
AUX=AUX->sig;
}
if(AUX==NULL){
gotoxy(31,9);printf("¡¡%d no esta en la lista!!...", x);
getch();
}
else{
gotoxy(33,9);printf("%d esta en la lista...", x);
getch();
}
}
} //fin de buscar

 

void mostrar(void){
int i=1;
if(CAB==NULL) {
gotoxy(28,5);printf("¡¡Lista vacia!!...");
getchar();
return;
}
AUX=CAB;
gotoxy(30,5);printf("Contenido de la lista");
while(AUX!=NULL){
gotoxy(30+i+i+i,7);printf("%d->NULL ",AUX->info);
AUX=AUX->sig;
i++;
}
getch();
}//fin de mostrar


Hoy habia 4 visitantes (5 clics a subpáginas) ¡Aqui en esta página!
Este sitio web fue creado de forma gratuita con PaginaWebGratis.es. ¿Quieres también tu sitio web propio?
Registrarse gratis