Stack: Difference between revisions
Content deleted Content added
Added perl and prolog |
No edit summary |
||
Line 748: | Line 748: | ||
(else 'badmsg))))) |
(else 'badmsg))))) |
||
</lang> |
</lang> |
||
=={{header|Tcl}}== |
|||
Here's a simple implementation using a list: |
|||
<lang tcl>proc push {stackvar value} { |
|||
upvar 1 $stackvar stack |
|||
lappend stack $value |
|||
} |
|||
proc pop {stackvar} { |
|||
upvar 1 $stackvar stack |
|||
set value [lindex $stack end] |
|||
set stack [lrange $stack 0 end-1] |
|||
return $value |
|||
} |
|||
proc size {stackvar} { |
|||
upvar 1 $stackvar stack |
|||
llength $stack |
|||
} |
|||
proc empty {stackvar} { |
|||
upvar 1 $stackvar stack |
|||
expr {[size stack] == 0} |
|||
} |
|||
proc peek {stackvar} { |
|||
upvar 1 $stackvar stack |
|||
lindex $stack end |
|||
} |
|||
set S [list] |
|||
empty S ;# ==> 1 (true) |
|||
push S foo |
|||
empty S ;# ==> 0 (false) |
|||
push S bar |
|||
peek S ;# ==> bar |
|||
pop S ;# ==> bar |
|||
peek S ;# ==> foo</lang> |
|||
There is a package in [http://tcllib.sourceforge.net/ tcllib] called <code>struct::stack</code> that presents an object interface: |
|||
<lang tcl>package require struct::stack |
|||
struct::stack S |
|||
S size ;# ==> 0 |
|||
S push a b c d e |
|||
S size ;# ==> 5 |
|||
S peek ;# ==> e |
|||
S pop ;# ==> e |
|||
S peek ;# ==> d |
|||
S pop 4 ;# ==> d c b a |
|||
S size ;# ==> 0</lang> |
|||
=={{header|UnixPipes}}== |
=={{header|UnixPipes}}== |
||
Uses moreutils |
Uses moreutils |