在任务中给出了堆栈,我应该实现3种方法:
一种告诉你堆栈中元素大小的方法
复制堆栈顶值的方法
反转堆栈/更改订单的方法
我已经成功实施了前两种方法,它们运作良好。
public int size() {
int count = 0;
for (Element element = top; element != null; element = element.next)
{
count++;
}
return count;
}
public void dupe() {
if (top == null)
{
throw new EmptyStackException();
}
push(top());
}
对于反向方法我遇到了问题,是否可以通过使用该堆栈来反转堆栈?这里不需要2个堆栈吗?
完整的代码在这里:
import java.util.EmptyStackException;
public class IntStack {
private Element top;
public IntStack() {
top = null;
}
public int pop() {
if (top == null)
throw new EmptyStackException();
int value = top.getValue();
top = top.getNext();
return value;
}
public int top() {
if (top == null)
throw new EmptyStackException();
return top.getValue();
}
public boolean isEmpty() {
return top == null;
}
public void push(int value) {
Element newTop = new Element(value);
newTop.setNext(top);
top = newTop;
}
public String toString() {
Element runingElement = top;
StringBuilder sb = new StringBuilder();
while (runingElement != null) {
sb.append(runingElement.getValue()).append("\n");
runingElement = runingElement.getNext();
}
return sb.toString();
}
public void dupe() {
if (top == null)
{
throw new EmptyStackException();
}
push(top());
}
public int size() {
int count = 0;
for (Element element = top; element != null; element = element.next)
{
count++;
}
return count;
}
public void reverse() {
}
答案 0 :(得分:1)
这是一种在不使用数组或其他堆栈的情况下反转堆栈的方法。当然,可能有更多的最佳方式来实现结果。本质上,堆栈的顶部将其下一个设置为null,并且它遍历每个元素,设置前一个节点旁边的元素。最后,它将内部顶部设置为最后一个元素。这种方法不是线程安全的。
public void reverse() {
if (isEmpty() || top.getNext() == null) {
return;
}
Element prev = top;
Element cur = top.getNext();
Element end = null;
// set the top of the stack to have it's next point to nothing
prev.setNext(null);
// while we have something to process, loop
while (cur != null) {
// keep the next from the current
Element tmp = cur.getNext();
// update the next to point to the previous
cur.setNext(prev);
// set the previous to where we are now
prev = cur;
// need to find the end; update to where we are now
end = cur;
// update our current to the temp; could be null
cur = tmp;
}
top = end;
}
将加载10个元素(0 - 9)的示例测试驱动程序:
public static void main(String[] args) {
IntStack stack = new IntStack();
// ensure reverse does nothing on empty stack
System.out.println("empty stack");
System.out.println(stack.toString());
System.out.println("Reversing.");
stack.reverse();
System.out.println(stack.toString());
// make sure does nothing on single entry
System.out.println("pushing 0");
stack.push(0);
System.out.println(stack.toString());
System.out.println("Reversing.");
stack.reverse();
System.out.println(stack.toString());
for (int i = 1; i < 10; ++i) {
System.out.println("Pushing " + i);
stack.push(i);
}
System.out.println("Stack after test load:");
System.out.println(stack.toString());
System.out.println("Printing size.");
System.out.println(stack.size() + "\n");
System.out.println("Reversing.");
stack.reverse();
System.out.println(stack.toString());
}
示例输出:
空堆
倒车。
推0 0
倒车。
0推1 推2 推3 推4 推5 推6
推7 推8 推9测试加载后堆叠:
9
8
7
6
5
4
3
2
1
0印刷尺寸 10
倒车。
0
1
2
3
4
5
6
7
8
9
答案 1 :(得分:1)
这是另一种反转堆栈的方法。
public void reverse() {
if( top == null || top.next == null) {
return; //do nothing
} if (top.next.next == null) {
Element last = top.next;
last.next = top;
last.next.next = null;
top = last;
} else {
Element secondLast = top;
Element last = top.next;
Element current = top.next.next;
secondLast.next = null;
while(true) {
last.next = secondLast;
if(current == null) {
top = last;
break;
}
secondLast = last;
last = current;
current = current.next;
}
}
}
注意:如果我们创建一个双向链表,这几乎可以轻松完成。