6-18 Two Stacks In One Array(20 point(s))

【信息科学与工程学】【数据科学】数据科学领域 第六篇 算法设计 07 堆、栈、表、矩阵、集合、队列、数组、字符串、树、图、动态规划、结构体、元组的算法01 当前段必须包含所有字符的最后出现位置。1. 字符串分割成最多片段2. 基因序列功能域划分3. 文本段落划分4. 音频声道分离5. 图像色彩分区6. 社交网络社群划分7. 蛋白质结构域8. 音乐乐章分段9. 代码模块划分10. 章节划分特征:一次扫描,贪心确定边界。 阅读详情
6-18 Two Stacks In One Array(20 point(s))

Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used.

Format of functions:

Stack CreateStack( int MaxElements );
int IsEmpty( Stack S, int Stacknum );
int IsFull( Stack S );
int Push( ElementType X, Stack S, int Stacknum );
ElementType Top_Pop( Stack S, int Stacknum );

where int Stacknum is the index of a stack which is either 1 or 2;int MaxElements is the size of the stack array; and Stack is defined as the following:

typedef struct StackRecord *Stack;
struct StackRecord  {
    int Capacity;       /* maximum size of the stack array */
    int Top1;           /* top pointer for Stack 1 */
    int Top2;           /* top pointer for Stack 2 */
    ElementType *Array; /* space for the two stacks */
}

Note: Push is supposed to return 1 if the operation can be done successfully, or 0 if fails. If the stack is empty,Top_Pop must return ERROR which is defined by the judge program.

Sample program of judge:

#include <stdio.h>
#include <stdlib.h>
#define ERROR 1e8
typedef int ElementType;
typedef enum { push, pop, end } Operation;

typedef struct StackRecord *Stack;
struct StackRecord  {
    int Capacity;       /* maximum size of the stack array */
    int Top1;           /* top pointer for Stack 1 */
    int Top2;           /* top pointer for Stack 2 */
    ElementType *Array; /* space for the two stacks */
};

Stack CreateStack( int MaxElements );
int IsEmpty( Stack S, int Stacknum );
int IsFull( Stack S );
int Push( ElementType X, Stack S, int Stacknum );
ElementType Top_Pop( Stack S, int Stacknum );

Operation GetOp();  /* details omitted */
void PrintStack( Stack S, int Stacknum ); /* details omitted */

int main()
{
    int N, Sn, X;
    Stack S;
    int done = 0;

    scanf("%d", &N);
    S = CreateStack(N);
    while ( !done ) {
        switch( GetOp() ) {
        case push: 
            scanf("%d %d", &Sn, &X);
            if (!Push(X, S, Sn)) printf("Stack %d is Full!\n", Sn);
            break;
        case pop:
            scanf("%d", &Sn);
            X = Top_Pop(S, Sn);
            if ( X==ERROR ) printf("Stack %d is Empty!\n", Sn);
            break;
        case end:
            PrintStack(S, 1);
            PrintStack(S, 2);
            done = 1;
            break;
        }
    }
    return 0;
}

/* Your function will be put here */

Sample Input:

5
Push 1 1
Pop 2
Push 2 11
Push 1 2
Push 2 12
Pop 1
Push 2 13
Push 2 14
Push 1 3
Pop 2
End

Sample Output:

Stack 2 is Empty!
Stack 1 is Full!
Pop from Stack 1: 1
Pop from Stack 2: 13 12 11

