lunes, 26 de agosto de 2013

Pila, a medias, no lo acabe -.-

#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;//redefinicion pila
typedef PILANODO *PILANODOPTR;//redefinicion apuntador

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

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);//direccion de pilaptr(&pilaptr)
                   break;
           case 3: listar(pilaptr);
                      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])// *topeptr apunta a pilaptr
{ PILANODOPTR nuevoptr;//como no se va a modificar no se pone *
  nuevoptr = (PILANODOPTR) malloc(sizeof(PILANODO));
  if (nuevoptr != NULL)
     { nuevoptr->clave = cve;
       strcpy(nuevoptr->descrip, des);
       nuevoptr->sigptr = *topeptr;//en estas 2 lineas se intercambian los valores del apyuntador
       *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 listar (PILANODOPTR auxptr)
{
    if(auxptr==NULL)
         printf("\n\n La pila esta vacia");
    else
    {while(auxptr->sigptr!=NULL)
        {
        printf("\n %d%s",auxptr->clave,auxptr->descrip);
        auxptr=auxptr->sigptr;
        }
       
    }
}


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