순서 대기 열 기본 동작

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#define  SMALL 1
#if(SMALL)
 #define MAX 7
 int QueueData[MAX] = {'A','B','C','D','E','F','G'};
#else
 #define MAX 14
 int QueueData[MAX] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N'};
#endif
/*        */ 
typedef struct
{
 int data[MAX];
 int head;
 int tail;
}sqQueue;
/*       */ 
struct LinkQueue
{
 int data;
 struct LinkQueue *link;
};
typedef struct LinkQueue lkQueue;
//-----------------------------------------------------------------------------------------
/*              */
//          sqInitialize()
sqQueue *sq_Initialize()
{
 sqQueue *p;
 p = (sqQueue *)malloc(sizeof(sqQueue));
 if(p==NULL)
 {
  return(NULL);
 }
 else
 {
  p->data[0] = 0;
  p->head = 0;
  p->tail = 0;
  return(p);
 }
}
//         qIsEmpty()
int qIsEmpty(sqQueue *queue)
{
 if(queue->head==queue->tail)
  return(1);
 else
  return(0);
}
//         qIsFull()
int qIsFull(sqQueue *queue)
{
 if(queue->head==MAX)
  return(1);
 else
  return(0);
}
//         qInQueue()
int qInQueue(sqQueue *queue,int data)
{
 if(queue->tail==MAX)
 { 
  printf("    !/n");
  return(0);
 }
 else
 {
  queue->data[queue->tail++] = data;
  return(1);
 }
}
//         qOutQueue()
int qOutQueue(sqQueue *queue,int *p)
{
 if(queue->head==queue->tail)
 {
  printf("    !/n");
  return(0);
 }
 else
 {
  *p = queue->data[queue->head++];
  return(1);
 }
}
//             qInputValue()
void qInputValue(sqQueue *queue,int array[])
{
 int i=0;
 while(qInQueue(queue,array[i]))
  printf("Queue[%d] = %c/t",i,array[i++]);
}
//             qOutputValue()
void qOutputValue(sqQueue *queue)
{
 int i,QueueData;
 i=0;
 while(qOutQueue(queue,&QueueData))
  printf("Queue[%d] = %c/t",i++,QueueData);
 printf("/n");
}
//-----------------------------------------------------------------------------------------
/*              */
//          lk_Initialize()
lkQueue *lk_Initialize()
{
 lkQueue *p;
 p = (lkQueue *)malloc(sizeof(lkQueue));
 if(p==NULL)
 {
  return(NULL);
 }
 else
 {
  p->data = 0;
  p->link = NULL;
  return(p);
 }
}
//         lkInQueue()
//  :    ,       ,         
lkQueue *lkInQueue(lkQueue *tail,int data)
{
 lkQueue *p;
 p = (lkQueue *)malloc(sizeof(lkQueue));
 if(p==NULL)
 {
  printf("             !/n");
  return(tail);
 }
 else
 {
  p->data = data;
  p->link = tail;
  return(p);
 }
}
//         lkInQueue()
//  :    ,       ,         
lkQueue *lkOutQueue(lkQueue *head,int *data)
{
 *p = head->data;
 lkQueue *p;
 p = (lkQueue *)malloc(sizeof(lkQueue));
 if(p==NULL)
 {
  printf("           !/n");
  return(tail);
 }
 else
 {
  p->data = data;
  p->link = tail;
  return(p);
 }
}
//-----------------------------------------------------------------------------------------
int main(int argc, char* argv[])
{
 sqQueue *queue1;
 lkQueue **lkHead,**lkTail;
 queue1 = sq_Initialize();
 if(queue1==NULL)
 {
  printf("           !/n");
  return 0;
 }
 else
 {
  printf("/n    [  ]  ........./n");
  qInputValue(queue1,QueueData);
  printf("/n    [  ]  ........./n");
  qOutputValue(queue1);
 }
 printf("/n    !/n");
 return 0;
} 

좋은 웹페이지 즐겨찾기