martes, 26 de noviembre de 2013



PROGRAMA 4 


Ejemplo de PILA 
DANIEL PRADO GARCIA


#include <stdio.h>     DANIEL PRADO GARCIA
#include <conio.h>
#include <stdlib.h>

struct pilanodo 
      {
       int clave; // Nombre clave
       char descrip[20]; //arreglo de 20 caracteres 
       struct pilanodo *sigptr; // apuntando al siguiente nodo 
      };

typedef struct pilanodo PILANODO; 
typedef PILANODO *PILANODOPTR;

void push(PILANODOPTR *, int, char[]); //prototipo, al final regresa el valor. 
void instrucciones(void); // Funcion nula.
void listar (PILANODOPTR);
main()
     { 
     PILANODOPTR pilaptr = NULL; //apuntando al tope de la pila . 
     int opcion, cve; .
     char desc[20]; 
     instrucciones();
     printf("\n          Opcion a Elegir : ");
     scanf("%d", &opcion);

    while (opcion != 4)
       { 
           switch(opcion)
                  { case 1: printf("\n         Escribe la clave del articulo (entero): ");
                   scanf("%d", &cve);
                   fflush(stdin);  
                   printf("\n             Escribir descripcon del articulo: ");
                   gets(desc);           
                   push(&pilaptr, cve, desc); 
                   break;
                   case 3: listar (pilaptr);
                   
           default : printf("\n                Opcion invalida. \n\n");
                     printf("\n                Enter para continuar...");
                     getch();
                     instrucciones();
         }
         instrucciones();
         printf("\n                Elige opcion: ");
         scanf("%d", &opcion);
      }
  printf("\n                Fin del programa.\n\n");
  printf("                Enter para salir...");
  getch();
}
//Funcion para imprimir los nodos 
void listar(PILANODOPTR auxptr)
{
if (auxptr==NULL)
    printf("\n\n LA PILA ESTA VACIA");
    else 
    {   printf("\n\n El contenido de la pila es:");
        printf("\n\n ----Tope de la pila -----");
        while (auxptr!=NULL)
        { 
         printf("\n %d %s",auxptr->clave, auxptr->descrip);
         auxptr=auxptr->sigptr;
    
        }
        printf("\n Fondo de la pila");
    }
    getch();
}


void push(PILANODOPTR *topeptr, int cve, char des[20])
{ PILANODOPTR nuevoptr;
  nuevoptr = (PILANODOPTR) malloc(sizeof(PILANODO));
  if (nuevoptr != NULL) 
     { nuevoptr->clave = cve;
       strcpy(nuevoptr->descrip, des);  // Copia 
       nuevoptr->sigptr = *topeptr;
       *topeptr = nuevoptr;
     }
     else
     { printf("\n                %d %s, no fue insertado."
              "\n                No hay memoria disponible. \n", cve, des);
       printf("\n                Enter para continuar...");
       getch();
     }
}

void instrucciones(void) //Funcion nula

         system("cls"); //limpia la pantalla
           printf(" \n\n\n\n"
     "                OPERACIONES DISPONIBLES CON LA PILA: \n\n\n\n"
     "                1 (push) INSERTAR UN VALOR EN LA PILA \n"
     "                2 (pop)  EXTRAER UN VALOR DE LA PILA \n"
     "                3 IMPRIMIR LA PILA \n"
     "                4 SALIR DEL PROGRAMA \n");
}

No hay comentarios:

Publicar un comentario