一、栈的介绍
栈的英文为(stack) 栈是一个先入后出(FILO-First In Last Out)的有序列表。 栈(stack)是限制线性表中元素的插入和删除只能在线性表的同一端进行的一种特殊线性表。允许插入和删除的一端,为变化的一端,称为栈顶(Top),另一端为固定的一端,称为栈底(Bottom)。 根据栈的定义可知,最先放入栈中元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入的元素最先删除,最先放入的元素最后删除
二、应用场景
三、代码实现
public class ArrayStackDemo {
public static void main ( String[ ] args) {
ArrayStack arrayStack = new ArrayStack ( 5 ) ;
arrayStack. show ( ) ;
arrayStack. push ( 1 ) ;
arrayStack. push ( 2 ) ;
arrayStack. push ( 3 ) ;
arrayStack. push ( 4 ) ;
arrayStack. push ( 5 ) ;
arrayStack. push ( 6 ) ;
System. out. println ( "添加值后的栈信息" ) ;
arrayStack. show ( ) ;
int stackPop = arrayStack. pop ( ) ;
System. out. println ( "取出的值为:" + stackPop) ;
int stackPop2 = arrayStack. pop ( ) ;
System. out. println ( "取出的值为:" + stackPop2) ;
System. out. println ( "取出值后的栈信息" ) ;
arrayStack. show ( ) ;
}
}
class ArrayStack {
private int maxSize;
private int [ ] stack;
private int top = - 1 ;
public ArrayStack ( int maxSize) {
this . maxSize = maxSize;
this . stack = new int [ this . maxSize] ;
}
public boolean isEmpty ( ) {
return this . top == - 1 ;
}
public boolean isFull ( ) {
return top == maxSize - 1 ;
}
public void push ( int value) {
if ( isFull ( ) ) {
System. out. println ( "栈已满!" ) ;
return ;
}
top++ ;
stack[ top] = value;
}
public int pop ( ) {
if ( isEmpty ( ) ) {
System. out. println ( "当前栈空" ) ;
throw new RuntimeException ( "当前栈无数据,不可出栈" ) ;
}
int value = stack[ top] ;
top-- ;
return value;
}
public void show ( ) {
if ( isEmpty ( ) ) {
System. out. println ( "当前栈空" ) ;
return ;
}
for ( int i = top; i >= 0 ; i-- ) {
System. out. printf ( "stack[%d]=%d\n" , i, stack[ i] ) ;
}
}
}
public class LinkedListStackDemo {
public static void main ( String[ ] args) {
LinkedListStack linkedListStack = new LinkedListStack ( 5 ) ;
linkedListStack. show ( ) ;
linkedListStack. push ( 1 ) ;
linkedListStack. push ( 2 ) ;
linkedListStack. push ( 3 ) ;
linkedListStack. push ( 4 ) ;
linkedListStack. push ( 5 ) ;
linkedListStack. push ( 6 ) ;
System. out. println ( "添加值后的栈信息" ) ;
linkedListStack. show ( ) ;
int stackPop = linkedListStack. pop ( ) ;
System. out. println ( "取出的值为:" + stackPop) ;
int stackPop2 = linkedListStack. pop ( ) ;
System. out. println ( "取出的值为:" + stackPop2) ;
System. out. println ( "取出值后的栈信息" ) ;
linkedListStack. show ( ) ;
}
}
class LinkedListStack {
private int maxSize;
private Node first = new Node ( - 1 ) ;
private Node top = first;
public LinkedListStack ( int maxSize) {
this . maxSize = maxSize;
}
public int size ( ) {
if ( first. next == null) {
return 0 ;
}
int size = 0 ;
Node temp = first. next;
while ( true ) {
size++ ;
if ( temp == top) {
break ;
}
temp = temp. next;
}
return size;
}
public boolean isEmpty ( ) {
return size ( ) == 0 ;
}
public boolean isFull ( ) {
return size ( ) == maxSize;
}
public void push ( int value) {
if ( isFull ( ) ) {
System. out. println ( "栈已满!" ) ;
return ;
}
Node node = new Node ( value) ;
top. next = node;
top = node;
}
public int pop ( ) {
if ( isEmpty ( ) ) {
System. out. println ( "当前栈空" ) ;
throw new RuntimeException ( "当前栈无数据,不可出栈" ) ;
}
int value = top. number;
Node temp = first;
while ( true ) {
if ( temp. next == top) {
top = temp;
break ;
}
temp = temp. next;
}
return value;
}
public void show ( ) {
if ( isEmpty ( ) ) {
System. out. println ( "当前栈空" ) ;
return ;
}
Node temp;
for ( int i = size ( ) ; i >= 0 ; i-- ) {
temp = first;
for ( int j = 0 ; j < i; j++ ) {
temp = temp. next;
if ( j == i- 1 ) {
System. out. printf ( "stack[%d]=%d\n" , j, temp. number) ;
}
}
}
}
}
class Node {
int number;
Node next;
public Node ( int number) {
this . number = number;
}
@Override
public String toString ( ) {
return "Node{number=" + this . number + "}" ;
}
}