什么是java堆栈
系统中的堆、堆栈、数据结构堆和堆栈不是概念。 系统中的堆、栈是实际的内存物理区域,数据结构中的堆、栈可以说是抽象的数据存储结构。
堆栈:实际上是满足后进先出性质、数据项按顺序排列的数据结构,只能在一端(称为堆栈顶部(top ) )插入和删除数据项。
堆栈-编译器自动取消分配,并存储函数的参数值、局部变量的值等。 其行为就像数据结构中的堆栈一样。
堆栈的优点是访问速度比堆快,仅次于直接位于cpu上的寄存器。 但是缺点是,必须确定存在堆栈内的数据大小和生存期,缺乏灵活性。
代码:
抢断的基本使用
初始化
堆叠=新堆叠
判断是否是空的
stack.empty (
取堆栈顶值(不退出堆栈) ) ) ) ) )。
stack.peek ()。
进入堆栈
sack.push (对象;
离开堆栈
stack.pop (;
实例:
公共类te st01 {
publicstaticvoidmain (字符串[ ] args ) {
堆叠=新堆叠(;
//1.empty ) )堆栈是否为空
system.out.println(stack.empty );
//2.peek ) )堆栈顶部值3 .堆栈推送) )。
stack.push(newinteger(1);
斯塔克. push (b );
系统. out.println (stack.peek ) );
//4.pop ) )出场
stack.pop (;
系统. out.println (stack.peek ) );
}
}
堆栈的主要操作
将语音推送(int data ):data插入堆栈
删除int pop ()并返回最后一个插入堆栈
堆栈辅助操作
int top ) :返回插入到堆栈中的最后一个元素,但不删除
int size () :返回堆栈中存储的元素数
int isempty (:确定堆栈中是否存在元素
int isstackfull (:确定堆栈中的元素是否已满
实现
堆栈抽象数据类型有多种实现方法。 常用方法如下:
基于简单数组的实现方法
基于动态数组的实现方法
基于链表的实现方法
1 )基于简单数组的实现:
公共类堆栈{
私密int size; //堆栈大小
隐私在顶; //堆栈顶部元素的下标
private char [ ]堆栈阵列; //堆栈容器
公共堆栈(intsize ) {
堆栈阵列=new char [ size ];
top=-1; //初始化堆栈时堆栈中没有元素,因此将堆栈的上标设置为-1
this.size=size;
}
//进入堆栈,堆栈顶部下标1
公共语音推送(charitem ) {
堆栈阵列[ top ]=item;
}
//离开堆栈、删除堆栈顶部元素、堆栈顶部元素下标-1
公共int pop
返回堆栈阵列[ top-- ];
}
//查看堆栈顶部元素,不删除
公共char find () {
返回堆栈阵列[ top ];
}
//审判天空
公共布尔输入
返回(top=-1 );
}
//判决已满
公共布尔is全满
返回(top==size-1 );
}
publicstaticvoidmain (字符串[ ] args ) {
堆叠=新堆叠(5;
斯塔克. push (a );
斯塔克. push (b );
斯塔克. push (c );
堆栈. push (d );
char ch=stack.find (;
system.out.println(ch;
}
}
执行结果:
d
2 )基于动态数组现实
现:扩容——给我的感觉就像是在搬家,搬完了东西,还得把钥匙给主人
public class stack {
public int size;//栈的大小
public int top;//栈顶元素的下标
public static char[] stackarray;//栈的容器
public stack(int size){
stackarray = new char[size];
top = -1; //初始化栈的时候由于栈内没有元素,栈顶下标设为-1
this.size = size;
}
//入栈,栈顶的下标+1
public void push(char item){
if(isfull()){
doublestack();
}
stackarray[++top] = item;
}
//模拟数组的扩容
public void doublestack(){
char[] newstackarray = new char[size*2];
for(int i = 0;i
newstackarray[i] = stackarray[i];
}
size = size*2;
stackarray = newstackarray;
}
//出栈,删除栈顶元素,栈顶元素的下标-1
public int pop(){
if(isempty()){
system.out.println("stack is empty");
return 0;
}else{
return stackarray[top--];
}
}
//查看栈顶元素,不删除
public char find(){
return stackarray[top];
}
//判空
public boolean isempty(){
return (top == -1);
}
//判满
public boolean isfull(){
return (top == size - 1);
}
public static void main(string[] args){
stack stack = new stack(5);
stack.push('a');
stack.push('b');
stack.push('c');
stack.push('d');
stack.push('e');
stack.push('f');
stack.push('g');
stack.push('h');//一共8个元素
char ch = stack.find();
system.out.println(ch);
system.out.println(stackarray.length);
}
}
运行结果:
h
10
3)基于链表实现
使用链表实现栈,通过在链表的表头插入元素的方式实现push操作,删除链表的表头结点实现pop操作。表头结点即栈顶结点
import java.util.emptystackexception;
class link{
public char data;
public link next;
public void show(){
system.out.println(data + " ");
}
public link(char data){
this.data = data;
}
}
public class stack2 {
link head;
public int size;//栈的大小
public int top;//栈顶元素的下标
public static char[] stackarray;//栈的容器
public void push(char data){
if(head == null){
head = new link(data);
}else{
link node = new link(data);
node.next = head;
head = node;
}
}
public void pop(){
if(head == null){
throw new emptystackexception();
}else{
char dat = head.data;
head.show();
head = head.next;
}
}
public int top(){
if(head == null){
return 0;
}else{
return head.data;
}
}
public boolean isempty(){
if(head == null) return true;
return false;
}
public static void main(string[] args){
stack2 stack = new stack2();
stack.push('a');
stack.push('b');
stack.push('c');
stack.push('d');
stack.push('e');
stack.push('f');
stack.pop();
}
}
运行结果:
f
到此这篇关于java栈的三种实现方式(完整版)的文章就介绍到这了,更多相关java栈内容请搜索萬仟网以前的文章或继续浏览下面的相关文章希望大家以后多多支持萬仟网!
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!