数据结构之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());
	}
}

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

猜你喜欢

转载自blog.csdn.net/yijiemuhuashi/article/details/118559467
今日推荐