Skip to content

Instantly share code, notes, and snippets.

@jofese
Last active August 29, 2015 14:09
Show Gist options
  • Save jofese/b920cc8e46501fa8edc1 to your computer and use it in GitHub Desktop.
Save jofese/b920cc8e46501fa8edc1 to your computer and use it in GitHub Desktop.
Ejercicio con Listas Enlazadas Simples en c++, Registrar contactos de telefono
/*
Autor: Joel Cesar Fernandez Segura
Curso: Estructura de Datos
Ejercicio: CONTACTOS DE TELEFONO
IDE: CodeBlocks
Pagina Web: http://codebotic.blogspot.com
*/
#include<iostream>
#include<cstdlib>
#define maxchar 50
using namespace std;
struct nodo{
char nombre[maxchar]; // nombre de contacto
int numero; //numero de celular
struct nodo *sgte; // puntero al siguiente nodo
};
typedef struct nodo *TAgenda;//definimos puntero de tipo nodo
/*----------------------------- MENU ------------------------------*/
void menu(void){
cout<<"\n\t\t[ CONTACTOS DE CELULAR ]\n";
cout<<"\t\t----------------------------------\n\n";
cout<<" 1. REGISTRAR CONTACTO "<<endl;
cout<<" 2. LISTAR CONTACTOS "<<endl;
cout<<" 3. ELIMINAR CONTACTO "<<endl;
cout<<" 3. SALIR "<<endl;
cout<<"\n Ingrese opcion : ";
}
/*----------------------- FUNCION REGISTRAR UN CONTACTO ----------------------*/
void registrar_contacto(TAgenda &lista){
TAgenda t,q = new(struct nodo);
cout<<"\n\n\t\t[ REGISTRO ]\n";
cout<<"\t\t------------";
cout<<"\n\tDATOS DE LA CONTACTO \n";
cin.ignore();cout<<"\n\tNOMBRE:"; cin.getline(q->nombre,maxchar);
cout<<"\n\tCELULAR:"; cin>>q->numero;
cout<<endl;
system("cls");
q->sgte = NULL;
if(lista==NULL){
lista = q;
} else {
t = lista;
while(t->sgte!=NULL){
t = t->sgte;
}
t->sgte = q;
}
}
/*--------------- FUNCION PARA MOSTRAR TODOS LOS CONTACTOS --------------------*/
void listar_contactos(TAgenda q){
system("cls");
int i=1;
cout<<"\n\n\t[ LISTA DE CONTACTOS ]";
cout<<"\n\n\t-------------------------"<<endl;
while(q!=NULL){
cout<<"\n\n\tCONTACTO ["<<i<<"] ";
cout<<"\n\t------------------------";
cout<<"\n\tNOMBRE : "<<q->nombre<<endl;
cout<<"\n\tCELULAR : "<<q->numero<<endl;
q=q->sgte;
i++;
}
}
/*----------------- FUNCION ELIMINAR CONTACTO SELECIONADO POR CODIGO ---------------------*/
void eliminar_contacto(TAgenda &lista){
int cod;
TAgenda q,t;
q=lista;
if(lista==NULL){
cout<<"\n\n\tLISTA VACIA.....!!!!";
}else{
cout<<"\n\n\n\tELIMINAR UN CONTACTO";
cout<<"\n\n\tINGRESE CELULAR:"; cin>>cod;
while(q!=NULL){
if(q->numero==cod){
if(q==lista)
lista=lista->sgte;
else
t->sgte=q->sgte;
delete(q);
cout<<"\n\n\CONTACTO ELIMINADO...!!!!!\n";
return;
}else {
t=q;
q=q->sgte;
}
}
if(q==NULL)
cout<<"\n\tCODIGO INCORRECTO...!!\n";
}
}
/*------------------------FUNCION PRINCIPAL-------------------------*/
int main(void){
system("color 0a");
TAgenda lista=NULL;
int op;
do{
menu();
cin>>op;
switch(op){
case 1: registrar_contacto(lista);
break;
case 2: listar_contactos(lista);
break;
case 3: eliminar_contacto(lista);
break;
case 4: return 0;
default: cout<<"\nINGRESE UNA OPCION VALIDA...\n"; break;
}
cout<<endl;
system("pause"); system("cls");
}while(op!=6);
system("pause");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment