《栈和队列》实验四 (无行号)
2015-10-29 08:46
204 查看
#include<stdio.h> #include<stdlib.h> #include<conio.h> #define TRUE 1 #define FALSE 0 #define OK 1 #define ERROR 0 #define INFEASIBLE -1 #define OVERFLOW -2 #define INIT_SIZE_STACK 100 #define STACKINCREMENT 10 typedef int Status; typedef char ElemType; typedef struct { ElemType *top; ElemType *base; int stacksize; }SqStack; //构造一个空栈 Status InitStack(SqStack &S) { S.base = (ElemType *)malloc(INIT_SIZE_STACK*sizeof(ElemType)); if(!S.base) exit(OVERFLOW); S.top = S.base; //栈空的标志 S.stacksize = INIT_SIZE_STACK; return OK; } //入栈 Status Push(SqStack &S,ElemType e) {//插入为新的栈顶元素 if(S.top - S.base >= S.stacksize) {//栈满,追加空间 S.base = (ElemType *)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(ElemType)); if(!S.base) exit(OVERFLOW); S.top = S.base+S.stacksize; S.stacksize +=STACKINCREMENT; } //*S.top = e; //S.top++; *(S.top++) = e; printf("s.[%d]=%c\n",S.top-S.base-1,e); return OK; } //删除栈顶元素 Status Pop(SqStack &S,ElemType &e) { if(S.top == S.base) return ERROR; //S.top--; //e = *S.top; e+*(--S.top); return OK; } //判断栈是否为空 int StackEmpty(SqStack S) { if(S.top == S.base) return 1; else return 0; } //得到栈顶元素 Status GetTop(SqStack S) { ElemType e; if(S.top == S.base) return ERROR; e = *(S.top-1); return e; } #define QueueSize 100 //循环队列的存储结构 typedef struct { ElemType *base; int front,rear; }SeqQueue; //构造一个循环队列 Status InitQueue(SeqQueue &Q) { Q.base = (ElemType *)malloc(QueueSize*sizeof(ElemType)); if(!Q.base) exit(OVERFLOW); Q.front = Q.rear = 0; return OK; } //插入为新的队尾元素 Status EnQueue(SeqQueue &Q,ElemType e) { if((Q.rear+1)%QueueSize==Q.front) { printf("Queue overflow"); return ERROR; } Q.base[Q.rear] = e; Q.rear = (Q.rear+1)%QueueSize; return OK; } //删除队头元素 Status DeQueue(SeqQueue &Q,ElemType &e) { if(Q.front == Q.rear) { printf("Queue empty"); return ERROR; } e = Q.base[Q.front]; Q.front = (Q.front+1)%QueueSize; return OK; } //得到队头元素 ElemType GetHead(SeqQueue Q) { if(Q.front == Q.rear) { printf("Queue enpty"); exit(ERROR); } else return Q.base[Q.front]; } //遍历循环队列 Status QueueTraverse(SeqQueue Q) { int p; if(Q.front == Q.rear) { printf("Queue empty"); return ERROR; } p = Q.front; do { printf("%2c",Q.base[p]); p = (p+1)%QueueSize; }while(p!=Q.rear); return OK; } void main() { SqStack s; SeqQueue q; ElemType ch,e1,e2; int state; InitStack(s); InitQueue(q); printf("input a string endding by#:"); scanf("%c",&ch); while(ch!='#') { Push(s,ch); EnQueue(q,ch); scanf("%c",&ch); } printf("\nThe Queue is;"); QueueTraverse(q); printf("\n"); state = TRUE; while(!StackEmpty(s) && state) { if(GetTop(s)==GetHead(q)) { Pop(s,e1); DeQueue(q,e2); } else state = FALSE; } if(state) printf("This string is HuiWen!\n"); else printf("The string is not HuiWen!\n"); }
相关文章推荐
- grep在指定类型的文件中查找字符 (转载)
- 简单并快乐着,带有语法强类型检查的事件总线(EventBus)框架完善篇
- angularjs与PHP的交互通信问题
- android OptionsMenu 和 ContextMenu
- oracle 利用over 查询数据和总条数,一条sql搞定
- 软件的组件化发展
- ubuntu 安装 ia32-libs
- Android 项目实践(五)——基础类和工具类的实现
- java中Comparable接口
- 【免费公开课】IT企业面试bar越来越高怎么办?如何应对?
- 如何在需求阶段发现更多的缺陷
- 并查集--HDU - 4496 D-City
- SQL Server执行计划的理解
- Android系统应用信息中存储和缓存的计算方法
- 【jQuery】jQuery操作<input>的聚焦与全选其内容
- AndroidManifest配置文件介绍
- 开源 java CMS - FreeCMS商业版isnew,newdays的使用方法
- redis集群实战
- webView调用百度地图的定位问题
- 10 个 Linux 中的 passwd 命令示例