AuD
Lecture 'Algorithmen und Datenstrukturen' (code examples)
aud.example.VerboseStack< T > Class Template Reference

A stack that outputs messages on push and pop. More...

+ Inheritance diagram for aud.example.VerboseStack< T >:
+ Collaboration diagram for aud.example.VerboseStack< T >:

Public Member Functions

 VerboseStack ()
 
pop () throws NoSuchElementException
 Pop element from stack. More...
 
void push (T x)
 Push x onto stack. More...
 
String toString ()
 Get string representation "|a|b|c". More...
 
- Public Member Functions inherited from aud.Stack< T >
 Stack ()
 
boolean is_empty ()
 Is stack empty? More...
 
top () throws NoSuchElementException
 Get stack top. More...
 
pop () throws NoSuchElementException
 Pop element from stack. More...
 
void push (T x)
 Push x onto stack. More...
 
abstract boolean is_empty ()
 Is stack empty? More...
 
abstract T top ()
 Get stack top. More...
 
abstract T pop ()
 Pop element from stack. More...
 
abstract void push (T x)
 Push x onto stack. More...
 
String toString ()
 Get string representation "|a|b|c". More...
 

Additional Inherited Members

- Protected Member Functions inherited from aud.adt.AbstractStack< T >
 AbstractStack ()
 create empty stack More...
 

Detailed Description

A stack that outputs messages on push and pop.

On push (pop) print the pushed (popped) entry to System.err. The printed message is indented by the height of the stack.

You can replace Stack in any class by VerboseStack to trace modifications of the stack. This is useful, e.g., for comparing iterative and recursive implementations of algorithms.

Definition at line 15 of file VerboseStack.java.

Constructor & Destructor Documentation

◆ VerboseStack()

Definition at line 19 of file VerboseStack.java.

19{}

Member Function Documentation

◆ pop()

T aud.example.VerboseStack< T >.pop ( ) throws NoSuchElementException

Pop element from stack.

Requires !is_empty().

Exceptions
NoSuchElementException
Returns
removed element

Reimplemented from aud.Stack< T >.

Definition at line 21 of file VerboseStack.java.

21 {
22 T x=super.pop();
23 --height_;
24 print("pop "+x);
25 return x;
26 }

Referenced by aud.example.IterativePreorderTraversal.iterative_traversal().

+ Here is the caller graph for this function:

◆ push()

void aud.example.VerboseStack< T >.push ( x)

Push x onto stack.

Parameters
xnew element

Reimplemented from aud.Stack< T >.

Definition at line 28 of file VerboseStack.java.

28 {
29 super.push(x);
30 print("push "+x);
31 ++height_;
32 }

Referenced by aud.example.IterativePreorderTraversal.iterative_traversal().

+ Here is the caller graph for this function:

◆ toString()

String aud.example.VerboseStack< T >.toString ( )

Get string representation "|a|b|c".

(Here, "c" is stack top. "|" denotes empty stack.)

Reimplemented from aud.adt.AbstractStack< T >.

Definition at line 41 of file VerboseStack.java.

41 {
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 }
boolean is_empty()
Is stack empty?
Definition: Stack.java:14
String toString()
Get string representation "|a|b|c".

References aud.Stack< T >.is_empty(), and aud.example.VerboseStack< T >.toString().

Referenced by aud.example.VerboseStack< T >.toString().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

The documentation for this class was generated from the following file: