在我开始之前,如果这很难读,我会道歉,它来自一本书并且我不允许改变它。我必须实现一个名为peek()的方法来检查堆栈的顶部,然后显示它。
我有reset()和size()工作,但为什么peek()无效?
interface ISimpleStack {
// Push a character onto the stack.
void push(char ch);
// Pop a character from the stack.
char pop();
// Return true if the stack is empty.
boolean isEmpty();
// Return true if the stack is full.
boolean isFull();
void reset();
char peek();
int size();
}
// A fixed-length stack for characters.
class FixedLengthStack implements ISimpleStack {
private char[] data; // this array holds the stack
private int tos; // index of top of stack
// Construct an empty stack given its size.
FixedLengthStack(int size) {
data = new char[size]; // create the array to hold the stack
tos = 0;
}
// Construct a stack from a stack.
FixedLengthStack(FixedLengthStack otherStack) {
// size of new stack equals that of otherStack
data = new char[otherStack.data.length];
// set tos to the same position
tos = otherStack.tos;
// copy the contents
for(int i = 0; i < tos; i++)
data[i] = otherStack.data[i];
}
// Construct a stack with initial values.
FixedLengthStack(char[] chrs) {
// create the array to hold the initial values
data = new char[chrs.length];
tos = 0;
// initialize the stack by pushing the contents
// of chrs onto it
for(char ch : chrs)
push(ch);
}
// Push a character onto the stack.
public void push(char ch) {
if(isFull()) {
System.out.println(" -- Stack is full.");
return;
}
data[tos] = ch;
tos++;
}
// Pop a character from the stack.
public char pop() {
if(isEmpty()) {
System.out.println(" -- Stack is empty.");
return (char) 0; // a placeholder value
}
tos--;
return data[tos];
}
// Return true if the stack is empty.
public boolean isEmpty() {
return tos==0;
}
// Return true if the stack is full.
public boolean isFull() {
return tos==data.length;
}
public void reset() {
tos = 0;
}
public char peek() {
if(isEmpty()) {
System.out.println(" -- Stack is empty.");
return (char) 0; // a placeholder value
}
return data[tos];
}
public int size() {
int size = 0;
for(int i = 0; i <= tos; i++){
size = i;
}
System.out.println("Size of stack is: " + size);
return size;
}
}
// A growable stack for characters.
class DynamicStack implements ISimpleStack {
private char[] data; // this array holds the stack
private int tos; // index of top of stack
// Construct an empty stack given its size.
DynamicStack(int size) {
data = new char[size]; // create the array to hold the stack
tos = 0;
}
// Construct a stack from a stack.
DynamicStack(DynamicStack otherStack) {
// size of new stack equals that of otherStack
data = new char[otherStack.data.length];
// set tos to the same position
tos = otherStack.tos;
// copy the contents
for(int i = 0; i < tos; i++)
data[i] = otherStack.data[i];
}
// Construct a stack with initial values.
DynamicStack(char[] chrs) {
// create the array to hold the initial values
data = new char[chrs.length];
tos = 0;
// initialize the stack by pushing the contents
// of chrs onto it
for(char ch : chrs)
push(ch);
}
// Push a character onto the stack.
public void push(char ch) {
// if there is no more room in the array,
// expand the size of the stack
if(tos == data.length) {
// double the size of the existing array
char[] t = new char[data.length * 2];
// copy the contents of the stack into the larger array
for(int i = 0; i < tos; i++)
t[i] = data[i];
// set data to refer to the new array
data = t;
}
data[tos] = ch;
tos++;
}
// Pop a character from the stack.
public char pop() {
if(isEmpty()) {
System.out.println(" -- Stack is empty.");
return (char) 0; // a placeholder value
}
tos--;
return data[tos];
}
// Return true if the stack is empty.
public boolean isEmpty() {
return tos==0;
}
// Return true if the stack is full. For DynamicStack,
// this method always returns false.
public boolean isFull() {
return false;
}
public void reset() {
tos = 0;
}
public char peek() {
if(isEmpty()) {
System.out.println(" -- Stack is empty.");
return (char) 0; // a placeholder value
}
return data[tos];
}
public int size() {
int size = 0;
for(int i = 0; i <= tos; i++){
size = i;
}
System.out.println("Size of stack is: " + size);
return size;
}
}
// Demonstrate ISimpleStack.
class ISimpleStackDemo {
public static void main(String[] args) {
int i;
char ch;
// create an ISimpleStack interface variable
ISimpleStack iStack;
// Now, construct a FixedLengthStack and a DynamicStack
FixedLengthStack fixedStack = new FixedLengthStack(10);
DynamicStack dynStack = new DynamicStack(5);
// first, use fixedStack through iStack
iStack = fixedStack;
// push characters onto fixedStack
for(i = 0; !iStack.isFull(); i++)
iStack.push((char) ('A'+i));
iStack.size();
System.out.print("Top of fixedStack: ");
while(!iStack.isEmpty()) {
ch = iStack.peek();
System.out.print(ch);
}
// pop characters off fixedStack
System.out.print("Contents of fixedStack: ");
while(!iStack.isEmpty()) {
ch = iStack.pop();
System.out.print(ch);
}
System.out.print("\nContents of fixedStack after reset: ");
iStack.reset();
ch = iStack.pop();
System.out.print(ch);
System.out.println();
// next, use dynStack through iStack
iStack = dynStack;
// push A through Z onto dynStack
// this will result in three increases in its size
for(i = 0; i < 26; i++)
iStack.push((char) ('A'+i));
iStack.size();
// pop characters off dynStack
System.out.print("Contents of dynStack: ");
while(!iStack.isEmpty()) {
ch = iStack.pop();
System.out.print(ch);
}
System.out.print("\nContents of dynStack after reset: ");
iStack.reset();
ch = iStack.pop();
System.out.print(ch);
}
}
答案 0 :(得分:0)
使用示例中的代码,对peek()进行以下更改可解决粘贴bin中第二个示例中提到的输出问题。这与FixedLengthStack中出现的问题相同。事实上,稍微好一点的实现可能是有一个抽象的Stack类,它实现了size(),reset(),pop(),(顺便说一句,应该也可以将数组设置为一个空数组),并查看()。
@Override
public char peek()
{
if (isEmpty()) {
System.out.println(" -- Stack is empty.");
return (char) 0; // a placeholder value
}
return data[tos - 1];
}