我试图在Java中创建一个使用数组实现的Stack。下面是我自定义堆栈类中的pop()方法
public E pop()
{
if(data.length == 0)
{
throw new EmptyStackException();
}
E poppedObject = data[0];
for(int i = 0; i < data.length-1; i++) //Moving all the elements one closer to top
{
data[i] = data[i+1];
}
return poppedObject;
}
当所有数据都已从堆栈中弹出并且您尝试从其中弹出一些内容时,应抛出EmptyStackException。但是,data.length不会随着对象的弹出而改变。如果一个堆栈是空的,如果它不能告诉data.length,pop方法应该如何判断?
答案 0 :(得分:2)
设置一个计数器,告诉你数组中的元素数量。 Array.length将告诉您堆栈的容量,而不是堆栈中的元素数量。对于此示例,count
是我的计数器
public E pop() throws EmptyStackException {
if(count <= 0) {
throw new EmptyStackException();
}
E poppedObject = data[0];
for(int i = 0; i < count; i++) { //Moving all the elements one closer to top
data[i] = data[i+1];
}
count--;
return poppedObject;
}
另请注意,如果正确实现堆栈,堆栈将自下而上增长,从而排除了将所有元素移近顶部的需要。因此,如果您这样做,pop
方法应该只是:
public E pop() throws EmptyStackException {
if(count == 0) {
throw new EmptyStackException();
}
return data[--count];
}
答案 1 :(得分:1)
我建议你看看Stack类是如何实现的。它也使用一个数组,但是有一个大小字段,可以保持堆栈的大小。
如果大小变得大于数组的长度,则只需要更改数组。
来自Stack.pop();
public synchronized E pop() {
E obj;
int len = size();
obj = peek();
removeElementAt(len - 1);
return obj;
}
BTW在堆栈中,您永远不需要重新排列元素。你应该从最后添加/删除。
在你的情况下,你可以写。
public int size() { return size; }
public void push(E e) {
if (size == data.length) growArray();
data[size++] = e;
}
public E pop() {
if (size == 0) throw new EmptyStackException();
return data[--size];
}
答案 2 :(得分:0)
下面是用户定义的堆栈代码的内部实现方式。
class UserDefinedStack< E> {
private static int defaultCapacity = 5;
private E[] stack = null;
int top = -1;
/**
* The default constructor will create stack of type with default size 5
*
*/
UserDefinedStack() {
stack = (E[]) new Object[defaultCapacity];
}
/**
* constructs a stack with initial size
*
* @param defaultSize is the size of the stack
*/
UserDefinedStack(int defaultCapacity) {
this.defaultCapacity = defaultCapacity;
stack = (E[]) new Object[defaultCapacity];
}
public void push(E element) {
top = top + 1;
if (defaultCapacity == top) {
//System.err.println("Stack is Full!...");
// System.out.println("re-creating new resizable Array!..");
stack = constructsResizableArray(stack, defaultCapacity);
//throw new RuntimeException("Statck Full!...");
}
stack[top] = element;
}
/**
* This method will remove the top of the element and return
*
* @return <tt>E</tt> returns top of the element
*
*/
public E pop() {
if (top == -1) {
System.out.println("Stack is Empty!...");
throw new RuntimeException("Statck Empty!...");
}
E e = stack[top];
stack[top] = null;
top--;
return e;
}
/**
* This method will return top of the element and without remove
*
* @param <E> the type of element to insert
* @return <tt>E</tt> returns top of the element
*
*/
public E peek() {
if (top == -1) {
System.out.println("Stack is Empty!...");
throw new RuntimeException("Statck Empty!...");
}
E e = stack[top];
return e;
}
public E[] constructsResizableArray(E[] stack, int defaultCapacity) {
UserDefinedStack.defaultCapacity = defaultCapacity * 2;
E[] newstack = (E[]) new Object[UserDefinedStack.defaultCapacity];
int i = 0;
for (E e : stack) {
newstack[i] = e;
i++;
}
stack = null;
//System.out.println("New Array returned back");
return newstack;
}
/**
* Iterate the stack over the elements
*
*/
public void iterateStack() {
for (E e : stack) {
System.out.print("::" + e);
}
}
public long size() {
return top + 1;
}
public long capacity() {
return this.stack.length;
}
}
StackIntTest
import java.util.ArrayList;
import java.util.Date;
import java.util.Stack;
/**
*
* @author rajasekhar.burepalli
*/
public class StackIntTest {
public static void main(String[] args) {
System.out.println("Using Customized Stack!...............");
Date startDate = new Date();
System.out.println("StartTime:::" + startDate);
UserDefinedStack< Integer> is = new UserDefinedStack<>();
for (int i = 1; i < 1212121; i++) {
is.push(i);
}
System.out.println("Size::::::" + is.size() + "---Capacity:::" + is.capacity());
System.out.println("end Time::" + new Date());
System.out.println("Using java.util.Stack!...............");
System.out.println("StartTime:::" + startDate);
Stack< Integer> is1 = new Stack<>();
for (int i = 1; i < 1212121; i++) {
is1.push(i);
}
System.out.println("end Time::" + new Date());
System.out.println("Size::::::" + is1.size() + "---Capacity:::" + is1.capacity());
System.out.println("Using java.util.ArrayList!...............");
System.out.println("StartTime:::" + startDate);
ArrayList< Integer> al = new ArrayList<>();
for (int i = 1; i < 1212121; i++) {
al.add(i);
}
System.out.println("end Time::" + new Date());
System.out.println("Size::::::" + al.size());
}
}
答案 3 :(得分:0)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//CREATING A STACK USING ARRAYS
//EXAMPLE
//CREATE AN ARRAY
int[] numbers = new int[5];
//Create a variable to find out the current elements position
int pointer = 0 ;
//Add elements to the array
for (int i = 0 ; i < numbers.length ; i++){
numbers[pointer++] = input.nextInt();
}
//Last In first Out
//Create a variable to store the removed element
int temp;
for (int i = 0; i < numbers.length; i++) {
pointer -= 1;
temp = numbers[pointer];
numbers[pointer] = 0 ;
System.out.println(temp);
}
}