入栈出栈代码实现以及相关例题
发布日期:2021-09-29 21:09:52 浏览次数:1 分类:技术文章

本文共 4908 字,大约阅读时间需要 16 分钟。

了解更多栈的知识, 

#include
#include
typedef struct node{ int date; node * next;}SeqStack ;SeqStack * init_SeqStack(SeqStack * top){ top=NULL; return top;}int is_Empty(SeqStack * top){ if(top==NULL)return 1; else return 0;}SeqStack * push_Stack(SeqStack * top){ SeqStack * New; New=(SeqStack *)malloc(sizeof(SeqStack)); printf("请输入要入栈的元素\n"); scanf("%d",&New->date); New->next=top; top=New; return top;}SeqStack * pop_Stack(SeqStack * top,int &m){ SeqStack * p=NULL; if(!is_Empty(top)){ m=top->date; p=top; top=top->next; free(p); return top; }}SeqStack * top_Stack(SeqStack * top,int &m){ if(!is_Empty(top)){ m= top->date; return top; }}int main(){ int m=0; SeqStack * s=NULL; init_SeqStack(s); s=push_Stack(s); s=push_Stack(s); s=push_Stack(s); s=push_Stack(s); s=top_Stack(s,m); printf("%d\n",m); s=top_Stack(s,m); printf("%d\n",m); s=pop_Stack(s,m); printf("%d\n",m); s=top_Stack(s,m); printf("%d\n",m); if(is_Empty(s)) printf("栈现在是空了"); system("pause"); return 0;}

//顺序栈的初始化,建立,插入,查找,删除。  //

//Author:Wang Yong                 //   

//Date: 2010.8.19                  //

 

 

#include <stdio.h>

#include <stdlib.h>

 

#define  MAX 100                //定义最大栈容量

 

typedef int ElemType;

 

///

 

//定义栈类型

typedef struct

{

    ElemType data[MAX];

    int top;

}SeqStack;

 

///

 

//栈的初始化

 

SeqStack SeqStackInit()

{

    SeqStack s;

    s.top = -1;

    return s;

}

 

///

 

//判断栈空的算法

 

int SeqStackIsEmpty(SeqStack s)

{

    if(s.top == -1)

        return 0;

    else

        return 1;

}

 

///

 

//进栈的算法

 

void SeqStackPush(SeqStack &s,ElemType x)

{

    if(s.top == MAX-1)              //进栈的时候必须判断是否栈满

        printf("stack full\n");

    s.top++;

    s.data[s.top] = x;

}

 

//

 

//出栈的算法

 

ElemType SeqStackPop(SeqStack &s)

{

    if(s.top == -1)             //出栈的时候必须判断是否栈空

        printf("stack empty\n");

    ElemType x;

    x = s.data[s.top];

    s.top--;

    return x;

}

 

//

int main()

{

    SeqStack  stack;

    stack = SeqStackInit();

    printf("请输入进栈的元素:");

    ElemType x;

    while(scanf("%d",&x) != -1)

    {

        SeqStackPush(stack,x); 

    }

    printf("出栈的结果:");

    while(stack.top != -1)

    {

        printf("%d ",SeqStackPop(stack));

    }

    printf("\n");

    return 0;

}

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.

 

\begin{picture}(6774,3429)(0,-10)\put(1789.500,1357.500){\arc{3645.278}{4.7247}......tFigFont{14}{16.8}{\rmdefault}{\mddefault}{\updefault}Station}}}}}\end{picture}

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 \leŸ 1000$ coaches numbered in increasing order $1, 2, \dots, 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 $a_1. a_2, \dots, a_N$. 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.

 

 

The input file 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, \dots, N$ The last line of the block contains just 0.

The last block consists of just one line containing 0.

 

 

The output file contains the lines corresponding to the lines with permutations in the input file. A line of the output file contains Yes if it is possible to marshal the coaches in the order required on the corresponding line of the input file. Otherwise it contains No. In addition, there is one empty line after the lines corresponding to one block of the input file. There is no line in the output file corresponding to the last ``null'' block of the input file.

 

 

 

