[Data structure] stack compilation and simple stack Application

Source: Internet
Author: User

First, write two stack exception classes: Full stack and empty Stack:

package com.jim.stack;public class ExceptionStackEmpty extends RuntimeException{    public ExceptionStackEmpty(String err){        System.out.println(err);    }}
package com.jim.stack;public class ExceptionStackFull extends RuntimeException {    public ExceptionStackFull(String err){        System.out.println(err);    }}

Then write the stack interface to define the format and parameters of the five basic stack methods:

package com.jim.stack;public interface Stack {    public int getSize();    public boolean isEmpty();    public Object top() throws ExceptionStackEmpty;    public void push(Object ele)throws ExceptionStackFull;    public Object pop() throws ExceptionStackEmpty;}

Then write the stack implementation array method ):

Package com. jim. stack. impl; import com. jim. stack. exceptionStackEmpty; import com. jim. stack. exceptionStackFull; import com. jim. stack. stack; public class Stack_Array implements Stack {public static final int CAPACITY = 1024; private int capacity; private Object [] obj; private int top =-1; public Stack_Array () {this (CAPACITY);} public Stack_Array (int capacity2) {capacity = capacity2; obj = new Object [capacity] ;}@ Override public int getSize () {return top + 1 ;}@ Override public boolean isEmpty () {return (top <0) ;}@ Override public Object pop () throws ExceptionStackEmpty {Object ele; if (this. isEmpty () throw new ExceptionStackEmpty ("exception: Stack is empty"); ele = obj [this. top]; obj [top] = null; top --; return ele ;}@ Override public void push (Object ele) throws ExceptionStackFull {if (this. getSize () = CAPACITY) throw new ExceptionStackFull ("exception: Full stack"); top ++; obj [top] = ele ;}@ Override public Object top () throws ExceptionStackEmpty {if (this. isEmpty () throw new ExceptionStackEmpty ("exception: Stack null"); return obj [this. top] ;}}

The following are the application of the test Stack:

This includes the reverse order of the array and some methods commonly used by the compiler to check symbol pairing:

Package com. jim. test; import com. jim. stack. impl. stack_Array; public class Test {public static Integer [] reverse (Integer [] a) {Stack_Array s = new Stack_Array (. length); Integer [] B = new Integer [. length]; for (int I = 0; I <. length; I ++) s. push (a [I]); for (int I = 0; I <B. length; I ++) B [I] = (Integer) s. pop (); return B;} public static void matches (String str) {char c [] = str. toCharArray (); Stack_Array s = new Stack_Array (c. length); for (int I = 0; I <c. length; I ++) {if (c [I] = '(') {s. push (c [I]);} else if (c [I] = ') {s. pop () ;}} if (s. isEmpty () {System. out. println ("pairing");} else {System. out. println ("unpaired") ;}/ * public static void main (String [] args) {Integer [] a = new Integer [5]; integer [] B = new Integer [. length]; for (int I = 0; I <. length; I ++) a [I] = I; System. out. println ("before the order of exchange:"); for (int I = 0; I <. length; I ++) System. out. print (a [I] + ""); B = reverse (a); System. out. println (); System. out. println ("after the switching order:"); for (int I = 0; I <. length; I ++) System. out. print (B [I] + "");} */public static void main (String [] args) {String str = "78 + 89*45/(89 + (45-96) + 45"; matches (str );}}


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.