`

Implement Stack in Java

 
阅读更多

 

public class Stack {
	private int[] data;
	private int size = 0;
	private int capacity = 100;
	public Stack() {
		data = new int[capacity];
}
	public boolean push(int val) {
		if(size == capacity) {
			capacity = size * 2;
			int[] arr = new int[capacity];
			System.arraycopy(data, 0, arr, 0, size);
			data = arr;
		}
		data[size++] = val;
		return true;
	}

	public int pop() {
		if(size == 0) throw new Exception(“empty stack”);
		int val = data[size-1];
		size--;
		return val;
	}

	public int size() {
		return size;
	}
}

 

 

分享到:
评论

相关推荐

    Java 9 Data Structures and Algorithms

    Ltd., he implemented a domain-specific programming language to easily implement complex data aggregation computation that would compile to Java bytecode. Currently, he is leading a team developing a ...

    仿真CPU的java写成的软件

    on exposure to a variety of architectures and to get a chance to design and implement their own architectures and write programs in machine language and assembly language for their architectures.

    JSP Simple Examples

    Unlike interface the abstract class may implement some of the methods defined in the class, but in this class at least one method should be abstract. Using Super class Variables With Sub-classed ...

    leetcode题库-Leetcode-Summary:Leetcode刷题总结(Java版)——更新中

      用两个栈实现队列(Implement_queue_with_two_stacks.java)   滑动窗口的最大值(Maximum_value_of_sliding_window.java)   包含min函数的栈(The_stack_containing_the_min_function.java)   队列的...

    java 面试题 总结

    18、heap和stack有什么区别。 栈是一种线形集合,其添加和删除元素的操作应在同一段完成。栈按照后进先出的方式进行处理。 堆是栈的一个组成元素 19、forward 和redirect的区别 forward是服务器请求资源,服务器直接...

    Spring.MVC.Cookbook.1784396419

    If you are an experienced Java developer, with prior experience in web technologies, and want to step up in your career and stay up-to-date or learn more about Spring Web scalability, this book is for...

    基于ssm+mysql的非遗视域下喀什旅游网源码数据库论文.doc

    In this project, we leverage Java technology and implement a tourism website under the perspective of intangible cultural heritage using the SSM (Spring, SpringMVC, MyBatis) framework. This allows ...

    leetcode java

    - 在字符串中查找子串(Implement strStr())则是一个经典的问题,需要实现字符串查找功能。 **数学(Math)** 数学题目主要考察基本的数学知识和逻辑推理能力。 - "反转整数"(Reverse Integer)涉及到了整数的...

    mastering-spring-cloud2018

    integrates with Elastic Stack in order to sent there log messages, and Zipkin to collect traces. Chapter 10, Additional Configuration and Discovery Features, will introduce two popular products used ...

    Erlang and Elixir for Imperative Programmers(Apress,2016)

    Since he could decide the architecture and software stack of the solution, he immediately thought of Erlang and its libraries and started to evaluate this option. It was not long after that he ...

    PaxCompiler v3.1 DateCode 2012/12/17 Full Source

    TPaxCompilerDebugger and TPaxCompilerExplorer components implement debugger (breakpoints, call stack, watches, step over, trace into, run to cursor, pause) and code explorer capabilities. Script-...

    paxCompiler for Delphi XE5 (Win32)

    TPaxCompilerDebugger and TPaxCompilerExplorer components implement debugger (breakpoints, call stack, watches, step over, trace into, run to cursor, pause) and code explorer capabilities. Script-...

    Packt.Mastering.Csharp.and.NET.Programming

    - **Memory Management**: Provides deeper insights into managing memory in C# applications, including stack vs. heap allocations and garbage collection strategies. - **Concurrency and Multithreading*...

    Addison.Wesley.C++.by.Dissection.2002.pdf

    - **Implementing Our Algorithm in C++:** Describes how to implement algorithms using C++ code. - **Software Engineering: Style:** Emphasizes the importance of coding style and best practices. - **...

    算法刷题笔记leetcode/lintcode

    - Stack(栈) - Set(集合) - Map(映射/哈希表) - Graph(图) - **Basics Sorting**(基本排序算法) - Bubble Sort(冒泡排序) - Selection Sort(选择排序) - Insertion Sort(插入排序) - Merge...

    Professional Assembly Language

    as C, C++, or even Java. This book does not spend much time teaching basic programming principles. It assumes that you are already familiar with the basics of computer programming, and are ...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Header Files The #define Guard Header File Dependencies Inline Functions The -inl.h Files Function Parameter Ordering Names and Order of Includes Scoping Namespaces Nested Classes Nonmember, Static ...

Global site tag (gtag.js) - Google Analytics