「java栈用什么实现的」java怎么用栈
今天给各位分享java栈用什么实现的的知识,其中也会对java怎么用栈进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
java中栈是如何实现的?
这是java.util包下的Stack类,你可以看一下它是如何实现的,至于用法,无非就是push,pop,peek等操作等 /* * @(#)Stack.java 1.30 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.util; /** * The Stack class represents a last-in-first-out * (LIFO) stack of objects. It extends class Vector with five * operations that allow a vector to be treated as a stack. The usual * push and pop operations are provided, as well as a * method to peek at the top item on the stack, a method to test * for whether the stack is empty, and a method to search * the stack for an item and discover how far it is from the top. * p * When a stack is first created, it contains no items. * * pA more complete and consistent set of LIFO stack operations is * provided by the {@link Deque} interface and its implementations, which * should be used in preference to this class. For example: * {@code * Deque stack = new ArrayDeque();} * * @author Jonathan Payne * @version 1.30, 11/17/05 * @since JDK1.0 */ public class Stack extends Vector { /** * Creates an empty Stack. */ public Stack() { } /** * Pushes an item onto the top of this stack. This has exactly * the same effect as: * * addElement(item) * * @param item the item to be pushed onto this stack. * @return the item argument. * @see java.util.Vector#addElement
Java如何实现堆栈
//这是JDK提供的栈
import java.util.Stack;
public class UsingStack {
public static void main(String[] args) {
//构造栈对象,使用类型限制,只能存储Integer数据
StackInteger s = new StackInteger();
//1、2、3依次入栈
s.push(1);
s.push(2);
s.push(3);
//3、2、1依次出栈
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}
//这是我写的顺序结构的栈
import java.util.EmptyStackException;
import java.util.Vector;
public class UsingStack{
public static void main(String[] args){
//构造栈对象,使用类型限制,只能存储Integer数据
MyStackInteger s = new MyStackInteger();
//1、2、3依次入栈
s.push(1);
s.push(2);
s.push(3);
//3、2、1依次出栈
System.out.println(s.pop());
System.out.println(s.pop());
System.out.println(s.pop());
}
}
/**
* 栈类
* @author developer_05
* @param T
*/
class MyStackT extends VectorT{
/**
* 构造方法
*/
public MyStack(){
}
/**
* 入栈方法
* @param item 待入栈的元素
* @return 返回入栈的元素
*/
public T push(T item) {
addElement(item);
return item;
}
/**
* 出栈方法(同步处理)
* @return 返回出栈元素
*/
public synchronized T pop() {
T obj;
int len = size();
if (len == 0)
throw new EmptyStackException();
obj = elementAt(len - 1);
removeElementAt(len - 1);
return obj;
}
/**
* 判断栈是否为空的方法
* @return 返回true(栈空)或false(栈非空)
*/
public boolean empty() {
return size() == 0;
}
private static final long serialVersionUID = 1L;
}
用java代码编写堆栈?
参看: 1 import java.util.*; 2 3 public class TestStack { 4 public static void main(String[] args) { 5 Stack stack = new Stack(); 6 7 for(int i = 0; i 10; i++) { 8 stack.push(new Integer(i)); 9 }1011 if(!stack.empty()) {12 System.out.println(stack.pop());13 }14 }15 }
java,编程实现栈的原理,如何编程呢
用面向对象的思想考虑该问题,基本的栈的概念包含两种行为:出栈、入栈。
使用数组来完成这个事儿的话,入栈时在数组的最后一条记录后添加内容,出栈时取最后一条记录。
用java实现数据结构“栈
Java栈的实现
public class MyStack { //定义一个堆栈类
int[] array; //用int数组来保存数据,根据需要可以换类型
int s_size; //定义堆栈的宽度
public MyStack(int i){ //定义一个带参数构造器
array=new int[i]; //动态定义数组的长度
s_size=0; //堆栈的默认宽度为0
}
public MyStack(){ //默认构造器
this(50); //默认构造器可容纳50个元素
}
public void push(int i){ //压栈
array[this.s_size]=i;
this.s_size++;
}
public int pop(){ //从堆栈中取元素,从栈顶开始取
if(this.s_size!=0){
int t=array[s_size-1]; //用中间变量保存栈顶的元素
array[s_size-1]=0; //取完元素该位置设为0
s_size--; //栈的大小减1
return t; //返回栈顶元素
}else{
System.out.println("This stack is empty"); //当栈为空时显示提示信息,返回0
return 0;
}
}
public boolean isEmpty(){ //判断栈是否为空
return this.s_size==0;
}
public int top(){ //从栈顶取值,功能和 pop() 方法一样
if(!this.isEmpty()){
int t=array[this.s_size-1];
array[this.s_size-1]=0;
this.s_size--;
return t;
}else{
System.out.println("This stack is empty!");
return 0;
}
}
public void printAll(){ //打印出堆栈中的所有元素的值,不是取出,元素依然在堆栈里
if(!this.isEmpty()){
for(int i=this.s_size - 1;i=0;i--){
System.out.println(array[i]);
}
}
}
//下面是测试代码
public static void main(String[] args){
MyStack stack=new MyStack();
stack.push(4);
stack.push(5);
stack.push(6);
stack.push(7);
//System.out.println(stack.isEmpty());
stack.printAll();
System.out.println("===========");
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.top());
}
}
java栈用什么实现的的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java怎么用栈、java栈用什么实现的的信息别忘了在本站进行查找喔。
发布于:2022-12-04,除非注明,否则均为
原创文章,转载请注明出处。