用栈队列的方式模拟计算机读取和结束
来源:互联网 发布:网络与新媒体 好就业吗 编辑:程序博客网 时间:2024/06/06 02:38
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
typedef struct sNode{
char *data;
int top;
int bottom;
int MAXSIZE;
};
typedef struct sNode *Stack;
Stack createStack(int MAXSIZE){
Stack s=(Stack)malloc(sizeof(struct sNode));
s->data=(char *)malloc(MAXSIZE*sizeof(char));
s->top=-1;
s->bottom=-1;
s->MAXSIZE=MAXSIZE;
return s;
}
bool isFull(Stack s){
return (s->MAXSIZE-1==s->top);
}
bool isEmpty(Stack s){
return (s->top==-1);
}
bool push(Stack s,char x){
if(isFull(s)){
printf("OVERFLOW!\n");
return false;
}else{
return (s->data[++(s->top)]=x);//可以使用指针的首地址的方式进行*(s->data+s->top)=x;
}
}
char pop(Stack s){
if(isEmpty(s)){
printf("UNDERFLOW!\n");
return 520;
}else{
return (s->data[(s->top)--]);//返回当前top对应的数值
}
}
char cc(Stack s){
if(isEmpty(s)){
printf("UNDERFLOW!\n");
return 520;
}else{
return (s->data[++(s->bottom)]);//返回当前top对应的数值
}
}
void verify(Stack s,int max){
if(max%2!=0){//当数字为奇数时不满足成对出现的情况直接判断错误,并打印error
printf("error!!!!!\n");
return ;
}
char c;
printf("Please input ;");
while(!isFull(s)){//当表指向最后一位时代表表满了
scanf(" %c",&c);//防止将/r读入到%c中影响结果
push(s,c);//将当前的字符存入栈中
}
int cont=0,pos=max/2;
char a,b;
while(pos--){
// printf("%c\n",cc(s));
// printf(" %c\n",pop(s));
// {//判断当最后一位和第一位相同,最后第二位和第二位相同,以此类推
// cont++;//计数器加一
// }
a=cc(s);//用a接收否者后面多次判断的话会影响结果
b=pop(s);
if(a=='['&&b==']'){
cont++;
}
if(a=='('&&b==')'){
cont++;
}
if(a=='{'&&b=='}'){
cont++;
}
}
if(cont==(max/2)){
printf("Complete!!!!!\n");
}else{
printf("error!!!!!\n");
}
}
int main(){
int max;
printf("Please input your MAXSIZE?");
scanf("%d",&max);
Stack s;
s=createStack(max);
verify(s,max);
return 0;
}
#include<stdlib.h>
#include<stdbool.h>
typedef struct sNode{
char *data;
int top;
int bottom;
int MAXSIZE;
};
typedef struct sNode *Stack;
Stack createStack(int MAXSIZE){
Stack s=(Stack)malloc(sizeof(struct sNode));
s->data=(char *)malloc(MAXSIZE*sizeof(char));
s->top=-1;
s->bottom=-1;
s->MAXSIZE=MAXSIZE;
return s;
}
bool isFull(Stack s){
return (s->MAXSIZE-1==s->top);
}
bool isEmpty(Stack s){
return (s->top==-1);
}
bool push(Stack s,char x){
if(isFull(s)){
printf("OVERFLOW!\n");
return false;
}else{
return (s->data[++(s->top)]=x);//可以使用指针的首地址的方式进行*(s->data+s->top)=x;
}
}
char pop(Stack s){
if(isEmpty(s)){
printf("UNDERFLOW!\n");
return 520;
}else{
return (s->data[(s->top)--]);//返回当前top对应的数值
}
}
char cc(Stack s){
if(isEmpty(s)){
printf("UNDERFLOW!\n");
return 520;
}else{
return (s->data[++(s->bottom)]);//返回当前top对应的数值
}
}
void verify(Stack s,int max){
if(max%2!=0){//当数字为奇数时不满足成对出现的情况直接判断错误,并打印error
printf("error!!!!!\n");
return ;
}
char c;
printf("Please input ;");
while(!isFull(s)){//当表指向最后一位时代表表满了
scanf(" %c",&c);//防止将/r读入到%c中影响结果
push(s,c);//将当前的字符存入栈中
}
int cont=0,pos=max/2;
char a,b;
while(pos--){
// printf("%c\n",cc(s));
// printf(" %c\n",pop(s));
// {//判断当最后一位和第一位相同,最后第二位和第二位相同,以此类推
// cont++;//计数器加一
// }
a=cc(s);//用a接收否者后面多次判断的话会影响结果
b=pop(s);
if(a=='['&&b==']'){
cont++;
}
if(a=='('&&b==')'){
cont++;
}
if(a=='{'&&b=='}'){
cont++;
}
}
if(cont==(max/2)){
printf("Complete!!!!!\n");
}else{
printf("error!!!!!\n");
}
}
int main(){
int max;
printf("Please input your MAXSIZE?");
scanf("%d",&max);
Stack s;
s=createStack(max);
verify(s,max);
return 0;
}
0 0
- 用栈队列的方式模拟计算机读取和结束
- 队列模拟栈和栈模拟队列
- 栈和队列的相互模拟
- 栈和队列的相互模拟
- (C++)栈和队列的模拟实现
- java用LinkedList模拟栈和队列
- [Python] 用list模拟栈和队列
- Python模拟栈 和 队列
- 数组模拟队列和栈
- 链表-模拟栈和队列
- LinkedList模拟栈和队列
- 菜鸟:模拟栈和队列的push和pop
- 用堆栈模拟队列,和用队列模拟堆栈
- 用堆栈模拟队列,和用队列模拟堆栈
- 使用消息队列读取模拟量的值
- 数据结构—栈和队列的相互模拟
- HDU 1702 (栈和队列的模拟)
- 计算机中数据的存储和读取
- Android 通过注入js实现读取webview中图片并放大的优化
- bzoj2190 SDOI2008仪仗队
- 从一个文件中读取用户列表,来添加系统用户
- Burp Suite证书导入证书(https抓包)
- 二分查找----递归实现
- 用栈队列的方式模拟计算机读取和结束
- java多线程:14、Exchanger同步辅助类
- Linux 三大文本处理命令之一GREP
- oc——block
- 有重复元素排列问题
- 车间生产管理中生产质检难题如何解决?
- spring mybatis集成报错:org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)
- public ArrayList(Collection<? extends E> c){}中的 ? 和 E
- PL/SQL Developer连接本地Oracle 11g 64位数据库