数据结构实验报告:栈和队列的基本操作及应用

版权声明:转载请留言 https://blog.csdn.net/qq_40744093 https://blog.csdn.net/qq_40744093/article/details/86706178

                                          Rails(***)

一、实验目的

1、掌握栈和队列的顺序存储结构和链式存储结构,以便在实际中灵活应用。

2、掌握栈和队列的特点,即后进先出和先进先出的原则。

3、掌握栈和队列的基本运算,如:入栈与出栈,入队与出队等运算在顺序存储结构和链式存储结构上的实现。

二、实验题目(已选题目四)

本次实验提供4个题目,每个题目都标有难度系数,*越多难度越大

  1. 题目一:回文判断(*)
  2. 题目二:商品货架管理(**)
  3. 题目三:舞伴问题(**)
  4. 题目四:Rails(***)

三、实验前的准备工作

1、掌握栈的逻辑结构和存储结构。

2、熟练掌握栈的出栈、入栈等操作。

3、掌握队列的逻辑结构和存储结构。

4、熟练掌握队列的出队、入队等操作

四、实验报告要求

1、实验报告要按照实验报告格式规范书写。

2、实验上要写出多批测试数据的运行结果。

3、结合运行结果,对程序进行分析。

扫描二维码关注公众号,回复: 5469923 查看本文章

、实验内容

 

1、题目四介绍与要求

[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

2、源代码及注释

#include<iostream>
using namespace std;
typedef struct Stack{                //声明一个结构体来存放链表节点
    int data;                        //表示火车的编号
    struct Stack *next;              //指向下一个节点的指针
}StackNode,*LinkStack;               //起别名,方便操作
bool empty(LinkStack &S);            //判断链栈是否为空的子函数
void push(LinkStack &S,int e);       //进栈子函数
int top(LinkStack &S);               //获取栈顶元素子函数
void pop(LinkStack &S);              //弹出栈顶元素子函数
int main()                           //主函数
{
    int n;                           //火车总数量n
    while(cout<<"请输入火车总数量:")
    {
        cin>>n;
        if(n==0)                     //火车数量为0时退出
            break;
        while(1)
        {
            LinkStack S=NULL;        //初始化链栈为空
            int train[1111],flag=0;  //新开一个火车数组,新建一个标志flag并初始化为0
            cout<<"若想终止,请输入0并回车,否则请输入"<<n<<"个火车的出站顺序:";
            for(int i=1;i<=n;++i)
            {
                cin>>train[i];
                if(train[1]==0)      //输入的第一个出站编号为0,满足终止条件
                {
                    flag=1;          //将标志位置为1
                    break;           //退出输入火车顺序
                }
            }
            if(flag)
            {
                cout<<endl;          //标志位为1,退出本轮
                break;
            }
            int i=0,j=1;             //i表示将要入栈的火车编号,j表示需要对比的火车编号
            while(i<=n)
            {
                if(empty(S)||top(S)!=train[j])
                {                    //入栈需要满足的条件
                    ++i;             //入栈火车编号加1
                    if(i==n+1)       //n个火车已全部入栈,退出循环
                        break;
                    push(S,i);       //入栈
                }
                else                 //说明此时编号匹配,则进行弹出操作
                {
                    pop(S);          //弹出栈顶元素
                    ++j;             //火车对比编号加1
                }
            }
            if(empty(S))             //栈空说明按照出站顺序全部匹配,输出 YES
                cout<<"最终结果是: YES"<<endl;
            else                     //否则输出 NO
                cout<<"最终结果是: NO"<<endl;
        }
    }
    return 0;
}
bool empty(LinkStack &S)
{
    if(!S)                           //栈空返回true
        return true;
    return false;                    //不空返回false
}
void push(LinkStack &S,int e)
{
    LinkStack p=(LinkStack)malloc(sizeof(StackNode));
                                     //定义一个栈节点并分配空间
    p->data=e;                       //入栈的火车编号
    p->next=S;
    S=p;                             //类似链表头插法入栈
}
int top(LinkStack &S)
{
    if(!empty(S))
        return S->data;              //若当前栈不为空,返回栈顶元素
}
void pop(LinkStack &S)
{
    if(!empty(S))                   //栈不为空
    {
        LinkStack p=S;              //定义临时指针指向S
        S=S->next;                  //栈顶指针向下移动
        free(p);                    //释放临时指针p
    }
}

3、运行结果展示

      

猜你喜欢

转载自blog.csdn.net/qq_40744093/article/details/86706178