栈和队列的基本操作及应用

题目一:回文判断
[问题描述]
对于一个从键盘输入的字符串,判断其是否为回文。回文即正反序相同。如“abba”是回文,而“abab”不是回文。
[基本要求]
(1)数据从键盘读入;
(2)输出要判断的字符串;
(3)利用栈和队列对给定的字符串判断其是否是回文,若是则输出“Yes”,否则输出“No”。
[Sample Input]

123321
12312

[Sample Output]

Yes
No

code:

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
using namespace std;
const int MAXSIZE=1100;
typedef struct
{
    char *base;
    char *top;
    int l;
} SqStack;
void InitStack(SqStack &s)
{
    s.base=new char[MAXSIZE];
    if(!s.base)
        exit(1);
    s.top=s.base;
    s.l=MAXSIZE;
    return;
}
void Push(SqStack &s,char ch)
{
    if(s.top-s.base==s.l)
        return;
    *s.top++=ch;
    return;
}
typedef struct
{
    char *base;
    int front;
    int rear;
} SqQueue;
void InitQueue(SqQueue &q)
{
    q.base=new char[MAXSIZE];
    if(!q.base)
        exit(1);
    q.front=q.rear=0;
    return;
}
void EnQueue(SqQueue &q,char ch)
{
    if(q.rear+1==MAXSIZE)
        exit(1);
    q.base[q.rear++]=ch;
    return;
}
bool Palindrome(SqStack s,SqQueue q)
{
    while(s.top!=s.base||q.rear!=q.front)
    {
        s.top--;
        //cout<<*s.top<<" "<<q.base[q.front]<<endl;
        if(*s.top!=q.base[q.front])
            break;
        q.front++;
    }
    if(s.top!=s.base||q.rear!=q.front)
        return false;
    else return true;
}
int main()
{
    SqStack s;
    InitStack(s);
    SqQueue q;
    InitQueue(q);
    char ch;
    ch=getchar();
    while(ch!='\n')
    {
        EnQueue(q,ch);
        Push(s,ch);
        ch=getchar();
    }
    if(Palindrome(s,q))
        cout<<"Yes"<<endl;
    else
        cout<<"No"<<endl;
    return 0;
}

题目二:商品货架管理

[问题描述]
商店货架以栈的方式摆放商品。生产日期越近的越靠近栈底,出货时从栈顶取货。一天营业结束,如果货架不满,则需上货。入货直接将商品摆放到货架上,则会使生产日期越近的商品越靠近栈顶。这样就需要倒货架,使生产日期越近的越靠近栈底。
[基本要求]
设计一个算法,保证每一次上货后始终保持生产日期越近的商品越靠近栈底。
[实现提示]
可以用一个队列和一个临时栈作为周转。

code:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXSIZE=20;
typedef struct
{
    char *base;
    char *top;
    int l;
} SqStack;
void InitStack(SqStack &s)
{
    s.base=new char[MAXSIZE];
    if(!s.base)
        exit(1);
    s.top=s.base;
    s.l=MAXSIZE;
    return;
}
void Push(SqStack &s,char ch)
{
    if(s.top-s.base==s.l)
        return;
    *s.top++=ch;
    return;
}
typedef struct
{
    char *base;
    int front;
    int rear;
} SqQueue;
void InitQueue(SqQueue &q)
{
    q.base=new char[MAXSIZE];
    if(!q.base)
        return;
    q.front=q.rear=0;
    return;
}
int EnQueue(SqQueue &q,char ch)
{
    if((q.rear+1)%MAXSIZE==q.front)
    {
        cout<<"货满,请先上货"<<endl;
        return 0;
    }
    q.base[q.rear]=ch;
    q.rear=(q.rear+1)%MAXSIZE;
    return 1;
}
void Purchase(SqQueue &q)
{
    cout<<"进货:"<<endl;
    cout<<"一个字符代表一个商品,以换行结束"<<endl;
    char ch[MAXSIZE];
    scanf("%s",ch);
    int len=strlen(ch),i;
    cout<<"进货为:";
    for(i=0; i<len; i++)
    {
        if(EnQueue(q,ch[i])==0)
            break;
        cout<<ch[i]<<" ";
    }
    cout<<endl;
    return;
}
void Loading(SqStack &s,SqQueue &q)
{
    cout<<"上货:"<<endl;
    if(q.front==q.rear)
    {
        cout<<"没货,请先进货"<<endl;
        return;
    }
    else
    {
        if(s.top-s.base==s.l)
        {
            cout<<"货架满,请先出货再上货"<<endl;
            return;
        }
        else
        {
            SqStack s1;
            InitStack(s1);
            while(s.top!=s.base)
            {
                Push(s1,*(--s.top));
            }
            cout<<"上货物品为:";
            while(q.rear!=q.front)
            {
                if(s1.top-s1.base==s1.l)
                {
                    cout<<endl<<"货架已满"<<endl;
                    break;
                }
                cout<<q.base[q.front];
                Push(s1,q.base[q.front]);
                q.front=(q.front+1)%MAXSIZE;
            }
            if(q.rear==q.front)
            {
                cout<<endl<<"无货,货架未满"<<endl;
            }
            while(s1.top!=s1.base)
            {
                Push(s,*--s1.top);
            }
        }
    }
}
void Shipment(SqStack &s)
{
    cout<<"出货:"<<endl;
    cout<<"输入出货数目:"<<endl;
    int n,sum=0;
    cin>>n;
    cout<<"出货物品为:";
    while(sum!=n)
    {
        sum++;
        if(s.top==s.base)
        {
            cout<<"货架已空!!"<<endl;
            return;
        }
        else
        {
            cout<<*--s.top<<" ";
        }
    }
    if(sum==n)
        cout<<endl<<"出货完成!!"<<endl;
}
int main()
{
    int b;
    SqQueue q;
    SqStack s;
    InitQueue(q);
    InitStack(s);
    cout<<"1进货"<<endl;
    cout<<"2上货"<<endl;
    cout<<"3出货"<<endl;
    cout<<"其他结束"<<endl;
    cin>>b;
    while(b==1||b==2||b==3)
    {
        switch(b)
        {
        case 1:
            Purchase(q);
            break;
        case 2:
            Loading(s,q);
            break;
        case 3:
            Shipment(s);
            break;
        }
        cout<<"1进货"<<endl;
        cout<<"2上货"<<endl;
        cout<<"3出货"<<endl;
        cout<<"其他结束"<<endl;
        cin>>b;
    }
    return 0;
}

题目三:舞伴问题

假设在周末舞会上,男士们和女士们进入舞厅时,各自排成一队。跳舞开始时,依次从男队和女队的队头上各出一人配成舞伴。若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。现要求写一算法模拟上述舞伴配对问题。
【实验提示】
先入队的男士或女士亦先出队配成舞伴。因此该问题具体有典型的先进先出特性,可用队列作为算法的数据结构。在算法中,假设男士和女士的记录存放在一个数组中作为输入,然后依次扫描该数组的各元素,并根据性别来决定是进入男队还是女队。当这两个队列构造完成之后,依次将两队当前的队头元素出队来配成舞伴,直至某队列变空为止。此时,若某队仍有等待配对者,算法输出此队列中等待者的人数及排在队头的等待者的名字,他(或她)将是下一轮舞曲开始时第一个可获得舞伴的人。
【实验要求】
利用队列实现,存储结构采用顺序或链式均可

[Sample Input]

9
Asd F
Qwe M
Wer F
Wqw M
Zxc M
Sdf F
Erf M
Dfg M
Edc F

10
Asd F
Qwe M
Wer F
Wqw M
Zxc M
Sdf F
Erf M
Dfg M
Edc F
Rty F

[Sample Output]

F:Asd M:Qwe
F:Wer M:Wqw
F:Sdf M:Zxc
F:Edc M:Erf
The first man to get a partner is:Dfg

