数据结构探险篇 二.链式栈的实现

本文详细介绍了使用Java实现的栈数据结构,包括栈类和节点类的定义,提供了获取栈内元素数量、判断栈是否为空或已满、获取栈顶元素、元素入栈和出栈等API,并展示了如何获取特定数据的索引、清空栈和打印栈内所有元素。
摘要由CSDN通过智能技术生成

数据结构

栈类
public class MyStack <T>{

	private int max_len;//栈能存放的最大节点数量
	private int len;//现在栈存放的节点的数量
	private StackNode<T> head;//栈顶节点
	
	
	public MyStack() {
		max_len = 100;
		len = 0;
		head = null;
	}
	
	public MyStack(int max){
		max_len = max;
		len = 0;
		head = null;
	}
}
节点类
package book_1_3;

public class StackNode <T> {

	private StackNode pre_stackNode;
	private StackNode nex_StackNode;
	private T data;
	
	public StackNode(T d) {
		pre_stackNode = null;
		data = d;
	}

	public StackNode getPre_stackNode() {
		return pre_stackNode;
	}

	public void setPre_stackNode(StackNode pre_stackNode) {
		this.pre_stackNode = pre_stackNode;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}
	
	

}

api

获取栈内元素的个数
	public int get_eleNum() {
		return len;
	}
判断当前栈是否为空
	public boolean is_empty(){
		if(len == 0){
			return true;
		}
		return false;
	}
判断当前栈是否已满
	public boolean is_fill(){
		if(len < max_len && len >= 0 && max_len >= 0){
			return false;
		}
		return true;
	}
获取栈顶元素不出栈
	public T peek(){
		//判空
		if(is_empty()){
			System.out.println("当前栈为空");
			return null;
		}
		return  head.getData();
	}
获取栈顶元素并出栈
	public T pop(){
		//判空
		if(is_empty()){
			System.out.println("当前栈为空");
			return null;
		}
		T res = head.getData();
		head = head.getPre_stackNode();
		len --;
		return res;
	}
将元素入栈
	public boolean push(T data){
		//判满
		if(is_fill()){
			System.out.println("当前栈已满");
			return false;
		}
		StackNode<T> newStackNode = new StackNode<T>(data);
		newStackNode.setPre_stackNode(head);
		head = newStackNode;
		len++;
		return true;
	}
获取在栈中与当前数据相同的第一个元素的索引
	public int get_dataIndex(T data){
		//判空
		if(is_empty()){
			System.out.println("当前栈为空");
			return -1;
		}
		
		StackNode<T> v = head;
		int index = 1;
		int last = -1;
		while(v != null){
			if(v.getData().toString().equals(data.toString())){
				last = index;
			}
			index++;
			v = v.getPre_stackNode();
		}
		if(last == -1){
			return last;
		}
		return len - last + 1;
	}
将栈清空
	public void clear(){
		head = null;
		len = 0;
	}
打印栈内所有元素
	public void prinStack(){
		StackNode v = head;
		while(v != null){
			System.out.println(v.getData().toString());
			v = v.getPre_stackNode();
		}
	}

完整代码

package book_1_3;
//节点类
public class StackNode <T> {

	private StackNode pre_stackNode;
	private StackNode nex_StackNode;
	private T data;
	
	public StackNode(T d) {
		pre_stackNode = null;
		data = d;
	}

	public StackNode getPre_stackNode() {
		return pre_stackNode;
	}

	public void setPre_stackNode(StackNode pre_stackNode) {
		this.pre_stackNode = pre_stackNode;
	}

	public T getData() {
		return data;
	}

	public void setData(T data) {
		this.data = data;
	}
	
	

}


//栈类
public class MyStack <T>{

	private int max_len;//栈能存放的最大节点数量
	private int len;//现在栈存放的节点的数量
	private StackNode<T> head;//栈顶节点
	
	
	public MyStack() {
		max_len = 100;
		len = 0;
		head = null;
	}
	
	public MyStack(int max){
		max_len = max;
		len = 0;
		head = null;
	}
	
   //获取栈内元素的个数
	public int get_eleNum() {
		return len;
	}
	
	//判断当前栈是否为空
	public boolean is_empty(){
		if(len == 0){
			return true;
		}
		return false;
	}
	
	//判断当前栈是否已满
	public boolean is_fill(){
		if(len < max_len && len >= 0 && max_len >= 0){
			return false;
		}
		return true;
	}
	
	//获取栈顶元素不出栈
	public T peek(){
		//判空
		if(is_empty()){
			System.out.println("当前栈为空");
			return null;
		}
		return  head.getData();
	}
	
	//获取栈顶元素并出栈
	public T pop(){
		T res = peek();
		head = head.getPre_stackNode();
		len --;
		return res;
	}
	
	//将元32.2素入栈
	public boolean push(T data){
		//判满
		if(is_fill()){
			System.out.println("当前栈已满");
			return false;
		}
		StackNode<T> newStackNode = new StackNode<T>(data);
		newStackNode.setPre_stackNode(head);
		head = newStackNode;
		len++;
		return true;
	}
	
	//获取在栈中与当前数据相同的第一个元素的索引
	public int get_dataIndex(T data){
		//判空
		if(is_empty()){
			System.out.println("当前栈为空");
			return -1;
		}
		
		StackNode<T> v = head;
		int index = 1;
		int last = -1;
		while(v != null){
			if(v.getData().toString().equals(data.toString())){
				last = index;
			}
			index++;
			v = v.getPre_stackNode();
		}
		if(last == -1){
			return last;
		}
		return len - last + 1;
	}
	
	//将栈清空
	public void clear(){
		head = null;
		len = 0;
	}
	
	//打印栈内所有元素
	public void prinStack(){
		StackNode v = head;
		while(v != null){
			System.out.println(v.getData().toString());
			v = v.getPre_stackNode();
		}
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值