- Java 数据结构与算法 教程
- Java 数据结构与算法 - 首页
- Java 数据结构与算法 - 概述
- Java 数据结构与算法 - 环境搭建
- Java 数据结构与算法 - 算法
- Java 数据结构与算法 - 数据结构
- Java 数据结构与算法 - 数组
- Java 数据结构与算法 - 链表
- Java 数据结构与算法 - 双向链表
- Java 数据结构与算法 - 循环链表
- Java 数据结构与算法 - 栈
- 数据结构与算法 - 表达式解析
- Java 数据结构与算法 - 队列
- Java 数据结构与算法 - 优先队列
- Java 数据结构与算法 - 树
- Java 数据结构与算法 - 哈希表
- Java 数据结构与算法 - 堆
- Java 数据结构与算法 - 图
- Java 数据结构与算法 - 搜索技术
- Java 数据结构与算法 - 排序技术
- Java 数据结构与算法 - 递归
- Java 数据结构与算法 有用资源
- Java 数据结构与算法 - 快速指南
- Java 数据结构与算法 - 有用资源
- Java 数据结构与算法 - 讨论
Java 数据结构与算法 - 栈
概述
栈是一种数据结构,它只允许在一端进行数据操作。它只允许访问最后插入的数据。栈也被称为后进先出 (LIFO) 数据结构,并且 Push 和 Pop 操作以这样一种方式相关联:只有最后一个被 Push(添加到栈中)的项目才能被 Pop(从栈中移除)。
栈的表示
我们将在本文中使用数组来实现栈。
基本操作
以下是栈的两个主要操作。
Push − 将元素推入栈顶。
Pop − 从栈顶弹出元素。
栈还支持其他一些操作。
Peek − 获取栈顶元素。
isFull − 检查栈是否已满。
isEmpty − 检查栈是否为空。
Push 操作
每当一个元素被推入栈时,栈会将该元素存储在存储区的顶部,并递增顶部索引以备后用。如果存储区已满,则通常会显示错误消息。
// push item on the top of the stack
public void push(int data) {
if(!isFull()){
// increment top by 1 and insert data
intArray[++top] = data;
}else{
System.out.println("Cannot add data. Stack is full.");
}
}
Pop 操作
每当需要从栈中弹出元素时,栈会从存储区的顶部检索该元素,并递减顶部索引以备后用。
// pop item from the top of the stack
public int pop() {
// retrieve data and decrement the top by 1
return intArray[top--];
}
栈的实现
Stack.java
package com.tutorialspoint.datastructure;
public class Stack {
private int size; // size of the stack
private int[] intArray; // stack storage
private int top; // top of the stack
// Constructor
public Stack(int size){
this.size = size;
intArray = new int[size]; //initialize array
top = -1; //stack is initially empty
}
// Operation : Push
// push item on the top of the stack
public void push(int data) {
if(!isFull()){
// increment top by 1 and insert data
intArray[++top] = data;
}else{
System.out.println("Cannot add data. Stack is full.");
}
}
// Operation : Pop
// pop item from the top of the stack
public int pop() {
//retrieve data and decrement the top by 1
return intArray[top--];
}
// Operation : Peek
// view the data at top of the stack
public int peek() {
//retrieve data from the top
return intArray[top];
}
// Operation : isFull
// return true if stack is full
public boolean isFull(){
return (top == size-1);
}
// Operation : isEmpty
// return true if stack is empty
public boolean isEmpty(){
return (top == -1);
}
}
演示程序
StackDemo.java
package com.tutorialspoint.datastructure;
public class StackDemo {
public static void main (String[] args){
// make a new stack
Stack stack = new Stack(10);
// push items on to the stack
stack.push(3);
stack.push(5);
stack.push(9);
stack.push(1);
stack.push(12);
stack.push(15);
System.out.println("Element at top of the stack: " + stack.peek());
System.out.println("Elements: ");
// print stack data
while(!stack.isEmpty()){
int data = stack.pop();
System.out.println(data);
}
System.out.println("Stack full: " + stack.isFull());
System.out.println("Stack empty: " + stack.isEmpty());
}
}
如果我们编译并运行上述程序,则会产生以下结果:
Element at top of the stack: 15 Elements: 15 12 1 9 5 3 Stack full: false Stack empty: true
广告