TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
Chapter03
1. Chapter 3 Stacks and Queues 3.1 Templates in C++ 3.2 The Stack Abstract Data Type 3.3 The Queue Abstract Data Type 3.4 Subtyping and Inheritance in C++ 3.5 A Mazing Problem 3.6 Evaluation of Expressions 3.7 Multiple Stacks and Queues
2.
3. A template may be viewed as a variable that can be instantiated to any data type, irrespective of whether this data type is a fundamental C++ type or a user-defined type.
5. 3.1 Templates in C++ template < class KeyType> void sort(KeyType *a, int n) { // 作非遞減排序 for ( int i = 0; i < n; i++) { int j = i; for ( int k = i + 1; k < n; k++) // 找出最小元素位置 if (a[k] < a[ j]) j = k; KeyType temp = a[i]; a[i] = a[j]; a[j] = temp; } } // Program 3.1 Selection sort using templates
12. A stack is an ordered list in which insertions and deletions are made at one end called the top . 例如:插入 A,B,C,D ,刪除一個元素。
13. 令 S = (a 0 , a 1 , ..., a n-1 ) , a 0 is bottom, a n-1 is top, a i is on top of a i-1 , 0 < i < n.
14.
15. 3.2 The Stack Abstract Data Type template < class KeyType> class Stack { // objects: A finite ordered list of zero or more elements public : Stack ( int MaxStackSize = DefaultSize); Boolean IsFull(); // return TRUE if Stack is full; FALSE otherwise Boolean IsEmpty(); // return TRUE if Stack is empty; FALSE otherwise void Add(const KeyType &); // if IsFull(), return 0; // else insert an element to the top of the Stack KeyType *Delete( KeyType &); // if IsEmpty(), then return 0; // else remove and return a pointer to the top element }; // Abstract data type Stack , member function 的部份
16. 3.2 The Stack Abstract Data Type template < class KeyType> class Stack{ // data member 的部份 ... private : int top; KeyType *stack; int MaxSize; }; template < class KeyType> // 建構子 Stack <KeyType> :: Stack ( int MaxStackSize):MaxSize(MaxStackSize) { stack = new KeyType [MaxSize]; top = -1; }
17. 3.2 The Stack Abstract Data Type template < class KeyType > inline Boolean Stack< KeyType >:: IsFull (){ if (top == MaxSize – 1) return TRUE; else return FALSE; } template < class KeyType > inline Boolean Stack< KeyType >:: IsEmpty (){ if (top == -1) return TRUE; else return FALSE; }
18. 3.2 The Stack Abstract Data Type template < class KeyType> void Stack< KeyType >:: Add ( const KeyType& x ){ // add x to stack if ( IsFull() ) StackFull(); else stack[++top] = x ; } template < class KeyType> KeyType* Stack<Keytype>:: Delete (KeyType& x) { // Remove top element from stack if (IsEmpty() ) { StackEmpty(); return 0;} x = stack[top--]; return ( & x ); }
19. Exercises 3.2 2. Consider the railroad switching network given in Figure 3.3. Raidroad cars numbered 1, 2, 3, ..., n are initially in the top right track segment (in this order, left to right). Railroad cars can be moved into the vertical track segment one at a time from either of the horizontal segments and then moved from the vertical segment to any one of the horizontal segments. The vertical segment operates as a stack as new cars enter at the top and cars depart the vertical segment from the top. For instance, if n = 3, we could move car 1 into the vertical segment, move 2 in, move 3 in, and then take the cars out producing the new order 3, 2, 1. For n = 3 and 4 what are the possible permutations of the cars that can be obtained? Are any permutations not possible ?
34. A data object of Type B IS- A data object of Type A if B is more specialized than A or A is more general than B. The set of all objects of Type B is a subset of the set of all objects of Type A.
37. C++ provides a mechanism to express the IS-A relationship called public inheritance .
38. class Stack: public Bag { // Bag 是 base class, Stack 是 derived class Stack( int MaxSize = DefaultSize); ~Stack(); int * Delete( int &); }
39. 3.4 Subtyping and Inheritance in C++ class Bag { public : Bag( int MaxSize = DefaultSize); virtual ~Bag(); virtual void Add( int ); virtual int * Delete( int &); virtual Boolean IsFull(); virtual Boolean IsEmpty(); protected : virtual void Full(); virtual void Empty(); int *array, MaxSize, top; }; class Stack: public Bag { Stack( int MaxSize = DefaultSize); ~Stack(); int * Delete( int &); }
40.
41. Another important consequence of public inheritance is that inherited members have the same level of access in the derived class as they did in the base class. Stack::Stack ( int MaxStackSize) :Bag(MaxStackSize) { } Stack::~Stack() { } // 直接呼叫 Bag 的解構子 int * Stack::Delete( int & x) { if (IsEmpty()) {Empty(); return 0;} x = array[top--]; return &x; }
55. 3.5 Amazing Problem initialize stack to the maze entrance coordinates and direction east; while ( stack is not empty) { ( i , j , dir ) = coordinates and direction deleted from top of stack ; while (there are more moves) { ( g , h ) = coordinates of next move; if (( g == m ) && ( h == p )) success; if (! maze [ g ][ h ]) && (! mark [ g ][ h ]) { // 有路走且尚未走過 mark [ g ][ h ] = 1; dir = next direction to try; add( i , j , dir ) to top of stack ; i = g; j = h; dir = north ; } } } cout << "no path found" << endl;
96. Write an algorithm to evaluate a prefix expression e. (hint: Scan e right to left and assume that the leftmost token of e is '# ')
97. Write an algorithm to transform an infix expression e into its prefix equivalent. Assume that the input expression e begins with a '# ' and that the prefix expression should begin with a '# '.
98.
99. 若有 n 個 stacks 放在同一個陣列,就只好將 M[m] 切成 n 等份,如下圖。 M 0 m/n – 1 2m/n – 1 m – 1 b[0] b[1] b[2] b[n] t[0] t[1] t[2]