`

Implement a Stack

 
阅读更多

Implement Stack in Java.

public class MyStack<E> {
	private static class Node<E> {
		E value;
		Node<E> next;
		public Node(E v, Node<E> n){
			value = v; next = n;
		}
	}
	
	private Node<E> head;
	private int size = 0;
	
	public void push(E e) {
		head = new Node<E>(e, head);
		size++;
	}
	
	public E pop() throws Exception {
		if(size == 0) throw new Exception("Empty Stack!");
		E e = head.value;
		head = head.next;
		size--;
		return e;
	}
	
	public E peek() throws Exception {
		if(size == 0) throw new Exception("Empty Stack!");
		return head.value;
	}
	
	public int size() {
		return size;
	}
}

 

 

分享到:
评论

相关推荐

    225.Implement stack using queues用队列实现栈【LeetCode单题讲解系列】

    225.Implement_stack_using_queues用队列实现栈【LeetCode单题讲解系列】

    2Queue1Stack.rar_Two Queues

    Implement a Stack using Two Queues with C++

    Full-Stack React Projects

    The benefits of using a full JavaScript stack for web development are undeniable, especially when robust and widely adopted technologies such as React, Node, and Express and are available. Combining ...

    Beginning.Elastic.Stack

    This book teaches you how to install, configure and implement the Elastic Stack (Elasticsearch, Logstash and Kibana) – the invaluable tool for anyone deploying a centralized log management solution ...

    leetcodepushfront-implement-stack-using-queues:使用队列实现堆栈

    stack.push(1); stack.push(2); stack.top(); // returns 2 stack.pop(); // returns 2 stack.empty(); // returns false 笔记: 您必须只使用队列的标准操作——这意味着只有向后推、从前面查看/弹出、大小和为空...

    前端大厂最新面试题-implement-queue-using-stack.docx

    前端大厂最新面试题-implement-queue-using-stack.docx

    DevOps.on.the.Microsoft.Stack.1484214471

    DevOps on the Microsoft Stack shows you how to help your organization implement DevOps, covering the tooling they will need and how to make everything work together while following best practices....

    Hands-On Full Stack Web Development with Aurelia pdf

    If you are a web or a full-stack JavaScript developer who have tried their hands on the traditional stacks like LAMP and wish to explore a new stack with modern web technologies then this book is for ...

    leetcode 225 Implement Stack using Queues

    leetcode 225 Implement Stack using Queues leetcode 2020年3月 每日一题打卡 思路: python 细节: 查找list中某元素的位置:list.index(i) 向下取整:int() 代码: class Solution(object): def majorityElement...

    leetcodepushfront-LeetCode_225--Implement-Stack-using-Queues:LeetCode_2

    stack.empty(); // 返回假 笔记: 您必须只使用队列的标准操作——这意味着只有向后推、从前面查看/弹出、大小和为空操作是有效的。 根据您的语言,队列可能不受本机支持。 您可以使用列表或双端队列(双端队列)来...

    DevOps on the Microsoft Stack(Apress,2016)

    DevOps on the Microsoft Stack shows you how to help your organization implement DevOps, covering the tooling they will need and how to make everything work together while following best practices....

    leetcodepushfront-stack_w_2_queues:stack_w_2_queues

    Implement the following operations of a stack using queues: - push(x): Push element x onto stack. - pop(): Removes the element on top of the stack. - top(): Get the top element. - empty(): Return ...

    数据结构 双向栈道 数组实现 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. 的代码

    Building ERP Solutions with Microsoft Dynamics NAV

    Find out how to transmit your device location from a UWP application to NAV in order to implement a distributed solution for managing couriers in a sales company Make the most of Microsoft Azure and ...

    Learning Kibana 5.0

    Then you will be shown how to implement a pure metric analytics architecture and visualize it using Timelion, a very recent and trendy feature of the Elastic stack. You will learn how to correlate ...

    仿真CPU的java写成的软件

    It is a useful tool for instructors who want their students to get hands-on exposure to a variety of architectures and to get a chance to design and implement their own architectures and write ...

    Swift Data Structure and Algorithms [2016]

    Find out about Swift generators and sequences, and see how to use them to implement advanced data structures such as Stack, StackList, Queue, and LinkedList Implement sorting algorithms such as ...

    PHP 7 - Data Structures and Algorithms

    Implement linked lists, double linked lists, stack, queues, and priority queues using PHP ? Work with sorting, searching, and recursive algorithms ? Make use of greedy, dynamic, and pattern matching ...

    Swift.Data.Structure.and.Algorithms

    oriented programming Find out about Swift generators and sequences, and see how to use them to implement advanced data structures such as Stack, StackList, Queue, and LinkedList Implement sorting ...

    Linux Recipes for Oracle DBAs

    the book is written for database administrators who need to get work done and lack the luxury of curling up fireside with a stack of Linux documentation. The book is task–oriented: Look up the task ...

Global site tag (gtag.js) - Google Analytics