
1 // Simple Stack Example
2 #include <iostream>
3 #include "console.h"
4 #include "stack.h"
5
6 using namespace std;
7 const char SPACE = ' ';
8
9 int main() {
10 string sentence = "hope is what defines humanity";
11 string word;
12 Stack<string> wordStack;
13
14 cout << "Original sentence: " << sentence << endl;
15
16 for (char c : sentence) {
17 if (c == SPACE and word != "") {
18 wordStack.push(word);
19 word = ""; // reset
20 }
21 else {
22 word += c;
23 }
24 }
25 if (word != "") {
26 wordStack.push(word);
27 }
28
29 cout << " New sentence: ";
30 while (!wordStack.isEmpty()) {
31 string word = wordStack.pop();
32 cout << word << SPACE;
33 }
34 cout << endl;
35 return 0;
36 }
37
38 // Queue mystery
39 #include <iostream>
40 #include "console.h"
41 #include "simpio.h"
42 #include "queue.h"
43
44 using namespace std;
45
46 int main() {
47 Queue<int> queue;
48 // produce: {1, 2, 3, 4, 5, 6}
49 for (int i = 1; i <= 6; i++) {
50 queue.enqueue(i);
51 }
52 // original mystery:
53 for (int i = 0; i < queue.size(); i++) {
54 cout << queue.dequeue() << " ";
55 }
56
57 // better idiom:
58 // while (!queue.isEmpty()) {
59 // cout << queue.dequeue() << " ";
60 // }
61
62 // okay idiom if you want to go through original queue elements once:
63 // int origQSize = queue.size();
64 // for (int i=0; i < origQSize; i++) {
65 // int value = queue.dequeue();
66 // cout << value << " ";
67 // // re-enqueue even values
68 // if (value % 2 == 0) {
69 // queue.enqueue(value);
70 // }
71 // }
72
73 cout << queue.toString() << " size " << queue.size() << endl;
74 return 0;
75 }