数据结构第三章课后题

3.2(2)假设以I和O分别表示进栈和出栈,站的初态和终栈均为空,写出一个算法判定所给的操作序列是否合法,若合法则返回1,否则返回0.

代码:

#include <iostream>
#include <malloc.h>
using namespace std;
#define MaxSize 50
typedef struct Node
{
    char data[MaxSize];
    int top;
} LiStack;
void InitStack(LiStack *&s)
{
    s=(LiStack *)malloc(sizeof(LiStack));
    s->top=-1;
}
bool is_legal(LiStack *s,char a[],int n)
{
    for(int i=0; i<n; i++)
    {
        if(a[i]=='I')
        {
            s->top++;
            s->data[s->top]=a[i];
        }
        else
        {
            int e;
            e=s->data[s->top];
            s->top--;
            if(s->top==-2)
                return false;
        }
    }
    if(s->top==-1)
        return true;
    return false;
}
int main()
{
    LiStack *s;
    char a[100],x;
    int i=0;
    while(cin>>a[i])
        i++;
    InitStack(s);
    if(is_legal(s,a,i))
        cout<<"合法!"<<endl;
    else
        cout<<"不合法"<<endl;
    return 0;
}




3.3假设表达式中允许包含三中括号:圆括号、方括号和大括号。编写一个算法判断表达式中的括号是否正确配对。

代码:

#include <iostream>
#include <malloc.h>
using namespace std;
typedef struct
{
    char data[1000];
    int top;
} SqStack;
void InitStack(SqStack *&s,char a[])
{
    s=(SqStack *)malloc(sizeof(SqStack));
    s->top=-1;
}
bool Is_legal(SqStack *&s,char a[],int n)
{
    char e;
    if(s->top==n-1)
        return false;
    for(int i=0; i<n; i++)
    {
        if(a[i]=='('||a[i]=='['||a[i]=='{')
        {
            s->top++;
            s->data[s->top]=a[i];
        }
        else
        {
            e=s->data[s->top];
            s->top--;
            cout<<e<<a[i]<<" ";
            if(i==n-1&&s->top==-1)
                return true;
            if((e=='('&&a[i]==')')||(e=='['&&a[i]==']')||(e=='{'&&a[i]=='}'))
                continue;
            else
                return false;

        }
    }
}
int main()
{
    SqStack *s;
    char a[1000];
    int i=0;
    while(cin>>a[i])
    {
        i++;
    }
    InitStack(s,a);
    if(Is_legal(s,a,i))
        cout <<"合法"<<endl;
    else
        cout<<"不合法"<<endl;
    return 0;
}



3.4设从键盘输入一个整数序列a1,a2,a3,……an,是编程实现:当ai>0时,ai进队,当ai<0时,将队首元素出队,当ai=0时,输入结束。要求将队列处理成环形队列,进队和出队操作单独编写算法,并在异常情况是(如队满)打印错误信息。

代码:

#include <iostream>
#include <malloc.h>
using namespace std;
#define MaxSize 5
typedef struct
{
    int data[MaxSize];
    int front,rear;
} SqQueue;
void InitQueue(SqQueue *&q)
{
    q=(SqQueue *)malloc(sizeof(SqQueue));
    q->front=q->rear=0;
}
bool enQueue(SqQueue *&q,int e)
{
    if((q->rear+1)%MaxSize==q->front)
    {
        cout<<"队列已满!"<<endl;
        return false;
    }
    q->rear=(q->rear+1)%MaxSize;
    q->data[q->rear]=e;
    return true;
}
bool daQueue(SqQueue *&q,int e)
{
    if(q->front==q->rear)
    {
        cout<<"队列已为空!"<<endl;
        return false;
    }
    q->front=(q->front+1)%MaxSize;
    return true;
}
int main()
{
    SqQueue *q;
    int a;
    InitQueue(q);
    while(cin>>a&&a!=0)
    {
        if(a>0)
            enQueue(q,a);
        else
            daQueue(q,a);

    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值