F:Asd M:Qwe
F:Wer M:Wqw
F:Sdf M:Zxc
F:Edc M:Erf
F:Rty M:Dfg
The partner matches exactly.

code:

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXSIZE=20;
typedef struct
{
    char *base;
    char *top;
    int l;
} SqStack;
void InitStack(SqStack &s)
{
    s.base=new char[MAXSIZE];
    if(!s.base)
        exit(1);
    s.top=s.base;
    s.l=MAXSIZE;
    return;
}
void Push(SqStack &s,char ch)
{
    if(s.top-s.base==s.l)
        return;
    *s.top++=ch;
    return;
}
typedef struct
{
    char *base;
    int front;
    int rear;
} SqQueue;
void InitQueue(SqQueue &q)
{
    q.base=new char[MAXSIZE];
    if(!q.base)
        return;
    q.front=q.rear=0;
    return;
}
int EnQueue(SqQueue &q,char ch)
{
    if((q.rear+1)%MAXSIZE==q.front)
    {
        cout<<"货满,请先上货"<<endl;
        return 0;
    }
    q.base[q.rear]=ch;
    q.rear=(q.rear+1)%MAXSIZE;
    return 1;
}
void Purchase(SqQueue &q)
{
    cout<<"进货:"<<endl;
    cout<<"一个字符代表一个商品,以换行结束"<<endl;
    char ch[MAXSIZE];
    scanf("%s",ch);
    int len=strlen(ch),i;
    cout<<"进货为:";
    for(i=0; i<len; i++)
    {
        if(EnQueue(q,ch[i])==0)
            break;
        cout<<ch[i]<<" ";
    }
    cout<<endl;
    return;
}
void Loading(SqStack &s,SqQueue &q)
{
    cout<<"上货:"<<endl;
    if(q.front==q.rear)
    {
        cout<<"没货,请先进货"<<endl;
        return;
    }
    else
    {
        if(s.top-s.base==s.l)
        {
            cout<<"货架满,请先出货再上货"<<endl;
            return;
        }
        else
        {
            SqStack s1;
            InitStack(s1);
            while(s.top!=s.base)
            {
                Push(s1,*(--s.top));
            }
            cout<<"上货物品为:";
            while(q.rear!=q.front)
            {
                if(s1.top-s1.base==s1.l)
                {
                    cout<<endl<<"货架已满"<<endl;
                    break;
                }
                cout<<q.base[q.front];
                Push(s1,q.base[q.front]);
                q.front=(q.front+1)%MAXSIZE;
            }
            if(q.rear==q.front)
            {
                cout<<endl<<"无货,货架未满"<<endl;
            }
            while(s1.top!=s1.base)
            {
                Push(s,*--s1.top);
            }
        }
    }
}
void Shipment(SqStack &s)
{
    cout<<"出货:"<<endl;
    cout<<"输入出货数目:"<<endl;
    int n,sum=0;
    cin>>n;
    cout<<"出货物品为:";
    while(sum!=n)
    {
        sum++;
        if(s.top==s.base)
        {
            cout<<"货架已空!!"<<endl;
            return;
        }
        else
        {
            cout<<*--s.top<<" ";
        }
    }
    if(sum==n)
        cout<<endl<<"出货完成!!"<<endl;
}
int main()
{
    int b;
    SqQueue q;
    SqStack s;
    InitQueue(q);
    InitStack(s);
    cout<<"1进货"<<endl;
    cout<<"2上货"<<endl;
    cout<<"3出货"<<endl;
    cout<<"其他结束"<<endl;
    cin>>b;
    while(b==1||b==2||b==3)
    {
        switch(b)
        {
        case 1:
            Purchase(q);
            break;
        case 2:
            Loading(s,q);
            break;
        case 3:
            Shipment(s);
            break;
        }
        cout<<"1进货"<<endl;
        cout<<"2上货"<<endl;
        cout<<"3出货"<<endl;
        cout<<"其他结束"<<endl;
        cin>>b;
    }
    return 0;
}

