队列




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>}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ZenZenZ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值