-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpre_decimalStack.cpp
More file actions
45 lines (40 loc) · 985 Bytes
/
pre_decimalStack.cpp
File metadata and controls
45 lines (40 loc) · 985 Bytes
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
#include <iostream>
#include "decimalStack.h"
using namespace std;
BinaryStack::BinaryStack() {
top = nullptr;
}
void BinaryStack::push(int val) {
StackNode* newNode;
newNode = new StackNode;
newNode->value = val;
newNode->next = top;
top = newNode;
}
int BinaryStack::pop() {
if (isEmpty()) {
cout << "The List is Empty.\n";
return -1;
}
int val = top->value;
StackNode* temp = top;
top = top->next;
delete temp;
return val;
}
bool BinaryStack::isEmpty() {
if (top == nullptr)
return true;
else return false;
}
BinaryStack::~BinaryStack() {
while (!isEmpty()) {
pop();
}
}
void BinaryStack::pileBinaryDigits(BinaryStack& stacklist, int num) {
if (num == 0) return; // Base case: stop recursion when num reaches 0
// Recursive step: push remainder and call function with quotient
pileBinaryDigits(stacklist, num / 2);
stacklist.push(num % 2); // Push binary digit onto the stack after recursive call
}