Exercise · Volume 1 · 2.2.4-4
Exercise 2.2.4-4
not stated yet in Lean solution written
| Rating | 20 |
|---|---|
| Kind | K5, design an algorithm: give a method for a task, often with a cost bound from the written solution |
| Section | Section 2.2.4 of Volume 1 |
The task
With representation (4), where the list has a list head, give the insertion and deletion that make the list work as a stack.
The exact wording is in your copy of the book.
Hints
Open one hint at a time. Try the exercise again after each.
Hint 1
The place where a stack changes is next to the list head.
Solution
Show the solution
Let be the address of the list head. The stack is the circle where is the top. It is empty when .
To push the item : take , set , then and .
To pop into : if , the stack is empty and we have underflow. Otherwise , , and .
No special case for the empty list is needed in the push, and none in the pop apart from the underflow test, which is the reason for the list head.
Check
Push and pop three times. The items come out in the order . After the last pop again.
Remarks
The same two operations at the other end need a pointer to the last node, which is why the text keeps at the right end for queues.
This solution is drafted, and a person has not reviewed it yet. The source.
In Lean
not stated yet The exercise is not stated in Lean yet.
The status is computed from the Lean build. The node of the map.
Something is wrong? Report it.