【发布时间】:2015-06-14 16:59:05
【问题描述】:
下面这段代码的大符号是什么。我仍然无法完全掌握这个概念。 我应该从有经验的编码人员那里得到一个想法,以根据此代码对大 o 性能进行总结。
import java.util.*;
import java.util.InputMismatchException;
import javax.swing.*;
public class MyStack {
private int maxSize;
private long[] stackArray;
private int top;
public MyStack(int s) {
maxSize = s;
stackArray = new long[maxSize];
top = -1;
}
public void push(long j) {
stackArray[++top] = j;
System.out.println("Push onto stack");
}
public long pop() {
return stackArray[top--];
}
public long peek() {
return stackArray[top];
}
public boolean isEmpty() {
return (top == -1);
}
public boolean isFull() {
return (top == maxSize - 1);
}
public static void main(String[] args) {
Scanner num = new Scanner(System.in);
int input =0;
int x;
MyStack theStack = new MyStack(5);
for(x=0; x<5; x++)
{
System.out.println("\nEnter a number(Push): ");
input = num.nextInt();
theStack.push(input);
}
System.out.print("The first element on the top is the top of the stack");
System.out.println("");
while (!theStack.isEmpty()) {
long value = theStack.pop();
System.out.print(value);
System.out.println(" Pop");
}
System.out.println("");
}
}
【问题讨论】:
-
一个类没有大 O 复杂度。你有什么问题?
标签: java arrays performance stack notation