miércoles, 28 de agosto de 2013

Filas o Colas

FiFo

first in first out

si se compara una fila, se tiene un aountador que esta apuntando al tope de esa fila, y entre sus nodos se crean enlases, y el primero que entro a la pila, es nulo, y de esa manera se van haciendo los enlances, en la cola, vamos a suponer que se tiene estos nodos que se van a encolar,
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>

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

typedef struct colanodo COLANODO;
typedef COLANODO *COLANODOPTR;

void enqueue(COLANODOPTR *, COLANODOPTR *, int, char[]);
void menu(void);

main()
{ COLANODOPTR adelaptr = NULL, atrasptr = NULL;
  int opcion, clav;
  char descri[20];
  menu();
  printf("\n                Elige opcion: ");
  scanf("%d", &opcion);

  while (opcion != 4)
        { switch(opcion)
             { case 1: printf("\n                INSERCION EN LA COLA");   
                          printf("\n\n                Clave del articulo:  ");
                    scanf("%d", &clav);
                    fflush(stdin);
                    printf("\n          Descripcion del articulo:  ");
                    gets(descri);
                    enqueue(&adelaptr, &atrasptr, clav, descri);
                    break;
            default : printf("\n                Opcion no permitida.\n\n");
                      printf("                Enter para continuar...");
                      getch();
                      menu();
                      break;
             }
       menu();
       printf("\n                Elige opcion: ");
       scanf("%d", &opcion);
        }
  printf("\n                Fin del programa.\n\n");
  printf("\n                Enter para salir...");
  getch();
}

void menu(void)
     { system("cls");
       printf("\n\n\n\n"
       "              OPERACIONES DISPONIBLES CON LA COLA: \n\n\n\n"
       "              1 (enqueue) AGREGAR UN DATO A LA COLA\n"
       "              2 (dequeue) ELIMINAR UN DATO DE LA COLA\n"
       "              3 IMPRIMIR LA COLA\n"
       "              4 SALIR DEL PROGRAMA\n");
     }

void enqueue(COLANODOPTR *adelaptr, COLANODOPTR *atrasptr, int cla, char desc[20])
     { COLANODOPTR nvoptr;
       nvoptr = (COLANODOPTR) malloc(sizeof(COLANODO));
       if (nvoptr != NULL) {
    nvoptr->clave = cla;
    strcpy(nvoptr->descrip, desc);
    nvoptr->sigptr = NULL;
    if (*adelaptr==NULL)
        *adelaptr = nvoptr;
    else
       (*atrasptr)->sigptr = nvoptr;
    *atrasptr = nvoptr;
       }
       else
   printf("%c %s no fue insertado. No hay memoria disponible\n\n", cla, desc);
       }
 
 

No hay comentarios:

Publicar un comentario