【DataStructure】The description and usage of Stack

A stack is collection that implements the last-in-first-out protocal.This means that the only access object in the collections is the last one thatwas inserted.The fundamental  operations of a stack are: add an element onto the top of stack, access the current elments on the top of the stack and remove the current element on the top of stack.Now we first view the defination in the java framework. 

public class Stack<E> extends Vector<E> {
    /**
     * Creates an empty Stack.
     */
    public Stack() {
    }

but  in the comments of this method: 

 * <p>A more complete and consistent set of LIFO stack operations is
 * provided by the {@link Deque} interface and its implementations, which
 * should be used in preference to this class.  For example:
 * <pre>   {@code
 *   Deque<Integer> stack = new ArrayDeque<Integer>();}</pre>

The example about string stack which is inited by deque, the source is coppied from the book. 

package com.albertshao.ds.stack;

//  Data Structures with Java, Second Edition
//  by John R. Hubbard
//  Copyright 2007 by McGraw-Hill

import java.util.*;

public class TestStringStack {
  public static void main(String[] args) {
    Deque<String> stack = new ArrayDeque<String>();
    stack.push("GB");
    stack.push("DE");
    stack.push("FR");
    stack.push("ES");
    System.out.println(stack);
    //peek:Retrieves, but does not remove
    System.out.println("stack.peek(): " + stack.peek());
    System.out.println("stack.pop(): " + stack.pop());
    System.out.println(stack);
    System.out.println("stack.pop(): " + stack.pop());
    System.out.println(stack);
    System.out.println("stack.push(\"IE\"): ");
    stack.push("IE");
    System.out.println(stack);
  }
}

/*  Output:
[ES, FR, DE, GB]
stack.peek(): ES
stack.pop(): ES
[FR, DE, GB]
stack.pop(): FR
[DE, GB]
stack.push("IE"): 
[IE, DE, GB]
*/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值