Java数据结构与算法之stack栈

本文探讨了Java中数据结构的重要组成部分——栈,并介绍了如何在Java中实现栈。通过学习路线,从Array和LinkedList到Queue,文章详细阐述了栈在数据处理中的作用和操作方式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目录:
1.栈概述
2.数组实现自定义栈
3.链表实现自定义栈
4.集合实现自定义栈

1.栈概述
栈和队列一样,也是线性表的一种,它唯一的特点是需要满足先进后出(FILO)的规则,也就是只能对栈的一头进行操作,添加数据
称为压栈,移除数据称为弹栈。而在java中栈的实现可以通过数组,链表,集合(ArrayList/LinkedList)3种方式进行实现。


2.数组实现自定义栈
(1)自定义栈接口CustomStack.java
package com.datastructure.test;


public interface CustomStack<T> {
	//压栈方法
	public void push(T data)throws Exception;
	//弹栈/移除顶部元素,并返回对应的数据
	public T pop()throws Exception;
	//获取stact第一个元素
	public T peek();
	//判断栈是否为空
	public boolean empty();
	//返回栈中元素的个数
	public int size();
	
}

(2)数组自定义栈类CustomArrayStack.java
package com.datastructure.test;


public class CustomArrayStack<T> implements CustomStack<T> {
	static final int defaultSize = 15;
	//指示顶部元素的位置
	private int size;
	private T[] arrays;
	/*
	 * 无参构造方法,做一些初始化的操作
	 */
	@SuppressWarnings("unchecked")
	public CustomArrayStack() {
		size = 0;
		arrays = (T[]) new Object[defaultSize];
	}
	/*
	 * 根据用户自定义数组大小初始化数组
	 */
	@SuppressWarnings("unchecked")
	public CustomArrayStack(int customSize) {
		size = 0;
		arrays = (T[]) new Object[customSize];
	}
	/*
	 * 向栈顶添加元素
	 */
	@Override
	public void push(T data) throws Exception {
		if (size<arrays.length) {
			arrays[size] = data;
			size++;
		}else {
			throw new Exception("数组栈已经满啦!");
		}
	}
	/*
	 * 将栈顶的元素移除
	 */
	@Override
	public T pop() throws Exception {
		T topData;
		if (empty()) {
			throw new Exception("数组栈为空!");
		}else {
			topData = arrays[size-1];
			size--;
		}
		return topData;
	}
	/*
	 * 获取栈顶的元素
	 */
	@Override
	public T peek() {
		return arrays[size-1];
	}
	/*
	 * 判断栈是否为空
	 */
	@Override
	public boolean empty() {
		return size==0;
	}
	/*
	 * 返回栈的长度
	 */
	@Override
	public int size() {
		return size;
	}


}

(3)测试类CustomArrayStackTest.java
package com.datastructure.test;


public class CustomArrayStackTest {


	public static void main(String[] args) {
		CustomArrayStack<String> stack = new CustomArrayStack<>(10);
		System.out.println("是否为空:"+stack.empty());
		try {
			stack.push("I");
			stack.push("am");
			stack.push("andy");
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("栈顶元素:"+stack.size());
		System.out.println("栈顶元素:"+stack.peek());
		try {
			System.out.println("移除元素:"+stack.pop());
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("栈顶元素:"+stack.size());
		System.out.println("栈顶元素:"+stack.peek());
		
	}


}

(4)测试控制台输出
是否为空:true
栈长度:3
栈顶元素:andy
移除元素:andy
栈长度:2
栈顶元素:am

3.链表实现自定义栈
(1)自定义栈接口CustomStack.java(与2一致)
(2)链表自定义栈类CustomLinkedStack.java
package com.datastructure.test;


public class CustomLinkedStack<T> implements CustomStack<T> {
	private int size;
	private Node topNode;
	@Override
	public void push(T data) throws Exception {
		Node newTopNode;
		if (empty()) {
		    newTopNode = new Node(data, null);		
		}else {
		    newTopNode = new Node(data, topNode);		
		}
		topNode = newTopNode;
		size++;
	}


	@Override
	public T pop() throws Exception {
		Node oldTopNode = topNode;
		topNode = topNode.nextNode;
		size--;
		return oldTopNode.data;
	}


	@Override
	public T peek() {
		return topNode.data;
	}


	@Override
	public boolean empty() {
		return size==0;
	}


	@Override
	public int size() {
		return size;
	}
	/**
	 * 节点内部类
	 */
	class Node{
		private T data;
		private Node nextNode;
		public Node(T data,Node nextNode){
			this.data = data;
			this.nextNode = nextNode;
		}
	}


}

(3)测试类CustomLinkedStackTest.java
package com.datastructure.test;


public class CustomLinkedStackTest {


	public static void main(String[] args) {
		CustomLinkedStack<String> stack = new CustomLinkedStack<>();
		System.out.println("是否为空:"+stack.empty());
		try {
			stack.push("I");
			stack.push("am");
			stack.push("andy");
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("栈长度:"+stack.size());
		System.out.println("栈顶元素:"+stack.peek());
		try {
			System.out.println("移除元素:"+stack.pop());
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("栈长度:"+stack.size());
		System.out.println("栈顶元素:"+stack.peek());
		
	}


}	

(4)测试结果与2一致

4.集合实现自定义栈
(1)自定义栈接口CustomStack.java(与2一致)
(2)集合类LinkedList实现栈自定义CustomCollectionStack.java
package com.datastructure.test;


import java.util.LinkedList;


class CustomCollectionStack<T> implements CustomStack<T> {
	private LinkedList<T> linkedList = new LinkedList<>();
	
	public CustomCollectionStack() {
		
	}


	@Override
	public void push(T data) throws Exception {
		linkedList.add(data);
	}


	@Override
	public T pop() throws Exception {
		return linkedList.removeLast();
	}


	@Override
	public T peek() {
		return linkedList.getLast();
	}


	@Override
	public boolean empty() {
		return linkedList.isEmpty();
	}


	@Override
	public int size() {
		return linkedList.size();
	}


}

(3)测试类CustomCollectionStackTest.java
package com.datastructure.test;


public class CustomCollectionStackTest {


	public static void main(String[] args) {
		CustomCollectionStack<String> stack = new CustomCollectionStack<>();
		System.out.println("是否为空:"+stack.empty());
		try {
			stack.push("I");
			stack.push("am");
			stack.push("andy");
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("栈长度:"+stack.size());
		System.out.println("栈顶元素:"+stack.peek());
		try {
			System.out.println("移除元素:"+stack.pop());
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println("栈长度:"+stack.size());
		System.out.println("栈顶元素:"+stack.peek());
		
	}


}
	

(4)结果与2一致


文章:

Java数据结构与算法之学习路线

Java数据结构与算法之Array数组

Java数据结构与算法之LinkedList单链表


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值