This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: We denote T1 as top for the rst stack and T2 as top for the second stack. The algorithm is like this: Init() { T1=1; T2=n; } The following procedure checks for stack overflow: int isFull() { return (T2T1==1); } Problem 2: Grade 5 pts You have a character string as input to your program. You need to nd out whether the string is of the form a C b. Here C is the letter C, a and b are sequences of As and Bs, and a and b must be mirror images of each other, e.g., a = ABAAB, b = BAABA. For full grade you must give an O(N) algorithm where N is the number of letters in the input string. Keep two variables T1 and T2. T1 is an index to the rst character of the string and T2 is an index to the last character of the string. Compare the characters that correspond to positions T1 and T2. If they do not match, then the input is not of the desired form. If they match, then you increment T1 and decrement T2 and follow the same rule. The algorithm terminates when both T1 and T2 nd character C. Since this scheme traverses the whole input string, is O ( n ). Problem 3: Grade 10 pts You have two sorted lists, L 1 ; L 2 : You know the lengths of each list, L 1 has length N 1 and L 2 has length N 2 : Design algorithms to output a sorted list L 1 \ L 2 : You need to give two algorithms. One should have complexity O ( N 1 + N 2...
View
Full Document
 Spring '09
 Algorithms, Data Structures, The Return, OTH, 12rst element t, element t position, 12rst stk nd

Click to edit the document details