AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
VerboseStack.java
Go to the documentation of this file.
1package aud.example;
2
3import java.util.NoSuchElementException;
4import aud.Stack;
5
15public class VerboseStack<T> extends Stack<T> {
16
17 int height_ = 0;
18
19 public VerboseStack() {}
20
21 @Override public T pop() throws NoSuchElementException {
22 T x=super.pop();
23 --height_;
24 print("pop "+x);
25 return x;
26 }
27
28 @Override public void push(T x) {
29 super.push(x);
30 print("push "+x);
31 ++height_;
32 }
33
34 private void print(String message) {
35 for (int i=0;i<height_;++i)
36 System.err.print(" ");
37 System.err.println(message+" => "+toString());
38 }
39
40 @Override
41 public String toString() {
42 if (!is_empty()) {
43 T x=super.pop();
44 String bottom=toString();
45 super.push(x);
46 return (bottom+(bottom.length()>1 ? "|":""))+x;
47 }
48 else
49 return "|";
50 }
51
52};
Implementation of a stack based on aud.Vector.
Definition: Stack.java:8
boolean is_empty()
Is stack empty?
Definition: Stack.java:14
A stack that outputs messages on push and pop.
String toString()
Get string representation "|a|b|c".
T pop()
Pop element from stack.
void push(T x)
Push x onto stack.
AuD lecture: Data structures, algorithms, examples.