Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply Stack in java data structure

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "how to apply stack in java data structure". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use stacks in java data structures.

1. Declare a stack interface SStack

Package ch05; public interface SStack {boolean isEmpty (); / / determines whether the stack is empty void push (T x); / / element x enters the stack T pop (); / / leaves the stack and returns the top element T peek (); / / returns the top element of the stack without leaving the stack}

two。 The sequential stack class SeqStack is defined, which includes two private member variables: the object array of the data elements and the subscript of the top element of the stack. The construction method can instantiate the empty sequential stack with the capacity of size, call the member methods in the class to realize the operations such as entering the stack, leaving the stack, and getting the top elements of the stack, and rewrite the toString () method to get the string description of the sequential stack.

Package ch05; public class SeqStack implements SStack {Object [] element; int top; / / constructor, create an empty stack, storage capacity size public SeqStack (int size) {element=new Object [size]; top=-1;} / / determine whether the stack is empty @ Override public boolean isEmpty () {return top==-1 } / / element x stacks @ Override public void push (T x) {if (x==null) {return;} / / if the stack is full, expand the stack capacity if (this.top==element.length-1) {Object [] temp=this.element; element=new Object [temp.length*2]; for (int iTunes 0) ITun0) {str= "("; for (int itemtopteri > = 0poliiMurbe -) {str+= element [I] + ",";} str=str.substring (0meme str.length ()-1); str+= ")";} else {/ / empty stack str= "()";} return str;}}

3. Define the node class Node, including two member variables of the data domain and the address domain, instantiate the node by the construction method, and rewrite the toString () method to get the string description of the node.

Package ch05; public class Node {public T data; public Node next; public Node (T data, Node next) {this.data = data; this.next = next;} public Node () {this (null,null);}}

4. Define the chain stack class LinkedStack:

Package ch05; public class LinkedStack implements SStack {private Node top; public LinkedStack () {top=new Node ();} @ Override public boolean isEmpty () {return top.next==null? True:false;} @ Override public void push (T x) {if (x==null) {return;} / / generate a new node Node q=new Node (xMago null); q.nexttop.next; top.next=q;} @ Override public T pop () {T elem=null If (top.nextlines null) {elem=top.next.data; top.next=top.next.next;} return elem;} @ Override public T peek () {T elem=null; if (top.nextlines null) {elem=top.next.data;} return elem } / / returns a description string of all elements in the sequential stack in the form of "(,)", overriding the toString () method public String toString () {String str= ""; Node paired top.nextof the Object class; if (paired null) {str= "("; while (paired null) {str+=p.data+ ","; p=p.next) } str=str.substring (0 str= str.length ()-1); str+= ")";} else {str= "()";} return str;}}

5. Parenthesis matching

Package ch07; import java.util.Scanner; public class Bracket {/ / parentheses match public static String isMatched (String infix) {SeqStack stack = new SeqStack (infix.length ()); for (int I = 0; I < infix.length (); iTunes +) {char ch = infix.charAt (I) Switch (ch) {case'(': stack.push (ch); break Case')': if (stack.isEmpty () | |! stack.pop () .equals ('(')) {return "expect (") } return stack.isEmpty ()? "": "expect)" } / / Test parenthesis matching algorithm public static void main (String [] args) {/ / parenthesis matching Scanner r = new Scanner (System.in); System.out.print ("enter parenthesis expression:"); String infix = r.nextLine () System.out.println (isMatched (infix));}}

6. Expression evaluation (suffix expression):

Package ch05; import java.util.Scanner; public class ExpressionPoland {/ / parentheses match public static String isMatched (String infix) {SeqStack stack = new SeqStack (infix.length ()); for (int I = 0; I < infix.length (); iTunes +) {char ch = infix.charAt (I) Switch (ch) {case'(': stack.push (ch); break; case')': if (stack.isEmpty () | |! stack.pop () .equals ('()) {return "expect (") } return stack.isEmpty ()? "": "expect";} / / convert the infix expression to the suffix expression public static StringBuffer toPostfix (String infix) {SeqStack stack=new SeqStack (infix.length ()); StringBuffer postfix=new StringBuffer (infix.length () * 2); int iFix0 System.out.println ("\ n suffix expression procedure:"); System.out.println ("character" + "\ tstack\ t\ tpostfix"); while (I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report