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