Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Class that implements a stack of integers
- * by a linked list of integers.
- */
- public class StackByLinkedList implements Stack {
- /** head node of the linked list */
- LinkedListNode myHead;
- /**
- * Check if the list is empty
- * @return 'true' if empty, 'false' otherwise
- */
- public boolean empty(){
- return (myHead == null);
- }
- /**
- * Get the value that is on the top of the stack
- * @return the top value
- */
- public int top(){
- return myHead.value();
- }
- /**
- * Add a value at the top of the stack
- * @param aValue new value
- */
- public void push(int aValue){
- LinkedListNode temp = new LinkedListNode(aValue, myHead);
- myHead = temp;
- }
- /**
- * Remove the value that is on the top of the stack
- */
- public void pop(){
- myHead = myHead.next();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement