Java Simulation Stack Structure

Source: Internet
Author: User

Stacks and queues:

It is often used as a programmer's tool to assist with the ideation algorithm, which is short in life and is created at runtime.

Restricted access, only one data can be read or deleted at certain moments

is an abstract structure, internal implementation mechanism, not visible to users, such as arrays, linked lists to implement the stack

Stack:

At the same time, only one data is allowed to be accessed, LIFO

For both the stack and the stack time complexity is O (1), that is, not dependent on the number of data items in the stack, the operation is relatively fast

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 ());}}}

In the above example, there is a maxsize rule, because the array is to specify the size, if you want to unlimited, you can use other structures to do the storage, of course, you can also new one of the length of the array.

example, using LinkedList storage to implement the stack

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