├── .gitignore
├── Chapter01
├── Amortized
│ ├── Amortized.cbp
│ └── main.cpp
├── Assignment_Operator_Overload
│ ├── Assignment_Operator_Overload.cbp
│ ├── Assignment_Operator_Overload.depend
│ ├── Assignment_Operator_Overload.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── Assignment_Operator_Overload
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── Class_Templates
│ ├── Class_Templates.cbp
│ ├── Class_Templates.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── Class_Templates
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── Constructor
│ ├── Constructor.cbp
│ ├── Constructor.depend
│ ├── Constructor.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── Constructor
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── Derived_Class
│ ├── Derived_Class.cbp
│ ├── Derived_Class.depend
│ ├── Derived_Class.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── Derived_Class
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── Do-While
│ ├── Do-While.cbp
│ ├── Do-While.depend
│ ├── Do-While.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── Do-While
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── Enum
│ ├── Enum.cbp
│ ├── Enum.depend
│ ├── Enum.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── Enum
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── FirstApp
│ ├── FirstApp.cbp
│ ├── FirstApp.depend
│ ├── FirstApp.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── FirstApp
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── For
│ ├── For.cbp
│ ├── For.depend
│ ├── For.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── For
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── Function_Templates
│ ├── Function_Templates.cbp
│ ├── Function_Templates.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── Function_Templates
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── If
│ ├── If.cbp
│ ├── If.depend
│ ├── If.layout
│ ├── bin
│ │ ├── Debug
│ │ │ └── If
│ │ └── Release
│ │ │ └── If
│ ├── main.cpp
│ └── obj
│ │ ├── Debug
│ │ └── main.o
│ │ └── Release
│ │ └── main.o
├── If_ElseIf
│ ├── If_ElseIf.cbp
│ ├── If_ElseIf.depend
│ ├── If_ElseIf.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── If_ElseIf
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── If_ElseIf_2
│ ├── If_ElseIf_2.cbp
│ ├── If_ElseIf_2.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── If_ElseIf_2
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── Looping
│ ├── Looping.cbp
│ ├── Looping.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── Looping
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── Pairing
│ ├── Pairing.cbp
│ ├── Pairing.depend
│ ├── Pairing.layout
│ ├── bin
│ │ ├── Debug
│ │ │ └── Pairing
│ │ └── Release
│ │ │ └── Pairing
│ ├── main.cpp
│ └── obj
│ │ ├── Debug
│ │ └── main.o
│ │ └── Release
│ │ └── main.o
├── Recursive
│ ├── Recursive.cbp
│ ├── Recursive.depend
│ ├── bin
│ │ └── Debug
│ │ │ └── Recursive
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── Search
│ ├── Search.cbp
│ └── main.cpp
├── Simple_Class
│ ├── Simple_Class.cbp
│ ├── Simple_Class.depend
│ ├── Simple_Class.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── Simple_Class
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── Struct
│ ├── Struct.cbp
│ ├── Struct.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── Struct
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── Switch
│ ├── Switch.cbp
│ ├── Switch.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── Switch
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── Templates
│ ├── Templates.cbp
│ ├── Templates.depend
│ ├── Templates.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── Templates
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── While
│ ├── While.cbp
│ ├── While.depend
│ ├── While.layout
│ ├── bin
│ │ └── Debug
│ │ │ └── While
│ ├── main.cpp
│ └── obj
│ │ └── Debug
│ │ └── main.o
├── in_out.cpp
└── simple.cpp
├── Chapter02
├── Array
│ ├── Array.cbp
│ └── Array.cpp
├── Array_As_Pointer
│ ├── Array_As_Pointer.cbp
│ └── Array_As_Pointer.cpp
├── Doubly_Linked_List
│ ├── Doubly_Linked_List.cbp
│ ├── include
│ │ ├── DoublyLinkedList.h
│ │ └── DoublyNode.h
│ ├── main.cpp
│ └── src
│ │ ├── DoublyLinkedList.cpp
│ │ └── DoublyNode.cpp
├── List
│ ├── List.cbp
│ ├── include
│ │ └── List.h
│ ├── main.cpp
│ └── src
│ │ └── List.cpp
├── Node_Chain
│ ├── Node_Chain.cbp
│ └── main.cpp
├── Node_Chain_Template
│ ├── Node_Chain_Template.cbp
│ └── main.cpp
├── Singly_Linked_List.7z
├── Singly_Linked_List
│ ├── Singly_Linked_List.cbp
│ ├── include
│ │ ├── LinkedList.h
│ │ └── Node.h
│ ├── main.cpp
│ └── src
│ │ ├── LinkedList.cpp
│ │ └── Node.cpp
├── Std_List
│ ├── Std_List.cbp
│ └── main.cpp
└── Vector
│ ├── Vector.cbp
│ └── main.cpp
├── Chapter03
├── Deque
│ ├── Deque.cbp
│ ├── include
│ │ └── Deque.h
│ ├── main.cpp
│ └── src
│ │ └── Deque.cpp
├── Node
│ ├── DoublyNode.h
│ └── Node.h
├── Parenthesis_Validity
│ ├── Parenthesis_Validity.cbp
│ ├── Parenthesis_Validity.cpp
│ └── main.cpp
├── Queue
│ ├── Queue.cbp
│ ├── include
│ │ └── Queue.h
│ ├── main.cpp
│ └── src
│ │ └── Queue.cpp
└── Stack
│ ├── Stack.cbp
│ ├── include
│ └── Stack.h
│ ├── main.cpp
│ └── src
│ └── Stack.cpp
├── Chapter04
├── Bubble_Sort
│ ├── Bubble_Sort.cbp
│ └── Bubble_Sort.cpp
├── Counting_Sort
│ ├── Counting_Sort.cbp
│ └── Counting_Sort.cpp
├── Insertion_Sort
│ ├── Insertion_Sort.cbp
│ └── Insertion_Sort.cpp
├── Merge_Sort
│ ├── Merge_Sort.cbp
│ └── Merge_Sort.cpp
├── Quick_Sort
│ ├── Quick_Sort.cbp
│ └── Quick_Sort.cpp
├── Radix_Sort
│ ├── Node.h
│ ├── Queue.h
│ ├── Radix_Sort.cbp
│ └── Radix_Sort.cpp
└── Selection_Sort
│ ├── Selection_Sort.cbp
│ └── Selection_Sort.cpp
├── Chapter05
├── Binary_Search
│ ├── Binary_Search.cbp
│ └── Binary_Search.cpp
├── Exponential_Search
│ ├── Exponential_Search.cbp
│ └── main.cpp
├── Interpolation_Search
│ ├── Interpolation_Search.cbp
│ └── Interpolation_Search.cpp
├── Jump_Search
│ ├── Jump_Search.cbp
│ └── Jump_Search.cpp
├── Linear_Search
│ ├── Linear_Search.cbp
│ └── Linear_Search.cpp
├── Sublist_Search
│ ├── Sublist_Search.cbp
│ └── Sublist_Search.cpp
└── Ternary_Search
│ ├── Ternary_Search.cbp
│ └── Ternary_Search.cpp
├── Chapter06
├── Anagram
│ ├── Anagram
│ ├── Anagram.cbp
│ ├── Anagram.cpp
│ └── Anagram.o
├── BinaryString_To_Decimal
│ ├── BinaryString_To_Decimal.cbp
│ └── BinaryString_To_Decimal.cpp
├── Characters_Array
│ ├── Characters_Array.cbp
│ └── Characters_Array.cpp
├── Decimal_To_BinaryString
│ ├── Decimal_To_BinaryString.cbp
│ └── Decimal_To_BinaryString.cpp
├── Palindrome
│ ├── Palindrome.cbp
│ └── Palindrome.cpp
├── Pattern_Searching
│ ├── Pattern_Searching
│ ├── Pattern_Searching.cbp
│ ├── Pattern_Searching.cpp
│ └── Pattern_Searching.o
├── Subsequence_Generator
│ ├── Subsequence_Generator.cbp
│ └── Subsequence_Generator.cpp
└── Subsequence_String
│ ├── Subsequence_String.cbp
│ └── Subsequence_String.cpp
├── Chapter07
├── Binary_Heap
│ ├── Binary_Heap.cbp
│ ├── Binary_Heap.cpp
│ ├── include
│ │ └── BinaryHeap.h
│ └── src
│ │ └── BinaryHeap.cpp
├── Binary_Search_Tree
│ ├── AVL_Tree.cpp
│ ├── Binary_Search_Tree.cbp
│ ├── Binary_Search_Tree.cpp
│ ├── include
│ │ ├── AVLNode.h
│ │ ├── BSTNode.cpp
│ │ └── BSTNode.h
│ └── src
│ │ ├── AVLNode.cpp
│ │ └── BSTNode.cpp
└── Binary_Tree
│ ├── Binary_Tree.cbp
│ └── Binary_Tree.cpp
├── Chapter08
├── Hash_Table_OP_LP
│ ├── Hash_Table_OP_LP.cbp
│ ├── include
│ │ └── HashTable.h
│ ├── main.cpp
│ └── src
│ │ └── HashTable.cpp
└── Hash_Table_SC
│ ├── Hash_Table_SC.cbp
│ ├── include
│ └── HashTable.h
│ ├── main.cpp
│ └── src
│ └── HashTable.cpp
├── Chapter09
├── CAPTCHA
│ ├── CAPTCHA.cbp
│ └── main.cpp
├── Coin_Change_Dynamic_Programming
│ ├── Coin_Change_Dynamic_Programming.cbp
│ └── main.cpp
├── Coin_Change_Problem
│ ├── Coin_Change_Problem.cbp
│ ├── Coin_Change_Problem.cpp
│ └── main.cpp
├── Fibonacci
│ ├── Fibonacci.cbp
│ └── main.cpp
├── Huffman_Coding
│ ├── Huffman_Coding.cbp
│ └── main.cpp
├── Random_Number_Gеnеrаtоrѕ
│ ├── Random_Number_Gеnеrаtоrѕ.cbp
│ └── main.cpp
└── TicTacToe
│ ├── TicTacToe.cbp
│ └── main.cpp
├── LICENSE
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | # Created by https://www.gitignore.io/api/codeblocks
2 |
3 | ### CodeBlocks ###
4 | # specific to CodeBlocks IDE
5 | *.layout
6 | *.depend
7 |
8 | # generated directories
9 | bin/
10 | obj/
11 |
12 | # End of https://www.gitignore.io/api/codeblocks
13 |
14 | ### temporary folder
15 | temp/
16 |
--------------------------------------------------------------------------------
/Chapter01/Amortized/Amortized.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter01/Amortized/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | int SumOfDivision(
6 | int nArr[], int n, int mArr[], int m)
7 | {
8 | int total = 0;
9 |
10 | for(int i = 0; i < n; ++i)
11 | {
12 | for(int j = 0; j < m; ++j)
13 | {
14 | total += (nArr[i] * mArr[j]);
15 | }
16 | }
17 |
18 | return total;
19 | }
20 |
21 | int main()
22 | {
23 | cout << "Hello world!" << endl;
24 | return 0;
25 | }
26 |
--------------------------------------------------------------------------------
/Chapter01/Assignment_Operator_Overload/Assignment_Operator_Overload.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Assignment_Operator_Overload/Assignment_Operator_Overload.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510566595 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/Assignment_Operator_Overload/main.cpp
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Chapter01/Assignment_Operator_Overload/Assignment_Operator_Overload.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Assignment_Operator_Overload/bin/Debug/Assignment_Operator_Overload:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Assignment_Operator_Overload/bin/Debug/Assignment_Operator_Overload
--------------------------------------------------------------------------------
/Chapter01/Assignment_Operator_Overload/main.cpp:
--------------------------------------------------------------------------------
1 | // Assignment_Operator_Overload.cbp
2 | #include
3 |
4 | using namespace std;
5 |
6 | class Animal
7 | {
8 | protected:
9 | string m_name;
10 |
11 | public:
12 | Animal(string name) : m_name(name)
13 | {
14 |
15 | }
16 |
17 | // The interface that has to be implemented
18 | // in derived class
19 | virtual string MakeSound() = 0;
20 |
21 | string GetName()
22 | {
23 | return m_name;
24 | }
25 |
26 | };
27 |
28 | class Dog : public Animal
29 | {
30 | public:
31 | // Forward the constructor arguments
32 | Dog(string name) : Animal(name) {}
33 |
34 | // Copy assignment operator overloading
35 | void operator = (const Dog &D ) {
36 | m_name = D.m_name;
37 | }
38 |
39 | // here we implement the interface
40 | string MakeSound() override
41 | {
42 | return "woof-woof!";
43 | }
44 |
45 | };
46 |
47 | int main()
48 | {
49 | Dog dog = Dog("Bulldog");
50 | cout << dog.GetName() << " is barking: ";
51 | cout << dog.MakeSound() << endl;
52 |
53 | Dog dog2 = dog;
54 | cout << dog2.GetName() << " is barking: ";
55 | cout << dog2.MakeSound() << endl;
56 |
57 | return 0;
58 | }
59 |
--------------------------------------------------------------------------------
/Chapter01/Assignment_Operator_Overload/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Assignment_Operator_Overload/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Class_Templates/Class_Templates.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Class_Templates/Class_Templates.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Class_Templates/bin/Debug/Class_Templates:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Class_Templates/bin/Debug/Class_Templates
--------------------------------------------------------------------------------
/Chapter01/Class_Templates/main.cpp:
--------------------------------------------------------------------------------
1 | // Class_Templates.cbp
2 | #include
3 |
4 | using namespace std;
5 |
6 | class Animal
7 | {
8 | protected:
9 | string m_name;
10 |
11 | public:
12 | Animal(string name) : m_name(name)
13 | {
14 |
15 | }
16 |
17 | // The interface that has to be implemented
18 | // in derived class
19 | virtual string MakeSound() = 0;
20 |
21 | string GetName()
22 | {
23 | return m_name;
24 | }
25 |
26 | };
27 |
28 | class Dog : public Animal
29 | {
30 | public:
31 | // Forward the constructor arguments
32 | Dog(string name) : Animal(name) {}
33 |
34 | // Copy assignment operator overloading
35 | void operator = (const Dog &D)
36 | {
37 | m_name = D.m_name;
38 | }
39 |
40 | // here we implement the interface
41 | string MakeSound() override
42 | {
43 | return "woof-woof!";
44 | }
45 |
46 | };
47 |
48 | class Cat : public Animal
49 | {
50 | public:
51 | // Forward the constructor arguments
52 | Cat(string name) : Animal(name) {}
53 |
54 | // Copy assignment operator overloading
55 | void operator = (const Cat &D)
56 | {
57 | m_name = D.m_name;
58 | }
59 |
60 | // here we implement the interface
61 | string MakeSound() override
62 | {
63 | return "meow-meow!";
64 | }
65 |
66 | };
67 |
68 | template
69 | void GetNameAndMakeSound(T& theAnimal)
70 | {
71 | cout << theAnimal.GetName() << " goes ";
72 | cout << theAnimal.MakeSound() << endl;
73 | }
74 |
75 | template
76 | class AnimalTemplate
77 | {
78 | private:
79 | T m_animal;
80 |
81 | public:
82 | AnimalTemplate(T animal) : m_animal(animal) {}
83 |
84 | void GetNameAndMakeSound()
85 | {
86 | cout << m_animal.GetName() << " goes ";
87 | cout << m_animal.MakeSound() << endl;
88 | }
89 | };
90 |
91 | int main()
92 | {
93 | Dog dog = Dog("Bulldog");
94 | AnimalTemplate dogTemplate(dog);
95 | dogTemplate.GetNameAndMakeSound();
96 |
97 | Cat cat = Cat("Persian Cat");
98 | AnimalTemplate catTemplate(cat);
99 | catTemplate.GetNameAndMakeSound();
100 |
101 | return 0;
102 | }
103 |
--------------------------------------------------------------------------------
/Chapter01/Class_Templates/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Class_Templates/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Constructor/Constructor.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Constructor/Constructor.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510561639 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/Constructor/main.cpp
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Chapter01/Constructor/Constructor.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Constructor/bin/Debug/Constructor:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Constructor/bin/Debug/Constructor
--------------------------------------------------------------------------------
/Chapter01/Constructor/main.cpp:
--------------------------------------------------------------------------------
1 | // Constructor.cbp
2 | #include
3 |
4 | using namespace std;
5 |
6 | class Animal
7 | {
8 | private:
9 | string m_name;
10 |
11 | public:
12 | Animal(string name) : m_name(name)
13 | {
14 |
15 | }
16 |
17 | string GetName()
18 | {
19 | return m_name;
20 | }
21 | };
22 |
23 | int main()
24 | {
25 | Animal dog = Animal("dog");
26 |
27 | cout << "Hi, I'm a " << dog.GetName() << endl;
28 |
29 | return 0;
30 | }
31 |
--------------------------------------------------------------------------------
/Chapter01/Constructor/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Constructor/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Derived_Class/Derived_Class.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Derived_Class/Derived_Class.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510564251 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/Derived_Class/main.cpp
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Chapter01/Derived_Class/Derived_Class.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Derived_Class/bin/Debug/Derived_Class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Derived_Class/bin/Debug/Derived_Class
--------------------------------------------------------------------------------
/Chapter01/Derived_Class/main.cpp:
--------------------------------------------------------------------------------
1 | // Derived_Class.cbp
2 | #include
3 |
4 | using namespace std;
5 |
6 | class Animal
7 | {
8 | private:
9 | string m_name;
10 |
11 | public:
12 | Animal(string name) : m_name(name)
13 | {
14 |
15 | }
16 |
17 | // The interface that has to be implemented
18 | // in derived class
19 | virtual string MakeSound() = 0;
20 |
21 | string GetName()
22 | {
23 | return m_name;
24 | }
25 | };
26 |
27 | class Dog : public Animal
28 | {
29 | public:
30 | // Forward the constructor arguments
31 | Dog(string name) : Animal(name) {}
32 |
33 | // here we implement the interface
34 | string MakeSound() override
35 | {
36 | return "woof-woof!";
37 | }
38 |
39 | };
40 |
41 | int main()
42 | {
43 | Dog dog = Dog("Bulldog");
44 |
45 | cout << dog.GetName() << " is barking: ";
46 | cout << dog.MakeSound() << endl;
47 |
48 | return 0;
49 | }
50 |
--------------------------------------------------------------------------------
/Chapter01/Derived_Class/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Derived_Class/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Do-While/Do-While.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Do-While/Do-While.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510204710 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/Do-While/main.cpp
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Chapter01/Do-While/Do-While.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Do-While/bin/Debug/Do-While:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Do-While/bin/Debug/Do-While
--------------------------------------------------------------------------------
/Chapter01/Do-While/main.cpp:
--------------------------------------------------------------------------------
1 | // Do-While.cbp
2 | #include
3 | #include
4 | #include
5 |
6 | using namespace std;
7 |
8 | int GenerateRandomNumber(int min, int max)
9 | {
10 | // static used for efficiency,
11 | // so we only calculate this value once
12 | static const double fraction =
13 | 1.0 / (static_cast(RAND_MAX) + 1.0);
14 |
15 | // evenly distribute the random number
16 | // across our range
17 | return min + static_cast(
18 | (max - min + 1) * (rand() * fraction));
19 | }
20 |
21 | int main()
22 | {
23 | // set initial seed value to system clock
24 | srand(static_cast(time(0)));
25 |
26 | char userChar;
27 |
28 | int iMin = 1;
29 | int iMax = 100;
30 | int iGuess;
31 |
32 | // Menu display
33 | cout << "Choose a number between 1 to 100, ";
34 | cout << "and I'll guess your number." << endl;
35 | cout << "Press L and ENTER if my guessed number is lower than yours";
36 | cout << endl;
37 | cout << "Press G and ENTER if my guessed number is greater than yours";
38 | cout << endl;
39 | cout << "Press Y and ENTER if I've successfully guessed your number!";
40 | cout << endl << endl;
41 |
42 | // Run the DO-WHILE loop
43 | do
44 | {
45 | iGuess = GenerateRandomNumber(iMin, iMax);
46 | cout << "I guess your number is " << iGuess << endl;
47 | cout << "What do you think? ";
48 | cin >> userChar;
49 | if(userChar == 'L' || userChar == 'l')
50 | iMin = iGuess + 1;
51 | else if(userChar == 'G' || userChar == 'g')
52 | iMax = iGuess - 1;
53 |
54 | }
55 | while(userChar != 'Y' && userChar != 'y');
56 |
57 | cout << "Yeeaayy.. I've got your number." << endl;
58 | return 0;
59 | }
60 |
--------------------------------------------------------------------------------
/Chapter01/Do-While/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Do-While/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Enum/Enum.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Enum/Enum.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510309693 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/Enum/main.cpp
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Chapter01/Enum/Enum.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Enum/bin/Debug/Enum:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Enum/bin/Debug/Enum
--------------------------------------------------------------------------------
/Chapter01/Enum/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Enum/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/FirstApp/FirstApp.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/FirstApp/FirstApp.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510044477 source:/media/wisnu/FUN/packt-dsa-cpp/Chapter01/FirstApp/main.cpp
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Chapter01/FirstApp/FirstApp.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/FirstApp/bin/Debug/FirstApp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/FirstApp/bin/Debug/FirstApp
--------------------------------------------------------------------------------
/Chapter01/FirstApp/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | int main()
6 | {
7 | cout << "Hello world!" << endl;
8 | return 0;
9 | }
10 |
--------------------------------------------------------------------------------
/Chapter01/FirstApp/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/FirstApp/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/For/For.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/For/For.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510052540 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/For/main.cpp
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Chapter01/For/For.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/For/bin/Debug/For:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/For/bin/Debug/For
--------------------------------------------------------------------------------
/Chapter01/For/main.cpp:
--------------------------------------------------------------------------------
1 | // For.cbp
2 | #include
3 | #include
4 | #include
5 |
6 | using namespace std;
7 |
8 | int GenerateRandomNumber(int min, int max)
9 | {
10 | // static used for efficiency,
11 | // so we only calculate this value once
12 | static const double fraction =
13 | 1.0 / (static_cast(RAND_MAX) + 1.0);
14 |
15 | // evenly distribute the random number
16 | // across our range
17 | return min + static_cast(
18 | (max - min + 1) * (rand() * fraction));
19 | }
20 |
21 | int main()
22 | {
23 | // set initial seed value to system clock
24 | srand(static_cast(time(0)));
25 |
26 | // loop ten times
27 | for (int i=0; i < 10; ++i)
28 | {
29 | cout << GenerateRandomNumber(0, 100) << " ";
30 | }
31 | cout << "\n";
32 |
33 | return 0;
34 | }
35 |
--------------------------------------------------------------------------------
/Chapter01/For/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/For/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Function_Templates/Function_Templates.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Function_Templates/Function_Templates.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Function_Templates/bin/Debug/Function_Templates:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Function_Templates/bin/Debug/Function_Templates
--------------------------------------------------------------------------------
/Chapter01/Function_Templates/main.cpp:
--------------------------------------------------------------------------------
1 | // Function_Templates.cbp
2 | #include
3 |
4 | using namespace std;
5 |
6 | class Animal
7 | {
8 | protected:
9 | string m_name;
10 |
11 | public:
12 | Animal(string name) : m_name(name)
13 | {
14 |
15 | }
16 |
17 | // The interface that has to be implemented
18 | // in derived class
19 | virtual string MakeSound() = 0;
20 |
21 | string GetName()
22 | {
23 | return m_name;
24 | }
25 |
26 | };
27 |
28 | class Dog : public Animal
29 | {
30 | public:
31 | // Forward the constructor arguments
32 | Dog(string name) : Animal(name) {}
33 |
34 | // Copy assignment operator overloading
35 | void operator = (const Dog &D)
36 | {
37 | m_name = D.m_name;
38 | }
39 |
40 | // here we implement the interface
41 | string MakeSound() override
42 | {
43 | return "woof-woof!";
44 | }
45 |
46 | };
47 |
48 | class Cat : public Animal
49 | {
50 | public:
51 | // Forward the constructor arguments
52 | Cat(string name) : Animal(name) {}
53 |
54 | // Copy assignment operator overloading
55 | void operator = (const Cat &D)
56 | {
57 | m_name = D.m_name;
58 | }
59 |
60 | // here we implement the interface
61 | string MakeSound() override
62 | {
63 | return "meow-meow!";
64 | }
65 |
66 | };
67 |
68 | template
69 | void GetNameAndMakeSound(T& theAnimal)
70 | {
71 | cout << theAnimal.GetName() << " goes ";
72 | cout << theAnimal.MakeSound() << endl;
73 | }
74 |
75 | int main()
76 | {
77 | Dog dog = Dog("Bulldog");
78 | GetNameAndMakeSound(dog);
79 |
80 | Cat cat = Cat("Persian Cat");
81 | GetNameAndMakeSound(cat);
82 |
83 | return 0;
84 | }
85 |
--------------------------------------------------------------------------------
/Chapter01/Function_Templates/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Function_Templates/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/If/If.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/If/If.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510050327 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/If/main.cpp
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Chapter01/If/If.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/If/bin/Debug/If:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/If/bin/Debug/If
--------------------------------------------------------------------------------
/Chapter01/If/bin/Release/If:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/If/bin/Release/If
--------------------------------------------------------------------------------
/Chapter01/If/main.cpp:
--------------------------------------------------------------------------------
1 | // If.cbp
2 | #include
3 |
4 | using namespace std;
5 |
6 | int main ()
7 | {
8 | int i;
9 | cout << "Please enter an integer value: ";
10 | cin >> i;
11 | cout << "The value you entered is ";
12 |
13 | if(i > 100)
14 | cout << "greater than 100.";
15 | else
16 | cout << "equals or less than 100.";
17 |
18 | cout << endl;
19 | return 0;
20 | }
21 |
--------------------------------------------------------------------------------
/Chapter01/If/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/If/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/If/obj/Release/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/If/obj/Release/main.o
--------------------------------------------------------------------------------
/Chapter01/If_ElseIf/If_ElseIf.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/If_ElseIf/If_ElseIf.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510733758 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/If_ElseIf/main.cpp
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Chapter01/If_ElseIf/If_ElseIf.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/If_ElseIf/bin/Debug/If_ElseIf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/If_ElseIf/bin/Debug/If_ElseIf
--------------------------------------------------------------------------------
/Chapter01/If_ElseIf/main.cpp:
--------------------------------------------------------------------------------
1 | // If_ElseIf.cbp
2 | #include
3 |
4 | using namespace std;
5 |
6 | int main ()
7 | {
8 | int i;
9 | cout << "Please enter an integer value: ";
10 | cin >> i;
11 | cout << "The value you entered is ";
12 |
13 | if(i > 100)
14 | cout << "greater than 100.";
15 | else if(i < 100)
16 | cout << "less than 100.";
17 | else
18 | cout << "equals to 100";
19 |
20 | cout << endl;
21 | return 0;
22 | }
23 |
--------------------------------------------------------------------------------
/Chapter01/If_ElseIf/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/If_ElseIf/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/If_ElseIf_2/If_ElseIf_2.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/If_ElseIf_2/If_ElseIf_2.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/If_ElseIf_2/bin/Debug/If_ElseIf_2:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/If_ElseIf_2/bin/Debug/If_ElseIf_2
--------------------------------------------------------------------------------
/Chapter01/If_ElseIf_2/main.cpp:
--------------------------------------------------------------------------------
1 | // If_ElseIf_2.cbp
2 | #include
3 |
4 | using namespace std;
5 |
6 | int main ()
7 | {
8 | int i, a, b;
9 |
10 | cout << "Operation Mode: " << endl;
11 | cout << "1. Addition" << endl;
12 | cout << "2. Subtraction" << endl;
13 | cout << "3. Multiplication" << endl;
14 | cout << "4. Division" << endl;
15 | cout << "Please enter the operation mode: ";
16 | cin >> i;
17 |
18 | cout << "Please enter the first number: ";
19 | cin >> a;
20 | cout << "Please enter the second number: ";
21 | cin >> b;
22 |
23 | cout << "The result of ";
24 |
25 | if(i == 1)
26 | cout << a << " + " << b << " is " << a + b;
27 | else if(i == 2)
28 | cout << a << " - " << b << " is " << a - b;
29 | else if(i == 3)
30 | cout << a << " * " << b << " is " << a * b;
31 | else if(i == 4)
32 | cout << a << " / " << b << " is " << a / b;
33 |
34 | cout << endl;
35 | return 0;
36 | }
37 |
--------------------------------------------------------------------------------
/Chapter01/If_ElseIf_2/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/If_ElseIf_2/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Looping/Looping.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Looping/Looping.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Looping/bin/Debug/Looping:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Looping/bin/Debug/Looping
--------------------------------------------------------------------------------
/Chapter01/Looping/main.cpp:
--------------------------------------------------------------------------------
1 | // Looping.cbp
2 | #include
3 |
4 | using namespace std;
5 |
6 | void Looping(int n)
7 | {
8 | int i = 0;
9 |
10 | while(i < n)
11 | {
12 | cout << i << endl;
13 | i = i + 1;
14 | }
15 | }
16 |
17 | int main()
18 | {
19 | Looping(100);
20 | return 0;
21 | }
22 |
--------------------------------------------------------------------------------
/Chapter01/Looping/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Looping/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Pairing/Pairing.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Pairing/Pairing.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510822156 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/Pairing/main.cpp
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Chapter01/Pairing/Pairing.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Pairing/bin/Debug/Pairing:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Pairing/bin/Debug/Pairing
--------------------------------------------------------------------------------
/Chapter01/Pairing/bin/Release/Pairing:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Pairing/bin/Release/Pairing
--------------------------------------------------------------------------------
/Chapter01/Pairing/main.cpp:
--------------------------------------------------------------------------------
1 | // Pairing
2 | #include
3 |
4 | using namespace std;
5 |
6 | void Pairing(int n)
7 | {
8 | int i = 0;
9 |
10 | while(i < n)
11 | {
12 | int j = 0;
13 |
14 | while(j < n)
15 | {
16 | cout << i << ", " << j << endl;
17 | j = j + 1;
18 | }
19 | i = i + 1;
20 | }
21 | }
22 |
23 | int main()
24 | {
25 | Pairing(10);
26 | return 0;
27 | }
28 |
--------------------------------------------------------------------------------
/Chapter01/Pairing/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Pairing/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Pairing/obj/Release/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Pairing/obj/Release/main.o
--------------------------------------------------------------------------------
/Chapter01/Recursive/Recursive.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Recursive/Recursive.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1511148931 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/Recursive/main.cpp
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Chapter01/Recursive/bin/Debug/Recursive:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Recursive/bin/Debug/Recursive
--------------------------------------------------------------------------------
/Chapter01/Recursive/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | int Factorial(int n)
6 | {
7 | if(n == 1)
8 | return 1;
9 |
10 | return n * Factorial(n - 1);
11 | }
12 |
13 | int main()
14 | {
15 | cout << "Factorial of 6 (6!) is ";
16 | cout << Factorial(6) << endl;
17 | return 0;
18 | }
19 |
--------------------------------------------------------------------------------
/Chapter01/Recursive/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Recursive/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Search/Search.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter01/Search/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | int Search(int arr[], int arrSize, int x)
6 | {
7 | // Iterate through arr elements
8 | for (int i = 0; i < arrSize; ++i)
9 | {
10 | // If x is found
11 | // returns index of x
12 | if (arr[i] == x)
13 | return i;
14 | }
15 |
16 | // If x is not found
17 | // returns -1
18 | return -1;
19 | }
20 |
21 | int main()
22 | {
23 | int arr[] = {42, 55, 39, 71, 20, 18, 6, 84};
24 | int x = 18;
25 | int n = sizeof(arr)/sizeof(arr[0]);
26 |
27 | cout << Search(arr, n, x);
28 |
29 | return 0;
30 | }
31 |
--------------------------------------------------------------------------------
/Chapter01/Simple_Class/Simple_Class.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Simple_Class/Simple_Class.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510549303 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/Simple_Class/main.cpp
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Chapter01/Simple_Class/Simple_Class.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Simple_Class/bin/Debug/Simple_Class:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Simple_Class/bin/Debug/Simple_Class
--------------------------------------------------------------------------------
/Chapter01/Simple_Class/main.cpp:
--------------------------------------------------------------------------------
1 | // Simple_Class.cbp
2 | #include
3 |
4 | using namespace std;
5 |
6 | class Animal
7 | {
8 | private:
9 | string m_name;
10 |
11 | public:
12 | void GiveName(string name)
13 | {
14 | m_name = name;
15 | }
16 |
17 | string GetName()
18 | {
19 | return m_name;
20 | }
21 | };
22 |
23 | int main()
24 | {
25 | Animal dog = Animal();
26 | dog.GiveName("dog");
27 |
28 | cout << "Hi, I'm a " << dog.GetName() << endl;
29 |
30 | return 0;
31 | }
32 |
--------------------------------------------------------------------------------
/Chapter01/Simple_Class/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Simple_Class/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Struct/Struct.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Struct/Struct.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Struct/bin/Debug/Struct:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Struct/bin/Debug/Struct
--------------------------------------------------------------------------------
/Chapter01/Struct/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Struct/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Switch/Switch.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Switch/Switch.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Switch/bin/Debug/Switch:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Switch/bin/Debug/Switch
--------------------------------------------------------------------------------
/Chapter01/Switch/main.cpp:
--------------------------------------------------------------------------------
1 | // Switch.cbp
2 | #include
3 |
4 | using namespace std;
5 |
6 | int main ()
7 | {
8 | int i, a, b;
9 |
10 | cout << "Operation Mode: " << endl;
11 | cout << "1. Addition" << endl;
12 | cout << "2. Subtraction" << endl;
13 | cout << "3. Multiplication" << endl;
14 | cout << "4. Division" << endl;
15 | cout << "Please enter the operation mode: ";
16 | cin >> i;
17 |
18 | cout << "Please enter the first number: ";
19 | cin >> a;
20 | cout << "Please enter the second number: ";
21 | cin >> b;
22 |
23 | cout << "The result of ";
24 |
25 | switch(i)
26 | {
27 | case 1:
28 | cout << a << " + " << b << " is " << a + b;
29 | break;
30 | case 2:
31 | cout << a << " - " << b << " is " << a - b;
32 | break;
33 | case 3:
34 | cout << a << " * " << b << " is " << a * b;
35 | break;
36 | case 4:
37 | cout << a << " / " << b << " is " << a / b;
38 | break;
39 | }
40 |
41 | cout << endl;
42 | return 0;
43 | }
44 |
--------------------------------------------------------------------------------
/Chapter01/Switch/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Switch/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/Templates/Templates.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/Templates/Templates.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510569550 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/Templates/main.cpp
3 |
4 |
5 |
--------------------------------------------------------------------------------
/Chapter01/Templates/Templates.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/Templates/bin/Debug/Templates:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Templates/bin/Debug/Templates
--------------------------------------------------------------------------------
/Chapter01/Templates/main.cpp:
--------------------------------------------------------------------------------
1 | // Templates.cbp
2 | #include
3 |
4 | using namespace std;
5 |
6 | class Animal
7 | {
8 | protected:
9 | string m_name;
10 |
11 | public:
12 | Animal(string name) : m_name(name)
13 | {
14 |
15 | }
16 |
17 | // The interface that has to be implemented
18 | // in derived class
19 | virtual string MakeSound() = 0;
20 |
21 | string GetName()
22 | {
23 | return m_name;
24 | }
25 |
26 | };
27 |
28 | class Dog : public Animal
29 | {
30 | public:
31 | // Forward the constructor arguments
32 | Dog(string name) : Animal(name) {}
33 |
34 | // Copy assignment operator overloading
35 | void operator = (const Dog &D)
36 | {
37 | m_name = D.m_name;
38 | }
39 |
40 | // here we implement the interface
41 | string MakeSound() override
42 | {
43 | return "woof-woof!";
44 | }
45 |
46 | };
47 |
48 | class Cat : public Animal
49 | {
50 | public:
51 | // Forward the constructor arguments
52 | Cat(string name) : Animal(name) {}
53 |
54 | // Copy assignment operator overloading
55 | void operator = (const Cat &D)
56 | {
57 | m_name = D.m_name;
58 | }
59 |
60 | // here we implement the interface
61 | string MakeSound() override
62 | {
63 | return "meow-meow!";
64 | }
65 |
66 | };
67 |
68 | template
69 | void GetNameAndMakeSound(T& theAnimal)
70 | {
71 | cout << theAnimal.GetName() << " sounds ";
72 | cout << theAnimal.MakeSound() << endl;
73 | }
74 |
75 | int main()
76 | {
77 | Dog dog = Dog("Bulldog");
78 | GetNameAndMakeSound(dog);
79 |
80 | Cat cat = Cat("Persian Cat");
81 | GetNameAndMakeSound(cat);
82 |
83 | return 0;
84 | }
85 |
--------------------------------------------------------------------------------
/Chapter01/Templates/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/Templates/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/While/While.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter01/While/While.depend:
--------------------------------------------------------------------------------
1 | # depslib dependency file v1.0
2 | 1510199909 source:/media/wisnu/FUN/packt-dsa-cpp/source_code/ch01_sc/While/main.cpp
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/Chapter01/While/While.layout:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/Chapter01/While/bin/Debug/While:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/While/bin/Debug/While
--------------------------------------------------------------------------------
/Chapter01/While/main.cpp:
--------------------------------------------------------------------------------
1 | // While.cbp
2 | #include
3 | #include
4 | #include
5 |
6 | using namespace std;
7 |
8 | int GenerateRandomNumber(int min, int max)
9 | {
10 | // static used for efficiency,
11 | // so we only calculate this value once
12 | static const double fraction =
13 | 1.0 / (static_cast(RAND_MAX) + 1.0);
14 |
15 | // evenly distribute the random number
16 | // across our range
17 | return min + static_cast(
18 | (max - min + 1) * (rand() * fraction));
19 | }
20 |
21 | int main()
22 | {
23 | // set initial seed value to system clock
24 | srand(static_cast(time(0)));
25 |
26 | // Computer generate random number
27 | // between 1 to 100
28 | int computerNumber = GenerateRandomNumber(1, 100);
29 |
30 | // User inputs a guessed number
31 | int userNumber;
32 | cout << "Please enter a number between 1 to 100: ";
33 | cin >> userNumber;
34 |
35 | // Run the WHILE loop
36 | while(userNumber != computerNumber)
37 | {
38 | cout << userNumber << " is ";
39 | if(userNumber > computerNumber)
40 | cout << "greater";
41 | else
42 | cout << "lower";
43 | cout << " than computer's number" << endl;
44 | cout << "Choose another number: ";
45 | cin >> userNumber;
46 | }
47 |
48 | cout << "Yeeaayy.. You've got the number." << endl;
49 | return 0;
50 | }
51 |
--------------------------------------------------------------------------------
/Chapter01/While/obj/Debug/main.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter01/While/obj/Debug/main.o
--------------------------------------------------------------------------------
/Chapter01/in_out.cpp:
--------------------------------------------------------------------------------
1 | // in_out.cpp
2 | #include
3 |
4 | int main ()
5 | {
6 | int i;
7 | std::cout << "Please enter an integer value: ";
8 | std::cin >> i;
9 | std::cout << "The value you entered is " << i;
10 | std::cout << "\n";
11 | return 0;
12 | }
13 |
--------------------------------------------------------------------------------
/Chapter01/simple.cpp:
--------------------------------------------------------------------------------
1 | int main()
2 | {
3 | return 0;
4 | }
5 |
--------------------------------------------------------------------------------
/Chapter02/Array/Array.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter02/Array/Array.cpp:
--------------------------------------------------------------------------------
1 | // Project: Array.cbp
2 | // File : Array.cpp
3 |
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main()
9 | {
10 | // Initialize an array
11 | int arr[] = { 21, 47, 87, 35, 92 };
12 |
13 | // Access each element
14 | cout << "Array elements: ";
15 | for(int i = 0; i < sizeof(arr)/sizeof(*arr); ++i)
16 | cout << arr[i] << " ";
17 | cout << endl;
18 |
19 | // Manipulate several elements
20 | arr[2] = 30;
21 | arr[3] = 64;
22 |
23 | // Access each element again
24 | cout << "Array elements: ";
25 | for(int i = 0; i < sizeof(arr)/sizeof(*arr); ++i)
26 | cout << arr[i] << " ";
27 | cout << endl;
28 | }
29 |
--------------------------------------------------------------------------------
/Chapter02/Array_As_Pointer/Array_As_Pointer.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter02/Array_As_Pointer/Array_As_Pointer.cpp:
--------------------------------------------------------------------------------
1 | // Project: Array_As_Pointer.cbp
2 | // File : Array_As_Pointer.cpp
3 |
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main()
9 | {
10 | // Initialize tee array length
11 | int arrLength = 5;
12 |
13 | // Initialize a pointer
14 | // to hold an array
15 | int * ptr = new int[arrLength] { 21, 47, 87, 35, 92 };
16 |
17 | // Display each element value
18 | // by incrementing the pointer (ptr++)
19 | cout << "Using pointer increment" << endl;
20 | cout << "Value\tAddress" << endl;
21 | while(*ptr)
22 | {
23 | cout << *ptr << "\t";
24 | cout << ptr << endl;
25 | ptr++;
26 | }
27 | cout << endl;
28 |
29 | // Since we have moved forward the pointer five times
30 | // we need to move it back
31 | ptr = ptr - 5;
32 |
33 | // Display each element value
34 | // by accessing pointer index (ptr[])
35 | cout << "Using pointer index" << endl;
36 | cout << "Value\tAddress" << endl;
37 | for(int i = 0; i < arrLength; ++i)
38 | {
39 | cout << ptr[i] << "\t";
40 | cout << &ptr[i] << endl;
41 | }
42 |
43 | return 0;
44 | }
45 |
--------------------------------------------------------------------------------
/Chapter02/Doubly_Linked_List/Doubly_Linked_List.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/Chapter02/Doubly_Linked_List/include/DoublyNode.h:
--------------------------------------------------------------------------------
1 | // Project: Doubly_Linked_List.cbp
2 | // File : DoublyNode.h
3 | #ifndef DOUBLYNODE_H
4 | #define DOUBLYNODE_H
5 |
6 | #include
7 |
8 | template
9 | class DoublyNode
10 | {
11 | public:
12 | T Value;
13 | DoublyNode * Previous;
14 | DoublyNode * Next;
15 |
16 | DoublyNode(T value);
17 | };
18 |
19 | template
20 | DoublyNode::DoublyNode(T value)
21 | : Value(value), Previous(NULL), Next(NULL) {}
22 |
23 | #endif // DOUBLYNODE_H
24 |
--------------------------------------------------------------------------------
/Chapter02/Doubly_Linked_List/main.cpp:
--------------------------------------------------------------------------------
1 | // Project: Doubly_Linked_List.cbp
2 | // File : main.cpp
3 | #include
4 | #include "DoublyNode.h"
5 | #include "DoublyLinkedList.h"
6 |
7 | using namespace std;
8 |
9 | int main()
10 | {
11 | // NULL
12 | DoublyLinkedList linkedList = DoublyLinkedList();
13 |
14 | // it will be printed backwardly
15 | // 43->NULL
16 | linkedList.InsertHead(43);
17 |
18 | // 43->76->NULL
19 | linkedList.InsertHead(76);
20 |
21 | // 15->43->76->NULL
22 | linkedList.InsertTail(15);
23 |
24 | // 44->15->43->76->NULL
25 | linkedList.InsertTail(44);
26 |
27 | // Print the list element
28 | cout << "First Printed:" << endl;
29 | linkedList.PrintListBackward();
30 | cout << endl;
31 |
32 | // 100->44->15->43->76->NULL
33 | linkedList.Insert(4, 100);
34 |
35 | // 100->44->48->15->43->76->NULL
36 | linkedList.Insert(3, 48);
37 |
38 | // 100->44->48->15->43->76->22->NULL
39 | linkedList.Insert(0, 22);
40 |
41 | // Print the list element
42 | cout << "Second Printed:" << endl;
43 | linkedList.PrintListBackward();
44 | cout << endl;
45 |
46 | return 0;
47 | }
48 |
--------------------------------------------------------------------------------
/Chapter02/Doubly_Linked_List/src/DoublyLinkedList.cpp:
--------------------------------------------------------------------------------
1 | // Project: Doubly_Linked_List.cbp
2 | // File : DoublyLinkedList.cpp
3 | #include "DoublyLinkedList.h"
4 |
--------------------------------------------------------------------------------
/Chapter02/Doubly_Linked_List/src/DoublyNode.cpp:
--------------------------------------------------------------------------------
1 | // Project: Doubly_Linked_List.cbp
2 | // File : DoublyNode.cpp
3 | #include "DoublyNode.h"
4 |
--------------------------------------------------------------------------------
/Chapter02/List/List.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Chapter02/List/include/List.h:
--------------------------------------------------------------------------------
1 | // Project: List.cbp
2 | // File : List.h
3 | #ifndef LIST_H
4 | #define LIST_H
5 |
6 | #include
7 |
8 | class List
9 | {
10 | private:
11 | int m_count;
12 | int * m_items;
13 |
14 | public:
15 | List();
16 | ~List();
17 | int Get(int index);
18 | void Insert(int index, int val);
19 | int Search(int val);
20 | void Remove(int index);
21 | int Count();
22 | };
23 | #endif // LIST_H
24 |
--------------------------------------------------------------------------------
/Chapter02/List/main.cpp:
--------------------------------------------------------------------------------
1 | // Project: List.cbp
2 | // File : main.cpp
3 | #include "List.h"
4 |
5 | using namespace std;
6 |
7 | int main()
8 | {
9 | // Initialize a List
10 | List list = List();
11 |
12 | // Add several items to the List
13 | list.Insert(0, 21);
14 | list.Insert(1, 47);
15 | list.Insert(2, 87);
16 | list.Insert(3, 35);
17 | list.Insert(4, 92);
18 |
19 | // Print current List
20 | cout << "List elements:" << endl;
21 | for(int i = 0; i < list.Count(); ++i)
22 | {
23 | cout << list.Get(i) << " ";
24 | }
25 | cout << endl << endl;
26 |
27 | // Insert several items in the middle of the List
28 | list.Insert(2, 25);
29 | list.Insert(2, 71);
30 |
31 | // Print the List again
32 | cout << "New List elements:" << endl;
33 | for(int i = 0; i < list.Count(); ++i)
34 | {
35 | cout << list.Get(i) << " ";
36 | }
37 | cout << endl << endl;
38 |
39 | // Search value 71
40 | cout << "Search element 71" << endl;
41 | int result = list.Search(71);
42 | if(result == -1)
43 | cout << "71 is not found";
44 | else
45 | cout << "71 is found at index " << result;
46 | cout << endl << endl;
47 |
48 | // Remove index 2
49 | cout << "Remove element at index 2" << endl;
50 | list.Remove(2);
51 | cout << endl;
52 |
53 | // Print the List again
54 | cout << "New List elements:" << endl;
55 | for(int i = 0; i < list.Count(); ++i)
56 | {
57 | cout << list.Get(i) << " ";
58 | }
59 | cout << endl << endl;
60 |
61 | // Search value 71 again
62 | cout << "Search element 71 again" << endl;
63 | result = list.Search(71);
64 | if(result == -1)
65 | cout << "71 is not found";
66 | else
67 | cout << "71 is found at index " << result;
68 | cout << endl;
69 |
70 | return 0;
71 | }
72 |
--------------------------------------------------------------------------------
/Chapter02/Node_Chain/Node_Chain.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter02/Node_Chain/main.cpp:
--------------------------------------------------------------------------------
1 | // Project: Node_Chain.cbp
2 | // File : main.cpp
3 | #include
4 |
5 | using namespace std;
6 |
7 | class Node
8 | {
9 | public:
10 | int Value;
11 | Node * Next;
12 | };
13 |
14 | void PrintNode(Node * node)
15 | {
16 | // It will print the initial node
17 | // until it finds NULL for the Next pointer
18 | // that indicate the end of the Node Chain
19 | while(node != NULL)
20 | {
21 | cout << node->Value << " -> ";
22 | node = node->Next;
23 | }
24 |
25 | cout << "NULL" << endl;
26 | }
27 |
28 | int main()
29 | {
30 | // +------+------+
31 | // | 7 | NULL |
32 | // +------+------+
33 | Node * node1 = new Node;
34 | node1->Value = 7;
35 |
36 | // +------+------+
37 | // | 14 | NULL |
38 | // +------+------+
39 | Node * node2 = new Node;
40 | node2->Value = 14;
41 |
42 | // +------+------+
43 | // | 21 | NULL |
44 | // +------+------+
45 | Node * node3 = new Node;
46 | node3->Value = 21;
47 |
48 | // +------+------+ +------+------+ +------+------+
49 | // | 7 | +---->| 14 | NULL | | 21 | NULL |
50 | // +------+------+ +------+------+ +------+------+
51 | node1->Next = node2;
52 |
53 | // +------+------+ +------+------+ +------+------+
54 | // | 7 | +---->| 14 | +---->| 21 | NULL |
55 | // +------+------+ +------+------+ +------+------+
56 | node2->Next = node3;
57 |
58 | // Print the node
59 | PrintNode(node1);
60 |
61 | return 0;
62 | }
63 |
--------------------------------------------------------------------------------
/Chapter02/Node_Chain_Template/Node_Chain_Template.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter02/Node_Chain_Template/main.cpp:
--------------------------------------------------------------------------------
1 | // Project: Node_Chain_Template.cbp
2 | // File : main.cpp
3 | #include
4 |
5 | using namespace std;
6 |
7 | template
8 | class Node
9 | {
10 | public:
11 | T Value;
12 | Node * Next;
13 |
14 | Node(T value) : Value(value){}
15 | };
16 |
17 | template
18 | void PrintNode(Node * node)
19 | {
20 | // It will print the initial node
21 | // until it finds NULL for the Next pointer
22 | // that indicate the end of the Node Chain
23 | while(node != NULL)
24 | {
25 | cout << node->Value << " -> ";
26 | node = node->Next;
27 | }
28 |
29 | cout << "NULL" << endl;
30 | }
31 |
32 | int main()
33 | {
34 | // +------+------+
35 | // | 4.93 | NULL |
36 | // +------+------+
37 | Node * node1 = new Node(4.93);
38 |
39 | // +------+------+
40 | // | 6.45 | NULL |
41 | // +------+------+
42 | Node * node2 = new Node(6.45);
43 |
44 | // +------+------+
45 | // | 8.17 | NULL |
46 | // +------+------+
47 | Node * node3 = new Node(8.17);
48 |
49 | // +------+------+ +------+------+ +------+------+
50 | // | 4.93 | +---->| 6.45 | NULL | | 8.17 | NULL |
51 | // +------+------+ +------+------+ +------+------+
52 | node1->Next = node2;
53 |
54 | // +------+------+ +------+------+ +------+------+
55 | // | 4.93 | +---->| 6.45 | +---->| 8.17 | NULL |
56 | // +------+------+ +------+------+ +------+------+
57 | node2->Next = node3;
58 |
59 | // Print the node
60 | PrintNode(node1);
61 |
62 | return 0;
63 | }
64 |
--------------------------------------------------------------------------------
/Chapter02/Singly_Linked_List.7z:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter02/Singly_Linked_List.7z
--------------------------------------------------------------------------------
/Chapter02/Singly_Linked_List/Singly_Linked_List.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
--------------------------------------------------------------------------------
/Chapter02/Singly_Linked_List/include/Node.h:
--------------------------------------------------------------------------------
1 | // Project: Singly_Linked_List.cbp
2 | // File : Node.h
3 | #ifndef NODE_H
4 | #define NODE_H
5 |
6 | #include
7 |
8 | template
9 | class Node
10 | {
11 | public:
12 | T Value;
13 | Node * Next;
14 |
15 | Node(T value);
16 | };
17 |
18 | template
19 | Node::Node(T value) : Value(value), Next(NULL) {}
20 |
21 | #endif // NODE_H
22 |
--------------------------------------------------------------------------------
/Chapter02/Singly_Linked_List/main.cpp:
--------------------------------------------------------------------------------
1 | // Project: Singly_Linked_List.cbp
2 | // File : main.cpp
3 | #include
4 | #include "Node.h"
5 | #include "LinkedList.h"
6 |
7 | using namespace std;
8 |
9 | int main()
10 | {
11 | // NULL
12 | LinkedList linkedList = LinkedList();
13 |
14 | // 43->NULL
15 | linkedList.InsertHead(43);
16 |
17 | // 76->43->NULL
18 | linkedList.InsertHead(76);
19 |
20 | // 76->43->15->NULL
21 | linkedList.InsertTail(15);
22 |
23 | // 76->43->15->44->NULL
24 | linkedList.InsertTail(44);
25 |
26 | // Print the list element
27 | cout << "First Printed:" << endl;
28 | linkedList.PrintList();
29 | cout << endl;
30 |
31 | // 76->43->15->44->100->NULL
32 | linkedList.Insert(4, 100);
33 |
34 | // 76->43->15->48->44->100->NULL
35 | linkedList.Insert(3, 48);
36 |
37 | // 22->76->43->15->48->44->100->NULL
38 | linkedList.Insert(0, 22);
39 |
40 | // Print the list element
41 | cout << "Second Printed:" << endl;
42 | linkedList.PrintList();
43 | cout << endl;
44 |
45 | // Get value of the second index
46 | // It should be 43
47 | cout << "Get value of the second index:" << endl;
48 | Node * get = linkedList.Get(2);
49 | if(get != NULL)
50 | cout << get->Value;
51 | else
52 | cout << "not found";
53 | cout << endl << endl;
54 |
55 | // Find the position of value 15
56 | // It must be 3
57 | cout << "The position of value 15:" << endl;
58 | int srch = linkedList.Search(15);
59 | cout << srch << endl << endl;
60 |
61 | // Remove first element
62 | cout << "Remove the first element:" << endl;
63 | linkedList.Remove(0);
64 | // 76->43->15->48->44->100->NULL
65 | linkedList.PrintList();
66 | cout << endl;
67 |
68 | // Remove fifth element
69 | cout << "Remove the fifth element:" << endl;
70 | linkedList.Remove(4);
71 | // 76->43->15->48->100->NULL
72 | linkedList.PrintList();
73 | cout << endl;
74 |
75 | // Remove tenth element
76 | cout << "Remove the tenth element:" << endl;
77 | linkedList.Remove(9);
78 | // Nothing happen
79 | // 76->43->15->48->100->NULL
80 | linkedList.PrintList();
81 | cout << endl;
82 |
83 | return 0;
84 | }
85 |
--------------------------------------------------------------------------------
/Chapter02/Singly_Linked_List/src/LinkedList.cpp:
--------------------------------------------------------------------------------
1 | // Project: Singly_Linked_List.cbp
2 | // File : LinkedList.cpp
3 |
--------------------------------------------------------------------------------
/Chapter02/Singly_Linked_List/src/Node.cpp:
--------------------------------------------------------------------------------
1 | // Project: Singly_Linked_List.cbp
2 | // File : Node.cpp
3 |
--------------------------------------------------------------------------------
/Chapter02/Std_List/Std_List.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter02/Vector/Vector.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter03/Deque/Deque.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Chapter03/Deque/main.cpp:
--------------------------------------------------------------------------------
1 | // Project: Deque.cbp
2 | // File : main.cpp
3 | #include
4 | #include "Deque.h"
5 |
6 | using namespace std;
7 |
8 | int main()
9 | {
10 | // NULL
11 | Deque deque = Deque();
12 |
13 | // Enqueue several numbers to the deque
14 | // 26
15 | deque.EnqueueFront(26);
16 | // 26 - 78
17 | deque.EnqueueBack(78);
18 | // 26 - 78 - 44
19 | deque.EnqueueBack(44);
20 | // 91 - 26 - 78 - 44
21 | deque.EnqueueFront(91);
22 | // 35 - 91 - 26 - 78 - 44
23 | deque.EnqueueFront(35);
24 | // 35 - 91 - 26 - 78 - 44 - 12
25 | deque.EnqueueBack(12);
26 |
27 | // list the element of queue
28 | while(!deque.IsEmpty())
29 | {
30 | // Get the front element
31 | cout << deque.Back() << " - ";
32 |
33 | // Remove the front element
34 | deque.DequeueBack();
35 | }
36 | cout << "END" << endl;
37 |
38 | return 0;
39 | }
40 |
--------------------------------------------------------------------------------
/Chapter03/Deque/src/Deque.cpp:
--------------------------------------------------------------------------------
1 | // Project: Deque.cbp
2 | // File : Deque.cpp
3 | #include "Deque.h"
4 |
--------------------------------------------------------------------------------
/Chapter03/Node/DoublyNode.h:
--------------------------------------------------------------------------------
1 | // DoublyNode.h
2 | #ifndef DOUBLYNODE_H
3 | #define DOUBLYNODE_H
4 |
5 | #include
6 |
7 | template
8 | class DoublyNode
9 | {
10 | public:
11 | T Value;
12 | DoublyNode * Previous;
13 | DoublyNode * Next;
14 |
15 | DoublyNode(T value);
16 | };
17 |
18 | template
19 | DoublyNode::DoublyNode(T value)
20 | : Value(value), Previous(NULL), Next(NULL) {}
21 |
22 | #endif // DOUBLYNODE_H
23 |
--------------------------------------------------------------------------------
/Chapter03/Node/Node.h:
--------------------------------------------------------------------------------
1 | // Node.h
2 | #ifndef NODE_H
3 | #define NODE_H
4 |
5 | #include
6 |
7 | template
8 | class Node
9 | {
10 | public:
11 | T Value;
12 | Node * Next;
13 |
14 | Node(T value);
15 | };
16 |
17 | template
18 | Node::Node(T value) : Value(value), Next(NULL) {}
19 |
20 | #endif // NODE_H
21 |
--------------------------------------------------------------------------------
/Chapter03/Parenthesis_Validity/Parenthesis_Validity.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter03/Parenthesis_Validity/main.cpp:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Chapter03/Queue/Queue.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Chapter03/Queue/include/Queue.h:
--------------------------------------------------------------------------------
1 | // Project: Queue.cbp
2 | // File : Queue.h
3 | #ifndef QUEUE_H
4 | #define QUEUE_H
5 |
6 | #include "../../Node/Node.h"
7 |
8 | template
9 | class Queue
10 | {
11 | private:
12 | int m_count;
13 | Node * m_front;
14 | Node * m_back;
15 |
16 | public:
17 | Queue();
18 | bool IsEmpty();
19 | T Front();
20 | void Enqueue(T val);
21 | void Dequeue();
22 | };
23 |
24 | template
25 | Queue::Queue() : m_count(0), m_front(NULL), m_back(NULL) {}
26 |
27 | template
28 | bool Queue::IsEmpty()
29 | {
30 | // return TRUE if there's at least one item
31 | // otherwise, return FALSE
32 | return m_count <= 0;
33 | }
34 |
35 | template
36 | T Queue::Front()
37 | {
38 | // Just return the value
39 | // of m_front node
40 | return m_front->Value;
41 | }
42 |
43 | template
44 | void Queue::Enqueue(T val)
45 | {
46 | // Create a new Node
47 | Node * node = new Node(val);
48 |
49 | if(m_count == 0)
50 | {
51 | // If the queue is empty
52 | // the new node will be
53 | // m_front and m_back
54 | node->Next = NULL;
55 | m_front = node;
56 | m_back = m_front;
57 | }
58 | else
59 | {
60 | // If there's at least one element
61 | // in the queue, the current m_back element
62 | // won't be the Back element anymore
63 | // so the Next pointer of the current m_back
64 | // point to the new node
65 | m_back->Next = node;
66 |
67 | // The new Node now become the Back position
68 | m_back = node;
69 | }
70 |
71 | // One element is added
72 | m_count++;
73 | }
74 |
75 | template
76 | void Queue::Dequeue()
77 | {
78 | // Do nothing if list is empty
79 | if(m_count == 0)
80 | return;
81 |
82 | // Save the current Front
83 | // to a new node
84 | Node * node = m_front;
85 |
86 | // Point the Front pointer
87 | // to the element next to the current Front
88 | m_front = m_front->Next;
89 |
90 | // Now it's safe to remove
91 | // the first element
92 | delete node;
93 |
94 | // One element is removed
95 | m_count--;
96 | }
97 |
98 | #endif // QUEUE_H
99 |
--------------------------------------------------------------------------------
/Chapter03/Queue/main.cpp:
--------------------------------------------------------------------------------
1 | // Project: Queue.cbp
2 | // File : main.cpp
3 | #include
4 | #include "Queue.h"
5 |
6 | using namespace std;
7 |
8 | int main()
9 | {
10 | // NULL
11 | Queue queueInt = Queue();
12 |
13 | // Enqueue several numbers to the queue
14 | queueInt.Enqueue(35);
15 | queueInt.Enqueue(91);
16 | queueInt.Enqueue(26);
17 | queueInt.Enqueue(78);
18 | queueInt.Enqueue(44);
19 | queueInt.Enqueue(12);
20 |
21 | // list the element of queue
22 | while(!queueInt.IsEmpty())
23 | {
24 | // Get the front element
25 | cout << queueInt.Front() << " - ";
26 |
27 | // Remove the front element
28 | queueInt.Dequeue();
29 | }
30 | cout << "END" << endl;
31 |
32 | return 0;
33 | }
34 |
--------------------------------------------------------------------------------
/Chapter03/Queue/src/Queue.cpp:
--------------------------------------------------------------------------------
1 | // Project: Queue.cbp
2 | // File : Queue.cpp
3 | #include "Queue.h"
4 |
--------------------------------------------------------------------------------
/Chapter03/Stack/Stack.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Chapter03/Stack/include/Stack.h:
--------------------------------------------------------------------------------
1 | // Project: Stack.cbp
2 | // File : Stack.h
3 | #ifndef STACK_H
4 | #define STACK_H
5 |
6 | #include "../../Node/Node.h"
7 |
8 | template
9 | class Stack
10 | {
11 | private:
12 | int m_count;
13 | Node * m_top;
14 |
15 | public:
16 | Stack();
17 | bool IsEmpty();
18 | T Top();
19 | void Push(T val);
20 | void Pop();
21 | };
22 |
23 | template
24 | Stack::Stack() : m_count(0), m_top(NULL) {}
25 |
26 | template
27 | bool Stack::IsEmpty()
28 | {
29 | // return TRUE if there are no items
30 | // otherwise, return FALSE
31 | return m_count <= 0;
32 | }
33 |
34 | template
35 | T Stack::Top()
36 | {
37 | // Just return the value
38 | // of m_top node
39 | return m_top->Value;
40 | }
41 |
42 | template
43 | void Stack::Push(T val)
44 | {
45 | // Create a new Node
46 | Node * node = new Node(val);
47 |
48 | // The Next pointer of this new node
49 | // will point to current m_top node
50 | node->Next = m_top;
51 |
52 | // The new Node now becomes the m_top node
53 | m_top = node;
54 |
55 | // One item is added
56 | m_count++;
57 | }
58 |
59 | template
60 | void Stack::Pop()
61 | {
62 | // Do nothing if Stack is empty
63 | if(IsEmpty())
64 | return;
65 |
66 | // Prepare the current m_top
67 | // to remove
68 | Node * node = m_top;
69 |
70 | // The new m_top node will be
71 | // the Next pointer of the current m_top node
72 | m_top = m_top->Next;
73 |
74 | // Now it's safe to remove
75 | // the first element
76 | delete node;
77 |
78 | // One item is removed
79 | m_count--;
80 | }
81 |
82 | #endif // STACK_H
83 |
--------------------------------------------------------------------------------
/Chapter03/Stack/main.cpp:
--------------------------------------------------------------------------------
1 | // Project: Stack.cbp
2 | // File : main.cpp
3 | #include
4 | #include "Stack.h"
5 |
6 | using namespace std;
7 |
8 | int main()
9 | {
10 | // NULL
11 | Stack stackInt = Stack();
12 |
13 | // Store several numbers to the stack
14 | stackInt.Push(32);
15 | stackInt.Push(47);
16 | stackInt.Push(18);
17 | stackInt.Push(59);
18 | stackInt.Push(88);
19 | stackInt.Push(91);
20 |
21 | // list the element of stack
22 | while(!stackInt.IsEmpty())
23 | {
24 | // Get the top element
25 | cout << stackInt.Top() << " - ";
26 |
27 | // Remove the top element
28 | stackInt.Pop();
29 | }
30 | cout << "END" << endl;
31 |
32 | return 0;
33 | }
34 |
--------------------------------------------------------------------------------
/Chapter03/Stack/src/Stack.cpp:
--------------------------------------------------------------------------------
1 | // Project: Stack.cbp
2 | // File : Stack.cpp
3 | #include "Stack.h"
4 |
--------------------------------------------------------------------------------
/Chapter04/Bubble_Sort/Bubble_Sort.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter04/Bubble_Sort/Bubble_Sort.cpp:
--------------------------------------------------------------------------------
1 | // Project: Bubble_Sort.cbp
2 | // File : Bubble_Sort.cpp
3 |
4 | #include
5 |
6 | using namespace std;
7 |
8 | void BubbleSort(int arr[], int arrSize)
9 | {
10 | // Flag for swapping element
11 | bool isSwapped;
12 |
13 | // The value will be decreased
14 | // every time one element has been sorted
15 | int unsortedElements = arrSize;
16 | do
17 | {
18 | // If there's at least two element are swapped
19 | // it will be true
20 | isSwapped = false;
21 |
22 | // Iterate through the array's element
23 | for(int i = 0; i < unsortedElements - 1; ++i)
24 | {
25 | if (arr[i] > arr[i+1])
26 | {
27 | swap(arr[i], arr[i+1]);
28 | isSwapped = true;
29 | }
30 | }
31 |
32 | // After iteration, the last element has been sorted
33 | // so it will be ignored in the next iteration
34 | --unsortedElements;
35 | }
36 | // Do comparison again if there's swapped element
37 | // otherwise, all array's elements have been sorted
38 | while(isSwapped);
39 | }
40 |
41 | int main()
42 | {
43 | cout << "Bubble Sort" << endl;
44 |
45 | // Initialize a new array
46 | int arr[] = {43, 21, 26, 38, 17, 30};
47 | int arrSize = sizeof(arr)/sizeof(*arr);
48 |
49 | // Display the initial array
50 | cout << "Initial array: ";
51 | for (int i=0; i < arrSize; ++i)
52 | cout << arr[i] << " ";
53 | cout << endl;
54 |
55 | // Sort the array with BubbleSort algorithm
56 | BubbleSort(arr, arrSize);
57 |
58 | // Display the sorted array
59 | cout << "Sorted array : ";
60 | for (int i=0; i < arrSize; ++i)
61 | cout << arr[i] << " ";
62 | cout << endl;
63 |
64 | return 0;
65 | }
66 |
--------------------------------------------------------------------------------
/Chapter04/Counting_Sort/Counting_Sort.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter04/Counting_Sort/Counting_Sort.cpp:
--------------------------------------------------------------------------------
1 | // Project: Counting_Sort.cbp
2 | // File : Counting_Sort.cpp
3 |
4 | #include
5 |
6 | using namespace std;
7 |
8 | void CountingSort(int arr[], int arrSize)
9 | {
10 | // Create key/counting array
11 | // with assumption that all element value
12 | // are from 0 to 9
13 | int counterSize = 10;
14 | int * counterArray = new int [counterSize];
15 |
16 | // Increase the respective counter by 1
17 | for(int i = 0; i < arrSize; ++i)
18 | {
19 | ++counterArray[arr[i]];
20 | }
21 |
22 | // Counter for iterating the arrCounter array
23 | int arrCounter = 0;
24 |
25 | for(int i = 0; i < counterSize; ++i)
26 | {
27 | while(counterArray[i] > 0)
28 | {
29 | // Restore element to list
30 | arr[arrCounter++] = i;
31 |
32 | // Decrease counter by 1
33 | --counterArray[i];
34 | }
35 | }
36 | }
37 |
38 | int main()
39 | {
40 | cout << "Counting Sort" << endl;
41 |
42 | // Initialize a new array
43 | int arr[] = {9, 6, 5, 6, 1, 7, 2, 4, 3, 5, 7, 7, 9, 6};
44 | int arrSize = sizeof(arr)/sizeof(*arr);
45 |
46 | // Display the initial array
47 | cout << "Initial array: ";
48 | for (int i=0; i < arrSize; ++i)
49 | cout << arr[i] << " ";
50 | cout << endl;
51 |
52 | // Sort the array with BubbleSort algorithm
53 | CountingSort(arr, arrSize);
54 |
55 | // Display the sorted array
56 | cout << "Sorted array : ";
57 | for (int i=0; i < arrSize; ++i)
58 | cout << arr[i] << " ";
59 | cout << endl;
60 |
61 | return 0;
62 | }
63 |
--------------------------------------------------------------------------------
/Chapter04/Insertion_Sort/Insertion_Sort.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter04/Insertion_Sort/Insertion_Sort.cpp:
--------------------------------------------------------------------------------
1 | // Project: Insertion_Sort.cbp
2 | // File : Insertion_Sort.cpp
3 |
4 | #include
5 |
6 | using namespace std;
7 |
8 | void InsertionSort(int arr[], int arrSize)
9 | {
10 | // Iterate to all array's element
11 | for(int i = 0; i < arrSize; ++i)
12 | {
13 | // Set the current element
14 | // as reference value
15 | int refValue = arr[i];
16 |
17 | // variable to shift the element
18 | // to right position
19 | int j;
20 |
21 | // Iterate through the sorted element
22 | // to insert the reference value
23 | // in right position
24 | for(j = i - 1; j >= 0; --j)
25 | {
26 | // if the value of the current index
27 | // is greater than the reference value then
28 | // move the current value to right side
29 | // otherwise, put the reference value
30 | // in the current index
31 | if(arr[j] > refValue)
32 | arr[j+1] = arr[j];
33 | else
34 | break;
35 | }
36 |
37 | // here's the line to put the reference value
38 | // in the current index (the right position)
39 | arr[j + 1] = refValue;
40 | }
41 | }
42 |
43 | int main()
44 | {
45 | cout << "Insertion Sort" << endl;
46 |
47 | // Initialize a new array
48 | int arr[] = {43, 21, 26, 38, 17, 30};
49 | int arrSize = sizeof(arr)/sizeof(*arr);
50 |
51 | // Display the initial array
52 | cout << "Initial array: ";
53 | for (int i=0; i < arrSize; ++i)
54 | cout << arr[i] << " ";
55 | cout << endl;
56 |
57 | // Sort the array with InsertionSort algorithm
58 | InsertionSort(arr, arrSize);
59 |
60 | // Display the sorted array
61 | cout << "Sorted array : ";
62 | for (int i=0; i < arrSize; ++i)
63 | cout << arr[i] << " ";
64 | cout << endl;
65 |
66 | return 0;
67 | }
68 |
--------------------------------------------------------------------------------
/Chapter04/Merge_Sort/Merge_Sort.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter04/Quick_Sort/Quick_Sort.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter04/Radix_Sort/Node.h:
--------------------------------------------------------------------------------
1 | // Project: Radix_Sort.cbp
2 | // File : Node.h
3 | #ifndef NODE_H
4 | #define NODE_H
5 |
6 | #include
7 |
8 | template
9 | class Node
10 | {
11 | public:
12 | T Value;
13 | Node * Next;
14 |
15 | Node(T value);
16 | };
17 |
18 | template
19 | Node::Node(T value) : Value(value), Next(NULL) {}
20 |
21 | #endif // NODE_H
22 |
--------------------------------------------------------------------------------
/Chapter04/Radix_Sort/Queue.h:
--------------------------------------------------------------------------------
1 | // Project: Radix_Sort.cbp
2 | // File : Queue.h
3 | #ifndef QUEUE_H
4 | #define QUEUE_H
5 |
6 | #include "Node.h"
7 |
8 | template
9 | class Queue
10 | {
11 | private:
12 | int m_count;
13 | Node * m_front;
14 | Node * m_back;
15 |
16 | public:
17 | Queue();
18 | bool IsEmpty();
19 | T Front();
20 | void Enqueue(T val);
21 | void Dequeue();
22 | };
23 |
24 | template
25 | Queue::Queue() : m_count(0), m_front(NULL), m_back(NULL) {}
26 |
27 | template
28 | bool Queue::IsEmpty()
29 | {
30 | // return TRUE if there's at least one item
31 | // otherwise, return FALSE
32 | return m_count <= 0;
33 | }
34 |
35 | template
36 | T Queue::Front()
37 | {
38 | // Just return the value
39 | // of m_front node
40 | return m_front->Value;
41 | }
42 |
43 | template
44 | void Queue::Enqueue(T val)
45 | {
46 | // Create a new Node
47 | Node * node = new Node(val);
48 |
49 | if(m_count == 0)
50 | {
51 | // If the queue is empty
52 | // the new node will be
53 | // m_front and m_back
54 | node->Next = NULL;
55 | m_front = node;
56 | m_back = m_front;
57 | }
58 | else
59 | {
60 | // If there's at least one element
61 | // in the queue, the current m_back element
62 | // won't be the Back element anymore
63 | // so the Next pointer of the current m_back
64 | // point to the new node
65 | m_back->Next = node;
66 |
67 | // The new Node now become the Back position
68 | m_back = node;
69 | }
70 |
71 | // One element is added
72 | m_count++;
73 | }
74 |
75 | template
76 | void Queue::Dequeue()
77 | {
78 | // Do nothing if list is empty
79 | if(m_count == 0)
80 | return;
81 |
82 | // Save the current Front
83 | // to a new node
84 | Node * node = m_front;
85 |
86 | // Point the Front pointer
87 | // to the element next to the current Front
88 | m_front = m_front->Next;
89 |
90 | // Now it's safe to remove
91 | // the first element
92 | delete node;
93 |
94 | // One element is removed
95 | m_count--;
96 | }
97 |
98 | #endif // QUEUE_H
99 |
--------------------------------------------------------------------------------
/Chapter04/Radix_Sort/Radix_Sort.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter04/Radix_Sort/Radix_Sort.cpp:
--------------------------------------------------------------------------------
1 | // Project: Radix_Sort.cbp
2 | // File : Radix_Sort.cpp
3 |
4 | #include
5 | #include "Queue.h"
6 |
7 | using namespace std;
8 |
9 | void RadixSort(int arr[], int arrSize)
10 | {
11 | // Create ten buckets for each digits
12 | // (0 - 9)
13 | Queue * buckets = new Queue[10];
14 |
15 | // Find the largest element
16 | int largestElement = arr[0];
17 | for(int i = 0; i < arrSize; ++i)
18 | {
19 | if(largestElement < arr[i])
20 | largestElement = arr[i];
21 | }
22 |
23 | // Iterate through every digit
24 | // using exponetial (10^exp) to find the digit
25 | for(int exp = 1; largestElement/exp > 0; exp *= 10)
26 | {
27 | // Iterate the array's elements
28 | for(int i = 0; i < arrSize; ++i)
29 | {
30 | // Move element into respective bucket
31 | buckets[(arr[i]/exp)%10].Enqueue(arr[i]);
32 | }
33 |
34 | // Reconstruct the array starting from
35 | // the smallest digit in the buckets
36 | // Reset the array counter before reconstructing
37 | int arrCounter = 0;
38 | for(int i = 0; i < 10; ++i)
39 | {
40 | // Get all elements from the buckets
41 | while(!buckets[i].IsEmpty())
42 | {
43 | // Get the front element
44 | // then restore element to array
45 | arr[arrCounter++] = buckets[i].Front();
46 |
47 | // Remove the front element
48 | buckets[i].Dequeue();
49 | }
50 | }
51 | }
52 |
53 | return;
54 | }
55 |
56 | int main()
57 | {
58 | cout << "Radix Sort" << endl;
59 |
60 | // Initialize a new array
61 | int arr[] = {429, 3309, 65, 7439, 12, 9954, 30, 4567, 8, 882};
62 | int arrSize = sizeof(arr)/sizeof(*arr);
63 |
64 | // Display the initial array
65 | cout << "Initial array: ";
66 | for (int i=0; i < arrSize; ++i)
67 | cout << arr[i] << " ";
68 | cout << endl;
69 |
70 | // Sort the array with QuickSort algorithm
71 | RadixSort(arr, arrSize);
72 |
73 | // Display the sorted array
74 | cout << "Sorted array : ";
75 | for (int i=0; i < arrSize; ++i)
76 | cout << arr[i] << " ";
77 | cout << endl;
78 |
79 | return 0;
80 | }
81 |
--------------------------------------------------------------------------------
/Chapter04/Selection_Sort/Selection_Sort.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter04/Selection_Sort/Selection_Sort.cpp:
--------------------------------------------------------------------------------
1 | // Project: Selection_Sort.cbp
2 | // File : Selection_Sort.cpp
3 |
4 | #include
5 |
6 | using namespace std;
7 |
8 | void SelectionSort(int arr[], int arrSize)
9 | {
10 | // variable to store the index of the minimum value
11 | // in each iteration
12 | int minIndex;
13 |
14 | // Iterate until the N-1 elements
15 | for(int i = 0; i < arrSize - 1; ++i)
16 | {
17 | // Set the first unsorted element
18 | // as the minimum value
19 | minIndex = i;
20 |
21 | // Iterate through the unsorted elements only
22 | for(int j = i + 1; j < arrSize; ++j)
23 | {
24 | // set the new minimum value
25 | // if the saved minimum value is higher
26 | // than the current index value
27 | if (arr[j] < arr[minIndex])
28 | minIndex = j;
29 | }
30 |
31 | // Swap the the first unsorted element
32 | // with the minimum value
33 | swap(arr[i], arr[minIndex]);
34 | }
35 | }
36 |
37 | int main()
38 | {
39 | cout << "Selection Sort" << endl;
40 |
41 | // Initialize a new array
42 | int arr[] = {43, 21, 26, 38, 17, 30};
43 | int arrSize = sizeof(arr)/sizeof(*arr);
44 |
45 | // Display the initial array
46 | cout << "Initial array: ";
47 | for (int i = 0; i < arrSize; ++i)
48 | cout << arr[i] << " ";
49 | cout << endl;
50 |
51 | // Sort the array with SelectionSort algorithm
52 | SelectionSort(arr, arrSize);
53 |
54 | // Display the sorted array
55 | cout << "Sorted array : ";
56 | for (int i = 0; i < arrSize; ++i)
57 | cout << arr[i] << " ";
58 | cout << endl;
59 |
60 | return 0;
61 | }
62 |
--------------------------------------------------------------------------------
/Chapter05/Binary_Search/Binary_Search.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter05/Binary_Search/Binary_Search.cpp:
--------------------------------------------------------------------------------
1 | // Project: Binary_Search.cbp
2 | // File : Binary_Search.cpp
3 |
4 | #include
5 |
6 | using namespace std;
7 |
8 | int BinarySearch(
9 | int arr[],
10 | int startIndex,
11 | int endIndex,
12 | int val)
13 | {
14 | // Only perform searching process
15 | // if the end index is higher than
16 | // or equals to start index
17 | if(startIndex <= endIndex)
18 | {
19 | // Find middle index
20 | int middleIndex = startIndex + (endIndex - startIndex) / 2;
21 |
22 | // If the middle index's value is the searched value
23 | // then return the index
24 | if(arr[middleIndex] == val)
25 | {
26 | return middleIndex;
27 | }
28 | // If the middle index's value is greater than the searched value
29 | // then perform another Binary Search to the left sub array
30 | // arr[startIndex ... middleIndex - 1]
31 | else if(arr[middleIndex] > val)
32 | {
33 | return BinarySearch(arr, startIndex, middleIndex - 1, val);
34 | }
35 | // If the middle index's value is lower than the searched value
36 | // then perform another Binary Search to the left sub array
37 | // arr[middleIndex + 1 ... endIndex]
38 | else
39 | {
40 | return BinarySearch(arr, middleIndex + 1, endIndex, val);
41 | }
42 | }
43 |
44 | // Just in case no any value found
45 | return -1;
46 | }
47 |
48 | int main()
49 | {
50 | cout << "Binary Search" << endl;
51 |
52 | // Initialize a new array
53 | int arr[] = {3, 8, 11, 15, 16, 23, 28, 30, 32, 39, 42, 44, 47, 48, 50};
54 | int arrSize = sizeof(arr)/sizeof(*arr);
55 |
56 | // Define value to be searched
57 | int searchedValue = 16;
58 |
59 | // Find the searched value using Binary Search
60 | int i = BinarySearch(arr, 0, arrSize - 1, searchedValue);
61 |
62 | // Notify user the result
63 | // if the return is not -1,
64 | // the searched value is found
65 | if(i != -1)
66 | {
67 | cout << searchedValue << " is found in index ";
68 | cout << i << endl;
69 | }
70 | else
71 | {
72 | cout << "Could not find value " << searchedValue;
73 | cout << endl;
74 | }
75 |
76 | return 0;
77 | }
78 |
--------------------------------------------------------------------------------
/Chapter05/Exponential_Search/Exponential_Search.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter05/Interpolation_Search/Interpolation_Search.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter05/Jump_Search/Jump_Search.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter05/Linear_Search/Linear_Search.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter05/Linear_Search/Linear_Search.cpp:
--------------------------------------------------------------------------------
1 | // Project: Linear_Search.cbp
2 | // File : Linear_Search.cpp
3 | #include
4 |
5 | using namespace std;
6 |
7 | int LinearSearch(
8 | int arr[],
9 | int startIndex,
10 | int endIndex,
11 | int val)
12 | {
13 | // Iterate through the start index
14 | // to the end index and
15 | // return the searched value's index
16 | for(int i = startIndex; i < endIndex; ++i)
17 | {
18 | if(arr[i] == val)
19 | {
20 | return i;
21 | }
22 | }
23 |
24 | // return -1 if no val is found
25 | return -1;
26 | }
27 |
28 | int main()
29 | {
30 | cout << "Linear Search" << endl;
31 |
32 | // Initialize a new array
33 | int arr[] = {43, 21, 26, 38, 17, 30, 25, 18};
34 | int arrSize = sizeof(arr)/sizeof(*arr);
35 |
36 | // Define value to be searched
37 | int searchedValue = 30;
38 |
39 | // Find the searched value using Linear Search
40 | int i = LinearSearch(arr, 0, arrSize - 1, searchedValue);
41 |
42 | // Notify user the result
43 | // if the return is not -1,
44 | // the searched value is found
45 | if(i != -1)
46 | {
47 | cout << searchedValue << " is found in index ";
48 | cout << i << endl;
49 | }
50 | else
51 | {
52 | cout << "Could not find value " << searchedValue;
53 | cout << endl;
54 | }
55 |
56 | return 0;
57 | }
58 |
--------------------------------------------------------------------------------
/Chapter05/Sublist_Search/Sublist_Search.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter05/Ternary_Search/Ternary_Search.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter06/Anagram/Anagram:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter06/Anagram/Anagram
--------------------------------------------------------------------------------
/Chapter06/Anagram/Anagram.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter06/Anagram/Anagram.cpp:
--------------------------------------------------------------------------------
1 | // Project: Anagram.cbp
2 | // File : Anagram.cpp
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | using namespace std;
9 |
10 | bool IsAnagram(
11 | string str1,
12 | string str2)
13 | {
14 | // Anagram is not case sensitive
15 | // so we convert all characters
16 | // to uppercase
17 | transform(
18 | str1.begin(),
19 | str1.end(),
20 | str1.begin(),
21 | ::toupper);
22 | transform(
23 | str2.begin(),
24 | str2.end(),
25 | str2.begin(),
26 | ::toupper);
27 |
28 | // Anagram does not care about space
29 | // so we remove all spaces if any
30 | str1.erase(
31 | remove(
32 | str1.begin(),
33 | str1.end(),
34 | ' '),
35 | str1.end());
36 | str2.erase(
37 | remove(
38 | str2.begin(),
39 | str2.end(),
40 | ' '),
41 | str2.end());
42 |
43 | // Then, we sort string1 and string2
44 | sort(str1.begin(), str1.end());
45 | sort(str2.begin(), str2.end());
46 |
47 | // If they both are anagram,
48 | // they will be exactly same after sorted
49 | return str1 == str2;
50 | }
51 |
52 | int main()
53 | {
54 | cout << "Anagram" << endl;
55 |
56 | // Input string1
57 | string string1;
58 | cout << "Input string 1 -> ";
59 | getline(cin, string1);
60 |
61 | // Input string2
62 | string string2;
63 | cout << "Input string 2 -> ";
64 | getline(cin, string2);
65 |
66 | // Check if they are anagram
67 | cout << "'" << string1 << "' and '";
68 | cout << string2 << "' are ";
69 | if(IsAnagram(string1, string2))
70 | {
71 | cout << "anagram";
72 | }
73 | else
74 | {
75 | cout << "NOT anagram";
76 | }
77 | cout << endl;
78 |
79 | return 0;
80 | }
81 |
--------------------------------------------------------------------------------
/Chapter06/Anagram/Anagram.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter06/Anagram/Anagram.o
--------------------------------------------------------------------------------
/Chapter06/BinaryString_To_Decimal/BinaryString_To_Decimal.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter06/BinaryString_To_Decimal/BinaryString_To_Decimal.cpp:
--------------------------------------------------------------------------------
1 | // Project: BinaryString_To_Decimal.cbp
2 | // File : BinaryString_To_Decimal.cpp
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | using namespace std;
9 |
10 | int BinaryStringToDecimal(
11 | string binaryString)
12 | {
13 | // Initial result
14 | int decNumber = 0;
15 |
16 | // variable for current power base
17 | int n;
18 |
19 | // Declare reverse iterator
20 | string::reverse_iterator it;
21 |
22 | // Iterate all characters in binaryString
23 | // from the last character to the first character
24 | for (
25 | it = binaryString.rbegin(), n = 0;
26 | it != binaryString.rend();
27 | ++it, ++n)
28 | {
29 | // if character '1' is found
30 | // add decNumber with power of current base
31 | char c = *it;
32 | if(c == '1')
33 | {
34 | decNumber += pow(2, n);
35 | }
36 | }
37 |
38 | // return the decimal
39 | return decNumber;
40 | }
41 |
42 | int main()
43 | {
44 | cout << "Binary String To Decimal" << endl;
45 |
46 | // Input binary string
47 | string binaryString;
48 | cout << "Input binary string -> ";
49 | getline(cin, binaryString);
50 |
51 | // Convert to decimal
52 | int decNumber = BinaryStringToDecimal(
53 | binaryString);
54 |
55 | // Show the result to user
56 | cout << "Decimal of '" << binaryString;
57 | cout << "' is " << decNumber << endl;
58 |
59 | return 0;
60 | }
61 |
--------------------------------------------------------------------------------
/Chapter06/Characters_Array/Characters_Array.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter06/Characters_Array/Characters_Array.cpp:
--------------------------------------------------------------------------------
1 | // Project: Characters_Array.cbp
2 | // File : Characters_Array.cpp
3 |
4 | #include
5 |
6 | using namespace std;
7 |
8 | int main()
9 | {
10 | char name[] = "Wisnu";
11 |
12 | cout << name << endl;
13 |
14 | return 0;
15 | }
16 |
--------------------------------------------------------------------------------
/Chapter06/Decimal_To_BinaryString/Decimal_To_BinaryString.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter06/Decimal_To_BinaryString/Decimal_To_BinaryString.cpp:
--------------------------------------------------------------------------------
1 | // Project: Decimal_To_BinaryString.cbp
2 | // File : Decimal_To_BinaryString.cpp
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | using namespace std;
9 |
10 | string DecimalToBinaryString(
11 | int decimalNumber)
12 | {
13 | // Initial result
14 | string binaryString = "0";
15 |
16 | // Only perform this procedure
17 | // if decimalNumber is greater than 0
18 | if(decimalNumber > 0)
19 | {
20 | // Reset result
21 | binaryString = "";
22 |
23 | // Declare a variable for division process
24 | div_t dv{};
25 |
26 | // Initialize the division quot
27 | dv.quot = decimalNumber;
28 |
29 | // Perform these procedure
30 | // until the quot is 0
31 | do
32 | {
33 | dv = div(dv.quot, 2);
34 | binaryString = to_string(dv.rem) + binaryString;
35 | }
36 | while(dv.quot);
37 | }
38 |
39 | // return the binary string
40 | return binaryString;
41 | }
42 |
43 | int main()
44 | {
45 | cout << "Decimal To Binary String" << endl;
46 |
47 | // Input a decimal number
48 | int decNum;
49 | cout << "Input a decimal number -> ";
50 | cin >> decNum;
51 |
52 | // Convert to binary string
53 | string binaryString = DecimalToBinaryString(decNum);
54 |
55 | // Show the result to user
56 | cout << "Binary string of " << decNum;
57 | cout << " is '" << binaryString;
58 | cout << "'" << endl;
59 |
60 | return 0;
61 | }
62 |
--------------------------------------------------------------------------------
/Chapter06/Palindrome/Palindrome.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter06/Palindrome/Palindrome.cpp:
--------------------------------------------------------------------------------
1 | // Project: Palindrome.cbp
2 | // File : Palindrome.cpp
3 |
4 | #include
5 | #include
6 |
7 | using namespace std;
8 |
9 | bool IsPalindrome(
10 | string str)
11 | {
12 | // Palindrome is not case sensitive
13 | // so we convert all characters
14 | // to uppercase
15 | transform(
16 | str.begin(),
17 | str.end(),
18 | str.begin(),
19 | ::toupper);
20 |
21 | // Palindrome does not care about space
22 | // so we remove all spaces if any
23 | str.erase(
24 | remove(
25 | str.begin(),
26 | str.end(),
27 | ' '),
28 | str.end());
29 |
30 | // --- Palindrome detector ---
31 | // Starting from leftmost and rightmost elements
32 | // of the str
33 | int left = 0;
34 | int right = str.length() - 1;
35 |
36 | // Comparing the current leftmost
37 | // and rightmost elements
38 | // until all elements are checked or
39 | // until unmatched characters are found
40 | while(right > left)
41 | {
42 | if(str[left++] != str[right--])
43 | {
44 | return false;
45 | }
46 | }
47 |
48 | // If all characters which are compared
49 | // are same, it must be palindrome
50 | return true;
51 | // --- End of palindrome detector ---
52 | }
53 |
54 | int main()
55 | {
56 | cout << "Palindrome" << endl;
57 |
58 | // Input string
59 | string str;
60 | cout << "Input string -> ";
61 | getline(cin, str);
62 |
63 | // Check if it is palindrome
64 | cout << "'" << str << "' is ";
65 | if(IsPalindrome(str))
66 | {
67 | cout << "a palindrome";
68 | }
69 | else
70 | {
71 | cout << "NOT a palindrome";
72 | }
73 | cout << endl;
74 |
75 | return 0;
76 | }
77 |
--------------------------------------------------------------------------------
/Chapter06/Pattern_Searching/Pattern_Searching:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter06/Pattern_Searching/Pattern_Searching
--------------------------------------------------------------------------------
/Chapter06/Pattern_Searching/Pattern_Searching.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter06/Pattern_Searching/Pattern_Searching.o:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/PacktPublishing/CPP-Data-Structures-and-Algorithms/705dbea26fc826510870d3b1276913af3510ad36/Chapter06/Pattern_Searching/Pattern_Searching.o
--------------------------------------------------------------------------------
/Chapter06/Subsequence_Generator/Subsequence_Generator.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter06/Subsequence_Generator/Subsequence_Generator.cpp:
--------------------------------------------------------------------------------
1 | // Project: Subsequence_Generator.cbp
2 | // File : Subsequence_Generator.cpp
3 |
4 | #include
5 | #include
6 | #include
7 |
8 | using namespace std;
9 |
10 | vector GenerateSubsequences(
11 | string str)
12 | {
13 | // Return value is stored
14 | // on vecString
15 | vector vecString;
16 |
17 | // Retrieve str length
18 | int strLen = str.size();
19 |
20 | // bitCounter is used to check
21 | // character position
22 | int bitCounter = pow(2, strLen);
23 |
24 | // Check from 000..001 to 111..111
25 | // Empty subset is ignored
26 | for (int i = 1; i < bitCounter; ++i)
27 | {
28 | // Temporary variable
29 | // to store current subsequence
30 | string subsequence = "";
31 |
32 | // Construct the new subsequence string
33 | for (int j = 0; j < strLen; j++)
34 | {
35 | // Check if j-th bit in the bitCounter is set
36 | // If so, pick j-th character from str
37 | if (i & (1 << j))
38 | {
39 | subsequence += str[j];
40 | }
41 | }
42 |
43 | // Put it to vector
44 | vecString.push_back(subsequence);
45 | }
46 |
47 | // Return the vector
48 | return vecString;
49 | }
50 |
51 | int main()
52 | {
53 | cout << "Subsequence Generator" << endl;
54 |
55 | // Input string
56 | string str;
57 | cout << "Input string -> ";
58 | getline(cin, str);
59 |
60 | // Generate subsequences
61 | vector myvector = GenerateSubsequences(
62 | str);
63 |
64 | // Show the result to user
65 | cout << "All subsequences of '" << str;
66 | cout << "':" << endl;
67 | for (
68 | vector::iterator it = myvector.begin();
69 | it != myvector.end();
70 | ++it)
71 | {
72 | cout << *it << " ";
73 | }
74 | cout << endl;
75 |
76 | return 0;
77 | }
78 |
--------------------------------------------------------------------------------
/Chapter06/Subsequence_String/Subsequence_String.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter06/Subsequence_String/Subsequence_String.cpp:
--------------------------------------------------------------------------------
1 | // Project: Subsequence_String.cbp
2 | // File : Subsequence_String.cpp
3 |
4 | #include
5 |
6 | using namespace std;
7 |
8 | bool IsSubSequence(
9 | string str1,
10 | string str2,
11 | int x,
12 | int y)
13 | {
14 | // If all characters in str1
15 | // have been checked,
16 | // it means that the str1 is subsequence
17 | // of str2
18 | if (x == 0)
19 | {
20 | return true;
21 | }
22 |
23 | // There is some characters in str2
24 | // that don't matched with str1
25 | // so return false
26 | if (y == 0)
27 | {
28 | return false;
29 | }
30 |
31 | // If last characters of two strings are matching,
32 | // continue to check another character
33 | if (str1[x - 1] == str2[y - 1])
34 | {
35 | return IsSubSequence(str1, str2, x - 1, y - 1);
36 | }
37 | // If not, check the second last of str2
38 | else
39 | {
40 | return IsSubSequence(str1, str2, x, y - 1);
41 | }
42 | }
43 |
44 | int main()
45 | {
46 | cout << "Subsequence String" << endl;
47 | cout << "Check if 1st string is a ";
48 | cout << "subsequence of 2nd string" << endl;
49 |
50 | // Input first string
51 | string string1;
52 | cout << "Input 1st string -> ";
53 | getline(cin, string1);
54 |
55 | // Input second string
56 | string string2;
57 | cout << "Input 2nd string -> ";
58 | getline(cin, string2);
59 |
60 | // Check if str1 is subsequence of str2
61 | cout << "'" << string1 << "' is ";
62 | if(IsSubSequence(
63 | string1,
64 | string2,
65 | string1.size(),
66 | string2.size()))
67 | {
68 | cout << "subsequence";
69 | }
70 | else
71 | {
72 | cout << "NOT subsequence";
73 | }
74 | cout << " of '" << string2;
75 | cout << "'" << endl;
76 |
77 | return 0;
78 | }
79 |
--------------------------------------------------------------------------------
/Chapter07/Binary_Heap/Binary_Heap.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/Chapter07/Binary_Heap/Binary_Heap.cpp:
--------------------------------------------------------------------------------
1 | // Project: Binary_Heap.cbp
2 | // File : Binary_Heap.cpp
3 |
4 | #include
5 | #include "BinaryHeap.h"
6 |
7 | using namespace std;
8 |
9 | int main()
10 | {
11 | cout << "Priority Queue" << endl;
12 |
13 | // Instantiate priority queue
14 | BinaryHeap * priorityQueue =
15 | new BinaryHeap();
16 |
17 | // Check if the queue is empty
18 | // it should be TRUE
19 | cout << "Is queue empty? ";
20 | bool b = priorityQueue->IsEmpty();
21 | if(b)
22 | cout << "TRUE";
23 | else
24 | cout << "FALSE";
25 | cout << endl;
26 |
27 | // Insert a new element
28 | priorityQueue->Insert(14);
29 | cout << "Insert 14 to queue" << endl;
30 |
31 | // Check again if the queue is empty
32 | // it should be FALSE now
33 | cout << "Is queue empty? ";
34 | b = priorityQueue->IsEmpty();
35 | if(b)
36 | cout << "TRUE";
37 | else
38 | cout << "FALSE";
39 | cout << endl;
40 |
41 | // Insert others elements
42 | priorityQueue->Insert(53);
43 | priorityQueue->Insert(8);
44 | priorityQueue->Insert(32);
45 | cout << "Insert 53, 8 and 32 to queue";
46 | cout << endl;
47 |
48 | // Peek the maximum element
49 | // It should be 53
50 | cout << "GetMax() = ";
51 | cout << priorityQueue->GetMax();
52 | cout << endl;
53 |
54 | // Extract maximum element
55 | cout << "ExtractMax() = ";
56 | cout << priorityQueue->ExtractMax();
57 | cout << endl;
58 |
59 | // Peek the maximum element
60 | // It should be 32 now
61 | cout << "GetMax() = ";
62 | cout << priorityQueue->GetMax();
63 | cout << endl;
64 |
65 | return 0;
66 | }
67 |
--------------------------------------------------------------------------------
/Chapter07/Binary_Heap/include/BinaryHeap.h:
--------------------------------------------------------------------------------
1 | #ifndef BINARYHEAP_H
2 | #define BINARYHEAP_H
3 |
4 | #include
5 |
6 | class BinaryHeap
7 | {
8 | private:
9 | std::vector vect;
10 | int heapSize;
11 |
12 | // three helper navigation function
13 | int p(int i) { return i>>1; } // i/2
14 | int l(int i) { return i<<1; } // i*2
15 | int r(int i) { return (i<<1)+1; } // i*2+1
16 |
17 | void ShiftUp(int index);
18 | void ShiftDown(int i);
19 |
20 | public:
21 | BinaryHeap();
22 |
23 | bool IsEmpty();
24 | void Insert(int key);
25 | int ExtractMax();
26 | int GetMax();
27 | };
28 |
29 | #endif // BINARYHEAP_H
30 |
--------------------------------------------------------------------------------
/Chapter07/Binary_Search_Tree/AVL_Tree.cpp:
--------------------------------------------------------------------------------
1 | // Project: Binary_Search_Tree.cbp
2 | // File : AVL_Tree.cpp
3 |
4 | #include
5 | #include "AVLNode.h"
6 |
7 | using namespace std;
8 |
9 | int main()
10 | {
11 | cout << "AVL Tree" << endl;
12 |
13 | // Instantiate BST instance
14 | AVL * avlTree = new AVL;
15 |
16 | // Insert first key
17 | avlTree->Insert(69);
18 |
19 | /*
20 | Add key 62
21 | 69
22 | /
23 | 62
24 | */
25 | avlTree->Insert(62);
26 |
27 | /*
28 | Add key 46
29 | it will rotate right since
30 | the balance = 2
31 | 62
32 | / \
33 | 46 69
34 | */
35 | avlTree->Insert(46);
36 |
37 | /*
38 | Add key 32
39 | 62
40 | / \
41 | 46 69
42 | /
43 | 32
44 | */
45 | avlTree->Insert(32);
46 |
47 | /*
48 | Add key 24
49 | it will rotate right since
50 | the balance = 2
51 | 62
52 | / \
53 | 32 69
54 | / \
55 | 24 46
56 | */
57 | avlTree->Insert(24);
58 |
59 | /*
60 | Add key 13
61 | it will rotate right since
62 | the balance = 2
63 | 32
64 | / \
65 | 24 62
66 | / / \
67 | 13 46 69
68 | */
69 | avlTree->Insert(13);
70 |
71 | // Prove the avlTree
72 | // Successor(32) should be 46
73 | // Successor(13) should be 24
74 | // Successor(69) should be -1 or NULL
75 | cout << "Successor(32) = ";
76 | cout << avlTree->Successor(24) << endl;
77 | cout << "Successor(13) = ";
78 | cout << avlTree->Successor(13) << endl;
79 | cout << "Successor(69) = ";
80 | cout << avlTree->Successor(69) << endl;
81 |
82 | return 0;
83 | }
84 |
--------------------------------------------------------------------------------
/Chapter07/Binary_Search_Tree/Binary_Search_Tree.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/Chapter07/Binary_Search_Tree/include/AVLNode.h:
--------------------------------------------------------------------------------
1 | #ifndef AVLNODE_H
2 | #define AVLNODE_H
3 |
4 | #include "BSTNode.h"
5 |
6 | class AVL : public BST
7 | {
8 | private:
9 | BSTNode * root;
10 |
11 | int GetHeight(BSTNode * node);
12 | BSTNode * RotateLeft(BSTNode * node);
13 | BSTNode * RotateRight(BSTNode * node);
14 | BSTNode * Insert(BSTNode * node, int key);
15 | BSTNode * Remove(BSTNode * node, int key);
16 |
17 | public:
18 | AVL();
19 |
20 | void Insert(int v);
21 | void Remove(int v);
22 | };
23 |
24 | #endif // AVLNODE_H
25 |
--------------------------------------------------------------------------------
/Chapter07/Binary_Search_Tree/include/BSTNode.h:
--------------------------------------------------------------------------------
1 | // Project: Binary_Search_Tree.cbp
2 | // File : BSTNode.h
3 |
4 | #ifndef BSTNODE_H
5 | #define BSTNODE_H
6 |
7 | #include
8 |
9 | class BSTNode
10 | {
11 | public:
12 | int Key;
13 | BSTNode * Left;
14 | BSTNode * Right;
15 | BSTNode * Parent;
16 | int Height;
17 | };
18 |
19 | class BST
20 | {
21 | private:
22 | BSTNode * root;
23 |
24 | protected:
25 | BSTNode * Insert(BSTNode * node, int key);
26 | void PrintTreeInOrder(BSTNode * node);
27 | BSTNode * Search(BSTNode * node, int key);
28 | int FindMin(BSTNode * node);
29 | int FindMax(BSTNode * node);
30 | int Successor(BSTNode * node);
31 | int Predecessor(BSTNode * node);
32 | BSTNode * Remove(BSTNode * node, int v);
33 |
34 | public:
35 | BST();
36 |
37 | void Insert(int key);
38 | void PrintTreeInOrder();
39 | bool Search(int key);
40 | int FindMin();
41 | int FindMax();
42 | int Successor(int key);
43 | int Predecessor(int key);
44 | void Remove(int v);
45 | };
46 |
47 | #endif // BSTNODE_H
48 |
--------------------------------------------------------------------------------
/Chapter07/Binary_Tree/Binary_Tree.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
--------------------------------------------------------------------------------
/Chapter07/Binary_Tree/Binary_Tree.cpp:
--------------------------------------------------------------------------------
1 | // Project: Binary_Tree.cbp
2 | // File : Binary_Tree.cpp
3 |
4 | #include
5 |
6 | using namespace std;
7 |
8 | class TreeNode
9 | {
10 | public:
11 | int Key;
12 | TreeNode * Left;
13 | TreeNode * Right;
14 | };
15 |
16 | TreeNode * NewTreeNode(
17 | int key)
18 | {
19 | // Create a new node
20 | TreeNode * node = new TreeNode;
21 |
22 | // Assign a key
23 | node->Key = key;
24 |
25 | // Initialize Left and Right pointer
26 | node->Left = NULL;
27 | node->Right = NULL;
28 |
29 | return node;
30 | }
31 |
32 | void PrintTreeInOrder(TreeNode * node)
33 | {
34 | if(node == NULL) return;
35 |
36 | PrintTreeInOrder(node->Left);
37 | cout << node->Key << " ";
38 | PrintTreeInOrder(node->Right);
39 | }
40 |
41 | void Print (TreeNode * x, int & id)
42 | {
43 | if (!x) return;
44 |
45 | Print (x->Left,id);
46 |
47 | id++;
48 | cout << id << ' ' << x->Key << endl;
49 |
50 | Print (x->Right,id);
51 | }
52 |
53 | int main()
54 | {
55 | cout << "Binary Tree" << endl;
56 |
57 | // Creating root element
58 | TreeNode * root = NewTreeNode(1);
59 |
60 | /*
61 | Add children to root element
62 | 1
63 | / \
64 | 2 3
65 | */
66 | root->Left = NewTreeNode(2);
67 | root->Right = NewTreeNode(3);
68 |
69 | /*
70 | Add children to element 2
71 | 1
72 | / \
73 | 2 3
74 | / \
75 | 4 5
76 | */
77 | root->Left->Left = NewTreeNode(4);
78 | root->Left->Right = NewTreeNode(5);
79 |
80 | /*
81 | Add children to element 3
82 | 1
83 | / \
84 | 2 3
85 | / \ / \
86 | 4 5 6 7
87 | */
88 | root->Right->Left = NewTreeNode(6);
89 | root->Right->Right = NewTreeNode(7);
90 |
91 | int id = 0;
92 | Print(root, id);
93 |
94 | return 0;
95 | }
96 |
--------------------------------------------------------------------------------
/Chapter08/Hash_Table_OP_LP/Hash_Table_OP_LP.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/Chapter08/Hash_Table_OP_LP/include/HashTable.h:
--------------------------------------------------------------------------------
1 | // Project: Hash_Table_OP_LP.cbp
2 | // File : HashTable.h
3 |
4 | #ifndef HASHTABLE_H
5 | #define HASHTABLE_H
6 |
7 | #include
8 |
9 | #define TABLE_SIZE 7
10 |
11 | class HashElement
12 | {
13 | public:
14 | int Key;
15 | std::string Value;
16 |
17 | HashElement(int key, std::string value)
18 | {
19 | this->Key = key;
20 | this->Value = value;
21 | }
22 | };
23 |
24 | class HashTable
25 | {
26 | private:
27 | int currentSize;
28 | HashElement * *arr;
29 | HashElement * deletedElement;
30 |
31 | int HashFunction(int key);
32 |
33 | public:
34 | HashTable();
35 |
36 | void Insert(int key, std::string value);
37 | std::string Search(int key);
38 | void Remove(int key);
39 | bool IsEmpty();
40 | void PrintHashTable();
41 | };
42 |
43 | #endif // HASHTABLE_H
44 |
--------------------------------------------------------------------------------
/Chapter08/Hash_Table_SC/Hash_Table_SC.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/Chapter08/Hash_Table_SC/include/HashTable.h:
--------------------------------------------------------------------------------
1 | // Project: Hash_Table_SC.cbp
2 | // File : HashTable.h
3 |
4 | #ifndef HASHTABLE_H
5 | #define HASHTABLE_H
6 |
7 | #include
8 | #include
9 |
10 | #define TABLE_SIZE 7
11 |
12 | class HashTable
13 | {
14 | private:
15 | std::list>
16 | tableList[TABLE_SIZE];
17 |
18 | int HashFunction(int key);
19 |
20 | public:
21 | HashTable();
22 |
23 | void Insert(int key, std::string value);
24 | std::string Search(int key);
25 | void Remove(int key);
26 | bool IsEmpty();
27 | };
28 |
29 | #endif // HASHTABLE_H
30 |
--------------------------------------------------------------------------------
/Chapter08/Hash_Table_SC/main.cpp:
--------------------------------------------------------------------------------
1 | // Project: Hash_Table_SC.cbp
2 | // File : main.cpp
3 |
4 | #include
5 | #include "HashTable.h"
6 |
7 | using namespace std;
8 |
9 | int main()
10 | {
11 | cout << "Hash Table - Separate Chaining" << endl;
12 |
13 | HashTable * hashTable = new HashTable();
14 |
15 | // Check if hash table is empty
16 | bool b = hashTable->IsEmpty();
17 | if(b)
18 | cout << "Hash table is empty";
19 | else
20 | cout << "Hash table is not empty";
21 | cout << endl;
22 |
23 | // Adding a data then
24 | // check if hash table is empty
25 | cout << "Adding a data" << endl;
26 | hashTable->Insert(434, "Dylan");
27 | b = hashTable->IsEmpty();
28 | if(b)
29 | cout << "Hash table is empty";
30 | else
31 | cout << "Hash table is not empty";
32 | cout << endl;
33 |
34 | // Adding others data
35 | hashTable->Insert(391, "Dominic");
36 | hashTable->Insert(806, "Adam");
37 | hashTable->Insert(117, "Lindsey");
38 | hashTable->Insert(548, "Cameron");
39 | hashTable->Insert(669, "Terry");
40 | hashTable->Insert(722, "Brynn");
41 | hashTable->Insert(276, "Jody");
42 | hashTable->Insert(953, "Frankie");
43 | hashTable->Insert(895, "Vanessa");
44 |
45 | // Search key 669
46 | // it should be found
47 | int key = 669;
48 | cout << "Search value for key ";
49 | cout << key << endl;
50 | string name = hashTable->Search(key);
51 | if(name != "")
52 | {
53 | cout << "Value for key " << key;
54 | cout << " is " << name;
55 | }
56 | else
57 | {
58 | cout << "Value for key " << key;
59 | cout << " is not found";
60 | }
61 | cout << endl;
62 |
63 | // Remove key 669
64 | // then search the key
65 | // it should not be found
66 | cout << "Remove node of key ";
67 | cout << key << endl;
68 | hashTable->Remove(key);
69 | name = hashTable->Search(key);
70 | if(name != "")
71 | {
72 | cout << "Value for key " << key;
73 | cout << " is " << name;
74 | }
75 | else
76 | {
77 | cout << "Value for key " << key;
78 | cout << " is not found";
79 | }
80 | cout << endl;
81 |
82 | return 0;
83 | }
84 |
--------------------------------------------------------------------------------
/Chapter09/CAPTCHA/CAPTCHA.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter09/CAPTCHA/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace std;
5 |
6 | string GenerateCaptcha(int n)
7 | {
8 | time_t t;
9 |
10 | srand((unsigned) time(&t)); //аll сhаrасtеrѕ
11 |
12 | char * chrs = "аbсdеfghіjklmnорԛrѕtuvwxуzABCDEFGHI"
13 | "JKLMNOPQRSTUVWXYZ0123456789";
14 |
15 | // Generate n сhаrасtеrѕ frоm above ѕеt аnd
16 | // add thеѕе characters tо сарtсhа.
17 | string captcha = "";
18 |
19 | while(--n)
20 | captcha.push_back(chrs[rand() % 62]);
21 |
22 | return captcha;
23 | }
24 |
25 | int main()
26 | {
27 | cout << "CAPTCHA" << endl;
28 | cout << GenerateCaptcha(10) << endl;
29 | return 0;
30 | }
31 |
--------------------------------------------------------------------------------
/Chapter09/Coin_Change_Dynamic_Programming/Coin_Change_Dynamic_Programming.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter09/Coin_Change_Dynamic_Programming/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | int count(int S[], int m, int n)
6 | {
7 | int x, y;
8 |
9 | // Base саѕе (n = 0)
10 | int table[n + 1][m];
11 |
12 | // Fіll thе еntеrіеѕ for 0 vаluе саѕе
13 | // (n = 0)
14 | for (int i = 0; i < m; ++i)
15 | table[0][i] = 1;
16 |
17 | // Fill rеѕt оf the table еntrіеѕ іn bоttоm
18 | // up mаnnеr
19 | for (int i = 1; i < n + 1; ++i)
20 | {
21 | for (int j = 0; j < m; ++j)
22 | {
23 | //solutions соunt іnсludіng S[j]
24 | x = (i - S[j] >= 0) ?
25 | table[i - S[j]][j] :
26 | 0;
27 |
28 | //ѕоlutіоnѕ соunt excluding S[j]
29 | y = (j >= 1) ? table[i][j-1] : 0;
30 |
31 | // tоtаl соunt
32 | table[i][j] = x + y;
33 | }
34 | }
35 |
36 | return table[n][m-1];
37 | }
38 |
39 | int main()
40 | {
41 | cout << "Coin Change Dynamic Programming" << endl;
42 |
43 | int S[] = {2, 5, 3, 6};
44 | int size = 4;
45 | int n = 10;
46 |
47 | cout << count(S, size, n) << endl;
48 | return 0;
49 | }
50 |
--------------------------------------------------------------------------------
/Chapter09/Coin_Change_Problem/Coin_Change_Problem.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter09/Coin_Change_Problem/Coin_Change_Problem.cpp:
--------------------------------------------------------------------------------
1 | // Project: Coin_Change_Problem.cbp
2 | // File : Coin_Change_Problem.cpp
3 |
4 | #include
5 | #include
6 |
7 | using namespace std;
8 |
9 | void MinimalChangeCoin(double changingNominal)
10 | {
11 | // All denominations of U.S. Currency
12 | double denom[] =
13 | {0.01, 0.05, 0.10, 0.25, 1, 2, 5, 10, 20, 50, 100};
14 | int totalDenom = sizeof(denom) / sizeof(denom[0]);
15 |
16 | // Initialize result as a vector
17 | vector result;
18 |
19 | // Traverse through all denomination
20 | for (int i = totalDenom - 1; i >= 0; --i)
21 | {
22 | // Find denominations
23 | while (changingNominal >= denom[i])
24 | {
25 | changingNominal -= denom[i];
26 | result.push_back(denom[i]);
27 | }
28 |
29 | // If there's no any denomination
30 | // that can be given just exit the loop
31 | if (changingNominal < denom[0])
32 | break;
33 | }
34 |
35 | // Print result
36 | for (int i = 0; i < result.size(); ++i)
37 | cout << result[i] << " ";
38 | cout << endl;
39 | }
40 |
41 | int main()
42 | {
43 | cout << "Coin Change Problem" << endl;
44 |
45 | // Initialize the change nominal
46 | float change = 17.61;
47 |
48 | // Getting the minimal
49 | cout << "Minimal number of change for ";
50 | cout << change << " is " << endl;
51 | MinimalChangeCoin(change);
52 |
53 | return 0;
54 | }
55 |
--------------------------------------------------------------------------------
/Chapter09/Coin_Change_Problem/main.cpp:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/Chapter09/Fibonacci/Fibonacci.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter09/Fibonacci/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 | #include
3 |
4 | using namespace std;
5 |
6 | long long fib(int n)
7 | {
8 | if (n <= 1)
9 | return 1;
10 | else
11 | return fib(n - 1) + fib(n - 2);
12 | }
13 |
14 | long long fib2(int n)
15 | {
16 | if(n <= 1)
17 | return 1;
18 |
19 | long long last = 1;
20 | long long nextToLast = 1;
21 | long long answer = 1;
22 |
23 | for(int i = 2; i <=n; ++i)
24 | {
25 | answer = last + nextToLast;
26 | nextToLast = last;
27 | last = answer;
28 | }
29 |
30 | return answer;
31 | }
32 |
33 | int main()
34 | {
35 | cout << "Fibonacci" << endl;
36 |
37 | cout << fib2(10) << endl;
38 | return 0;
39 | }
40 |
--------------------------------------------------------------------------------
/Chapter09/Huffman_Coding/Huffman_Coding.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter09/Random_Number_Gеnеrаtоrѕ/Random_Number_Gеnеrаtоrѕ.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter09/Random_Number_Gеnеrаtоrѕ/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | static const int A = 1;
6 | static const int M = 10;
7 |
8 | class Random
9 | {
10 | private:
11 | int state;
12 |
13 | public:
14 | explicit Random(int initialValue = 1);
15 |
16 | int randomInt();
17 | double random0_1();
18 | int randomInt(int low, int high);
19 | };
20 |
21 | Random::Random(int initialValue)
22 | {
23 | if(initialValue < 0)
24 | initialValue += M;
25 |
26 | state = initialValue;
27 |
28 | if(state == 0)
29 | state = 1;
30 | }
31 |
32 | int Random::randomInt( )
33 | {
34 | return state = (A * state) % M;
35 | }
36 |
37 | double Random::random0_1( )
38 | {
39 | return static_cast(randomInt()) / M;
40 | }
41 |
42 | int main()
43 | {
44 | return 0;
45 | }
46 |
--------------------------------------------------------------------------------
/Chapter09/TicTacToe/TicTacToe.cbp:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
--------------------------------------------------------------------------------
/Chapter09/TicTacToe/main.cpp:
--------------------------------------------------------------------------------
1 | #include
2 |
3 | using namespace std;
4 |
5 | int main()
6 | {
7 | cout << "Hello world!" << endl;
8 | return 0;
9 | }
10 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2018 Packt
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # C++ Data Structures and Algorithms
2 | This is the code repository for [C++ Data Structures and Algorithms](https://www.packtpub.com/application-development/c-data-structures-and-algorithms?utm_source=github&utm_medium=repository&utm_campaign=9781788835213), published by [Packt](https://www.packtpub.com/?utm_source=github). It contains all the supporting project files necessary to work through the book from start to finish.
3 | ## About the Book
4 | C++ is a general-purpose programming language which has evolved over the years and is used to develop software for many different sectors. This book will be your companion as it takes you through implementing classic data structures and algorithms to help you get up and running as a confident C++ programmer.
5 |
6 |
7 | ## Instructions and Navigation
8 | All of the code is organized into folders. Each folder starts with a number followed by the application name. For example, Chapter02.
9 |
10 | All code files are present in their respective folder.
11 |
12 | The code will look like the following:
13 | ```
14 | // in_out.cpp
15 | #include
16 | int main ()
17 | {
18 | int i;
19 | std::cout << "Please enter an integer value: ";
20 | std::cin >> i;
21 | std::cout << "The value you entered is " << i;
22 | std::cout << "\n";
23 | return 0;
24 | }
25 | ```
26 |
27 | To get through this book and successfully complete all the source code examples, you will
28 | need the following specifications:
29 | * Desktop PC or Notebook with Windows, Linux, or macOS
30 | * GNU GCC v5.4.0 or above
31 | * Code::Block IDE v17.12 (for Windows and Linux OS) or Code::Block IDE v13.12
32 | (for macOS)
33 |
34 | ## Related Products
35 | * [C++ High Performance](https://www.packtpub.com/application-development/c-high-performance?utm_source=github&utm_medium=repository&utm_campaign=9781787120952)
36 |
37 | * [Mastering the C++17 STL](https://www.packtpub.com/application-development/mastering-c17-stl?utm_source=github&utm_medium=repository&utm_campaign=9781787126824)
38 |
39 | * [Learning C++ Functional Programming](https://www.packtpub.com/application-development/learning-c-functional-programming?utm_source=github&utm_medium=repository&utm_campaign=9781787281974)
40 |
--------------------------------------------------------------------------------