ccoommmmiitt 33 iinn ccpppp
This commit is contained in:
1 parent
32af7721e0
commit
5ebfed9781
5 files changed
+262
-76
No files matched your search
Vendored
+2
-1
@@ -50,6 +50,7 @@
|
||||
"xtr1common": "cpp",
|
||||
"xutility": "cpp",
|
||||
"vector": "cpp",
|
||||
"list": "cpp"
|
||||
"list": "cpp",
|
||||
"optional": "cpp"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include <iostream>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
|
||||
class node
|
||||
{
|
||||
public:
|
||||
int data;
|
||||
std::unique_ptr<node> next;
|
||||
|
||||
node(int data) : data(data), next(nullptr) {}
|
||||
node(int data, std::unique_ptr<node> next) : data(data), next(std::move(next)) {}
|
||||
~node() {}
|
||||
};
|
||||
class linked_list
|
||||
{
|
||||
public:
|
||||
std::unique_ptr<node> head;
|
||||
linked_list() : head(nullptr) {}
|
||||
|
||||
linked_list(std::vector<int> v) : head(nullptr)
|
||||
{
|
||||
for (int i : v)
|
||||
{
|
||||
append(i);
|
||||
}
|
||||
}
|
||||
|
||||
linked_list(std::initializer_list<int> v) : head(nullptr)
|
||||
{
|
||||
for (int i : v)
|
||||
{
|
||||
append(i);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void append(int x) // prepend
|
||||
{
|
||||
auto temp = std::make_unique<node>(x, std::move(head));
|
||||
head = std::move(temp);
|
||||
}
|
||||
/// Pops the head of the linked list and returns the value in x.
|
||||
///
|
||||
/// If the linked list is empty, returns false. Otherwise, returns true.
|
||||
/// @param x The value popped from the linked list, if not empty.
|
||||
/// @return true if a value was popped, false if the list was empty.
|
||||
std::optional<int> pop()
|
||||
{
|
||||
if (!head)
|
||||
return std::nullopt;
|
||||
int value = head->data;
|
||||
head = std::move(head->next);
|
||||
return value;
|
||||
}
|
||||
|
||||
/// Initialize a linked list with a vector of values
|
||||
///
|
||||
/// The linked list is created by prepending each item in the vector
|
||||
/// to the list in order. The resulting list is a linked list where
|
||||
/// each node contains one item from the vector, and the order of the
|
||||
/// items in the vector is preserved in the linked list.
|
||||
linked_list &add(std::vector<int> &v)
|
||||
{
|
||||
for (int i : v)
|
||||
{
|
||||
append(i);
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
node *tail() const
|
||||
{
|
||||
node *temp = head.get();
|
||||
while (temp->next != nullptr)
|
||||
{
|
||||
temp = temp->next.get();
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
linked_list &add(const linked_list &l)
|
||||
{
|
||||
std::vector<int> v = l.view();
|
||||
while (v.size() > 0)
|
||||
{
|
||||
append(v.back());
|
||||
v.pop_back();
|
||||
}
|
||||
// delete v?
|
||||
|
||||
return *this;
|
||||
}
|
||||
linked_list &add(std::initializer_list<int> l)
|
||||
{
|
||||
for (int i : l)
|
||||
{
|
||||
append(i);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
std::vector<int> view() const
|
||||
{
|
||||
std::vector<int> ret;
|
||||
node *temp = head.get();
|
||||
while (temp != nullptr)
|
||||
{
|
||||
ret.push_back(temp->data);
|
||||
temp = temp->next.get();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
friend std::ostream &operator<<(std::ostream &os, const linked_list &list)
|
||||
{
|
||||
node *temp = list.head.get();
|
||||
while (temp)
|
||||
{
|
||||
os << temp->data << " -> ";
|
||||
temp = temp->next.get();
|
||||
}
|
||||
os << "nullptr";
|
||||
return os;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
linked_list l;
|
||||
l.append(1);
|
||||
l.append(2);
|
||||
l.append(3);
|
||||
l.append(4);
|
||||
l.append(5);
|
||||
std::cout << l << std::endl;
|
||||
int x = l.pop().value();
|
||||
std::cout << x << std::endl;
|
||||
linked_list l2 = {6, 7, 8, 9};
|
||||
l.add(l2);
|
||||
l2.add({17, 18, 19, 20, 21});
|
||||
std::cout << l2 << std::endl;
|
||||
std::cout << l << std::endl;
|
||||
std::vector v = l.view();
|
||||
for (int i : v)
|
||||
{
|
||||
std::cout << i << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
v = l2.view();
|
||||
for (int i : v)
|
||||
{
|
||||
std::cout << i << " ";
|
||||
}
|
||||
std::cout << "\n";
|
||||
return 0;
|
||||
}
|
||||
+14
-6
@@ -7,25 +7,33 @@ private:
|
||||
int arr[cap];
|
||||
int front = 0;
|
||||
int back = 0;
|
||||
|
||||
public:
|
||||
int size = 0;
|
||||
|
||||
void push(int x)
|
||||
public:
|
||||
int get_size() { return size; }
|
||||
/**
|
||||
* Adds an element to the back of the queue.
|
||||
*
|
||||
* @param x The element to be added.
|
||||
* @return Returns 0 if the element was successfully added, or 1 if the queue is full.
|
||||
*/
|
||||
bool push(int x)
|
||||
{
|
||||
if (size == cap)
|
||||
return;
|
||||
return 1;
|
||||
arr[back] = x;
|
||||
back = (back + 1) % cap;
|
||||
size++;
|
||||
return 0;
|
||||
}
|
||||
void pop(int *put)
|
||||
bool pop(int *put)
|
||||
{
|
||||
if (size == 0)
|
||||
return;
|
||||
return 1;
|
||||
*put = arr[front];
|
||||
front = (front + 1) % cap;
|
||||
size--;
|
||||
return 0;
|
||||
}
|
||||
std::vector<int> view()
|
||||
{
|
||||
|
||||
+11
-5
@@ -6,23 +6,29 @@ private:
|
||||
int top;
|
||||
|
||||
public:
|
||||
lda_stack() { top = 0;
|
||||
for (int i = 0; i < 60; i++) stack[i] = 0;
|
||||
}
|
||||
int push(int x)
|
||||
lda_stack(): top(0) {}
|
||||
bool push(int x)
|
||||
{
|
||||
if (top >= 60)
|
||||
return 1;
|
||||
stack[top++] = x;
|
||||
return 0;
|
||||
}
|
||||
int pop(int *put)
|
||||
bool pop(int *put)
|
||||
{
|
||||
if (top == 0)
|
||||
return 1;
|
||||
*put = stack[--top];
|
||||
return 0;
|
||||
}
|
||||
bool peek(int *put)
|
||||
{
|
||||
if (top == 0)
|
||||
return 1;
|
||||
*put = stack[top - 1];
|
||||
return 0;
|
||||
}
|
||||
|
||||
int size() { return top; } // top is the size of the stack, not the index
|
||||
};
|
||||
|
||||
|
||||
+79
-64
@@ -1,102 +1,117 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
|
||||
int log2int(int n){
|
||||
if (n <= 1){
|
||||
return 0; // no support + n is int anyway
|
||||
}
|
||||
return 1 + log2int(n / 2);
|
||||
}
|
||||
class binary_tree
|
||||
{
|
||||
public:
|
||||
binary_tree() {}
|
||||
|
||||
void append(int x)
|
||||
{
|
||||
data.push_back(x);
|
||||
maintain_shape(0);
|
||||
bubble_up(data.size() - 1);
|
||||
}
|
||||
void maintain_shape(int ind)
|
||||
|
||||
void add(std::vector<int> add)
|
||||
{
|
||||
int left = 2 * ind + 1;
|
||||
int right = 2 * ind + 2; // damn this is accounted for 0 indexing gj ai
|
||||
if (left > data.size())
|
||||
{ // 100% leaf
|
||||
return;
|
||||
}
|
||||
if (right > data.size())
|
||||
{ // only node is left, and that node is also last ele in data
|
||||
if (data[ind] < data[left])
|
||||
for (int x : add)
|
||||
{
|
||||
std::swap(data[ind], data[left]); // bigger higher
|
||||
append(x);
|
||||
}
|
||||
return;
|
||||
}
|
||||
maintain_shape(left);
|
||||
maintain_shape(right);
|
||||
// ordering? so the last item at index data.size() - 1 isnt swapped last: may lead to problem??? idk
|
||||
if (data[ind] < data[left])
|
||||
std::swap(data[ind], data[left]);
|
||||
if (data[ind] < data[right])
|
||||
std::swap(data[ind], data[right]);
|
||||
}
|
||||
int pop() {
|
||||
|
||||
int pop()
|
||||
{
|
||||
if (data.empty())
|
||||
throw std::runtime_error("Heap is empty");
|
||||
int ret = data[0];
|
||||
data[0] = data[data.size() - 1];
|
||||
data[0] = data.back();
|
||||
data.pop_back();
|
||||
maintain_shape(0);
|
||||
if (!data.empty())
|
||||
sift_down(0);
|
||||
return ret;
|
||||
}
|
||||
void add(std::vector<int> add) {
|
||||
for (int i = 0; i < add.size(); i++) {
|
||||
data.push_back(add[i]);
|
||||
}
|
||||
maintain_shape(0);
|
||||
}
|
||||
void print() {
|
||||
// insane sauce:
|
||||
int twos = 0;
|
||||
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
if (log2int(i + 1) == twos + 1) {
|
||||
std::cout << std::endl;
|
||||
twos++;
|
||||
void print()
|
||||
{
|
||||
int levels = 0;
|
||||
for (int i = 0; i < data.size(); i++)
|
||||
{
|
||||
if (log2int(i + 1) == levels + 1)
|
||||
{
|
||||
std::cout << "\n";
|
||||
levels++;
|
||||
}
|
||||
std::cout << data[i] << " ";
|
||||
}
|
||||
std::cout << std::endl;
|
||||
std::cout << "\n";
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<int> data;
|
||||
};
|
||||
|
||||
int log2int(int n)
|
||||
{
|
||||
int log = 0;
|
||||
while (n > 1)
|
||||
{
|
||||
n /= 2;
|
||||
log++;
|
||||
}
|
||||
return log;
|
||||
}
|
||||
|
||||
void bubble_up(int idx)
|
||||
{
|
||||
while (idx > 0)
|
||||
{
|
||||
int parent = (idx - 1) / 2;
|
||||
if (data[parent] >= data[idx])
|
||||
break;
|
||||
std::swap(data[parent], data[idx]);
|
||||
idx = parent;
|
||||
}
|
||||
}
|
||||
|
||||
void sift_down(int idx)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
int left = 2 * idx + 1;
|
||||
int right = 2 * idx + 2;
|
||||
int largest = idx;
|
||||
|
||||
if (left < data.size() && data[left] > data[largest])
|
||||
largest = left;
|
||||
if (right < data.size() && data[right] > data[largest])
|
||||
largest = right;
|
||||
|
||||
if (largest == idx)
|
||||
break;
|
||||
|
||||
std::swap(data[idx], data[largest]);
|
||||
idx = largest;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char const *argv[])
|
||||
{
|
||||
binary_tree bt;
|
||||
bt.append(1);
|
||||
bt.append(2);
|
||||
bt.append(6);
|
||||
std::cout << "\n";
|
||||
bt.print();
|
||||
bt.append(4);
|
||||
bt.append(10);
|
||||
bt.append(20);
|
||||
bt.append(5);
|
||||
std::cout << "\n";
|
||||
bt.print();
|
||||
bt.append(9);
|
||||
std::cout << "\n";
|
||||
bt.print();
|
||||
bt.append(8);
|
||||
std::cout << "\n";
|
||||
bt.append(6);
|
||||
bt.append(15);
|
||||
bt.print();
|
||||
|
||||
int popped = bt.pop();
|
||||
std::cout << "popped: " << popped << std::endl;
|
||||
std::cout << "Popped: " << popped << "\n";
|
||||
bt.print();
|
||||
popped = bt.pop();
|
||||
std::cout << "popped: " << popped << std::endl;
|
||||
bt.print();
|
||||
bt.add({6,5,11, 8, 10,15,3,7,1,12,4, 9});
|
||||
std::cout << "\n";
|
||||
|
||||
bt.add({50, 40, 60});
|
||||
bt.print();
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user