Article Directory
Introduction
The stack is a linear structure with first-in-last-out.
One, [array implementation] stack order storage
The structure of the stack
public class ArrayStack {
/**
* max size of the stack
*/
private int maxSize;
/**
* a stack object
*/
private Object[] stack;
/**
* init the top of the stack
* from 0 to maxSize
*/
private int top = -1;
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new Object[this.maxSize];
}
------functions------
}
1, into the stack
Ideas:
1. First judge whether the stack is full, and return if it is full.
2. Otherwise, add 1 to the top first, and then assign a value to the stack array.
Look at the picture and talk:
Code:
/**
* push a element
*
* @param object
*/
public void push(Object object) {
if (this.isFull()) {
System.out.println("the stack is full ! can't push !");
return;
}
stack[++top] = object;
}
2. Out of the stack
Ideas:
1. First judge whether the stack is empty, if it is empty, prompt and return.
2. Otherwise, save the node first, and then subtract 1 from top.
Look at the picture and talk:
Code:
/**
* pop a element
*
* @return
*/
public Object pop() {
if (isFull()) {
throw new RuntimeException("the stack is empty!");
}
return stack[top--];
}
3. Judge the stack is full
Code:
/**
* Whether the stack is full
*
* @return
*/
public boolean isFull() {
return top > this.maxSize - 1;
}
4. Determine the stack is empty
Code:
/**
* Whether the stack is empty
*
* @return
*/
public boolean isEmpty() {
return top == -1;
}
5. Get the length of the stack
Ideas:
Because the top pointer always points to the top of the stack, the value of top is the length of the stack, but since this case starts from 0, the length of the stack needs to be increased by 1, namely: top+1.
Look at the picture and talk:
Code:
/**
* get length of stack
* @return
*/
public int getLength(){
return top+1;
}
6. Traverse the stack
Code:
/**
* print a stack
*/
public void list() {
if (isEmpty()) {
System.out.println("the stack is empty!");
}
for (int i = top; i >= 0; i--) {
System.out.print("stack[" + i + "]=");
System.out.println(stack[i]);
}
}
Complete code
package com.qingfeng.stack.array;
@SuppressWarnings("all")
public class ArrayStack {
/**
* max size of the stack
*/
private int maxSize;
/**
* a stack object
*/
private Object[] stack;
/**
* init the top of the stack
* from 0 to maxSize
*/
private int top = -1;
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
stack = new Object[this.maxSize];
}
/**
* Whether the stack is empty
*
* @return
*/
public boolean isEmpty() {
return top == -1;
}
/**
* Whether the stack is full
*
* @return
*/
public boolean isFull() {
return top > this.maxSize - 1;
}
/**
* push a element
*
* @param object
*/
public void push(Object object) {
if (this.isFull()) {
System.out.println("the stack is full ! can't push !");
return;
}
stack[++top] = object;
}
/**
* pop a element
*
* @return
*/
public Object pop() {
if (isFull()) {
throw new RuntimeException("the stack is empty!");
}
return stack[top--];
}
/**
* print a stack
*/
public void list() {
if (isEmpty()) {
System.out.println("the stack is empty!");
}
for (int i = top; i >= 0; i--) {
System.out.print("stack[" + i + "]=");
System.out.println(stack[i]);
}
}
/**
* get length of stack
* @return
*/
public int getLength(){
return top+1;
}
}
test
Code:
public class ArrayStackTest {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(5);
/*------------------------------------------------------------------*/
System.out.println("-----testPush-----");
stack.push(10);
stack.push(20);
stack.push(30);
stack.push(80);
stack.list();
/*------------------------------------------------------------------*/
System.out.println("-----testPop-----");
stack.pop();
stack.list();
/*------------------------------------------------------------------*/
System.out.println("-----testGetLength-----");
System.out.println("the length of stack is: "+stack.getLength());
}
}
operation result:
-----testPush-----
stack[3]=80
stack[2]=30
stack[1]=20
stack[0]=10
-----testPop-----
stack[2]=30
stack[1]=20
stack[0]=10
-----testGetLength-----
the length of stack is: 3
Process finished with exit code 0