51 2 3 4 55 4 1 2 3066 5 4 3 2 100

 

 

 

YesNo

Yes

分析

题意:给定一串连续的数,放入一个栈中,看接收的数据能否按照某种顺序输出,eg:1 2 3 4 5---->5 4 3 2 1

两种思路:

  • 考虑栈为空     先把1,2,…n放入栈中,直到等于输入的数a[i],n出栈,以此类推,如果最后栈为空,证明放入栈中的n个数字已经都被清空,则这组数据可以按照某种顺序输出      ***每次输入数据后一定要清空栈,否则不AC
  • 考虑出栈的个数     两种思路差不多,考虑出栈的个数可以看看入栈的1,2,…n出栈的数字有多少

考虑栈为空  

#include
#include
using namespace std;const int MAXN=1010;int n,a[MAXN];int main() { while(~scanf("%d",&n)&&n!=0) {//火车量 stack
s; int flag; while(~scanf("%d",&a[0])&&a[0]!=0) {//车厢序号不能为0 while(!s.empty())s.pop(); for(int i=1; i

考虑出栈的个数  

#include
#include
using namespace std;const int MAXN=1000+10;int n,a[MAXN];int main() { while(~scanf("%d",&n)&&n!=0) {//火车量 stack
s; while(~scanf("%d",&a[0])&&a[0]!=0) {//车厢序号不能为0 for(int i=1; i

 

转载地址:https://blog.csdn.net/hou_shiyu/article/details/81204601 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Set的用法以及相关例题——插入不重复元素
下一篇:结构体代码实现以及相关例题

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年03月14日 20时59分47秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

java转换ab的值,查看新闻/公告--[整理]Java将AB1234形式的16进制字符串转换为10进制数值,考虑字节序的影响.... 2019-04-21
ui php h5,画出自己的UI组件的详情 2019-04-21
linux服务文件编写,linux编写systemd下服务脚本 2019-04-21
hdfs linux 目录是否存在,Linux中判断hdfs文件是否存在 2019-04-21
linux学习需要什么基础,学linux需要什么基础? 2019-04-21
linux vim编辑kconfig 无法wq,Linux-4.9.2内核在mini2440上的移植(三)——编译环境测试... 2019-04-21
高斯勒让德在c语言中的程序,c语言:用递归方法编写程序,求n阶勒让德多项式的值... 2019-04-21
c语言单片机电子时钟,新人求个51单片机的电子时钟汇编语言(C语言的还没学到)... 2019-04-21
c++语言文件流,C++文件流 2019-04-21
android 动态毛玻璃,Android毛玻璃背景效果简单实现代码 2019-04-21
android 按钮提示,的Android按钮工具提示 2019-04-21
iphone通讯录 android,3个方法,教你如何快速而又有效的将联系人从iPhone转移到安卓... 2019-04-21
android horizontalscrollview 滑动事件,ScrollView的滑动监听(以HorizontalScrollView为例) 2019-04-21
win7自定义html为桌面,Win7系统自定义桌面主题的方法 2019-04-21
单系统 台电x80pro_台电x80 pro (ID:E3E6)安装remix OS系统教程整理 2019-04-21
linux存储pdf伟岸_python的reportlab库介绍、制作pdf和作图 2019-04-21
安徽信息技术初中会考上机考试模拟_2020年中小学寒假、考试时间定下了! 2019-04-21
ubuntu 退出anaconda环境_从零开始深度学习第15讲:ubuntu16.04 下深度学习开发环境搭建与配置... 2019-04-21
稳定币usda是哪个发行的_武夷山币装帧款曝光,共4款设计,你喜欢哪款? 2019-04-21
可变车道怎么走不违章_走ETC竟比人工车道贵50%!交警:这3点不知道,吃亏的是自己... 2019-04-21