栈小伙伴们都知道吧,那你们知道在java中栈该如何实现吗?它又有几种实现方法呢?快让小编为你介绍介绍吧。
在java中,栈一般有这三种常用实现方法,如下:
一、使用链表模拟栈实现
package com.dataStructure.stack; /** * 类功能描述: * * @version 1.0.0 * @auther Create by Barry * @date Create on 2018/3/12. * @history */ public class LinkedStack implements Stack { private Node header; private int elementCount = 0; private int maxSize; public LinkedStack(int maxSize) { this.maxSize = maxSize; } @Override public boolean push(Object data) { if (elementCount == maxSize) { throw new RuntimeException("Stack is Full"); } header = new Node(data, header); elementCount++; return true; } @Override public Object pop() { if (isEmpty()) { throw new RuntimeException("Stack is empty"); } Object returnData = header.data; header = header.next; elementCount--; return returnData; } @Override public Object peek() { return header.data; } @Override public int elementCount() { return elementCount; } @Override public boolean isEmpty() { return (elementCount == 0); } @Override public void showElements() { while (header != null) { System.out.println(header.data); header = header.next; } } class Node { private Object data; private Node next; public Node(Object data) { this(data, null); } public Node(Object data, Node next) { this.data = data; this.next = next; } } public static void main(String[] args) { Stack stack = new ArrayStack(5); for (int i = 1; i <= 5; i++) { stack.push(i); } stack.pop(); stack.showElements(); } }
二、使用Java自带Stack方法实现
package com.dataStructure.stack; /** * 类功能描述: * * @version 1.0.0 * @auther Create by Barry * @date Create on 2018/3/12. * @history */ public interface Stack { public boolean push(Object element); public Object pop(); public Object peek(); public int elementCount(); public boolean isEmpty(); public void showElements(); }
三、使用数组模拟栈实现
package com.dataStructure.stack; /** * 类功能描述: * * @version 1.0.0 * @auther Create by Barry * @date Create on 2018/3/12. * @history */ public class ArrayStack implements Stack { private int maxSize; private Object data[]; private int top = -1; public ArrayStack(int maxSize) { data = new Object[maxSize]; this.maxSize = maxSize; } @Override public boolean push(Object element) { if (top == maxSize - 1) { return false; } data[++top] = element; return true; } @Override public Object pop() { if (this.isEmpty()) { return -1; } Object returnData = data[top]; top--; return returnData; } @Override public Object peek() { if (this.isEmpty()) { return -1; } return data[top]; } @Override public int elementCount() { return top + 1; } @Override public boolean isEmpty() { if (top == -1) { return true; } return false; } @Override public void showElements() { System.out.println("--------Show all element start--------"); for (int i = top; i >= 0; i--) { System.out.println(data[i]); } System.out.println("--------Show all element end--------"); } public static void main(String[] args) { Stack stack = new ArrayStack(11); /*System.out.println("=============================test 1 start--------------"); stack.push(1); stack.showElements(); System.out.println(stack.elementCount()); System.out.println("=============================test 1 end--------------"); System.out.println("=============================test 2 start--------------"); stack.push(2); stack.showElements(); System.out.println(stack.elementCount()); System.out.println("=============================test 2 end--------------"); System.out.println("=============================test 3 start--------------"); stack.pop(); stack.showElements(); System.out.println(stack.elementCount()); System.out.println("=============================test 3 end--------------");*/ /*System.out.println("=============================test 4 start--------------"); for(int i=1; i<=12; i++ ){ stack.push(i); } stack.showElements(); System.out.println(stack.elementCount()); System.out.println("=============================test 4 end--------------");*/ String str = "1234321"; System.out.println(ArrayStack.isSysmetryStr(str)); } /** * 用栈判断字符串是否为对称串, * 即从左到右和从右到左读取是一样的 * 1234321为对称串 * @param str * @return */ public static boolean isSysmetryStr(String str) { if (str == null || str.length() == 0) { return true; } Stack stack = new ArrayStack(str.length()); for (int i = 0; i < str.length(); i++) { stack.push((int) str.charAt(i)); } for (int i = 0; i < str.length(); i++) { Object data = stack.pop(); if ((int) data == (int) str.charAt(i)) { return false; } } return true; } }
以上就是关于如何在java中实现栈的所有内容了,更多栈相关java编程常见问题请关注我们的网站了解答案详情吧。
推荐阅读: