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
+125 -95

No files matched your search

+5 -1
View File
@@ -51,6 +51,10 @@
"xutility": "cpp",
"vector": "cpp",
"list": "cpp",
"optional": "cpp"
"optional": "cpp",
"algorithm": "cpp",
"deque": "cpp",
"queue": "cpp",
"stack": "cpp"
}
}
+13 -19
View File
@@ -12,7 +12,7 @@ public:
node(int data) : data(data), next(nullptr) {}
node(int data, std::unique_ptr<node> next) : data(data), next(std::move(next)) {}
~node() {}
// ~node() {}
};
class linked_list
{
@@ -28,20 +28,14 @@ public:
}
}
linked_list(std::initializer_list<int> v) : head(nullptr)
{
for (int i : v)
{
append(i);
}
}
linked_list(std::initializer_list<int> v) : linked_list(std::vector<int>(v)) {}
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.
@@ -70,27 +64,27 @@ public:
}
return *this;
}
node *tail() const
{
node *temp = head.get();
if (!temp) return nullptr;
while (temp->next != nullptr)
{
temp = temp->next.get();
}
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)
{
append(v.back());
v.pop_back();
if (!l.head) return;
node* l_tail = l.tail(); // valid cuz check above
l_tail->next = std::move(head);
head = std::make_unique<node>(l.head);
// return *this;
}
// delete v?
return *this;
}
linked_list &add(std::initializer_list<int> l)
{
for (int i : l)
@@ -133,7 +127,7 @@ int main(int argc, char const *argv[])
l.append(4);
l.append(5);
std::cout << l << std::endl;
int x = l.pop().value();
int x = l.pop().value(); // should be 5
std::cout << x << std::endl;
linked_list l2 = {6, 7, 8, 9};
l.add(l2);
+39 -42
View File
@@ -1,5 +1,7 @@
#include <vector>
#include <iostream>
#include <optional>
class queue
{
private:
@@ -10,35 +12,47 @@ private:
int size = 0;
public:
int get_size() { return size; }
int get_size() const { 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.
* @return Returns true if the element was successfully added, false if the queue is full.
*/
bool push(int x)
{
if (size == cap)
return 1;
return false; // Queue is full
arr[back] = x;
back = (back + 1) % cap;
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)
return 1;
*put = arr[front];
return std::nullopt; // Queue is empty
int value = arr[front];
front = (front + 1) % cap;
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;
// front to back
for (int i = 0; i < size; i++)
{
ret.push_back(arr[(front + i) % cap]);
@@ -47,7 +61,7 @@ public:
}
};
int main(int argc, char const *argv[])
int main()
{
queue q;
q.push(1);
@@ -70,6 +84,7 @@ int main(int argc, char const *argv[])
q.push(18);
q.push(20);
q.push(20);
std::vector<int> v = q.view();
for (const int i : v)
{
@@ -77,7 +92,11 @@ int main(int argc, char const *argv[])
}
std::cout << std::endl;
q.push(21);
if (!q.push(21))
{
std::cout << "Queue is full!" << std::endl;
}
v = q.view();
for (const int i : v)
{
@@ -85,42 +104,20 @@ int main(int argc, char const *argv[])
}
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)
auto popped = q.pop();
if (popped.has_value())
{
std::cout << i << " ";
std::cout << popped.value() << " ";
}
else
{
std::cout << "Queue is empty!" << 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)
{
+68 -33
View File
@@ -1,52 +1,87 @@
#include <stdio.h>
#include <iostream>
#include <vector>
#include <optional>
class lda_stack
{
private:
int stack[60];
int top;
std::vector<int> stack;
size_t capacity;
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)
{
if (top >= 60)
return 1;
stack[top++] = x;
return 0;
}
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;
if (stack.size() >= capacity)
return false; // Stack is full
stack.push_back(x);
return true;
}
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;
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);
auto 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;
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;
}