stack of data structures

Source: Internet
Author: User

Stack is an ADT data structure, the structure is advanced, the stack inherits the Vector,vector inherited the Abstractlist class, this shows that stack is also a collection. There are two common ways to achieve this:

One is a single-linked list (node has only one next pointer to LinkedList), and the other is an array. The JDK is implemented as an array.

1. The stack is characterized by advanced back-out.

Some common methods in the stack are:

Pop () pops out of the stack (i.e. deletes an element from the top of the stack, returns the element),

Peek () gets the top element of the stack (not removed from the top of the stack),

Push (e) pushes an element to the top of the stack (that is, add an element from the top of the stack),

Empty (judging if the stack is null),

Search (Object o) searches for an element from the stack.

The above is all the methods implemented in the stack.

2. Application of Stacks

How to calculate an expression a+b*c+ (d*e+f) *g

1. To calculate the expression, first know the post-order expression

Usually we use the expression as a middle order expression, that is, the operation symbol in the middle of the operation value, like A+b, a+b*c+ (d*e+f) *g,

But there is another expression called post-order expression. The expression is calculated after the operation symbol is placed on the operation value,

such as: A+b written ab+, a+b*c written abc*+, a+b*c+d+e/f written abc*+def/++

How the ordinal expression is converted to a post-order expression, such as String str = a+b*c+ (d*e+f) *g

1. First there are two stored objects, one is the result value of the post-order expression variable string SF, a stack that stores operational symbols

2. Traverse the string str, get the value of each item, if it is an arithmetic value (ABC), add to SF;

3. Output Results abc*+de*f+g*+

If it is an operation symbol:

1) If it is */(directly added to the stack

2) If it is +-,

If the element at the top of the stack stack is directly added to the stack at +-or null,

If */Then all elements in the stack (if any) are ejected (, then (), and then added to SF. Then press + + into the stack

3) if it is), eject (all previous elements and then add to SF

3. Eject all the elements in the stack and add them to SF

Below is the code

1Privatestatic Set<string> diginal =New Hashset<string> ();//Storing the operation symbols in an expression23PublicStaticvoidMain (string[] args) {4 String str = "a+b*c+ (d*e+f) *g";5 Pattern p = pattern.compile ("[+* ()-/]");6 Matcher m =P.matcher (str);7While(M.find ()) {8 String item =M.group ();9Diginal.add (item);10}11}1213/**14* The expression of the middle sequence is followed by an expression15*16*@paramStr17*@return18*/19PublicStaticString transfer (String str) {20Boolean flag =False//Judging there is no (StringBuffer SF =NewStringBuffer ();Stack<character> Stack =New stack<character>();23for (int i = 0; I < str.length (); i++) {24CHAR item =Str.charat (i);25if (!diginal.contains (item + ""))) {26Sf.append (item);27}Else{28if (item = = ' + ' | | item = = '-')) {29if (!Stack.isempty ()&& (stack.peek () = = ' * ' | | stack.peek () = = '/')) {31while (!Stack.isempty ()) {32if (flag && stack.peek () = = ' (') {33Break;34}35Sf.append (Stack.pop ());36}37Stack.push (item);38}Else{39Stack.push (item);40}41}Elseif (item = = ') ') {42while (Stack.peek ()! = ' (') {43Sf.append (Stack.pop ());44}45Stack.pop ();46}Else{47if (item = = ' (') {Flag =True49 }50  Stack.add (item); 51 }52 53 }54 }55 while (! Stack.isempty ()) {56  Sf.append (Stack.pop ()); 57 }58 return sf.tostring (); 59}             

2. Calculate the value by the post-post expression (give only the string in the form of the result)

1. Initialize two variables, result string SF, stack that holds the value of the operation

2. Traversing the post-order expression string str, each item is item

3. When item is an operand, it is added to the stack, and if it is +-*/, two elements are ejected from the top of the stack stack, then the operation is performed, and then the result value is pressed into the stack.

Below is the code

1/**2* Calculation3*4*@return5*/6PublicStaticString cal (String str) {7 stack<string> Stack =New stack<string>();8 StringBuffer SF =NewStringBuffer ();9for (int i = 0, n = str.length (); I < n; i++) {10CHAR item =Str.charat (i);11if (! ( Item = = ' + ' | | item = = '-' | | item = = ' * ' | | item = = '/')) {Stack.push (item + ""));13} else {14 String b = Stack.pop ();  Stack.pop ();  Sf.append (b). Append (item). append (a); // enclosed in parentheses indicates the result 18 Span style= "color: #000000;" >}19 }20 21 return Stack.pop ();                  

stack of data structures

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.