Dept. of Computer Science and Information Engineering, National Dong Hwa University Data Structures - Middle Exam, Nov. 8, 2010 1.

(A band matrix a is an n-by-n array in which a[i][j]==0, if |i-j|≧k , where k is a positive integer constant and 1 ≦ k ≦ n-1 . a.

(05%) What is the maximum number of nonzero elements in such a matrix? Write your answer in terms of n and k. Ans: X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

X

…

X

X

X

…

…

…

…

n-by-n 的矩陣中，對角線上有 n 個元素，對角線旁的兩條線上有 n-1 個 元素，再往兩旁則遞減，一直到最邊緣處是 n-k+1 個，所以總共有 2( n + (n-1) + (n-2) + … +(n-k+1) ) – n = k(2n-k+1) – n b.

(15%) Suppose that these elements (nonzero elements) of the matrix is stored in a one-dimensional array b with the row-major way sequentially. Develop an algorithm for mapping a[i][j] to array b, where |i-j|<k=2. Note that the first element of array a is a[0][0], which is stored in b[0]. That is a[0][0]=b[0], a[0][1]=b[1], and ….

Ans: b[0]

b[1]

b[2]

b[3]

b[4]

b[5]

b[6]

b[7]

b[8]

b[9]

b[10]

b[11] b[12] b[13] …

…

…

…

…

a[i][j] = b[ 3*(i-1)+2+(j-i)+1 ] = b[2i+j]

2.

(10%)Transform the prefix expression * + A / B C + D * / - E F G H to infix and postfix expressions. Draw its expression tree. Ans: infix: ( A + ( B / C ) ) * ( D + ( ( ( E - F ) / G ) * H ) ) postfix: A B C / + D E F – G / H * + * expression tree:

*

+ A

+ /

B

D C

* /

E 3.

H G

F

(10%)Assume that A = 2, B = 4, C = 5 and D = 3. Compute the postfix expression AB*C+ADC-*+ with the help of a stack. Please explain your algorithm and illustrate it with the example (Do not write a program.). Ans: 讀到數字就 push 進 stack 遇到運算符號（＋－× /）就將 stack 中最後兩數 pop 出來做運算 將運算後的結果 push 回 stack

將最後的答案 pop 出來

4.

(05%) (a) In a circular queue with array implementation, where do the pointers front and rear point to? Ans: n n-1 … 2 1

E …

←rear

A ←front

0 front 指向第一筆資料的前面 (沒資料) rear 指向最後一筆資料

(有資料)

(05%) (b) In a circular queue with array implementation, what are the conditions of an empty queue and a full queue? Ans: n-1 n-2 ←front ←rear … 2 1 0 empty queue: front = rear 的時候 F …

E …

2

A ←front

1 0

n-1 n-2

G

←rear

full queue: (rear +1) mod n = front 的時候

5.

(10%) Convert the following polynomial into the generalized list which its node type like vble/*down/coef

exp

*next

((x10 + 2x8) y + 3x8y2) z2 + ((x4 + 6x3) y4 +2y ) z

Ans

6.

Given the doubly linked list node structure following LLink

ITEM

RLink

LLink: pointer to previous node, RLink: pointer to next node. a. (07%)Please draw a diagram to show the actions of insertion and deletion. Ans Insertion

Deletion

b.

(08%)Please write a code segment to implemaent insertion and deletion. Ans Insertion p -> LLink = x ; //(1) p -> RLink = x ->RLink ; //(2) x ->RLink -> LLink = p ; //(3)

x ->RLink = p //(4) deletion x ->LLink ->RLink = x ->RLink ; //(1) x ->RLink ->LLink = x ->LLink ; //(2) c.

(10%) Write a C/C++ function to compute the minimum of all elements of a doubly linked list of integers. Ans LenS(S:pointer to a doubly linked list){ Courrent = Head->Rlink; Min_item = Coorrent->item; While(Courrent!=Head){ Courent = Courent->rlink; If (Courrent-->item < Min_item) Min_item = Courrent-->item; } Return Min_item; }

7.

Suppose that we are given the post-order sequence ＤＢＧＥＨＪＦＣＡ and the in-order sequence ＤＢＡＥＧＣＨＦＪof the same binary tree. a. (07%) Draw a binary tree defined by such a pair of sequence. Ans

b.

(08%) What is the pre-order of this tree ?

Ans Pre-order:ＡＢＤＣＥＧＦＨＪ

Advertisement