Skip to content

Commit 05dab77

Browse files
authored
Merge branch 'master' into various_improvements
2 parents f5b5e59 + 8476290 commit 05dab77

File tree

7 files changed

+124
-81
lines changed

7 files changed

+124
-81
lines changed

data_structures/stack.h

Lines changed: 57 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
1-
/* This class specifies the basic operation on a stack as a linked list */
1+
/**
2+
* @file stack.h
3+
* @author danghai
4+
* @brief This class specifies the basic operation on a stack as a linked list
5+
**/
26
#ifndef DATA_STRUCTURES_STACK_H_
37
#define DATA_STRUCTURES_STACK_H_
48

59
#include <cassert>
610
#include <iostream>
711

8-
/* Definition of the node */
12+
/** Definition of the node as a linked-list
13+
* \tparam Type type of data nodes of the linked list should contain
14+
*/
915
template <class Type>
1016
struct node {
11-
Type data;
12-
node<Type> *next;
17+
Type data; ///< data at current node
18+
node<Type> *next; ///< pointer to the next ::node instance
1319
};
1420

15-
/* Definition of the stack class */
21+
/** Definition of the stack class
22+
* \tparam Type type of data nodes of the linked list in the stack should
23+
* contain
24+
*/
1625
template <class Type>
1726
class stack {
1827
public:
1928
/** Show stack */
2029
void display() {
2130
node<Type> *current = stackTop;
2231
std::cout << "Top --> ";
23-
while (current != NULL) {
32+
while (current != nullptr) {
2433
std::cout << current->data << " ";
2534
current = current->next;
2635
}
@@ -30,15 +39,45 @@ class stack {
3039

3140
/** Default constructor*/
3241
stack() {
33-
stackTop = NULL;
42+
stackTop = nullptr;
3443
size = 0;
3544
}
3645

46+
/** Copy constructor*/
47+
explicit stack(const stack<Type> &otherStack) {
48+
node<Type> *newNode, *current, *last;
49+
50+
/* If stack is no empty, make it empty */
51+
if (stackTop != nullptr) {
52+
stackTop = nullptr;
53+
}
54+
if (otherStack.stackTop == nullptr) {
55+
stackTop = nullptr;
56+
} else {
57+
current = otherStack.stackTop;
58+
stackTop = new node<Type>;
59+
stackTop->data = current->data;
60+
stackTop->next = nullptr;
61+
last = stackTop;
62+
current = current->next;
63+
/* Copy the remaining stack */
64+
while (current != nullptr) {
65+
newNode = new node<Type>;
66+
newNode->data = current->data;
67+
newNode->next = nullptr;
68+
last->next = newNode;
69+
last = newNode;
70+
current = current->next;
71+
}
72+
}
73+
size = otherStack.size;
74+
}
75+
3776
/** Destructor */
3877
~stack() {}
3978

4079
/** Determine whether the stack is empty */
41-
bool isEmptyStack() { return (stackTop == NULL); }
80+
bool isEmptyStack() { return (stackTop == nullptr); }
4281

4382
/** Add new item to the stack */
4483
void push(Type item) {
@@ -52,7 +91,7 @@ class stack {
5291

5392
/** Return the top element of the stack */
5493
Type top() {
55-
assert(stackTop != NULL);
94+
assert(stackTop != nullptr);
5695
return stackTop->data;
5796
}
5897

@@ -70,30 +109,30 @@ class stack {
70109
}
71110

72111
/** Clear stack */
73-
void clear() { stackTop = NULL; }
112+
void clear() { stackTop = nullptr; }
74113

75114
/** Overload "=" the assignment operator */
76115
stack<Type> &operator=(const stack<Type> &otherStack) {
77116
node<Type> *newNode, *current, *last;
78117

79118
/* If stack is no empty, make it empty */
80-
if (stackTop != NULL) {
81-
stackTop = NULL;
119+
if (stackTop != nullptr) {
120+
stackTop = nullptr;
82121
}
83-
if (otherStack.stackTop == NULL) {
84-
stackTop = NULL;
122+
if (otherStack.stackTop == nullptr) {
123+
stackTop = nullptr;
85124
} else {
86125
current = otherStack.stackTop;
87126
stackTop = new node<Type>;
88127
stackTop->data = current->data;
89-
stackTop->next = NULL;
128+
stackTop->next = nullptr;
90129
last = stackTop;
91130
current = current->next;
92131
/* Copy the remaining stack */
93-
while (current != NULL) {
132+
while (current != nullptr) {
94133
newNode = new node<Type>;
95134
newNode->data = current->data;
96-
newNode->next = NULL;
135+
newNode->next = nullptr;
97136
last->next = newNode;
98137
last = newNode;
99138
current = current->next;
@@ -105,7 +144,7 @@ class stack {
105144

106145
private:
107146
node<Type> *stackTop; /**< Pointer to the stack */
108-
int size;
147+
int size; ///< size of stack
109148
};
110149

111150
#endif // DATA_STRUCTURES_STACK_H_

data_structures/stack_using_array.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
#include <iostream>
22

33
int *stack;
4-
int top = 0, stack_size;
4+
int stack_idx = 0, stack_size;
55

66
void push(int x) {
7-
if (top == stack_size) {
7+
if (stack_idx == stack_size) {
88
std::cout << "\nOverflow";
99
} else {
10-
stack[top++] = x;
10+
stack[stack_idx++] = x;
1111
}
1212
}
1313

1414
void pop() {
15-
if (top == 0) {
15+
if (stack_idx == 0) {
1616
std::cout << "\nUnderflow";
1717
} else {
18-
std::cout << "\n" << stack[--top] << " deleted";
18+
std::cout << "\n" << stack[--stack_idx] << " deleted";
1919
}
2020
}
2121

2222
void show() {
23-
for (int i = 0; i < top; i++) {
23+
for (int i = 0; i < stack_idx; i++) {
2424
std::cout << stack[i] << "\n";
2525
}
2626
}
2727

28-
void topmost() { std::cout << "\nTopmost element: " << stack[top - 1]; }
28+
void topmost() { std::cout << "\nTopmost element: " << stack[stack_idx - 1]; }
2929
int main() {
3030
std::cout << "\nEnter stack_size of stack : ";
3131
std::cin >> stack_size;
3232
stack = new int[stack_size];
3333
int ch, x;
3434
do {
35+
std::cout << "\n0. Exit";
3536
std::cout << "\n1. Push";
3637
std::cout << "\n2. Pop";
3738
std::cout << "\n3. Print";
@@ -51,5 +52,7 @@ int main() {
5152
}
5253
} while (ch != 0);
5354

55+
delete[] stack;
56+
5457
return 0;
5558
}

data_structures/stack_using_linked_list.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@ struct node {
55
node *next;
66
};
77

8-
node *top;
8+
node *top_var;
99

1010
void push(int x) {
1111
node *n = new node;
1212
n->val = x;
13-
n->next = top;
14-
top = n;
13+
n->next = top_var;
14+
top_var = n;
1515
}
1616

1717
void pop() {
18-
if (top == nullptr) {
18+
if (top_var == nullptr) {
1919
std::cout << "\nUnderflow";
2020
} else {
21-
node *t = top;
21+
node *t = top_var;
2222
std::cout << "\n" << t->val << " deleted";
23-
top = top->next;
23+
top_var = top_var->next;
2424
delete t;
2525
}
2626
}
2727

2828
void show() {
29-
node *t = top;
29+
node *t = top_var;
3030
while (t != nullptr) {
3131
std::cout << t->val << "\n";
3232
t = t->next;

math/fibonacci_fast.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,32 @@
1919
#include <cstdio>
2020
#include <iostream>
2121

22-
/** maximum number that can be computed - The result after 93 cannot be stored
23-
* in a `uint64_t` data type. */
24-
const uint64_t MAX = 93;
22+
/**
23+
* maximum number that can be computed - The result after 93 cannot be stored
24+
* in a `uint64_t` data type.
25+
*/
2526

26-
/** Array of computed fibonacci numbers */
27-
uint64_t f[MAX] = {0};
27+
#define MAX 93
2828

2929
/** Algorithm */
3030
uint64_t fib(uint64_t n) {
31-
if (n == 0)
31+
static uint64_t f1 = 1,
32+
f2 = 1; // using static keyword will retain the values of
33+
// f1 and f2 for the next function call.
34+
35+
if (n <= 2)
36+
return f2;
37+
if (n >= 93) {
38+
std::cerr
39+
<< "Cannot compute for n>93 due to limit of 64-bit integers\n";
3240
return 0;
33-
if (n == 1 || n == 2)
34-
return (f[n] = 1);
35-
36-
if (f[n])
37-
return f[n];
41+
}
3842

39-
uint64_t k = (n % 2 != 0) ? (n + 1) / 2 : n / 2;
43+
uint64_t temp = f2; // we do not need temp to be static
44+
f2 += f1;
45+
f1 = temp;
4046

41-
f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
42-
: (2 * fib(k - 1) + fib(k)) * fib(k);
43-
return f[n];
47+
return f2;
4448
}
4549

4650
/** Main function */

others/matrix_exponentiation.cpp

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,15 @@ using std::vector;
3939
#define pb push_back
4040
#define MOD 1000000007
4141

42-
/** returns absolute value */
43-
inline ll ab(ll x) { return x > 0LL ? x : -x; }
44-
45-
/** global variable k
42+
/** global variable mat_size
4643
* @todo @stepfencurryxiao add documetnation
4744
*/
48-
ll k;
45+
ll mat_size;
4946

50-
/** global vector variables
47+
/** global vector variables used in the ::ans function.
5148
* @todo @stepfencurryxiao add documetnation
5249
*/
53-
vector<ll> a, b, c;
50+
vector<ll> fib_b, fib_c;
5451

5552
/** To multiply 2 matrices
5653
* \param [in] A matrix 1 of size (m\f$\times\f$n)
@@ -59,10 +56,10 @@ vector<ll> a, b, c;
5956
*/
6057
vector<vector<ll>> multiply(const vector<vector<ll>> &A,
6158
const vector<vector<ll>> &B) {
62-
vector<vector<ll>> C(k + 1, vector<ll>(k + 1));
63-
for (ll i = 1; i <= k; i++) {
64-
for (ll j = 1; j <= k; j++) {
65-
for (ll z = 1; z <= k; z++) {
59+
vector<vector<ll>> C(mat_size + 1, vector<ll>(mat_size + 1));
60+
for (ll i = 1; i <= mat_size; i++) {
61+
for (ll j = 1; j <= mat_size; j++) {
62+
for (ll z = 1; z <= mat_size; z++) {
6663
C[i][j] = (C[i][j] + (A[i][z] * B[z][j]) % MOD) % MOD;
6764
}
6865
}
@@ -94,32 +91,32 @@ vector<vector<ll>> power(const vector<vector<ll>> &A, ll p) {
9491
ll ans(ll n) {
9592
if (n == 0)
9693
return 0;
97-
if (n <= k)
98-
return b[n - 1];
94+
if (n <= mat_size)
95+
return fib_b[n - 1];
9996
// F1
100-
vector<ll> F1(k + 1);
101-
for (ll i = 1; i <= k; i++) F1[i] = b[i - 1];
97+
vector<ll> F1(mat_size + 1);
98+
for (ll i = 1; i <= mat_size; i++) F1[i] = fib_b[i - 1];
10299

103100
// Transpose matrix
104-
vector<vector<ll>> T(k + 1, vector<ll>(k + 1));
105-
for (ll i = 1; i <= k; i++) {
106-
for (ll j = 1; j <= k; j++) {
107-
if (i < k) {
101+
vector<vector<ll>> T(mat_size + 1, vector<ll>(mat_size + 1));
102+
for (ll i = 1; i <= mat_size; i++) {
103+
for (ll j = 1; j <= mat_size; j++) {
104+
if (i < mat_size) {
108105
if (j == i + 1)
109106
T[i][j] = 1;
110107
else
111108
T[i][j] = 0;
112109
continue;
113110
}
114-
T[i][j] = c[k - j];
111+
T[i][j] = fib_c[mat_size - j];
115112
}
116113
}
117114
// T^n-1
118115
T = power(T, n - 1);
119116

120117
// T*F1
121118
ll res = 0;
122-
for (ll i = 1; i <= k; i++) {
119+
for (ll i = 1; i <= mat_size; i++) {
123120
res = (res + (T[1][i] * F1[i]) % MOD) % MOD;
124121
}
125122
return res;
@@ -133,19 +130,19 @@ int main() {
133130
cin >> t;
134131
ll i, j, x;
135132
while (t--) {
136-
cin >> k;
137-
for (i = 0; i < k; i++) {
133+
cin >> mat_size;
134+
for (i = 0; i < mat_size; i++) {
138135
cin >> x;
139-
b.pb(x);
136+
fib_b.pb(x);
140137
}
141-
for (i = 0; i < k; i++) {
138+
for (i = 0; i < mat_size; i++) {
142139
cin >> x;
143-
c.pb(x);
140+
fib_c.pb(x);
144141
}
145142
cin >> x;
146143
cout << ans(x) << endl;
147-
b.clear();
148-
c.clear();
144+
fib_b.clear();
145+
fib_c.clear();
149146
}
150147
return 0;
151148
}

0 commit comments

Comments
 (0)