commit 2 in cpp
This commit is contained in:
1 parent
9e742f0d79
commit
32af7721e0
4 files changed
+275
-1
No files matched your search
Vendored
+3
-1
@@ -48,6 +48,8 @@
|
|||||||
"xmemory": "cpp",
|
"xmemory": "cpp",
|
||||||
"xstring": "cpp",
|
"xstring": "cpp",
|
||||||
"xtr1common": "cpp",
|
"xtr1common": "cpp",
|
||||||
"xutility": "cpp"
|
"xutility": "cpp",
|
||||||
|
"vector": "cpp",
|
||||||
|
"list": "cpp"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+124
@@ -0,0 +1,124 @@
|
|||||||
|
#include <vector>
|
||||||
|
#include <iostream>
|
||||||
|
class queue
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
static const int cap = 20;
|
||||||
|
int arr[cap];
|
||||||
|
int front = 0;
|
||||||
|
int back = 0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
int size = 0;
|
||||||
|
|
||||||
|
void push(int x)
|
||||||
|
{
|
||||||
|
if (size == cap)
|
||||||
|
return;
|
||||||
|
arr[back] = x;
|
||||||
|
back = (back + 1) % cap;
|
||||||
|
size++;
|
||||||
|
}
|
||||||
|
void pop(int *put)
|
||||||
|
{
|
||||||
|
if (size == 0)
|
||||||
|
return;
|
||||||
|
*put = arr[front];
|
||||||
|
front = (front + 1) % cap;
|
||||||
|
size--;
|
||||||
|
}
|
||||||
|
std::vector<int> view()
|
||||||
|
{
|
||||||
|
std::vector<int> ret;
|
||||||
|
// front to back
|
||||||
|
for (int i = 0; i < size; i++)
|
||||||
|
{
|
||||||
|
ret.push_back(arr[(front + i) % cap]);
|
||||||
|
}
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, char const *argv[])
|
||||||
|
{
|
||||||
|
queue q;
|
||||||
|
q.push(1);
|
||||||
|
q.push(2);
|
||||||
|
q.push(14);
|
||||||
|
q.push(2);
|
||||||
|
q.push(5);
|
||||||
|
q.push(674);
|
||||||
|
q.push(7);
|
||||||
|
q.push(5563);
|
||||||
|
q.push(9);
|
||||||
|
q.push(66666);
|
||||||
|
q.push(1111);
|
||||||
|
q.push(12);
|
||||||
|
q.push(13);
|
||||||
|
q.push(1440);
|
||||||
|
q.push(15);
|
||||||
|
q.push(16);
|
||||||
|
q.push(1778);
|
||||||
|
q.push(18);
|
||||||
|
q.push(20);
|
||||||
|
q.push(20);
|
||||||
|
std::vector<int> v = q.view();
|
||||||
|
for (const int i : v)
|
||||||
|
{
|
||||||
|
std::cout << i << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
q.push(21);
|
||||||
|
v = q.view();
|
||||||
|
for (const int i : v)
|
||||||
|
{
|
||||||
|
std::cout << i << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
int x;
|
||||||
|
for (int i = 0; i < 12; i++)
|
||||||
|
{
|
||||||
|
q.pop(&x);
|
||||||
|
std::cout << x << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
v = q.view();
|
||||||
|
for (const int i : v)
|
||||||
|
{
|
||||||
|
std::cout << i << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
q.push(21);
|
||||||
|
q.push(22);
|
||||||
|
q.push(23);
|
||||||
|
q.push(24);
|
||||||
|
q.push(25);
|
||||||
|
q.push(26);
|
||||||
|
q.push(27);
|
||||||
|
q.push(28);
|
||||||
|
q.push(29);
|
||||||
|
q.push(30);
|
||||||
|
q.push(31);
|
||||||
|
q.push(32);
|
||||||
|
q.push(33);
|
||||||
|
q.push(34);
|
||||||
|
q.push(35);
|
||||||
|
q.push(36);
|
||||||
|
q.push(37);
|
||||||
|
q.push(38);
|
||||||
|
q.push(39);
|
||||||
|
q.push(40);
|
||||||
|
|
||||||
|
v = q.view();
|
||||||
|
for (const int i : v)
|
||||||
|
{
|
||||||
|
std::cout << i << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
+46
@@ -0,0 +1,46 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
class lda_stack
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
int stack[60];
|
||||||
|
int top;
|
||||||
|
|
||||||
|
public:
|
||||||
|
lda_stack() { top = 0;
|
||||||
|
for (int i = 0; i < 60; i++) stack[i] = 0;
|
||||||
|
}
|
||||||
|
int push(int x)
|
||||||
|
{
|
||||||
|
if (top >= 60)
|
||||||
|
return 1;
|
||||||
|
stack[top++] = x;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int pop(int *put)
|
||||||
|
{
|
||||||
|
if (top == 0)
|
||||||
|
return 1;
|
||||||
|
*put = stack[--top];
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
int size() { return top; } // top is the size of the stack, not the index
|
||||||
|
};
|
||||||
|
|
||||||
|
int main(int argc, char const *argv[])
|
||||||
|
{
|
||||||
|
lda_stack s1;
|
||||||
|
s1.push(1);
|
||||||
|
s1.push(2);
|
||||||
|
s1.push(3);
|
||||||
|
s1.push(4);
|
||||||
|
int x;
|
||||||
|
s1.pop(&x);
|
||||||
|
printf("%d\n", x);
|
||||||
|
s1.pop(&x);
|
||||||
|
printf("%d\n", x);
|
||||||
|
s1.pop(&x);
|
||||||
|
printf("%d\n", x);
|
||||||
|
s1.pop(&x);
|
||||||
|
printf("%d\n", x);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
@@ -0,0 +1,102 @@
|
|||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
void maintain_shape(int ind)
|
||||||
|
{
|
||||||
|
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])
|
||||||
|
{
|
||||||
|
std::swap(data[ind], data[left]); // bigger higher
|
||||||
|
}
|
||||||
|
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 ret = data[0];
|
||||||
|
data[0] = data[data.size() - 1];
|
||||||
|
data.pop_back();
|
||||||
|
maintain_shape(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++;
|
||||||
|
}
|
||||||
|
std::cout << data[i] << " ";
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::vector<int> data;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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(5);
|
||||||
|
std::cout << "\n";
|
||||||
|
bt.print();
|
||||||
|
bt.append(9);
|
||||||
|
std::cout << "\n";
|
||||||
|
bt.print();
|
||||||
|
bt.append(8);
|
||||||
|
std::cout << "\n";
|
||||||
|
bt.print();
|
||||||
|
int popped = bt.pop();
|
||||||
|
std::cout << "popped: " << popped << std::endl;
|
||||||
|
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.print();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in new issue
Block a user