「栈的实现java」中缀转后缀栈的实现
本篇文章给大家谈谈栈的实现java,以及中缀转后缀栈的实现对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
- 1、java,编程实现栈的原理,如何编程呢
- 2、用java实现数据结构“栈
- 3、【JAVA】写一个类,实现栈这种数据结构,要求底层数据使用ArrayList存储。
- 4、Java如何实现堆栈
- 5、java中栈是如何实现的?
- 6、用java编写出来:用数组实现一个栈
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】写一个类,实现栈这种数据结构,要求底层数据使用ArrayList存储。
栈的特点的就是后进先出,那么你就linkedList,如果要添加一个元素,就把他存到最后一个位置,要取一个元素,也从最后开始取就可以实现了,只有linkedList才有存,取,删最后一个元素这个方法,所以要要用linkedList
代码如下:
public
class
StudyTest
{
private
LinkedList
list
=
new
LinkedList
();
public
String
get()
{
return
list.getLast();
}
public
void
add(String
str)
{
this.list.addLast(str);
}
public
void
remove(){
this.list.removeLast();
}
}
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.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编写出来:用数组实现一个栈
public class Stack {
private Object[] stack;
//这个不需要;
//private int top = 0; //初始化栈顶
//这个也不需要;
//写一个栈出来,最好是可以动态的,可以自己改变大小的,即数组的长度;
//private int size = 0; // 初始化大小
//元素个数;
private int size;
//默认长度为10;
public Stack(){
this(10);
}
//也可以自己设置长度,即容量;
public Stack(int len){
stack = new Object[len];
}
//返回元素个数;
public int size(){
return size;
}
//返回数组长度,即容量;
public int capacity(){
return stack.length;
}
//实现动态的数组;
public void ensureCapacity(){
if(size() == capacity()){
Object[] newStack = new Object[size() * 3 / 2 + 1];
System.arraycopy(stack, 0, newStack, 0, size());
stack = newStack;
}
}
//入栈;
public void push(Object o){
size++;
ensureCapacity();
stack[size - 1] = o;
}
/*
public void push(Object object) {
if (isFull()) {
System.out.println("栈满! 入栈失败");
}
stack[top++] = object;
}
*/
//判空;
public boolean isEmpty(){
return size == 0;
}
//出栈;
public Object pop(){
//首先要判空;
if(isEmpty()){
throw new ArrayIndexOutOfBoundsException("不能为空");
}
Object o = stack[--size];
stack[size] = null;
return o;
}
/*
// 出栈
public Object pop() {
Object object = stack[--top];
stack[top] = null;
return object;
}
*/
/*
// 计算栈当前大小
public int size() {
return top;
}
// 判断是否是空栈
public boolean isEmpey() {
return top == 0;
}
// 判断是否栈满
public boolean isFull() {
return top = size;
}
public Stack(int size) {
this.size = size;
}
*/
public static void main(String[] args) {
Stack stack = new Stack(3);
String[] data = new String[] { "a", "b", "c" };
for (int i = 0; i data.length; i++) {
stack.push(data[i]);
System.out.println(data[i] + "");
}
System.out.println("***********");
while (!stack.isEmpty()) {
System.out.println(stack.pop() + "");
}
//}
}
}
你自己对比一下,我是在你的里面修改的
栈的实现java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于中缀转后缀栈的实现、栈的实现java的信息别忘了在本站进行查找喔。
发布于:2022-12-27,除非注明,否则均为
原创文章,转载请注明出处。