status QueueInit(SQueue* queue);
status QueueDestroy(SQueue* queue);
status QueueWrite(SQueue* queue,ElemType e);
status QueueRead(SQueue* queue,ElemType* e);
void WriteToQueue(SQueue* queue);
void ReadFromQueue(SQueue* queue);
void QueuePrint(SQueue* queue);
void DataStructure()
{
<span style="white-space:pre"> </span>SQueue queue;
<span style="white-space:pre"> </span>QueueInit(&queue);
<span style="white-space:pre"> </span>//_beginthread(WriteToQueue,0,&queue);
<span style="white-space:pre"> </span>WriteToQueue(&queue);
<span style="white-space:pre"> </span>free(queue.addr);
}
void QueuePrint(SQueue* queue)
{
<span style="white-space:pre"> </span>int i;
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>for(i=0; i<MAX_INIT_SIZE; i++)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>printf("%c ",queue->addr[i]);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>printf("=========\n");
}
status QueueInit(SQueue* queue)
{
<span style="white-space:pre"> </span>assert( NULL != (queue->addr = (ElemType*)malloc(sizeof(ElemType)*MAX_INIT_SIZE)));
<span style="white-space:pre"> </span>queue->head = -1;
<span style="white-space:pre"> </span>queue->tail = 0;
<span style="white-space:pre"> </span>queue->size = MAX_INIT_SIZE;
<span style="white-space:pre"> </span>memset(queue->addr,0,queue->size);
<span style="white-space:pre"> </span>return 0;
}
/************************************************************************/
//tail keep empty
/************************************************************************/
status QueueWrite(SQueue* queue,ElemType e)
{<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>if(queue->tail == queue->head)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>printf("over flow\n");
<span style="white-space:pre"> </span>return -1;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>queue->addr[queue->tail] = e;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(queue->tail == queue->size-1)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>queue->tail = 0;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>queue->tail += 1;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(queue->head == -1)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>queue->head = 0;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return 0;
}
status QueueRead(SQueue* queue,ElemType* e)
{
<span style="white-space:pre"> </span>if(queue->head < 0)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>printf("Queue is empty\n");
<span style="white-space:pre"> </span>*e = '\r';
<span style="white-space:pre"> </span>return -1;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>*e = queue->addr[queue->head];
<span style="white-space:pre"> </span>if(queue->head == queue->size-1)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>queue->head = 0;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>else
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>queue->head += 1;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>if(queue->head == queue->tail)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>queue->head = -1;
<span style="white-space:pre"> </span>queue->tail = 0;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>return 0;
}
void WriteToQueue(SQueue* queue)
{
<span style="white-space:pre"> </span>ElemType get,read;
<span style="white-space:pre"> </span>printf("anykey to start\n");
<span style="white-space:pre"> </span>while((get = fgetc(stdin)) != 'q')
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>printf("\nr/w to switch\nwrite:\n");
<span style="white-space:pre"> </span>while((get = fgetc(stdin)) != 'r')
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>printf(">>%c ",get);
<span style="white-space:pre"> </span>QueueWrite(queue,get);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>QueuePrint(queue);
<span style="white-space:pre"> </span>printf("read:\n");
<span style="white-space:pre"> </span>while((get = fgetc(stdin)) != 'w')
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>read = '=';
<span style="white-space:pre"> </span>QueueRead(queue,&read);
<span style="white-space:pre"> </span>printf("%c ",read);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>}
}