In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the knowledge of "how to use array stack in Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Stack introduction
The stack is an ordered first-in-and-out list.
Stack is a special linear table that restricts the insertion and deletion of elements in a linear table to be carried out at the same end of the linear table. one end that allows insertion and deletion is called the top of the stack, and the other end is the fixed end, which is called the bottom of the stack.
The first element in the stack is at the bottom of the stack, and the last element is at the top of the stack.
The first element out of the stack is at the top of the stack, and the last element is at the bottom of the stack.
Analysis.
Using arrays to simulate the implementation of the stack, first of all, considering that the length of the array is fixed, so using the stack must be given a specific length, that is, the maximum length MaxSize. Customize a pointer at the top of the stack and initialize the data to-1, because the index value of the array starts at 0, and in order not to cause conflicts, start with-1.
Empty stack: when top=- 1 equals initialization data, no element exists in the array, the stack is empty.
Stack full: as elements are added, the top pointer of the stack will move backward, but there is a full situation considering that the length of the array is fixed. The judgment condition is that when top=MaxSize- 1, the stack is full. For example, define an array of three sizes, put a data 1 top from-1 to 0, then put a data 2 stop top from 0 to 1, and then put a data 3 top from 1 to 2. At this time, the array is full, and the judgment condition is top = MaxSize, which means the stack is full.
Enter the stack: judge whether the stack is full before entering the stack, otherwise you can't enter the stack. Assign top+1, the element that indexes the array as top, to the added data.
Out-of-stack: determine whether the stack is empty before leaving the stack, otherwise you can't get out of the stack. If it is not empty, first take the element at the top of the stack, that is, the element whose index value is top, and then set the top-1.
Traversing the stack: traversing also needs to determine whether the stack is empty. Traversal data is also traversed from the elements at the top of the stack to the end at the bottom of the stack.
Code implementation
Package cn.mrlij.stack; import java.util.Arrays;import java.util.Scanner; / * use array implementation stack * * @ author dreamer * * / public class ArrayStackDemo {public static void main (String [] args) {/ / Test ArrayStack a = new ArrayStack (5); boolean flag = true;// flag used to determine the end of the loop Scanner sc = new Scanner (System.in); String key = "" / / options for accepting menus: while (flag) {System.out.println ("show: display stack"); System.out.println ("exit: exit the program"); System.out.println ("push: out of the stack"); System.out.println ("pop: out of the stack"); key = sc.nextLine (); switch (key) {case "show": a.show (); break; case "exit": flag = false; System.out.println ("Program is over!") ; break; case "push": System.out.println ("Please enter the data to enter the stack:"); int val = sc.nextInt (); a.push (val); break; case "pop": try {int pop = a.pop (); System.out.println ("out-of-stack value is:" + pop);} catch (Exception e) {/ / TODO: handle exception System.out.println (e.getMessage ());} break; default: break } class ArrayStack {private int MaxSize;// defines the maximum length of the array private int [] arr;// defines the array, and the data is placed at the top of the array private int top =-1 int maxSize / definition stack, and the initialization data is-1 public ArrayStack (int maxSize) {this.MaxSize = maxSize; arr = new int [MaxSize];} / / determine whether the array is empty public boolean isEmpty () {return top = =-1 } / / determine whether the array is full public boolean isFull () {System.out.println ("stack top:" + top+ "maximum length:" + MaxSize); return top = = MaxSize-1;} / / enter stack public void push (int val) {/ / first determine whether the stack is full, and cannot add if (isFull ()) {System.out.println ("stack is full ~"); return;} top++; arr [top] = val } / / destack public int pop () {/ / first determine whether the stack is empty if (isEmpty ()) {throw new RuntimeException ("stack is empty, cannot exit stack!") ;} int val = arr [top]; top--; return val;} public void show () {if (isEmpty ()) {System.out.println ("No data"); return;} for (int I = top; I > = 0; iMel -) {System.out.print (arr [I] + "\ t");} System.out.println () This is the end of the content of "how to use Java to implement the stack with arrays". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.