Arrays, linked lists, trees, and other data structures are suitable for storing data records in database applications. They are often used to record the real-world objects and activity data, and access to the data: insert, delete, and search for specific data items
Stack and queue are mostly used as programmer tools. They are mainly used as auxiliary tools for idea algorithms, rather than full data storage tools. The lifecycle of these data structures is much shorter than that of those database types. They are created only during the execution of program operations. Generally, they execute a special task and are destroyed after the task is completed.
Access to stacks and queues is restricted, that is, only one data item can be read or deleted at a specific time.
Stacks and queues are more abstract structures than arrays and other data structures. They are organized and maintained at a higher layer.
The main mechanism of stack can be implemented through arrays or linked lists. The internal implementation of priority queues can be implemented using arrays or a special tree-heap.
First, understand the concept and instance of stack, and then have a deep understanding of queue and priority queue respectively.
The stack only allows access to one data item: the last inserted data. After this data item is removed, the last and second inserted data items can be accessed. It is a"Back-in-first-out.
The most basic stack operation isOutbound Stack(Pop ),Inbound Stack(Push), there are other extension operations, such as viewing the top elements of the stack, determining whether the stack is empty, whether it is full, and the size of the read stack.
Next we will use arrays to write an encapsulation class for Stack operations.
Public class Stack {private int size; // Stack size private int top; // subscripts of the top element of the Stack private int [] stackArray; // Stack container // constructor public Stack (int size) {stackArray = new int [size]; top =-1; // when initializing the Stack, there is no element in the stack, and the bottom of the stack is set to-1 this. size = size;} // stack entry. At the same time, add the subscript of the top element of the stack to the public void push (int elem) {stackArray [++ top] = elem; // Insert the stack top} // The stack top element is deleted. At the same time, the subscript of the stack top element minus one public int pop () {return stackArray [top --];} // view the top element of the stack, but do not delete the public int peek () {return stackArray [top];} // empty public boolean isEmpty () {return (top =-1);} // public boolean isFull () {return (top = size-1 );}}
In the above example, the possible exceptions are not handled, and the programmers need to ensure the correctness of the program. For example, to ensure that there are elements in the stack before the stack is released, ensure that the stack is not full before stack entry.
Inbound stack operation
Out-of-stack operations
Stack is usually used to parse certain types of text strings. Generally, text strings are code lines written in computer languages, and the Program for parsing them is the compiler.
Next we will use the stack to implement a classic application: delimiter matching. When programming in Eclipse, if the code we write contains an extra "{", the latter is missing a "}", or the brackets are disordered, an error will be reported. Next, we will use the stack to simulate this separator matching.
The delimiter matching program constantly reads a character from the string. if it finds that it is a left separator ({, [, (), it is pushed into the stack. When a right separator is read (),], and}), the top element of the stack is displayed, and whether it matches the right separator. If they do not match, the program reports an error. If no matching separator exists until the end, the program also reports an error.
Let's take a look at the following correct string and the change process in the stack:
A {B (c [d] e) f}
Content in the read character Stack
A blank
{{
B {
({(
C {(
[{([
D {([
] {(
E {(
){
F {
} Blank
The last left separator needs to be matched first, which complies with the "back-to-first-out" rule of the stack.
In this example, the character is to be processed, so you need to modify the previous Stack class. You need to change the array of elements to the char type, change the parameter type of the relevant method to char type, and the rest remain unchanged.
Public class Stack {private int size; // Stack size private int top; // subscripts of the top element of the Stack private char [] stackArray; // Stack container // constructor public Stack (int size) {stackArray = new char [size]; top =-1; // when initializing the Stack, there is no element in the stack, and the bottom of the stack is set to-1 this. size = size;} // stack entry. At the same time, add the subscript of the top element of the stack to the public void push (char elem) {stackArray [++ top] = elem; // Insert the stack top} // The stack top element is deleted. At the same time, the subscript of the stack top element is reduced by one public char pop () {return stackArray [top --];} // view the top element of the stack, but do not delete public char peek () {return stackArray [top];} // empty public boolean isEmpty () {return (top =-1);} // public boolean isFull () {return (top = size-1 );}}
Then write a class to encapsulate the delimiter matching operation:
Public class BrecketChecker {private String input; // stores the String to be checked // constructor, and accepts the public BrecketChecker (String in) {this. input = in;} // check the delimiter matching method public void check () {int strLength = input. length (); Stack stack = new Stack (strLength); for (int I = 0; I <strLength; I ++) {char ch = input. charAt (I); // obtain the switch (ch) {case '{': case '[': case '(': // if it is a left separator, push to stack. push (ch); break; case '}': ca Se ']': case') ': // if it is a right separator, it matches with the top element of the stack if (! Stack. isEmpty () {charchx = stack. pop (); if (ch = '{' & chx! = '}') | (Ch = '(' & chx! = ') | (Ch =' ['& chx! = ']') {System. out. println ("An error occurred while matching! Character: "+ ch +", Subscript: "+ I) ;}} else {System. out. println (" Matching Error! Character: "+ ch +", Subscript: "+ I);} default: break;} if (! Stack. isEmpty () {// if there are other elements in the stack when the matching ends, it indicates that the right separator is missing System. out. println ("brackets are not closed! ");}}}
Test class
Public static void main (String [] args) {System. out. println ("input String to be checked:"); String str = getString (); BrecketChecker checker = newBrecketChecker (str); checker. check ();} public static String getString () {String str = ""; try {InputStreamReader reader = new InputStreamReader (System. in); BufferedReader bReader = newBufferedReader (reader); str = bReader. readLine ();} catch (IOException e) {e. printStackTrace ();} return str ;}