code:
Stack CreateStack( int MaxElements ){
	Stack s = (Stack)malloc(sizeof(struct StackRecord));//申请一个栈的结构体空间 
	s->Array = (ElementType)malloc(MaxElements);//申请数组的大小空间 
	s->Top1 = -1;//栈1从头开始 
	s->Top2 = MaxElements;//栈2从尾开始 
	s->Capacity = MaxElements;//容量为数组大小 
	return s;//返回 
}
int IsEmpty( Stack S, int Stacknum ){
	if(Stacknum==1){//栈1 
		if(S->Top1==-1)return 1;//如果top1是-1为空 
		else return 0;
	}
	else{//栈2 
		if(S->Top2==S->Capacity)return 1;//如果top2是最大值为空 
		else return 0;
	}
}
int IsFull( Stack S ){
	if(S->Top1+1==S->Top2)return 1;//如果两个顶相邻,则满
	else return 0; 
} 
int Push( ElementType X, Stack S, int Stacknum ){
	if(Stacknum==1){//push栈1 
		if(IsFull(S))return 0;//如果满了返回0 
		else{//否则push进去 
			S->Array[++S->Top1] = X;
			return 1;
		}
	}
	else{//push栈2 
		if(IsFull(S))return 0;//栈满返回0 
		else{//否则push 
			S->Array[--S->Top2] = X;
			return 1;
		}
	}
}
ElementType Top_Pop( Stack S, int Stacknum ){
	if(Stacknum==1){
		if(IsEmpty(S,1))return ERROR;//为空返回error 
		else {
			int t = S->Array[S->Top1];//否则返回栈顶值 
			S->Top1--;//弹出 
			return t;
		}
	}
	else{
		if(IsEmpty(S,2))return ERROR;
		else {
			int t = S->Array[S->Top2];//同理 
			S->Top2++;;
			return t;
		}
	}
}

 
数据结构 双向栈道 数组实现 C语言 Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used. 的代码 立即下载

相关推荐

数据结构基础 浙江大学 作业答案

数据结构基础 浙江大学 作业答案 pdf

2-5 Two Stacks In One Array(一个数组中的两个堆栈)

编写例程以仅使用一个数组实现两个堆栈。除非使用数组中的每个位置(也就是只有数组满的时候),否则堆栈不应声明溢出。

JYHZZ的博客 804

【电商风控】电商风控标注是什么

本篇文章给大家谈谈。

866

Two Stacks In One Array

Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used. Format of functions: Stack CreateStack( int Ma

dxz935的博客 3359

【数据集】基于ubESTARFM法的100m 地温LST数据集(澳大利亚)

【数据集】基于ubESTARFM法的100m 地温LST数据集(澳大利亚)

WW、forever的博客 264

【15】Two Stacks In One Array

2-5 Two Stacks In One Array <两栈共享存储空间(双端堆栈)> Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used. > 编写只使用一个数组实现两个堆栈的例程。除非数组中的每一个数组都应该使用溢出。

WR的博客 548

Coze实战:解锁多Bot协同与条件分支工作流的奥秘

本文详细介绍了在字节跳动Coze平台实现多Bot协同与条件分支工作流的方法。首先阐述了Coze平台的功能优势,包括丰富的插件、知识库和工作流功能。随后分步骤讲解了多Bot协同的实现:从准备工作、创建Bot到协同配置,并提供了电商客服的实例演示。在条件分支工作流部分,详细说明了条件节点的作用类型、条件设定方法以及具体配置操作,强调测试优化的重要性。最后通过电商平台综合案例,展示了多Bot协同与条件分支的实际应用效果,并针对常见问题提供解决方案。文章为开发者在Coze平台上构建智能应用提供了实用指导,有助于提升

远程部署调试 运行安装 项目调试 二次开发 项目技术新 持续迭代 部分源码免费分享 449

6-1 Two Stacks In One Array-PTA双指指针stack表操作实现

6-1 Two Stacks In One Array(20 分) Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used. Format of fun

NITjianbagengmu的博客 1648

Android16 动态隐藏systemUI右侧音量条

本文介绍了如何屏蔽SystemUI系统音量条并显示自定义音量控制条的实现方法。主要通过两个步骤:1)在编译脚本中定义控制属性persist.rp.show.volume.dialog=off来隐藏系统音量条;2)修改SystemUI源码中的VolumeDialogImpl.java文件,在showH方法中根据属性值判断是否显示系统音量条。当属性值为off时直接return,从而屏蔽系统音量条的显示,实现自定义音量控制条的替代显示。该方法通过系统属性控制显示状态,具有灵活可控的特点。

