ccoommmmiitt 33 iinn ccpppp

This commit is contained in:
lda committed 2024-12-06 08:39:21 +07:00
1 parent 32af7721e0
commit 5ebfed9781
5 files changed
+269 -83

No files matched your search

+14 -6
View File
@@ -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()
{
+16 -10
View File
@@ -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];
*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
};
@@ -35,12 +41,12 @@ int main(int argc, char const *argv[])
s1.push(4);
int x;
s1.pop(&x);
printf("%d\n", x);
printf("%d\n", x);
s1.pop(&x);
printf("%d\n", x);
printf("%d\n", x);
s1.pop(&x);
printf("%d\n", x);
printf("%d\n", x);
s1.pop(&x);
printf("%d\n", x);
printf("%d\n", x);
return 0;
}