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

+41 -44
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,11 +104,17 @@ 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 << " ";
auto popped = q.pop();
if (popped.has_value())
{
std::cout << popped.value() << " ";
}
else
{
std::cout << "Queue is empty!" << std::endl;
}
}
std::cout << std::endl;
@@ -100,33 +125,5 @@ int main(int argc, char const *argv[])
}
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;
}
}
+69 -34
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;
}
}