this is so ass tho ngl

This commit is contained in:
lda committed 2025-06-08 19:24:38 +07:00
1 parent 2c87094755
commit a329f16cec
4 files changed
+181 -151

No files matched your search

+56 -52
View File
@@ -1,56 +1,60 @@
{ {
"files.associations": { "files.associations": {
"ostream": "cpp", "ostream": "cpp",
"atomic": "cpp", "atomic": "cpp",
"bit": "cpp", "bit": "cpp",
"cctype": "cpp", "cctype": "cpp",
"charconv": "cpp", "charconv": "cpp",
"clocale": "cpp", "clocale": "cpp",
"cmath": "cpp", "cmath": "cpp",
"compare": "cpp", "compare": "cpp",
"concepts": "cpp", "concepts": "cpp",
"cstddef": "cpp", "cstddef": "cpp",
"cstdint": "cpp", "cstdint": "cpp",
"cstdio": "cpp", "cstdio": "cpp",
"cstdlib": "cpp", "cstdlib": "cpp",
"cstring": "cpp", "cstring": "cpp",
"ctime": "cpp", "ctime": "cpp",
"cwchar": "cpp", "cwchar": "cpp",
"exception": "cpp", "exception": "cpp",
"format": "cpp", "format": "cpp",
"initializer_list": "cpp", "initializer_list": "cpp",
"ios": "cpp", "ios": "cpp",
"iosfwd": "cpp", "iosfwd": "cpp",
"iostream": "cpp", "iostream": "cpp",
"istream": "cpp", "istream": "cpp",
"iterator": "cpp", "iterator": "cpp",
"limits": "cpp", "limits": "cpp",
"locale": "cpp", "locale": "cpp",
"memory": "cpp", "memory": "cpp",
"new": "cpp", "new": "cpp",
"stdexcept": "cpp", "stdexcept": "cpp",
"streambuf": "cpp", "streambuf": "cpp",
"string": "cpp", "string": "cpp",
"system_error": "cpp", "system_error": "cpp",
"tuple": "cpp", "tuple": "cpp",
"type_traits": "cpp", "type_traits": "cpp",
"typeinfo": "cpp", "typeinfo": "cpp",
"utility": "cpp", "utility": "cpp",
"xfacet": "cpp", "xfacet": "cpp",
"xiosbase": "cpp", "xiosbase": "cpp",
"xlocale": "cpp", "xlocale": "cpp",
"xlocbuf": "cpp", "xlocbuf": "cpp",
"xlocinfo": "cpp", "xlocinfo": "cpp",
"xlocmes": "cpp", "xlocmes": "cpp",
"xlocmon": "cpp", "xlocmon": "cpp",
"xlocnum": "cpp", "xlocnum": "cpp",
"xloctime": "cpp", "xloctime": "cpp",
"xmemory": "cpp", "xmemory": "cpp",
"xstring": "cpp", "xstring": "cpp",
"xtr1common": "cpp", "xtr1common": "cpp",
"xutility": "cpp", "xutility": "cpp",
"vector": "cpp", "vector": "cpp",
"list": "cpp", "list": "cpp",
"optional": "cpp" "optional": "cpp",
"algorithm": "cpp",
"deque": "cpp",
"queue": "cpp",
"stack": "cpp"
} }
} }
+15 -21
View File
@@ -12,7 +12,7 @@ public:
node(int data) : data(data), next(nullptr) {} node(int data) : data(data), next(nullptr) {}
node(int data, std::unique_ptr<node> next) : data(data), next(std::move(next)) {} node(int data, std::unique_ptr<node> next) : data(data), next(std::move(next)) {}
~node() {} // ~node() {}
}; };
class linked_list class linked_list
{ {
@@ -28,20 +28,14 @@ public:
} }
} }
linked_list(std::initializer_list<int> v) : head(nullptr) linked_list(std::initializer_list<int> v) : linked_list(std::vector<int>(v)) {}
{
for (int i : v)
{
append(i);
}
}
void append(int x) // prepend void append(int x) // prepend
{ {
auto temp = std::make_unique<node>(x, std::move(head)); auto temp = std::make_unique<node>(x, std::move(head));
head = std::move(temp); head = std::move(temp);
} }
/// Pops the head of the linked list and returns the value in x. /// Pops the head of the linked list and returns the value in x.
/// ///
/// If the linked list is empty, returns false. Otherwise, returns true. /// If the linked list is empty, returns false. Otherwise, returns true.
@@ -70,27 +64,27 @@ public:
} }
return *this; return *this;
} }
node *tail() const node *tail() const
{ {
node *temp = head.get(); node *temp = head.get();
if (!temp) return nullptr;
while (temp->next != nullptr) while (temp->next != nullptr)
{ {
temp = temp->next.get(); temp = temp->next.get();
} }
return temp; return temp;
} }
linked_list &add(const linked_list &l) // what did i do to my argument
{ void add(const linked_list &l)
std::vector<int> v = l.view(); {
while (v.size() > 0) if (!l.head) return;
{ node* l_tail = l.tail(); // valid cuz check above
append(v.back()); l_tail->next = std::move(head);
v.pop_back(); head = std::make_unique<node>(l.head);
} // return *this;
// delete v?
return *this;
} }
linked_list &add(std::initializer_list<int> l) linked_list &add(std::initializer_list<int> l)
{ {
for (int i : l) for (int i : l)
@@ -133,7 +127,7 @@ int main(int argc, char const *argv[])
l.append(4); l.append(4);
l.append(5); l.append(5);
std::cout << l << std::endl; std::cout << l << std::endl;
int x = l.pop().value(); int x = l.pop().value(); // should be 5
std::cout << x << std::endl; std::cout << x << std::endl;
linked_list l2 = {6, 7, 8, 9}; linked_list l2 = {6, 7, 8, 9};
l.add(l2); l.add(l2);
+41 -44
View File
@@ -1,5 +1,7 @@
#include <vector> #include <vector>
#include <iostream> #include <iostream>
#include <optional>
class queue class queue
{ {
private: private:
@@ -10,35 +12,47 @@ private:
int size = 0; int size = 0;
public: public:
int get_size() { return size; } int get_size() const { return size; }
/** /**
* Adds an element to the back of the queue. * Adds an element to the back of the queue.
* *
* @param x The element to be added. * @param x The element to be added.
* @return Returns 0 if the element was successfully added, or 1 if the queue is full. * @return Returns true if the element was successfully added, false if the queue is full.
*/ */
bool push(int x) bool push(int x)
{ {
if (size == cap) if (size == cap)
return 1; return false; // Queue is full
arr[back] = x; arr[back] = x;
back = (back + 1) % cap; back = (back + 1) % cap;
size++; size++;
return 0; return true;
} }
bool pop(int *put)
/**
* Removes an element from the front of the queue.
*
* @return Returns an optional containing the popped value, or std::nullopt if the queue is empty.
*/
std::optional<int> pop()
{ {
if (size == 0) if (size == 0)
return 1; return std::nullopt; // Queue is empty
*put = arr[front]; int value = arr[front];
front = (front + 1) % cap; front = (front + 1) % cap;
size--; size--;
return 0; return value;
} }
std::vector<int> view()
/**
* Returns a vector containing the elements of the queue from front to back.
*
* @return A vector of integers representing the queue's contents.
*/
std::vector<int> view() const
{ {
std::vector<int> ret; std::vector<int> ret;
// front to back
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
{ {
ret.push_back(arr[(front + i) % cap]); ret.push_back(arr[(front + i) % cap]);
@@ -47,7 +61,7 @@ public:
} }
}; };
int main(int argc, char const *argv[]) int main()
{ {
queue q; queue q;
q.push(1); q.push(1);
@@ -70,6 +84,7 @@ int main(int argc, char const *argv[])
q.push(18); q.push(18);
q.push(20); q.push(20);
q.push(20); q.push(20);
std::vector<int> v = q.view(); std::vector<int> v = q.view();
for (const int i : v) for (const int i : v)
{ {
@@ -77,7 +92,11 @@ int main(int argc, char const *argv[])
} }
std::cout << std::endl; std::cout << std::endl;
q.push(21); if (!q.push(21))
{
std::cout << "Queue is full!" << std::endl;
}
v = q.view(); v = q.view();
for (const int i : v) for (const int i : v)
{ {
@@ -85,11 +104,17 @@ int main(int argc, char const *argv[])
} }
std::cout << std::endl; std::cout << std::endl;
int x;
for (int i = 0; i < 12; i++) for (int i = 0; i < 12; i++)
{ {
q.pop(&x); auto popped = q.pop();
std::cout << x << " "; if (popped.has_value())
{
std::cout << popped.value() << " ";
}
else
{
std::cout << "Queue is empty!" << std::endl;
}
} }
std::cout << std::endl; std::cout << std::endl;
@@ -100,33 +125,5 @@ int main(int argc, char const *argv[])
} }
std::cout << std::endl; 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; return 0;
} }
+69 -34
View File
@@ -1,52 +1,87 @@
#include <stdio.h> #include <iostream>
#include <vector>
#include <optional>
class lda_stack class lda_stack
{ {
private: private:
int stack[60]; std::vector<int> stack;
int top; size_t capacity;
public: public:
lda_stack(): top(0) {} lda_stack(size_t cap = 60) : capacity(cap) {}
/**
* Pushes an element onto the stack.
*
* @param x The element to be pushed.
* @return Returns true if the element was successfully pushed, false if the stack is full.
*/
bool push(int x) bool push(int x)
{ {
if (top >= 60) if (stack.size() >= capacity)
return 1; return false; // Stack is full
stack[top++] = x; stack.push_back(x);
return 0; return true;
}
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 /**
* Pops the top element from the stack.
*
* @return Returns an optional containing the popped value, or std::nullopt if the stack is empty.
*/
std::optional<int> pop()
{
if (stack.empty())
return std::nullopt; // Stack is empty
int value = stack.back();
stack.pop_back();
return value;
}
/**
* Peeks at the top element of the stack without removing it.
*
* @return Returns an optional containing the top value, or std::nullopt if the stack is empty.
*/
std::optional<int> peek() const
{
if (stack.empty())
return std::nullopt; // Stack is empty
return stack.back();
}
/**
* Returns the current size of the stack.
*
* @return The number of elements in the stack.
*/
size_t size() const { return stack.size(); }
}; };
int main(int argc, char const *argv[]) int main()
{ {
lda_stack s1; lda_stack s1;
s1.push(1); s1.push(1);
s1.push(2); s1.push(2);
s1.push(3); s1.push(3);
s1.push(4); s1.push(4);
int x;
s1.pop(&x); auto popped = s1.pop();
printf("%d\n", x); if (popped.has_value())
s1.pop(&x); std::cout << popped.value() << std::endl;
printf("%d\n", x);
s1.pop(&x); popped = s1.pop();
printf("%d\n", x); if (popped.has_value())
s1.pop(&x); std::cout << popped.value() << std::endl;
printf("%d\n", x);
popped = s1.pop();
if (popped.has_value())
std::cout << popped.value() << std::endl;
popped = s1.pop();
if (popped.has_value())
std::cout << popped.value() << std::endl;
return 0; return 0;
} }