Chapter Questions
Draw pictures showing how the array below appears in a machine's memory when stored in row major order and in column major order: $$\begin{array}{|c|c|c|c|}\hline A & B & C & D \\\hline E & F & G & H \\\hline \mathrm{I} & \mathrm{J} & \mathrm{K} & \mathrm{L} \\\hline\end{array}$$
Suppose a homogeneous array with six rows and eight columns is stored in row major order starting at address 20 (base ten). If each entry in the array requires only one memory cell, what is the address of the entry in the third row and fourth column? What if each entry requires two memory cells?
Rework Problem 2 assuming column major order rather than row major order.
What complications are imposed if one tries to implement a dynamic list using a traditional one-dimensional homogeneous array?
Describe a method for storing three-dimensional homogeneous arrays. What address polynomial would be used to locate the entry in the ith plane, $j$ th row, and the $k$ th column?
Suppose the list of letters A, B, C, E, F, and G is stored in a contiguous block of memory cells. What activities are required to insert the letter D in the list, assuming that the list's alphabetical order is to be maintained?
The following table represents the contents of some cells in a computer's main memory along with the address of each cell represented. Note that some of the cells contain letters of the alphabet, and each such cell is followed by an empty cell. Place addresses in these empty cells so that each cell containing a letter together with the following cell form an entry in a linked list in which the letters appear in alphabetical order. (Use zero for the NIL pointer.) What address should the head pointer contain?$$\begin{array}{cc}\text { Address } & \text { Contents } \\11 & \mathrm{C} \\12 & \\13 & \mathrm{G} \\14 & \\15 & \mathrm{E} \\16 & \\17 & \mathrm{~B} \\18 & \\19 & \mathrm{U} \\20 & \\21 & \mathrm{~F} \\22 &\end{array}$$
The following table represents a portion of a linked list in a computer's main memory. Each entry in the list consists of two cells: The first contains a letter of the alphabet; the second contains a pointer to the next list entry. Alter the pointers so that the letter $\mathrm{N}$ is no longer in the list. Then replace the letter $\mathrm{N}$ with the letter $\mathrm{G}$ and alter the pointers so that the new letter appears in the list in its proper place in alphabetical order.$$\begin{array}{cc}\text { Address } & \text { Contents } \\30 & \mathrm{~J} \\31 & 38 \\32 & \mathrm{~B} \\33 & 30 \\34 & \mathrm{X} \\35 & 46 \\36 & \mathrm{~N} \\37 & 40 \\38 & \mathrm{~K} \\39 & 36 \\40 & \mathrm{P} \\41 & 34\end{array}$$
The table below represents a linked list using the same format as in the preceding problems. If the head pointer contains the value 44 , what name is represented by the list? Change the pointers so that the list contains the name Jean.$$\begin{array}{cc}\text { Address } & \text { Contents } \\40 & \mathrm{~N} \\41 & 46 \\42 & \mathrm{I} \\43 & 40 \\44 & \mathrm{~J} \\45 & 50 \\46 & \mathrm{E} \\47 & 00 \\48 & \mathrm{M} \\49 & 42 \\50 & \mathrm{~A} \\51 & 40\end{array}$$
Which of the following routines correctly inserts NewEntry immediately after the entry called PreviousEntry in a linked list? What is wrong with the other routine?Routine 1:1. Copy the value in the pointer field of PreviousEntry into the pointer field of NewEntry.2. Change the value in the pointer field of PreviousEntry to the address of NewEntry.Routine 2:1. Change the value in the pointer field of PreviousEntry to the address of NewEntry.2. Copy the value in the pointer field of PreviousEntry into the pointer field of NewEntry.
Design a procedure for concatenating two linked lists (that is, placing one before the other to form a single list).
Design a procedure for combining two sorted contiguous lists into a single sorted contiguous list. What if the lists are linked?
Design a procedure for reversing the order of a linked list.
a. Design an algorithm for printing a linked list in reverse order using a stack as an auxiliary storage structure.b. Design a recursive procedure to perform this same task without making explicit use of a stack. In what form is a stack still involved in your recursive solution?
Sometimes a single linked list is given two different orders by attaching two pointers to each entry rather than one. Fill in the table below so that by following the first pointer after each letter one finds the name Carol, but by following the second pointer after each letter one finds the letters in alphabetical order. What values belong in the head pointer of each of the two lists represented?$$\begin{array}{cc}\text { Address } & \text { Contents } \\60 & \mathrm{O} \\61 & \\62 & \\63 & \mathrm{C} \\64 & \\65 & \\66 & \mathrm{~A} \\67 & \\68 & \mathrm{~L} \\69 & \\70 & \mathrm{R} \\71 & \\72 &\end{array}$$
The table below represents a stack stored in a contiguous block of memory cells, as discussed in the text. If the base of the stack is at address 10 and the stack pointer contains the value 12 , what value is retrieved by a pop instruction? What value is in the stack pointer after the pop operation?$$\begin{array}{cc}\text { Address } & \text { Contents } \\10 & \mathrm{~F} \\11 & \mathrm{C} \\12 & \mathrm{~A} \\13 & \mathrm{~B} \\14 & \mathrm{E}\end{array}$$
Draw a table showing the final contents of the memory cells if the instruction in Problem 16 had been to push the letter $\mathrm{D}$ on the stack rather than to pop a letter. What would the value in the stack pointer be after the push instruction?
Design a procedure to remove the bottom entry from a stack so that the rest of the stack is retained. You should access the stack using only push and pop operations. What auxiliary storage structure should be used to solve this problem?
Design a procedure to compare the contents of two stacks.
Suppose you were given two stacks. If you were only allowed to move entries one at a time from one stack to another, what rearrangements of the original data would be possible? What arrangements would be possible if you were given three stacks?
Suppose you were given three stacks and you were only allowed to move entries one at a time from one stack to another. Design an algorithm for reversing two adjacent entries on one of the stacks.
Suppose we want to create a stack of names that vary in length. Why is it advantageous to store the names in separate areas of memory and then build the stack out of pointers to these names rather than allowing the stack to contain the names themselves?
Does a queue crawl through memory in the direction of its head or its tail?
Suppose you wanted to implement a "queue" in which new entries had priorities associated with them. Thus a new entry should be placed in front of those entries with lower priorities. Describe a storage system for implementing such a "queue" and justify your decisions.
Suppose the entries in a queue require one memory cell each, the head pointer contains the value 11 , and the tail pointer contains the value 17. What are the values of these pointers after one entry is inserted and two are removed?
a. Suppose a queue implemented in a circular fashion is in the state shown in the diagram below. Draw a diagram showing the structure after the letters $G$ and $R$ are inserted, three letters are removed, and the letters $\mathrm{D}$ and $\mathrm{P}$ are inserted.b. What error occurs in part (a) if the letters $G, R, D$, and $P$ are inserted before any letters are removed?
Describe how an array could be used to implement a queue in a program written in a highlevel language.
Suppose you were given two queues and you were only allowed to move one entry at a time from the head of a queue to the tail of either. Design an algorithm for reversing two adjacent entries in one of the queues.
The table below represents a tree stored in a machine's memory. Each node of the tree consists of three cells. The first cell contains the data (a letter), the second contains a pointer to the node's left child, and the third contains a pointer to the node's right child. A value of 0 represents a NIL pointer. If the value of the root pointer is 55 , draw a picture of the tree.$$\begin{array}{cc}\text { Address } & \text { Contents } \\40 & G \\41 & 0 \\42 & 0\end{array}$$$$\begin{array}{cc}\text { Address } & \text { Contents } \\43 & \mathrm{X} \\44 & 0 \\45 & 0 \\46 & \mathrm{~J} \\47 & 49 \\48 & 0 \\49 & \mathrm{M} \\50 & 0 \\51 & 0 \\52 & \mathrm{~F} \\53 & 43 \\54 & 40 \\55 & \mathrm{~W} \\56 & 46 \\57 & 52\end{array}$$
The table below represents the contents of a block of cells in a computer's main memory. Note that some of the cells contain letters of the alphabet, and each of those cells is followed by two blank cells. Fill in the blank cells so that the memory block represents the tree that follows. Use the first cell following a letter as the pointer to that node's left child and the next cell as the pointer to the right child. Use 0 for NIL pointers. What value should be in the root pointer?$$\begin{array}{cc}\text { Address } & \text { Contents } \\30 & \mathrm{C} \\31 & \\32 & \\33 & \mathrm{H} \\34 & \\35 & \\36 & \mathrm{~K} \\37 & \\38 & \mathrm{E} \\39 & \\40 & \mathrm{G} \\41 & \\42 & \mathrm{P} \\43 & \\44 & \\45 & \\46 & \\47 &\end{array}$$
Design a nonrecursive algorithm to replace the recursive one represented in Figure 8.21 .
Design a nonrecursive algorithm to replace the recursive one represented in Figure 8.24. Use a stack to control any backtracking that might be necessary.
Apply the recursive tree-printing algorithm of Figure 8.24. Draw a diagram representing the nested activations of the algorithm (and the current position in each) at the time node $\mathrm{X}$ is printed.
While keeping the root node the same and without changing the physical location of the data elements, change the pointers in the tree of Problem 29 so the tree-printing algorithm of Figure 8.24 prints the nodes alphabetically.
Draw a diagram showing how the binary tree below appears in memory when stored without pointers using a block of contiguous memory cells as described in Section 8.3.
Suppose the contiguous cells representing a binary tree as described in Section 8.3 contained the values A, B, C, D, E, F, and F, respectively. Draw a picture of the tree.
Give an example in which you might want to implement a list (the conceptual structure) as a tree (the actual underlying structure). Give an example in which you might want to implement a tree (the conceptual structure) as a list (the actual underlying structure).
The linked tree structures discussed in the text contained pointers that allowed one to move down the tree from parents to children. Describe a pointer system that would allow movement up the tree from children to parents. What about movement among siblings?
Describe a data structure suitable for representing a board configuration during a chess game.
Identify the trees below whose nodes would be printed in alphabetical order by the algorithm in Figure 8.24.
Modify the procedure in Figure 8.24 to print the "list" in reverse order.
Describe a tree structure that can be used to store the genealogical history of a family. What operations are performed on the tree? If the tree is implemented as a linked structure, what pointers should be associated with each node? Design procedures to perform the operations you identified above, assuming that the tree is implemented as a linked structure with the pointers you just described. Using your storage system, explain how one could find all the siblings of a person.
Design a procedure for finding and deleting a given value from a tree stored in the fashion of Figure 8.20.
In the traditional implementation of a tree, each node is constructed with a separate pointer for each possible child. The number of such pointers is a design decision and represents the maximum number of children any node can have. If a node has fewer children than pointers, some of its pointers are simply set to NIL. But such a node can never have more children than pointers. Describe how a tree could be implemented without limiting the number of children a node could have.
Using the define type pseudocode statement introduced in Section 8.5, define a userdefined data type representing data regarding an employee of a company (such as name, address, job assignment, pay scale, and so on).
Using the define type pseudocode statement introduced in Section 8.5, sketch a definition of an abstract data type representing a list of names. In particular, what structure would contain the list and what procedures would be provided to manipulate the list? (You do not need to include detailed descriptions of the procedures.)
Using the define type pseudocode statement introduced in Section 8.5, sketch a definition of an abstract data type representing a queue. Then give pseudocode statements showing how instances of that type could be created and how entries could be inserted in and deleted from those instances.
a. What is the difference between a userdefined data type and a primitive data type?b. What is the difference between an abstract data type and a user-defined data type?
Identify the data structures and procedures that might appear in an abstract data type representing an address book.
Identify the data structures and procedures that might appear in an abstract data type representing a simple spacecraft in a video game.
Modify Figure 8.27 so that the class defines a queue rather than a stack.
In what way is a class more general than a traditional abstract data type?
Using instructions of the form DR0S and ER0S as described at the end of Section 8.7, write a complete machine language routine to push an entry onto a stack implemented as shown in Figure 8.12. Assume that the stack pointer is in register $\mathrm{F}$ and that the entry to be pushed is in register 5 .
Suppose each entry in a linked list consists of one memory cell of data followed by a pointer to the next list entry. Moreover, suppose that a new entry located at memory address $A 0$ is to be inserted between the entries at locations B5 and C4. Using the language described in Appendix $\mathrm{C}$ and the additional op-codes $\mathrm{D}$ and $\mathrm{E}$ as described at the end of Section 8.7, write a machine-language routine to perform the insertion.
What advantages does an instruction of the form DR0S as described in Section 8.7 have over an instruction of the form DRXY? What advantage does the form DRXS as described in Question/Exercise 4 of Section 8.7 have over the form DROS?