`

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

    cpp-算法精粹

    Implement Stack using Queues 队列 Implement Queue using Stacks 二叉树 二叉树的遍历 Binary Tree Preorder Traversal Binary Tree Inorder Traversal Binary Tree Postorder Traversal Binary Tree Level Order ...

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

    Microservices.Flexible.Software.Architecture

    architectures, and scenarios from a technology-neutral standpoint, and demonstrates how to implement them with today’s leading technologies such as Docker, Java, Spring Boot, the Netflix stack, and ...

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

    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