题目四:Rails

[Description]
There is a famous railway station in PopPush City. Country there is incredibly hilly. The station was built in last century. Unfortunately, funds were extremely limited that time. It was possible to establish only a surface track. Moreover, it turned out that the station could be only a dead-end one (see picture) and due to lack of available space it could have only one track.

The local tradition is that every train arriving from the direction A continues in the direction B with coaches reorganized in some way. Assume that the train arriving from the direction A has N <= 1000 coaches numbered in increasing order 1, 2, …, N. The chief for train reorganizations must know whether it is possible to marshal coaches continuing in the direction B so that their order will be a1, a2, …, aN. Help him and write a program that decides whether it is possible to get the required order of coaches. You can assume that single coaches can be disconnected from the train before they enter the station and that they can move themselves until they are on the track in the direction B. You can also suppose that at any time there can be located as many coaches as necessary in the station. But once a coach has entered the station it cannot return to the track in the direction A and also once it has left the station in the direction B it cannot return back to the station.
[Input]
The input consists of blocks of lines. Each block except the last describes one train and possibly more requirements for its reorganization. In the first line of the block there is the integer N described above. In each of the next lines of the block there is a permutation of 1, 2, …, N. The last line of the block contains just 0.

The last block consists of just one line containing 0.
[Output]
The output contains the lines corresponding to the lines with permutations in the input. A line of the output contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input. Otherwise it contains No. In addition, there is one empty line after the lines corresponding to one block of the input. There is no line in the output corresponding to the last ``null’’ block of the input.
[Sample Input]

5
1 2 3 4 5
5 4 1 2 3
0
6
6 5 4 3 2 1
0
0

[Sample Output]

Yes
No

Yes

code:

#include <iostream>
#include <cstdlib>
using namespace std;
const int MAXSIZE=1100;
typedef struct
{
    char *base;
    char *top;
    int l;
} SqStack;
void InitStack(SqStack &s)
{
    s.base=new char[MAXSIZE];
    if(!s.base)
        exit(1);
    s.top=s.base;
    s.l=MAXSIZE;
    return;
}
void Push(SqStack &s,int ch)
{
    if(s.top-s.base==s.l)
        return;
    *s.top++=ch;
    return;
}
typedef struct
{
    int *base;
    int front;
    int rear;
} SqQueue;
void InitQueue(SqQueue &q)
{
    q.base=new int[MAXSIZE];
    if(!q.base)
        exit(1);
    q.front=q.rear=0;
    return;
}
void EnQueue(SqQueue &q,int ch)
{
    if((q.rear+1)%MAXSIZE==q.front)
        exit(1);
    q.base[q.rear]=ch;
    q.rear=(q.rear+1)%MAXSIZE;
    return;
}
int main()
{
    int n;
    while(cin>>n)
    {
        if(n==0)
            break;
        while(true)
        {
            SqQueue q;
            InitQueue(q);
            int t,i;
            for(i=0;i<n;i++)
            {
                cin>>t;
                if(t==0)
                    break;
                EnQueue(q,t);
            }
            if(i<n)
                break;
            SqStack s;
            InitStack(s);
            t=1;
            while(q.front!=q.rear)
            {
                if(s.base==s.top)
                {
                    Push(s,t);
                    t++;
                }
                else if(*(s.top-1)!=q.base[q.front])
                {
                    Push(s,t);
                    t++;
                }
                else if(*(s.top-1)==q.base[q.front])
                {
                    s.top--;
                    q.front=(q.front+1)%MAXSIZE;
                }
                if(t==n+2)
                    break;
            }
            if(q.front!=q.rear||t==n+2)
                cout<<"No"<<endl;
            else
                cout<<"Yes"<<endl;
        }

    }
    return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

为君倾此杯

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

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

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

打赏作者

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

抵扣说明:

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

余额充值