【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]
*/

【DataStructure】The description and usage of Stack

时间: 2024-11-18 11:10:34

【DataStructure】The description and usage of Stack的相关文章

【DataStructure】One of queue usage: Simulation System

Statements: This blog was written by me, but most of content  is quoted from book[Data Structure with Java Hubbard] [Description] This simulationillustrates objectoriented programming(OOP). Java objects are instantiated to represent all the interacti

【DataStructure】The description of generic collections

In this blog, generic collections will be talked about  in details.  In the past bacause of shortage of generic argument,  less importance has been attached to the this module. Just now after reading the chapter about this knowledge, I gradually real

【DataStructure】The description of Java Collections Framework

The Java Connections FrameWork is a group of class or method and interfacs in the java.util package. Its main purpose is to provide a unified framework for implementing common data structure. A collections is a object that contains other objects,whic

【DataStructure】Description and usage of queue

[Description] A queue is a collection that implements the first-in-first-out protocal. This means that the only accessiable object in the collection in the first one that was inserted. The most common example of a queue is a waiting line. [Interface]

【DataStructure】Another usage of List: Polynomial

Statements: This blog was written by me, but most of content  is quoted from book[Data Structure with Java Hubbard] [Description] Apolynomialis a mathematical function of the form: p(x) = a0xn+ a1xn–1+a2xn–2+ ???+an–1x + an The greatest exponent, 

【DataStructure】Descriptioin and usage of List

Statements: This blog was written by me, but most of content  is quoted from book[Data Structure with Java Hubbard] [Description] Alistis a collection of elements that are accessible sequentially: the first element, followed by the second element, fo

【DataStructure】Description and Introduction of Tree

[Description] At ree is a nonlinear data structure that models a hierarchical organization. The characteristic eatures are that each element may have several successors (called its "children") and every element except one (called the "root&

【DataStructure】Charming usage of Set in the java

In an attempt to remove duplicate elements from list, I go to the lengths to take advantage of  methods in the java api. After investiagting the document of java api, the result is so satisfying that I speak hightly of wisdom of developer of java lan

【DataStructure】 Classical Question: Josephus Cycle

[Description] This problem is based upon a report by the historian Joseph ben Matthias (Josephus) on the outcome of a suicide pact that he had made between himself and 40 soldiers as they were besieged by superior Roman forces in 67 A.D. Josephus pro