java:

public class ArrayStack {

    private int[] data;
private int top;
private int size; public ArrayStack(int size) {
this.data = new int[size];
this.size = size;
this.top = -1;
} public boolean isEmpty() {
if (this.top == -1) {
return true;
}
return false;
} public boolean push(int x) {
if (this.top == this.size - 1) {
return false;
} else {
this.top++;
this.data[top] = x;
return true;
}
} public int pop() {
if (isEmpty()) {
return -1;
}
return data[top--];
}
}

c语言:

#include <stdio.h>
#include <stdlib.h>
#define MAX 10
typedef struct Stack
{
int data[MAX];
int top;
}Stack; void initStack(Stack *stack)
{
stack->top=-1;
} int isEmpty(Stack stack)
{
if(stack.top==-1)
{
return 1;
}
return 0;
} int push(Stack *stack,int x)
{
if(stack->top==MAX-1)
{
return 0;
}
else
{
stack->top++;
stack->data[stack->top]=x;
return 1;
}
} int pop(Stack *stack,int *x)
{
if(isEmpty(*stack))
{
return 0;
}
else
{
*x=stack->data[stack->top];
stack->top--;
return 1;
}
} int main()
{
Stack *stack=(Stack*)malloc(sizeof(Stack));
initStack(stack);
push(stack,1);
push(stack,2);
push(stack,3);
push(stack,4);
int x,y;
pop(stack,&x);
pop(stack,&y);
printf("%d,%d\n",x,y);
return 0;
}

原理一样,但是用java写会感觉更舒服,用面向对象的思想比较清楚。

java、C语言实现数组模拟栈的更多相关文章

  1. Java连载69-接受输入、用数组模拟栈

    一.编写一个酒店管理系统 1.直接上代码 package com.bjpowernode.java_learning; ​ public class D69_1_ { //编写一个程序模拟酒店的管理系 ...

  2. 数组模拟栈(C语言)

    用数组模拟栈的实现: #include <stdio.h> #include <stdlib.h> #define STACK_SIZE 100 typedef struct ...

  3. Java数组模拟栈

    一.概述 注意:模拟战还可以用链表 二.代码 public class ArrayStack { @Test public void test() { Stack s = new Stack(5); ...

  4. c语言学习,模拟栈操作

    1.stack.c模拟栈操作函数的实现 #include<stdio.h> #include<stdlib.h> ; static char *stack;//数据栈 ;//栈 ...

  5. Hdu 3887树状数组+模拟栈

    题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java ...

  6. JavaScript数组模拟栈和队列

    *栈和队列:js中没有真正的栈和队列的类型              一切都是用数组对象模拟的 栈:只能从一端进出的数组,另一端封闭       FILO   何时使用:今后只要仅希望数组只能从一端进 ...

  7. LeetCode 155 - 最小栈 - [数组模拟栈]

    题目链接:https://leetcode-cn.com/problems/min-stack/description/ 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的 ...

  8. 数据结构(3):java使用数组模拟堆栈

    堆栈原理: 数组模拟堆栈: //数组模拟栈 class ArrayStack{ //栈顶 private int top = -1; private int maxSize; private int[ ...

  9. java模拟栈的操作

    栈是一种有序列表,可以使用数组的结构来储存栈的数据内容 思路 1. 创建一个栈类StackArray 2. 定义一个top来模拟栈顶,初始化为-1 3. 入栈: 当有数据加入到栈的时候 top++ s ...

随机推荐

  1. oracle mybatis批量插入,无匹配找默认

    批量插入<insert id="insertIndi" parameterType="java.util.HashMap" useGeneratedKey ...

  2. C 标准库 - string.h之strrchr使用

    strrchr Locate last occurrence of character in string, Returns a pointer to the last occurrence of c ...

  3. css3毛玻璃效果白边问题

    注:css3毛玻璃效果应该很多人都知道怎么实现,但是有个问题是图片模糊了之后相当于缩小了,所以颜色深的图片会出现白边,这里说下我参考网上的解决方式吧! 1.毛玻璃实现方法: CSS3 blur滤镜实现 ...

  4. 九度oj 1437 To Fill or Not to Fill 2012年浙江大学计算机及软件工程研究生机试真题

    题目1437:To Fill or Not to Fill 时间限制:1 秒 内存限制:128 兆 特殊判题:否 提交:1488 解决:345 题目描述: With highways availabl ...

  5. Selenium库简介

    Selenium是一个自动化测试工具,利用它可以驱动浏览器执行特定的动作,如点击.下拉等操作,同时还可以获取浏览器当前呈现的页面的源代码,做到可见即可爬.对于一些JavaScript动态渲染的页面来说 ...

  6. PageOffice 使用Dome

    一.前言 PageOffice是一款帮助Web应用系统或Web网站实现用户在线编辑Word.Excel.PowerPoint文档,Word/Excel模板动态填充,Word/Excel在线输入提交,系 ...

  7. python-爬虫之requests模块介绍(登陆github)

    介绍 使用requests可以模拟浏览器的请求,比起之前用到的urllib,requests模块的api更加便捷(本质就是封装了urllib3) 注意 requests库发送请求将网页内容下载下来以后 ...

  8. springboot定时任务,去掉指定日期

    今天用springboot写到一个需求:每周定时发送任务,但是要避开法定节假日. 网上找了些博客看,主要参考了https://www.cnblogs.com/lic309/p/4089633.html ...

  9. 跨平台 GUI可视化 网络调试工具

    mNetAssisthttp://blog.chinaunix.net/uid-21977056-id-4310527.htmlhttps://github.com/busyluo/mNetAssis ...

  10. JavaScript对象中的constructor属性

    constructor属性始终指向创建当前对象的构造函数. 比如下面的例子: // 等价于 var foo = new Array(1, 56, 34, 12); var arr = [1, 56, ...