数据结构08

0801

队列是一种操作受限的线性表,具有先进先出的特性。现在有如下操作。
1:取队头元素
2:入队
3:出队
请给出缺失的函数(用课堂上介绍的循环队列实现)。
输入要求:
多组输入
每组输入一个整数cmd,代表操作命令,如果是入队操作,再输入一个需要入队的整数。
输出要求:
根据输入输出操作结果,详见代码
数据示例1:
输入:
1
2 1
2 2
2 3
2 4
2 5
2 6
1
3
1
2 6
3
3
3
3
3
3
输出:
ERROR
OK
OK
OK
OK
OK
ERROR
1
OK
2
OK
OK
OK
OK
OK
OK
ERROR
#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 5

// 函数结果状态码 
typedef int Status;
#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int ElemType;
typedef struct{
    ElemType  *base; 
    int front;
    int rear;
    int size;
}SqQueue;  

// 初始化空队列,数组长度为 MAXSIZE  
void init(SqQueue &Q);

// 将队头元素赋值到e中。成功返回OK,否则返回ERROR 
Status front(SqQueue Q, int &e);

// 将e入队。成功返回OK,否则返回ERROR 
Status enqueue(SqQueue &Q, ElemType e);

// 出队。成功返回OK,否则返回ERROR 
Status dequeue(SqQueue &Q);

int main(){
 int cmd, e, re;
 SqQueue Q;
 init(Q); 
 while(~scanf("%d",&cmd)){
  switch(cmd){
   case 1: // get front 
    re = front(Q, e);
    if(re) printf("%d\n", e);
    else printf("ERROR\n");
    break;
   case 2: // enqueue 
    scanf("%d", &e);
    re = enqueue(Q,e);
    if(re) printf("OK\n");
    else printf("ERROR\n");
    break;
   case 3: // dequeue 
    re = dequeue(Q);
    if(re) printf("OK\n");
    else printf("ERROR\n");
    break;
   default:
    printf("Unknown Command\n");
    break;
  }
 }
}
//
// 初始化空队列,数组长度为 MAXSIZE  
void init(SqQueue &Q){
	Q.base=new ElemType[MAXSIZE];
	Q.front=1;
	Q.rear=0;
	Q.size=0; 
}

// 将队头元素赋值到e中。成功返回OK,否则返回ERROR 
Status front(SqQueue Q, int &e){
	if(Q.size==0) return ERROR;
	e=Q.base[Q.front];
	return OK;
}

// 将e入队。成功返回OK,否则返回ERROR 
Status enqueue(SqQueue &Q, ElemType e){
	if(Q.size==MAXSIZE) return ERROR;
	if(++Q.rear==MAXSIZE) Q.rear=0;
	Q.base[Q.rear]=e;
	Q.size++;
	return OK;
}

// 出队。成功返回OK,否则返回ERROR 
Status dequeue(SqQueue &Q){
	if(Q.size==0) return ERROR;
	if(++Q.front==MAXSIZE)
	Q.front=0;
	Q.size--;
	return OK;
}

0802

队列是一种操作受限的线性表,具有先进先出的特性。现在有如下操作。
1:取队头元素
2:入队
3:出队
请给出缺失的函数(用链队列实现)。
输入要求:
多组输入
每组输入一个整数cmd,代表操作命令,如果是入队操作,再输入一个需要入队的整数。
输出要求:
根据输入输出操作结果,详见代码
数据示例1:
输入:
1
2 1
2 2
2 3
2 4
2 5
2 6
1
3
1
2 6
3
1
3
3
3
1
3
1
3
1
输出:
ERROR
OK
OK
OK
OK
OK
OK
1
OK
2
OK
OK
3
OK
OK
OK
6
OK
6
OK
ERROR
#include<stdio.h>
#include<stdlib.h>

// 函数结果状态码 
typedef int Status;
#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef int ElemType;
typedef struct QNode{
    ElemType   data;       //数据域
    struct QNode  *next;   //指针域
}QNode;  

typedef struct{
 QNode *front; // 队头 
 QNode *rear;  // 队尾 
}LinkQueue;

// 初始化空队列  
void init(LinkQueue &Q);

// 将队头元素赋值到e中。成功返回OK,否则返回ERROR 
Status front(LinkQueue Q, int &e);

// 将e入队,返回OK 
Status enqueue(LinkQueue &Q, ElemType e);

// 出队。成功返回OK,否则返回ERROR 
Status dequeue(LinkQueue &Q);

int main(){
 int cmd, e, re;
 LinkQueue Q;
 init(Q); 
 while(~scanf("%d",&cmd)){
  switch(cmd){
   case 1: // get front 
    re = front(Q, e);
    if(re) printf("%d\n", e);
    else printf("ERROR\n");
    break;
   case 2: // enqueue 
    scanf("%d", &e);
    re = enqueue(Q,e);
    if(re) printf("OK\n");
    else printf("ERROR\n");
    break;
   case 3: // dequeue 
    re = dequeue(Q);
    if(re) printf("OK\n");
    else printf("ERROR\n");
    break;
   default:
    printf("Unknown Command\n");
    break;
  }
 }
}

// 初始化空队列  
void init(LinkQueue &Q){
	Q.front=Q.rear=new QNode;
	Q.front->next=NULL;
}

// 将队头元素赋值到e中。成功返回OK,否则返回ERROR 
Status front(LinkQueue Q, int &e){
	if(Q.front->next==NULL)return ERROR;
	e=Q.front->next->data;
	return OK;
}

// 将e入队,返回OK 
Status enqueue(LinkQueue &Q, ElemType e){
	QNode *p=new QNode;
	p->data=e;
	p->next=NULL;
	Q.rear->next=p;
	Q.rear=p;
	return OK;
}

// 出队。成功返回OK,否则返回ERROR 
Status dequeue(LinkQueue &Q){
	if(Q.front->next==NULL) return ERROR;
	QNode *p=Q.front->next;
	Q.front->next=p->next;
	if(Q.rear==p)
	Q.rear=Q.front;
	delete p;
	return OK;
}

0803

判断一个表达式中的圆括号()和方括号[]是否匹配。
输入要求:
多组输入,每组输入一个长度不大于20的字符串(无空格),占一行。
输出要求:
括号匹配输出yes,否则输出no。每组输出后换行
数据示例1:
输入:
([](x+y))
[([][])]
[(])
([())
输出:
yes
yes
no
no
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
	char c[20];
	char a[20];
	while(gets(c)!=NULL){
		int flag=1;
		int l=strlen(c);
		int j=0;
		for(int i=0;i<l;i++){
			if(c[i]=='('||c[i]=='['){
				a[j++]=c[i];
			}
			else if(c[i]==')'||c[i]==']'){
				if(j==0){
					flag=0;
					break;
				}
				char p=a[--j];
				if((c[i]==')'&&p!='(')||(c[i]==']'&&p!='[')){
					flag=0;
					break;
				}
			}
		}
		if(j!=0){
			flag=0;
		}
		if(flag==1){
			printf("yes\n");
		}else{
			printf("no\n");
		}
	}
	return 0;
}

0804

后缀表达式求值
输入要求:
多组输入,每组输入一行长度不大于20的字符串(无空格),代表一个后缀表达式。
其中操作数是1-9的个位数字,操作符包括加(+)、减(-)、乘(*)、除(/)并且保证除数不为0。
示例:
246+*
293/+5-
输出要求:
每组数据输出表达式计算的结果,并换行。
示例:
20
0
数据示例1:
输入:
246+*
293/+5-
输出:
20
0
#include <stack>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
stack<char>s;
int as(char a){
	int b=a-48;
	return b;
}

int main()
{
	char a[20];
	while(gets(a)!=NULL){
		int num;
		int sum=0;
		int c,b;
		int l=strlen(a);
		for(int i=0;i<l;i++){
			if(a[i]>='1'&&a[i]<='9'){
				s.push(as(a[i]));
			}
			else if(a[i]=='+'||a[i]=='-'||a[i]=='*'||a[i]=='/'){
				if(a[i]=='+'){
					b=s.top();
					s.pop();
					c=s.top();
					s.pop();
					num=c+b;
					s.push(num);
				}
				else if(a[i]=='-'){
					b=s.top();
					s.pop();
					c=s.top();
					s.pop();
					num=c-b;
					s.push(num);
				}
				else if(a[i]=='*'){
					b=s.top();
					s.pop();
					c=s.top();
					s.pop();
					num=c*b;
					s.push(num);
				}
				else if(a[i]=='/'){
					b=s.top();
					s.pop();
					c=s.top();
					s.pop();
					num=c/b;
					s.push(num);
				}
			}
		}
		sum=num;
		printf("%d\n",sum);
	}
}

0805

将中缀表达式转换成后缀表达式
输入要求:
多组输入,每组输入一行长度不大于20的字符串(无空格),代表一个中缀表达式。
其中操作数是1-9的个位数字,操作符包括加(+)、减(-)、乘(*)、除(/)和括号并且保证除数不为0。
示例:
2*(4+6)
2+9/3-5
输出要求:
每组输出一个转换后的后缀表达式,并换行。
示例:
246+*
293/+5-
数据示例1:
输入:
2*(4+6)
2+9/3-5
输出:
246+*
293/+5-
#include <stack>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
using namespace std;
stack<char>s;
//char as(char a){
//	if()
//	return b;
//}

int main(){
	char a[20];
	while(gets(a)){
		int l=strlen(a);
		for(int i=0;i<l;i++){
			if(a[i]>='1'&&a[i]<='9'){
				printf("%c",a[i]);
//				break;
			}
			else {again:
				
				if(s.empty()) s.push(a[i]);
				
				else {
				switch(a[i]){
				
					case '+':
					case '-':
						if(s.top()=='*'||s.top()=='/'||s.top()=='+'||s.top()=='-'){	
							printf("%c",s.top());
							s.pop();
							goto again;
						}else s.push(a[i]);
						break;
						
					case '*':
					case '/':
						if(s.top()=='*'||s.top()=='/'){
						printf("%c",s.top());
						s.pop();
						goto again;
						}else s.push(a[i]);
					break;
					
					case '(':
						s.push(a[i]);
						break;
					case ')':
						while(s.top()!='('){
							printf("%c",s.top());
							s.pop();
						}
						s.pop();
						break;
						}
					}
				}
			}
				while(!s.empty()){
					printf("%c",s.top());
					s.pop();
				}
			}
			return 0;
		}

おすすめ

転載: blog.csdn.net/unhere123/article/details/116035436