mirror of
https://hub.njuu.cf/TheAlgorithms/C-Plus-Plus.git
synced 2023-10-11 13:05:55 +08:00
Merge pull request #890 from Panquesito7/lgtm_fixes
fix: Various LGTM fixes
This commit is contained in:
commit
46854f516e
@ -1,37 +1,38 @@
|
||||
#include <iostream>
|
||||
|
||||
int *stack;
|
||||
int top = 0, stack_size;
|
||||
int stack_idx = 0, stack_size;
|
||||
|
||||
void push(int x) {
|
||||
if (top == stack_size) {
|
||||
if (stack_idx == stack_size) {
|
||||
std::cout << "\nOverflow";
|
||||
} else {
|
||||
stack[top++] = x;
|
||||
stack[stack_idx++] = x;
|
||||
}
|
||||
}
|
||||
|
||||
void pop() {
|
||||
if (top == 0) {
|
||||
if (stack_idx == 0) {
|
||||
std::cout << "\nUnderflow";
|
||||
} else {
|
||||
std::cout << "\n" << stack[--top] << " deleted";
|
||||
std::cout << "\n" << stack[--stack_idx] << " deleted";
|
||||
}
|
||||
}
|
||||
|
||||
void show() {
|
||||
for (int i = 0; i < top; i++) {
|
||||
for (int i = 0; i < stack_idx; i++) {
|
||||
std::cout << stack[i] << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
void topmost() { std::cout << "\nTopmost element: " << stack[top - 1]; }
|
||||
void topmost() { std::cout << "\nTopmost element: " << stack[stack_idx - 1]; }
|
||||
int main() {
|
||||
std::cout << "\nEnter stack_size of stack : ";
|
||||
std::cin >> stack_size;
|
||||
stack = new int[stack_size];
|
||||
int ch, x;
|
||||
do {
|
||||
std::cout << "\n0. Exit";
|
||||
std::cout << "\n1. Push";
|
||||
std::cout << "\n2. Pop";
|
||||
std::cout << "\n3. Print";
|
||||
@ -51,5 +52,7 @@ int main() {
|
||||
}
|
||||
} while (ch != 0);
|
||||
|
||||
delete[] stack;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -1,35 +1,34 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
struct node {
|
||||
int val;
|
||||
node *next;
|
||||
};
|
||||
|
||||
node *top;
|
||||
node *top_var;
|
||||
|
||||
void push(int x) {
|
||||
node *n = new node;
|
||||
n->val = x;
|
||||
n->next = top;
|
||||
top = n;
|
||||
n->next = top_var;
|
||||
top_var = n;
|
||||
}
|
||||
|
||||
void pop() {
|
||||
if (top == NULL) {
|
||||
cout << "\nUnderflow";
|
||||
if (top_var == NULL) {
|
||||
std::cout << "\nUnderflow";
|
||||
} else {
|
||||
node *t = top;
|
||||
cout << "\n" << t->val << " deleted";
|
||||
top = top->next;
|
||||
node *t = top_var;
|
||||
std::cout << "\n" << t->val << " deleted";
|
||||
top_var = top_var->next;
|
||||
delete t;
|
||||
}
|
||||
}
|
||||
|
||||
void show() {
|
||||
node *t = top;
|
||||
node *t = top_var;
|
||||
while (t != NULL) {
|
||||
cout << t->val << "\n";
|
||||
std::cout << t->val << "\n";
|
||||
t = t->next;
|
||||
}
|
||||
}
|
||||
@ -37,14 +36,14 @@ void show() {
|
||||
int main() {
|
||||
int ch, x;
|
||||
do {
|
||||
cout << "\n1. Push";
|
||||
cout << "\n2. Pop";
|
||||
cout << "\n3. Print";
|
||||
cout << "\nEnter Your Choice : ";
|
||||
cin >> ch;
|
||||
std::cout << "\n1. Push";
|
||||
std::cout << "\n2. Pop";
|
||||
std::cout << "\n3. Print";
|
||||
std::cout << "\nEnter Your Choice : ";
|
||||
std::cin >> ch;
|
||||
if (ch == 1) {
|
||||
cout << "\nInsert : ";
|
||||
cin >> x;
|
||||
std::cout << "\nInsert : ";
|
||||
std::cin >> x;
|
||||
push(x);
|
||||
} else if (ch == 2) {
|
||||
pop();
|
||||
|
@ -19,28 +19,32 @@
|
||||
#include <cstdio>
|
||||
#include <iostream>
|
||||
|
||||
/** maximum number that can be computed - The result after 93 cannot be stored
|
||||
* in a `uint64_t` data type. */
|
||||
const uint64_t MAX = 93;
|
||||
/**
|
||||
* maximum number that can be computed - The result after 93 cannot be stored
|
||||
* in a `uint64_t` data type.
|
||||
*/
|
||||
|
||||
/** Array of computed fibonacci numbers */
|
||||
uint64_t f[MAX] = {0};
|
||||
#define MAX 93
|
||||
|
||||
/** Algorithm */
|
||||
uint64_t fib(uint64_t n) {
|
||||
if (n == 0)
|
||||
static uint64_t f1 = 1,
|
||||
f2 = 1; // using static keyword will retain the values of
|
||||
// f1 and f2 for the next function call.
|
||||
|
||||
if (n <= 2)
|
||||
return f2;
|
||||
if (n >= 93) {
|
||||
std::cerr
|
||||
<< "Cannot compute for n>93 due to limit of 64-bit integers\n";
|
||||
return 0;
|
||||
if (n == 1 || n == 2)
|
||||
return (f[n] = 1);
|
||||
}
|
||||
|
||||
if (f[n])
|
||||
return f[n];
|
||||
uint64_t temp = f2; // we do not need temp to be static
|
||||
f2 += f1;
|
||||
f1 = temp;
|
||||
|
||||
uint64_t k = (n % 2 != 0) ? (n + 1) / 2 : n / 2;
|
||||
|
||||
f[n] = (n % 2 != 0) ? (fib(k) * fib(k) + fib(k - 1) * fib(k - 1))
|
||||
: (2 * fib(k - 1) + fib(k)) * fib(k);
|
||||
return f[n];
|
||||
return f2;
|
||||
}
|
||||
|
||||
/** Main function */
|
||||
|
@ -20,13 +20,13 @@
|
||||
char stack[MAX];
|
||||
|
||||
//! pointer to track stack index
|
||||
int top = -1;
|
||||
int stack_idx = -1;
|
||||
|
||||
//! push byte to stack variable
|
||||
void push(char ch) { stack[++top] = ch; }
|
||||
void push(char ch) { stack[++stack_idx] = ch; }
|
||||
|
||||
//! pop a byte out of stack variable
|
||||
char pop() { return stack[top--]; }
|
||||
char pop() { return stack[stack_idx--]; }
|
||||
|
||||
//! @}-------------- end stack -----------
|
||||
|
||||
@ -56,7 +56,7 @@ int main() {
|
||||
while (valid == 1 && i < exp.length()) {
|
||||
if (exp[i] == '(' || exp[i] == '{' || exp[i] == '[' || exp[i] == '<') {
|
||||
push(exp[i]);
|
||||
} else if (top >= 0 && stack[top] == opening(exp[i])) {
|
||||
} else if (stack_idx >= 0 && stack[stack_idx] == opening(exp[i])) {
|
||||
pop();
|
||||
} else {
|
||||
valid = 0;
|
||||
@ -65,7 +65,7 @@ int main() {
|
||||
}
|
||||
|
||||
// makes sure the stack is empty after processsing (above)
|
||||
if (valid == 1 && top == -1) {
|
||||
if (valid == 1 && stack_idx == -1) {
|
||||
std::cout << "\nCorrect Expression";
|
||||
} else {
|
||||
std::cout << "\nWrong Expression";
|
||||
|
Loading…
Reference in New Issue
Block a user