tzj1058756809的博客 778

MATLAB 与 STK 互联生成Walker并导出轨道数据(CSV导出)

本文介绍了使用 MATLAB 控制 STK 完成以下自动化流程:打开并连接 STK 场景;自动创建 Delta 型 Walker 星座;添加矩形传感器并配置视场;获取轨道六根数与视场角信息;自动保存至 CSV 文件,供后续使用。

weixin_52370774的博客 737

2-5 Two Stacks In One Array

【代码】2-5 Two Stacks In One Array。

听取WA声一片 175

xilinx原语及bank简介

图8是HP bank与HR bank的IO模块,其中PAD是FPGA的金属管脚,IOB是输入、输出引脚的控制模块。此时使用原语就可以只调用需要的组件,不需要去考虑其余组件参数设置,就方便很多,借助此次机会就对IDDR、ODDR、IDELAYE2、ODELAYE2、IDELAYCRTL、ISERDESE、OSERDESE等几个原语做了简要了解,并对原语的各个模式进行了仿真,本文最初是介绍IDDR的,但在介绍IDDR这些原语之前,还需要了解一下7系列FPGA bank的分类和区别,所以就有了本文。

qq_43416206的博客 411

PTA-Data Structures and Algorithms (English)

PTA题目集题解

weixin_74229477的博客 952

手把手教你ensp仿真搭建校园网(含防火墙及无线网络)系列(三)校园无线网络AC+AP配置

本系列博客将带你从零开始,使用 华为 eNSP(Enterprise Network Simulation Platform) 仿真搭建一个完整的校园网络,涵盖 核心层、汇聚层、接入层、防火墙安全策略、无线AC+AP组网 等实战场景。无论你是网络初学者还是希望深化实战经验的工程师,都能通过本教程掌握企业级网络的规划、配置与排错技巧。

echo的博客 1356

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

数据结构:Two Stacks In One Array

Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used. Format of functions: Stack CreateStack( int MaxE...

weixin_44094072的博客 574

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

6-2 Two Stacks In One Array (20 分)(C source code)

原题 Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used. Format of functions: Stack CreateStack( int MaxElements ); int IsEmpty( Stack S, int Stacknum );

fengyaxinzhi的博客 332

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

6-18 Two Stacks In One Array(20 分)

Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used. Format of functions: Stack CreateStack( in...

weixin_30580943的博客 336

6-2 Two Stacks In One Array(20 分)

6-2 Two Stacks In One Array(20 分)Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used.Format of functi...

友人A 1830

pta Two Stacks In One Array(简单版双向数组)

Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used. Format of functions: Stack Cr...

weixin_30411997的博客 427

Two stack in one array

。。。

LSH的博客 1662

2-5 Two Stacks In One Array (20 分)

2-5Two Stacks In One Array(20分) Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used. Format of f...

韩旭051的博客 2584

Data Structures and Algorithms (English) - 6-2 Two Stacks In One Array(20 分)

题目链接:点击打开链接 题目大意:略。 解题思路:略。 AC 代码 Stack CreateStack(int MaxElements) { Stack p=(Stack)malloc(sizeof(struct StackRecord)); p-&gt;Array=(int*)malloc(MaxElements*sizeof(int)); p...

874

6-2 Two Stacks In One Array

6-2 Two Stacks In One Array (20 分) Write routines to implement two stacks using only one array. Your stack routines should not declare an overflow unless every slot in the array is used. Format of f...

lannister_awalys_pay的博客 581
上一篇: 6-17 Shortest Path [2](25 point(s))(dijkstra)
下一篇: 6-19 Count Connected Components(20 point(s))
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值