commit 2 in cpp

This commit is contained in:
lda committed 2024-12-05 23:48:34 +07:00
1 parent 9e742f0d79
commit 32af7721e0
4 files changed
+275 -1

No files matched your search

+124
View File
@@ -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
View File
@@ -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;
}