Skip to content

fixes #804 #1011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions xml/chapter3/section3/subsection2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,34 @@ delete_queue(q1);
</SPLITINLINE>
that takes a queue as input and prints the sequence of items in the queue.
<LABEL NAME="ex:3_21"/>
<SOLUTION>
<SNIPPET>
<REQUIRES>make_queue</REQUIRES>
<REQUIRES>modify_pointers</REQUIRES>
<REQUIRES>insert_queue</REQUIRES>
<REQUIRES>is_empty_queue</REQUIRES>
<REQUIRES>delete_queue</REQUIRES>
<EXAMPLE>ex_3_21_solution_example</EXAMPLE>
<JAVASCRIPT>
function print_queue(q) {
return display(head(q));
}
</JAVASCRIPT>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>ex_3_21_solution_example</NAME>
<JAVASCRIPT>
const q1 = make_queue();
print_queue(q1); // prints: null
insert_queue(q1, "a");
print_queue(q1); // prints: ["a", null]
insert_queue(q1, "b");
print_queue(q1); // prints: ["a", ["b", null]]
delete_queue(q1);
print_queue(q1); // prints: ["b", null]
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
</EXERCISE>

<EXERCISE>
Expand Down Expand Up @@ -786,6 +814,76 @@ function make_queue() {
and provide implementations of the queue operations using this
representation.
<LABEL NAME="ex:3_22"/>
<SOLUTION>
<SNIPPET>
<EXAMPLE>ex_3_22_example</EXAMPLE>
<JAVASCRIPT>
// provided by GitHub user devinryu

function make_queue() {
let front_ptr = null;
let rear_ptr = null;
function is_empty_queue() {
return is_null(front_ptr);
}
function insert_queue(item) {
let new_pair = pair(item, null);
if (is_empty_queue()) {
front_ptr = new_pair;
rear_ptr = new_pair;
} else {
set_tail(rear_ptr, new_pair);
rear_ptr = new_pair;
}
}
function delete_queue() {
if (is_empty_queue()) {
error("FRONT called with an empty queue");
} else {
front_ptr = tail(front_ptr);
}
}
function print_queue() {
display(front_ptr);
}
function dispatch(m) {
return m === "insert_queue"
? insert_queue
: m === "delete_queue"
? delete_queue
: m === "is_empty_queue"
? is_empty_queue
: m === "print_queue"
? print_queue
: error(m, "Unknow operation -- DISPATCH");
}
return dispatch;
}
function insert_queue(queue, item) {
return queue("insert_queue")(item);
}
function delete_queue(queue) {
return queue("delete_queue")();
}
function print_queue(queue) {
return queue("print_queue")();
}
</JAVASCRIPT>
</SNIPPET>
<SNIPPET HIDE="yes">
<NAME>ex_3_22_example</NAME>
<JAVASCRIPT>
const q = make_queue();
print_queue(q); // prints: null
insert_queue(q, "a");
print_queue(q); // prints: ["a", null]
insert_queue(q, "b");
print_queue(q); // prints: ["a", ["b", null]]
delete_queue(q);
print_queue(q); // prints: ["b", null]
</JAVASCRIPT>
</SNIPPET>
</SOLUTION>
</EXERCISE>

<EXERCISE>
Expand Down
Loading