This document provides instructions for implementing mutable data structures in JavaScript. It asks the reader to write code to:
1. Add methods to a stack object implementation to check if a stack is empty, clear a stack, peek at the top element without removing it, push an element onto the stack, and pop the top element off the stack.
2. Write functions to push all elements of a list onto a stack and pop all elements off a stack into a list.
3. Implement a function to reverse a list using only the stack operations.
This document provides instructions for implementing mutable data structures in JavaScript. It asks the reader to write code to:
1. Add methods to a stack object implementation to check if a stack is empty, clear a stack, peek at the top element without removing it, push an element onto the stack, and pop the top element off the stack.
2. Write functions to push all elements of a list onto a stack and pop all elements off a stack into a list.
3. Implement a function to reverse a list using only the stack operations.
School of Computing CS1101S: Programming Methodology Semester I, 2014/2015 Recitation 8 Mutable Data Structures JavaScript 1. var x = <expression>; Assignment statement. Assign variable x to the result of evaluating <expression>. 2. set_head(p, x) Assign the head of pair p to x. 3. set_tail(p, x) Assign the tail of pair p to x. Problems: 1. Consider the following implementation of a stack object. function make_stack() { var stack = pair("stack", []); return stack; } Note that the stack is represented by a pair whose head is a tag "stack". The stack is represented by a list, which of course is initially empty. We can create a new stack using the program: var my_stack = make_stack(); (a) Add a method called is empty which returns true if the stack is empty. function is_empty(stack) { // enter your code here } (b) Add a method called clean which empties the stack of any elements it may contain. function clean(stack) { // enter your code here } (c) Add a method called peek which returns the top element of the stack, leaving the stack unchanged. If the stack is empty, signal a stack underow error. CS1101S, Semester I, 2014/2015Recitation 8 2 function peek(stack) { // enter your code here } (d) Add a method called push which allows an element to be added to the top of the stack. function push(stack, x) { // enter your code here } (e) Add a method called pop which removes and returns the top element of the stack. Remember to program defensively. function pop(stack) { // enter your code here } 2. Write a function called push all which takes a stack and a list and pushes all the elements of the list onto the stack. It should return the stack. function push_all(stack, lst) { // enter your code here } 3. Write a function called pop all which takes a stack and pops elements off it until it becomes empty, adding each element to the output list. function pop_all(stack) { // enter your code here } 4. Implement the reverse function only using the stack operations you have implemented so far. function reverse(lst) { }