Modify the code of Figure 4.18 to support storing variable-length strings of at most 255 characters. The
Question:
Modify the code of Figure 4.18 to support storing variable-length strings of at most 255 characters.
The stack array should have type char. A string is represented by a series of characters (one character per stack element), with the length of the string stored in the stack element immediately above the string itself, as illustrated by Figure 4.32.
The push operation would store an element requiring i storage units in the i positions beginning with the current value of top and store the size in the position i storage units above top.
The value of top would then be reset above the newly inserted element. The pop operation need only look at the size value stored in position top - 1 and then pop off the appropriate number of units. You may store the string on the stack in reverse order if you prefer, provided that when it is popped from the stack, it is returned in its proper order.
Step by Step Answer:
Practical Introduction To Data Structures And Algorithm Analysis Java Edition
ISBN: 9780136609117
1st Edition
Authors: Clifford A. Shaffer