嘿嘿,标题党了。没有臭弟弟喔,只有笨姐姐~

刚刚学数据结构,学到堆栈,真的很不熟练,希望通过这种方式让自己能够好好掌握叭。

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 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

结尾无空行

哎,英文题,居然是英文题(这对我来说就是遭天谴的程度惹),还是我女神陈越奶奶出的,呜呜呜,我一定会好好学哒!!!

思路

第一,这题其实就是让我们写五个和堆栈操作有关的函数,初始化,pop,push,空?,满?;

第二,千万不要因为是英文题就先产生恐惧心理,仔细看清关于函数的一些具体要求,返回值啊输出啥什么的;

第三,谨记,细节决定成败!

函数代码

Stack CreateStack(int MaxElements) { //初始化堆栈Stack S;S = (Stack) malloc(sizeof(struct StackRecord));S->Array=(int*)malloc(MaxElements*sizeof(int));//数组也是需要分配空间的,且不止一个哦!!这应该是我在前面学习中还没有遇到过的S->Capacity = MaxElements;S->Top1 = -1;//初始化标记为空的位置,也方便了等下判断为空S->Top2 = MaxElements;return S;
}int IsEmpty(Stack S, int Stacknum) {if (Stacknum == 1) return (S->Top1 == -1); //这种写法真的好简洁好妙好喜欢啊,我也要学if (Stacknum == 2) return (S->Top2 == S->Capacity);//但注意思路不要断,分支这里很明确
}int IsFull(Stack S) {return ((S->Top2 - S->Top1) == 1);//这种写法也就是为真直接return 1,假直接就return 0;
}int Push(ElementType X, Stack S, int Stacknum) {if ((IsFull(S))) return 0;else {if (Stacknum == 1) {S->Array[(++S->Top1)] = X;//先加再用} else if (Stacknum == 2) {S->Array[(--S->Top2)] = X;//先减再用}return 1;}
}ElementType Top_Pop(Stack S, int Stacknum) {if (IsEmpty(S, Stacknum)) return ERROR;else {if (Stacknum == 1) {return S->Array[S->Top1--];} else if (Stacknum == 2) {return S->Array[S->Top2++];  //两步合成一步就是S->Array[S->Top2--];也就是先赋值然后运算}}
}

和臭弟弟一起学数据结构的第一天(6-1 Two Stacks In One Array (20 分))相关推荐

  1. php学数据结构,PHP 程序员学数据结构与算法之《栈》

    介绍 "要成高手,必练此功". 要成为优秀的程序员,数据结构和算法是必修的内容.而现在的Web程序员使用传统算法和数据结构都比较少,因为很多算法都是包装好的,不用我们去操心具体的实 ...

  2. 从零开始学数据结构和算法(二)线性表的链式存储结构

    链表 链式存储结构 定义 线性表的链式存储结构的特点是用一组任意的存储单元的存储线性表的数据元素,这组存储单元是可以连续的,也可以是不连续的. 种类 结构图 单链表 应用:MessageQueue 插 ...

  3. 小朋友学数据结构(3):二叉树的建立和遍历

    小朋友学数据结构(3):二叉树的建立和遍历 一.基本概念 BinaryTree.png 二叉树:每个结点的子结点个数不大于2的树,叫做二叉树. 根结点:最顶部的那个结点叫做根结点,根结点是所有子结点的 ...

  4. java实验报告合肥工业大学_合肥工业大学数据结构上机实验代码与实验报告(全)github地址...

    C++实现链队类--合肥工业大学数据结构实验5:链式队列 实验5 5.1 实验目的 熟练掌握队列的顺序链式存储结构. 熟练掌握队列的有关算法设计,并在链队列上实现. 根据具体给定的需求,合理设计并实现 ...

  5. 为什么要学数据结构?| 原力计划

    作者 | 牧小农 责编 | 屠敏 出品 | CSDN 博客 前言 在可视化化程序设计的今天,借助于集成开发环境可以很快地生成程序,程序设计不再是计算机专业人员的专利.很多人认为,只要掌握几种开发工具就 ...

  6. 广东工业大学数据结构AnyView参考答案

    广东工业大学数据结构AnyView参考答案 觉得还行,请点赞关注,您的点赞,我的动力! 1.第一章 #include "allinclude.h" //DO NOT edit th ...

  7. 小白学数据结构——零、算法初步(算法分类及最大子数组小试牛刀)

    1. 为啥要学数据结构? 应用:机器学习,数据挖掘,自然语言处理,密码学,计算机图形学 研究:时空复杂度问题 找工作常用:贪心,分治,动态规划,树,图 2.什么是算法? 把大象装进冰箱分为几步?打开冰 ...

  8. 为什么要学数据结构?

    文章目录 一.前言 二.为什么要学数据结构 三.数据结构无处不在 3.1 数据库 3.2 操作系统 3.3 文件压缩 3.4 游戏 四.数据结构类型 一.前言 在可视化化程序设计的今天,借助于集成开发 ...

  9. 【趣学算法】第一章 算法之美(上)

    14天阅读挑战赛 [趣学算法]第一章 算法之美(上) 文章目录 [趣学算法]第一章 算法之美(上) 1.打开算法之门 2.妙不可言---算法复杂性 2.1 算法的引入 [算法的定义] [算法题] [& ...

最新文章

  1. AttributeError: h5py.h5.H5PYConfig‘ has no attribute ‘__reduce_cython__‘
  2. Win64 驱动内核编程-33.枚举与删除对象回调
  3. Qt使用dmctk时的错误
  4. asp: menu 父级选中
  5. 六年级计算机应用计划,2017六年级信息技术下册教学计划
  6. python requests请求终止_Requests 如何中断请求?
  7. adb 不识别解决办法
  8. 管家婆财贸双全 凭证记账 Date exceeds maximum of 19-12-31 报错解决办法
  9. 基于51单片机的步进电机驱动程序
  10. python实现离散沃尔什变换_快速沃尔什变换(示例代码)
  11. Storyboard故事板
  12. 【打卡-Coggle竞赛学习2023年1月】文本相似度匹配
  13. java大数据开发做什么你知道吗?大数据的职业发展规划
  14. 96Boards MIPI CSI Camera Mezzanine V2.1
  15. Doevent()理解
  16. 聚类与分类方法的主要区别是什么?
  17. eclipse如何修改工作空间名称
  18. JDK8 十大新特性详解
  19. echarts 圆环图
  20. Mysql 分隔符拆分字段

热门文章

  1. socat 常见用法
  2. 华为云GPU服务器使用PaddleClas和PaddleServing训练、部署车辆类型分类模型服务
  3. Comet OJ-2019六一欢乐赛 C收服宝可梦吧!
  4. MySQL导入sql文件报错:2006 - MySQL server has gone away(mysql 导入大sql文件)
  5. [机缘参悟-78]:深度思考-职场中注意事项与大忌-员工版
  6. 唯有流过血的手指才能弹出千古绝唱
  7. EIP712 web3.py签名问题注意事项
  8. CentOS查看CUDA版本
  9. mysql重新命名表_MySQL重命名表
  10. Switch-计算器