数据结构实验2

函数题

6-1 排队叫号系统

编写程序实现银行排队叫号系统,采用链队列作为存储结构。

函数接口定义:

Status InitLinkQueue(LinkQueue &Q);//对链队列进行初始化
Status EnLinkQueue(LinkQueue &Q,QElemType e);//入队
Status DeLinkQueue(LinkQueue &Q,QElemType &e);//出队
Status QueueEmpty(LinkQueue Q);//判断队空

裁判测试程序样例:

在这里给出函数被调用进行测试的例子。例如:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define MAX_QSIZE 30
typedef int Status;
typedef int QElemType;
typedef struct Qnode
{
    QElemType data;
    struct Qnode * next;
}QNode, *QueuePtr;//创建节点类型
typedef  struct Queue{
  QueuePtr   front ; //队首指针
  QueuePtr   rear ; //队尾指针
  }LinkQueue;
Status InitLinkQueue(LinkQueue &Q);//对链队列进行初始化
Status EnLinkQueue(LinkQueue &Q,QElemType e);//入队
Status DeLinkQueue(LinkQueue &Q,QElemType &e);//出队
Status QueueEmpty(LinkQueue Q);//判断队空

int main()
{
    QElemType no=1;
    QElemType callno;
    int select,flag=1;
    LinkQueue qu;
    InitLinkQueue(qu);
    while(flag==1)
    {
        //printf("1:排队2:叫号0:退出 请选择:");
        scanf("%d",&select);
        switch(select)
        {
        case 1: printf("您的序号为:%d\n",no);
                EnLinkQueue(qu,no);  //no入队
                no++;
                break;
        case 2: if(DeLinkQueue(qu,callno)==ERROR)
                    printf(">>没有排队的客户!\n");
            else//队不空
                printf(">>请%d号办理业务\n",callno);
                break;
        case 0:flag=0;
        }
    }
    return 0;
}

/* 请在这里填写答案 */

输入样例:

1
1
1
2
2
2
2
0

输出样例:

您的序号为:1
您的序号为:2
您的序号为:3
>>请1号办理业务
>>请2号办理业务
>>请3号办理业务
>>没有排队的客户!

参考答案:

Status InitLinkQueue(LinkQueue &Q)
{
	Q.front=(QNode *)malloc(sizeof(QNode));
	if(Q.front!=NULL)
	{
		Q.rear=Q.front;
		Q.front->next=NULL;
 		return(TRUE);
	}
	else return(FALSE);
}
Status EnLinkQueue(LinkQueue &Q,QElemType e)
{
    QNode *newNode;
	newNode=(QNode *)malloc(sizeof(QNode));
	if(newNode!=NULL)
	{
		newNode->data=e;
		newNode->next=NULL;
		Q.rear->next=newNode;
		Q.rear=newNode;
		return(TRUE);
	}
	else return ERROR;
}
Status DeLinkQueue(LinkQueue &Q,QElemType &e)
{
    QNode *p;
	if(Q.front==Q.rear) return ERROR;
	p=Q.front->next;
	Q.front->next=p->next;
	if(Q.rear==p) Q.rear=Q.front;  
	e=p->data;
	free(p);
	return OK;
}
Status QueueEmpty(LinkQueue Q)
{
    if(Q.front==Q.rear) return TRUE;
	else return FALSE;
}

6-2 括号匹配

下面程序可实现表达式中括号匹配检查,约定只有‘()’、'[]'、'{}'三种括号。请将以下程序补充完整。

函数接口定义:

在这里描述函数接口。例如:
Status push(Sqstack &S,SElemType x) //x入栈S
{
}
Status pop(Sqstack &S,SElemType &e)//从S栈出栈1次元素放入e
{
}
Status Compare(char s[]) //s为表达式
{
   Sqstack S;
   SElemType e;
   Status flag=TRUE;
   int i=0;
   iniStack(S);
   while(s[i]!='#' && flag==TRUE )
   {
       switch(s[i])
       {
         case '(':
         case '[':
         case '{':push(S,s[i]);break;
         case ')': if(pop(S,e)==ERROR || e!='(')//如果是(
                     flag=FALSE;break;
         case ']': if(_________________)//如果是[
                     flag=FALSE;break;
         case '}': if(_________________)//如果是{
                     flag=FALSE;break;
       }
       i++;
   }
   if(flag==TRUE && s[i]=='#' && S.top==S.base)
     return TRUE;
   else
     return FALSE;
}

裁判测试程序样例:

#include <stdio.h>
#include <malloc.h>
typedef int Status;
typedef char SElemType;
#define stack_INIT_SIZE     100
#define stackINCREMENT  10
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
 typedef struct {
       SElemType  *base;   //栈底指针
       SElemType  *top;     //栈顶指针
       int stacksize;       //栈空间
}Sqstack;

