循环队列的基本操作
2016-04-24 22:09
288 查看
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 20 typedef struct{ char data[MAXSIZE]; int rear; int front; }SeqQueue; SeqQueue * init(){ SeqQueue * sq; sq = (SeqQueue *)malloc(sizeof(SeqQueue)); sq->rear = 0; sq->front = 0; return sq; } void inSeqQueue(SeqQueue * sq,char x){ if((sq->rear+1)%MAXSIZE == sq->front){ printf("the queue is full!\n"); }else{ sq->rear = (sq->rear + 1) % MAXSIZE; sq->data[sq->rear] = x; } } char outSeqQueue(SeqQueue * sq){ char x; if(sq->rear == sq->front){ printf("the queue is empty!\n"); return '0'; }else{ sq->front = (sq->front + 1) % MAXSIZE; x = sq->data[sq->front]; return x; } } void print(SeqQueue * sq){ int i; i = (sq->front + 1) % MAXSIZE; while(i != sq->rear){ printf("%4c",sq->data[i]); i = (i+1) % MAXSIZE; } printf("%4c",sq->data[i]); printf("\n"); } int main(){ SeqQueue * sq; char x; char y; sq = init(); printf("please input the values of the queue!\n"); scanf("%c",&x); while(x!='\n'){ inSeqQueue(sq,x); scanf("%c",&x); } print(sq); printf("after output (once)\n"); y = outSeqQueue(sq); printf("the output value is:%c\n",y); print(sq); printf("after output (twice)\n"); y = outSeqQueue(sq); printf("the output value is:%c\n",y); print(sq); return 0; }
相关文章推荐
- hdu5673 Robot
- Hibernate4实战 之 第二部分:Hibernate的基本配置
- CQOI2016 密钥破解 pollard-rho
- android.webkit.WebView/WebViewClient/WebChromeClient
- http请求和消息
- linux常用查看硬件设备信息命令
- LeetCode *** 79. Word Search
- 机器学习基石第七讲:the vc dimension
- jQuery EasyUI之Form表单提交
- 如何应用assets下的文件
- 链栈的基本操作
- python文件读写
- 20145322何志威 《Java程序设计》第8周学习总结
- netstat命令使用
- Java的构造方法
- 装饰者模式
- web服务器的工作原理及相关协议
- IP地址与网段的划分
- java.util包---LinkedList
- vim下ctags+taglist环境的搭建