Java Simulation Stack Structure

Source: Internet
Author: User

Stacks and queues:

It is often used as a tool for programming apes to assist with ideation algorithms. Short lifecycle, created at execution time

Access is restricted. At certain moments, only one data can be read or deleted

is an abstract structure. The internal implementation mechanism. Not visible to users. For example, using arrays, linked lists to implement the stack

Stack:

At the same time, just agree to a data to be interviewed, LIFO

The time complexity for both the stack and the stack is O (1), that is, the number of data items in the stack is not dependent, and the operation is faster

example, using an array as the storage structure of the stack

public class Stacks<t> {private int max;private t[] ary;private int top;//Pointer, pointing to the top element of the stack, the subscript public StackS (int size) {this. max = Size;ary = (t[]) New object[max];top =-1;} into the stack public void push (T data) {if (!isfull ()) ary[++top] = data;} Out of Stack public T pop () {if (IsEmpty ()) {return null;} return ary[top--];} View stack Top Public T peek () {return ary[top];} Whether the stack is empty public boolean isEmpty () {return top = =-1;} Whether the stack is full public boolean isfull () {return top = = max-1;} sizepublic int size () {return top + 1;} public static void Main (string[] args) {stacks<integer> stack = new stacks<integer> (3), for (int i = 0; i < 5; i++) {Stack.push (i); SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());} for (int i = 0; i < 5; i++) {Integer peek = Stack.peek (); System.out.println ("Peek:" + peek); SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());} for (int i = 0; i < 5; i++) {Integer pop = Stack.pop (); System.out.println ("Pop:" + Pop); SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());} SYSTEM.OUT.PRINTLN ("----"); for (int i = 5; I &Gt 0; i--) {Stack.push (i); SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());} for (int i = 5; i > 0; i--) {Integer peek = Stack.peek (); System.out.println ("Peek:" + peek); SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());} for (int i = 5; i > 0; i--) {Integer pop = Stack.pop (); System.out.println ("Pop:" + Pop); SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());}}}

The example above. There is a maxsize rule. Since the array is to be of a specified size, if you want to be unlimited, you can use other structures to do the storage, of course, you can also new an array of length.

bit. Using LinkedList storage to implement stacks

/** * Use LinkedList Storage to implement stacks * @author stone * * @param <T> */public class Stackss<t> {private linkedlist<t> Datas;public stackss () {datas = new linkedlist<t> ();} into the stack public void push (T data) {datas.addlast (data);} Out of Stack public T pop () {return datas.removelast ();} View stack Top Public T peek () {return datas.getlast ();} Whether the stack is empty public boolean isEmpty () {return datas.isempty ();} sizepublic int size () {return datas.size ();} public static void Main (string[] args) {stacks<integer> stack = new stacks<integer> (3), for (int i = 0; i < 5; i++) {Stack.push (i); SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());} for (int i = 0; i < 5; i++) {Integer peek = Stack.peek (); System.out.println ("Peek:" + peek); SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());} for (int i = 0; i < 5; i++) {Integer pop = Stack.pop (); System.out.println ("Pop:" + Pop); SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());} SYSTEM.OUT.PRINTLN ("----"); for (int i = 5; i > 0; i--) {Stack.push (i); SYSTEM.OUT.PRINTLN ("Size:" + stAck.size ());} for (int i = 5; i > 0; i--) {Integer peek = Stack.peek (); System.out.println ("Peek:" + peek); SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());} for (int i = 5; i > 0; i--) {Integer pop = Stack.pop (); System.out.println ("Pop:" + Pop); SYSTEM.OUT.PRINTLN ("Size:" + stack.size ());}}}

Examples, reverse the word, use STATCK structure

public class Wordreverse {public static void main (string[] args) {Reverse ("corporation"); static void reverse (String word) {if (word = = null) return; stackss<character> stack = new stackss<character> (); char[] Chararray = Word.tochararray (); int len = chararray.length;for (int i = 0; I <len; i++) {Stack.push (chararray[i]);} StringBuilder sb = new StringBuilder (), while (!stack.isempty ()) {Sb.append (Stack.pop ());} System.out.println ("After reversal:" + sb.tostring ());}}
Print:

After reversal: social type strains



Java Simulation Stack Structure

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.