lunes, 26 de agosto de 2013

Pilas

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <windows.h>

struct pilanodo {
       int clave;
       char descrip[20];
       struct pilanodo *sigptr;
      };

typedef struct pilanodo PILANODO;
typedef PILANODO *PILANODOPTR;

void push(PILANODOPTR *, int, char[]);
void instrucciones(void);

main()
{ PILANODOPTR pilaptr = NULL;
  int opcion, cve;
  char desc[20];
  instrucciones();
  printf("\n                Elige opcion: ");
  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             Escribe la descripcon del articulo: ");
             gets(desc);     
             push(&pilaptr, cve, desc);
             break;
        default : printf("\n                Opcion no permitida. \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();
}

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);
       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)
{ system("cls");
  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