Topic(s): Organization and Implementation of Sequence Data Structures, including Vectors, Linked Lists, Stacks, and Queues
Related Reading: Class notes and Chapter 4
Following is a sequence of stack operations, using the List interface from Shaffer's text.
L2.append(10); L2.append(20); L2.insert(39); L2.append(15); L2.moveToStart(); L2.next(); L2.insert(12);
Similar to problem #1, following is a sequence of stack operations, using the Stack interface from Shaffer's text.
S1.push (12); S1.push (-5); Int t1 = S1.pop(); S1.push (-8); S1.push (20); Int t2 = S1.topValue(); S1.pop (); S1.push (4);
Similar to problem #1, following is a sequence of queue operations, using the Queue interface from Shaffer's text.
Q1.enqueue (9); Q1.enqueue (2); Int t1 = Q1.frontValue (); Q1.enqueue (-6); Q1.dequeue (); Q1.enqueue (14); Int t2 = Q1.dequeue (); Q1.enqueue (3);
In the text, Shaffer discusses two implementations of the Queue ADT, with either an array or a linked list. The linked list implementation is straightforward and efficient, but in order to achieve efficiency in an array implementation, it is necessary to implement it as a circular array. In such a case, separate "front" and "rear" indices are maintained, that are incremented and move circularly around the array, such that the next index (after an enqueue or dequeue operation, which increments the front or rear indices, respectively) is computed as follows:
i = (i + 1) % capacity;where 'capacity' is the allocated size of the array (and the maximum number of elements that can be stored in the queue).
Assume you've got such a circular array, with a capacity of 10. Imagine that the even integer numbers from 2 to 24 are added to and removed from the queue such that numbers are added in groups of three, followed by one nmber being removed afterwards. For example, 2, 4, and 6 would be added, then one number removed, followed by the next three numbers being added, then one removed, and so on.
Assuming that the circular array is initially empty, with "front" and "rear" both point to index 0, show the state of the circular array (i.e. the internal state of the queue) at the end, indicating in each of the 10 array positions the number it contains (or nothing, if empty) and the final position of the "front" and "rear" pointers.
Exercise #4.19, part (a), from Section 4.6 of the Shaffer text (p. 147).
Using a stack, you can "match" left and right parentheses by adding to the stack when you encounter a left parentheses, and removing from the stack when you find a right parentheses. You may use the C++ stack, or Shaffer's Stack, whichever you prefer.
You should implement your solution as a function that takes a string input argument and returns a bool result. Assume that the string can be any series of characters -- such as an equation like 3 * (x + 4 / (9 - y)) -- and your method should ignore all characters expect the left, '(', and right, ')', parentheses.
Provide a main() function that includes a number of test cases. Make sure your code works for a variety of positive examples (those with an equal number of left and right parens) and negative examples (those with mismatching numbers of left and right parens -- be sure to test cases with both extra left parens and extra right parens).