希望大家帮忙啊,给分啦
public class Stack{ Object o = new Object(10); pubic void Push(){}
public Object Pop(){}
}需求:将任何数据放入栈区内(0 作为载体)
//怎么模拟堆栈的实现啊//完成上面的push,pop方法
public class Stack{ Object o = new Object(10); pubic void Push(){}
public Object Pop(){}
}需求:将任何数据放入栈区内(0 作为载体)
//怎么模拟堆栈的实现啊//完成上面的push,pop方法
public class MyStack {
private Object [] o = new Object[10];
private int top_point = 1;
public void push(Object o)
{
if (top_point >= 10)
{
System.out.println("Stack is full!");
return;
}
this.o[top_point++] = o;
}
public Object pop()
{
if (top_point >= 10)
{
System.out.println("Stack is null!");
return null;
}
return this.o[--top_point];
}
}
private int nItems;
private Object []obj;public Stack(int size){
nItems=0;
obj=new Object[size];
}
public void push(Object objs){
obj[nItems++]=obj;
}public Object pop(){
return obj[nItems--];
}
}
//应该还要判断Stack是否为空出栈的时候 进栈的时候判断是否为满
{
public static final int MAX =1000;
Object object[] = new Object[MAX];
int top;
public Stack()
{
top=0;
}
public void Push(Object o)
{
if(top>=MAX) throw new Exception("OverFlow");
object[top]=o;
top++;
}
public Object Pop()
{
if(top<0) throw new Exception("OverFlow");
Object temp=object[top];
object[top]=null;
top--;
return temp;
}
}
public Object Pop(){return o;}
}
* @(#)Stack.java 1.28 03/12/19
*
* Copyright 2004 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/package java.util;/**
* The <code>Stack</code> class represents a last-in-first-out
* (LIFO) stack of objects. It extends class <tt>Vector</tt> with five
* operations that allow a vector to be treated as a stack. The usual
* <tt>push</tt> and <tt>pop</tt> operations are provided, as well as a
* method to <tt>peek</tt> at the top item on the stack, a method to test
* for whether the stack is <tt>empty</tt>, and a method to <tt>search</tt>
* the stack for an item and discover how far it is from the top.
* <p>
* When a stack is first created, it contains no items.
*
* @author Jonathan Payne
* @version 1.28, 12/19/03
* @since JDK1.0
*/
public
class Stack<E> extends Vector<E> {
/**
* Creates an empty Stack.
*/
public Stack() {
} /**
* Pushes an item onto the top of this stack. This has exactly
* the same effect as:
* <blockquote><pre>
* addElement(item)</pre></blockquote>
*
* @param item the item to be pushed onto this stack.
* @return the <code>item</code> argument.
* @see java.util.Vector#addElement
*/
public E push(E item) {
addElement(item); return item;
} /**
* Removes the object at the top of this stack and returns that
* object as the value of this function.
*
* @return The object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @exception EmptyStackException if this stack is empty.
*/
public synchronized E pop() {
E obj;
int len = size(); obj = peek();
removeElementAt(len - 1); return obj;
} /**
* Looks at the object at the top of this stack without removing it
* from the stack.
*
* @return the object at the top of this stack (the last item
* of the <tt>Vector</tt> object).
* @exception EmptyStackException if this stack is empty.
*/
public synchronized E peek() {
int len = size(); if (len == 0)
throw new EmptyStackException();
return elementAt(len - 1);
} /
这是java API 中 Stack的源代码可以参考一下!
public class MyStack { private int topNo = 0;// topNo==0表示栈中已无对象。 private int maxItem; private Object[] items; public MyStack(int itemNo) {
items = new Object[itemNo];
maxItem = itemNo;
} public synchronized void push(Object item) {
if (topNo < maxItem)
items[topNo++] = item;
else
System.out.println("Stack is full!"); } public synchronized Object pop() {
if (topNo == 0) {
System.out.println("Stack is empty!");
return "Null";
} else
return items[--topNo];
}
public static void main(String [] args)
{
MyStack ms=new MyStack(10);
for (int i=0;i<11;i++)
{
ms.push(""+i);
}
for (int i=0;i<12;i++)
{
System.out.println( ms.pop().toString() );
}
}
}