Java数据结构与算法之stack栈

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

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


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


public interface CustomStack {
	//压栈方法
	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 implements CustomStack {
	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

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


public class CustomArrayStackTest {


	public static void main(String[] args) {
		CustomArrayStack 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 implements CustomStack {
	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 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 implements CustomStack {
	private LinkedList 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 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单链表


Java数据结构与算法之Queue队列

Java数据结构与算法之stack栈


你可能感兴趣的:(JAVA)