数据结构之Stack实现

Stack作为一中数据结构,先入后出的特性。下面给出栈的数据结构抽象:

package com.dzlijian.utils.comments;
/**
 * 
 * @ClassName:  Stack   
 * @Description:栈的数据结构抽象   
 * @author: 长子科技 
 * @date:   2021年7月7日 下午10:48:15   
 *   
 * @param <E>  
 * @Copyright: 2021 www.tydic.com Inc. All rights reserved. 
 * 注意:本内容仅限于长子科技股份有限公司内部传阅,禁止外泄以及用于其他的商业目
 */
public interface Stack<E> {
	/*
	 * 判空
	 */
	public boolean isEmpty();
	/*
	 * 获取长度
	 */
	public int size();
	/*压栈
	 * 
	 */
	public void push(E item);
	/*
	 * 弹栈
	 */
	public E pop();
	/*
	 * 获取栈顶
	 */
	public E peek();
}

同样分别使用数组和链表来实现栈数据结构:
1,使用数组实现的代码:`package com.dzlijian.utils.comments;

public class ArrayStack implements Stack {

private E[] data;

private int size;

private int pop;

private static final int DEFAULT_CAPACITY=10;

 public ArrayStack(int capacity) {
	size=0;
	pop=-1;
	data=(E[])new Object[capacity];
}
 public ArrayStack() {
	 this(DEFAULT_CAPACITY);
 }
 

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

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

private void grow(int newCapacity){
	if(newCapacity<=DEFAULT_CAPACITY){
		return;
	}
	E[] newdata=(E[])new Object[newCapacity];
	for(int i=0;i<size;i++){
		newdata[i]=data[(pop+i)%data.length];
	}
	data=newdata;
	pop=size-1;
}

@Override
public void push(E item) {
	if(item==null){
		throw new IllegalArgumentException("数据为空");
	}
	if(size==data.length){
		grow(size*2);
	}
	pop=(pop+1)%data.length;
	data[pop]=item;
	if(size==0){
		pop=0;
	}
	size++;
}

@Override
public E pop() {
	if(size==0){
		throw new IllegalArgumentException("栈为空");
	}
	E e=data[pop];
	data[pop]=null;
	pop--;
	size--;
	return e;
}

@Override
public E peek() {
	if(size==0){
		throw new IllegalArgumentException("栈为空");
	}
	return data[pop];
}

public static void main(String[] args) {
	Stack<Integer> stack=new ArrayStack<>();
	stack.push(1);
	stack.push(2);
	stack.push(3);
	stack.pop();
	System.out.println(stack.size());
}

}
`此方法实现,注意grow方法的实现。
2,使用链表来实现stack:

package com.dzlijian.utils.comments;


public class LinkedStack<E> implements Stack<E> {
	
	
	private static class Node<E>{
		
		private E data;
		
		private Node<E> next;
		
		public Node(E e,Node<E> next){
			data=e;
			this.next=next;
		}
		public Node(E e){
			this(e,null);
		}
	}
	
	private int size;
	
	private Node<E> head;
	
	private Node<E> tail;
	
	 public LinkedStack() {
		this.size=0;
		this.head=null;
		this.tail=null;
	}

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

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

	@Override
	public void push(E item) {
		Node<E> node=new Node<E>(item,null);
		if(size==0){
			head=node;
			tail=head;
			size++;
			return;
		}
		
		tail.next=node;
		tail=node;
		size++;
		
	}

	@Override
	public E pop() {
		
		if(size==0){
			throw new IllegalArgumentException("栈为空");
		}
		
		Node<E> result=tail;
		if(size==1){
			size--;
			head=null;
			tail=null;
			return result.data;
		}
		if(size==2){
			size--;
			head.next=null;
			tail=head;
			return result.data;
		}
		
		if(size>2){
			Node<E> pre=head;
			while (pre.next.next!=null) {
				pre=pre.next;
			}
			tail=pre;
			tail.next=null;
			size--;
		}
		return result.data;
	}

	@Override
	public E peek() {
		return tail.data;
	}
	
	public static void main(String[] args) {
		Stack<Integer> stack=new LinkedStack<>();
		stack.push(1);
		stack.push(2);
		System.out.println(stack.size());
	}
}

这里使用链表来实现栈的数据结构,比较简单。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一截木化石

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值