「栈的实现java」栈的实现及应用实验原理

博主:adminadmin 2022-11-27 10:23:05 64

今天给各位分享栈的实现java的知识,其中也会对栈的实现及应用实验原理进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

怎样用java实现栈

这个...怎么说了,直接用啊。Java本身就有 java.util.Stack 这个专门处理栈的类。

如果是要交作业,直接把Stack的代码贴过去吧,jdk自带的东西,全部都有源码的,呵呵。

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,编程实现栈的原理,如何编程呢

用面向对象的思想考虑该问题,基本的栈的概念包含两种行为:出栈、入栈。

使用数组来完成这个事儿的话,入栈时在数组的最后一条记录后添加内容,出栈时取最后一条记录。

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的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于栈的实现及应用实验原理、栈的实现java的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-27,除非注明,否则均为首码项目网原创文章,转载请注明出处。