lunes, 9 de septiembre de 2013

Programa de Lista

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

struct lista {
       int clave;
       char descrip[25];
       struct lista *sigptr; };
typedef struct lista LISTA;
typedef LISTA *LSTPTR;

void enlista(LSTPTR *iniptr, LSTPTR *finptr, int cla, char descr[25])
{ LSTPTR nvoptr, actptr, otroptr;
  nvoptr = (LSTPTR) malloc(sizeof(LISTA));
  if (nvoptr != NULL)
     {nvoptr->clave = cla;
      strcpy(nvoptr->descrip, descr);
      nvoptr->sigptr = NULL;
      if (*iniptr == NULL)
  {*iniptr = nvoptr;
   *finptr = nvoptr;
  }
      else
  {actptr = *iniptr;
   if (cla < (*iniptr)->clave)
     {nvoptr->sigptr = *iniptr;
      *iniptr = nvoptr;
     }
  else
     if (cla > (*finptr)->clave)
        {(*finptr)->sigptr = nvoptr;
  *finptr = nvoptr;
        }
     else
        {otroptr = actptr->sigptr;
  while (cla > otroptr->clave)
        {actptr= actptr->sigptr;
         otroptr= actptr->sigptr;
        }
  actptr->sigptr = nvoptr;
  nvoptr->sigptr = otroptr;
        }
  }
     }
  else
      printf("%d no fue insertado. No hay memoria disponible\n\n", cla);
}

void menu(void)
     { system("cls");
       printf("\n\n\n\n"
       "              OPERACIONES DISPONIBLES EN LA LISTA ORDENADA: \n\n\n\n"
       "              1 AGREGAR UN ELEMENTO A LA LISTA\n"
       "              2 IMPRIMIR LA LISTA\n"
       "              3 EXTRAER UN ELEMENTO DE LA LISTA\n"
       "              4 SALIR DEL PROGRAMA\n");
     }

main()
{ LSTPTR iniptr = NULL, finptr = NULL;
  char opcion, descri[25];
  int clav;
  menu();
  printf("\n                Elige opcion: ");
  scanf("%c", &opcion);
  while (opcion != '4')
        { switch(opcion)
             { case '1': printf("\n                Clave del articulo (entero): ");
                      scanf("%d", &clav);
                      fflush(stdin);
                      printf("\n                  Descripcion del articulo : ");
                      gets(descri);
                      enlista(&iniptr, &finptr, clav, descri)     ;
                      break;
               default : printf("\n\n Opcion no valida.....");
                            printf("\n\n Enter para continuar...");
                            getch(); 
                            break;
             }
       menu();
       fflush(stdin);
       printf("\n                Elige opcion: ");
       scanf("%c", &opcion);
        }
  printf("\n                Fin del programa.\n\n");
  printf("                Enter para terminar...");
  getch();
}

No hay comentarios:

Publicar un comentario