赞
踩
public interface stack<T> {
boolean isEmpty(); //判空
void clear(); //清空
T pop(); //弹出
void push(T data); //压入
int getLength(); //返回长度
T peek(); //返回栈顶值
int search(T t); //查找元素位置
void display(); //输出所有元素
}
import java.util.Objects; /** * 用数组实现栈 * @param <T> */ public class ArrayStack<T> implements stack<T> { public ArrayStack(){} private T[] array=(T[])new Object[16]; private int size=0; @Override public boolean isEmpty() { return (size==0); } @Override public void clear() { for(int i=0;i<size;i++){ array[i]=null; } size=0; } @Override public T pop() { if(size==0) return null; else { T temp=array[size-1]; array[size-1]=null; size--; return temp; } } @Override public void push(T data) { if(size>=array.length){ resize(); //重新分配两倍大小的内存 array[size++]=data; } else { array[size++]=data; } } public void resize(){ T[] temp=(T[])new Object[array.length*2]; for(int i=0;i<array.length;i++){ temp[i]=array[i]; } for(int i=array.length;i<array.length*2;i++){ temp[i]=null; } array=temp; temp=null; } @Override public int getLength() { return size; } @Override public T peek() { if(size==0) return null; else return array[size-1]; } @Override public int search(T t) { for(int i=0;i<size;i++){ if(array[i]==t) return i+1; } return 0; } @Override public void display() { for(int i=0;i<size;i++){ System.out.println("data:"+array[i]); } } public static void main(String[] args) { ArrayStack<String> a = new ArrayStack(); a.push("hello,world..."); a.push("my name is: "); a.push("jeavenwong"); a.display(); System.out.println(a.getLength()); System.out.println(a.peek()); System.out.println(a.pop()); } }
public class LinkStack<T> implements stack<T> { private Node top;//栈顶指针 private int size;//栈的大小 class Node{ private T data; private Node next; } public LinkStack(){ this.top=null; this.size=0; } @Override public boolean isEmpty() { return (size==0); } @Override public void clear() { top=null; size=0; } @Override public T pop() { if(top==null) return null; else { T popData=top.data; top=top.next; size--; return popData; } } @Override public void push(T data) { Node node=new Node(); node.data=data; node.next=null; if(top==null){ top=node; node=null; size++; }else { node.next=top; top=node; node=null; size++; } } @Override public int getLength() { return size; } @Override public T peek() { return top.data; } @Override public int search(T t) { Node temp=top; int num=size; while (temp!=null){ if(temp.data==t) return num; temp=temp.next; num--; } return 0; } @Override public void display() { Node node=top; while (top!=null){ System.out.println("data:"+top.data); top=top.next; } top=node; node=null; } public static void main(String[] args) { LinkStack<String> b = new LinkStack(); b.push("how are you?"); b.push("i am fine..."); b.push("and you?..."); b.display(); System.out.println(b.peek()); System.out.println(b.getLength()); System.out.println(b.isEmpty()); System.out.println(b.search("i am fine...")); System.out.println(b.pop()); b.display(); b.clear(); b.display(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。