循环队列 C语言实现
"Queue.h"
#include "Queue.h" #include <stdio.h> #include <stdlib.h> status Queue_init(SqQueue *Q) { Q->data = (char*)malloc(sizeof(char)*MAXSIZE); Q->front = ; Q->rear = ; return OK; } status Queue_In(SqQueue *Q,char e) //进入队列 { if((Q->rear +)%MAXSIZE ==Q->front) { printf("队列已满"); return ERROR; } Q->data[Q->rear] = e; Q->rear =(Q->rear+)%MAXSIZE; return OK; } char Queue_Out(SqQueue *Q) { char e; if(Q->front == Q->rear) { printf("队列为空"); return ERROR; } e = Q->data[Q->front]; Q->front = (Q->front+)%MAXSIZE; return e; } void Print_Queue(SqQueue Q) { int i=Q.front; printf("队列:\n"); while(i!= Q.rear) { printf("%c",Q.data[i]); i = (i+)%MAXSIZE; } printf("\n"); } int Queue_length(SqQueue Q) { return (Q.rear-Q.front+MAXSIZE)%MAXSIZE; }
"main.c"
#include <stdio.h> #include <stdlib.h> #include "Queue.h" #include <string.h> int main() { SqQueue Q; char c; char rec[MAXSIZE] ; int i; Queue_init(&Q); printf("请输入一个字符串:\n"); gets(rec); for(i=;rec[i]!= '\0';i++) { Queue_In(&Q,rec[i]); } Print_Queue(Q); printf("队长为:%d\n",Queue_length(Q)); printf("!加入游戏\n"); Queue_In(&Q,'!'); printf("%c退出了游戏\n",Queue_Out(&Q)); Print_Queue(Q); return ; }
相关推荐
ipqtjmqj 2020-03-23
hanyujianke 2020-02-25
niushao 2020-01-10
xx0cw 2019-12-10
whtqsq 2019-09-07
zzpdljd 2019-08-02
meridian00 2019-07-01
lickylin 2019-07-01
基尔霍夫的猫 2019-06-28
鱼天翱 2019-06-21
hanyujianke 2017-10-27
PHP100 2019-03-28
大故事家 2018-05-13
大故事家 2017-11-29
BAT 批处理程序 2017-06-19