数据结构Java实现——①栈

写在前面


转眼数据结构这本书就学完了,但是一直学习的是C版的,感觉很不爽,就借了本Java版的自学了点,感觉还不错, 特此共享。此处是栈。

文字描述


首先,所谓栈,就是一个先进后出的一个线性表。其功能类似于一个水杯,如图


只能从一端添加,而且也只能在该端删除,对每一个元素而已,先进后出

代码描述


1.栈的抽象接口

package org.Stone6762.MStack;
/**
 * @author_Stone6762
 * @Description_栈功能的抽象接口
 */
public interface MStack {

	/** 
	 * @Describe_将栈清空
	 */
	public void clear();

	/** 
	 * @Describe_判断栈是否为空
	 * @return
	 */
	public boolean isEmpty();

	/** 
	 * @Describe_求栈的长度
	 * @return
	 */
	public int length();

	/** 
	 * @Describe_返回栈顶元素
	 * @return
	 */
	public Object peek();

	/** 
	 * @Describe_入栈
	 * @param data
	 * @throws Exception
	 */
	public void push(Object data) throws Exception;

	/** 
	 * @Describe_出站
	 * @return
	 */
	public Object pop();

}

2.栈的顺序存储

package org.Stone6762.MStack.imple;

import org.Stone6762.MStack.MStack;

/**
 * @author_Stone6762
 * @Description_
 */
public class SqStack implements MStack {

	private Object[] stackElem;// ---存储栈中的数据
	private int top;// ----栈顶元素

	public SqStack(int maxSize) {
		top = 0;
		stackElem = new Object[maxSize];
	}

	@Override
	public void clear() {
		this.top = 0;
	}

	@Override
	public boolean isEmpty() {
		return this.top==0;
	}

	@Override
	public int length() {
		return this.top;
	}

	@Override
	public Object peek() {
		if(!isEmpty()){
			return stackElem[top-1];
		}
		return null;
	}

	@Override
	public void push(Object object) throws Exception {
		if(top==stackElem.length){
			throw new Exception("栈已满    ");
		}else{
			this.stackElem[top]=object;
			top++;			
		}
	}

	@Override
	public Object pop() {
		if(!isEmpty()){
			return stackElem[--top];
		}
		return null;
	}

	public void display(){
		for (int i = top-1; i >=0; i--) {
			System.out.print(stackElem[i].toString()+"  ");
		}
	}
	
}

3.链栈


3.1结点类的描述

package org.Stone6762.entity;

/**
 * @author_Stone6762
 * @Description_
 */
public class Node {

	/**@data每一个结点内部存储的数据
	 */
	private Object data;

	/**@next指向下一个结点
	 */
	private Node next;

	public Node() {
		this(null, null);
	}

	public Node(Object data) {
		this(data, null);
	}

	public Node(Object data, Node next) {
		this.data = data;
		this.next = next;
	}

	public Object getData() {
		return data;
	}

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

	public Node getNext() {
		return next;
	}

	public void setNext(Node next) {
		this.next = next;
	}

}

3.2链栈的描述

package org.Stone6762.MStack.imple;

import org.Stone6762.MStack.MStack;
import org.Stone6762.entity.Node;

/**
 * @author_Stone6762
 * @Description_
 */
public class LinkStack implements MStack {

	/**
	 * @top指向栈顶元素
	 */
	private Node top;

	@Override
	public void clear() {
		this.top = null;
	}

	@Override
	public boolean isEmpty() {
		return this.top == null;
	}

	@Override
	public int length() {
		Node p = this.top;
		int length = 0;
		while (p != null) {
			length++;
			p = p.getNext();
		}
		return length;
	}

	@Override
	public Object peek() {
		if (!isEmpty()) {
			return this.top.getData();
		} else {
			return null;
		}
	}

	@Override
	public void push(Object data) throws Exception {
		/*
		 * 先创建一个新的结点,将数据填入到新的结点中,然后将新结点的指针指向栈顶结点 最后,让栈顶指针指向新结点即可
		 */

		Node p = new Node(data);
		p.setNext(top);
		top = p;
	}

	@Override
	public Object pop() {
		if (isEmpty()) {
			return null;
		} else {
			Node p = this.top;
			top = top.getNext();
			return p.getData();
		}
	}

	/**
	 * @Describe_输出栈中的所有元素_由栈顶到栈底
	 */
	public void display() {
		Node p = this.top;
		while (p != null) {
			System.out.println(p.getData().toString() + "    ");
			p = p.getNext();
		}
	}

}






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值