Status iniStack(Sqstack &S) //初始化栈
{
 //构造一个空栈S
    S.base=(SElemType*)malloc(stack_INIT_SIZE * sizeof(SElemType));
    if(!S.base)return(ERROR);
    S.top=S.base;
    S.stacksize=stack_INIT_SIZE;
    return OK;
  }//InitStack

/* 你的代码将被嵌入到这里 */

int main()
{
    char s[100];
    Status st;
  scanf("%s",s);
   st=Compare(s);
    if(st==TRUE)
        printf("检查结果:括号匹配!");
    else
        printf("检查结果:括号不匹配!");
    return 0;
}

参考答案:

Status push(Sqstack &S,SElemType x) //x入栈S
{
    if(S.top-S.base==S.stacksize) return ERROR;
    *S.top=x;
    S.top++;
    return OK;
}
Status pop(Sqstack &S,SElemType &e)//从S栈出栈1次元素放入e
{
    if(S.top==S.base) return ERROR;
    S.top--;
    e=*S.top;
    return OK;
}
Status Compare(char s[]) //s为表达式
{
   Sqstack S;
   SElemType e;
   Status flag=TRUE;
   int i=0;
   iniStack(S);
   while(s[i]!='#' && flag==TRUE )
   {
       switch(s[i])
       {
         case '(':
         case '[':
         case '{':push(S,s[i]);break;
         case ')': if(pop(S,e)==ERROR || e!='(')//如果是(
                     flag=FALSE;break;
         case ']': if(pop(S,e)==ERROR || e!='[')//如果是[
                     flag=FALSE;break;
         case '}': if(pop(S,e)==ERROR || e!='{')//如果是{
                     flag=FALSE;break;
       }
       i++;
   }
   if(flag==TRUE && s[i]=='#' && S.top==S.base)
     return TRUE;
   else
     return FALSE;
}

 编程题

7-1 软硬车厢交替排列

设车辆厂生产了硬座车厢和软座车厢共n节(混合在一起),要求使用队列的基本操作,编程实现所有硬座车厢和所有软座车厢交替排列。例如硬座车厢用H来表示,软座车厢用S来表示,从键盘上输入8节车厢代号为SHHSSHSS ,输出为HSHSHSSS。若从键盘输入8节车厢代号为SHHSHHSH ,输出为HSHSHSHH。

输入格式:

第一行输入硬座和软座车厢共有的节数,2<=节数<=10;
第二行按节数输入车厢代号。

输出格式:

若车厢节数超出合法范围,输出“ERROR”,否则输出H和S车厢交替排列结果,注意输出结果以H开头。

输入样例:

8
SHHSSHSS

输出样例:

HSHSHSSS

参考答案:

#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define TRUE 1
#define FALSE 0
#define MAX_QSIZE 30
typedef int Status;
typedef char QElemType;
typedef struct Qnode
{
    QElemType data;
    struct Qnode * next;
}QNode, *QueuePtr;//创建节点类型
typedef  struct Queue{
  QueuePtr   front ; //队首指针
  QueuePtr   rear ; //队尾指针
  }LinkQueue;
Status InitLinkQueue(LinkQueue &Q)
{
	Q.front=(QNode *)malloc(sizeof(QNode));
	if(Q.front!=NULL)
	{
		Q.rear=Q.front;
		Q.front->next=NULL;
 		return OK;
	}
	else return ERROR;
}
Status EnLinkQueue(LinkQueue &Q,QElemType e)
{
    QNode *newNode;
	newNode=(QNode *)malloc(sizeof(QNode));
	if(newNode!=NULL)
	{
		newNode->data=e;
		newNode->next=NULL;
		Q.rear->next=newNode;
		Q.rear=newNode;
		return OK;
	}
	else return ERROR;
}
Status DeLinkQueue(LinkQueue &Q,QElemType &e)
{
    QNode *p;
	if(Q.front==Q.rear) return ERROR;
	p=Q.front->next;
	Q.front->next=p->next;
	if(Q.rear==p) Q.rear=Q.front;  
	e=p->data;
	free(p);
	return OK;
}
Status QueueEmpty(LinkQueue Q)
{
    if(Q.front==Q.rear) return TRUE;
	else return FALSE;
}
int main()
{
    LinkQueue QH,QS;
    InitLinkQueue(QH);
    InitLinkQueue(QS);
    char temp;
    int n;
    scanf("%d",&n);
    if(n>10 || n<2)
    {
        printf("ERROR");
        return 0;
    }
    getchar();
    for(int i=0;i<n;i++)
    {
        scanf("%c",&temp);
        if(temp=='H') EnLinkQueue(QH,temp);
        else if(temp=='S') EnLinkQueue(QS,temp);
    }
    for(int i=0;i<n;i++)
    {
        if((i%2==0 && QueueEmpty(QH)==FALSE) || QueueEmpty(QS)==TRUE)
        {
            char e;
            DeLinkQueue(QH,e);
            printf("%c",e);
        }
        else if((i%2==1 && QueueEmpty(QS)==FALSE) || QueueEmpty(QH)==TRUE)
        {
            char e;
            DeLinkQueue(QS,e);
            printf("%c",e);
        }
    }
    return 0;
}

