Data Str &amp; Algorithm HW Solutions 4

# Data Str &amp; Algorithm HW Solutions 4 - } 1.14...

This preview shows page 1. Sign up to view the full content.

4 Chap. 1 Data Structures and Algorithms 1.13 The f rst part is easy with the hint, but the second part is rather dif f cult to do without a stack. a) bool checkstring(string S) { int count = 0; for (int i=0; i<length(S); i++) if (S[i] == ’(’) count++; if (S[i] == ’)’) { if (count == 0) return FALSE; count--; } } if (count == 0) return TRUE; else return FALSE; } b) int checkstring(String Str) { Stack S; int count = 0; for (int i=0; i<length(S); i++) if (S[i] == ’(’) S.push(i); if (S[i] == ’)’) { if (S.isEmpty()) return i; S.pop(); } } if (S.isEmpty()) return -1; else return S.pop();
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } 1.14 Answers to this question are discussed in Section 7.2. 1.15 This is somewhat different from writing sorting algorithms for a computer, since persons working space is typically limited, as is their ability to physically manip-ulate the pieces of paper. Nonetheless, many of the common sorting algorithms have their analogs to solutions for this problem. Most typical answers will be insertion sort, variations on mergesort, and variations on binsort. 1.16 Answers to this question are discussed in Chapter 8....
View Full Document

## This note was uploaded on 12/27/2011 for the course MAP 2302 taught by Professor Bell,d during the Fall '08 term at UNF.

Ask a homework question - tutors are online