/* -*- prolog -*- ****************************************************************************** File: stack.pro Description: The stack ADT. From Luger & Stubblefield. A single predicate, stack(), can be used to define the push, pop, and peek predicates, by varying the arguments to stack(). For example, where Stack is a list of the form [H|T], here is how you would define push(), pop(), and peek() in terms of stack(). push(X, Stack, NewStack) :- stack(X, Stack, NewStack), NewStack=[X|Stack]. pop(X, NewStack,Stack) :- stack(X, NewStack, Stack), Stack = [X|NewStack]. peek(X, Stack, Stack) :- stack(X, _, [X|Tail]), Stack = [X|Tail]. ****************************************************************************** */ empty_stack([]). stack(Top, Stack, [Top|Stack]). /* used for push, pop and peek */ member_stack(Element, Stack) :- member(Element,Stack). add_list_to_stack(List,Stack,Result) :- append(List,Stack,Result). reverse_print_stack(S) :- empty_stack(S). reverse_print_stack(S) :- stack(E,Rest,S), reverse_print_stack(Rest), write(E),nl. member(H,[H|T]). member(X,[_|T]) :- member(X,T).