---
title: "栈    java实现"
author: "拳拳天下"
date: "2015-01-29T17:12:09Z"
tags: ["Java"]
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2015-01-28T16:30:00Z"
id: 2180892
url: "https://smiling8866.iteye.com/blog/2180892"
---

package com.shine.stack; /* * 栈的接口 \*/ public interface SStack { boolean isEmpty(); void push(T x); T pop(); T get(); } package com.shine.stack; /* * 顺序栈类 \*/ public class SeqStack implements SStack { private Object element\[]; private int top; public SeqStack(int size){//构造容量为size的空栈 this.element = new Object\[Math.abs(size)]; this.top = -1; } public SeqStack(){ this(64); } @Override public boolean isEmpty() { return this.top==-1; } @Override public void push(T x) {//入栈操作 if(x==null) return; if(this.top==this.element.length-1){//若栈满，需要扩充栈的容量 Object\[] temp = this.element; this.element = new Object\[temp.length\*2]; for(int i=0;i implements SStack{ private Node top; public LinkedStack(){ this.top = null; } @Override public boolean isEmpty() { return this.top==null; } @Override public void push(T x) { if(x!=null) this.top = new Node(x,this.top); } @Override public T pop() { if(this.top == null) return null; T temp = this.top.data; this.top = this.top.next; return temp; } @Override public T get() { return this.top==null?null:this.top.data; } }