Stack, Chinese translation for stacks, actually refers to the stack, heap, heap. This is about the stack of data structures, not the heap and stack inside the memory allocation.
Stack is the structure of the advanced data, like your plate one by one pile up, the last one is stacked on the top.
Queue is to buy apples in line, the first one can buy first.
Stack
public class Stack {private int array[]; private int Max; private int top; Public Stack (int max) {this.max = max; Array = new Int[max]; top = 0; public void push (int value) {if (Isfull ()) {System.out.println ("Full,can not insert"); Return } Array[top++]=value; } public int pop () {return array[--top]; public Boolean isEmpty () {if (top = = 0) {return true; } return false; public Boolean isfull () {if (top = = max) {return true; } return false; public void display () {while (!isempty ()) {System.out.println (pop ()); }} public static void Main (string[] args) {stack s = new Stack (5); S.push (1); S.push (3); S.push (5); S.push (5); S.push (5); S.display (); }}
actually still think set top for-1 to calculate a little, remember here i++ and ++i, if I=1, then array[i++]=2, refers to array[1]=2, the next time I use the value of I will change 2, and ++i is the direct use of i=2.
Top points to 0, because each time you push an element plus one, the Top=max is added to the last element. Because of the advanced, then the first out is the last, just for the location of the top-1.
Correct output:
55531
First, the use of the stack-the word reverse order.
Public String Reverse (string in) { string out= ""; for (int i = 0; i < in.length (); i++) { char c = in.charat (i); Push (c); } while (!isempty ()) { out+=pop (); } return out; } public static void Main (string[] args) { Scanner s = new Scanner (system.in); String string = S.nextline (); Stack st = new Stack (string.length ()); System.out.println (St.reverse (String)); }
change the array type of the stack to char.
Read input can also be read with IO.
public static void Main (string[] args) { InputStreamReader is = new InputStreamReader (system.in); BufferedReader B = new BufferedReader (is); String string= ""; try { string = B.readline (); } catch (IOException e) { e.printstacktrace (); } Stack st = new Stack (string.length ()); System.out.println (St.reverse (String)); }
Second, the use of the stack-delimiter matching.
public int charat (char c) {for (int i = 0; i < Array.Length; i++) {if (c = = Array[i]) return i; } return array.length;} public void Match (string in) {string out= ""; for (int i = 0; i < in.length (); i++) {char c = in.charat (i); if (c = = ' {' | | c = = ' (' | | c = = ' [') {push (c); } if (c = = '} ' | | c = = ') ' | | c = = '] ') {char temp = pop (); if (c = = '} ' && temp! = ' {' | | c = = ') ' && temp! = ' (' | | c = = ') ' && temp! = '] ') {S Ystem.out.println ("Can not match in" +i); }}} while (!isempty ()) {char c = pop (); if (c = = ' {') {System.out.println ("insert} to Match" +charat (c)); } if (c = = ' [') {System.out.println ("insert] to match" +charat (c)); } if (c = = ' (') {System.out.println ("insert) to match" +charat (c)); }}}public static void Main (string[] args) { Scanner s = new Scanner (system.in); String string = S.nextline (); Stack st = new Stack (string.length ()); St.match (string);} RESULT:KLSJDF (klj{lkjjsdf{) can not match on 19insert} to match 1insert) to match 0
will ({Press into the stack once, once encountered)}] will be compared with the popup element, if it matches, then match. If not)}], the left sign of the stack pops up and the hint is where the specific right symbol type is missing.
This is a tool that can be implemented using stacks.
Stack of data into the stack and the time complexity of the stack is constant O (1), because it is independent of the number of data, direct press into the popup, the operating time is short, the advantage is here, if the use of real life only use the advanced after the order and only use the data in and out of the comparison, then you can use the stack.
Java Data structures and algorithms (iv)--stack