Advertisement
Guest User

C37-1

a guest
Oct 21st, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4. struct nodo{
  5.     float peso;
  6.     int posicion;
  7.     nodo *siguiente;
  8. };
  9. void insertar(nodo *&,nodo *&,float,int);
  10. void mostrar(nodo *,nodo *);
  11. void vaciar(nodo *&,int);
  12. int main() {
  13.     nodo *lista=NULL,*primero=NULL;
  14.     char opc;
  15.     int contador=0;
  16.     float peso_aux;
  17.     do{
  18.         cout<<"**** MENU ****"<<endl;
  19.         cout<<"1. Cargar un nodo"<<endl;
  20.         cout<<"2. Mostrar lista"<<endl;
  21.         cout<<"S. Salir"<<endl;
  22.         cout<<"Ingrese opcion:";
  23.         cin>>opc;
  24.         switch(opc){
  25.         case '1':
  26.             cout<<"Ingrese el peso a cargar: ";
  27.             cin>>peso_aux;
  28.             insertar(lista,primero,peso_aux,contador);
  29.             cout<<"Cargado con exito"<<endl<<endl;
  30.             contador++;
  31.             break;
  32.         case '2':
  33.             cout<<"Mostrando lista: ";
  34.             mostrar(lista,primero);
  35.             cout<<endl<<endl;
  36.             break;
  37.         case 's':
  38.         case 'S':
  39.             cout<<"Saliendo del programa...";
  40.             vaciar(lista,contador);
  41.             break;
  42.         default:
  43.             cout<<"Error: Ingrese una opcion valida"<<endl;
  44.             break;
  45.         }
  46.     } while(opc!='s' && opc!='S');
  47.     return 0;
  48. }
  49. void insertar(nodo *&a,nodo *&b,float c,int d){ // lista - primero - peso - posicion
  50.     nodo *nuevo=(nodo *)malloc(sizeof(nodo)),*aux=a;
  51.     nuevo->peso=c;
  52.     nuevo->posicion=d;
  53.     if(a==NULL){
  54.         nuevo->siguiente=nuevo;
  55.         b=nuevo;
  56.         a=nuevo;
  57.     }
  58.     else{
  59.         while(aux->siguiente!=b){
  60.             aux=aux->siguiente;
  61.         }
  62.         nuevo->siguiente=b;
  63.         aux->siguiente=nuevo;
  64.     }
  65. }
  66. void mostrar(nodo *a,nodo *b){ // lista - primero
  67.     nodo *aux=a;
  68.     if(aux!=NULL){
  69.         do{
  70.             cout<<aux->peso<<":"<<aux->posicion<<" - ";
  71.             aux=aux->siguiente;
  72.         }while(aux!=b);
  73.     }
  74. }
  75. void vaciar(nodo *&a,int b){ // lista - contador
  76.     nodo *aux=a,*anterior=NULL;
  77.     while(b>0){
  78.         anterior=aux;
  79.         aux=aux->siguiente;
  80.         free(anterior);
  81.         b--;
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement