基于数组实现
package com.ma.stack;
/**
* @author sunfch
*此栈实现基于数组,初始栈时已经设置栈大小
*/
public class MyArrayStack {
private int size;
private int top = -1;
private int[] arr;
MyArrayStack(){
arr =new int[3]; //默认栈大小为3
}
MyArrayStack(int size) {
if(size > 0){
this.size = size;
arr = new int[size];
}
else throw new RuntimeException("数组长度值不正确");
}
//==============================================================================
//入栈
public void push(int data){
if(top >= size-1){
System.out.println("栈已满");
return;
}
arr[++top] = data;
}
//==============================================================================
//出栈
public int pop(){
if(top < 0){
throw new RuntimeException("栈为空");
}
else{
int temp = arr[top];
arr[top] = 0;
top--;
return temp;
}
}
//查看栈顶元素但不出栈
public int peek(){
if(top < 0){
throw new RuntimeException("栈为空");
}
else{
return arr[top];
}
}
//==============================================================================
public boolean isEmpty(){
return top == -1;
}
//==============================================================================
public static void main(String[] args) {
MyArrayStack stack = new MyArrayStack(3);//初始栈大小为3
stack.push(2);
stack.push(5);
stack.push(10);
//stack.push(11);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
//System.out.println(stack.pop());
// System.out.println(stack.peek());
// System.out.println(stack.peek());
System.out.println(stack.isEmpty());
}
}
基于数组实现:
package com.ma.stack;
/**
* @author sunfch
* 此栈实现基于链表,初始栈时未设置栈大小(栈大小可变)
*此栈是将新元素插到链表头部(链表头部是栈顶)
*/
public class MyLinkStack {
private Node top;//记录栈顶元素
private int size = 0;//记录栈大小
public MyLinkStack() {
top = null;
}
class Node{
public int data; //记录数据
public Node next;
Node(int data){
this.data = data;
}
}
//=============================================================================================
//入栈
public void push(int data){
Node newNode = new Node(data);
if(top == null){
top =newNode;
}
else{
newNode.next = top;//将新元素插到链表头部
top = newNode;
}
size++;
}
//=============================================================================================
//出栈
public Integer pop(){
if(top == null) throw new RuntimeException("栈为空");
else{
int val = top.data;
top = top.next;
size--;
return val;
}
}
//查看栈顶元素但不出栈
public Integer peek(){
if(top == null) throw new RuntimeException("栈为空");
else{
return top.data;
}
}
//=============================================================================================
public boolean isEmpty(){
return top == null;
}
public int getSize(){
return size;
}
//=============================================================================================
public static void main(String[] args) {
MyLinkStack my = new MyLinkStack();
my.push(4);
my.push(5);
my.push(6);
System.out.println("当前size:"+my.getSize());
System.out.println(my.isEmpty());
System.out.println(my.pop());
System.out.println("当前size:"+my.getSize());
System.out.println(my.pop());
System.out.println(my.pop());
System.out.println(my.isEmpty());
//System.out.println(my.pop());
}
}