7-2 后缀式求值(一位整型操作数版)

我们人类习惯于书写“中缀式”,如 3 + 5 * 2 ,其值为13。 (p.s. 为什么人类习惯中缀式呢?是因为中缀式比后缀式好用么?)

而计算机更加习惯“后缀式”(也叫“逆波兰式”,Reverse Polish Notation)。上述中缀式对应的后缀式是: 3 5 2 * +

现在,请对输入的后缀式进行求值。为了简化输入处理和运算,运算数(操作数)不超过300个且均为1位正整数,运算符(操作符)仅有+ - * /(加减乘除)四种,运算数和运算符之间没有空格间隔,且题目保证运算的中间结果和最终结果都在整型范围内

但是注意,题目输入的后缀式可能错误,例如:

  • 1234+- 错误,缺少运算符
  • 123+-* 错误,缺少运算数
  • 122-/ 错误,除数为0

题目保证以上三种错误不会同时发生。

输入格式:

第一行给出一个不超过10的正整数k;

接下来k行,每行给出一个后缀式,后缀式的格式如上文所描述,1位操作数且无空格间隔。

输出格式:

输出有k行,对于所输入的每个后缀式,判断是否正确(可求值),并在一行里输出:

  • 如果后缀式无误、可求值,输出结果
  • 如果发现除数为0,则输出Division By Zero!
  • 如果发现其它错误,则输出Expression Error!

输入样例1:

2
1234+-*
123+4-*

输出样例1:

-5
1

输入样例2:

2
12+
1234+-

输出样例2:

3
Expression Error!

输入样例3:

2
2222+-*
22+-*

输出样例3:

-4
Expression Error!

输入样例4:

1
2222-/+

输出样例4:

Division By Zero!

参考答案:

#include <stdio.h>
#include <malloc.h>
typedef int Status;
typedef int SElemType;
#define stack_INIT_SIZE 100
#define stackINCREMENT 10
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
typedef struct {
       SElemType  *base;   //栈底指针
       SElemType  *top;     //栈顶指针
       int stacksize;       //栈空间
}Sqstack;
Status iniStack(Sqstack &S) //初始化栈
{
 //构造一个空栈S
    S.base=(SElemType*)malloc(stack_INIT_SIZE * sizeof(SElemType));
    if(!S.base)return(ERROR);
    S.top=S.base;
    S.stacksize=stack_INIT_SIZE;
    return OK;
  }//InitStack
Status push(Sqstack &S,SElemType x) //x入栈S
{
    if(S.top-S.base==S.stacksize) return ERROR;
    *S.top=x;
    S.top++;
    return OK;
}
Status pop(Sqstack &S,SElemType &e)//从S栈出栈1次元素放入e
{
    if(S.top==S.base) return ERROR;
    S.top--;
    e=*S.top;
    return OK;
}
Status Run(Sqstack &S)
{
    iniStack(S);
    char temp;
    int num1,num2,e;
    scanf("%c",&temp);
    while(temp!='\n')
    {
        if(temp!='+'&&temp!='-'&&temp!='*'&&temp!='/') push(S,temp-'0');
        else
        {
            if(S.top-S.base<=1)
            {
                printf("Expression Error!");
                free(S.base);
                return 0;
            }
            switch(temp)
            {
                case '+':
                    pop(S,e);
                    num1=e;
                    pop(S,e);
                    num2=e;
                    push(S,num1+num2);
                    break;
                case'-':
                    pop(S,e);
                    num1=e;
                    pop(S,e);
                    num2=e;
                    push(S,num2-num1);
                    break;
                case '*':
                    pop(S,e);
                    num1=e;
                    pop(S,e);
                    num2=e;
                    push(S,num1*num2);
                    break;
                case '/':
                    pop(S,e);
                    num1=e;
                    pop(S,e);
                    num2=e;
                    if(num1==0)
                    {
                        printf("Division By Zero!");
                        free(S.base);
                        return 0;
                    }
                    push(S,num2/num1);
                    break;
            }
        }
        scanf("%c",&temp);
    }
    if(S.top-S.base>1) 
    {
        printf("Expression Error!\n");
        free(S.base);
        return 0;
    }
    pop(S,e);
    printf("%d\n",e);
    free(S.base);
    return 1;
}
int main()
{
    int n;
    Sqstack S;
    scanf("%d",&n);
    getchar();
    for(int i=1;i<=n;i++) Run(S);
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yhan计算机

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值